blob: 53447afb9bb1364ea8478a2cee52e0ab321924d3 [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 */
480 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000481 slang_T *su_sallang; /* default language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000482} suginfo_T;
483
484/* One word suggestion. Used in "si_ga". */
485typedef struct suggest_S
486{
487 char_u *st_word; /* suggested word, allocated string */
488 int st_orglen; /* length of replaced text */
489 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000490 int st_altscore; /* used when st_score compares equal */
491 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000492 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000493 slang_T *st_slang; /* language used for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000494} suggest_T;
495
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000496#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000497
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000498/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
499 * called the score may change, thus we need to keep more than what is
500 * displayed. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000501#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 50 ? 50 : (su)->su_maxcount)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000502
503/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
504 * of suggestions that are not going to be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000505#define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000506
507/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000508#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000509#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000510#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000511#define SCORE_RARE 180 /* rare word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000512#define SCORE_SWAP 90 /* swap two characters */
513#define SCORE_SWAP3 110 /* swap two characters in three */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000514#define SCORE_REP 65 /* REP replacement */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000515#define SCORE_SUBST 93 /* substitute a character */
516#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000517#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000518#define SCORE_DEL 94 /* delete a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000519#define SCORE_DELDUP 66 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000520#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000521#define SCORE_INS 96 /* insert a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000522#define SCORE_INSDUP 67 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000523#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000524#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000525
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000526#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000527#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
528 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000529
530#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000531#define SCORE_MAXMAX 999999 /* accept any score */
532
533/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000534 * Structure to store info for word matching.
535 */
536typedef struct matchinf_S
537{
538 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000539
540 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000541 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000542 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000543 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000544 char_u *mi_cend; /* char after what was used for
545 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000546
547 /* case-folded text */
548 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000549 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000550
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000551 /* for when checking word after a prefix */
552 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000553 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000554 int mi_prefcnt; /* number of entries at mi_prefarridx */
555 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000556#ifdef FEAT_MBYTE
557 int mi_cprefixlen; /* byte length of prefix in original
558 case */
559#else
560# define mi_cprefixlen mi_prefixlen /* it's the same value */
561#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000562
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000563 /* for when checking a compound word */
564 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000565 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
566 int mi_complen; /* nr of compound words used */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000567
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000568 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000569 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000570 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000571 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000572
573 /* for NOBREAK */
574 int mi_result2; /* "mi_resul" without following word */
575 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000576} matchinf_T;
577
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000578/*
579 * The tables used for recognizing word characters according to spelling.
580 * These are only used for the first 256 characters of 'encoding'.
581 */
582typedef struct spelltab_S
583{
584 char_u st_isw[256]; /* flags: is word char */
585 char_u st_isu[256]; /* flags: is uppercase char */
586 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000587 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000588} spelltab_T;
589
590static spelltab_T spelltab;
591static int did_set_spelltab;
592
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000593#define CF_WORD 0x01
594#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000595
596static void clear_spell_chartab __ARGS((spelltab_T *sp));
597static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000598static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
599static int spell_iswordp_nmw __ARGS((char_u *p));
600#ifdef FEAT_MBYTE
601static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
602#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000603static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000604
605/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000606 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000607 */
608typedef enum
609{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000610 STATE_START = 0, /* At start of node check for NUL bytes (goodword
611 * ends); if badword ends there is a match, otherwise
612 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000613 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000614 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000615 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
616 STATE_PLAIN, /* Use each byte of the node. */
617 STATE_DEL, /* Delete a byte from the bad word. */
618 STATE_INS, /* Insert a byte in the bad word. */
619 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000620 STATE_UNSWAP, /* Undo swap two characters. */
621 STATE_SWAP3, /* Swap two characters over three. */
622 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000623 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000624 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000625 STATE_REP_INI, /* Prepare for using REP items. */
626 STATE_REP, /* Use matching REP items from the .aff file. */
627 STATE_REP_UNDO, /* Undo a REP item replacement. */
628 STATE_FINAL /* End of this node. */
629} state_T;
630
631/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000632 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000633 */
634typedef struct trystate_S
635{
Bram Moolenaarea424162005-06-16 21:51:00 +0000636 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000637 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000638 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000639 short ts_curi; /* index in list of child nodes */
640 char_u ts_fidx; /* index in fword[], case-folded bad word */
641 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
642 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000643 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000644 * PFD_PREFIXTREE or PFD_NOPREFIX */
645 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000646#ifdef FEAT_MBYTE
647 char_u ts_tcharlen; /* number of bytes in tword character */
648 char_u ts_tcharidx; /* current byte index in tword character */
649 char_u ts_isdiff; /* DIFF_ values */
650 char_u ts_fcharstart; /* index in fword where badword char started */
651#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000652 char_u ts_prewordlen; /* length of word in "preword[]" */
653 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000654 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000655 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000656 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000657 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000658} trystate_T;
659
Bram Moolenaarea424162005-06-16 21:51:00 +0000660/* values for ts_isdiff */
661#define DIFF_NONE 0 /* no different byte (yet) */
662#define DIFF_YES 1 /* different byte found */
663#define DIFF_INSERT 2 /* inserting character */
664
Bram Moolenaard12a1322005-08-21 22:08:24 +0000665/* values for ts_flags */
666#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
667#define TSF_DIDSPLIT 2 /* tried split at this point */
668
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000669/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000670#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000671#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
672#define PFD_NOTSPECIAL 0xfd /* first value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000673
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000674/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000675#define FIND_FOLDWORD 0 /* find word case-folded */
676#define FIND_KEEPWORD 1 /* find keep-case word */
677#define FIND_PREFIX 2 /* find word after prefix */
678#define FIND_COMPOUND 3 /* find case-folded compound word */
679#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000680
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000681static slang_T *slang_alloc __ARGS((char_u *lang));
682static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000683static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000684static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000685static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000686static 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 +0000687static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000688static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000689static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000690static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000691static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000692static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000693static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000694static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000695static 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 +0000696static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000697static char_u *read_string __ARGS((FILE *fd, int cnt));
698static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
699static int read_charflags_section __ARGS((FILE *fd));
700static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
701static int read_rep_section __ARGS((FILE *fd, slang_T *slang));
702static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
703static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
704static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000705static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000706static int init_syl_tab __ARGS((slang_T *slang));
707static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000708static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
709static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000710#ifdef FEAT_MBYTE
711static int *mb_str2wide __ARGS((char_u *s));
712#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000713static 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 +0000714static void clear_midword __ARGS((buf_T *buf));
715static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000716static int find_region __ARGS((char_u *rp, char_u *region));
717static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000718static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000719static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000720static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000721static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000722static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000723static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +0000724static 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 +0000725#ifdef FEAT_EVAL
726static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
727#endif
728static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
729static void spell_suggest_intern __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000730static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000731static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000732static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000733static void suggest_try_special __ARGS((suginfo_T *su));
734static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000735static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000736#ifdef FEAT_MBYTE
737static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
738#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000739static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000740static void score_comp_sal __ARGS((suginfo_T *su));
741static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000742static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000743static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000744static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000745static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000746static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000747static 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 +0000748static void add_banned __ARGS((suginfo_T *su, char_u *word));
749static int was_banned __ARGS((suginfo_T *su, char_u *word));
750static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000751static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000752static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000753static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
754static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
755static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000756#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000757static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000758#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000759static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000760static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000761static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000762static 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 +0000763
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000764/*
765 * Use our own character-case definitions, because the current locale may
766 * differ from what the .spl file uses.
767 * These must not be called with negative number!
768 */
769#ifndef FEAT_MBYTE
770/* Non-multi-byte implementation. */
771# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
772# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
773# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
774#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000775# if defined(HAVE_WCHAR_H)
776# include <wchar.h> /* for towupper() and towlower() */
777# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000778/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
779 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
780 * the "w" library function for characters above 255 if available. */
781# ifdef HAVE_TOWLOWER
782# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
783 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
784# else
785# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
786 : (c) < 256 ? spelltab.st_fold[c] : (c))
787# endif
788
789# ifdef HAVE_TOWUPPER
790# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
791 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
792# else
793# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
794 : (c) < 256 ? spelltab.st_upper[c] : (c))
795# endif
796
797# ifdef HAVE_ISWUPPER
798# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
799 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
800# else
801# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000802 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000803# endif
804#endif
805
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000806
807static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000808static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000809static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000810static char *e_affname = N_("Affix name too long in %s line %d: %s");
811static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
812static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000813static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000814
815/*
816 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000817 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000818 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
819 * or when it's OK it remains unchanged.
820 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000821 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000822 * "capcol" is used to check for a Capitalised word after the end of a
823 * sentence. If it's zero then perform the check. Return the column where to
824 * check next, or -1 when no sentence end was found. If it's NULL then don't
825 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000826 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000827 * Returns the length of the word in bytes, also when it's OK, so that the
828 * caller can skip over the word.
829 */
830 int
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000831spell_check(wp, ptr, attrp, capcol)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000832 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000833 char_u *ptr;
834 int *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000835 int *capcol; /* column to check for Capital */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000836{
837 matchinf_T mi; /* Most things are put in "mi" so that it can
838 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000839 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000840 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000841 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000842 int lpi;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000843
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000844 /* A word never starts at a space or a control character. Return quickly
845 * then, skipping over the character. */
846 if (*ptr <= ' ')
847 return 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000848 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000849
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000850 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000851 * 0X99FF. But when a word character follows do check spelling to find
852 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000853 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000854 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000855 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
856 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000857 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000858 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000859 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000860 nrlen = mi.mi_end - ptr;
861 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000862 if (!spell_iswordp(mi.mi_end, wp->w_buffer))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000863 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000864
865 /* Try including the digits in the word. */
866 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000867 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000868 else
869 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000870
Bram Moolenaar0c405862005-06-22 22:26:26 +0000871 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000872 mi.mi_word = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000873 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000874 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000875 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000876 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000877 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000878 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000879
880 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
881 {
882 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000883 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000884 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000885 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000886 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000887 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000888 if (capcol != NULL)
889 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000890
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000891 /* We always use the characters up to the next non-word character,
892 * also for bad words. */
893 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000894
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000895 /* Check caps type later. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000896 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000897
Bram Moolenaar5195e452005-08-19 20:32:47 +0000898 /* case-fold the word with one non-word character, so that we can check
899 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000900 if (*mi.mi_fend != NUL)
901 mb_ptr_adv(mi.mi_fend);
902
903 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
904 MAXWLEN + 1);
905 mi.mi_fwordlen = STRLEN(mi.mi_fword);
906
907 /* The word is bad unless we recognize it. */
908 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +0000909 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000910
911 /*
912 * Loop over the languages specified in 'spelllang'.
913 * We check them all, because a matching word may be longer than an
914 * already found matching word.
915 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000916 for (lpi = 0; lpi < wp->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000917 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000918 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, lpi);
919
920 /* If reloading fails the language is still in the list but everything
921 * has been cleared. */
922 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
923 continue;
924
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000925 /* Check for a matching word in case-folded words. */
926 find_word(&mi, FIND_FOLDWORD);
927
928 /* Check for a matching word in keep-case words. */
929 find_word(&mi, FIND_KEEPWORD);
930
931 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000932 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +0000933
934 /* For a NOBREAK language, may want to use a word without a following
935 * word as a backup. */
936 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
937 && mi.mi_result2 != SP_BAD)
938 {
939 mi.mi_result = mi.mi_result2;
940 mi.mi_end = mi.mi_end2;
941 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000942 }
943
944 if (mi.mi_result != SP_OK)
945 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000946 /* If we found a number skip over it. Allows for "42nd". Do flag
947 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000948 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000949 {
950 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
951 return nrlen;
952 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000953
954 /* When we are at a non-word character there is no error, just
955 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000956 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000957 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000958 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
959 {
960 regmatch_T regmatch;
961
962 /* Check for end of sentence. */
963 regmatch.regprog = wp->w_buffer->b_cap_prog;
964 regmatch.rm_ic = FALSE;
965 if (vim_regexec(&regmatch, ptr, 0))
966 *capcol = (int)(regmatch.endp[0] - ptr);
967 }
968
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000969#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000970 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000971 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000972#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000973 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000974 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000975 else if (mi.mi_end == ptr)
976 /* Always include at least one character. Required for when there
977 * is a mixup in "midword". */
978 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +0000979 else if (mi.mi_result == SP_BAD
980 && LANGP_ENTRY(wp->w_buffer->b_langp, 0)->lp_slang->sl_nobreak)
981 {
982 char_u *p, *fp;
983 int save_result = mi.mi_result;
984
985 /* First language in 'spelllang' is NOBREAK. Find first position
986 * at which any word would be valid. */
987 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000988 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +0000989 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000990 p = mi.mi_word;
991 fp = mi.mi_fword;
992 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +0000993 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000994 mb_ptr_adv(p);
995 mb_ptr_adv(fp);
996 if (p >= mi.mi_end)
997 break;
998 mi.mi_compoff = fp - mi.mi_fword;
999 find_word(&mi, FIND_COMPOUND);
1000 if (mi.mi_result != SP_BAD)
1001 {
1002 mi.mi_end = p;
1003 break;
1004 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001005 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001006 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001007 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001008 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001009
1010 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1011 *attrp = highlight_attr[HLF_SPB];
1012 else if (mi.mi_result == SP_RARE)
1013 *attrp = highlight_attr[HLF_SPR];
1014 else
1015 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001016 }
1017
Bram Moolenaar5195e452005-08-19 20:32:47 +00001018 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1019 {
1020 /* Report SpellCap only when the word isn't badly spelled. */
1021 *attrp = highlight_attr[HLF_SPC];
1022 return wrongcaplen;
1023 }
1024
Bram Moolenaar51485f02005-06-04 21:55:20 +00001025 return (int)(mi.mi_end - ptr);
1026}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001027
Bram Moolenaar51485f02005-06-04 21:55:20 +00001028/*
1029 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001030 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1031 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1032 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1033 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001034 *
1035 * For a match mip->mi_result is updated.
1036 */
1037 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001038find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001039 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001040 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001041{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001042 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001043 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001044 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001045 int endidxcnt = 0;
1046 int len;
1047 int wlen = 0;
1048 int flen;
1049 int c;
1050 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001051 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001052#ifdef FEAT_MBYTE
1053 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001054#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001055 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001056 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001057 slang_T *slang = mip->mi_lp->lp_slang;
1058 unsigned flags;
1059 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001060 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001061 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001062 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001063 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001064
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001065 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001066 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001067 /* Check for word with matching case in keep-case tree. */
1068 ptr = mip->mi_word;
1069 flen = 9999; /* no case folding, always enough bytes */
1070 byts = slang->sl_kbyts;
1071 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001072
1073 if (mode == FIND_KEEPCOMPOUND)
1074 /* Skip over the previously found word(s). */
1075 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001076 }
1077 else
1078 {
1079 /* Check for case-folded in case-folded tree. */
1080 ptr = mip->mi_fword;
1081 flen = mip->mi_fwordlen; /* available case-folded bytes */
1082 byts = slang->sl_fbyts;
1083 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001084
1085 if (mode == FIND_PREFIX)
1086 {
1087 /* Skip over the prefix. */
1088 wlen = mip->mi_prefixlen;
1089 flen -= mip->mi_prefixlen;
1090 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001091 else if (mode == FIND_COMPOUND)
1092 {
1093 /* Skip over the previously found word(s). */
1094 wlen = mip->mi_compoff;
1095 flen -= mip->mi_compoff;
1096 }
1097
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001098 }
1099
Bram Moolenaar51485f02005-06-04 21:55:20 +00001100 if (byts == NULL)
1101 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001102
Bram Moolenaar51485f02005-06-04 21:55:20 +00001103 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001104 * Repeat advancing in the tree until:
1105 * - there is a byte that doesn't match,
1106 * - we reach the end of the tree,
1107 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001108 */
1109 for (;;)
1110 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001111 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001112 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001113
1114 len = byts[arridx++];
1115
1116 /* If the first possible byte is a zero the word could end here.
1117 * Remember this index, we first check for the longest word. */
1118 if (byts[arridx] == 0)
1119 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001120 if (endidxcnt == MAXWLEN)
1121 {
1122 /* Must be a corrupted spell file. */
1123 EMSG(_(e_format));
1124 return;
1125 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001126 endlen[endidxcnt] = wlen;
1127 endidx[endidxcnt++] = arridx++;
1128 --len;
1129
1130 /* Skip over the zeros, there can be several flag/region
1131 * combinations. */
1132 while (len > 0 && byts[arridx] == 0)
1133 {
1134 ++arridx;
1135 --len;
1136 }
1137 if (len == 0)
1138 break; /* no children, word must end here */
1139 }
1140
1141 /* Stop looking at end of the line. */
1142 if (ptr[wlen] == NUL)
1143 break;
1144
1145 /* Perform a binary search in the list of accepted bytes. */
1146 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001147 if (c == TAB) /* <Tab> is handled like <Space> */
1148 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001149 lo = arridx;
1150 hi = arridx + len - 1;
1151 while (lo < hi)
1152 {
1153 m = (lo + hi) / 2;
1154 if (byts[m] > c)
1155 hi = m - 1;
1156 else if (byts[m] < c)
1157 lo = m + 1;
1158 else
1159 {
1160 lo = hi = m;
1161 break;
1162 }
1163 }
1164
1165 /* Stop if there is no matching byte. */
1166 if (hi < lo || byts[lo] != c)
1167 break;
1168
1169 /* Continue at the child (if there is one). */
1170 arridx = idxs[lo];
1171 ++wlen;
1172 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001173
1174 /* One space in the good word may stand for several spaces in the
1175 * checked word. */
1176 if (c == ' ')
1177 {
1178 for (;;)
1179 {
1180 if (flen <= 0 && *mip->mi_fend != NUL)
1181 flen = fold_more(mip);
1182 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1183 break;
1184 ++wlen;
1185 --flen;
1186 }
1187 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001188 }
1189
1190 /*
1191 * Verify that one of the possible endings is valid. Try the longest
1192 * first.
1193 */
1194 while (endidxcnt > 0)
1195 {
1196 --endidxcnt;
1197 arridx = endidx[endidxcnt];
1198 wlen = endlen[endidxcnt];
1199
1200#ifdef FEAT_MBYTE
1201 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1202 continue; /* not at first byte of character */
1203#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001204 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001205 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001206 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001207 continue; /* next char is a word character */
1208 word_ends = FALSE;
1209 }
1210 else
1211 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001212 /* The prefix flag is before compound flags. Once a valid prefix flag
1213 * has been found we try compound flags. */
1214 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001215
1216#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001217 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001218 {
1219 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001220 * when folding case. This can be slow, take a shortcut when the
1221 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001222 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001223 if (STRNCMP(ptr, p, wlen) != 0)
1224 {
1225 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1226 mb_ptr_adv(p);
1227 wlen = p - mip->mi_word;
1228 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001229 }
1230#endif
1231
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001232 /* Check flags and region. For FIND_PREFIX check the condition and
1233 * prefix ID.
1234 * Repeat this if there are more flags/region alternatives until there
1235 * is a match. */
1236 res = SP_BAD;
1237 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1238 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001239 {
1240 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001241
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001242 /* For the fold-case tree check that the case of the checked word
1243 * matches with what the word in the tree requires.
1244 * For keep-case tree the case is always right. For prefixes we
1245 * don't bother to check. */
1246 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001247 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001248 if (mip->mi_cend != mip->mi_word + wlen)
1249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001250 /* mi_capflags was set for a different word length, need
1251 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001252 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001253 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001254 }
1255
Bram Moolenaar0c405862005-06-22 22:26:26 +00001256 if (mip->mi_capflags == WF_KEEPCAP
1257 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001258 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001259 }
1260
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001261 /* When mode is FIND_PREFIX the word must support the prefix:
1262 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001263 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001264 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001265 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001266 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001267 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001268 mip->mi_word + mip->mi_cprefixlen, slang,
1269 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001270 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001271 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001272
1273 /* Use the WF_RARE flag for a rare prefix. */
1274 if (c & WF_RAREPFX)
1275 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001276 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001277 }
1278
Bram Moolenaar78622822005-08-23 21:00:13 +00001279 if (slang->sl_nobreak)
1280 {
1281 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1282 && (flags & WF_BANNED) == 0)
1283 {
1284 /* NOBREAK: found a valid following word. That's all we
1285 * need to know, so return. */
1286 mip->mi_result = SP_OK;
1287 break;
1288 }
1289 }
1290
1291 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1292 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001293 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00001294 /* If there is no flag or the word is shorter than
1295 * COMPOUNDMIN reject it quickly.
1296 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001297 * that's too short... Myspell compatibility requires this
1298 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001299 if (((unsigned)flags >> 24) == 0
1300 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001301 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001302#ifdef FEAT_MBYTE
1303 /* For multi-byte chars check character length against
1304 * COMPOUNDMIN. */
1305 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001306 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001307 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1308 wlen - mip->mi_compoff) < slang->sl_compminlen)
1309 continue;
1310#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001311
Bram Moolenaare52325c2005-08-22 22:54:29 +00001312 /* Limit the number of compound words to COMPOUNDMAX if no
1313 * maximum for syllables is specified. */
1314 if (!word_ends && mip->mi_complen + 2 > slang->sl_compmax
1315 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001316 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001317
Bram Moolenaard12a1322005-08-21 22:08:24 +00001318 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001319 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001320 ? slang->sl_compstartflags
1321 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001322 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001323 continue;
1324
Bram Moolenaare52325c2005-08-22 22:54:29 +00001325 if (mode == FIND_COMPOUND)
1326 {
1327 int capflags;
1328
1329 /* Need to check the caps type of the appended compound
1330 * word. */
1331#ifdef FEAT_MBYTE
1332 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1333 mip->mi_compoff) != 0)
1334 {
1335 /* case folding may have changed the length */
1336 p = mip->mi_word;
1337 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1338 mb_ptr_adv(p);
1339 }
1340 else
1341#endif
1342 p = mip->mi_word + mip->mi_compoff;
1343 capflags = captype(p, mip->mi_word + wlen);
1344 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1345 && (flags & WF_FIXCAP) != 0))
1346 continue;
1347
1348 if (capflags != WF_ALLCAP)
1349 {
1350 /* When the character before the word is a word
1351 * character we do not accept a Onecap word. We do
1352 * accept a no-caps word, even when the dictionary
1353 * word specifies ONECAP. */
1354 mb_ptr_back(mip->mi_word, p);
1355 if (spell_iswordp_nmw(p)
1356 ? capflags == WF_ONECAP
1357 : (flags & WF_ONECAP) != 0
1358 && capflags != WF_ONECAP)
1359 continue;
1360 }
1361 }
1362
Bram Moolenaar5195e452005-08-19 20:32:47 +00001363 /* If the word ends the sequence of compound flags of the
1364 * words must match with one of the COMPOUNDFLAGS items and
1365 * the number of syllables must not be too large. */
1366 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1367 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1368 if (word_ends)
1369 {
1370 char_u fword[MAXWLEN];
1371
1372 if (slang->sl_compsylmax < MAXWLEN)
1373 {
1374 /* "fword" is only needed for checking syllables. */
1375 if (ptr == mip->mi_word)
1376 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1377 else
1378 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1379 }
1380 if (!can_compound(slang, fword, mip->mi_compflags))
1381 continue;
1382 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001383 }
1384
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001385 /* Check NEEDCOMPOUND: can't use word without compounding. */
1386 else if (flags & WF_NEEDCOMP)
1387 continue;
1388
Bram Moolenaar78622822005-08-23 21:00:13 +00001389 nobreak_result = SP_OK;
1390
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001391 if (!word_ends)
1392 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001393 int save_result = mip->mi_result;
1394 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001395 langp_T *save_lp = mip->mi_lp;
1396 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001397
1398 /* Check that a valid word follows. If there is one and we
1399 * are compounding, it will set "mi_result", thus we are
1400 * always finished here. For NOBREAK we only check that a
1401 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001402 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001403 if (slang->sl_nobreak)
1404 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001405
1406 /* Find following word in case-folded tree. */
1407 mip->mi_compoff = endlen[endidxcnt];
1408#ifdef FEAT_MBYTE
1409 if (has_mbyte && mode == FIND_KEEPWORD)
1410 {
1411 /* Compute byte length in case-folded word from "wlen":
1412 * byte length in keep-case word. Length may change when
1413 * folding case. This can be slow, take a shortcut when
1414 * the case-folded word is equal to the keep-case word. */
1415 p = mip->mi_fword;
1416 if (STRNCMP(ptr, p, wlen) != 0)
1417 {
1418 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1419 mb_ptr_adv(p);
1420 mip->mi_compoff = p - mip->mi_fword;
1421 }
1422 }
1423#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001424 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001425 ++mip->mi_complen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001426
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001427 /* For NOBREAK we need to try all NOBREAK languages, at least
1428 * to find the ".add" file(s). */
1429 for (lpi = 0; lpi < mip->mi_buf->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001430 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001431 if (slang->sl_nobreak)
1432 {
1433 mip->mi_lp = LANGP_ENTRY(mip->mi_buf->b_langp, lpi);
1434 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1435 || !mip->mi_lp->lp_slang->sl_nobreak)
1436 continue;
1437 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001438
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001439 find_word(mip, FIND_COMPOUND);
1440
1441 /* When NOBREAK any word that matches is OK. Otherwise we
1442 * need to find the longest match, thus try with keep-case
1443 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001444 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1445 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001446 /* Find following word in keep-case tree. */
1447 mip->mi_compoff = wlen;
1448 find_word(mip, FIND_KEEPCOMPOUND);
1449
1450 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1451 {
1452 /* Check for following word with prefix. */
1453 mip->mi_compoff = c;
1454 find_prefix(mip, FIND_COMPOUND);
1455 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001456 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001457
1458 if (!slang->sl_nobreak)
1459 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001460 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001461 --mip->mi_complen;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001462 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001463
Bram Moolenaar78622822005-08-23 21:00:13 +00001464 if (slang->sl_nobreak)
1465 {
1466 nobreak_result = mip->mi_result;
1467 mip->mi_result = save_result;
1468 mip->mi_end = save_end;
1469 }
1470 else
1471 {
1472 if (mip->mi_result == SP_OK)
1473 break;
1474 continue;
1475 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001476 }
1477
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001478 if (flags & WF_BANNED)
1479 res = SP_BANNED;
1480 else if (flags & WF_REGION)
1481 {
1482 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001483 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001484 res = SP_OK;
1485 else
1486 res = SP_LOCAL;
1487 }
1488 else if (flags & WF_RARE)
1489 res = SP_RARE;
1490 else
1491 res = SP_OK;
1492
Bram Moolenaar78622822005-08-23 21:00:13 +00001493 /* Always use the longest match and the best result. For NOBREAK
1494 * we separately keep the longest match without a following good
1495 * word as a fall-back. */
1496 if (nobreak_result == SP_BAD)
1497 {
1498 if (mip->mi_result2 > res)
1499 {
1500 mip->mi_result2 = res;
1501 mip->mi_end2 = mip->mi_word + wlen;
1502 }
1503 else if (mip->mi_result2 == res
1504 && mip->mi_end2 < mip->mi_word + wlen)
1505 mip->mi_end2 = mip->mi_word + wlen;
1506 }
1507 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001508 {
1509 mip->mi_result = res;
1510 mip->mi_end = mip->mi_word + wlen;
1511 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001512 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001513 mip->mi_end = mip->mi_word + wlen;
1514
Bram Moolenaar78622822005-08-23 21:00:13 +00001515 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001516 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001517 }
1518
Bram Moolenaar78622822005-08-23 21:00:13 +00001519 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001520 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001521 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001522}
1523
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001524/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00001525 * Return TRUE if "flags" is a valid sequence of compound flags and
1526 * "word[len]" does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001527 */
1528 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001529can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001530 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001531 char_u *word;
1532 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001533{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001534 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001535#ifdef FEAT_MBYTE
1536 char_u uflags[MAXWLEN * 2];
1537 int i;
1538#endif
1539 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001540
1541 if (slang->sl_compprog == NULL)
1542 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001543#ifdef FEAT_MBYTE
1544 if (enc_utf8)
1545 {
1546 /* Need to convert the single byte flags to utf8 characters. */
1547 p = uflags;
1548 for (i = 0; flags[i] != NUL; ++i)
1549 p += mb_char2bytes(flags[i], p);
1550 *p = NUL;
1551 p = uflags;
1552 }
1553 else
1554#endif
1555 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001556 regmatch.regprog = slang->sl_compprog;
1557 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001558 if (!vim_regexec(&regmatch, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001559 return FALSE;
1560
Bram Moolenaare52325c2005-08-22 22:54:29 +00001561 /* Count the number of syllables. This may be slow, do it last. If there
1562 * are too many syllables AND the number of compound words is above
1563 * COMPOUNDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001564 if (slang->sl_compsylmax < MAXWLEN
1565 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001566 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001567 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001568}
1569
1570/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001571 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1572 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001573 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001574 */
1575 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001576valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001577 int totprefcnt; /* nr of prefix IDs */
1578 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001579 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001580 char_u *word;
1581 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001582 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001583{
1584 int prefcnt;
1585 int pidx;
1586 regprog_T *rp;
1587 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001588 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001589
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001590 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001591 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1592 {
1593 pidx = slang->sl_pidxs[arridx + prefcnt];
1594
1595 /* Check the prefix ID. */
1596 if (prefid != (pidx & 0xff))
1597 continue;
1598
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001599 /* Check if the prefix doesn't combine and the word already has a
1600 * suffix. */
1601 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1602 continue;
1603
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001604 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001605 * stored in the two bytes above the prefix ID byte. */
1606 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001607 if (rp != NULL)
1608 {
1609 regmatch.regprog = rp;
1610 regmatch.rm_ic = FALSE;
1611 if (!vim_regexec(&regmatch, word, 0))
1612 continue;
1613 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001614 else if (cond_req)
1615 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001616
Bram Moolenaar53805d12005-08-01 07:08:33 +00001617 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001618 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001619 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001620 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001621}
1622
1623/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001624 * Check if the word at "mip->mi_word" has a matching prefix.
1625 * If it does, then check the following word.
1626 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001627 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1628 * prefix in a compound word.
1629 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001630 * For a match mip->mi_result is updated.
1631 */
1632 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001633find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001634 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001635 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001636{
1637 idx_T arridx = 0;
1638 int len;
1639 int wlen = 0;
1640 int flen;
1641 int c;
1642 char_u *ptr;
1643 idx_T lo, hi, m;
1644 slang_T *slang = mip->mi_lp->lp_slang;
1645 char_u *byts;
1646 idx_T *idxs;
1647
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001648 byts = slang->sl_pbyts;
1649 if (byts == NULL)
1650 return; /* array is empty */
1651
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001652 /* We use the case-folded word here, since prefixes are always
1653 * case-folded. */
1654 ptr = mip->mi_fword;
1655 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001656 if (mode == FIND_COMPOUND)
1657 {
1658 /* Skip over the previously found word(s). */
1659 ptr += mip->mi_compoff;
1660 flen -= mip->mi_compoff;
1661 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001662 idxs = slang->sl_pidxs;
1663
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001664 /*
1665 * Repeat advancing in the tree until:
1666 * - there is a byte that doesn't match,
1667 * - we reach the end of the tree,
1668 * - or we reach the end of the line.
1669 */
1670 for (;;)
1671 {
1672 if (flen == 0 && *mip->mi_fend != NUL)
1673 flen = fold_more(mip);
1674
1675 len = byts[arridx++];
1676
1677 /* If the first possible byte is a zero the prefix could end here.
1678 * Check if the following word matches and supports the prefix. */
1679 if (byts[arridx] == 0)
1680 {
1681 /* There can be several prefixes with different conditions. We
1682 * try them all, since we don't know which one will give the
1683 * longest match. The word is the same each time, pass the list
1684 * of possible prefixes to find_word(). */
1685 mip->mi_prefarridx = arridx;
1686 mip->mi_prefcnt = len;
1687 while (len > 0 && byts[arridx] == 0)
1688 {
1689 ++arridx;
1690 --len;
1691 }
1692 mip->mi_prefcnt -= len;
1693
1694 /* Find the word that comes after the prefix. */
1695 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001696 if (mode == FIND_COMPOUND)
1697 /* Skip over the previously found word(s). */
1698 mip->mi_prefixlen += mip->mi_compoff;
1699
Bram Moolenaar53805d12005-08-01 07:08:33 +00001700#ifdef FEAT_MBYTE
1701 if (has_mbyte)
1702 {
1703 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001704 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1705 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001706 }
1707 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001708 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001709#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001710 find_word(mip, FIND_PREFIX);
1711
1712
1713 if (len == 0)
1714 break; /* no children, word must end here */
1715 }
1716
1717 /* Stop looking at end of the line. */
1718 if (ptr[wlen] == NUL)
1719 break;
1720
1721 /* Perform a binary search in the list of accepted bytes. */
1722 c = ptr[wlen];
1723 lo = arridx;
1724 hi = arridx + len - 1;
1725 while (lo < hi)
1726 {
1727 m = (lo + hi) / 2;
1728 if (byts[m] > c)
1729 hi = m - 1;
1730 else if (byts[m] < c)
1731 lo = m + 1;
1732 else
1733 {
1734 lo = hi = m;
1735 break;
1736 }
1737 }
1738
1739 /* Stop if there is no matching byte. */
1740 if (hi < lo || byts[lo] != c)
1741 break;
1742
1743 /* Continue at the child (if there is one). */
1744 arridx = idxs[lo];
1745 ++wlen;
1746 --flen;
1747 }
1748}
1749
1750/*
1751 * Need to fold at least one more character. Do until next non-word character
1752 * for efficiency.
1753 * Return the length of the folded chars in bytes.
1754 */
1755 static int
1756fold_more(mip)
1757 matchinf_T *mip;
1758{
1759 int flen;
1760 char_u *p;
1761
1762 p = mip->mi_fend;
1763 do
1764 {
1765 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001766 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001767
1768 /* Include the non-word character so that we can check for the
1769 * word end. */
1770 if (*mip->mi_fend != NUL)
1771 mb_ptr_adv(mip->mi_fend);
1772
1773 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1774 mip->mi_fword + mip->mi_fwordlen,
1775 MAXWLEN - mip->mi_fwordlen);
1776 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1777 mip->mi_fwordlen += flen;
1778 return flen;
1779}
1780
1781/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001782 * Check case flags for a word. Return TRUE if the word has the requested
1783 * case.
1784 */
1785 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001786spell_valid_case(wordflags, treeflags)
1787 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001788 int treeflags; /* flags for the word in the spell tree */
1789{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001790 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001791 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001792 && ((treeflags & WF_ONECAP) == 0
1793 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001794}
1795
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001796/*
1797 * Return TRUE if spell checking is not enabled.
1798 */
1799 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00001800no_spell_checking(wp)
1801 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001802{
Bram Moolenaar95529562005-08-25 21:21:38 +00001803 if (!wp->w_p_spell || *wp->w_buffer->b_p_spl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001804 {
1805 EMSG(_("E756: Spell checking is not enabled"));
1806 return TRUE;
1807 }
1808 return FALSE;
1809}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001810
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001811/*
1812 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001813 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
1814 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001815 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1816 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001817 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001818 */
1819 int
Bram Moolenaar95529562005-08-25 21:21:38 +00001820spell_move_to(wp, dir, allwords, curline, attrp)
1821 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001822 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001823 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001824 int curline;
Bram Moolenaar95529562005-08-25 21:21:38 +00001825 int *attrp; /* return: attributes of bad word or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001826{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001827 linenr_T lnum;
1828 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001829 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001830 char_u *line;
1831 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001832 char_u *endp;
1833 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001834 int len;
Bram Moolenaar95529562005-08-25 21:21:38 +00001835 int has_syntax = syntax_present(wp->w_buffer);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001836 int col;
1837 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001838 char_u *buf = NULL;
1839 int buflen = 0;
1840 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001841 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001842 int found_one = FALSE;
1843 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001844
Bram Moolenaar95529562005-08-25 21:21:38 +00001845 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00001846 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001847
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001848 /*
1849 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001850 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001851 *
1852 * When searching backwards, we continue in the line to find the last
1853 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001854 *
1855 * We concatenate the start of the next line, so that wrapped words work
1856 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1857 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001858 */
Bram Moolenaar95529562005-08-25 21:21:38 +00001859 lnum = wp->w_cursor.lnum;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001860 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001861
1862 while (!got_int)
1863 {
Bram Moolenaar95529562005-08-25 21:21:38 +00001864 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001865
Bram Moolenaar0c405862005-06-22 22:26:26 +00001866 len = STRLEN(line);
1867 if (buflen < len + MAXWLEN + 2)
1868 {
1869 vim_free(buf);
1870 buflen = len + MAXWLEN + 2;
1871 buf = alloc(buflen);
1872 if (buf == NULL)
1873 break;
1874 }
1875
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001876 /* In first line check first word for Capital. */
1877 if (lnum == 1)
1878 capcol = 0;
1879
1880 /* For checking first word with a capital skip white space. */
1881 if (capcol == 0)
1882 capcol = skipwhite(line) - line;
1883
Bram Moolenaar0c405862005-06-22 22:26:26 +00001884 /* Copy the line into "buf" and append the start of the next line if
1885 * possible. */
1886 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00001887 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001888 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1889
1890 p = buf + skip;
1891 endp = buf + len;
1892 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001893 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001894 /* When searching backward don't search after the cursor. Unless
1895 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001896 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00001897 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001898 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00001899 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001900 break;
1901
1902 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001903 attr = 0;
Bram Moolenaar95529562005-08-25 21:21:38 +00001904 len = spell_check(wp, p, &attr, &capcol);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001905
1906 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001907 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001908 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001909 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001910 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001911 found_one = TRUE;
1912
Bram Moolenaar51485f02005-06-04 21:55:20 +00001913 /* When searching forward only accept a bad word after
1914 * the cursor. */
1915 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001916 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00001917 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001918 && (wrapped
1919 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00001920 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001921 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001922 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001923 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001924 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001925 col = p - buf;
Bram Moolenaar95529562005-08-25 21:21:38 +00001926 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar51485f02005-06-04 21:55:20 +00001927 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001928 }
1929 else
1930 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001931
Bram Moolenaar51485f02005-06-04 21:55:20 +00001932 if (can_spell)
1933 {
1934 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001935 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001936#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001937 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001938#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001939 if (dir == FORWARD)
1940 {
1941 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00001942 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001943 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001944 if (attrp != NULL)
1945 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001946 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001947 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001948 else if (curline)
1949 /* Insert mode completion: put cursor after
1950 * the bad word. */
1951 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001952 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001953 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001954 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001955 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001956 }
1957
Bram Moolenaar51485f02005-06-04 21:55:20 +00001958 /* advance to character after the word */
1959 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001960 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001961 }
1962
Bram Moolenaar5195e452005-08-19 20:32:47 +00001963 if (dir == BACKWARD && found_pos.lnum != 0)
1964 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001965 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00001966 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001967 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001968 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001969 }
1970
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001971 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001972 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001973
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001974 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001975 if (dir == BACKWARD)
1976 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001977 /* If we are back at the starting line and searched it again there
1978 * is no match, give up. */
1979 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001980 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001981
1982 if (lnum > 1)
1983 --lnum;
1984 else if (!p_ws)
1985 break; /* at first line and 'nowrapscan' */
1986 else
1987 {
1988 /* Wrap around to the end of the buffer. May search the
1989 * starting line again and accept the last match. */
1990 lnum = wp->w_buffer->b_ml.ml_line_count;
1991 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001992 if (!shortmess(SHM_SEARCH))
1993 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001994 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001995 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001996 }
1997 else
1998 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001999 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2000 ++lnum;
2001 else if (!p_ws)
2002 break; /* at first line and 'nowrapscan' */
2003 else
2004 {
2005 /* Wrap around to the start of the buffer. May search the
2006 * starting line again and accept the first match. */
2007 lnum = 1;
2008 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002009 if (!shortmess(SHM_SEARCH))
2010 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002011 }
2012
2013 /* If we are back at the starting line and there is no match then
2014 * give up. */
2015 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002016 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002017
2018 /* Skip the characters at the start of the next line that were
2019 * included in a match crossing line boundaries. */
2020 if (attr == 0)
2021 skip = p - endp;
2022 else
2023 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002024
2025 /* Capscol skips over the inserted space. */
2026 --capcol;
2027
2028 /* But after empty line check first word in next line */
2029 if (*skipwhite(line) == NUL)
2030 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002031 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002032
2033 line_breakcheck();
2034 }
2035
Bram Moolenaar0c405862005-06-22 22:26:26 +00002036 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002037 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002038}
2039
2040/*
2041 * For spell checking: concatenate the start of the following line "line" into
2042 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
2043 */
2044 void
2045spell_cat_line(buf, line, maxlen)
2046 char_u *buf;
2047 char_u *line;
2048 int maxlen;
2049{
2050 char_u *p;
2051 int n;
2052
2053 p = skipwhite(line);
2054 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2055 p = skipwhite(p + 1);
2056
2057 if (*p != NUL)
2058 {
2059 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002060 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002061 n = p - line;
2062 if (n >= maxlen)
2063 n = maxlen - 1;
2064 vim_memset(buf + 1, ' ', n);
2065 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002066}
2067
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002068typedef struct spelload_S
2069{
2070 char_u sl_lang[MAXWLEN + 1]; /* language name */
2071 slang_T *sl_slang; /* resulting slang_T struct */
2072 int sl_nobreak; /* NOBREAK language found */
2073} spelload_T;
2074
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002075/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002076 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002077 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002078 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002079 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002080spell_load_lang(lang)
2081 char_u *lang;
2082{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002083 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002084 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002085 spelload_T sl;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002086
Bram Moolenaarb765d632005-06-07 21:00:02 +00002087 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002088 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002089 STRCPY(sl.sl_lang, lang);
2090 sl.sl_slang = NULL;
2091 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002092
Bram Moolenaarb765d632005-06-07 21:00:02 +00002093 /*
2094 * Find the first spell file for "lang" in 'runtimepath' and load it.
2095 */
2096 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2097 "spell/%s.%s.spl", lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002098 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002099
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002100 if (r == FAIL && *sl.sl_lang != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002101 {
2102 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002103 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002104 "spell/%s.ascii.spl", lang);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002105 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002106 }
2107
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002108 if (r == FAIL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002109 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2110 lang, spell_enc(), lang);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002111 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002112 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002113 /* At least one file was loaded, now load all the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002114 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002115 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002116 }
2117}
2118
2119/*
2120 * Return the encoding used for spell checking: Use 'encoding', except that we
2121 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2122 */
2123 static char_u *
2124spell_enc()
2125{
2126
2127#ifdef FEAT_MBYTE
2128 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2129 return p_enc;
2130#endif
2131 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002132}
2133
2134/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002135 * Get the name of the .spl file for the internal wordlist into
2136 * "fname[MAXPATHL]".
2137 */
2138 static void
2139int_wordlist_spl(fname)
2140 char_u *fname;
2141{
2142 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2143 int_wordlist, spell_enc());
2144}
2145
2146/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002147 * Allocate a new slang_T.
2148 * Caller must fill "sl_next".
2149 */
2150 static slang_T *
2151slang_alloc(lang)
2152 char_u *lang;
2153{
2154 slang_T *lp;
2155
Bram Moolenaar51485f02005-06-04 21:55:20 +00002156 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002157 if (lp != NULL)
2158 {
2159 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002160 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002161 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002162 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002163 }
2164 return lp;
2165}
2166
2167/*
2168 * Free the contents of an slang_T and the structure itself.
2169 */
2170 static void
2171slang_free(lp)
2172 slang_T *lp;
2173{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002174 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002175 vim_free(lp->sl_fname);
2176 slang_clear(lp);
2177 vim_free(lp);
2178}
2179
2180/*
2181 * Clear an slang_T so that the file can be reloaded.
2182 */
2183 static void
2184slang_clear(lp)
2185 slang_T *lp;
2186{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002187 garray_T *gap;
2188 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002189 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002190 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002191
Bram Moolenaar51485f02005-06-04 21:55:20 +00002192 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002193 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002194 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002195 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002196 vim_free(lp->sl_pbyts);
2197 lp->sl_pbyts = NULL;
2198
Bram Moolenaar51485f02005-06-04 21:55:20 +00002199 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002200 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002201 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002202 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002203 vim_free(lp->sl_pidxs);
2204 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002205
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002206 gap = &lp->sl_rep;
2207 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002208 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002209 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2210 vim_free(ftp->ft_from);
2211 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002212 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002213 ga_clear(gap);
2214
2215 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002216 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002217 {
2218 /* "ga_len" is set to 1 without adding an item for latin1 */
2219 if (gap->ga_data != NULL)
2220 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2221 for (i = 0; i < gap->ga_len; ++i)
2222 vim_free(((int **)gap->ga_data)[i]);
2223 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002224 else
2225 /* SAL items: free salitem_T items */
2226 while (gap->ga_len > 0)
2227 {
2228 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2229 vim_free(smp->sm_lead);
2230 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2231 vim_free(smp->sm_to);
2232#ifdef FEAT_MBYTE
2233 vim_free(smp->sm_lead_w);
2234 vim_free(smp->sm_oneof_w);
2235 vim_free(smp->sm_to_w);
2236#endif
2237 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002238 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002239
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002240 for (i = 0; i < lp->sl_prefixcnt; ++i)
2241 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002242 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002243 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002244 lp->sl_prefprog = NULL;
2245
2246 vim_free(lp->sl_midword);
2247 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002248
Bram Moolenaar5195e452005-08-19 20:32:47 +00002249 vim_free(lp->sl_compprog);
2250 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002251 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002252 lp->sl_compprog = NULL;
2253 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002254 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002255
2256 vim_free(lp->sl_syllable);
2257 lp->sl_syllable = NULL;
2258 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002259
Bram Moolenaarea424162005-06-16 21:51:00 +00002260#ifdef FEAT_MBYTE
2261 {
2262 int todo = lp->sl_map_hash.ht_used;
2263 hashitem_T *hi;
2264
2265 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
2266 if (!HASHITEM_EMPTY(hi))
2267 {
2268 --todo;
2269 vim_free(hi->hi_key);
2270 }
2271 }
2272 hash_clear(&lp->sl_map_hash);
2273#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002274
2275 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002276 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002277 lp->sl_compsylmax = MAXWLEN;
2278 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002279}
2280
2281/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002282 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002283 * Invoked through do_in_runtimepath().
2284 */
2285 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002286spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002287 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002288 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002289{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002290 spelload_T *slp = (spelload_T *)cookie;
2291 slang_T *slang;
2292
2293 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2294 if (slang != NULL)
2295 {
2296 /* When a previously loaded file has NOBREAK also use it for the
2297 * ".add" files. */
2298 if (slp->sl_nobreak && slang->sl_add)
2299 slang->sl_nobreak = TRUE;
2300 else if (slang->sl_nobreak)
2301 slp->sl_nobreak = TRUE;
2302
2303 slp->sl_slang = slang;
2304 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002305}
2306
2307/*
2308 * Load one spell file and store the info into a slang_T.
2309 *
2310 * This is invoked in two ways:
2311 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2312 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2313 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2314 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002315 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002316 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002317 static slang_T *
2318spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002319 char_u *fname;
2320 char_u *lang;
2321 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002322 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002323{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002324 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002325 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002326 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002327 char_u *bp;
2328 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002329 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002330 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002331 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002332 int round;
2333 char_u *save_sourcing_name = sourcing_name;
2334 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002335 slang_T *lp = NULL;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002336 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002337 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002338 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002339
Bram Moolenaarb765d632005-06-07 21:00:02 +00002340 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002341 if (fd == NULL)
2342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002343 if (!silent)
2344 EMSG2(_(e_notopen), fname);
2345 else if (p_verbose > 2)
2346 {
2347 verbose_enter();
2348 smsg((char_u *)e_notopen, fname);
2349 verbose_leave();
2350 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002351 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002352 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002353 if (p_verbose > 2)
2354 {
2355 verbose_enter();
2356 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2357 verbose_leave();
2358 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002359
Bram Moolenaarb765d632005-06-07 21:00:02 +00002360 if (old_lp == NULL)
2361 {
2362 lp = slang_alloc(lang);
2363 if (lp == NULL)
2364 goto endFAIL;
2365
2366 /* Remember the file name, used to reload the file when it's updated. */
2367 lp->sl_fname = vim_strsave(fname);
2368 if (lp->sl_fname == NULL)
2369 goto endFAIL;
2370
2371 /* Check for .add.spl. */
2372 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2373 }
2374 else
2375 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002376
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002377 /* Set sourcing_name, so that error messages mention the file name. */
2378 sourcing_name = fname;
2379 sourcing_lnum = 0;
2380
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002381 /* <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002382 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002383 for (i = 0; i < VIMSPELLMAGICL; ++i)
2384 buf[i] = getc(fd); /* <fileID> */
2385 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2386 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002387 EMSG(_("E757: This does not look like a spell file"));
2388 goto endFAIL;
2389 }
2390 c = getc(fd); /* <versionnr> */
2391 if (c < VIMSPELLVERSION)
2392 {
2393 EMSG(_("E771: Old spell file, needs to be updated"));
2394 goto endFAIL;
2395 }
2396 else if (c > VIMSPELLVERSION)
2397 {
2398 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002399 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002400 }
2401
Bram Moolenaar5195e452005-08-19 20:32:47 +00002402
2403 /*
2404 * <SECTIONS>: <section> ... <sectionend>
2405 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2406 */
2407 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002408 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002409 n = getc(fd); /* <sectionID> or <sectionend> */
2410 if (n == SN_END)
2411 break;
2412 c = getc(fd); /* <sectionflags> */
2413 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2414 /* <sectionlen> */
2415 if (len < 0)
2416 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002417
Bram Moolenaar5195e452005-08-19 20:32:47 +00002418 res = 0;
2419 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002420 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002421 case SN_REGION:
2422 res = read_region_section(fd, lp, len);
2423 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002424
Bram Moolenaar5195e452005-08-19 20:32:47 +00002425 case SN_CHARFLAGS:
2426 res = read_charflags_section(fd);
2427 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002428
Bram Moolenaar5195e452005-08-19 20:32:47 +00002429 case SN_MIDWORD:
2430 lp->sl_midword = read_string(fd, len); /* <midword> */
2431 if (lp->sl_midword == NULL)
2432 goto endFAIL;
2433 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002434
Bram Moolenaar5195e452005-08-19 20:32:47 +00002435 case SN_PREFCOND:
2436 res = read_prefcond_section(fd, lp);
2437 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002438
Bram Moolenaar5195e452005-08-19 20:32:47 +00002439 case SN_REP:
2440 res = read_rep_section(fd, lp);
2441 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002442
Bram Moolenaar5195e452005-08-19 20:32:47 +00002443 case SN_SAL:
2444 res = read_sal_section(fd, lp);
2445 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002446
Bram Moolenaar5195e452005-08-19 20:32:47 +00002447 case SN_SOFO:
2448 res = read_sofo_section(fd, lp);
2449 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002450
Bram Moolenaar5195e452005-08-19 20:32:47 +00002451 case SN_MAP:
2452 p = read_string(fd, len); /* <mapstr> */
2453 if (p == NULL)
2454 goto endFAIL;
2455 set_map_str(lp, p);
2456 vim_free(p);
2457 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002458
Bram Moolenaar5195e452005-08-19 20:32:47 +00002459 case SN_COMPOUND:
2460 res = read_compound(fd, lp, len);
2461 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002462
Bram Moolenaar78622822005-08-23 21:00:13 +00002463 case SN_NOBREAK:
2464 lp->sl_nobreak = TRUE;
2465 break;
2466
Bram Moolenaar5195e452005-08-19 20:32:47 +00002467 case SN_SYLLABLE:
2468 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2469 if (lp->sl_syllable == NULL)
2470 goto endFAIL;
2471 if (init_syl_tab(lp) == FAIL)
2472 goto endFAIL;
2473 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002474
Bram Moolenaar5195e452005-08-19 20:32:47 +00002475 default:
2476 /* Unsupported section. When it's required give an error
2477 * message. When it's not required skip the contents. */
2478 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002479 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002480 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002481 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002482 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002483 while (--len >= 0)
2484 if (getc(fd) < 0)
2485 goto truncerr;
2486 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002487 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002488 if (res == SP_FORMERROR)
2489 {
2490formerr:
2491 EMSG(_(e_format));
2492 goto endFAIL;
2493 }
2494 if (res == SP_TRUNCERROR)
2495 {
2496truncerr:
2497 EMSG(_(e_spell_trunc));
2498 goto endFAIL;
2499 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002500 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002501 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002502 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002503
Bram Moolenaar51485f02005-06-04 21:55:20 +00002504 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002505 * round 2: <KWORDTREE>
2506 * round 3: <PREFIXTREE> */
2507 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002508 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002509 /* The tree size was computed when writing the file, so that we can
2510 * allocate it as one long block. <nodecount> */
2511 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2512 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002513 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002514 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002515 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002516 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002517 bp = lalloc((long_u)len, TRUE);
2518 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002519 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002520 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002521 lp->sl_fbyts = bp;
2522 else if (round == 2)
2523 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002524 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002525 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002526
2527 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002528 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2529 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002530 goto endFAIL;
2531 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002532 lp->sl_fidxs = ip;
2533 else if (round == 2)
2534 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002535 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002536 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002537
2538 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002539 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002540 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002541 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002542 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002543 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002544 }
2545 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002546
Bram Moolenaarb765d632005-06-07 21:00:02 +00002547 /* For a new file link it in the list of spell files. */
2548 if (old_lp == NULL)
2549 {
2550 lp->sl_next = first_lang;
2551 first_lang = lp;
2552 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002553
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002554 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002555
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002556endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002557 if (lang != NULL)
2558 /* truncating the name signals the error to spell_load_lang() */
2559 *lang = NUL;
2560 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002561 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002562 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002563
2564endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002565 if (fd != NULL)
2566 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002567 sourcing_name = save_sourcing_name;
2568 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002569
2570 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002571}
2572
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002573/*
2574 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002575 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002576 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002577 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2578 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002579 */
2580 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002581read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002582 FILE *fd;
2583 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002584 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002585{
2586 int cnt = 0;
2587 int i;
2588 char_u *str;
2589
2590 /* read the length bytes, MSB first */
2591 for (i = 0; i < cnt_bytes; ++i)
2592 cnt = (cnt << 8) + getc(fd);
2593 if (cnt < 0)
2594 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002595 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002596 return NULL;
2597 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002598 *cntp = cnt;
2599 if (cnt == 0)
2600 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002601
Bram Moolenaar5195e452005-08-19 20:32:47 +00002602 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002603 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002604 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002605 return str;
2606}
2607
Bram Moolenaar7887d882005-07-01 22:33:52 +00002608/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002609 * Read a string of length "cnt" from "fd" into allocated memory.
2610 * Returns NULL when out of memory.
2611 */
2612 static char_u *
2613read_string(fd, cnt)
2614 FILE *fd;
2615 int cnt;
2616{
2617 char_u *str;
2618 int i;
2619
2620 /* allocate memory */
2621 str = alloc((unsigned)cnt + 1);
2622 if (str != NULL)
2623 {
2624 /* Read the string. Doesn't check for truncated file. */
2625 for (i = 0; i < cnt; ++i)
2626 str[i] = getc(fd);
2627 str[i] = NUL;
2628 }
2629 return str;
2630}
2631
2632/*
2633 * Read SN_REGION: <regionname> ...
2634 * Return SP_*ERROR flags.
2635 */
2636 static int
2637read_region_section(fd, lp, len)
2638 FILE *fd;
2639 slang_T *lp;
2640 int len;
2641{
2642 int i;
2643
2644 if (len > 16)
2645 return SP_FORMERROR;
2646 for (i = 0; i < len; ++i)
2647 lp->sl_regions[i] = getc(fd); /* <regionname> */
2648 lp->sl_regions[len] = NUL;
2649 return 0;
2650}
2651
2652/*
2653 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2654 * <folcharslen> <folchars>
2655 * Return SP_*ERROR flags.
2656 */
2657 static int
2658read_charflags_section(fd)
2659 FILE *fd;
2660{
2661 char_u *flags;
2662 char_u *fol;
2663 int flagslen, follen;
2664
2665 /* <charflagslen> <charflags> */
2666 flags = read_cnt_string(fd, 1, &flagslen);
2667 if (flagslen < 0)
2668 return flagslen;
2669
2670 /* <folcharslen> <folchars> */
2671 fol = read_cnt_string(fd, 2, &follen);
2672 if (follen < 0)
2673 {
2674 vim_free(flags);
2675 return follen;
2676 }
2677
2678 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
2679 if (flags != NULL && fol != NULL)
2680 set_spell_charflags(flags, flagslen, fol);
2681
2682 vim_free(flags);
2683 vim_free(fol);
2684
2685 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
2686 if ((flags == NULL) != (fol == NULL))
2687 return SP_FORMERROR;
2688 return 0;
2689}
2690
2691/*
2692 * Read SN_PREFCOND section.
2693 * Return SP_*ERROR flags.
2694 */
2695 static int
2696read_prefcond_section(fd, lp)
2697 FILE *fd;
2698 slang_T *lp;
2699{
2700 int cnt;
2701 int i;
2702 int n;
2703 char_u *p;
2704 char_u buf[MAXWLEN + 1];
2705
2706 /* <prefcondcnt> <prefcond> ... */
2707 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
2708 if (cnt <= 0)
2709 return SP_FORMERROR;
2710
2711 lp->sl_prefprog = (regprog_T **)alloc_clear(
2712 (unsigned)sizeof(regprog_T *) * cnt);
2713 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002714 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002715 lp->sl_prefixcnt = cnt;
2716
2717 for (i = 0; i < cnt; ++i)
2718 {
2719 /* <prefcond> : <condlen> <condstr> */
2720 n = getc(fd); /* <condlen> */
2721 if (n < 0 || n >= MAXWLEN)
2722 return SP_FORMERROR;
2723
2724 /* When <condlen> is zero we have an empty condition. Otherwise
2725 * compile the regexp program used to check for the condition. */
2726 if (n > 0)
2727 {
2728 buf[0] = '^'; /* always match at one position only */
2729 p = buf + 1;
2730 while (n-- > 0)
2731 *p++ = getc(fd); /* <condstr> */
2732 *p = NUL;
2733 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
2734 }
2735 }
2736 return 0;
2737}
2738
2739/*
2740 * Read REP items section from "fd": <repcount> <rep> ...
2741 * Return SP_*ERROR flags.
2742 */
2743 static int
2744read_rep_section(fd, slang)
2745 FILE *fd;
2746 slang_T *slang;
2747{
2748 int cnt;
2749 garray_T *gap;
2750 fromto_T *ftp;
2751 short *first;
2752 int i;
2753
2754 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
2755 if (cnt < 0)
2756 return SP_TRUNCERROR;
2757
2758 gap = &slang->sl_rep;
2759 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002760 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002761
2762 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
2763 for (; gap->ga_len < cnt; ++gap->ga_len)
2764 {
2765 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
2766 ftp->ft_from = read_cnt_string(fd, 1, &i);
2767 if (i < 0)
2768 return i;
2769 if (i == 0)
2770 return SP_FORMERROR;
2771 ftp->ft_to = read_cnt_string(fd, 1, &i);
2772 if (i <= 0)
2773 {
2774 vim_free(ftp->ft_from);
2775 if (i < 0)
2776 return i;
2777 return SP_FORMERROR;
2778 }
2779 }
2780
2781 /* Fill the first-index table. */
2782 first = slang->sl_rep_first;
2783 for (i = 0; i < 256; ++i)
2784 first[i] = -1;
2785 for (i = 0; i < gap->ga_len; ++i)
2786 {
2787 ftp = &((fromto_T *)gap->ga_data)[i];
2788 if (first[*ftp->ft_from] == -1)
2789 first[*ftp->ft_from] = i;
2790 }
2791 return 0;
2792}
2793
2794/*
2795 * Read SN_SAL section: <salflags> <salcount> <sal> ...
2796 * Return SP_*ERROR flags.
2797 */
2798 static int
2799read_sal_section(fd, slang)
2800 FILE *fd;
2801 slang_T *slang;
2802{
2803 int i;
2804 int cnt;
2805 garray_T *gap;
2806 salitem_T *smp;
2807 int ccnt;
2808 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002809 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002810
2811 slang->sl_sofo = FALSE;
2812
2813 i = getc(fd); /* <salflags> */
2814 if (i & SAL_F0LLOWUP)
2815 slang->sl_followup = TRUE;
2816 if (i & SAL_COLLAPSE)
2817 slang->sl_collapse = TRUE;
2818 if (i & SAL_REM_ACCENTS)
2819 slang->sl_rem_accents = TRUE;
2820
2821 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
2822 if (cnt < 0)
2823 return SP_TRUNCERROR;
2824
2825 gap = &slang->sl_sal;
2826 ga_init2(gap, sizeof(salitem_T), 10);
2827 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002828 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002829
2830 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
2831 for (; gap->ga_len < cnt; ++gap->ga_len)
2832 {
2833 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2834 ccnt = getc(fd); /* <salfromlen> */
2835 if (ccnt < 0)
2836 return SP_TRUNCERROR;
2837 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002838 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002839 smp->sm_lead = p;
2840
2841 /* Read up to the first special char into sm_lead. */
2842 for (i = 0; i < ccnt; ++i)
2843 {
2844 c = getc(fd); /* <salfrom> */
2845 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
2846 break;
2847 *p++ = c;
2848 }
2849 smp->sm_leadlen = p - smp->sm_lead;
2850 *p++ = NUL;
2851
2852 /* Put (abc) chars in sm_oneof, if any. */
2853 if (c == '(')
2854 {
2855 smp->sm_oneof = p;
2856 for (++i; i < ccnt; ++i)
2857 {
2858 c = getc(fd); /* <salfrom> */
2859 if (c == ')')
2860 break;
2861 *p++ = c;
2862 }
2863 *p++ = NUL;
2864 if (++i < ccnt)
2865 c = getc(fd);
2866 }
2867 else
2868 smp->sm_oneof = NULL;
2869
2870 /* Any following chars go in sm_rules. */
2871 smp->sm_rules = p;
2872 if (i < ccnt)
2873 /* store the char we got while checking for end of sm_lead */
2874 *p++ = c;
2875 for (++i; i < ccnt; ++i)
2876 *p++ = getc(fd); /* <salfrom> */
2877 *p++ = NUL;
2878
2879 /* <saltolen> <salto> */
2880 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
2881 if (ccnt < 0)
2882 {
2883 vim_free(smp->sm_lead);
2884 return ccnt;
2885 }
2886
2887#ifdef FEAT_MBYTE
2888 if (has_mbyte)
2889 {
2890 /* convert the multi-byte strings to wide char strings */
2891 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2892 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2893 if (smp->sm_oneof == NULL)
2894 smp->sm_oneof_w = NULL;
2895 else
2896 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
2897 if (smp->sm_to == NULL)
2898 smp->sm_to_w = NULL;
2899 else
2900 smp->sm_to_w = mb_str2wide(smp->sm_to);
2901 if (smp->sm_lead_w == NULL
2902 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
2903 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
2904 {
2905 vim_free(smp->sm_lead);
2906 vim_free(smp->sm_to);
2907 vim_free(smp->sm_lead_w);
2908 vim_free(smp->sm_oneof_w);
2909 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002910 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002911 }
2912 }
2913#endif
2914 }
2915
2916 /* Fill the first-index table. */
2917 set_sal_first(slang);
2918
2919 return 0;
2920}
2921
2922/*
2923 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
2924 * Return SP_*ERROR flags.
2925 */
2926 static int
2927read_sofo_section(fd, slang)
2928 FILE *fd;
2929 slang_T *slang;
2930{
2931 int cnt;
2932 char_u *from, *to;
2933 int res;
2934
2935 slang->sl_sofo = TRUE;
2936
2937 /* <sofofromlen> <sofofrom> */
2938 from = read_cnt_string(fd, 2, &cnt);
2939 if (cnt < 0)
2940 return cnt;
2941
2942 /* <sofotolen> <sofoto> */
2943 to = read_cnt_string(fd, 2, &cnt);
2944 if (cnt < 0)
2945 {
2946 vim_free(from);
2947 return cnt;
2948 }
2949
2950 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
2951 if (from != NULL && to != NULL)
2952 res = set_sofo(slang, from, to);
2953 else if (from != NULL || to != NULL)
2954 res = SP_FORMERROR; /* only one of two strings is an error */
2955 else
2956 res = 0;
2957
2958 vim_free(from);
2959 vim_free(to);
2960 return res;
2961}
2962
2963/*
2964 * Read the compound section from the .spl file:
2965 * <compmax> <compminlen> <compsylmax> <compflags>
2966 * Returns SP_*ERROR flags.
2967 */
2968 static int
2969read_compound(fd, slang, len)
2970 FILE *fd;
2971 slang_T *slang;
2972 int len;
2973{
2974 int todo = len;
2975 int c;
2976 int atstart;
2977 char_u *pat;
2978 char_u *pp;
2979 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002980 char_u *ap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002981
2982 if (todo < 2)
2983 return SP_FORMERROR; /* need at least two bytes */
2984
2985 --todo;
2986 c = getc(fd); /* <compmax> */
2987 if (c < 2)
2988 c = MAXWLEN;
2989 slang->sl_compmax = c;
2990
2991 --todo;
2992 c = getc(fd); /* <compminlen> */
2993 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002994 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002995 slang->sl_compminlen = c;
2996
2997 --todo;
2998 c = getc(fd); /* <compsylmax> */
2999 if (c < 1)
3000 c = MAXWLEN;
3001 slang->sl_compsylmax = c;
3002
3003 /* Turn the COMPOUNDFLAGS items into a regexp pattern:
3004 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003005 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3006 * Conversion to utf-8 may double the size. */
3007 c = todo * 2 + 7;
3008#ifdef FEAT_MBYTE
3009 if (enc_utf8)
3010 c += todo * 2;
3011#endif
3012 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003013 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003014 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003015
Bram Moolenaard12a1322005-08-21 22:08:24 +00003016 /* We also need a list of all flags that can appear at the start and one
3017 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003018 cp = alloc(todo + 1);
3019 if (cp == NULL)
3020 {
3021 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003022 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003023 }
3024 slang->sl_compstartflags = cp;
3025 *cp = NUL;
3026
Bram Moolenaard12a1322005-08-21 22:08:24 +00003027 ap = alloc(todo + 1);
3028 if (ap == NULL)
3029 {
3030 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003031 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003032 }
3033 slang->sl_compallflags = ap;
3034 *ap = NUL;
3035
Bram Moolenaar5195e452005-08-19 20:32:47 +00003036 pp = pat;
3037 *pp++ = '^';
3038 *pp++ = '\\';
3039 *pp++ = '(';
3040
3041 atstart = 1;
3042 while (todo-- > 0)
3043 {
3044 c = getc(fd); /* <compflags> */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003045
3046 /* Add all flags to "sl_compallflags". */
3047 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003048 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003049 {
3050 *ap++ = c;
3051 *ap = NUL;
3052 }
3053
Bram Moolenaar5195e452005-08-19 20:32:47 +00003054 if (atstart != 0)
3055 {
3056 /* At start of item: copy flags to "sl_compstartflags". For a
3057 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3058 if (c == '[')
3059 atstart = 2;
3060 else if (c == ']')
3061 atstart = 0;
3062 else
3063 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003064 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003065 {
3066 *cp++ = c;
3067 *cp = NUL;
3068 }
3069 if (atstart == 1)
3070 atstart = 0;
3071 }
3072 }
3073 if (c == '/') /* slash separates two items */
3074 {
3075 *pp++ = '\\';
3076 *pp++ = '|';
3077 atstart = 1;
3078 }
3079 else /* normal char, "[abc]" and '*' are copied as-is */
3080 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003081 if (c == '+' || c == '~')
Bram Moolenaar5195e452005-08-19 20:32:47 +00003082 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003083#ifdef FEAT_MBYTE
3084 if (enc_utf8)
3085 pp += mb_char2bytes(c, pp);
3086 else
3087#endif
3088 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003089 }
3090 }
3091
3092 *pp++ = '\\';
3093 *pp++ = ')';
3094 *pp++ = '$';
3095 *pp = NUL;
3096
3097 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3098 vim_free(pat);
3099 if (slang->sl_compprog == NULL)
3100 return SP_FORMERROR;
3101
3102 return 0;
3103}
3104
Bram Moolenaar6de68532005-08-24 22:08:48 +00003105/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003106 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003107 * Like strchr() but independent of locale.
3108 */
3109 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003110byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003111 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003112 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003113{
3114 char_u *p;
3115
3116 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003117 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003118 return TRUE;
3119 return FALSE;
3120}
3121
Bram Moolenaar5195e452005-08-19 20:32:47 +00003122#define SY_MAXLEN 30
3123typedef struct syl_item_S
3124{
3125 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3126 int sy_len;
3127} syl_item_T;
3128
3129/*
3130 * Truncate "slang->sl_syllable" at the first slash and put the following items
3131 * in "slang->sl_syl_items".
3132 */
3133 static int
3134init_syl_tab(slang)
3135 slang_T *slang;
3136{
3137 char_u *p;
3138 char_u *s;
3139 int l;
3140 syl_item_T *syl;
3141
3142 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3143 p = vim_strchr(slang->sl_syllable, '/');
3144 while (p != NULL)
3145 {
3146 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003147 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003148 break;
3149 s = p;
3150 p = vim_strchr(p, '/');
3151 if (p == NULL)
3152 l = STRLEN(s);
3153 else
3154 l = p - s;
3155 if (l >= SY_MAXLEN)
3156 return SP_FORMERROR;
3157 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003158 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003159 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3160 + slang->sl_syl_items.ga_len++;
3161 vim_strncpy(syl->sy_chars, s, l);
3162 syl->sy_len = l;
3163 }
3164 return OK;
3165}
3166
3167/*
3168 * Count the number of syllables in "word".
3169 * When "word" contains spaces the syllables after the last space are counted.
3170 * Returns zero if syllables are not defines.
3171 */
3172 static int
3173count_syllables(slang, word)
3174 slang_T *slang;
3175 char_u *word;
3176{
3177 int cnt = 0;
3178 int skip = FALSE;
3179 char_u *p;
3180 int len;
3181 int i;
3182 syl_item_T *syl;
3183 int c;
3184
3185 if (slang->sl_syllable == NULL)
3186 return 0;
3187
3188 for (p = word; *p != NUL; p += len)
3189 {
3190 /* When running into a space reset counter. */
3191 if (*p == ' ')
3192 {
3193 len = 1;
3194 cnt = 0;
3195 continue;
3196 }
3197
3198 /* Find longest match of syllable items. */
3199 len = 0;
3200 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3201 {
3202 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3203 if (syl->sy_len > len
3204 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3205 len = syl->sy_len;
3206 }
3207 if (len != 0) /* found a match, count syllable */
3208 {
3209 ++cnt;
3210 skip = FALSE;
3211 }
3212 else
3213 {
3214 /* No recognized syllable item, at least a syllable char then? */
3215#ifdef FEAT_MBYTE
3216 c = mb_ptr2char(p);
3217 len = (*mb_ptr2len)(p);
3218#else
3219 c = *p;
3220 len = 1;
3221#endif
3222 if (vim_strchr(slang->sl_syllable, c) == NULL)
3223 skip = FALSE; /* No, search for next syllable */
3224 else if (!skip)
3225 {
3226 ++cnt; /* Yes, count it */
3227 skip = TRUE; /* don't count following syllable chars */
3228 }
3229 }
3230 }
3231 return cnt;
3232}
3233
3234/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003235 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003236 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003237 */
3238 static int
3239set_sofo(lp, from, to)
3240 slang_T *lp;
3241 char_u *from;
3242 char_u *to;
3243{
3244 int i;
3245
3246#ifdef FEAT_MBYTE
3247 garray_T *gap;
3248 char_u *s;
3249 char_u *p;
3250 int c;
3251 int *inp;
3252
3253 if (has_mbyte)
3254 {
3255 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3256 * characters. The index is the low byte of the character.
3257 * The list contains from-to pairs with a terminating NUL.
3258 * sl_sal_first[] is used for latin1 "from" characters. */
3259 gap = &lp->sl_sal;
3260 ga_init2(gap, sizeof(int *), 1);
3261 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003262 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003263 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3264 gap->ga_len = 256;
3265
3266 /* First count the number of items for each list. Temporarily use
3267 * sl_sal_first[] for this. */
3268 for (p = from, s = to; *p != NUL && *s != NUL; )
3269 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003270 c = mb_cptr2char_adv(&p);
3271 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003272 if (c >= 256)
3273 ++lp->sl_sal_first[c & 0xff];
3274 }
3275 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003276 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003277
3278 /* Allocate the lists. */
3279 for (i = 0; i < 256; ++i)
3280 if (lp->sl_sal_first[i] > 0)
3281 {
3282 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3283 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003284 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003285 ((int **)gap->ga_data)[i] = (int *)p;
3286 *(int *)p = 0;
3287 }
3288
3289 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3290 * list. */
3291 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3292 for (p = from, s = to; *p != NUL && *s != NUL; )
3293 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003294 c = mb_cptr2char_adv(&p);
3295 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003296 if (c >= 256)
3297 {
3298 /* Append the from-to chars at the end of the list with
3299 * the low byte. */
3300 inp = ((int **)gap->ga_data)[c & 0xff];
3301 while (*inp != 0)
3302 ++inp;
3303 *inp++ = c; /* from char */
3304 *inp++ = i; /* to char */
3305 *inp++ = NUL; /* NUL at the end */
3306 }
3307 else
3308 /* mapping byte to char is done in sl_sal_first[] */
3309 lp->sl_sal_first[c] = i;
3310 }
3311 }
3312 else
3313#endif
3314 {
3315 /* mapping bytes to bytes is done in sl_sal_first[] */
3316 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003317 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003318
3319 for (i = 0; to[i] != NUL; ++i)
3320 lp->sl_sal_first[from[i]] = to[i];
3321 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3322 }
3323
Bram Moolenaar5195e452005-08-19 20:32:47 +00003324 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003325}
3326
3327/*
3328 * Fill the first-index table for "lp".
3329 */
3330 static void
3331set_sal_first(lp)
3332 slang_T *lp;
3333{
3334 salfirst_T *sfirst;
3335 int i;
3336 salitem_T *smp;
3337 int c;
3338 garray_T *gap = &lp->sl_sal;
3339
3340 sfirst = lp->sl_sal_first;
3341 for (i = 0; i < 256; ++i)
3342 sfirst[i] = -1;
3343 smp = (salitem_T *)gap->ga_data;
3344 for (i = 0; i < gap->ga_len; ++i)
3345 {
3346#ifdef FEAT_MBYTE
3347 if (has_mbyte)
3348 /* Use the lowest byte of the first character. For latin1 it's
3349 * the character, for other encodings it should differ for most
3350 * characters. */
3351 c = *smp[i].sm_lead_w & 0xff;
3352 else
3353#endif
3354 c = *smp[i].sm_lead;
3355 if (sfirst[c] == -1)
3356 {
3357 sfirst[c] = i;
3358#ifdef FEAT_MBYTE
3359 if (has_mbyte)
3360 {
3361 int n;
3362
3363 /* Make sure all entries with this byte are following each
3364 * other. Move the ones that are in the wrong position. Do
3365 * keep the same ordering! */
3366 while (i + 1 < gap->ga_len
3367 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3368 /* Skip over entry with same index byte. */
3369 ++i;
3370
3371 for (n = 1; i + n < gap->ga_len; ++n)
3372 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3373 {
3374 salitem_T tsal;
3375
3376 /* Move entry with same index byte after the entries
3377 * we already found. */
3378 ++i;
3379 --n;
3380 tsal = smp[i + n];
3381 mch_memmove(smp + i + 1, smp + i,
3382 sizeof(salitem_T) * n);
3383 smp[i] = tsal;
3384 }
3385 }
3386#endif
3387 }
3388 }
3389}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003390
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003391#ifdef FEAT_MBYTE
3392/*
3393 * Turn a multi-byte string into a wide character string.
3394 * Return it in allocated memory (NULL for out-of-memory)
3395 */
3396 static int *
3397mb_str2wide(s)
3398 char_u *s;
3399{
3400 int *res;
3401 char_u *p;
3402 int i = 0;
3403
3404 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3405 if (res != NULL)
3406 {
3407 for (p = s; *p != NUL; )
3408 res[i++] = mb_ptr2char_adv(&p);
3409 res[i] = NUL;
3410 }
3411 return res;
3412}
3413#endif
3414
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003415/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003416 * Read one row of siblings from the spell file and store it in the byte array
3417 * "byts" and index array "idxs". Recursively read the children.
3418 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00003419 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00003420 *
3421 * Returns the index follosing the siblings.
3422 * Returns -1 if the file is shorter than expected.
3423 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003424 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003425 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003426read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003427 FILE *fd;
3428 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003429 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003430 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003431 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003432 int prefixtree; /* TRUE for reading PREFIXTREE */
3433 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003434{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003435 int len;
3436 int i;
3437 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003438 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003439 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003440 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003441#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003442
Bram Moolenaar51485f02005-06-04 21:55:20 +00003443 len = getc(fd); /* <siblingcount> */
3444 if (len <= 0)
3445 return -1;
3446
3447 if (startidx + len >= maxidx)
3448 return -2;
3449 byts[idx++] = len;
3450
3451 /* Read the byte values, flag/region bytes and shared indexes. */
3452 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003453 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003454 c = getc(fd); /* <byte> */
3455 if (c < 0)
3456 return -1;
3457 if (c <= BY_SPECIAL)
3458 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003459 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003460 {
3461 /* No flags, all regions. */
3462 idxs[idx] = 0;
3463 c = 0;
3464 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003465 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003466 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003467 if (prefixtree)
3468 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003469 /* Read the optional pflags byte, the prefix ID and the
3470 * condition nr. In idxs[] store the prefix ID in the low
3471 * byte, the condition index shifted up 8 bits, the flags
3472 * shifted up 24 bits. */
3473 if (c == BY_FLAGS)
3474 c = getc(fd) << 24; /* <pflags> */
3475 else
3476 c = 0;
3477
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003478 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003479
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003480 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
3481 if (n >= maxprefcondnr)
3482 return -2;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003483 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003484 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003485 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003486 {
3487 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003488 * idxs[] the flags go in the low two bytes, region above
3489 * that and prefix ID above the region. */
3490 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003491 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003492 if (c2 == BY_FLAGS2)
3493 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003494 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003495 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003496 if (c & WF_AFX)
3497 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003498 }
3499
Bram Moolenaar51485f02005-06-04 21:55:20 +00003500 idxs[idx] = c;
3501 c = 0;
3502 }
3503 else /* c == BY_INDEX */
3504 {
3505 /* <nodeidx> */
3506 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
3507 if (n < 0 || n >= maxidx)
3508 return -2;
3509 idxs[idx] = n + SHARED_MASK;
3510 c = getc(fd); /* <xbyte> */
3511 }
3512 }
3513 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003514 }
3515
Bram Moolenaar51485f02005-06-04 21:55:20 +00003516 /* Recursively read the children for non-shared siblings.
3517 * Skip the end-of-word ones (zero byte value) and the shared ones (and
3518 * remove SHARED_MASK) */
3519 for (i = 1; i <= len; ++i)
3520 if (byts[startidx + i] != 0)
3521 {
3522 if (idxs[startidx + i] & SHARED_MASK)
3523 idxs[startidx + i] &= ~SHARED_MASK;
3524 else
3525 {
3526 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003527 idx = read_tree(fd, byts, idxs, maxidx, idx,
3528 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003529 if (idx < 0)
3530 break;
3531 }
3532 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003533
Bram Moolenaar51485f02005-06-04 21:55:20 +00003534 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003535}
3536
3537/*
3538 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003539 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003540 */
3541 char_u *
3542did_set_spelllang(buf)
3543 buf_T *buf;
3544{
3545 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003546 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003547 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00003548 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003549 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003550 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003551 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003552 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003553 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003554 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003555 int len;
3556 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003557 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003558 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003559 char_u *use_region = NULL;
3560 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003561 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003562 int i, j;
3563 langp_T *lp, *lp2;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003564
3565 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003566 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003567
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003568 /* loop over comma separated language names. */
3569 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003570 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003571 /* Get one language name. */
3572 copy_option_part(&splp, lang, MAXWLEN, ",");
3573
Bram Moolenaar5482f332005-04-17 20:18:43 +00003574 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003575 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003576
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003577 /* If the name ends in ".spl" use it as the name of the spell file.
3578 * If there is a region name let "region" point to it and remove it
3579 * from the name. */
3580 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
3581 {
3582 filename = TRUE;
3583
Bram Moolenaarb6356332005-07-18 21:40:44 +00003584 /* Locate a region and remove it from the file name. */
3585 p = vim_strchr(gettail(lang), '_');
3586 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
3587 && !ASCII_ISALPHA(p[3]))
3588 {
3589 vim_strncpy(region_cp, p + 1, 2);
3590 mch_memmove(p, p + 3, len - (p - lang) - 2);
3591 len -= 3;
3592 region = region_cp;
3593 }
3594 else
3595 dont_use_region = TRUE;
3596
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003597 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003598 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
3599 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003600 break;
3601 }
3602 else
3603 {
3604 filename = FALSE;
3605 if (len > 3 && lang[len - 3] == '_')
3606 {
3607 region = lang + len - 2;
3608 len -= 3;
3609 lang[len] = NUL;
3610 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003611 else
3612 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003613
3614 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003615 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
3616 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003617 break;
3618 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003619
Bram Moolenaarb6356332005-07-18 21:40:44 +00003620 if (region != NULL)
3621 {
3622 /* If the region differs from what was used before then don't
3623 * use it for 'spellfile'. */
3624 if (use_region != NULL && STRCMP(region, use_region) != 0)
3625 dont_use_region = TRUE;
3626 use_region = region;
3627 }
3628
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003629 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003630 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003631 {
3632 if (filename)
3633 (void)spell_load_file(lang, lang, NULL, FALSE);
3634 else
3635 spell_load_lang(lang);
3636 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003637
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003638 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003639 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003640 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003641 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
3642 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
3643 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003644 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003645 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003646 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003647 {
3648 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003649 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003650 if (c == REGION_ALL)
3651 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003652 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003653 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003654 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003655 /* This addition file is for other regions. */
3656 region_mask = 0;
3657 }
3658 else
3659 /* This is probably an error. Give a warning and
3660 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003661 smsg((char_u *)
3662 _("Warning: region %s not supported"),
3663 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003664 }
3665 else
3666 region_mask = 1 << c;
3667 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003668
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003669 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003670 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003671 if (ga_grow(&ga, 1) == FAIL)
3672 {
3673 ga_clear(&ga);
3674 return e_outofmem;
3675 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003676 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003677 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
3678 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003679 use_midword(slang, buf);
3680 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003681 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003682 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003683 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003684 }
3685
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003686 /* round 0: load int_wordlist, if possible.
3687 * round 1: load first name in 'spellfile'.
3688 * round 2: load second name in 'spellfile.
3689 * etc. */
3690 spf = curbuf->b_p_spf;
3691 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003692 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003693 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003694 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003695 /* Internal wordlist, if there is one. */
3696 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003697 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003698 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003699 }
3700 else
3701 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003702 /* One entry in 'spellfile'. */
3703 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
3704 STRCAT(spf_name, ".spl");
3705
3706 /* If it was already found above then skip it. */
3707 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003708 {
3709 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
3710 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003711 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003712 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003713 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003714 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003715 }
3716
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003717 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003718 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
3719 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003720 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003721 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003722 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003723 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003724 * region name, the region is ignored otherwise. for int_wordlist
3725 * use an arbitrary name. */
3726 if (round == 0)
3727 STRCPY(lang, "internal wordlist");
3728 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00003729 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003730 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003731 p = vim_strchr(lang, '.');
3732 if (p != NULL)
3733 *p = NUL; /* truncate at ".encoding.add" */
3734 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003735 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003736
3737 /* If one of the languages has NOBREAK we assume the addition
3738 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003739 if (slang != NULL && nobreak)
3740 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003741 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003742 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003743 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003744 region_mask = REGION_ALL;
3745 if (use_region != NULL && !dont_use_region)
3746 {
3747 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003748 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003749 if (c != REGION_ALL)
3750 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003751 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003752 /* This spell file is for other regions. */
3753 region_mask = 0;
3754 }
3755
3756 if (region_mask != 0)
3757 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003758 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
3759 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
3760 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003761 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
3762 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003763 use_midword(slang, buf);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003764 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003765 }
3766 }
3767
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003768 /* Everything is fine, store the new b_langp value. */
3769 ga_clear(&buf->b_langp);
3770 buf->b_langp = ga;
3771
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003772 /* For each language figure out what language to use for sound folding and
3773 * REP items. If the language doesn't support it itself use another one
3774 * with the same name. E.g. for "en-math" use "en". */
3775 for (i = 0; i < ga.ga_len; ++i)
3776 {
3777 lp = LANGP_ENTRY(ga, i);
3778
3779 /* sound folding */
3780 if (lp->lp_slang->sl_sal.ga_len > 0)
3781 /* language does sound folding itself */
3782 lp->lp_sallang = lp->lp_slang;
3783 else
3784 /* find first similar language that does sound folding */
3785 for (j = 0; j < ga.ga_len; ++j)
3786 {
3787 lp2 = LANGP_ENTRY(ga, j);
3788 if (lp2->lp_slang->sl_sal.ga_len > 0
3789 && STRNCMP(lp->lp_slang->sl_name,
3790 lp2->lp_slang->sl_name, 2) == 0)
3791 {
3792 lp->lp_sallang = lp2->lp_slang;
3793 break;
3794 }
3795 }
3796
3797 /* REP items */
3798 if (lp->lp_slang->sl_rep.ga_len > 0)
3799 /* language has REP items itself */
3800 lp->lp_replang = lp->lp_slang;
3801 else
3802 /* find first similar language that does sound folding */
3803 for (j = 0; j < ga.ga_len; ++j)
3804 {
3805 lp2 = LANGP_ENTRY(ga, j);
3806 if (lp2->lp_slang->sl_rep.ga_len > 0
3807 && STRNCMP(lp->lp_slang->sl_name,
3808 lp2->lp_slang->sl_name, 2) == 0)
3809 {
3810 lp->lp_replang = lp2->lp_slang;
3811 break;
3812 }
3813 }
3814 }
3815
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003816 return NULL;
3817}
3818
3819/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003820 * Clear the midword characters for buffer "buf".
3821 */
3822 static void
3823clear_midword(buf)
3824 buf_T *buf;
3825{
3826 vim_memset(buf->b_spell_ismw, 0, 256);
3827#ifdef FEAT_MBYTE
3828 vim_free(buf->b_spell_ismw_mb);
3829 buf->b_spell_ismw_mb = NULL;
3830#endif
3831}
3832
3833/*
3834 * Use the "sl_midword" field of language "lp" for buffer "buf".
3835 * They add up to any currently used midword characters.
3836 */
3837 static void
3838use_midword(lp, buf)
3839 slang_T *lp;
3840 buf_T *buf;
3841{
3842 char_u *p;
3843
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003844 if (lp->sl_midword == NULL) /* there aren't any */
3845 return;
3846
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003847 for (p = lp->sl_midword; *p != NUL; )
3848#ifdef FEAT_MBYTE
3849 if (has_mbyte)
3850 {
3851 int c, l, n;
3852 char_u *bp;
3853
3854 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003855 l = (*mb_ptr2len)(p);
3856 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003857 buf->b_spell_ismw[c] = TRUE;
3858 else if (buf->b_spell_ismw_mb == NULL)
3859 /* First multi-byte char in "b_spell_ismw_mb". */
3860 buf->b_spell_ismw_mb = vim_strnsave(p, l);
3861 else
3862 {
3863 /* Append multi-byte chars to "b_spell_ismw_mb". */
3864 n = STRLEN(buf->b_spell_ismw_mb);
3865 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
3866 if (bp != NULL)
3867 {
3868 vim_free(buf->b_spell_ismw_mb);
3869 buf->b_spell_ismw_mb = bp;
3870 vim_strncpy(bp + n, p, l);
3871 }
3872 }
3873 p += l;
3874 }
3875 else
3876#endif
3877 buf->b_spell_ismw[*p++] = TRUE;
3878}
3879
3880/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003881 * Find the region "region[2]" in "rp" (points to "sl_regions").
3882 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003883 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003884 */
3885 static int
3886find_region(rp, region)
3887 char_u *rp;
3888 char_u *region;
3889{
3890 int i;
3891
3892 for (i = 0; ; i += 2)
3893 {
3894 if (rp[i] == NUL)
3895 return REGION_ALL;
3896 if (rp[i] == region[0] && rp[i + 1] == region[1])
3897 break;
3898 }
3899 return i / 2;
3900}
3901
3902/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003903 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003904 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00003905 * Word WF_ONECAP
3906 * W WORD WF_ALLCAP
3907 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003908 */
3909 static int
3910captype(word, end)
3911 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003912 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003913{
3914 char_u *p;
3915 int c;
3916 int firstcap;
3917 int allcap;
3918 int past_second = FALSE; /* past second word char */
3919
3920 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003921 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003922 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003923 return 0; /* only non-word characters, illegal word */
3924#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00003925 if (has_mbyte)
3926 c = mb_ptr2char_adv(&p);
3927 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003928#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00003929 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003930 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003931
3932 /*
3933 * Need to check all letters to find a word with mixed upper/lower.
3934 * But a word with an upper char only at start is a ONECAP.
3935 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003936 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003937 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003938 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003939 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003940 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003941 {
3942 /* UUl -> KEEPCAP */
3943 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003944 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003945 allcap = FALSE;
3946 }
3947 else if (!allcap)
3948 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003949 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003950 past_second = TRUE;
3951 }
3952
3953 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003954 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003955 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003956 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003957 return 0;
3958}
3959
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003960/*
3961 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
3962 * capital. So that make_case_word() can turn WOrd into Word.
3963 * Add ALLCAP for "WOrD".
3964 */
3965 static int
3966badword_captype(word, end)
3967 char_u *word;
3968 char_u *end;
3969{
3970 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003971 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003972 int l, u;
3973 int first;
3974 char_u *p;
3975
3976 if (flags & WF_KEEPCAP)
3977 {
3978 /* Count the number of UPPER and lower case letters. */
3979 l = u = 0;
3980 first = FALSE;
3981 for (p = word; p < end; mb_ptr_adv(p))
3982 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003983 c = PTR2CHAR(p);
3984 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003985 {
3986 ++u;
3987 if (p == word)
3988 first = TRUE;
3989 }
3990 else
3991 ++l;
3992 }
3993
3994 /* If there are more UPPER than lower case letters suggest an
3995 * ALLCAP word. Otherwise, if the first letter is UPPER then
3996 * suggest ONECAP. Exception: "ALl" most likely should be "All",
3997 * require three upper case letters. */
3998 if (u > l && u > 2)
3999 flags |= WF_ALLCAP;
4000 else if (first)
4001 flags |= WF_ONECAP;
4002 }
4003 return flags;
4004}
4005
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004006# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4007/*
4008 * Free all languages.
4009 */
4010 void
4011spell_free_all()
4012{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004013 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004014 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004015 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004016
4017 /* Go through all buffers and handle 'spelllang'. */
4018 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4019 ga_clear(&buf->b_langp);
4020
4021 while (first_lang != NULL)
4022 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004023 slang = first_lang;
4024 first_lang = slang->sl_next;
4025 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004026 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004027
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004028 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004029 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004030 /* Delete the internal wordlist and its .spl file */
4031 mch_remove(int_wordlist);
4032 int_wordlist_spl(fname);
4033 mch_remove(fname);
4034 vim_free(int_wordlist);
4035 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004036 }
4037
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004038 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004039}
4040# endif
4041
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004042# if defined(FEAT_MBYTE) || defined(PROTO)
4043/*
4044 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004045 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004046 */
4047 void
4048spell_reload()
4049{
4050 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004051 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004052
Bram Moolenaarea408852005-06-25 22:49:46 +00004053 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004054 init_spell_chartab();
4055
4056 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004057 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004058
4059 /* Go through all buffers and handle 'spelllang'. */
4060 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4061 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004062 /* Only load the wordlists when 'spelllang' is set and there is a
4063 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004064 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004065 {
4066 FOR_ALL_WINDOWS(wp)
4067 if (wp->w_buffer == buf && wp->w_p_spell)
4068 {
4069 (void)did_set_spelllang(buf);
4070# ifdef FEAT_WINDOWS
4071 break;
4072# endif
4073 }
4074 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004075 }
4076}
4077# endif
4078
Bram Moolenaarb765d632005-06-07 21:00:02 +00004079/*
4080 * Reload the spell file "fname" if it's loaded.
4081 */
4082 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004083spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004084 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004085 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004086{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004087 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004088 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004089
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004090 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004091 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004092 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004093 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004094 slang_clear(slang);
4095 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004096 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004097 slang_clear(slang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004098 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004099 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004100 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004101 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004102
4103 /* When "zg" was used and the file wasn't loaded yet, should redo
4104 * 'spelllang' to get it loaded. */
4105 if (added_word && !didit)
4106 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004107}
4108
4109
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004110/*
4111 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004112 */
4113
Bram Moolenaar51485f02005-06-04 21:55:20 +00004114#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004115 and .dic file. */
4116/*
4117 * Main structure to store the contents of a ".aff" file.
4118 */
4119typedef struct afffile_S
4120{
4121 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004122 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004123 int af_slash; /* character used in word for slash */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004124 unsigned af_rar; /* RAR ID for rare word */
4125 unsigned af_kep; /* KEP ID for keep-case word */
4126 unsigned af_bad; /* BAD ID for banned word */
4127 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004128 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004129 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004130 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4131 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004132 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004133} afffile_T;
4134
Bram Moolenaar6de68532005-08-24 22:08:48 +00004135#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004136#define AFT_LONG 1 /* flags are two characters */
4137#define AFT_CAPLONG 2 /* flags are one or two characters */
4138#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004139
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004140typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004141/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4142struct affentry_S
4143{
4144 affentry_T *ae_next; /* next affix with same name/number */
4145 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4146 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004147 char_u *ae_cond; /* condition (NULL for ".") */
4148 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004149 char_u ae_rare; /* rare affix */
4150 char_u ae_nocomp; /* word with affix not compoundable */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004151};
4152
Bram Moolenaar6de68532005-08-24 22:08:48 +00004153#ifdef FEAT_MBYTE
4154# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4155#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004156# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004157#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004158
Bram Moolenaar51485f02005-06-04 21:55:20 +00004159/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4160typedef struct affheader_S
4161{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004162 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4163 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4164 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004165 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004166 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004167 affentry_T *ah_first; /* first affix entry */
4168} affheader_T;
4169
4170#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4171
Bram Moolenaar6de68532005-08-24 22:08:48 +00004172/* Flag used in compound items. */
4173typedef struct compitem_S
4174{
4175 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4176 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4177 int ci_newID; /* affix ID after renumbering. */
4178} compitem_T;
4179
4180#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4181
Bram Moolenaar51485f02005-06-04 21:55:20 +00004182/*
4183 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004184 * the need to keep track of each allocated thing, everything is freed all at
4185 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004186 */
4187#define SBLOCKSIZE 16000 /* size of sb_data */
4188typedef struct sblock_S sblock_T;
4189struct sblock_S
4190{
4191 sblock_T *sb_next; /* next block in list */
4192 int sb_used; /* nr of bytes already in use */
4193 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004194};
4195
4196/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004197 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004198 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004199typedef struct wordnode_S wordnode_T;
4200struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004201{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004202 union /* shared to save space */
4203 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004204 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004205 int index; /* index in written nodes (valid after first
4206 round) */
4207 } wn_u1;
4208 union /* shared to save space */
4209 {
4210 wordnode_T *next; /* next node with same hash key */
4211 wordnode_T *wnode; /* parent node that will write this node */
4212 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004213 wordnode_T *wn_child; /* child (next byte in word) */
4214 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4215 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004216 int wn_refs; /* Nr. of references to this node. Only
4217 relevant for first node in a list of
4218 siblings, in following siblings it is
4219 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004220 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004221 char_u wn_affixID; /* when "wn_byte" is NUL: supported/required
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004222 prefix ID or 0 */
4223 short_u wn_flags; /* when "wn_byte" is NUL: WF_ flags */
4224 short wn_region; /* when "wn_byte" is NUL: region mask; for
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004225 PREFIXTREE it's the prefcondnr */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004226#ifdef SPELL_PRINTTREE
4227 int wn_nr; /* sequence nr for printing */
4228#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004229};
4230
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004231#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4232
Bram Moolenaar51485f02005-06-04 21:55:20 +00004233#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004234
Bram Moolenaar51485f02005-06-04 21:55:20 +00004235/*
4236 * Info used while reading the spell files.
4237 */
4238typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004239{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004240 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004241 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004242
Bram Moolenaar51485f02005-06-04 21:55:20 +00004243 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004244 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004245
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004246 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004247
Bram Moolenaar51485f02005-06-04 21:55:20 +00004248 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004249 long si_blocks_cnt; /* memory blocks allocated */
4250 long si_compress_cnt; /* words to add before lowering
4251 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004252 wordnode_T *si_first_free; /* List of nodes that have been freed during
4253 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004254 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004255#ifdef SPELL_PRINTTREE
4256 int si_wordnode_nr; /* sequence nr for nodes */
4257#endif
4258
4259
Bram Moolenaar51485f02005-06-04 21:55:20 +00004260 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004261 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004262 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004263 int si_region; /* region mask */
4264 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004265 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004266 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004267 int si_msg_count; /* number of words added since last message */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004268 int si_region_count; /* number of regions supported (1 when there
4269 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004270 char_u si_region_name[16]; /* region names; used only if
4271 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004272
4273 garray_T si_rep; /* list of fromto_T entries from REP lines */
4274 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004275 char_u *si_sofofr; /* SOFOFROM text */
4276 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004277 int si_followup; /* soundsalike: ? */
4278 int si_collapse; /* soundsalike: ? */
4279 int si_rem_accents; /* soundsalike: remove accents */
4280 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004281 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004282 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004283 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004284 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004285 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004286 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004287 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004288 garray_T si_prefcond; /* table with conditions for postponed
4289 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004290 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004291 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004292} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004293
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004294static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004295static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
4296static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
4297static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004298static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004299static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
4300static void aff_check_number __ARGS((int spinval, int affval, char *name));
4301static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004302static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004303static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4304static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00004305static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004306static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004307static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004308static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004309static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004310static 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 +00004311static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
4312static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
4313static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004314static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004315static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004316static 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 +00004317static 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 +00004318static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
4319static void deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
4320static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
4321static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
4322static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004323static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004324static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00004325static void clear_node __ARGS((wordnode_T *node));
4326static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004327static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004328static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004329
Bram Moolenaar53805d12005-08-01 07:08:33 +00004330/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
4331 * but it must be negative to indicate the prefix tree to tree_add_word().
4332 * Use a negative number with the lower 8 bits zero. */
4333#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004334
Bram Moolenaar5195e452005-08-19 20:32:47 +00004335/*
4336 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
4337 */
4338static long compress_start = 30000; /* memory / SBLOCKSIZE */
4339static long compress_inc = 100; /* memory / SBLOCKSIZE */
4340static long compress_added = 500000; /* word count */
4341
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004342#ifdef SPELL_PRINTTREE
4343/*
4344 * For debugging the tree code: print the current tree in a (more or less)
4345 * readable format, so that we can see what happens when adding a word and/or
4346 * compressing the tree.
4347 * Based on code from Olaf Seibert.
4348 */
4349#define PRINTLINESIZE 1000
4350#define PRINTWIDTH 6
4351
4352#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4353 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4354
4355static char line1[PRINTLINESIZE];
4356static char line2[PRINTLINESIZE];
4357static char line3[PRINTLINESIZE];
4358
4359 static void
4360spell_clear_flags(wordnode_T *node)
4361{
4362 wordnode_T *np;
4363
4364 for (np = node; np != NULL; np = np->wn_sibling)
4365 {
4366 np->wn_u1.index = FALSE;
4367 spell_clear_flags(np->wn_child);
4368 }
4369}
4370
4371 static void
4372spell_print_node(wordnode_T *node, int depth)
4373{
4374 if (node->wn_u1.index)
4375 {
4376 /* Done this node before, print the reference. */
4377 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
4378 PRINTSOME(line2, depth, " ", 0, 0);
4379 PRINTSOME(line3, depth, " ", 0, 0);
4380 msg(line1);
4381 msg(line2);
4382 msg(line3);
4383 }
4384 else
4385 {
4386 node->wn_u1.index = TRUE;
4387
4388 if (node->wn_byte != NUL)
4389 {
4390 if (node->wn_child != NULL)
4391 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
4392 else
4393 /* Cannot happen? */
4394 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
4395 }
4396 else
4397 PRINTSOME(line1, depth, " $ ", 0, 0);
4398
4399 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
4400
4401 if (node->wn_sibling != NULL)
4402 PRINTSOME(line3, depth, " | ", 0, 0);
4403 else
4404 PRINTSOME(line3, depth, " ", 0, 0);
4405
4406 if (node->wn_byte == NUL)
4407 {
4408 msg(line1);
4409 msg(line2);
4410 msg(line3);
4411 }
4412
4413 /* do the children */
4414 if (node->wn_byte != NUL && node->wn_child != NULL)
4415 spell_print_node(node->wn_child, depth + 1);
4416
4417 /* do the siblings */
4418 if (node->wn_sibling != NULL)
4419 {
4420 /* get rid of all parent details except | */
4421 STRCPY(line1, line3);
4422 STRCPY(line2, line3);
4423 spell_print_node(node->wn_sibling, depth);
4424 }
4425 }
4426}
4427
4428 static void
4429spell_print_tree(wordnode_T *root)
4430{
4431 if (root != NULL)
4432 {
4433 /* Clear the "wn_u1.index" fields, used to remember what has been
4434 * done. */
4435 spell_clear_flags(root);
4436
4437 /* Recursively print the tree. */
4438 spell_print_node(root, 0);
4439 }
4440}
4441#endif /* SPELL_PRINTTREE */
4442
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004443/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004444 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00004445 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004446 */
4447 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004448spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004449 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004450 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004451{
4452 FILE *fd;
4453 afffile_T *aff;
4454 char_u rline[MAXLINELEN];
4455 char_u *line;
4456 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004457#define MAXITEMCNT 7
4458 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004459 int itemcnt;
4460 char_u *p;
4461 int lnum = 0;
4462 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004463 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004464 int aff_todo = 0;
4465 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004466 char_u *low = NULL;
4467 char_u *fol = NULL;
4468 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004469 int do_rep;
4470 int do_sal;
4471 int do_map;
4472 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004473 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004474 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004475 int compminlen = 0; /* COMPOUNDMIN value */
4476 int compsylmax = 0; /* COMPOUNDSYLMAX value */
4477 int compmax = 0; /* COMPOUNDMAX value */
4478 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDFLAGS
4479 concatenated */
4480 char_u *midword = NULL; /* MIDWORD value */
4481 char_u *syllable = NULL; /* SYLLABLE value */
4482 char_u *sofofrom = NULL; /* SOFOFROM value */
4483 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004484
Bram Moolenaar51485f02005-06-04 21:55:20 +00004485 /*
4486 * Open the file.
4487 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004488 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004489 if (fd == NULL)
4490 {
4491 EMSG2(_(e_notopen), fname);
4492 return NULL;
4493 }
4494
Bram Moolenaarb765d632005-06-07 21:00:02 +00004495 if (spin->si_verbose || p_verbose > 2)
4496 {
4497 if (!spin->si_verbose)
4498 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004499 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004500 out_flush();
4501 if (!spin->si_verbose)
4502 verbose_leave();
4503 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004504
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004505 /* Only do REP lines when not done in another .aff file already. */
4506 do_rep = spin->si_rep.ga_len == 0;
4507
4508 /* Only do SAL lines when not done in another .aff file already. */
4509 do_sal = spin->si_sal.ga_len == 0;
4510
4511 /* Only do MAP lines when not done in another .aff file already. */
4512 do_map = spin->si_map.ga_len == 0;
4513
Bram Moolenaar51485f02005-06-04 21:55:20 +00004514 /*
4515 * Allocate and init the afffile_T structure.
4516 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004517 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004518 if (aff == NULL)
4519 return NULL;
4520 hash_init(&aff->af_pref);
4521 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004522 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004523
4524 /*
4525 * Read all the lines in the file one by one.
4526 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004527 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004528 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004529 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004530 ++lnum;
4531
4532 /* Skip comment lines. */
4533 if (*rline == '#')
4534 continue;
4535
4536 /* Convert from "SET" to 'encoding' when needed. */
4537 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004538#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004539 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004540 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004541 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004542 if (pc == NULL)
4543 {
4544 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4545 fname, lnum, rline);
4546 continue;
4547 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004548 line = pc;
4549 }
4550 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004551#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004552 {
4553 pc = NULL;
4554 line = rline;
4555 }
4556
4557 /* Split the line up in white separated items. Put a NUL after each
4558 * item. */
4559 itemcnt = 0;
4560 for (p = line; ; )
4561 {
4562 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
4563 ++p;
4564 if (*p == NUL)
4565 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004566 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004567 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004568 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004569 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004570 ++p;
4571 if (*p == NUL)
4572 break;
4573 *p++ = NUL;
4574 }
4575
4576 /* Handle non-empty lines. */
4577 if (itemcnt > 0)
4578 {
4579 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
4580 && aff->af_enc == NULL)
4581 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004582#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004583 /* Setup for conversion from "ENC" to 'encoding'. */
4584 aff->af_enc = enc_canonize(items[1]);
4585 if (aff->af_enc != NULL && !spin->si_ascii
4586 && convert_setup(&spin->si_conv, aff->af_enc,
4587 p_enc) == FAIL)
4588 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
4589 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004590 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004591#else
4592 smsg((char_u *)_("Conversion in %s not supported"), fname);
4593#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004594 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00004595 else if (STRCMP(items[0], "FLAG") == 0 && itemcnt == 2
4596 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004597 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004598 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00004599 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004600 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00004601 aff->af_flagtype = AFT_NUM;
4602 else if (STRCMP(items[1], "caplong") == 0)
4603 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004604 else
4605 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
4606 fname, lnum, items[1]);
4607 if (aff->af_rar != 0 || aff->af_kep != 0 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004608 || aff->af_needaffix != 0
4609 || aff->af_needcomp != 0
4610 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00004611 || aff->af_suff.ht_used > 0
4612 || aff->af_pref.ht_used > 0)
4613 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
4614 fname, lnum, items[1]);
4615 }
4616 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2
4617 && midword == NULL)
4618 {
4619 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004620 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00004621 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
4622 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004623 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004624 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004625 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004626 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004627 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004628 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004629 else if (STRCMP(items[0], "SLASH") == 0 && itemcnt == 2
4630 && aff->af_slash == 0)
4631 {
4632 aff->af_slash = items[1][0];
4633 if (items[1][1] != NUL)
4634 smsg((char_u *)_("Character used for SLASH must be ASCII; in %s line %d: %s"),
4635 fname, lnum, items[1]);
4636 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004637 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
4638 && aff->af_rar == 0)
4639 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004640 aff->af_rar = affitem2flag(aff->af_flagtype, items[1],
4641 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004642 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004643 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
4644 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004645 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004646 aff->af_kep = affitem2flag(aff->af_flagtype, items[1],
4647 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004648 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00004649 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
4650 && aff->af_bad == 0)
4651 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004652 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
4653 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004654 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004655 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
4656 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004657 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004658 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
4659 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004660 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004661 else if (STRCMP(items[0], "NEEDCOMPOUND") == 0 && itemcnt == 2
4662 && aff->af_needcomp == 0)
4663 {
4664 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
4665 fname, lnum);
4666 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004667 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004668 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004669 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004670 /* Turn flag "c" into COMPOUNDFLAGS compatible string "c+",
4671 * "Na" into "Na+", "1234" into "1234+". */
4672 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004673 if (p != NULL)
4674 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004675 STRCPY(p, items[1]);
4676 STRCAT(p, "+");
4677 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004678 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004679 }
4680 else if (STRCMP(items[0], "COMPOUNDFLAGS") == 0 && itemcnt == 2)
4681 {
4682 /* Concatenate this string to previously defined ones, using a
4683 * slash to separate them. */
4684 l = STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004685 if (compflags != NULL)
4686 l += STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004687 p = getroom(spin, l, FALSE);
4688 if (p != NULL)
4689 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004690 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004691 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004692 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004693 STRCAT(p, "/");
4694 }
4695 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004696 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004697 }
4698 }
4699 else if (STRCMP(items[0], "COMPOUNDMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004700 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004701 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004702 compmax = atoi((char *)items[1]);
4703 if (compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004704 smsg((char_u *)_("Wrong COMPOUNDMAX value in %s line %d: %s"),
4705 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004706 }
4707 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004708 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004709 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004710 compminlen = atoi((char *)items[1]);
4711 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004712 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
4713 fname, lnum, items[1]);
4714 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004715 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004716 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004717 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004718 compsylmax = atoi((char *)items[1]);
4719 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004720 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
4721 fname, lnum, items[1]);
4722 }
4723 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004724 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004725 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004726 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004727 }
Bram Moolenaar78622822005-08-23 21:00:13 +00004728 else if (STRCMP(items[0], "NOBREAK") == 0 && itemcnt == 1)
4729 {
4730 spin->si_nobreak = TRUE;
4731 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004732 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
4733 {
4734 aff->af_pfxpostpone = TRUE;
4735 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004736 else if ((STRCMP(items[0], "PFX") == 0
4737 || STRCMP(items[0], "SFX") == 0)
4738 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00004739 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004740 {
Bram Moolenaar95529562005-08-25 21:21:38 +00004741 int lasti = 4;
4742 char_u key[AH_KEY_LEN];
4743
4744 if (*items[0] == 'P')
4745 tp = &aff->af_pref;
4746 else
4747 tp = &aff->af_suff;
4748
4749 /* Myspell allows the same affix name to be used multiple
4750 * times. The affix files that do this have an undocumented
4751 * "S" flag on all but the last block, thus we check for that
4752 * and store it in ah_follows. */
4753 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
4754 hi = hash_find(tp, key);
4755 if (!HASHITEM_EMPTY(hi))
4756 {
4757 cur_aff = HI2AH(hi);
4758 if (cur_aff->ah_combine != (*items[2] == 'Y'))
4759 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
4760 fname, lnum, items[1]);
4761 if (!cur_aff->ah_follows)
4762 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
4763 fname, lnum, items[1]);
4764 }
4765 else
4766 {
4767 /* New affix letter. */
4768 cur_aff = (affheader_T *)getroom(spin,
4769 sizeof(affheader_T), TRUE);
4770 if (cur_aff == NULL)
4771 break;
4772 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
4773 fname, lnum);
4774 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
4775 break;
4776 if (cur_aff->ah_flag == aff->af_bad
4777 || cur_aff->ah_flag == aff->af_rar
4778 || cur_aff->ah_flag == aff->af_kep
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004779 || cur_aff->ah_flag == aff->af_needaffix
4780 || cur_aff->ah_flag == aff->af_needcomp)
4781 smsg((char_u *)_("Affix also used for BAD/RAR/KEP/NEEDAFFIX/NEEDCOMPOUND in %s line %d: %s"),
Bram Moolenaar95529562005-08-25 21:21:38 +00004782 fname, lnum, items[1]);
4783 STRCPY(cur_aff->ah_key, items[1]);
4784 hash_add(tp, cur_aff->ah_key);
4785
4786 cur_aff->ah_combine = (*items[2] == 'Y');
4787 }
4788
4789 /* Check for the "S" flag, which apparently means that another
4790 * block with the same affix name is following. */
4791 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
4792 {
4793 ++lasti;
4794 cur_aff->ah_follows = TRUE;
4795 }
4796 else
4797 cur_aff->ah_follows = FALSE;
4798
Bram Moolenaar8db73182005-06-17 21:51:16 +00004799 /* Myspell allows extra text after the item, but that might
4800 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00004801 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00004802 smsg((char_u *)_("Trailing text in %s line %d: %s"),
4803 fname, lnum, items[4]);
4804
Bram Moolenaar95529562005-08-25 21:21:38 +00004805 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004806 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
4807 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004808
Bram Moolenaar95529562005-08-25 21:21:38 +00004809 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004810 {
Bram Moolenaar95529562005-08-25 21:21:38 +00004811 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00004812 {
4813 /* Use a new number in the .spl file later, to be able
4814 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004815 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004816 cur_aff->ah_newID = ++spin->si_newprefID;
4817
4818 /* We only really use ah_newID if the prefix is
4819 * postponed. We know that only after handling all
4820 * the items. */
4821 did_postpone_prefix = FALSE;
4822 }
Bram Moolenaar95529562005-08-25 21:21:38 +00004823 else
4824 /* Did use the ID in a previous block. */
4825 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004826 }
Bram Moolenaar95529562005-08-25 21:21:38 +00004827
Bram Moolenaar51485f02005-06-04 21:55:20 +00004828 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004829 }
4830 else if ((STRCMP(items[0], "PFX") == 0
4831 || STRCMP(items[0], "SFX") == 0)
4832 && aff_todo > 0
4833 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00004834 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004835 {
4836 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004837 int rare = FALSE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004838 int nocomp = FALSE;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004839 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004840 int lasti = 5;
4841
Bram Moolenaar5195e452005-08-19 20:32:47 +00004842 /* Check for "rare" and "nocomp" after the other info. */
4843 while (itemcnt > lasti)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004844 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004845 if (!rare && STRICMP(items[lasti], "rare") == 0)
4846 {
4847 rare = TRUE;
4848 ++lasti;
4849 }
4850 else if (!nocomp && STRICMP(items[lasti], "nocomp") == 0)
4851 {
4852 nocomp = TRUE;
4853 ++lasti;
4854 }
4855 else
4856 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004857 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004858
Bram Moolenaar8db73182005-06-17 21:51:16 +00004859 /* Myspell allows extra text after the item, but that might
4860 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004861 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004862 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00004863
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004864 /* New item for an affix letter. */
4865 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004866 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004867 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004868 if (aff_entry == NULL)
4869 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004870 aff_entry->ae_rare = rare;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004871 aff_entry->ae_nocomp = nocomp;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004872
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004873 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004874 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004875 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004876 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004877
Bram Moolenaar51485f02005-06-04 21:55:20 +00004878 /* Don't use an affix entry with non-ASCII characters when
4879 * "spin->si_ascii" is TRUE. */
4880 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004881 || has_non_ascii(aff_entry->ae_add)))
4882 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00004883 aff_entry->ae_next = cur_aff->ah_first;
4884 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004885
4886 if (STRCMP(items[4], ".") != 0)
4887 {
4888 char_u buf[MAXLINELEN];
4889
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004890 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004891 if (*items[0] == 'P')
4892 sprintf((char *)buf, "^%s", items[4]);
4893 else
4894 sprintf((char *)buf, "%s$", items[4]);
4895 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004896 RE_MAGIC + RE_STRING + RE_STRICT);
4897 if (aff_entry->ae_prog == NULL)
4898 smsg((char_u *)_("Broken condition in %s line %d: %s"),
4899 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004900 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004901
4902 /* For postponed prefixes we need an entry in si_prefcond
4903 * for the condition. Use an existing one if possible. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004904 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004905 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004906 /* When the chop string is one lower-case letter and
4907 * the add string ends in the upper-case letter we set
4908 * the "upper" flag, clear "ae_chop" and remove the
4909 * letters from "ae_add". The condition must either
4910 * be empty or start with the same letter. */
4911 if (aff_entry->ae_chop != NULL
4912 && aff_entry->ae_add != NULL
4913#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004914 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00004915 aff_entry->ae_chop)] == NUL
4916#else
4917 && aff_entry->ae_chop[1] == NUL
4918#endif
4919 )
4920 {
4921 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004922
Bram Moolenaar53805d12005-08-01 07:08:33 +00004923 c = PTR2CHAR(aff_entry->ae_chop);
4924 c_up = SPELL_TOUPPER(c);
4925 if (c_up != c
4926 && (aff_entry->ae_cond == NULL
4927 || PTR2CHAR(aff_entry->ae_cond) == c))
4928 {
4929 p = aff_entry->ae_add
4930 + STRLEN(aff_entry->ae_add);
4931 mb_ptr_back(aff_entry->ae_add, p);
4932 if (PTR2CHAR(p) == c_up)
4933 {
4934 upper = TRUE;
4935 aff_entry->ae_chop = NULL;
4936 *p = NUL;
4937
4938 /* The condition is matched with the
4939 * actual word, thus must check for the
4940 * upper-case letter. */
4941 if (aff_entry->ae_cond != NULL)
4942 {
4943 char_u buf[MAXLINELEN];
4944#ifdef FEAT_MBYTE
4945 if (has_mbyte)
4946 {
4947 onecap_copy(items[4], buf, TRUE);
4948 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004949 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004950 }
4951 else
4952#endif
4953 *aff_entry->ae_cond = c_up;
4954 if (aff_entry->ae_cond != NULL)
4955 {
4956 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004957 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004958 vim_free(aff_entry->ae_prog);
4959 aff_entry->ae_prog = vim_regcomp(
4960 buf, RE_MAGIC + RE_STRING);
4961 }
4962 }
4963 }
4964 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004965 }
4966
Bram Moolenaar53805d12005-08-01 07:08:33 +00004967 if (aff_entry->ae_chop == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004968 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004969 int idx;
4970 char_u **pp;
4971 int n;
4972
Bram Moolenaar6de68532005-08-24 22:08:48 +00004973 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004974 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
4975 --idx)
4976 {
4977 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
4978 if (str_equal(p, aff_entry->ae_cond))
4979 break;
4980 }
4981 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
4982 {
4983 /* Not found, add a new condition. */
4984 idx = spin->si_prefcond.ga_len++;
4985 pp = ((char_u **)spin->si_prefcond.ga_data)
4986 + idx;
4987 if (aff_entry->ae_cond == NULL)
4988 *pp = NULL;
4989 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004990 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004991 aff_entry->ae_cond);
4992 }
4993
4994 /* Add the prefix to the prefix tree. */
4995 if (aff_entry->ae_add == NULL)
4996 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004997 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00004998 p = aff_entry->ae_add;
4999 /* PFX_FLAGS is a negative number, so that
5000 * tree_add_word() knows this is the prefix tree. */
5001 n = PFX_FLAGS;
5002 if (rare)
5003 n |= WFP_RARE;
5004 if (!cur_aff->ah_combine)
5005 n |= WFP_NC;
5006 if (upper)
5007 n |= WFP_UP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005008 tree_add_word(spin, p, spin->si_prefroot, n,
5009 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005010 did_postpone_prefix = TRUE;
5011 }
5012
5013 /* Didn't actually use ah_newID, backup si_newprefID. */
5014 if (aff_todo == 0 && !did_postpone_prefix)
5015 {
5016 --spin->si_newprefID;
5017 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005018 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005019 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005020 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005021 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005022 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2
5023 && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005024 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005025 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005026 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005027 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2
5028 && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005029 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005030 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005031 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005032 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2
5033 && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005034 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005035 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005036 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005037 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005038 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005039 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005040 if (!isdigit(*items[1]))
5041 smsg((char_u *)_("Expected REP count in %s line %d"),
5042 fname, lnum);
5043 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005044 else if (STRCMP(items[0], "REP") == 0 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005045 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005046 /* REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005047 /* Myspell ignores extra arguments, we require it starts with
5048 * # to detect mistakes. */
5049 if (itemcnt > 3 && items[3][0] != '#')
5050 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005051 if (do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005052 {
5053 /* Replace underscore with space (can't include a space
5054 * directly). */
5055 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5056 if (*p == '_')
5057 *p = ' ';
5058 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5059 if (*p == '_')
5060 *p = ' ';
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005061 add_fromto(spin, &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005062 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005063 }
5064 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
5065 {
5066 /* MAP item or count */
5067 if (!found_map)
5068 {
5069 /* First line contains the count. */
5070 found_map = TRUE;
5071 if (!isdigit(*items[1]))
5072 smsg((char_u *)_("Expected MAP count in %s line %d"),
5073 fname, lnum);
5074 }
5075 else if (do_map)
5076 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005077 int c;
5078
5079 /* Check that every character appears only once. */
5080 for (p = items[1]; *p != NUL; )
5081 {
5082#ifdef FEAT_MBYTE
5083 c = mb_ptr2char_adv(&p);
5084#else
5085 c = *p++;
5086#endif
5087 if ((spin->si_map.ga_len > 0
5088 && vim_strchr(spin->si_map.ga_data, c)
5089 != NULL)
5090 || vim_strchr(p, c) != NULL)
5091 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5092 fname, lnum);
5093 }
5094
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005095 /* We simply concatenate all the MAP strings, separated by
5096 * slashes. */
5097 ga_concat(&spin->si_map, items[1]);
5098 ga_append(&spin->si_map, '/');
5099 }
5100 }
5101 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
5102 {
5103 if (do_sal)
5104 {
5105 /* SAL item (sounds-a-like)
5106 * Either one of the known keys or a from-to pair. */
5107 if (STRCMP(items[1], "followup") == 0)
5108 spin->si_followup = sal_to_bool(items[2]);
5109 else if (STRCMP(items[1], "collapse_result") == 0)
5110 spin->si_collapse = sal_to_bool(items[2]);
5111 else if (STRCMP(items[1], "remove_accents") == 0)
5112 spin->si_rem_accents = sal_to_bool(items[2]);
5113 else
5114 /* when "to" is "_" it means empty */
5115 add_fromto(spin, &spin->si_sal, items[1],
5116 STRCMP(items[2], "_") == 0 ? (char_u *)""
5117 : items[2]);
5118 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005119 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005120 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005121 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005122 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005123 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005124 }
5125 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005126 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005127 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005128 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005129 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005130 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005131 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005132 fname, lnum, items[0]);
5133 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005134 }
5135
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005136 if (fol != NULL || low != NULL || upp != NULL)
5137 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005138 if (spin->si_clear_chartab)
5139 {
5140 /* Clear the char type tables, don't want to use any of the
5141 * currently used spell properties. */
5142 init_spell_chartab();
5143 spin->si_clear_chartab = FALSE;
5144 }
5145
Bram Moolenaar3982c542005-06-08 21:56:31 +00005146 /*
5147 * Don't write a word table for an ASCII file, so that we don't check
5148 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005149 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00005150 * mb_get_class(), the list of chars in the file will be incomplete.
5151 */
5152 if (!spin->si_ascii
5153#ifdef FEAT_MBYTE
5154 && !enc_utf8
5155#endif
5156 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005157 {
5158 if (fol == NULL || low == NULL || upp == NULL)
5159 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
5160 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00005161 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005162 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005163
5164 vim_free(fol);
5165 vim_free(low);
5166 vim_free(upp);
5167 }
5168
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005169 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005170 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005171 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005172 aff_check_number(spin->si_compmax, compmax, "COMPOUNDMAX");
5173 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005174 }
5175
Bram Moolenaar6de68532005-08-24 22:08:48 +00005176 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005177 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005178 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
5179 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005180 }
5181
Bram Moolenaar6de68532005-08-24 22:08:48 +00005182 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005183 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005184 if (syllable == NULL)
5185 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
5186 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
5187 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005188 }
5189
Bram Moolenaar6de68532005-08-24 22:08:48 +00005190 if (compflags != NULL)
5191 process_compflags(spin, aff, compflags);
5192
5193 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005194 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005195 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005196 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005197 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005198 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005199 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005200 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005201 MSG(_("Too many posponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005202 }
5203
Bram Moolenaar6de68532005-08-24 22:08:48 +00005204 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005205 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005206 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
5207 spin->si_syllable = syllable;
5208 }
5209
5210 if (sofofrom != NULL || sofoto != NULL)
5211 {
5212 if (sofofrom == NULL || sofoto == NULL)
5213 smsg((char_u *)_("Missing SOFO%s line in %s"),
5214 sofofrom == NULL ? "FROM" : "TO", fname);
5215 else if (spin->si_sal.ga_len > 0)
5216 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005217 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005218 {
5219 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
5220 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
5221 spin->si_sofofr = sofofrom;
5222 spin->si_sofoto = sofoto;
5223 }
5224 }
5225
5226 if (midword != NULL)
5227 {
5228 aff_check_string(spin->si_midword, midword, "MIDWORD");
5229 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005230 }
5231
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005232 vim_free(pc);
5233 fclose(fd);
5234 return aff;
5235}
5236
5237/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005238 * Turn an affix flag name into a number, according to the FLAG type.
5239 * returns zero for failure.
5240 */
5241 static unsigned
5242affitem2flag(flagtype, item, fname, lnum)
5243 int flagtype;
5244 char_u *item;
5245 char_u *fname;
5246 int lnum;
5247{
5248 unsigned res;
5249 char_u *p = item;
5250
5251 res = get_affitem(flagtype, &p);
5252 if (res == 0)
5253 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005254 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005255 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
5256 fname, lnum, item);
5257 else
5258 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
5259 fname, lnum, item);
5260 }
5261 if (*p != NUL)
5262 {
5263 smsg((char_u *)_(e_affname), fname, lnum, item);
5264 return 0;
5265 }
5266
5267 return res;
5268}
5269
5270/*
5271 * Get one affix name from "*pp" and advance the pointer.
5272 * Returns zero for an error, still advances the pointer then.
5273 */
5274 static unsigned
5275get_affitem(flagtype, pp)
5276 int flagtype;
5277 char_u **pp;
5278{
5279 int res;
5280
Bram Moolenaar95529562005-08-25 21:21:38 +00005281 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005282 {
5283 if (!VIM_ISDIGIT(**pp))
5284 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005285 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005286 return 0;
5287 }
5288 res = getdigits(pp);
5289 }
5290 else
5291 {
5292#ifdef FEAT_MBYTE
5293 res = mb_ptr2char_adv(pp);
5294#else
5295 res = *(*pp)++;
5296#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00005297 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00005298 && res >= 'A' && res <= 'Z'))
5299 {
5300 if (**pp == NUL)
5301 return 0;
5302#ifdef FEAT_MBYTE
5303 res = mb_ptr2char_adv(pp) + (res << 16);
5304#else
5305 res = *(*pp)++ + (res << 16);
5306#endif
5307 }
5308 }
5309 return res;
5310}
5311
5312/*
5313 * Process the "compflags" string used in an affix file and append it to
5314 * spin->si_compflags.
5315 * The processing involves changing the affix names to ID numbers, so that
5316 * they fit in one byte.
5317 */
5318 static void
5319process_compflags(spin, aff, compflags)
5320 spellinfo_T *spin;
5321 afffile_T *aff;
5322 char_u *compflags;
5323{
5324 char_u *p;
5325 char_u *prevp;
5326 unsigned flag;
5327 compitem_T *ci;
5328 int id;
5329 int len;
5330 char_u *tp;
5331 char_u key[AH_KEY_LEN];
5332 hashitem_T *hi;
5333
5334 /* Make room for the old and the new compflags, concatenated with a / in
5335 * between. Processing it makes it shorter, but we don't know by how
5336 * much, thus allocate the maximum. */
5337 len = STRLEN(compflags) + 1;
5338 if (spin->si_compflags != NULL)
5339 len += STRLEN(spin->si_compflags) + 1;
5340 p = getroom(spin, len, FALSE);
5341 if (p == NULL)
5342 return;
5343 if (spin->si_compflags != NULL)
5344 {
5345 STRCPY(p, spin->si_compflags);
5346 STRCAT(p, "/");
5347 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005348 spin->si_compflags = p;
5349 tp = p + STRLEN(p);
5350
5351 for (p = compflags; *p != NUL; )
5352 {
5353 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
5354 /* Copy non-flag characters directly. */
5355 *tp++ = *p++;
5356 else
5357 {
5358 /* First get the flag number, also checks validity. */
5359 prevp = p;
5360 flag = get_affitem(aff->af_flagtype, &p);
5361 if (flag != 0)
5362 {
5363 /* Find the flag in the hashtable. If it was used before, use
5364 * the existing ID. Otherwise add a new entry. */
5365 vim_strncpy(key, prevp, p - prevp);
5366 hi = hash_find(&aff->af_comp, key);
5367 if (!HASHITEM_EMPTY(hi))
5368 id = HI2CI(hi)->ci_newID;
5369 else
5370 {
5371 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
5372 if (ci == NULL)
5373 break;
5374 STRCPY(ci->ci_key, key);
5375 ci->ci_flag = flag;
5376 /* Avoid using a flag ID that has a special meaning in a
5377 * regexp (also inside []). */
5378 do
5379 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005380 check_renumber(spin);
5381 id = spin->si_newcompID--;
5382 } while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005383 ci->ci_newID = id;
5384 hash_add(&aff->af_comp, ci->ci_key);
5385 }
5386 *tp++ = id;
5387 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005388 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005389 ++p;
5390 }
5391 }
5392
5393 *tp = NUL;
5394}
5395
5396/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005397 * Check that the new IDs for postponed affixes and compounding don't overrun
5398 * each other. We have almost 255 available, but start at 0-127 to avoid
5399 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
5400 * When that is used up an error message is given.
5401 */
5402 static void
5403check_renumber(spin)
5404 spellinfo_T *spin;
5405{
5406 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
5407 {
5408 spin->si_newprefID = 127;
5409 spin->si_newcompID = 255;
5410 }
5411}
5412
5413/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005414 * Return TRUE if flag "flag" appears in affix list "afflist".
5415 */
5416 static int
5417flag_in_afflist(flagtype, afflist, flag)
5418 int flagtype;
5419 char_u *afflist;
5420 unsigned flag;
5421{
5422 char_u *p;
5423 unsigned n;
5424
5425 switch (flagtype)
5426 {
5427 case AFT_CHAR:
5428 return vim_strchr(afflist, flag) != NULL;
5429
Bram Moolenaar95529562005-08-25 21:21:38 +00005430 case AFT_CAPLONG:
5431 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00005432 for (p = afflist; *p != NUL; )
5433 {
5434#ifdef FEAT_MBYTE
5435 n = mb_ptr2char_adv(&p);
5436#else
5437 n = *p++;
5438#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00005439 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00005440 && *p != NUL)
5441#ifdef FEAT_MBYTE
5442 n = mb_ptr2char_adv(&p) + (n << 16);
5443#else
5444 n = *p++ + (n << 16);
5445#endif
5446 if (n == flag)
5447 return TRUE;
5448 }
5449 break;
5450
Bram Moolenaar95529562005-08-25 21:21:38 +00005451 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00005452 for (p = afflist; *p != NUL; )
5453 {
5454 n = getdigits(&p);
5455 if (n == flag)
5456 return TRUE;
5457 if (*p != NUL) /* skip over comma */
5458 ++p;
5459 }
5460 break;
5461 }
5462 return FALSE;
5463}
5464
5465/*
5466 * Give a warning when "spinval" and "affval" numbers are set and not the same.
5467 */
5468 static void
5469aff_check_number(spinval, affval, name)
5470 int spinval;
5471 int affval;
5472 char *name;
5473{
5474 if (spinval != 0 && spinval != affval)
5475 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
5476}
5477
5478/*
5479 * Give a warning when "spinval" and "affval" strings are set and not the same.
5480 */
5481 static void
5482aff_check_string(spinval, affval, name)
5483 char_u *spinval;
5484 char_u *affval;
5485 char *name;
5486{
5487 if (spinval != NULL && STRCMP(spinval, affval) != 0)
5488 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
5489}
5490
5491/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005492 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
5493 * NULL as equal.
5494 */
5495 static int
5496str_equal(s1, s2)
5497 char_u *s1;
5498 char_u *s2;
5499{
5500 if (s1 == NULL || s2 == NULL)
5501 return s1 == s2;
5502 return STRCMP(s1, s2) == 0;
5503}
5504
5505/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005506 * Add a from-to item to "gap". Used for REP and SAL items.
5507 * They are stored case-folded.
5508 */
5509 static void
5510add_fromto(spin, gap, from, to)
5511 spellinfo_T *spin;
5512 garray_T *gap;
5513 char_u *from;
5514 char_u *to;
5515{
5516 fromto_T *ftp;
5517 char_u word[MAXWLEN];
5518
5519 if (ga_grow(gap, 1) == OK)
5520 {
5521 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
5522 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005523 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005524 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005525 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005526 ++gap->ga_len;
5527 }
5528}
5529
5530/*
5531 * Convert a boolean argument in a SAL line to TRUE or FALSE;
5532 */
5533 static int
5534sal_to_bool(s)
5535 char_u *s;
5536{
5537 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
5538}
5539
5540/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00005541 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
5542 * When "s" is NULL FALSE is returned.
5543 */
5544 static int
5545has_non_ascii(s)
5546 char_u *s;
5547{
5548 char_u *p;
5549
5550 if (s != NULL)
5551 for (p = s; *p != NUL; ++p)
5552 if (*p >= 128)
5553 return TRUE;
5554 return FALSE;
5555}
5556
5557/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005558 * Free the structure filled by spell_read_aff().
5559 */
5560 static void
5561spell_free_aff(aff)
5562 afffile_T *aff;
5563{
5564 hashtab_T *ht;
5565 hashitem_T *hi;
5566 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005567 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005568 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005569
5570 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005571
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005572 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005573 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
5574 {
5575 todo = ht->ht_used;
5576 for (hi = ht->ht_array; todo > 0; ++hi)
5577 {
5578 if (!HASHITEM_EMPTY(hi))
5579 {
5580 --todo;
5581 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005582 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
5583 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005584 }
5585 }
5586 if (ht == &aff->af_suff)
5587 break;
5588 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005589
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005590 hash_clear(&aff->af_pref);
5591 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005592 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005593}
5594
5595/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005596 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005597 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005598 */
5599 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005600spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005601 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005602 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005603 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005604{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005605 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005606 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005607 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005608 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005609 char_u store_afflist[MAXWLEN];
5610 int pfxlen;
5611 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005612 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005613 char_u *pc;
5614 char_u *w;
5615 int l;
5616 hash_T hash;
5617 hashitem_T *hi;
5618 FILE *fd;
5619 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005620 int non_ascii = 0;
5621 int retval = OK;
5622 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005623 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005624 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005625
Bram Moolenaar51485f02005-06-04 21:55:20 +00005626 /*
5627 * Open the file.
5628 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005629 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005630 if (fd == NULL)
5631 {
5632 EMSG2(_(e_notopen), fname);
5633 return FAIL;
5634 }
5635
Bram Moolenaar51485f02005-06-04 21:55:20 +00005636 /* The hashtable is only used to detect duplicated words. */
5637 hash_init(&ht);
5638
Bram Moolenaarb765d632005-06-07 21:00:02 +00005639 if (spin->si_verbose || p_verbose > 2)
5640 {
5641 if (!spin->si_verbose)
5642 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005643 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005644 out_flush();
5645 if (!spin->si_verbose)
5646 verbose_leave();
5647 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005648
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005649 /* start with a message for the first line */
5650 spin->si_msg_count = 999999;
5651
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005652 /* Read and ignore the first line: word count. */
5653 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005654 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005655 EMSG2(_("E760: No word count in %s"), fname);
5656
5657 /*
5658 * Read all the lines in the file one by one.
5659 * The words are converted to 'encoding' here, before being added to
5660 * the hashtable.
5661 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005662 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005663 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005664 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005665 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005666 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005667 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005668
Bram Moolenaar51485f02005-06-04 21:55:20 +00005669 /* Remove CR, LF and white space from the end. White space halfway
5670 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005671 l = STRLEN(line);
5672 while (l > 0 && line[l - 1] <= ' ')
5673 --l;
5674 if (l == 0)
5675 continue; /* empty line */
5676 line[l] = NUL;
5677
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005678 /* Find the optional affix names. Replace the SLASH character by a
5679 * slash. */
5680 afflist = NULL;
5681 for (p = line; *p != NUL; mb_ptr_adv(p))
5682 {
5683 if (*p == affile->af_slash)
5684 *p = '/';
5685 else if (*p == '/')
5686 {
5687 *p = NUL;
5688 afflist = p + 1;
5689 break;
5690 }
5691 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005692
5693 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
5694 if (spin->si_ascii && has_non_ascii(line))
5695 {
5696 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005697 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005698 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005699
Bram Moolenaarb765d632005-06-07 21:00:02 +00005700#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005701 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005702 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005703 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005704 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005705 if (pc == NULL)
5706 {
5707 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5708 fname, lnum, line);
5709 continue;
5710 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005711 w = pc;
5712 }
5713 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005714#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005715 {
5716 pc = NULL;
5717 w = line;
5718 }
5719
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005720 /* This takes time, print a message every 10000 words. */
5721 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005722 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005723 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005724 vim_snprintf((char *)message, sizeof(message),
5725 _("line %6d, word %6d - %s"),
5726 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
5727 msg_start();
5728 msg_puts_long_attr(message, 0);
5729 msg_clr_eos();
5730 msg_didout = FALSE;
5731 msg_col = 0;
5732 out_flush();
5733 }
5734
Bram Moolenaar51485f02005-06-04 21:55:20 +00005735 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005736 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005737 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005738 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005739 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005740 if (retval == FAIL)
5741 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005742
Bram Moolenaar51485f02005-06-04 21:55:20 +00005743 hash = hash_hash(dw);
5744 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005745 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005746 {
5747 if (p_verbose > 0)
5748 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005749 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005750 else if (duplicate == 0)
5751 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
5752 fname, lnum, dw);
5753 ++duplicate;
5754 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005755 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00005756 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005757
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005758 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005759 store_afflist[0] = NUL;
5760 pfxlen = 0;
5761 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005762 if (afflist != NULL)
5763 {
5764 /* Check for affix name that stands for keep-case word and stands
5765 * for rare word (if defined). */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005766 if (affile->af_kep != 0 && flag_in_afflist(
5767 affile->af_flagtype, afflist, affile->af_kep))
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005768 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005769 if (affile->af_rar != 0 && flag_in_afflist(
5770 affile->af_flagtype, afflist, affile->af_rar))
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005771 flags |= WF_RARE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005772 if (affile->af_bad != 0 && flag_in_afflist(
5773 affile->af_flagtype, afflist, affile->af_bad))
Bram Moolenaar0c405862005-06-22 22:26:26 +00005774 flags |= WF_BANNED;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005775 if (affile->af_needaffix != 0 && flag_in_afflist(
5776 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00005777 need_affix = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005778 if (affile->af_needcomp != 0 && flag_in_afflist(
5779 affile->af_flagtype, afflist, affile->af_needcomp))
5780 flags |= WF_NEEDCOMP;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005781
5782 if (affile->af_pfxpostpone)
5783 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005784 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005785
Bram Moolenaar5195e452005-08-19 20:32:47 +00005786 if (spin->si_compflags != NULL)
5787 /* Need to store the list of compound flags with the word.
5788 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005789 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005790 }
5791
Bram Moolenaar51485f02005-06-04 21:55:20 +00005792 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005793 if (store_word(spin, dw, flags, spin->si_region,
5794 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005795 retval = FAIL;
5796
5797 if (afflist != NULL)
5798 {
5799 /* Find all matching suffixes and add the resulting words.
5800 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005801 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005802 &affile->af_suff, &affile->af_pref,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005803 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005804 retval = FAIL;
5805
5806 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005807 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005808 &affile->af_pref, NULL,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005809 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005810 retval = FAIL;
5811 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005812 }
5813
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005814 if (duplicate > 0)
5815 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005816 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005817 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
5818 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005819 hash_clear(&ht);
5820
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005821 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005822 return retval;
5823}
5824
5825/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005826 * Get the list of prefix IDs from the affix list "afflist".
5827 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00005828 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
5829 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005830 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005831 static int
5832get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005833 afffile_T *affile;
5834 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005835 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005836{
5837 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005838 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005839 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005840 int id;
5841 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005842 hashitem_T *hi;
5843
Bram Moolenaar6de68532005-08-24 22:08:48 +00005844 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005845 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005846 prevp = p;
5847 if (get_affitem(affile->af_flagtype, &p) != 0)
5848 {
5849 /* A flag is a postponed prefix flag if it appears in "af_pref"
5850 * and it's ID is not zero. */
5851 vim_strncpy(key, prevp, p - prevp);
5852 hi = hash_find(&affile->af_pref, key);
5853 if (!HASHITEM_EMPTY(hi))
5854 {
5855 id = HI2AH(hi)->ah_newID;
5856 if (id != 0)
5857 store_afflist[cnt++] = id;
5858 }
5859 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005860 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005861 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005862 }
5863
Bram Moolenaar5195e452005-08-19 20:32:47 +00005864 store_afflist[cnt] = NUL;
5865 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005866}
5867
5868/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005869 * Get the list of compound IDs from the affix list "afflist" that are used
5870 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00005871 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005872 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005873 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00005874get_compflags(affile, afflist, store_afflist)
5875 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005876 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005877 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005878{
5879 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005880 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005881 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005882 char_u key[AH_KEY_LEN];
5883 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005884
Bram Moolenaar6de68532005-08-24 22:08:48 +00005885 for (p = afflist; *p != NUL; )
5886 {
5887 prevp = p;
5888 if (get_affitem(affile->af_flagtype, &p) != 0)
5889 {
5890 /* A flag is a compound flag if it appears in "af_comp". */
5891 vim_strncpy(key, prevp, p - prevp);
5892 hi = hash_find(&affile->af_comp, key);
5893 if (!HASHITEM_EMPTY(hi))
5894 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
5895 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005896 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005897 ++p;
5898 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005899
Bram Moolenaar5195e452005-08-19 20:32:47 +00005900 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005901}
5902
5903/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005904 * Apply affixes to a word and store the resulting words.
5905 * "ht" is the hashtable with affentry_T that need to be applied, either
5906 * prefixes or suffixes.
5907 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
5908 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005909 *
5910 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005911 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005912 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00005913store_aff_word(spin, word, afflist, affile, ht, xht, comb, flags,
5914 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005915 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005916 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005917 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005918 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005919 hashtab_T *ht;
5920 hashtab_T *xht;
5921 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005922 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005923 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005924 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
5925 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005926{
5927 int todo;
5928 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005929 affheader_T *ah;
5930 affentry_T *ae;
5931 regmatch_T regmatch;
5932 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005933 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005934 int i;
5935 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005936 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005937 char_u *use_pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005938 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00005939 size_t wordlen = STRLEN(word);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005940
Bram Moolenaar51485f02005-06-04 21:55:20 +00005941 todo = ht->ht_used;
5942 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005943 {
5944 if (!HASHITEM_EMPTY(hi))
5945 {
5946 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005947 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00005948
Bram Moolenaar51485f02005-06-04 21:55:20 +00005949 /* Check that the affix combines, if required, and that the word
5950 * supports this affix. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005951 if ((!comb || ah->ah_combine) && flag_in_afflist(
5952 affile->af_flagtype, afflist, ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00005953 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005954 /* Loop over all affix entries with this name. */
5955 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005956 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005957 /* Check the condition. It's not logical to match case
5958 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005959 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005960 * Another requirement from Myspell is that the chop
5961 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005962 * For prefixes, when "PFXPOSTPONE" was used, only do
5963 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005964 regmatch.regprog = ae->ae_prog;
5965 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005966 if ((xht != NULL || !affile->af_pfxpostpone
5967 || ae->ae_chop != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005968 && (ae->ae_chop == NULL
5969 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005970 && (ae->ae_prog == NULL
5971 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005972 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005973 /* Match. Remove the chop and add the affix. */
5974 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005975 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005976 /* prefix: chop/add at the start of the word */
5977 if (ae->ae_add == NULL)
5978 *newword = NUL;
5979 else
5980 STRCPY(newword, ae->ae_add);
5981 p = word;
5982 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005983 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005984 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005985#ifdef FEAT_MBYTE
5986 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005987 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005988 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005989 for ( ; i > 0; --i)
5990 mb_ptr_adv(p);
5991 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005992 else
5993#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005994 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005995 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005996 STRCAT(newword, p);
5997 }
5998 else
5999 {
6000 /* suffix: chop/add at the end of the word */
6001 STRCPY(newword, word);
6002 if (ae->ae_chop != NULL)
6003 {
6004 /* Remove chop string. */
6005 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006006 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006007 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006008 mb_ptr_back(newword, p);
6009 *p = NUL;
6010 }
6011 if (ae->ae_add != NULL)
6012 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006013 }
6014
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006015 /* Obey the "rare" flag of the affix. */
6016 if (ae->ae_rare)
6017 use_flags = flags | WF_RARE;
6018 else
6019 use_flags = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006020
6021 /* Obey the "nocomp" flag of the affix: don't use the
6022 * compound flags. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006023 use_pfxlist = pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006024 if (ae->ae_nocomp && pfxlist != NULL)
6025 {
6026 vim_strncpy(pfx_pfxlist, pfxlist, pfxlen);
6027 use_pfxlist = pfx_pfxlist;
6028 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006029
6030 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00006031 if (spin->si_prefroot != NULL
6032 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006033 {
6034 /* ... add a flag to indicate an affix was used. */
6035 use_flags |= WF_HAS_AFF;
6036
6037 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00006038 * affixes is not allowed. But do use the
6039 * compound flags after them. */
6040 if ((!ah->ah_combine || comb) && pfxlist != NULL)
6041 use_pfxlist += pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006042 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006043
Bram Moolenaar51485f02005-06-04 21:55:20 +00006044 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006045 if (store_word(spin, newword, use_flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006046 spin->si_region, use_pfxlist, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006047 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006048
Bram Moolenaar51485f02005-06-04 21:55:20 +00006049 /* When added a suffix and combining is allowed also
6050 * try adding prefixes additionally. */
6051 if (xht != NULL && ah->ah_combine)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006052 if (store_aff_word(spin, newword, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006053 xht, NULL, TRUE,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006054 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006055 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006056 }
6057 }
6058 }
6059 }
6060 }
6061
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006062 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006063}
6064
6065/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006066 * Read a file with a list of words.
6067 */
6068 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006069spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006070 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006071 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006072{
6073 FILE *fd;
6074 long lnum = 0;
6075 char_u rline[MAXLINELEN];
6076 char_u *line;
6077 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006078 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006079 int l;
6080 int retval = OK;
6081 int did_word = FALSE;
6082 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006083 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006084 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006085
6086 /*
6087 * Open the file.
6088 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006089 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006090 if (fd == NULL)
6091 {
6092 EMSG2(_(e_notopen), fname);
6093 return FAIL;
6094 }
6095
Bram Moolenaarb765d632005-06-07 21:00:02 +00006096 if (spin->si_verbose || p_verbose > 2)
6097 {
6098 if (!spin->si_verbose)
6099 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006100 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006101 out_flush();
6102 if (!spin->si_verbose)
6103 verbose_leave();
6104 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006105
6106 /*
6107 * Read all the lines in the file one by one.
6108 */
6109 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
6110 {
6111 line_breakcheck();
6112 ++lnum;
6113
6114 /* Skip comment lines. */
6115 if (*rline == '#')
6116 continue;
6117
6118 /* Remove CR, LF and white space from the end. */
6119 l = STRLEN(rline);
6120 while (l > 0 && rline[l - 1] <= ' ')
6121 --l;
6122 if (l == 0)
6123 continue; /* empty or blank line */
6124 rline[l] = NUL;
6125
6126 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
6127 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006128#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00006129 if (spin->si_conv.vc_type != CONV_NONE)
6130 {
6131 pc = string_convert(&spin->si_conv, rline, NULL);
6132 if (pc == NULL)
6133 {
6134 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6135 fname, lnum, rline);
6136 continue;
6137 }
6138 line = pc;
6139 }
6140 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006141#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006142 {
6143 pc = NULL;
6144 line = rline;
6145 }
6146
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006147 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00006148 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006149 ++line;
6150 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006151 {
6152 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006153 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
6154 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006155 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006156 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
6157 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006158 else
6159 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006160#ifdef FEAT_MBYTE
6161 char_u *enc;
6162
Bram Moolenaar51485f02005-06-04 21:55:20 +00006163 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006164 line += 10;
6165 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006166 if (enc != NULL && !spin->si_ascii
6167 && convert_setup(&spin->si_conv, enc,
6168 p_enc) == FAIL)
6169 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00006170 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006171 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006172 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006173#else
6174 smsg((char_u *)_("Conversion in %s not supported"), fname);
6175#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006176 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006177 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006178 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006179
Bram Moolenaar3982c542005-06-08 21:56:31 +00006180 if (STRNCMP(line, "regions=", 8) == 0)
6181 {
6182 if (spin->si_region_count > 1)
6183 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
6184 fname, lnum, line);
6185 else
6186 {
6187 line += 8;
6188 if (STRLEN(line) > 16)
6189 smsg((char_u *)_("Too many regions in %s line %d: %s"),
6190 fname, lnum, line);
6191 else
6192 {
6193 spin->si_region_count = STRLEN(line) / 2;
6194 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006195
6196 /* Adjust the mask for a word valid in all regions. */
6197 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006198 }
6199 }
6200 continue;
6201 }
6202
Bram Moolenaar7887d882005-07-01 22:33:52 +00006203 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
6204 fname, lnum, line - 1);
6205 continue;
6206 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006207
Bram Moolenaar7887d882005-07-01 22:33:52 +00006208 flags = 0;
6209 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006210
Bram Moolenaar7887d882005-07-01 22:33:52 +00006211 /* Check for flags and region after a slash. */
6212 p = vim_strchr(line, '/');
6213 if (p != NULL)
6214 {
6215 *p++ = NUL;
6216 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006217 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006218 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006219 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006220 else if (*p == '!') /* Bad, bad, wicked word. */
6221 flags |= WF_BANNED;
6222 else if (*p == '?') /* Rare word. */
6223 flags |= WF_RARE;
6224 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006225 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006226 if ((flags & WF_REGION) == 0) /* first one */
6227 regionmask = 0;
6228 flags |= WF_REGION;
6229
6230 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00006231 if (l > spin->si_region_count)
6232 {
6233 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00006234 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006235 break;
6236 }
6237 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006238 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00006239 else
6240 {
6241 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
6242 fname, lnum, p);
6243 break;
6244 }
6245 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006246 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006247 }
6248
6249 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6250 if (spin->si_ascii && has_non_ascii(line))
6251 {
6252 ++non_ascii;
6253 continue;
6254 }
6255
6256 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006257 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006258 {
6259 retval = FAIL;
6260 break;
6261 }
6262 did_word = TRUE;
6263 }
6264
6265 vim_free(pc);
6266 fclose(fd);
6267
Bram Moolenaarb765d632005-06-07 21:00:02 +00006268 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
6269 {
6270 if (p_verbose > 2)
6271 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00006272 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
6273 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006274 if (p_verbose > 2)
6275 verbose_leave();
6276 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006277 return retval;
6278}
6279
6280/*
6281 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006282 * This avoids calling free() for every little struct we use (and keeping
6283 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00006284 * The memory is cleared to all zeros.
6285 * Returns NULL when out of memory.
6286 */
6287 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006288getroom(spin, len, align)
6289 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006290 size_t len; /* length needed */
6291 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006292{
6293 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006294 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006295
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006296 if (align && bl != NULL)
6297 /* Round size up for alignment. On some systems structures need to be
6298 * aligned to the size of a pointer (e.g., SPARC). */
6299 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
6300 & ~(sizeof(char *) - 1);
6301
Bram Moolenaar51485f02005-06-04 21:55:20 +00006302 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
6303 {
6304 /* Allocate a block of memory. This is not freed until much later. */
6305 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
6306 if (bl == NULL)
6307 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006308 bl->sb_next = spin->si_blocks;
6309 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006310 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006311 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006312 }
6313
6314 p = bl->sb_data + bl->sb_used;
6315 bl->sb_used += len;
6316
6317 return p;
6318}
6319
6320/*
6321 * Make a copy of a string into memory allocated with getroom().
6322 */
6323 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006324getroom_save(spin, s)
6325 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006326 char_u *s;
6327{
6328 char_u *sc;
6329
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006330 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006331 if (sc != NULL)
6332 STRCPY(sc, s);
6333 return sc;
6334}
6335
6336
6337/*
6338 * Free the list of allocated sblock_T.
6339 */
6340 static void
6341free_blocks(bl)
6342 sblock_T *bl;
6343{
6344 sblock_T *next;
6345
6346 while (bl != NULL)
6347 {
6348 next = bl->sb_next;
6349 vim_free(bl);
6350 bl = next;
6351 }
6352}
6353
6354/*
6355 * Allocate the root of a word tree.
6356 */
6357 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006358wordtree_alloc(spin)
6359 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006360{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006361 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006362}
6363
6364/*
6365 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006366 * Always store it in the case-folded tree. For a keep-case word this is
6367 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
6368 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006369 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006370 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
6371 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006372 */
6373 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00006374store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006375 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006376 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006377 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006378 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006379 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006380 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006381{
6382 int len = STRLEN(word);
6383 int ct = captype(word, word + len);
6384 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006385 int res = OK;
6386 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006387
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006388 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006389 for (p = pfxlist; res == OK; ++p)
6390 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006391 if (!need_affix || (p != NULL && *p != NUL))
6392 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006393 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006394 if (p == NULL || *p == NUL)
6395 break;
6396 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00006397 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006398
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006399 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00006400 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006401 for (p = pfxlist; res == OK; ++p)
6402 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006403 if (!need_affix || (p != NULL && *p != NUL))
6404 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006405 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006406 if (p == NULL || *p == NUL)
6407 break;
6408 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00006409 ++spin->si_keepwcount;
6410 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006411 return res;
6412}
6413
6414/*
6415 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006416 * When "flags" < 0 we are adding to the prefix tree where flags is used for
6417 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006418 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006419 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006420 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006421tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006422 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006423 char_u *word;
6424 wordnode_T *root;
6425 int flags;
6426 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006427 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006428{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006429 wordnode_T *node = root;
6430 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006431 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006432 wordnode_T **prev = NULL;
6433 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006434
Bram Moolenaar51485f02005-06-04 21:55:20 +00006435 /* Add each byte of the word to the tree, including the NUL at the end. */
6436 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006437 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006438 /* When there is more than one reference to this node we need to make
6439 * a copy, so that we can modify it. Copy the whole list of siblings
6440 * (we don't optimize for a partly shared list of siblings). */
6441 if (node != NULL && node->wn_refs > 1)
6442 {
6443 --node->wn_refs;
6444 copyprev = prev;
6445 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
6446 {
6447 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006448 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006449 if (np == NULL)
6450 return FAIL;
6451 np->wn_child = copyp->wn_child;
6452 if (np->wn_child != NULL)
6453 ++np->wn_child->wn_refs; /* child gets extra ref */
6454 np->wn_byte = copyp->wn_byte;
6455 if (np->wn_byte == NUL)
6456 {
6457 np->wn_flags = copyp->wn_flags;
6458 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006459 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006460 }
6461
6462 /* Link the new node in the list, there will be one ref. */
6463 np->wn_refs = 1;
6464 *copyprev = np;
6465 copyprev = &np->wn_sibling;
6466
6467 /* Let "node" point to the head of the copied list. */
6468 if (copyp == node)
6469 node = np;
6470 }
6471 }
6472
Bram Moolenaar51485f02005-06-04 21:55:20 +00006473 /* Look for the sibling that has the same character. They are sorted
6474 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006475 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006476 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006477 while (node != NULL
6478 && (node->wn_byte < word[i]
6479 || (node->wn_byte == NUL
6480 && (flags < 0
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006481 ? node->wn_affixID < affixID
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006482 : node->wn_flags < (flags & WN_MASK)
6483 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006484 && node->wn_affixID < affixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006485 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006486 prev = &node->wn_sibling;
6487 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006488 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006489 if (node == NULL
6490 || node->wn_byte != word[i]
6491 || (word[i] == NUL
6492 && (flags < 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006493 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006494 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006495 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006496 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006497 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006498 if (np == NULL)
6499 return FAIL;
6500 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006501
6502 /* If "node" is NULL this is a new child or the end of the sibling
6503 * list: ref count is one. Otherwise use ref count of sibling and
6504 * make ref count of sibling one (matters when inserting in front
6505 * of the list of siblings). */
6506 if (node == NULL)
6507 np->wn_refs = 1;
6508 else
6509 {
6510 np->wn_refs = node->wn_refs;
6511 node->wn_refs = 1;
6512 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006513 *prev = np;
6514 np->wn_sibling = node;
6515 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006516 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006517
Bram Moolenaar51485f02005-06-04 21:55:20 +00006518 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006519 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006520 node->wn_flags = flags;
6521 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006522 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006523 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00006524 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006525 prev = &node->wn_child;
6526 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006527 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006528#ifdef SPELL_PRINTTREE
6529 smsg("Added \"%s\"", word);
6530 spell_print_tree(root->wn_sibling);
6531#endif
6532
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006533 /* count nr of words added since last message */
6534 ++spin->si_msg_count;
6535
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006536 if (spin->si_compress_cnt > 1)
6537 {
6538 if (--spin->si_compress_cnt == 1)
6539 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006540 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006541 }
6542
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006543 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006544 * When we have allocated lots of memory we need to compress the word tree
6545 * to free up some room. But compression is slow, and we might actually
6546 * need that room, thus only compress in the following situations:
6547 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00006548 * "compress_start" blocks.
6549 * 2. When compressed before and used "compress_inc" blocks before
6550 * adding "compress_added" words (si_compress_cnt > 1).
6551 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006552 * (si_compress_cnt == 1) and the number of free nodes drops below the
6553 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006554 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006555#ifndef SPELL_PRINTTREE
6556 if (spin->si_compress_cnt == 1
6557 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00006558 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006559#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006560 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006561 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00006562 * when the freed up room has been used and another "compress_inc"
6563 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006564 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006565 spin->si_blocks_cnt -= compress_inc;
6566 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006567
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006568 if (spin->si_verbose)
6569 {
6570 msg_start();
6571 msg_puts((char_u *)_(msg_compressing));
6572 msg_clr_eos();
6573 msg_didout = FALSE;
6574 msg_col = 0;
6575 out_flush();
6576 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006577
6578 /* Compress both trees. Either they both have many nodes, which makes
6579 * compression useful, or one of them is small, which means
6580 * compression goes fast. */
6581 wordtree_compress(spin, spin->si_foldroot);
6582 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006583 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006584
6585 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006586}
6587
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006588/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00006589 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
6590 * Sets "sps_flags".
6591 */
6592 int
6593spell_check_msm()
6594{
6595 char_u *p = p_msm;
6596 long start = 0;
6597 long inc = 0;
6598 long added = 0;
6599
6600 if (!VIM_ISDIGIT(*p))
6601 return FAIL;
6602 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
6603 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
6604 if (*p != ',')
6605 return FAIL;
6606 ++p;
6607 if (!VIM_ISDIGIT(*p))
6608 return FAIL;
6609 inc = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
6610 if (*p != ',')
6611 return FAIL;
6612 ++p;
6613 if (!VIM_ISDIGIT(*p))
6614 return FAIL;
6615 added = getdigits(&p) * 1024;
6616 if (*p != NUL)
6617 return FAIL;
6618
6619 if (start == 0 || inc == 0 || added == 0 || inc > start)
6620 return FAIL;
6621
6622 compress_start = start;
6623 compress_inc = inc;
6624 compress_added = added;
6625 return OK;
6626}
6627
6628
6629/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006630 * Get a wordnode_T, either from the list of previously freed nodes or
6631 * allocate a new one.
6632 */
6633 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006634get_wordnode(spin)
6635 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006636{
6637 wordnode_T *n;
6638
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006639 if (spin->si_first_free == NULL)
6640 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006641 else
6642 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006643 n = spin->si_first_free;
6644 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006645 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006646 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006647 }
6648#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006649 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006650#endif
6651 return n;
6652}
6653
6654/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006655 * Decrement the reference count on a node (which is the head of a list of
6656 * siblings). If the reference count becomes zero free the node and its
6657 * siblings.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006658 */
6659 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006660deref_wordnode(spin, node)
6661 spellinfo_T *spin;
6662 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006663{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006664 wordnode_T *np;
6665
6666 if (--node->wn_refs == 0)
6667 for (np = node; np != NULL; np = np->wn_sibling)
6668 {
6669 if (np->wn_child != NULL)
6670 deref_wordnode(spin, np->wn_child);
6671 free_wordnode(spin, np);
6672 }
6673}
6674
6675/*
6676 * Free a wordnode_T for re-use later.
6677 * Only the "wn_child" field becomes invalid.
6678 */
6679 static void
6680free_wordnode(spin, n)
6681 spellinfo_T *spin;
6682 wordnode_T *n;
6683{
6684 n->wn_child = spin->si_first_free;
6685 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006686 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006687}
6688
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006689/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006690 * Compress a tree: find tails that are identical and can be shared.
6691 */
6692 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006693wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006694 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006695 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006696{
6697 hashtab_T ht;
6698 int n;
6699 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006700 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006701
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006702 /* Skip the root itself, it's not actually used. The first sibling is the
6703 * start of the tree. */
6704 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006705 {
6706 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006707 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006708
6709#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00006710 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006711#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00006712 {
6713 if (!spin->si_verbose)
6714 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006715 if (tot > 1000000)
6716 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006717 else if (tot == 0)
6718 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006719 else
6720 perc = (tot - n) * 100 / tot;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006721 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006722 n, tot, perc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006723 if (p_verbose > 2)
6724 verbose_leave();
6725 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006726#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006727 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006728#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006729 hash_clear(&ht);
6730 }
6731}
6732
6733/*
6734 * Compress a node, its siblings and its children, depth first.
6735 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006736 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006737 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006738node_compress(spin, node, ht, tot)
6739 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006740 wordnode_T *node;
6741 hashtab_T *ht;
6742 int *tot; /* total count of nodes before compressing,
6743 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006744{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006745 wordnode_T *np;
6746 wordnode_T *tp;
6747 wordnode_T *child;
6748 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006749 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006750 int len = 0;
6751 unsigned nr, n;
6752 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006753
Bram Moolenaar51485f02005-06-04 21:55:20 +00006754 /*
6755 * Go through the list of siblings. Compress each child and then try
6756 * finding an identical child to replace it.
6757 * Note that with "child" we mean not just the node that is pointed to,
6758 * but the whole list of siblings, of which the node is the first.
6759 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006760 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006761 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006762 ++len;
6763 if ((child = np->wn_child) != NULL)
6764 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006765 /* Compress the child. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006766 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006767
6768 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006769 hash = hash_hash(child->wn_u1.hashkey);
6770 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006771 tp = NULL;
6772 if (!HASHITEM_EMPTY(hi))
6773 {
6774 /* There are children with an identical hash value. Now check
6775 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006776 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006777 if (node_equal(child, tp))
6778 {
6779 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006780 * current one. This means the current child and all
6781 * its siblings is unlinked from the tree. */
6782 ++tp->wn_refs;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006783 deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006784 np->wn_child = tp;
6785 ++compressed;
6786 break;
6787 }
6788 if (tp == NULL)
6789 {
6790 /* No other child with this hash value equals the child of
6791 * the node, add it to the linked list after the first
6792 * item. */
6793 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006794 child->wn_u2.next = tp->wn_u2.next;
6795 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006796 }
6797 }
6798 else
6799 /* No other child has this hash value, add it to the
6800 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006801 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006802 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006803 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006804 *tot += len;
6805
6806 /*
6807 * Make a hash key for the node and its siblings, so that we can quickly
6808 * find a lookalike node. This must be done after compressing the sibling
6809 * list, otherwise the hash key would become invalid by the compression.
6810 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006811 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006812 nr = 0;
6813 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006814 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006815 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006816 /* end node: use wn_flags, wn_region and wn_affixID */
6817 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006818 else
6819 /* byte node: use the byte value and the child pointer */
6820 n = np->wn_byte + ((long_u)np->wn_child << 8);
6821 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006822 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006823
6824 /* Avoid NUL bytes, it terminates the hash key. */
6825 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006826 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006827 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006828 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006829 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006830 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006831 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006832 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
6833 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006834
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006835 /* Check for CTRL-C pressed now and then. */
6836 fast_breakcheck();
6837
Bram Moolenaar51485f02005-06-04 21:55:20 +00006838 return compressed;
6839}
6840
6841/*
6842 * Return TRUE when two nodes have identical siblings and children.
6843 */
6844 static int
6845node_equal(n1, n2)
6846 wordnode_T *n1;
6847 wordnode_T *n2;
6848{
6849 wordnode_T *p1;
6850 wordnode_T *p2;
6851
6852 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
6853 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
6854 if (p1->wn_byte != p2->wn_byte
6855 || (p1->wn_byte == NUL
6856 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006857 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006858 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006859 : (p1->wn_child != p2->wn_child)))
6860 break;
6861
6862 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006863}
6864
6865/*
6866 * Write a number to file "fd", MSB first, in "len" bytes.
6867 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006868 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006869put_bytes(fd, nr, len)
6870 FILE *fd;
6871 long_u nr;
6872 int len;
6873{
6874 int i;
6875
6876 for (i = len - 1; i >= 0; --i)
6877 putc((int)(nr >> (i * 8)), fd);
6878}
6879
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006880static int
6881#ifdef __BORLANDC__
6882_RTLENTRYF
6883#endif
6884rep_compare __ARGS((const void *s1, const void *s2));
6885
6886/*
6887 * Function given to qsort() to sort the REP items on "from" string.
6888 */
6889 static int
6890#ifdef __BORLANDC__
6891_RTLENTRYF
6892#endif
6893rep_compare(s1, s2)
6894 const void *s1;
6895 const void *s2;
6896{
6897 fromto_T *p1 = (fromto_T *)s1;
6898 fromto_T *p2 = (fromto_T *)s2;
6899
6900 return STRCMP(p1->ft_from, p2->ft_from);
6901}
6902
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006903/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00006904 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006905 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006906 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006907 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006908write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006909 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006910 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006911{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006912 FILE *fd;
6913 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006914 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006915 wordnode_T *tree;
6916 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006917 int i;
6918 int l;
6919 garray_T *gap;
6920 fromto_T *ftp;
6921 char_u *p;
6922 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006923 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006924
Bram Moolenaarb765d632005-06-07 21:00:02 +00006925 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006926 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006927 {
6928 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006929 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006930 }
6931
Bram Moolenaar5195e452005-08-19 20:32:47 +00006932 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006933 /* <fileID> */
6934 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006935 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006936 EMSG(_(e_write));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006937 retval = FAIL;
6938 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00006939 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006940
Bram Moolenaar5195e452005-08-19 20:32:47 +00006941 /*
6942 * <SECTIONS>: <section> ... <sectionend>
6943 */
6944
6945 /* SN_REGION: <regionname> ...
6946 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006947 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006948 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006949 putc(SN_REGION, fd); /* <sectionID> */
6950 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6951 l = spin->si_region_count * 2;
6952 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6953 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
6954 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006955 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006956 }
6957 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006958 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006959
Bram Moolenaar5195e452005-08-19 20:32:47 +00006960 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
6961 *
6962 * The table with character flags and the table for case folding.
6963 * This makes sure the same characters are recognized as word characters
6964 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006965 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006966 * 'encoding'.
6967 * Also skip this for an .add.spl file, the main spell file must contain
6968 * the table (avoids that it conflicts). File is shorter too.
6969 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006970 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006971 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006972 char_u folchars[128 * 8];
6973 int flags;
6974
Bram Moolenaard12a1322005-08-21 22:08:24 +00006975 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006976 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6977
6978 /* Form the <folchars> string first, we need to know its length. */
6979 l = 0;
6980 for (i = 128; i < 256; ++i)
6981 {
6982#ifdef FEAT_MBYTE
6983 if (has_mbyte)
6984 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
6985 else
6986#endif
6987 folchars[l++] = spelltab.st_fold[i];
6988 }
6989 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
6990
6991 fputc(128, fd); /* <charflagslen> */
6992 for (i = 128; i < 256; ++i)
6993 {
6994 flags = 0;
6995 if (spelltab.st_isw[i])
6996 flags |= CF_WORD;
6997 if (spelltab.st_isu[i])
6998 flags |= CF_UPPER;
6999 fputc(flags, fd); /* <charflags> */
7000 }
7001
7002 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
7003 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007004 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007005
Bram Moolenaar5195e452005-08-19 20:32:47 +00007006 /* SN_MIDWORD: <midword> */
7007 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007008 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007009 putc(SN_MIDWORD, fd); /* <sectionID> */
7010 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7011
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007012 i = STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007013 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007014 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
7015 }
7016
Bram Moolenaar5195e452005-08-19 20:32:47 +00007017 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
7018 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007019 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007020 putc(SN_PREFCOND, fd); /* <sectionID> */
7021 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7022
7023 l = write_spell_prefcond(NULL, &spin->si_prefcond);
7024 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7025
7026 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007027 }
7028
Bram Moolenaar5195e452005-08-19 20:32:47 +00007029 /* SN_REP: <repcount> <rep> ...
7030 * SN_SAL: <salflags> <salcount> <sal> ... */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007031
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007032 /* Sort the REP items. */
7033 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
7034 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007035
Bram Moolenaar5195e452005-08-19 20:32:47 +00007036 /* round 1: SN_REP section
7037 * round 2: SN_SAL section (unless SN_SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007038 for (round = 1; round <= 2; ++round)
7039 {
7040 if (round == 1)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007041 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007042 gap = &spin->si_rep;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007043 putc(SN_REP, fd); /* <sectionID> */
7044 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007045 else
7046 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007047 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
7048 /* using SN_SOFO section instead of SN_SAL */
7049 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007050 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007051 putc(SN_SAL, fd); /* <sectionID> */
7052 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007053
Bram Moolenaar5195e452005-08-19 20:32:47 +00007054 /* This is for making suggestions, section is not required. */
7055 putc(0, fd); /* <sectionflags> */
7056
7057 /* Compute the length of what follows. */
7058 l = 2; /* count <repcount> or <salcount> */
7059 for (i = 0; i < gap->ga_len; ++i)
7060 {
7061 ftp = &((fromto_T *)gap->ga_data)[i];
7062 l += 1 + STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
7063 l += 1 + STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
7064 }
7065 if (round == 2)
7066 ++l; /* count <salflags> */
7067 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7068
7069 if (round == 2)
7070 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007071 i = 0;
7072 if (spin->si_followup)
7073 i |= SAL_F0LLOWUP;
7074 if (spin->si_collapse)
7075 i |= SAL_COLLAPSE;
7076 if (spin->si_rem_accents)
7077 i |= SAL_REM_ACCENTS;
7078 putc(i, fd); /* <salflags> */
7079 }
7080
7081 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
7082 for (i = 0; i < gap->ga_len; ++i)
7083 {
7084 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
7085 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
7086 ftp = &((fromto_T *)gap->ga_data)[i];
7087 for (rr = 1; rr <= 2; ++rr)
7088 {
7089 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
7090 l = STRLEN(p);
7091 putc(l, fd);
7092 fwrite(p, l, (size_t)1, fd);
7093 }
7094 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007095
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007096 }
7097
Bram Moolenaar5195e452005-08-19 20:32:47 +00007098 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
7099 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007100 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
7101 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007102 putc(SN_SOFO, fd); /* <sectionID> */
7103 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007104
7105 l = STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007106 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
7107 /* <sectionlen> */
7108
7109 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
7110 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007111
7112 l = STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007113 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
7114 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007115 }
7116
Bram Moolenaar5195e452005-08-19 20:32:47 +00007117 /* SN_MAP: <mapstr>
7118 * This is for making suggestions, section is not required. */
7119 if (spin->si_map.ga_len > 0)
7120 {
7121 putc(SN_MAP, fd); /* <sectionID> */
7122 putc(0, fd); /* <sectionflags> */
7123 l = spin->si_map.ga_len;
7124 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7125 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
7126 /* <mapstr> */
7127 }
7128
7129 /* SN_COMPOUND: compound info.
7130 * We don't mark it required, when not supported all compound words will
7131 * be bad words. */
7132 if (spin->si_compflags != NULL)
7133 {
7134 putc(SN_COMPOUND, fd); /* <sectionID> */
7135 putc(0, fd); /* <sectionflags> */
7136
7137 l = STRLEN(spin->si_compflags);
7138 put_bytes(fd, (long_u)(l + 3), 4); /* <sectionlen> */
7139 putc(spin->si_compmax, fd); /* <compmax> */
7140 putc(spin->si_compminlen, fd); /* <compminlen> */
7141 putc(spin->si_compsylmax, fd); /* <compsylmax> */
7142 /* <compflags> */
7143 fwrite(spin->si_compflags, (size_t)l, (size_t)1, fd);
7144 }
7145
Bram Moolenaar78622822005-08-23 21:00:13 +00007146 /* SN_NOBREAK: NOBREAK flag */
7147 if (spin->si_nobreak)
7148 {
7149 putc(SN_NOBREAK, fd); /* <sectionID> */
7150 putc(0, fd); /* <sectionflags> */
7151
7152 /* It's empty, the precense of the section flags the feature. */
7153 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
7154 }
7155
Bram Moolenaar5195e452005-08-19 20:32:47 +00007156 /* SN_SYLLABLE: syllable info.
7157 * We don't mark it required, when not supported syllables will not be
7158 * counted. */
7159 if (spin->si_syllable != NULL)
7160 {
7161 putc(SN_SYLLABLE, fd); /* <sectionID> */
7162 putc(0, fd); /* <sectionflags> */
7163
7164 l = STRLEN(spin->si_syllable);
7165 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7166 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
7167 }
7168
7169 /* end of <SECTIONS> */
7170 putc(SN_END, fd); /* <sectionend> */
7171
Bram Moolenaar50cde822005-06-05 21:54:54 +00007172
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007173 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007174 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007175 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007176 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007177 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007178 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007179 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007180 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007181 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007182 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007183 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007184 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007185
Bram Moolenaar0c405862005-06-22 22:26:26 +00007186 /* Clear the index and wnode fields in the tree. */
7187 clear_node(tree);
7188
Bram Moolenaar51485f02005-06-04 21:55:20 +00007189 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00007190 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00007191 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007192 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007193
Bram Moolenaar51485f02005-06-04 21:55:20 +00007194 /* number of nodes in 4 bytes */
7195 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00007196 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007197
Bram Moolenaar51485f02005-06-04 21:55:20 +00007198 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007199 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007200 }
7201
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007202 /* Write another byte to check for errors. */
7203 if (putc(0, fd) == EOF)
7204 retval = FAIL;
7205
7206 if (fclose(fd) == EOF)
7207 retval = FAIL;
7208
7209 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007210}
7211
7212/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007213 * Clear the index and wnode fields of "node", it siblings and its
7214 * children. This is needed because they are a union with other items to save
7215 * space.
7216 */
7217 static void
7218clear_node(node)
7219 wordnode_T *node;
7220{
7221 wordnode_T *np;
7222
7223 if (node != NULL)
7224 for (np = node; np != NULL; np = np->wn_sibling)
7225 {
7226 np->wn_u1.index = 0;
7227 np->wn_u2.wnode = NULL;
7228
7229 if (np->wn_byte != NUL)
7230 clear_node(np->wn_child);
7231 }
7232}
7233
7234
7235/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007236 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007237 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00007238 * This first writes the list of possible bytes (siblings). Then for each
7239 * byte recursively write the children.
7240 *
7241 * NOTE: The code here must match the code in read_tree(), since assumptions
7242 * are made about the indexes (so that we don't have to write them in the
7243 * file).
7244 *
7245 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007246 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007247 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00007248put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007249 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007250 wordnode_T *node;
7251 int index;
7252 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007253 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007254{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007255 int newindex = index;
7256 int siblingcount = 0;
7257 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007258 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007259
Bram Moolenaar51485f02005-06-04 21:55:20 +00007260 /* If "node" is zero the tree is empty. */
7261 if (node == NULL)
7262 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007263
Bram Moolenaar51485f02005-06-04 21:55:20 +00007264 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007265 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007266
7267 /* Count the number of siblings. */
7268 for (np = node; np != NULL; np = np->wn_sibling)
7269 ++siblingcount;
7270
7271 /* Write the sibling count. */
7272 if (fd != NULL)
7273 putc(siblingcount, fd); /* <siblingcount> */
7274
7275 /* Write each sibling byte and optionally extra info. */
7276 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007277 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007278 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007279 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007280 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007281 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007282 /* For a NUL byte (end of word) write the flags etc. */
7283 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007284 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007285 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007286 * associated condition nr (stored in wn_region). The
7287 * byte value is misused to store the "rare" and "not
7288 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00007289 if (np->wn_flags == (short_u)PFX_FLAGS)
7290 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007291 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00007292 {
7293 putc(BY_FLAGS, fd); /* <byte> */
7294 putc(np->wn_flags, fd); /* <pflags> */
7295 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007296 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007297 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007298 }
7299 else
7300 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007301 /* For word trees we write the flag/region items. */
7302 flags = np->wn_flags;
7303 if (regionmask != 0 && np->wn_region != regionmask)
7304 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007305 if (np->wn_affixID != 0)
7306 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007307 if (flags == 0)
7308 {
7309 /* word without flags or region */
7310 putc(BY_NOFLAGS, fd); /* <byte> */
7311 }
7312 else
7313 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007314 if (np->wn_flags >= 0x100)
7315 {
7316 putc(BY_FLAGS2, fd); /* <byte> */
7317 putc(flags, fd); /* <flags> */
7318 putc((unsigned)flags >> 8, fd); /* <flags2> */
7319 }
7320 else
7321 {
7322 putc(BY_FLAGS, fd); /* <byte> */
7323 putc(flags, fd); /* <flags> */
7324 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007325 if (flags & WF_REGION)
7326 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007327 if (flags & WF_AFX)
7328 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007329 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007330 }
7331 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007332 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007333 else
7334 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00007335 if (np->wn_child->wn_u1.index != 0
7336 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007337 {
7338 /* The child is written elsewhere, write the reference. */
7339 if (fd != NULL)
7340 {
7341 putc(BY_INDEX, fd); /* <byte> */
7342 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007343 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007344 }
7345 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00007346 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007347 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007348 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007349
Bram Moolenaar51485f02005-06-04 21:55:20 +00007350 if (fd != NULL)
7351 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
7352 {
7353 EMSG(_(e_write));
7354 return 0;
7355 }
7356 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007357 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007358
7359 /* Space used in the array when reading: one for each sibling and one for
7360 * the count. */
7361 newindex += siblingcount + 1;
7362
7363 /* Recursively dump the children of each sibling. */
7364 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007365 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
7366 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007367 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007368
7369 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007370}
7371
7372
7373/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00007374 * ":mkspell [-ascii] outfile infile ..."
7375 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007376 */
7377 void
7378ex_mkspell(eap)
7379 exarg_T *eap;
7380{
7381 int fcount;
7382 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007383 char_u *arg = eap->arg;
7384 int ascii = FALSE;
7385
7386 if (STRNCMP(arg, "-ascii", 6) == 0)
7387 {
7388 ascii = TRUE;
7389 arg = skipwhite(arg + 6);
7390 }
7391
7392 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
7393 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
7394 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007395 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007396 FreeWild(fcount, fnames);
7397 }
7398}
7399
7400/*
7401 * Create a Vim spell file from one or more word lists.
7402 * "fnames[0]" is the output file name.
7403 * "fnames[fcount - 1]" is the last input file name.
7404 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
7405 * and ".spl" is appended to make the output file name.
7406 */
7407 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007408mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007409 int fcount;
7410 char_u **fnames;
7411 int ascii; /* -ascii argument given */
7412 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007413 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007414{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007415 char_u fname[MAXPATHL];
7416 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00007417 char_u **innames;
7418 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007419 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007420 int i;
7421 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007422 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007423 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007424 spellinfo_T spin;
7425
7426 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007427 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007428 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007429 spin.si_followup = TRUE;
7430 spin.si_rem_accents = TRUE;
7431 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
7432 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
7433 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007434 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007435 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007436
Bram Moolenaarb765d632005-06-07 21:00:02 +00007437 /* default: fnames[0] is output file, following are input files */
7438 innames = &fnames[1];
7439 incount = fcount - 1;
7440
7441 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00007442 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007443 len = STRLEN(fnames[0]);
7444 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
7445 {
7446 /* For ":mkspell path/en.latin1.add" output file is
7447 * "path/en.latin1.add.spl". */
7448 innames = &fnames[0];
7449 incount = 1;
7450 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
7451 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007452 else if (fcount == 1)
7453 {
7454 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
7455 innames = &fnames[0];
7456 incount = 1;
7457 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
7458 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
7459 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007460 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
7461 {
7462 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007463 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007464 }
7465 else
7466 /* Name should be language, make the file name from it. */
7467 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
7468 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
7469
7470 /* Check for .ascii.spl. */
7471 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
7472 spin.si_ascii = TRUE;
7473
7474 /* Check for .add.spl. */
7475 if (strstr((char *)gettail(wfname), ".add.") != NULL)
7476 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00007477 }
7478
Bram Moolenaarb765d632005-06-07 21:00:02 +00007479 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007480 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007481 else if (vim_strchr(gettail(wfname), '_') != NULL)
7482 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00007483 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007484 EMSG(_("E754: Only up to 8 regions supported"));
7485 else
7486 {
7487 /* Check for overwriting before doing things that may take a lot of
7488 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007489 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007490 {
7491 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00007492 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007493 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007494 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007495 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007496 EMSG2(_(e_isadir2), wfname);
7497 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007498 }
7499
7500 /*
7501 * Init the aff and dic pointers.
7502 * Get the region names if there are more than 2 arguments.
7503 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007504 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007505 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007506 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007507
Bram Moolenaar3982c542005-06-08 21:56:31 +00007508 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007509 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007510 len = STRLEN(innames[i]);
7511 if (STRLEN(gettail(innames[i])) < 5
7512 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007513 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007514 EMSG2(_("E755: Invalid region in %s"), innames[i]);
7515 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007516 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00007517 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
7518 spin.si_region_name[i * 2 + 1] =
7519 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007520 }
7521 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00007522 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007523
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007524 spin.si_foldroot = wordtree_alloc(&spin);
7525 spin.si_keeproot = wordtree_alloc(&spin);
7526 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007527 if (spin.si_foldroot == NULL
7528 || spin.si_keeproot == NULL
7529 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007530 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007531 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007532 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007533 }
7534
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007535 /* When not producing a .add.spl file clear the character table when
7536 * we encounter one in the .aff file. This means we dump the current
7537 * one in the .spl file if the .aff file doesn't define one. That's
7538 * better than guessing the contents, the table will match a
7539 * previously loaded spell file. */
7540 if (!spin.si_add)
7541 spin.si_clear_chartab = TRUE;
7542
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007543 /*
7544 * Read all the .aff and .dic files.
7545 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007546 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007547 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007548 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007549 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007550 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007551 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007552
Bram Moolenaarb765d632005-06-07 21:00:02 +00007553 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007554 if (mch_stat((char *)fname, &st) >= 0)
7555 {
7556 /* Read the .aff file. Will init "spin->si_conv" based on the
7557 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007558 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007559 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007560 error = TRUE;
7561 else
7562 {
7563 /* Read the .dic file and store the words in the trees. */
7564 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00007565 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007566 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007567 error = TRUE;
7568 }
7569 }
7570 else
7571 {
7572 /* No .aff file, try reading the file as a word list. Store
7573 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007574 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007575 error = TRUE;
7576 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007577
Bram Moolenaarb765d632005-06-07 21:00:02 +00007578#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007579 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007580 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007581#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007582 }
7583
Bram Moolenaar78622822005-08-23 21:00:13 +00007584 if (spin.si_compflags != NULL && spin.si_nobreak)
7585 MSG(_("Warning: both compounding and NOBREAK specified"));
7586
Bram Moolenaar51485f02005-06-04 21:55:20 +00007587 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007588 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007589 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007590 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007591 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007592 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007593 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007594 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007595 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007596 MSG(_(msg_compressing));
Bram Moolenaarb765d632005-06-07 21:00:02 +00007597 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007598 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007599 verbose_leave();
7600 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007601 wordtree_compress(&spin, spin.si_foldroot);
7602 wordtree_compress(&spin, spin.si_keeproot);
7603 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007604 }
7605
Bram Moolenaar51485f02005-06-04 21:55:20 +00007606 if (!error)
7607 {
7608 /*
7609 * Write the info in the spell file.
7610 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007611 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007612 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007613 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007614 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007615 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007616 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007617 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007618 verbose_leave();
7619 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00007620
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007621 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +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();
7627 MSG(_("Done!"));
7628 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00007629 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007630 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007631 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007632 verbose_leave();
7633 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007634
Bram Moolenaarb765d632005-06-07 21:00:02 +00007635 /* If the file is loaded need to reload it. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007636 if (!error)
7637 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007638 }
7639
7640 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007641 ga_clear(&spin.si_rep);
7642 ga_clear(&spin.si_sal);
7643 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007644 ga_clear(&spin.si_prefcond);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007645
7646 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007647 for (i = 0; i < incount; ++i)
7648 if (afile[i] != NULL)
7649 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007650
7651 /* Free all the bits and pieces at once. */
7652 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007653 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007654}
7655
Bram Moolenaarb765d632005-06-07 21:00:02 +00007656
7657/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007658 * ":[count]spellgood {word}"
7659 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00007660 */
7661 void
7662ex_spell(eap)
7663 exarg_T *eap;
7664{
Bram Moolenaar7887d882005-07-01 22:33:52 +00007665 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007666 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007667}
7668
7669/*
7670 * Add "word[len]" to 'spellfile' as a good or bad word.
7671 */
7672 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007673spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007674 char_u *word;
7675 int len;
7676 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007677 int index; /* "zG" and "zW": zero, otherwise index in
7678 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007679{
7680 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007681 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007682 int new_spf = FALSE;
7683 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007684 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007685 char_u fnamebuf[MAXPATHL];
7686 char_u line[MAXWLEN * 2];
7687 long fpos, fpos_next = 0;
7688 int i;
7689 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007690
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007691 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007692 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007693 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00007694 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007695 int_wordlist = vim_tempname('s');
7696 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00007697 return;
7698 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007699 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007700 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007701 else
7702 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007703 /* If 'spellfile' isn't set figure out a good default value. */
7704 if (*curbuf->b_p_spf == NUL)
7705 {
7706 init_spellfile();
7707 new_spf = TRUE;
7708 }
7709
7710 if (*curbuf->b_p_spf == NUL)
7711 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00007712 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00007713 return;
7714 }
7715
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007716 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
7717 {
7718 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
7719 if (i == index)
7720 break;
7721 if (*spf == NUL)
7722 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00007723 EMSGN(_("E765: 'spellfile' does not have %ld entries"), index);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007724 return;
7725 }
7726 }
7727
Bram Moolenaarb765d632005-06-07 21:00:02 +00007728 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007729 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007730 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
7731 buf = NULL;
7732 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00007733 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007734 EMSG(_(e_bufloaded));
7735 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007736 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007737
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007738 fname = fnamebuf;
7739 }
7740
7741 if (bad)
7742 {
7743 /* When the word also appears as good word we need to remove that one,
7744 * since its flags sort before the one with WF_BANNED. */
7745 fd = mch_fopen((char *)fname, "r");
7746 if (fd != NULL)
7747 {
7748 while (!vim_fgets(line, MAXWLEN * 2, fd))
7749 {
7750 fpos = fpos_next;
7751 fpos_next = ftell(fd);
7752 if (STRNCMP(word, line, len) == 0
7753 && (line[len] == '/' || line[len] < ' '))
7754 {
7755 /* Found duplicate word. Remove it by writing a '#' at
7756 * the start of the line. Mixing reading and writing
7757 * doesn't work for all systems, close the file first. */
7758 fclose(fd);
7759 fd = mch_fopen((char *)fname, "r+");
7760 if (fd == NULL)
7761 break;
7762 if (fseek(fd, fpos, SEEK_SET) == 0)
7763 fputc('#', fd);
7764 fseek(fd, fpos_next, SEEK_SET);
7765 }
7766 }
7767 fclose(fd);
7768 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007769 }
7770
7771 fd = mch_fopen((char *)fname, "a");
7772 if (fd == NULL && new_spf)
7773 {
7774 /* We just initialized the 'spellfile' option and can't open the file.
7775 * We may need to create the "spell" directory first. We already
7776 * checked the runtime directory is writable in init_spellfile(). */
7777 STRCPY(NameBuff, fname);
7778 *gettail_sep(NameBuff) = NUL;
7779 if (mch_stat((char *)NameBuff, &st) < 0)
7780 {
7781 /* The directory doesn't exist. Try creating it and opening the
7782 * file again. */
7783 vim_mkdir(NameBuff, 0755);
7784 fd = mch_fopen((char *)fname, "a");
7785 }
7786 }
7787
7788 if (fd == NULL)
7789 EMSG2(_(e_notopen), fname);
7790 else
7791 {
7792 if (bad)
7793 fprintf(fd, "%.*s/!\n", len, word);
7794 else
7795 fprintf(fd, "%.*s\n", len, word);
7796 fclose(fd);
7797
7798 /* Update the .add.spl file. */
7799 mkspell(1, &fname, FALSE, TRUE, TRUE);
7800
7801 /* If the .add file is edited somewhere, reload it. */
7802 if (buf != NULL)
7803 buf_reload(buf);
7804
7805 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007806 }
7807}
7808
7809/*
7810 * Initialize 'spellfile' for the current buffer.
7811 */
7812 static void
7813init_spellfile()
7814{
7815 char_u buf[MAXPATHL];
7816 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007817 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007818 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007819 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007820 int aspath = FALSE;
7821 char_u *lstart = curbuf->b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007822
7823 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
7824 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007825 /* Find the end of the language name. Exclude the region. If there
7826 * is a path separator remember the start of the tail. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007827 for (lend = curbuf->b_p_spl; *lend != NUL
7828 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007829 if (vim_ispathsep(*lend))
7830 {
7831 aspath = TRUE;
7832 lstart = lend + 1;
7833 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007834
7835 /* Loop over all entries in 'runtimepath'. Use the first one where we
7836 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007837 rtp = p_rtp;
7838 while (*rtp != NUL)
7839 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007840 if (aspath)
7841 /* Use directory of an entry with path, e.g., for
7842 * "/dir/lg.utf-8.spl" use "/dir". */
7843 vim_strncpy(buf, curbuf->b_p_spl, lstart - curbuf->b_p_spl - 1);
7844 else
7845 /* Copy the path from 'runtimepath' to buf[]. */
7846 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00007847 if (filewritable(buf) == 2)
7848 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00007849 /* Use the first language name from 'spelllang' and the
7850 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007851 if (aspath)
7852 vim_strncpy(buf, curbuf->b_p_spl, lend - curbuf->b_p_spl);
7853 else
7854 {
7855 l = STRLEN(buf);
7856 vim_snprintf((char *)buf + l, MAXPATHL - l,
7857 "/spell/%.*s", (int)(lend - lstart), lstart);
7858 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007859 l = STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007860 fname = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang->sl_fname;
7861 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
7862 fname != NULL
7863 && strstr((char *)gettail(fname), ".ascii.") != NULL
7864 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00007865 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
7866 break;
7867 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007868 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007869 }
7870 }
7871}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007872
Bram Moolenaar51485f02005-06-04 21:55:20 +00007873
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007874/*
7875 * Init the chartab used for spelling for ASCII.
7876 * EBCDIC is not supported!
7877 */
7878 static void
7879clear_spell_chartab(sp)
7880 spelltab_T *sp;
7881{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007882 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007883
7884 /* Init everything to FALSE. */
7885 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
7886 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
7887 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007888 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007889 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007890 sp->st_upper[i] = i;
7891 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007892
7893 /* We include digits. A word shouldn't start with a digit, but handling
7894 * that is done separately. */
7895 for (i = '0'; i <= '9'; ++i)
7896 sp->st_isw[i] = TRUE;
7897 for (i = 'A'; i <= 'Z'; ++i)
7898 {
7899 sp->st_isw[i] = TRUE;
7900 sp->st_isu[i] = TRUE;
7901 sp->st_fold[i] = i + 0x20;
7902 }
7903 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007904 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007905 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007906 sp->st_upper[i] = i - 0x20;
7907 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007908}
7909
7910/*
7911 * Init the chartab used for spelling. Only depends on 'encoding'.
7912 * Called once while starting up and when 'encoding' changes.
7913 * The default is to use isalpha(), but the spell file should define the word
7914 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007915 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007916 */
7917 void
7918init_spell_chartab()
7919{
7920 int i;
7921
7922 did_set_spelltab = FALSE;
7923 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007924#ifdef FEAT_MBYTE
7925 if (enc_dbcs)
7926 {
7927 /* DBCS: assume double-wide characters are word characters. */
7928 for (i = 128; i <= 255; ++i)
7929 if (MB_BYTE2LEN(i) == 2)
7930 spelltab.st_isw[i] = TRUE;
7931 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007932 else if (enc_utf8)
7933 {
7934 for (i = 128; i < 256; ++i)
7935 {
7936 spelltab.st_isu[i] = utf_isupper(i);
7937 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
7938 spelltab.st_fold[i] = utf_fold(i);
7939 spelltab.st_upper[i] = utf_toupper(i);
7940 }
7941 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007942 else
7943#endif
7944 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007945 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007946 for (i = 128; i < 256; ++i)
7947 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007948 if (MB_ISUPPER(i))
7949 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007950 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007951 spelltab.st_isu[i] = TRUE;
7952 spelltab.st_fold[i] = MB_TOLOWER(i);
7953 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007954 else if (MB_ISLOWER(i))
7955 {
7956 spelltab.st_isw[i] = TRUE;
7957 spelltab.st_upper[i] = MB_TOUPPER(i);
7958 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007959 }
7960 }
7961}
7962
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007963/*
7964 * Set the spell character tables from strings in the affix file.
7965 */
7966 static int
7967set_spell_chartab(fol, low, upp)
7968 char_u *fol;
7969 char_u *low;
7970 char_u *upp;
7971{
7972 /* We build the new tables here first, so that we can compare with the
7973 * previous one. */
7974 spelltab_T new_st;
7975 char_u *pf = fol, *pl = low, *pu = upp;
7976 int f, l, u;
7977
7978 clear_spell_chartab(&new_st);
7979
7980 while (*pf != NUL)
7981 {
7982 if (*pl == NUL || *pu == NUL)
7983 {
7984 EMSG(_(e_affform));
7985 return FAIL;
7986 }
7987#ifdef FEAT_MBYTE
7988 f = mb_ptr2char_adv(&pf);
7989 l = mb_ptr2char_adv(&pl);
7990 u = mb_ptr2char_adv(&pu);
7991#else
7992 f = *pf++;
7993 l = *pl++;
7994 u = *pu++;
7995#endif
7996 /* Every character that appears is a word character. */
7997 if (f < 256)
7998 new_st.st_isw[f] = TRUE;
7999 if (l < 256)
8000 new_st.st_isw[l] = TRUE;
8001 if (u < 256)
8002 new_st.st_isw[u] = TRUE;
8003
8004 /* if "LOW" and "FOL" are not the same the "LOW" char needs
8005 * case-folding */
8006 if (l < 256 && l != f)
8007 {
8008 if (f >= 256)
8009 {
8010 EMSG(_(e_affrange));
8011 return FAIL;
8012 }
8013 new_st.st_fold[l] = f;
8014 }
8015
8016 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008017 * case-folding, it's upper case and the "UPP" is the upper case of
8018 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008019 if (u < 256 && u != f)
8020 {
8021 if (f >= 256)
8022 {
8023 EMSG(_(e_affrange));
8024 return FAIL;
8025 }
8026 new_st.st_fold[u] = f;
8027 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008028 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008029 }
8030 }
8031
8032 if (*pl != NUL || *pu != NUL)
8033 {
8034 EMSG(_(e_affform));
8035 return FAIL;
8036 }
8037
8038 return set_spell_finish(&new_st);
8039}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008040
8041/*
8042 * Set the spell character tables from strings in the .spl file.
8043 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008044 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008045set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008046 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008047 int cnt; /* length of "flags" */
8048 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008049{
8050 /* We build the new tables here first, so that we can compare with the
8051 * previous one. */
8052 spelltab_T new_st;
8053 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008054 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008055 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008056
8057 clear_spell_chartab(&new_st);
8058
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008059 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008060 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008061 if (i < cnt)
8062 {
8063 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
8064 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
8065 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008066
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008067 if (*p != NUL)
8068 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008069#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008070 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008071#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008072 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008073#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008074 new_st.st_fold[i + 128] = c;
8075 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
8076 new_st.st_upper[c] = i + 128;
8077 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008078 }
8079
Bram Moolenaar5195e452005-08-19 20:32:47 +00008080 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008081}
8082
8083 static int
8084set_spell_finish(new_st)
8085 spelltab_T *new_st;
8086{
8087 int i;
8088
8089 if (did_set_spelltab)
8090 {
8091 /* check that it's the same table */
8092 for (i = 0; i < 256; ++i)
8093 {
8094 if (spelltab.st_isw[i] != new_st->st_isw[i]
8095 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008096 || spelltab.st_fold[i] != new_st->st_fold[i]
8097 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008098 {
8099 EMSG(_("E763: Word characters differ between spell files"));
8100 return FAIL;
8101 }
8102 }
8103 }
8104 else
8105 {
8106 /* copy the new spelltab into the one being used */
8107 spelltab = *new_st;
8108 did_set_spelltab = TRUE;
8109 }
8110
8111 return OK;
8112}
8113
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008114/*
Bram Moolenaarea408852005-06-25 22:49:46 +00008115 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008116 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00008117 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008118 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00008119 */
8120 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008121spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00008122 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008123 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00008124{
Bram Moolenaarea408852005-06-25 22:49:46 +00008125#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008126 char_u *s;
8127 int l;
8128 int c;
8129
8130 if (has_mbyte)
8131 {
8132 l = MB_BYTE2LEN(*p);
8133 s = p;
8134 if (l == 1)
8135 {
8136 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008137 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008138 {
8139 s = p + 1; /* skip a mid-word character */
8140 l = MB_BYTE2LEN(*s);
8141 }
8142 }
8143 else
8144 {
8145 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008146 if (c < 256 ? buf->b_spell_ismw[c]
8147 : (buf->b_spell_ismw_mb != NULL
8148 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008149 {
8150 s = p + l;
8151 l = MB_BYTE2LEN(*s);
8152 }
8153 }
8154
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008155 c = mb_ptr2char(s);
8156 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008157 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008158 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008159 }
Bram Moolenaarea408852005-06-25 22:49:46 +00008160#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008161
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008162 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
8163}
8164
8165/*
8166 * Return TRUE if "p" points to a word character.
8167 * Unlike spell_iswordp() this doesn't check for "midword" characters.
8168 */
8169 static int
8170spell_iswordp_nmw(p)
8171 char_u *p;
8172{
8173#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008174 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008175
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008176 if (has_mbyte)
8177 {
8178 c = mb_ptr2char(p);
8179 if (c > 255)
8180 return mb_get_class(p) >= 2;
8181 return spelltab.st_isw[c];
8182 }
8183#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008184 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00008185}
8186
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008187#ifdef FEAT_MBYTE
8188/*
8189 * Return TRUE if "p" points to a word character.
8190 * Wide version of spell_iswordp().
8191 */
8192 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008193spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008194 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008195 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008196{
8197 int *s;
8198
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008199 if (*p < 256 ? buf->b_spell_ismw[*p]
8200 : (buf->b_spell_ismw_mb != NULL
8201 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008202 s = p + 1;
8203 else
8204 s = p;
8205
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008206 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008207 {
8208 if (enc_utf8)
8209 return utf_class(*s) >= 2;
8210 if (enc_dbcs)
8211 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
8212 return 0;
8213 }
8214 return spelltab.st_isw[*s];
8215}
8216#endif
8217
Bram Moolenaarea408852005-06-25 22:49:46 +00008218/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008219 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00008220 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008221 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008222 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008223write_spell_prefcond(fd, gap)
8224 FILE *fd;
8225 garray_T *gap;
8226{
8227 int i;
8228 char_u *p;
8229 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008230 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008231
Bram Moolenaar5195e452005-08-19 20:32:47 +00008232 if (fd != NULL)
8233 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
8234
8235 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008236
8237 for (i = 0; i < gap->ga_len; ++i)
8238 {
8239 /* <prefcond> : <condlen> <condstr> */
8240 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00008241 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008242 {
8243 len = STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008244 if (fd != NULL)
8245 {
8246 fputc(len, fd);
8247 fwrite(p, (size_t)len, (size_t)1, fd);
8248 }
8249 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008250 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008251 else if (fd != NULL)
8252 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008253 }
8254
Bram Moolenaar5195e452005-08-19 20:32:47 +00008255 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008256}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008257
8258/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008259 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
8260 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008261 * When using a multi-byte 'encoding' the length may change!
8262 * Returns FAIL when something wrong.
8263 */
8264 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008265spell_casefold(str, len, buf, buflen)
8266 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008267 int len;
8268 char_u *buf;
8269 int buflen;
8270{
8271 int i;
8272
8273 if (len >= buflen)
8274 {
8275 buf[0] = NUL;
8276 return FAIL; /* result will not fit */
8277 }
8278
8279#ifdef FEAT_MBYTE
8280 if (has_mbyte)
8281 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008282 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008283 char_u *p;
8284 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008285
8286 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008287 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008288 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008289 if (outi + MB_MAXBYTES > buflen)
8290 {
8291 buf[outi] = NUL;
8292 return FAIL;
8293 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008294 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008295 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008296 }
8297 buf[outi] = NUL;
8298 }
8299 else
8300#endif
8301 {
8302 /* Be quick for non-multibyte encodings. */
8303 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008304 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008305 buf[i] = NUL;
8306 }
8307
8308 return OK;
8309}
8310
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008311#define SPS_BEST 1
8312#define SPS_FAST 2
8313#define SPS_DOUBLE 4
8314
8315static int sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008316static int sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008317
8318/*
8319 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00008320 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008321 */
8322 int
8323spell_check_sps()
8324{
8325 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008326 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008327 char_u buf[MAXPATHL];
8328 int f;
8329
8330 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008331 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008332
8333 for (p = p_sps; *p != NUL; )
8334 {
8335 copy_option_part(&p, buf, MAXPATHL, ",");
8336
8337 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008338 if (VIM_ISDIGIT(*buf))
8339 {
8340 s = buf;
8341 sps_limit = getdigits(&s);
8342 if (*s != NUL && !VIM_ISDIGIT(*s))
8343 f = -1;
8344 }
8345 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008346 f = SPS_BEST;
8347 else if (STRCMP(buf, "fast") == 0)
8348 f = SPS_FAST;
8349 else if (STRCMP(buf, "double") == 0)
8350 f = SPS_DOUBLE;
8351 else if (STRNCMP(buf, "expr:", 5) != 0
8352 && STRNCMP(buf, "file:", 5) != 0)
8353 f = -1;
8354
8355 if (f == -1 || (sps_flags != 0 && f != 0))
8356 {
8357 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008358 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008359 return FAIL;
8360 }
8361 if (f != 0)
8362 sps_flags = f;
8363 }
8364
8365 if (sps_flags == 0)
8366 sps_flags = SPS_BEST;
8367
8368 return OK;
8369}
8370
8371/* Remember what "z?" replaced. */
8372static char_u *repl_from = NULL;
8373static char_u *repl_to = NULL;
8374
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008375/*
8376 * "z?": Find badly spelled word under or after the cursor.
8377 * Give suggestions for the properly spelled word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00008378 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008379 */
8380 void
Bram Moolenaard12a1322005-08-21 22:08:24 +00008381spell_suggest(count)
8382 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008383{
8384 char_u *line;
8385 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008386 char_u wcopy[MAXWLEN + 2];
8387 char_u *p;
8388 int i;
8389 int c;
8390 suginfo_T sug;
8391 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008392 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008393 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008394 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00008395 int selected = count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008396
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008397 /* Find the start of the badly spelled word. */
Bram Moolenaar95529562005-08-25 21:21:38 +00008398 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00008399 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008400 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008401 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
8402 return;
8403
8404 /* No bad word or it starts after the cursor: use the word under the
8405 * cursor. */
8406 curwin->w_cursor = prev_cursor;
8407 line = ml_get_curline();
8408 p = line + curwin->w_cursor.col;
8409 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008410 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00008411 mb_ptr_back(line, p);
8412 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008413 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00008414 mb_ptr_adv(p);
8415
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008416 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008417 {
8418 beep_flush();
8419 return;
8420 }
8421 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008422 }
8423
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008424 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008425
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008426 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008427 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008428
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008429 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008430
Bram Moolenaar5195e452005-08-19 20:32:47 +00008431 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
8432 * 'spellsuggest', whatever is smaller. */
8433 if (sps_limit > (int)Rows - 2)
8434 limit = (int)Rows - 2;
8435 else
8436 limit = sps_limit;
8437 spell_find_suggest(line + curwin->w_cursor.col, &sug, limit,
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008438 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008439
8440 if (sug.su_ga.ga_len == 0)
8441 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00008442 else if (count > 0)
8443 {
8444 if (count > sug.su_ga.ga_len)
8445 smsg((char_u *)_("Sorry, only %ld suggestions"),
8446 (long)sug.su_ga.ga_len);
8447 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008448 else
8449 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008450 vim_free(repl_from);
8451 repl_from = NULL;
8452 vim_free(repl_to);
8453 repl_to = NULL;
8454
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008455#ifdef FEAT_RIGHTLEFT
8456 /* When 'rightleft' is set the list is drawn right-left. */
8457 cmdmsg_rl = curwin->w_p_rl;
8458 if (cmdmsg_rl)
8459 msg_col = Columns - 1;
8460#endif
8461
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008462 /* List the suggestions. */
8463 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008464 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008465 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
8466 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008467#ifdef FEAT_RIGHTLEFT
8468 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
8469 {
8470 /* And now the rabbit from the high hat: Avoid showing the
8471 * untranslated message rightleft. */
8472 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
8473 sug.su_badlen, sug.su_badptr);
8474 }
8475#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008476 msg_puts(IObuff);
8477 msg_clr_eos();
8478 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00008479
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008480 msg_scroll = TRUE;
8481 for (i = 0; i < sug.su_ga.ga_len; ++i)
8482 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008483 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008484
8485 /* The suggested word may replace only part of the bad word, add
8486 * the not replaced part. */
8487 STRCPY(wcopy, stp->st_word);
8488 if (sug.su_badlen > stp->st_orglen)
8489 vim_strncpy(wcopy + STRLEN(wcopy),
8490 sug.su_badptr + stp->st_orglen,
8491 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008492 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
8493#ifdef FEAT_RIGHTLEFT
8494 if (cmdmsg_rl)
8495 rl_mirror(IObuff);
8496#endif
8497 msg_puts(IObuff);
8498
8499 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008500 msg_puts(IObuff);
8501
8502 /* The word may replace more than "su_badlen". */
8503 if (sug.su_badlen < stp->st_orglen)
8504 {
8505 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
8506 stp->st_orglen, sug.su_badptr);
8507 msg_puts(IObuff);
8508 }
8509
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008510 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008511 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008512 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008513 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008514 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008515 stp->st_salscore ? "s " : "",
8516 stp->st_score, stp->st_altscore);
8517 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008518 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00008519 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008520#ifdef FEAT_RIGHTLEFT
8521 if (cmdmsg_rl)
8522 /* Mirror the numbers, but keep the leading space. */
8523 rl_mirror(IObuff + 1);
8524#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00008525 msg_advance(30);
8526 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008527 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008528 msg_putchar('\n');
8529 }
8530
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008531#ifdef FEAT_RIGHTLEFT
8532 cmdmsg_rl = FALSE;
8533 msg_col = 0;
8534#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008535 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00008536 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008537 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00008538 selected -= lines_left;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008539 }
8540
Bram Moolenaard12a1322005-08-21 22:08:24 +00008541 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
8542 {
8543 /* Save the from and to text for :spellrepall. */
8544 stp = &SUG(sug.su_ga, selected - 1);
8545 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
8546 repl_to = vim_strsave(stp->st_word);
8547
8548 /* Replace the word. */
8549 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
8550 if (p != NULL)
8551 {
8552 c = sug.su_badptr - line;
8553 mch_memmove(p, line, c);
8554 STRCPY(p + c, stp->st_word);
8555 STRCAT(p, sug.su_badptr + stp->st_orglen);
8556 ml_replace(curwin->w_cursor.lnum, p, FALSE);
8557 curwin->w_cursor.col = c;
8558 changed_bytes(curwin->w_cursor.lnum, c);
8559
8560 /* For redo we use a change-word command. */
8561 ResetRedobuff();
8562 AppendToRedobuff((char_u *)"ciw");
8563 AppendToRedobuff(stp->st_word);
8564 AppendCharToRedobuff(ESC);
8565 }
8566 }
8567 else
8568 curwin->w_cursor = prev_cursor;
8569
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008570 spell_find_cleanup(&sug);
8571}
8572
8573/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008574 * Check if the word at line "lnum" column "col" is required to start with a
8575 * capital. This uses 'spellcapcheck' of the current buffer.
8576 */
8577 static int
8578check_need_cap(lnum, col)
8579 linenr_T lnum;
8580 colnr_T col;
8581{
8582 int need_cap = FALSE;
8583 char_u *line;
8584 char_u *line_copy = NULL;
8585 char_u *p;
8586 colnr_T endcol;
8587 regmatch_T regmatch;
8588
8589 if (curbuf->b_cap_prog == NULL)
8590 return FALSE;
8591
8592 line = ml_get_curline();
8593 endcol = 0;
8594 if ((int)(skipwhite(line) - line) >= (int)col)
8595 {
8596 /* At start of line, check if previous line is empty or sentence
8597 * ends there. */
8598 if (lnum == 1)
8599 need_cap = TRUE;
8600 else
8601 {
8602 line = ml_get(lnum - 1);
8603 if (*skipwhite(line) == NUL)
8604 need_cap = TRUE;
8605 else
8606 {
8607 /* Append a space in place of the line break. */
8608 line_copy = concat_str(line, (char_u *)" ");
8609 line = line_copy;
8610 endcol = STRLEN(line);
8611 }
8612 }
8613 }
8614 else
8615 endcol = col;
8616
8617 if (endcol > 0)
8618 {
8619 /* Check if sentence ends before the bad word. */
8620 regmatch.regprog = curbuf->b_cap_prog;
8621 regmatch.rm_ic = FALSE;
8622 p = line + endcol;
8623 for (;;)
8624 {
8625 mb_ptr_back(line, p);
8626 if (p == line || spell_iswordp_nmw(p))
8627 break;
8628 if (vim_regexec(&regmatch, p, 0)
8629 && regmatch.endp[0] == line + endcol)
8630 {
8631 need_cap = TRUE;
8632 break;
8633 }
8634 }
8635 }
8636
8637 vim_free(line_copy);
8638
8639 return need_cap;
8640}
8641
8642
8643/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008644 * ":spellrepall"
8645 */
8646/*ARGSUSED*/
8647 void
8648ex_spellrepall(eap)
8649 exarg_T *eap;
8650{
8651 pos_T pos = curwin->w_cursor;
8652 char_u *frompat;
8653 int addlen;
8654 char_u *line;
8655 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008656 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008657 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008658
8659 if (repl_from == NULL || repl_to == NULL)
8660 {
8661 EMSG(_("E752: No previous spell replacement"));
8662 return;
8663 }
8664 addlen = STRLEN(repl_to) - STRLEN(repl_from);
8665
8666 frompat = alloc(STRLEN(repl_from) + 7);
8667 if (frompat == NULL)
8668 return;
8669 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
8670 p_ws = FALSE;
8671
Bram Moolenaar5195e452005-08-19 20:32:47 +00008672 sub_nsubs = 0;
8673 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008674 curwin->w_cursor.lnum = 0;
8675 while (!got_int)
8676 {
8677 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
8678 || u_save_cursor() == FAIL)
8679 break;
8680
8681 /* Only replace when the right word isn't there yet. This happens
8682 * when changing "etc" to "etc.". */
8683 line = ml_get_curline();
8684 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
8685 repl_to, STRLEN(repl_to)) != 0)
8686 {
8687 p = alloc(STRLEN(line) + addlen + 1);
8688 if (p == NULL)
8689 break;
8690 mch_memmove(p, line, curwin->w_cursor.col);
8691 STRCPY(p + curwin->w_cursor.col, repl_to);
8692 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
8693 ml_replace(curwin->w_cursor.lnum, p, FALSE);
8694 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008695
8696 if (curwin->w_cursor.lnum != prev_lnum)
8697 {
8698 ++sub_nlines;
8699 prev_lnum = curwin->w_cursor.lnum;
8700 }
8701 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008702 }
8703 curwin->w_cursor.col += STRLEN(repl_to);
8704 }
8705
8706 p_ws = save_ws;
8707 curwin->w_cursor = pos;
8708 vim_free(frompat);
8709
Bram Moolenaar5195e452005-08-19 20:32:47 +00008710 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008711 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008712 else
8713 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008714}
8715
8716/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008717 * Find spell suggestions for "word". Return them in the growarray "*gap" as
8718 * a list of allocated strings.
8719 */
8720 void
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008721spell_suggest_list(gap, word, maxcount, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008722 garray_T *gap;
8723 char_u *word;
8724 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008725 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008726{
8727 suginfo_T sug;
8728 int i;
8729 suggest_T *stp;
8730 char_u *wcopy;
8731
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008732 spell_find_suggest(word, &sug, maxcount, FALSE, need_cap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008733
8734 /* Make room in "gap". */
8735 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
8736 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
8737 return;
8738
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008739 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008740 {
8741 stp = &SUG(sug.su_ga, i);
8742
8743 /* The suggested word may replace only part of "word", add the not
8744 * replaced part. */
8745 wcopy = alloc(STRLEN(stp->st_word)
8746 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
8747 if (wcopy == NULL)
8748 break;
8749 STRCPY(wcopy, stp->st_word);
8750 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
8751 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
8752 }
8753
8754 spell_find_cleanup(&sug);
8755}
8756
8757/*
8758 * Find spell suggestions for the word at the start of "badptr".
8759 * Return the suggestions in "su->su_ga".
8760 * The maximum number of suggestions is "maxcount".
8761 * Note: does use info for the current window.
8762 * This is based on the mechanisms of Aspell, but completely reimplemented.
8763 */
8764 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008765spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008766 char_u *badptr;
8767 suginfo_T *su;
8768 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00008769 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008770 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008771{
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008772 int attr = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008773 char_u buf[MAXPATHL];
8774 char_u *p;
8775 int do_combine = FALSE;
8776 char_u *sps_copy;
8777#ifdef FEAT_EVAL
8778 static int expr_busy = FALSE;
8779#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008780 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00008781 int i;
8782 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008783
8784 /*
8785 * Set the info in "*su".
8786 */
8787 vim_memset(su, 0, sizeof(suginfo_T));
8788 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
8789 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008790 if (*badptr == NUL)
8791 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008792 hash_init(&su->su_banned);
8793
8794 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008795 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008796 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008797 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008798
8799 if (su->su_badlen >= MAXWLEN)
8800 su->su_badlen = MAXWLEN - 1; /* just in case */
8801 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
8802 (void)spell_casefold(su->su_badptr, su->su_badlen,
8803 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008804 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008805 su->su_badflags = badword_captype(su->su_badptr,
8806 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008807 if (need_cap)
8808 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008809
Bram Moolenaar8b96d642005-09-05 22:05:30 +00008810 /* Find the default language for sound folding. We simply use the first
8811 * one in 'spelllang' that supports sound folding. That's good for when
8812 * using multiple files for one language, it's not that bad when mixing
8813 * languages (e.g., "pl,en"). */
8814 for (i = 0; i < curbuf->b_langp.ga_len; ++i)
8815 {
8816 lp = LANGP_ENTRY(curbuf->b_langp, i);
8817 if (lp->lp_sallang != NULL)
8818 {
8819 su->su_sallang = lp->lp_sallang;
8820 break;
8821 }
8822 }
8823
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008824 /* If the word is not capitalised and spell_check() doesn't consider the
8825 * word to be bad then it might need to be capitalised. Add a suggestion
8826 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008827 c = PTR2CHAR(su->su_badptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008828 if (!SPELL_ISUPPER(c) && attr == 0)
8829 {
8830 make_case_word(su->su_badword, buf, WF_ONECAP);
8831 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00008832 0, TRUE, su->su_sallang);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008833 }
8834
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008835 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00008836 if (banbadword)
8837 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008838
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008839 /* Make a copy of 'spellsuggest', because the expression may change it. */
8840 sps_copy = vim_strsave(p_sps);
8841 if (sps_copy == NULL)
8842 return;
8843
8844 /* Loop over the items in 'spellsuggest'. */
8845 for (p = sps_copy; *p != NUL; )
8846 {
8847 copy_option_part(&p, buf, MAXPATHL, ",");
8848
8849 if (STRNCMP(buf, "expr:", 5) == 0)
8850 {
8851#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008852 /* Evaluate an expression. Skip this when called recursively,
8853 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008854 if (!expr_busy)
8855 {
8856 expr_busy = TRUE;
8857 spell_suggest_expr(su, buf + 5);
8858 expr_busy = FALSE;
8859 }
8860#endif
8861 }
8862 else if (STRNCMP(buf, "file:", 5) == 0)
8863 /* Use list of suggestions in a file. */
8864 spell_suggest_file(su, buf + 5);
8865 else
8866 {
8867 /* Use internal method. */
8868 spell_suggest_intern(su);
8869 if (sps_flags & SPS_DOUBLE)
8870 do_combine = TRUE;
8871 }
8872 }
8873
8874 vim_free(sps_copy);
8875
8876 if (do_combine)
8877 /* Combine the two list of suggestions. This must be done last,
8878 * because sorting changes the order again. */
8879 score_combine(su);
8880}
8881
8882#ifdef FEAT_EVAL
8883/*
8884 * Find suggestions by evaluating expression "expr".
8885 */
8886 static void
8887spell_suggest_expr(su, expr)
8888 suginfo_T *su;
8889 char_u *expr;
8890{
8891 list_T *list;
8892 listitem_T *li;
8893 int score;
8894 char_u *p;
8895
8896 /* The work is split up in a few parts to avoid having to export
8897 * suginfo_T.
8898 * First evaluate the expression and get the resulting list. */
8899 list = eval_spell_expr(su->su_badword, expr);
8900 if (list != NULL)
8901 {
8902 /* Loop over the items in the list. */
8903 for (li = list->lv_first; li != NULL; li = li->li_next)
8904 if (li->li_tv.v_type == VAR_LIST)
8905 {
8906 /* Get the word and the score from the items. */
8907 score = get_spellword(li->li_tv.vval.v_list, &p);
8908 if (score >= 0)
8909 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00008910 su->su_badlen, score, 0, TRUE, su->su_sallang);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008911 }
8912 list_unref(list);
8913 }
8914
8915 /* Sort the suggestions and truncate at "maxcount". */
8916 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8917}
8918#endif
8919
8920/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008921 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008922 */
8923 static void
8924spell_suggest_file(su, fname)
8925 suginfo_T *su;
8926 char_u *fname;
8927{
8928 FILE *fd;
8929 char_u line[MAXWLEN * 2];
8930 char_u *p;
8931 int len;
8932 char_u cword[MAXWLEN];
8933
8934 /* Open the file. */
8935 fd = mch_fopen((char *)fname, "r");
8936 if (fd == NULL)
8937 {
8938 EMSG2(_(e_notopen), fname);
8939 return;
8940 }
8941
8942 /* Read it line by line. */
8943 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
8944 {
8945 line_breakcheck();
8946
8947 p = vim_strchr(line, '/');
8948 if (p == NULL)
8949 continue; /* No Tab found, just skip the line. */
8950 *p++ = NUL;
8951 if (STRICMP(su->su_badword, line) == 0)
8952 {
8953 /* Match! Isolate the good word, until CR or NL. */
8954 for (len = 0; p[len] >= ' '; ++len)
8955 ;
8956 p[len] = NUL;
8957
8958 /* If the suggestion doesn't have specific case duplicate the case
8959 * of the bad word. */
8960 if (captype(p, NULL) == 0)
8961 {
8962 make_case_word(p, cword, su->su_badflags);
8963 p = cword;
8964 }
8965
8966 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00008967 SCORE_FILE, 0, TRUE, su->su_sallang);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008968 }
8969 }
8970
8971 fclose(fd);
8972
8973 /* Sort the suggestions and truncate at "maxcount". */
8974 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8975}
8976
8977/*
8978 * Find suggestions for the internal method indicated by "sps_flags".
8979 */
8980 static void
8981spell_suggest_intern(su)
8982 suginfo_T *su;
8983{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008984 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008985 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008986 *
8987 * Set a maximum score to limit the combination of operations that is
8988 * tried.
8989 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008990 suggest_try_special(su);
8991
8992 /*
8993 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
8994 * from the .aff file and inserting a space (split the word).
8995 */
8996 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008997
8998 /* For the resulting top-scorers compute the sound-a-like score. */
8999 if (sps_flags & SPS_DOUBLE)
9000 score_comp_sal(su);
9001
9002 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00009003 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009004 *
9005 * Only do this when we don't have a lot of suggestions yet, because it's
9006 * very slow and often doesn't find new suggestions.
9007 */
9008 if ((sps_flags & SPS_DOUBLE)
9009 || (!(sps_flags & SPS_FAST)
9010 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
9011 {
9012 /* Allow a higher score now. */
9013 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009014 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009015 }
9016
9017 /* When CTRL-C was hit while searching do show the results. */
9018 ui_breakcheck();
9019 if (got_int)
9020 {
9021 (void)vgetc();
9022 got_int = FALSE;
9023 }
9024
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009025 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009026 {
9027 if (sps_flags & SPS_BEST)
9028 /* Adjust the word score for how it sounds like. */
9029 rescore_suggestions(su);
9030
9031 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009032 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009033 }
9034}
9035
9036/*
9037 * Free the info put in "*su" by spell_find_suggest().
9038 */
9039 static void
9040spell_find_cleanup(su)
9041 suginfo_T *su;
9042{
9043 int i;
9044
9045 /* Free the suggestions. */
9046 for (i = 0; i < su->su_ga.ga_len; ++i)
9047 vim_free(SUG(su->su_ga, i).st_word);
9048 ga_clear(&su->su_ga);
9049 for (i = 0; i < su->su_sga.ga_len; ++i)
9050 vim_free(SUG(su->su_sga, i).st_word);
9051 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009052
9053 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009054 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009055}
9056
9057/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009058 * Make a copy of "word", with the first letter upper or lower cased, to
9059 * "wcopy[MAXWLEN]". "word" must not be empty.
9060 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009061 */
9062 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009063onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009064 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009065 char_u *wcopy;
9066 int upper; /* TRUE: first letter made upper case */
9067{
9068 char_u *p;
9069 int c;
9070 int l;
9071
9072 p = word;
9073#ifdef FEAT_MBYTE
9074 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009075 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009076 else
9077#endif
9078 c = *p++;
9079 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009080 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009081 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009082 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009083#ifdef FEAT_MBYTE
9084 if (has_mbyte)
9085 l = mb_char2bytes(c, wcopy);
9086 else
9087#endif
9088 {
9089 l = 1;
9090 wcopy[0] = c;
9091 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009092 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009093}
9094
9095/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009096 * Make a copy of "word" with all the letters upper cased into
9097 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009098 */
9099 static void
9100allcap_copy(word, wcopy)
9101 char_u *word;
9102 char_u *wcopy;
9103{
9104 char_u *s;
9105 char_u *d;
9106 int c;
9107
9108 d = wcopy;
9109 for (s = word; *s != NUL; )
9110 {
9111#ifdef FEAT_MBYTE
9112 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009113 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009114 else
9115#endif
9116 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00009117
9118#ifdef FEAT_MBYTE
9119 /* We only change ß to SS when we are certain latin1 is used. It
9120 * would cause weird errors in other 8-bit encodings. */
9121 if (enc_latin1like && c == 0xdf)
9122 {
9123 c = 'S';
9124 if (d - wcopy >= MAXWLEN - 1)
9125 break;
9126 *d++ = c;
9127 }
9128 else
9129#endif
9130 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009131
9132#ifdef FEAT_MBYTE
9133 if (has_mbyte)
9134 {
9135 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
9136 break;
9137 d += mb_char2bytes(c, d);
9138 }
9139 else
9140#endif
9141 {
9142 if (d - wcopy >= MAXWLEN - 1)
9143 break;
9144 *d++ = c;
9145 }
9146 }
9147 *d = NUL;
9148}
9149
9150/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00009151 * Try finding suggestions by recognizing specific situations.
9152 */
9153 static void
9154suggest_try_special(su)
9155 suginfo_T *su;
9156{
9157 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009158 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009159 int c;
9160 char_u word[MAXWLEN];
9161
9162 /*
9163 * Recognize a word that is repeated: "the the".
9164 */
9165 p = skiptowhite(su->su_fbadword);
9166 len = p - su->su_fbadword;
9167 p = skipwhite(p);
9168 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
9169 {
9170 /* Include badflags: if the badword is onecap or allcap
9171 * use that for the goodword too: "The the" -> "The". */
9172 c = su->su_fbadword[len];
9173 su->su_fbadword[len] = NUL;
9174 make_case_word(su->su_fbadword, word, su->su_badflags);
9175 su->su_fbadword[len] = c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00009176 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL,
9177 0, TRUE, su->su_sallang);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009178 }
9179}
9180
9181/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009182 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00009183 *
9184 * This uses a state machine. At each node in the tree we try various
9185 * operations. When trying if an operation work "depth" is increased and the
9186 * stack[] is used to store info. This allows combinations, thus insert one
9187 * character, replace one and delete another. The number of changes is
9188 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaard12a1322005-08-21 22:08:24 +00009189 *
9190 * After implementing this I noticed an article by Kemal Oflazer that
9191 * describes something similar: "Error-tolerant Finite State Recognition with
9192 * Applications to Morphological Analysis and Spelling Correction" (1996).
9193 * The implementation in the article is simplified and requires a stack of
9194 * unknown depth. The implementation here only needs a stack depth of the
9195 * length of the word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009196 */
9197 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00009198suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009199 suginfo_T *su;
9200{
9201 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
9202 char_u tword[MAXWLEN]; /* good word collected so far */
9203 trystate_T stack[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009204 char_u preword[MAXWLEN * 3]; /* word found with proper case;
9205 * concatanation of prefix compound
9206 * words and split word. NUL terminated
9207 * when going deeper but not when coming
9208 * back. */
9209 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009210 trystate_T *sp;
9211 int newscore;
9212 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009213 char_u *byts, *fbyts, *pbyts;
9214 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009215 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009216 int c, c2, c3;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009217 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009218 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009219 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009220 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009221 int len;
9222 char_u *p;
9223 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00009224 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009225 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009226 slang_T *slang;
9227 int fword_ends;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009228 int lpi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009229
9230 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00009231 * to find matches (esp. REP items). Append some more text, changing
9232 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009233 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009234 n = STRLEN(fword);
9235 p = su->su_badptr + su->su_badlen;
9236 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009237
Bram Moolenaar8b96d642005-09-05 22:05:30 +00009238 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009239 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00009240 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009241 slang = lp->lp_slang;
9242
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009243 /* If reloading a spell file fails it's still in the list but
9244 * everything has been cleared. */
9245 if (slang->sl_fbyts == NULL)
9246 continue;
9247
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009248 /*
9249 * Go through the whole case-fold tree, try changes at each node.
9250 * "tword[]" contains the word collected from nodes in the tree.
9251 * "fword[]" the word we are trying to match with (initially the bad
9252 * word).
9253 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009254 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009255 sp = &stack[0];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009256 vim_memset(sp, 0, sizeof(trystate_T));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009257 sp->ts_curi = 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009258
Bram Moolenaarea424162005-06-16 21:51:00 +00009259 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009260 * When there are postponed prefixes we need to use these first. At
9261 * the end of the prefix we continue in the case-fold tree.
9262 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009263 fbyts = slang->sl_fbyts;
9264 fidxs = slang->sl_fidxs;
9265 pbyts = slang->sl_pbyts;
9266 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009267 if (pbyts != NULL)
9268 {
9269 byts = pbyts;
9270 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009271 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009272 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
9273 }
9274 else
9275 {
9276 byts = fbyts;
9277 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009278 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009279 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009280 }
9281
9282 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00009283 * Loop to find all suggestions. At each round we either:
9284 * - For the current state try one operation, advance "ts_curi",
9285 * increase "depth".
9286 * - When a state is done go to the next, set "ts_state".
9287 * - When all states are tried decrease "depth".
9288 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009289 while (depth >= 0 && !got_int)
9290 {
9291 sp = &stack[depth];
9292 switch (sp->ts_state)
9293 {
9294 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009295 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009296 /*
9297 * Start of node: Deal with NUL bytes, which means
9298 * tword[] may end here.
9299 */
9300 arridx = sp->ts_arridx; /* current node in the tree */
9301 len = byts[arridx]; /* bytes in this node */
9302 arridx += sp->ts_curi; /* index of current byte */
9303
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009304 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009305 {
9306 /* Skip over the NUL bytes, we use them later. */
9307 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
9308 ;
9309 sp->ts_curi += n;
9310
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009311 /* Always past NUL bytes now. */
9312 n = (int)sp->ts_state;
9313 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00009314 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009315
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009316 /* At end of a prefix or at start of prefixtree: check for
9317 * following word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009318 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009319 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00009320 /* Set su->su_badflags to the caps type at this
9321 * position. Use the caps type until here for the
9322 * prefix itself. */
9323#ifdef FEAT_MBYTE
9324 if (has_mbyte)
9325 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
9326 else
9327#endif
9328 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009329 flags = badword_captype(su->su_badptr,
9330 su->su_badptr + n);
9331 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00009332 su->su_badptr + su->su_badlen);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009333 ++depth;
9334 stack[depth] = stack[depth - 1];
9335 sp = &stack[depth];
9336 sp->ts_prefixdepth = depth - 1;
9337 byts = fbyts;
9338 idxs = fidxs;
9339 sp->ts_state = STATE_START;
9340 sp->ts_curi = 1; /* start just after length byte */
9341 sp->ts_arridx = 0;
9342
Bram Moolenaar53805d12005-08-01 07:08:33 +00009343 /* Move the prefix to preword[] with the right case
9344 * and make find_keepcap_word() works. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00009345 tword[sp->ts_twordlen] = NUL;
9346 make_case_word(tword + sp->ts_splitoff,
9347 preword + sp->ts_prewordlen,
9348 flags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009349 sp->ts_prewordlen = STRLEN(preword);
Bram Moolenaard12a1322005-08-21 22:08:24 +00009350 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009351 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009352 break;
9353 }
9354
Bram Moolenaar0c405862005-06-22 22:26:26 +00009355 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009356 {
9357 /* Past bytes in node and/or past NUL bytes. */
9358 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00009359 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009360 break;
9361 }
9362
9363 /*
9364 * End of word in tree.
9365 */
9366 ++sp->ts_curi; /* eat one NUL byte */
9367
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009368 flags = (int)idxs[arridx];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009369 fword_ends = (fword[sp->ts_fidx] == NUL
9370 || !spell_iswordp(fword + sp->ts_fidx, curbuf));
9371 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009372
Bram Moolenaard12a1322005-08-21 22:08:24 +00009373 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
9374 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009375 {
9376 /* There was a prefix before the word. Check that the
9377 * prefix can be used with this word. */
9378 /* Count the length of the NULs in the prefix. If there
9379 * are none this must be the first try without a prefix.
9380 */
9381 n = stack[sp->ts_prefixdepth].ts_arridx;
9382 len = pbyts[n++];
9383 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
9384 ;
9385 if (c > 0)
9386 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009387 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00009388 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009389 if (c == 0)
9390 break;
9391
9392 /* Use the WF_RARE flag for a rare prefix. */
9393 if (c & WF_RAREPFX)
9394 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009395
9396 /* Tricky: when checking for both prefix and
9397 * compounding we run into the prefix flag first.
9398 * Remember that it's OK, so that we accept the prefix
9399 * when arriving at a compound flag. */
9400 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009401 }
9402 }
9403
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009404 /* Check NEEDCOMPOUND: can't use word without compounding. */
9405 if (sp->ts_complen == sp->ts_compsplit && fword_ends
9406 && (flags & WF_NEEDCOMP))
9407 break;
9408
Bram Moolenaard12a1322005-08-21 22:08:24 +00009409 if (sp->ts_complen > sp->ts_compsplit)
9410 {
Bram Moolenaar78622822005-08-23 21:00:13 +00009411 if (slang->sl_nobreak)
9412 {
9413 /* There was a word before this word. When there was
9414 * no change in this word (it was correct) add the
9415 * first word as a suggestion. If this word was
9416 * corrected too, we need to check if a correct word
9417 * follows. */
9418 if (sp->ts_fidx - sp->ts_splitfidx
9419 == sp->ts_twordlen - sp->ts_splitoff
9420 && STRNCMP(fword + sp->ts_splitfidx,
9421 tword + sp->ts_splitoff,
9422 sp->ts_fidx - sp->ts_splitfidx) == 0)
9423 {
9424 preword[sp->ts_prewordlen] = NUL;
9425 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00009426 sp->ts_splitfidx - repextra,
9427 sp->ts_score, 0, FALSE,
9428 lp->lp_sallang);
Bram Moolenaar78622822005-08-23 21:00:13 +00009429 break;
9430 }
9431 }
9432 else
9433 {
9434 /* There was a compound word before this word. If
9435 * this word does not support compounding then give up
9436 * (splitting is tried for the word without compound
9437 * flag). */
9438 if (((unsigned)flags >> 24) == 0
9439 || sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaard12a1322005-08-21 22:08:24 +00009440 < slang->sl_compminlen)
Bram Moolenaar78622822005-08-23 21:00:13 +00009441 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009442#ifdef FEAT_MBYTE
9443 /* For multi-byte chars check character length against
9444 * COMPOUNDMIN. */
9445 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009446 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009447 && mb_charlen(tword + sp->ts_splitoff)
9448 < slang->sl_compminlen)
9449 break;
9450#endif
9451
Bram Moolenaar78622822005-08-23 21:00:13 +00009452 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
9453 compflags[sp->ts_complen + 1] = NUL;
9454 vim_strncpy(preword + sp->ts_prewordlen,
9455 tword + sp->ts_splitoff,
9456 sp->ts_twordlen - sp->ts_splitoff);
9457 p = preword;
9458 while (*skiptowhite(p) != NUL)
9459 p = skipwhite(skiptowhite(p));
9460 if (fword_ends && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00009461 compflags + sp->ts_compsplit))
Bram Moolenaar78622822005-08-23 21:00:13 +00009462 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00009463
Bram Moolenaar78622822005-08-23 21:00:13 +00009464 /* Get pointer to last char of previous word. */
9465 p = preword + sp->ts_prewordlen;
9466 mb_ptr_back(preword, p);
9467 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00009468 }
Bram Moolenaare52325c2005-08-22 22:54:29 +00009469 else
9470 p = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009471
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009472 /*
9473 * Form the word with proper case in preword.
9474 * If there is a word from a previous split, append.
9475 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009476 if (flags & WF_KEEPCAP)
9477 /* Must find the word in the keep-case tree. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009478 find_keepcap_word(slang, tword + sp->ts_splitoff,
9479 preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009480 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00009481 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009482 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00009483 * use that for the goodword too. But if the badword is
9484 * allcap and it's only one char long use onecap. */
9485 c = su->su_badflags;
9486 if ((c & WF_ALLCAP)
9487#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009488 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
Bram Moolenaar0c405862005-06-22 22:26:26 +00009489#else
9490 && su->su_badlen == 1
9491#endif
9492 )
9493 c = WF_ONECAP;
Bram Moolenaare52325c2005-08-22 22:54:29 +00009494 c |= flags;
9495
9496 /* When appending a compound word after a word character
9497 * don't use Onecap. */
9498 if (p != NULL && spell_iswordp_nmw(p))
9499 c &= ~WF_ONECAP;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009500 make_case_word(tword + sp->ts_splitoff,
Bram Moolenaare52325c2005-08-22 22:54:29 +00009501 preword + sp->ts_prewordlen, c);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009502 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009503
9504 /* Don't use a banned word. It may appear again as a good
9505 * word, thus remember it. */
9506 if (flags & WF_BANNED)
9507 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00009508 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009509 break;
9510 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009511 if (was_banned(su, preword + sp->ts_prewordlen)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009512 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009513 break;
9514
9515 newscore = 0;
9516 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009517 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009518 newscore += SCORE_REGION;
9519 if (flags & WF_RARE)
9520 newscore += SCORE_RARE;
9521
Bram Moolenaar0c405862005-06-22 22:26:26 +00009522 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00009523 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009524 newscore += SCORE_ICASE;
9525
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009526 if (fword_ends && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009527 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009528 /* The badword also ends: add suggestions. Give a penalty
9529 * when changing non-word char to word char, e.g., "thes,"
9530 * -> "these". */
9531 p = fword + sp->ts_fidx;
9532#ifdef FEAT_MBYTE
9533 if (has_mbyte)
9534 mb_ptr_back(fword, p);
9535 else
9536#endif
9537 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009538 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009539 {
9540 p = preword + STRLEN(preword);
9541#ifdef FEAT_MBYTE
9542 if (has_mbyte)
9543 mb_ptr_back(preword, p);
9544 else
9545#endif
9546 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009547 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009548 newscore += SCORE_NONWORD;
9549 }
9550
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009551 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00009552 sp->ts_fidx - repextra,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00009553 sp->ts_score + newscore, 0, FALSE,
9554 lp->lp_sallang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009555 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009556 else if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +00009557#ifdef FEAT_MBYTE
9558 /* Don't split halfway a character. */
9559 && (!has_mbyte || sp->ts_tcharlen == 0)
9560#endif
9561 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009562 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009563 int try_compound;
9564
9565 /* Get here in two situations:
9566 * 1. The word in the tree ends but the badword continues:
9567 * If the word allows compounding try that. Otherwise
9568 * try a split by inserting a space. For both check
9569 * that a valid words starts at fword[sp->ts_fidx].
Bram Moolenaar78622822005-08-23 21:00:13 +00009570 * For NOBREAK do like compounding to be able to check
9571 * if the next word is valid.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009572 * 2. The badword does end, but it was due to a change
9573 * (e.g., a swap). No need to split, but do check that
9574 * the following word is valid.
9575 */
Bram Moolenaard12a1322005-08-21 22:08:24 +00009576 try_compound = FALSE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009577 if (!fword_ends
Bram Moolenaar6de68532005-08-24 22:08:48 +00009578 && slang->sl_compprog != NULL
Bram Moolenaar5195e452005-08-19 20:32:47 +00009579 && ((unsigned)flags >> 24) != 0
9580 && sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009581 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009582#ifdef FEAT_MBYTE
9583 && (!has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009584 || slang->sl_compminlen == 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009585 || mb_charlen(tword + sp->ts_splitoff)
9586 >= slang->sl_compminlen)
9587#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00009588 && (slang->sl_compsylmax < MAXWLEN
9589 || sp->ts_complen + 1 - sp->ts_compsplit
9590 < slang->sl_compmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00009591 && (byte_in_str(sp->ts_complen == sp->ts_compsplit
Bram Moolenaard12a1322005-08-21 22:08:24 +00009592 ? slang->sl_compstartflags
9593 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00009594 ((unsigned)flags >> 24))))
Bram Moolenaar5195e452005-08-19 20:32:47 +00009595 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009596 try_compound = TRUE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009597 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
9598 compflags[sp->ts_complen + 1] = NUL;
9599 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00009600
Bram Moolenaar78622822005-08-23 21:00:13 +00009601 /* For NOBREAK we never try splitting, it won't make any
9602 * word valid. */
9603 if (slang->sl_nobreak)
9604 try_compound = TRUE;
9605
Bram Moolenaard12a1322005-08-21 22:08:24 +00009606 /* If we could add a compound word, and it's also possible
9607 * to split at this point, do the split first and set
9608 * TSF_DIDSPLIT to avoid doing it again. */
Bram Moolenaar78622822005-08-23 21:00:13 +00009609 else if (!fword_ends
Bram Moolenaard12a1322005-08-21 22:08:24 +00009610 && try_compound
9611 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009612 {
9613 try_compound = FALSE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009614 sp->ts_flags |= TSF_DIDSPLIT;
9615 --sp->ts_curi; /* do the same NUL again */
9616 compflags[sp->ts_complen] = NUL;
9617 }
9618 else
9619 sp->ts_flags &= ~TSF_DIDSPLIT;
9620
9621 if (!try_compound && !fword_ends)
9622 {
9623 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009624 * words so far are valid for compounding. If there
9625 * is only one word it must not have the NEEDCOMPOUND
9626 * flag. */
9627 if (sp->ts_complen == sp->ts_compsplit
9628 && (flags & WF_NEEDCOMP))
9629 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00009630 p = preword;
9631 while (*skiptowhite(p) != NUL)
9632 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +00009633 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +00009634 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00009635 compflags + sp->ts_compsplit))
9636 break;
9637 newscore += SCORE_SPLIT;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009638 }
9639
9640 if (try_deeper(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009641 {
9642 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009643 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009644 sp->ts_state = STATE_SPLITUNDO;
9645
9646 ++depth;
9647 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009648
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009649 /* Append a space to preword when splitting. */
9650 if (!try_compound && !fword_ends)
9651 STRCAT(preword, " ");
Bram Moolenaar5195e452005-08-19 20:32:47 +00009652 sp->ts_prewordlen = STRLEN(preword);
9653 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +00009654 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009655
9656 /* If the badword has a non-word character at this
9657 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009658 * non-word character with a space. Always skip a
9659 * character when the word ends. */
9660 if ((!try_compound
9661 && !spell_iswordp_nmw(fword + sp->ts_fidx))
9662 || fword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009663 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009664 int l;
9665
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009666#ifdef FEAT_MBYTE
9667 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009668 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009669 else
9670#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009671 l = 1;
9672 if (fword_ends)
9673 {
9674 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009675 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009676 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009677 sp->ts_prewordlen += l;
9678 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009679 }
9680 else
9681 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
9682 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009683 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00009684
Bram Moolenaard12a1322005-08-21 22:08:24 +00009685 /* When compounding include compound flag in
9686 * compflags[] (already set above). When splitting we
9687 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009688 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +00009689 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009690 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00009691 sp->ts_compsplit = sp->ts_complen;
9692 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009693
Bram Moolenaar53805d12005-08-01 07:08:33 +00009694 /* set su->su_badflags to the caps type at this
9695 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009696#ifdef FEAT_MBYTE
9697 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00009698 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009699 else
9700#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00009701 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009702 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00009703 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009704
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009705 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009706 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009707
9708 /* If there are postponed prefixes, try these too. */
9709 if (pbyts != NULL)
9710 {
9711 byts = pbyts;
9712 idxs = pidxs;
9713 sp->ts_prefixdepth = PFD_PREFIXTREE;
9714 sp->ts_state = STATE_NOPREFIX;
9715 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009716 }
9717 }
9718 break;
9719
9720 case STATE_SPLITUNDO:
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009721 /* Undo the changes done for word split or compound word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009722 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009723
9724 /* Continue looking for NUL bytes. */
9725 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009726
9727 /* In case we went into the prefix tree. */
9728 byts = fbyts;
9729 idxs = fidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009730 break;
9731
9732 case STATE_ENDNUL:
9733 /* Past the NUL bytes in the node. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00009734 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009735 if (fword[sp->ts_fidx] == NUL
9736#ifdef FEAT_MBYTE
9737 && sp->ts_tcharlen == 0
9738#endif
9739 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009740 {
9741 /* The badword ends, can't use the bytes in this node. */
9742 sp->ts_state = STATE_DEL;
9743 break;
9744 }
9745 sp->ts_state = STATE_PLAIN;
9746 /*FALLTHROUGH*/
9747
9748 case STATE_PLAIN:
9749 /*
9750 * Go over all possible bytes at this node, add each to
9751 * tword[] and use child node. "ts_curi" is the index.
9752 */
9753 arridx = sp->ts_arridx;
9754 if (sp->ts_curi > byts[arridx])
9755 {
9756 /* Done all bytes at this node, do next state. When still
9757 * at already changed bytes skip the other tricks. */
9758 if (sp->ts_fidx >= sp->ts_fidxtry)
9759 sp->ts_state = STATE_DEL;
9760 else
9761 sp->ts_state = STATE_FINAL;
9762 }
9763 else
9764 {
9765 arridx += sp->ts_curi++;
9766 c = byts[arridx];
9767
9768 /* Normal byte, go one level deeper. If it's not equal to
9769 * the byte in the bad word adjust the score. But don't
9770 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00009771 if (c == fword[sp->ts_fidx]
9772#ifdef FEAT_MBYTE
9773 || (sp->ts_tcharlen > 0
9774 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009775#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00009776 )
9777 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009778 else
9779 newscore = SCORE_SUBST;
9780 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
9781 && try_deeper(su, stack, depth, newscore))
9782 {
9783 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009784 sp = &stack[depth];
9785 ++sp->ts_fidx;
9786 tword[sp->ts_twordlen++] = c;
9787 sp->ts_arridx = idxs[arridx];
9788#ifdef FEAT_MBYTE
9789 if (newscore == SCORE_SUBST)
9790 sp->ts_isdiff = DIFF_YES;
9791 if (has_mbyte)
9792 {
9793 /* Multi-byte characters are a bit complicated to
9794 * handle: They differ when any of the bytes
9795 * differ and then their length may also differ. */
9796 if (sp->ts_tcharlen == 0)
9797 {
9798 /* First byte. */
9799 sp->ts_tcharidx = 0;
9800 sp->ts_tcharlen = MB_BYTE2LEN(c);
9801 sp->ts_fcharstart = sp->ts_fidx - 1;
9802 sp->ts_isdiff = (newscore != 0)
9803 ? DIFF_YES : DIFF_NONE;
9804 }
9805 else if (sp->ts_isdiff == DIFF_INSERT)
9806 /* When inserting trail bytes don't advance in
9807 * the bad word. */
9808 --sp->ts_fidx;
9809 if (++sp->ts_tcharidx == sp->ts_tcharlen)
9810 {
9811 /* Last byte of character. */
9812 if (sp->ts_isdiff == DIFF_YES)
9813 {
9814 /* Correct ts_fidx for the byte length of
9815 * the character (we didn't check that
9816 * before). */
9817 sp->ts_fidx = sp->ts_fcharstart
9818 + MB_BYTE2LEN(
9819 fword[sp->ts_fcharstart]);
9820
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00009821 /* For changing a composing character
9822 * adjust the score from SCORE_SUBST to
9823 * SCORE_SUBCOMP. */
9824 if (enc_utf8
9825 && utf_iscomposing(
9826 mb_ptr2char(tword
9827 + sp->ts_twordlen
9828 - sp->ts_tcharlen))
9829 && utf_iscomposing(
9830 mb_ptr2char(fword
9831 + sp->ts_fcharstart)))
9832 sp->ts_score -=
9833 SCORE_SUBST - SCORE_SUBCOMP;
9834
Bram Moolenaarea424162005-06-16 21:51:00 +00009835 /* For a similar character adjust score
9836 * from SCORE_SUBST to SCORE_SIMILAR. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009837 else if (slang->sl_has_map
9838 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00009839 mb_ptr2char(tword
9840 + sp->ts_twordlen
9841 - sp->ts_tcharlen),
9842 mb_ptr2char(fword
9843 + sp->ts_fcharstart)))
9844 sp->ts_score -=
9845 SCORE_SUBST - SCORE_SIMILAR;
9846 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009847 else if (sp->ts_isdiff == DIFF_INSERT
9848 && sp->ts_twordlen > sp->ts_tcharlen)
9849 {
Bram Moolenaarea408852005-06-25 22:49:46 +00009850 p = tword + sp->ts_twordlen
9851 - sp->ts_tcharlen;
9852 c = mb_ptr2char(p);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009853 if (enc_utf8 && utf_iscomposing(c))
9854 {
9855 /* Inserting a composing char doesn't
9856 * count that much. */
Bram Moolenaarea408852005-06-25 22:49:46 +00009857 sp->ts_score -= SCORE_INS
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009858 - SCORE_INSCOMP;
9859 }
9860 else
9861 {
9862 /* If the previous character was the
9863 * same, thus doubling a character,
9864 * give a bonus to the score. */
9865 mb_ptr_back(tword, p);
9866 if (c == mb_ptr2char(p))
9867 sp->ts_score -= SCORE_INS
Bram Moolenaarea408852005-06-25 22:49:46 +00009868 - SCORE_INSDUP;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009869 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009870 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009871
9872 /* Starting a new char, reset the length. */
9873 sp->ts_tcharlen = 0;
9874 }
9875 }
9876 else
9877#endif
9878 {
9879 /* If we found a similar char adjust the score.
9880 * We do this after calling try_deeper() because
9881 * it's slow. */
9882 if (newscore != 0
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009883 && slang->sl_has_map
9884 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00009885 c, fword[sp->ts_fidx - 1]))
9886 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
9887 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009888 }
9889 }
9890 break;
9891
9892 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00009893#ifdef FEAT_MBYTE
9894 /* When past the first byte of a multi-byte char don't try
9895 * delete/insert/swap a character. */
9896 if (has_mbyte && sp->ts_tcharlen > 0)
9897 {
9898 sp->ts_state = STATE_FINAL;
9899 break;
9900 }
9901#endif
9902 /*
9903 * Try skipping one character in the bad word (delete it).
9904 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009905 sp->ts_state = STATE_INS;
9906 sp->ts_curi = 1;
9907 if (fword[sp->ts_fidx] != NUL
9908 && try_deeper(su, stack, depth, SCORE_DEL))
9909 {
9910 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00009911
9912 /* Advance over the character in fword[]. Give a bonus to
9913 * the score if the same character is following "nn" ->
9914 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00009915#ifdef FEAT_MBYTE
9916 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00009917 {
9918 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00009919 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00009920 if (enc_utf8 && utf_iscomposing(c))
9921 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
9922 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
Bram Moolenaarea408852005-06-25 22:49:46 +00009923 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
9924 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009925 else
9926#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00009927 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009928 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00009929 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
9930 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
9931 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009932 break;
9933 }
9934 /*FALLTHROUGH*/
9935
9936 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00009937 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009938 * node. */
9939 n = sp->ts_arridx;
9940 if (sp->ts_curi > byts[n])
9941 {
9942 /* Done all bytes at this node, do next state. */
9943 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009944 }
9945 else
9946 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009947 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009948 n += sp->ts_curi++;
9949 c = byts[n];
9950 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
9951 {
9952 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009953 sp = &stack[depth];
9954 tword[sp->ts_twordlen++] = c;
9955 sp->ts_arridx = idxs[n];
9956#ifdef FEAT_MBYTE
9957 if (has_mbyte)
9958 {
9959 fl = MB_BYTE2LEN(c);
9960 if (fl > 1)
9961 {
9962 /* There are following bytes for the same
9963 * character. We must find all bytes before
9964 * trying delete/insert/swap/etc. */
9965 sp->ts_tcharlen = fl;
9966 sp->ts_tcharidx = 1;
9967 sp->ts_isdiff = DIFF_INSERT;
9968 }
9969 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009970 else
9971 fl = 1;
9972 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00009973#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00009974 {
9975 /* If the previous character was the same, thus
9976 * doubling a character, give a bonus to the
9977 * score. */
9978 if (sp->ts_twordlen >= 2
9979 && tword[sp->ts_twordlen - 2] == c)
9980 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
9981 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009982 }
9983 }
9984 break;
9985
9986 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00009987 /*
9988 * Swap two bytes in the bad word: "12" -> "21".
9989 * We change "fword" here, it's changed back afterwards.
9990 */
9991 p = fword + sp->ts_fidx;
9992 c = *p;
9993 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009994 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009995 /* End of word, can't swap or replace. */
9996 sp->ts_state = STATE_FINAL;
9997 break;
9998 }
9999#ifdef FEAT_MBYTE
10000 if (has_mbyte)
10001 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010002 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000010003 c = mb_ptr2char(p);
10004 c2 = mb_ptr2char(p + n);
10005 }
10006 else
10007#endif
10008 c2 = p[1];
10009 if (c == c2)
10010 {
10011 /* Characters are identical, swap won't do anything. */
10012 sp->ts_state = STATE_SWAP3;
10013 break;
10014 }
10015 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
10016 {
10017 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010018 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000010019#ifdef FEAT_MBYTE
10020 if (has_mbyte)
10021 {
10022 fl = mb_char2len(c2);
10023 mch_memmove(p, p + n, fl);
10024 mb_char2bytes(c, p + fl);
10025 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
10026 }
10027 else
10028#endif
10029 {
10030 p[0] = c2;
10031 p[1] = c;
10032 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
10033 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010034 }
10035 else
10036 /* If this swap doesn't work then SWAP3 won't either. */
10037 sp->ts_state = STATE_REP_INI;
10038 break;
10039
Bram Moolenaarea424162005-06-16 21:51:00 +000010040 case STATE_UNSWAP:
10041 /* Undo the STATE_SWAP swap: "21" -> "12". */
10042 p = fword + sp->ts_fidx;
10043#ifdef FEAT_MBYTE
10044 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010045 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010046 n = MB_BYTE2LEN(*p);
10047 c = mb_ptr2char(p + n);
10048 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
10049 mb_char2bytes(c, p);
10050 }
10051 else
10052#endif
10053 {
10054 c = *p;
10055 *p = p[1];
10056 p[1] = c;
10057 }
10058 /*FALLTHROUGH*/
10059
10060 case STATE_SWAP3:
10061 /* Swap two bytes, skipping one: "123" -> "321". We change
10062 * "fword" here, it's changed back afterwards. */
10063 p = fword + sp->ts_fidx;
10064#ifdef FEAT_MBYTE
10065 if (has_mbyte)
10066 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010067 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000010068 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010069 fl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +000010070 c2 = mb_ptr2char(p + n);
10071 c3 = mb_ptr2char(p + n + fl);
10072 }
10073 else
10074#endif
10075 {
10076 c = *p;
10077 c2 = p[1];
10078 c3 = p[2];
10079 }
10080
10081 /* When characters are identical: "121" then SWAP3 result is
10082 * identical, ROT3L result is same as SWAP: "211", ROT3L
10083 * result is same as SWAP on next char: "112". Thus skip all
10084 * swapping. Also skip when c3 is NUL. */
10085 if (c == c3 || c3 == NUL)
10086 {
10087 sp->ts_state = STATE_REP_INI;
10088 break;
10089 }
10090 if (try_deeper(su, stack, depth, SCORE_SWAP3))
10091 {
10092 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010093 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000010094#ifdef FEAT_MBYTE
10095 if (has_mbyte)
10096 {
10097 tl = mb_char2len(c3);
10098 mch_memmove(p, p + n + fl, tl);
10099 mb_char2bytes(c2, p + tl);
10100 mb_char2bytes(c, p + fl + tl);
10101 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
10102 }
10103 else
10104#endif
10105 {
10106 p[0] = p[2];
10107 p[2] = c;
10108 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
10109 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010110 }
10111 else
10112 sp->ts_state = STATE_REP_INI;
10113 break;
10114
Bram Moolenaarea424162005-06-16 21:51:00 +000010115 case STATE_UNSWAP3:
10116 /* Undo STATE_SWAP3: "321" -> "123" */
10117 p = fword + sp->ts_fidx;
10118#ifdef FEAT_MBYTE
10119 if (has_mbyte)
10120 {
10121 n = MB_BYTE2LEN(*p);
10122 c2 = mb_ptr2char(p + n);
10123 fl = MB_BYTE2LEN(p[n]);
10124 c = mb_ptr2char(p + n + fl);
10125 tl = MB_BYTE2LEN(p[n + fl]);
10126 mch_memmove(p + fl + tl, p, n);
10127 mb_char2bytes(c, p);
10128 mb_char2bytes(c2, p + tl);
10129 }
10130 else
10131#endif
10132 {
10133 c = *p;
10134 *p = p[2];
10135 p[2] = c;
10136 }
Bram Moolenaarea424162005-06-16 21:51:00 +000010137
Bram Moolenaarea424162005-06-16 21:51:00 +000010138 /* Rotate three characters left: "123" -> "231". We change
10139 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010140 if (try_deeper(su, stack, depth, SCORE_SWAP3))
10141 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010142 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010143 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000010144 p = fword + sp->ts_fidx;
10145#ifdef FEAT_MBYTE
10146 if (has_mbyte)
10147 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010148 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000010149 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010150 fl = mb_cptr2len(p + n);
10151 fl += mb_cptr2len(p + n + fl);
Bram Moolenaarea424162005-06-16 21:51:00 +000010152 mch_memmove(p, p + n, fl);
10153 mb_char2bytes(c, p + fl);
10154 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
10155 }
10156 else
10157#endif
10158 {
10159 c = *p;
10160 *p = p[1];
10161 p[1] = p[2];
10162 p[2] = c;
10163 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
10164 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010165 }
10166 else
10167 sp->ts_state = STATE_REP_INI;
10168 break;
10169
Bram Moolenaarea424162005-06-16 21:51:00 +000010170 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +000010171 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +000010172 p = fword + sp->ts_fidx;
10173#ifdef FEAT_MBYTE
10174 if (has_mbyte)
10175 {
10176 n = MB_BYTE2LEN(*p);
10177 n += MB_BYTE2LEN(p[n]);
10178 c = mb_ptr2char(p + n);
10179 tl = MB_BYTE2LEN(p[n]);
10180 mch_memmove(p + tl, p, n);
10181 mb_char2bytes(c, p);
10182 }
10183 else
10184#endif
10185 {
10186 c = p[2];
10187 p[2] = p[1];
10188 p[1] = *p;
10189 *p = c;
10190 }
Bram Moolenaarea424162005-06-16 21:51:00 +000010191
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010192 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +000010193 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010194 if (try_deeper(su, stack, depth, SCORE_SWAP3))
10195 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010196 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010197 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000010198 p = fword + sp->ts_fidx;
10199#ifdef FEAT_MBYTE
10200 if (has_mbyte)
10201 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010202 n = mb_cptr2len(p);
10203 n += mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +000010204 c = mb_ptr2char(p + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010205 tl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +000010206 mch_memmove(p + tl, p, n);
10207 mb_char2bytes(c, p);
10208 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
10209 }
10210 else
10211#endif
10212 {
10213 c = p[2];
10214 p[2] = p[1];
10215 p[1] = *p;
10216 *p = c;
10217 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
10218 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010219 }
10220 else
10221 sp->ts_state = STATE_REP_INI;
10222 break;
10223
Bram Moolenaarea424162005-06-16 21:51:00 +000010224 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +000010225 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +000010226 p = fword + sp->ts_fidx;
10227#ifdef FEAT_MBYTE
10228 if (has_mbyte)
10229 {
10230 c = mb_ptr2char(p);
10231 tl = MB_BYTE2LEN(*p);
10232 n = MB_BYTE2LEN(p[tl]);
10233 n += MB_BYTE2LEN(p[tl + n]);
10234 mch_memmove(p, p + tl, n);
10235 mb_char2bytes(c, p + n);
10236 }
10237 else
10238#endif
10239 {
10240 c = *p;
10241 *p = p[1];
10242 p[1] = p[2];
10243 p[2] = c;
10244 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010245 /*FALLTHROUGH*/
10246
10247 case STATE_REP_INI:
10248 /* Check if matching with REP items from the .aff file would
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010249 * work. Quickly skip if:
10250 * - there are no REP items
10251 * - the score is going to be too high anyway
10252 * - already applied a REP item or swapped here */
10253 if (lp->lp_replang == NULL
10254 || sp->ts_score + SCORE_REP >= su->su_maxscore
10255 || sp->ts_fidx < sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010256 {
10257 sp->ts_state = STATE_FINAL;
10258 break;
10259 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010260 gap = &lp->lp_replang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010261
10262 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +000010263 * may match. If the index is -1 there is none. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010264 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010265 if (sp->ts_curi < 0)
10266 {
10267 sp->ts_state = STATE_FINAL;
10268 break;
10269 }
10270
10271 sp->ts_state = STATE_REP;
10272 /*FALLTHROUGH*/
10273
10274 case STATE_REP:
10275 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +000010276 * match replace the characters and check if the resulting
10277 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010278 p = fword + sp->ts_fidx;
10279
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010280 gap = &lp->lp_replang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010281 while (sp->ts_curi < gap->ga_len)
10282 {
10283 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
10284 if (*ftp->ft_from != *p)
10285 {
10286 /* past possible matching entries */
10287 sp->ts_curi = gap->ga_len;
10288 break;
10289 }
10290 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
10291 && try_deeper(su, stack, depth, SCORE_REP))
10292 {
10293 /* Need to undo this afterwards. */
10294 sp->ts_state = STATE_REP_UNDO;
10295
10296 /* Change the "from" to the "to" string. */
10297 ++depth;
10298 fl = STRLEN(ftp->ft_from);
10299 tl = STRLEN(ftp->ft_to);
10300 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +000010301 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010302 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010303 repextra += tl - fl;
10304 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010305 mch_memmove(p, ftp->ft_to, tl);
10306 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +000010307#ifdef FEAT_MBYTE
10308 stack[depth].ts_tcharlen = 0;
10309#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010310 break;
10311 }
10312 }
10313
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010314 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010315 /* No (more) matches. */
10316 sp->ts_state = STATE_FINAL;
10317
10318 break;
10319
10320 case STATE_REP_UNDO:
10321 /* Undo a REP replacement and continue with the next one. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010322 ftp = (fromto_T *)lp->lp_replang->sl_rep.ga_data
10323 + sp->ts_curi - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010324 fl = STRLEN(ftp->ft_from);
10325 tl = STRLEN(ftp->ft_to);
10326 p = fword + sp->ts_fidx;
10327 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +000010328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010329 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010330 repextra -= tl - fl;
10331 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010332 mch_memmove(p, ftp->ft_from, fl);
10333 sp->ts_state = STATE_REP;
10334 break;
10335
10336 default:
10337 /* Did all possible states at this level, go up one level. */
10338 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010339
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010340 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010341 {
10342 /* Continue in or go back to the prefix tree. */
10343 byts = pbyts;
10344 idxs = pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010345 }
10346
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010347 /* Don't check for CTRL-C too often, it takes time. */
10348 line_breakcheck();
10349 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010350 }
10351 }
10352}
10353
10354/*
10355 * Try going one level deeper in the tree.
10356 */
10357 static int
10358try_deeper(su, stack, depth, score_add)
10359 suginfo_T *su;
10360 trystate_T *stack;
10361 int depth;
10362 int score_add;
10363{
10364 int newscore;
10365
10366 /* Refuse to go deeper if the scrore is getting too big. */
10367 newscore = stack[depth].ts_score + score_add;
10368 if (newscore >= su->su_maxscore)
10369 return FALSE;
10370
Bram Moolenaarea424162005-06-16 21:51:00 +000010371 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010372 stack[depth + 1].ts_state = STATE_START;
10373 stack[depth + 1].ts_score = newscore;
10374 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010375 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010376 return TRUE;
10377}
10378
Bram Moolenaar53805d12005-08-01 07:08:33 +000010379#ifdef FEAT_MBYTE
10380/*
10381 * Case-folding may change the number of bytes: Count nr of chars in
10382 * fword[flen] and return the byte length of that many chars in "word".
10383 */
10384 static int
10385nofold_len(fword, flen, word)
10386 char_u *fword;
10387 int flen;
10388 char_u *word;
10389{
10390 char_u *p;
10391 int i = 0;
10392
10393 for (p = fword; p < fword + flen; mb_ptr_adv(p))
10394 ++i;
10395 for (p = word; i > 0; mb_ptr_adv(p))
10396 --i;
10397 return (int)(p - word);
10398}
10399#endif
10400
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010401/*
10402 * "fword" is a good word with case folded. Find the matching keep-case
10403 * words and put it in "kword".
10404 * Theoretically there could be several keep-case words that result in the
10405 * same case-folded word, but we only find one...
10406 */
10407 static void
10408find_keepcap_word(slang, fword, kword)
10409 slang_T *slang;
10410 char_u *fword;
10411 char_u *kword;
10412{
10413 char_u uword[MAXWLEN]; /* "fword" in upper-case */
10414 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010415 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010416
10417 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010418 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010419 int round[MAXWLEN];
10420 int fwordidx[MAXWLEN];
10421 int uwordidx[MAXWLEN];
10422 int kwordlen[MAXWLEN];
10423
10424 int flen, ulen;
10425 int l;
10426 int len;
10427 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010428 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010429 char_u *p;
10430 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010431 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010432
10433 if (byts == NULL)
10434 {
10435 /* array is empty: "cannot happen" */
10436 *kword = NUL;
10437 return;
10438 }
10439
10440 /* Make an all-cap version of "fword". */
10441 allcap_copy(fword, uword);
10442
10443 /*
10444 * Each character needs to be tried both case-folded and upper-case.
10445 * All this gets very complicated if we keep in mind that changing case
10446 * may change the byte length of a multi-byte character...
10447 */
10448 depth = 0;
10449 arridx[0] = 0;
10450 round[0] = 0;
10451 fwordidx[0] = 0;
10452 uwordidx[0] = 0;
10453 kwordlen[0] = 0;
10454 while (depth >= 0)
10455 {
10456 if (fword[fwordidx[depth]] == NUL)
10457 {
10458 /* We are at the end of "fword". If the tree allows a word to end
10459 * here we have found a match. */
10460 if (byts[arridx[depth] + 1] == 0)
10461 {
10462 kword[kwordlen[depth]] = NUL;
10463 return;
10464 }
10465
10466 /* kword is getting too long, continue one level up */
10467 --depth;
10468 }
10469 else if (++round[depth] > 2)
10470 {
10471 /* tried both fold-case and upper-case character, continue one
10472 * level up */
10473 --depth;
10474 }
10475 else
10476 {
10477 /*
10478 * round[depth] == 1: Try using the folded-case character.
10479 * round[depth] == 2: Try using the upper-case character.
10480 */
10481#ifdef FEAT_MBYTE
10482 if (has_mbyte)
10483 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010484 flen = mb_cptr2len(fword + fwordidx[depth]);
10485 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010486 }
10487 else
10488#endif
10489 ulen = flen = 1;
10490 if (round[depth] == 1)
10491 {
10492 p = fword + fwordidx[depth];
10493 l = flen;
10494 }
10495 else
10496 {
10497 p = uword + uwordidx[depth];
10498 l = ulen;
10499 }
10500
10501 for (tryidx = arridx[depth]; l > 0; --l)
10502 {
10503 /* Perform a binary search in the list of accepted bytes. */
10504 len = byts[tryidx++];
10505 c = *p++;
10506 lo = tryidx;
10507 hi = tryidx + len - 1;
10508 while (lo < hi)
10509 {
10510 m = (lo + hi) / 2;
10511 if (byts[m] > c)
10512 hi = m - 1;
10513 else if (byts[m] < c)
10514 lo = m + 1;
10515 else
10516 {
10517 lo = hi = m;
10518 break;
10519 }
10520 }
10521
10522 /* Stop if there is no matching byte. */
10523 if (hi < lo || byts[lo] != c)
10524 break;
10525
10526 /* Continue at the child (if there is one). */
10527 tryidx = idxs[lo];
10528 }
10529
10530 if (l == 0)
10531 {
10532 /*
10533 * Found the matching char. Copy it to "kword" and go a
10534 * level deeper.
10535 */
10536 if (round[depth] == 1)
10537 {
10538 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
10539 flen);
10540 kwordlen[depth + 1] = kwordlen[depth] + flen;
10541 }
10542 else
10543 {
10544 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
10545 ulen);
10546 kwordlen[depth + 1] = kwordlen[depth] + ulen;
10547 }
10548 fwordidx[depth + 1] = fwordidx[depth] + flen;
10549 uwordidx[depth + 1] = uwordidx[depth] + ulen;
10550
10551 ++depth;
10552 arridx[depth] = tryidx;
10553 round[depth] = 0;
10554 }
10555 }
10556 }
10557
10558 /* Didn't find it: "cannot happen". */
10559 *kword = NUL;
10560}
10561
10562/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010563 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
10564 * su->su_sga.
10565 */
10566 static void
10567score_comp_sal(su)
10568 suginfo_T *su;
10569{
10570 langp_T *lp;
10571 char_u badsound[MAXWLEN];
10572 int i;
10573 suggest_T *stp;
10574 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010575 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010576 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010577
10578 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
10579 return;
10580
10581 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010582 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010583 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010584 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010585 if (lp->lp_slang->sl_sal.ga_len > 0)
10586 {
10587 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010588 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010589
10590 for (i = 0; i < su->su_ga.ga_len; ++i)
10591 {
10592 stp = &SUG(su->su_ga, i);
10593
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010594 /* Case-fold the suggested word, sound-fold it and compute the
10595 * sound-a-like score. */
10596 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010597 if (score < SCORE_MAXMAX)
10598 {
10599 /* Add the suggestion. */
10600 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
10601 sstp->st_word = vim_strsave(stp->st_word);
10602 if (sstp->st_word != NULL)
10603 {
10604 sstp->st_score = score;
10605 sstp->st_altscore = 0;
10606 sstp->st_orglen = stp->st_orglen;
10607 ++su->su_sga.ga_len;
10608 }
10609 }
10610 }
10611 break;
10612 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010613 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010614}
10615
10616/*
10617 * Combine the list of suggestions in su->su_ga and su->su_sga.
10618 * They are intwined.
10619 */
10620 static void
10621score_combine(su)
10622 suginfo_T *su;
10623{
10624 int i;
10625 int j;
10626 garray_T ga;
10627 garray_T *gap;
10628 langp_T *lp;
10629 suggest_T *stp;
10630 char_u *p;
10631 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010632 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010633 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010634
10635 /* Add the alternate score to su_ga. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010636 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010637 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010638 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010639 if (lp->lp_slang->sl_sal.ga_len > 0)
10640 {
10641 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010642 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010643
10644 for (i = 0; i < su->su_ga.ga_len; ++i)
10645 {
10646 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010647 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
10648 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010649 if (stp->st_altscore == SCORE_MAXMAX)
10650 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
10651 else
10652 stp->st_score = (stp->st_score * 3
10653 + stp->st_altscore) / 4;
10654 stp->st_salscore = FALSE;
10655 }
10656 break;
10657 }
10658 }
10659
10660 /* Add the alternate score to su_sga. */
10661 for (i = 0; i < su->su_sga.ga_len; ++i)
10662 {
10663 stp = &SUG(su->su_sga, i);
10664 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
10665 if (stp->st_score == SCORE_MAXMAX)
10666 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
10667 else
10668 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
10669 stp->st_salscore = TRUE;
10670 }
10671
10672 /* Sort the suggestions and truncate at "maxcount" for both lists. */
10673 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10674 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
10675
10676 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
10677 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
10678 return;
10679
10680 stp = &SUG(ga, 0);
10681 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
10682 {
10683 /* round 1: get a suggestion from su_ga
10684 * round 2: get a suggestion from su_sga */
10685 for (round = 1; round <= 2; ++round)
10686 {
10687 gap = round == 1 ? &su->su_ga : &su->su_sga;
10688 if (i < gap->ga_len)
10689 {
10690 /* Don't add a word if it's already there. */
10691 p = SUG(*gap, i).st_word;
10692 for (j = 0; j < ga.ga_len; ++j)
10693 if (STRCMP(stp[j].st_word, p) == 0)
10694 break;
10695 if (j == ga.ga_len)
10696 stp[ga.ga_len++] = SUG(*gap, i);
10697 else
10698 vim_free(p);
10699 }
10700 }
10701 }
10702
10703 ga_clear(&su->su_ga);
10704 ga_clear(&su->su_sga);
10705
10706 /* Truncate the list to the number of suggestions that will be displayed. */
10707 if (ga.ga_len > su->su_maxcount)
10708 {
10709 for (i = su->su_maxcount; i < ga.ga_len; ++i)
10710 vim_free(stp[i].st_word);
10711 ga.ga_len = su->su_maxcount;
10712 }
10713
10714 su->su_ga = ga;
10715}
10716
10717/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010718 * For the goodword in "stp" compute the soundalike score compared to the
10719 * badword.
10720 */
10721 static int
10722stp_sal_score(stp, su, slang, badsound)
10723 suggest_T *stp;
10724 suginfo_T *su;
10725 slang_T *slang;
10726 char_u *badsound; /* sound-folded badword */
10727{
10728 char_u *p;
10729 char_u badsound2[MAXWLEN];
10730 char_u fword[MAXWLEN];
10731 char_u goodsound[MAXWLEN];
10732
10733 if (stp->st_orglen <= su->su_badlen)
10734 p = badsound;
10735 else
10736 {
10737 /* soundfold the bad word with more characters following */
10738 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
10739
10740 /* When joining two words the sound often changes a lot. E.g., "t he"
10741 * sounds like "t h" while "the" sounds like "@". Avoid that by
10742 * removing the space. Don't do it when the good word also contains a
10743 * space. */
10744 if (vim_iswhite(su->su_badptr[su->su_badlen])
10745 && *skiptowhite(stp->st_word) == NUL)
10746 for (p = fword; *(p = skiptowhite(p)) != NUL; )
10747 mch_memmove(p, p + 1, STRLEN(p));
10748
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010749 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010750 p = badsound2;
10751 }
10752
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010753 /* Sound-fold the word and compute the score for the difference. */
10754 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010755
10756 return soundalike_score(goodsound, p);
10757}
10758
10759/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010760 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010761 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010762 */
10763 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000010764suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010765 suginfo_T *su;
10766{
10767 char_u salword[MAXWLEN];
10768 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010769 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010770 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010771 int curi[MAXWLEN];
10772 langp_T *lp;
10773 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010774 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010775 int depth;
10776 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010777 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010778 int round;
10779 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010780 int sound_score;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010781 int local_score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010782 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010783 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010784
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010785 /* Do this for all languages that support sound folding. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010786 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010787 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010788 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
10789 slang = lp->lp_slang;
10790 if (slang->sl_sal.ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010791 {
10792 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010793 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010794
10795 /*
10796 * Go through the whole tree, soundfold each word and compare.
10797 * round 1: use the case-folded tree.
10798 * round 2: use the keep-case tree.
10799 */
10800 for (round = 1; round <= 2; ++round)
10801 {
10802 if (round == 1)
10803 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010804 byts = slang->sl_fbyts;
10805 idxs = slang->sl_fidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010806 }
10807 else
10808 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010809 byts = slang->sl_kbyts;
10810 idxs = slang->sl_kidxs;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010811 if (byts == NULL) /* no keep-case words */
10812 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010813 }
10814
10815 depth = 0;
10816 arridx[0] = 0;
10817 curi[0] = 1;
10818 while (depth >= 0 && !got_int)
10819 {
10820 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010821 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010822 /* Done all bytes at this node, go up one level. */
10823 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010824 line_breakcheck();
10825 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010826 else
10827 {
10828 /* Do one more byte at this node. */
10829 n = arridx[depth] + curi[depth];
10830 ++curi[depth];
10831 c = byts[n];
10832 if (c == 0)
10833 {
10834 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010835 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010836 if (round == 2 || (flags & WF_KEEPCAP) == 0)
10837 {
10838 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010839 /* Sound-fold. Only in keep-case tree need to
10840 * case-fold the word. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010841 spell_soundfold(slang, tword,
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010842 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010843
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010844 /* Compute the edit distance between the
10845 * sound-a-like words. */
10846 sound_score = soundalike_score(salword,
10847 tsalword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010848
10849 /* Add a penalty for words in another region. */
10850 if ((flags & WF_REGION) && (((unsigned)flags
10851 >> 16) & lp->lp_region) == 0)
10852 local_score = SCORE_REGION;
10853 else
10854 local_score = 0;
10855 sound_score += local_score;
10856
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010857 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010858 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010859 char_u cword[MAXWLEN];
10860 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010861 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010862
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010863 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010864 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010865 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010866 /* Need to fix case according to
10867 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010868 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010869 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010870 }
10871 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010872 p = tword;
10873
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010874 if (sps_flags & SPS_DOUBLE)
10875 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000010876 su->su_badlen,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010877 sound_score, 0, FALSE,
10878 lp->lp_sallang);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010879 else
10880 {
10881 /* Compute the score. */
10882 score = spell_edit_score(
Bram Moolenaar5195e452005-08-19 20:32:47 +000010883 su->su_badword, p)
10884 + local_score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010885 if (sps_flags & SPS_BEST)
10886 /* give a bonus for the good word
10887 * sounding the same as the bad
10888 * word */
10889 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000010890 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010891 RESCORE(score, sound_score),
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010892 sound_score, TRUE,
10893 lp->lp_sallang);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010894 else
10895 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000010896 su->su_badlen,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010897 score + sound_score,
10898 0, FALSE,
10899 lp->lp_sallang);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010900 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010901 }
10902 }
10903
10904 /* Skip over other NUL bytes. */
10905 while (byts[n + 1] == 0)
10906 {
10907 ++n;
10908 ++curi[depth];
10909 }
10910 }
10911 else
10912 {
10913 /* Normal char, go one level deeper. */
10914 tword[depth++] = c;
10915 arridx[depth] = idxs[n];
10916 curi[depth] = 1;
10917 }
10918 }
10919 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010920 }
10921 }
10922 }
10923}
10924
10925/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010926 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010927 */
10928 static void
10929make_case_word(fword, cword, flags)
10930 char_u *fword;
10931 char_u *cword;
10932 int flags;
10933{
10934 if (flags & WF_ALLCAP)
10935 /* Make it all upper-case */
10936 allcap_copy(fword, cword);
10937 else if (flags & WF_ONECAP)
10938 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010939 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010940 else
10941 /* Use goodword as-is. */
10942 STRCPY(cword, fword);
10943}
10944
Bram Moolenaarea424162005-06-16 21:51:00 +000010945/*
10946 * Use map string "map" for languages "lp".
10947 */
10948 static void
10949set_map_str(lp, map)
10950 slang_T *lp;
10951 char_u *map;
10952{
10953 char_u *p;
10954 int headc = 0;
10955 int c;
10956 int i;
10957
10958 if (*map == NUL)
10959 {
10960 lp->sl_has_map = FALSE;
10961 return;
10962 }
10963 lp->sl_has_map = TRUE;
10964
10965 /* Init the array and hash table empty. */
10966 for (i = 0; i < 256; ++i)
10967 lp->sl_map_array[i] = 0;
10968#ifdef FEAT_MBYTE
10969 hash_init(&lp->sl_map_hash);
10970#endif
10971
10972 /*
10973 * The similar characters are stored separated with slashes:
10974 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
10975 * before the same slash. For characters above 255 sl_map_hash is used.
10976 */
10977 for (p = map; *p != NUL; )
10978 {
10979#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010980 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000010981#else
10982 c = *p++;
10983#endif
10984 if (c == '/')
10985 headc = 0;
10986 else
10987 {
10988 if (headc == 0)
10989 headc = c;
10990
10991#ifdef FEAT_MBYTE
10992 /* Characters above 255 don't fit in sl_map_array[], put them in
10993 * the hash table. Each entry is the char, a NUL the headchar and
10994 * a NUL. */
10995 if (c >= 256)
10996 {
10997 int cl = mb_char2len(c);
10998 int headcl = mb_char2len(headc);
10999 char_u *b;
11000 hash_T hash;
11001 hashitem_T *hi;
11002
11003 b = alloc((unsigned)(cl + headcl + 2));
11004 if (b == NULL)
11005 return;
11006 mb_char2bytes(c, b);
11007 b[cl] = NUL;
11008 mb_char2bytes(headc, b + cl + 1);
11009 b[cl + 1 + headcl] = NUL;
11010 hash = hash_hash(b);
11011 hi = hash_lookup(&lp->sl_map_hash, b, hash);
11012 if (HASHITEM_EMPTY(hi))
11013 hash_add_item(&lp->sl_map_hash, hi, b, hash);
11014 else
11015 {
11016 /* This should have been checked when generating the .spl
11017 * file. */
11018 EMSG(_("E999: duplicate char in MAP entry"));
11019 vim_free(b);
11020 }
11021 }
11022 else
11023#endif
11024 lp->sl_map_array[c] = headc;
11025 }
11026 }
11027}
11028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011029/*
11030 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
11031 * lines in the .aff file.
11032 */
11033 static int
11034similar_chars(slang, c1, c2)
11035 slang_T *slang;
11036 int c1;
11037 int c2;
11038{
Bram Moolenaarea424162005-06-16 21:51:00 +000011039 int m1, m2;
11040#ifdef FEAT_MBYTE
11041 char_u buf[MB_MAXBYTES];
11042 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011043
Bram Moolenaarea424162005-06-16 21:51:00 +000011044 if (c1 >= 256)
11045 {
11046 buf[mb_char2bytes(c1, buf)] = 0;
11047 hi = hash_find(&slang->sl_map_hash, buf);
11048 if (HASHITEM_EMPTY(hi))
11049 m1 = 0;
11050 else
11051 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
11052 }
11053 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011054#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000011055 m1 = slang->sl_map_array[c1];
11056 if (m1 == 0)
11057 return FALSE;
11058
11059
11060#ifdef FEAT_MBYTE
11061 if (c2 >= 256)
11062 {
11063 buf[mb_char2bytes(c2, buf)] = 0;
11064 hi = hash_find(&slang->sl_map_hash, buf);
11065 if (HASHITEM_EMPTY(hi))
11066 m2 = 0;
11067 else
11068 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
11069 }
11070 else
11071#endif
11072 m2 = slang->sl_map_array[c2];
11073
11074 return m1 == m2;
11075}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011076
11077/*
11078 * Add a suggestion to the list of suggestions.
11079 * Do not add a duplicate suggestion or suggestions with a bad score.
11080 * When "use_score" is not zero it's used, otherwise the score is computed
11081 * with spell_edit_score().
11082 */
11083 static void
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011084add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus, slang)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011085 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011086 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011087 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011088 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011089 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011090 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011091 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011092 slang_T *slang; /* language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011093{
Bram Moolenaar1e015462005-09-25 22:16:38 +000011094 int goodlen = STRLEN(goodword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011095 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011096 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011097 char_u *p = NULL;
11098 int c = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +000011099 int attr = 0;
11100 char_u longword[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011101
Bram Moolenaar1e015462005-09-25 22:16:38 +000011102 /* Check that the word really is valid. Esp. for banned words and for
11103 * split words, such as "the the". Need to append what follows to check
11104 * for that. */
11105 STRCPY(longword, goodword);
11106 vim_strncpy(longword + goodlen, su->su_badptr + badlen, MAXWLEN - goodlen);
11107 (void)spell_check(curwin, longword, &attr, NULL);
11108 if (attr != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011109 return;
11110
Bram Moolenaar0c405862005-06-22 22:26:26 +000011111 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
11112 * Remove the common part from "goodword". */
11113 i = badlen - su->su_badlen;
11114 if (i > 0)
11115 {
11116 /* This assumes there was no case folding or it didn't change the
11117 * length... */
Bram Moolenaar1e015462005-09-25 22:16:38 +000011118 p = goodword + goodlen - i;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011119 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
11120 {
11121 badlen = su->su_badlen;
11122 c = *p;
11123 *p = NUL;
11124 }
11125 else
11126 p = NULL;
11127 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011128 else if (i < 0)
11129 {
11130 /* When replacing part of the word check that we actually change
11131 * something. For "the the" a suggestion can be replacing the first
11132 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar1e015462005-09-25 22:16:38 +000011133 if (badlen == (int)goodlen
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011134 && STRNCMP(su->su_badword, goodword, badlen) == 0)
11135 return;
11136 }
11137
Bram Moolenaar0c405862005-06-22 22:26:26 +000011138
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011139 if (score <= su->su_maxscore)
11140 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011141 /* Check if the word is already there. Also check the length that is
11142 * being replaced "thes," -> "these" is a different suggestion from
11143 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011144 stp = &SUG(*gap, 0);
11145 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011146 if (STRCMP(stp[i].st_word, goodword) == 0
11147 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011148 {
11149 /* Found it. Remember the lowest score. */
11150 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011151 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011152 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011153 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011154 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011155 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011156 if (stp[i].st_slang == NULL)
11157 stp[i].st_slang = slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011158 break;
11159 }
11160
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011161 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011162 {
11163 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011164 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011165 stp->st_word = vim_strsave(goodword);
11166 if (stp->st_word != NULL)
11167 {
11168 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011169 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011170 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011171 stp->st_orglen = badlen;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011172 stp->st_slang = slang;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011173 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011174
11175 /* If we have too many suggestions now, sort the list and keep
11176 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011177 if (gap->ga_len > SUG_MAX_COUNT(su))
11178 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
11179 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011180 }
11181 }
11182 }
Bram Moolenaar0c405862005-06-22 22:26:26 +000011183
11184 if (p != NULL)
11185 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011186}
11187
11188/*
11189 * Add a word to be banned.
11190 */
11191 static void
11192add_banned(su, word)
11193 suginfo_T *su;
11194 char_u *word;
11195{
11196 char_u *s = vim_strsave(word);
11197 hash_T hash;
11198 hashitem_T *hi;
11199
11200 if (s != NULL)
11201 {
11202 hash = hash_hash(s);
11203 hi = hash_lookup(&su->su_banned, s, hash);
11204 if (HASHITEM_EMPTY(hi))
11205 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000011206 else
11207 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011208 }
11209}
11210
11211/*
11212 * Return TRUE if a word appears in the list of banned words.
11213 */
11214 static int
11215was_banned(su, word)
11216 suginfo_T *su;
11217 char_u *word;
11218{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011219 hashitem_T *hi = hash_find(&su->su_banned, word);
11220
11221 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011222}
11223
11224/*
11225 * Free the banned words in "su".
11226 */
11227 static void
11228free_banned(su)
11229 suginfo_T *su;
11230{
11231 int todo;
11232 hashitem_T *hi;
11233
11234 todo = su->su_banned.ht_used;
11235 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
11236 {
11237 if (!HASHITEM_EMPTY(hi))
11238 {
11239 vim_free(hi->hi_key);
11240 --todo;
11241 }
11242 }
11243 hash_clear(&su->su_banned);
11244}
11245
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011246/*
11247 * Recompute the score if sound-folding is possible. This is slow,
11248 * thus only done for the final results.
11249 */
11250 static void
11251rescore_suggestions(su)
11252 suginfo_T *su;
11253{
11254 langp_T *lp;
11255 suggest_T *stp;
11256 char_u sal_badword[MAXWLEN];
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011257 char_u sal_badword2[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011258 int i;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011259 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011260 slang_T *slang_first = NULL;
11261 slang_T *slang;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011262
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011263 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011264 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011265 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011266 if (lp->lp_slang->sl_sal.ga_len > 0)
11267 {
11268 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011269 slang_first = lp->lp_slang;
11270 spell_soundfold(slang_first, su->su_fbadword, TRUE, sal_badword);
11271 break;
11272 }
11273 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011274
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011275 if (slang_first != NULL)
11276 {
11277 for (i = 0; i < su->su_ga.ga_len; ++i)
11278 {
11279 /* Only rescore suggestions that have no sal score yet and do have
11280 * a language. */
11281 stp = &SUG(su->su_ga, i);
11282 if (!stp->st_had_bonus && stp->st_slang != NULL)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011283 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011284 slang = stp->st_slang;
11285 if (slang->sl_sal.ga_len > 0)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011286 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011287 if (slang == slang_first)
11288 stp->st_altscore = stp_sal_score(stp, su,
11289 slang, sal_badword);
11290 else
11291 {
11292 spell_soundfold(slang, su->su_fbadword,
11293 TRUE, sal_badword2);
11294 stp->st_altscore = stp_sal_score(stp, su,
11295 slang, sal_badword2);
11296 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011297 if (stp->st_altscore == SCORE_MAXMAX)
11298 stp->st_altscore = SCORE_BIG;
11299 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011300 }
11301 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011302 }
11303 }
11304}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011305
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011306static int
11307#ifdef __BORLANDC__
11308_RTLENTRYF
11309#endif
11310sug_compare __ARGS((const void *s1, const void *s2));
11311
11312/*
11313 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000011314 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011315 */
11316 static int
11317#ifdef __BORLANDC__
11318_RTLENTRYF
11319#endif
11320sug_compare(s1, s2)
11321 const void *s1;
11322 const void *s2;
11323{
11324 suggest_T *p1 = (suggest_T *)s1;
11325 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011326 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011327
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011328 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000011329 {
11330 n = p1->st_altscore - p2->st_altscore;
11331 if (n == 0)
11332 n = STRICMP(p1->st_word, p2->st_word);
11333 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011334 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011335}
11336
11337/*
11338 * Cleanup the suggestions:
11339 * - Sort on score.
11340 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011341 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011342 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011343 static int
11344cleanup_suggestions(gap, maxscore, keep)
11345 garray_T *gap;
11346 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011347 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011348{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011349 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011350 int i;
11351
11352 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011353 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011354
11355 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011356 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011357 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011358 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011359 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011360 gap->ga_len = keep;
11361 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011362 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011363 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011364}
11365
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011366#if defined(FEAT_EVAL) || defined(PROTO)
11367/*
11368 * Soundfold a string, for soundfold().
11369 * Result is in allocated memory, NULL for an error.
11370 */
11371 char_u *
11372eval_soundfold(word)
11373 char_u *word;
11374{
11375 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011376 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011377 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011378
11379 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
11380 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011381 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011382 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011383 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011384 if (lp->lp_slang->sl_sal.ga_len > 0)
11385 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011386 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011387 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011388 return vim_strsave(sound);
11389 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011390 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011391
11392 /* No language with sound folding, return word as-is. */
11393 return vim_strsave(word);
11394}
11395#endif
11396
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011397/*
11398 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000011399 *
11400 * There are many ways to turn a word into a sound-a-like representation. The
11401 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
11402 * swedish name matching - survey and test of different algorithms" by Klas
11403 * Erikson.
11404 *
11405 * We support two methods:
11406 * 1. SOFOFROM/SOFOTO do a simple character mapping.
11407 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011408 */
11409 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011410spell_soundfold(slang, inword, folded, res)
11411 slang_T *slang;
11412 char_u *inword;
11413 int folded; /* "inword" is already case-folded */
11414 char_u *res;
11415{
11416 char_u fword[MAXWLEN];
11417 char_u *word;
11418
11419 if (slang->sl_sofo)
11420 /* SOFOFROM and SOFOTO used */
11421 spell_soundfold_sofo(slang, inword, res);
11422 else
11423 {
11424 /* SAL items used. Requires the word to be case-folded. */
11425 if (folded)
11426 word = inword;
11427 else
11428 {
11429 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
11430 word = fword;
11431 }
11432
11433#ifdef FEAT_MBYTE
11434 if (has_mbyte)
11435 spell_soundfold_wsal(slang, word, res);
11436 else
11437#endif
11438 spell_soundfold_sal(slang, word, res);
11439 }
11440}
11441
11442/*
11443 * Perform sound folding of "inword" into "res" according to SOFOFROM and
11444 * SOFOTO lines.
11445 */
11446 static void
11447spell_soundfold_sofo(slang, inword, res)
11448 slang_T *slang;
11449 char_u *inword;
11450 char_u *res;
11451{
11452 char_u *s;
11453 int ri = 0;
11454 int c;
11455
11456#ifdef FEAT_MBYTE
11457 if (has_mbyte)
11458 {
11459 int prevc = 0;
11460 int *ip;
11461
11462 /* The sl_sal_first[] table contains the translation for chars up to
11463 * 255, sl_sal the rest. */
11464 for (s = inword; *s != NUL; )
11465 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011466 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011467 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
11468 c = ' ';
11469 else if (c < 256)
11470 c = slang->sl_sal_first[c];
11471 else
11472 {
11473 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
11474 if (ip == NULL) /* empty list, can't match */
11475 c = NUL;
11476 else
11477 for (;;) /* find "c" in the list */
11478 {
11479 if (*ip == 0) /* not found */
11480 {
11481 c = NUL;
11482 break;
11483 }
11484 if (*ip == c) /* match! */
11485 {
11486 c = ip[1];
11487 break;
11488 }
11489 ip += 2;
11490 }
11491 }
11492
11493 if (c != NUL && c != prevc)
11494 {
11495 ri += mb_char2bytes(c, res + ri);
11496 if (ri + MB_MAXBYTES > MAXWLEN)
11497 break;
11498 prevc = c;
11499 }
11500 }
11501 }
11502 else
11503#endif
11504 {
11505 /* The sl_sal_first[] table contains the translation. */
11506 for (s = inword; (c = *s) != NUL; ++s)
11507 {
11508 if (vim_iswhite(c))
11509 c = ' ';
11510 else
11511 c = slang->sl_sal_first[c];
11512 if (c != NUL && (ri == 0 || res[ri - 1] != c))
11513 res[ri++] = c;
11514 }
11515 }
11516
11517 res[ri] = NUL;
11518}
11519
11520 static void
11521spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011522 slang_T *slang;
11523 char_u *inword;
11524 char_u *res;
11525{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011526 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011527 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011528 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011529 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011530 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011531 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011532 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011533 int n, k = 0;
11534 int z0;
11535 int k0;
11536 int n0;
11537 int c;
11538 int pri;
11539 int p0 = -333;
11540 int c0;
11541
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011542 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011543 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011544 if (slang->sl_rem_accents)
11545 {
11546 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011547 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011548 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011549 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011550 {
11551 *t++ = ' ';
11552 s = skipwhite(s);
11553 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011554 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011555 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011556 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011557 *t++ = *s;
11558 ++s;
11559 }
11560 }
11561 *t = NUL;
11562 }
11563 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011564 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011565
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011566 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011567
11568 /*
11569 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011570 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011571 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011572 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011573 while ((c = word[i]) != NUL)
11574 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011575 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011576 n = slang->sl_sal_first[c];
11577 z0 = 0;
11578
11579 if (n >= 0)
11580 {
11581 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011582 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011583 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011584 /* Quickly skip entries that don't match the word. Most
11585 * entries are less then three chars, optimize for that. */
11586 k = smp[n].sm_leadlen;
11587 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011588 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011589 if (word[i + 1] != s[1])
11590 continue;
11591 if (k > 2)
11592 {
11593 for (j = 2; j < k; ++j)
11594 if (word[i + j] != s[j])
11595 break;
11596 if (j < k)
11597 continue;
11598 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011599 }
11600
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011601 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011602 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011603 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011604 while (*pf != NUL && *pf != word[i + k])
11605 ++pf;
11606 if (*pf == NUL)
11607 continue;
11608 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011609 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011610 s = smp[n].sm_rules;
11611 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011612
11613 p0 = *s;
11614 k0 = k;
11615 while (*s == '-' && k > 1)
11616 {
11617 k--;
11618 s++;
11619 }
11620 if (*s == '<')
11621 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011622 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011623 {
11624 /* determine priority */
11625 pri = *s - '0';
11626 s++;
11627 }
11628 if (*s == '^' && *(s + 1) == '^')
11629 s++;
11630
11631 if (*s == NUL
11632 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011633 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011634 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011635 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011636 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011637 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011638 && spell_iswordp(word + i - 1, curbuf)
11639 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011640 {
11641 /* search for followup rules, if: */
11642 /* followup and k > 1 and NO '-' in searchstring */
11643 c0 = word[i + k - 1];
11644 n0 = slang->sl_sal_first[c0];
11645
11646 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011647 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011648 {
11649 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011650 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011651 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011652 /* Quickly skip entries that don't match the word.
11653 * */
11654 k0 = smp[n0].sm_leadlen;
11655 if (k0 > 1)
11656 {
11657 if (word[i + k] != s[1])
11658 continue;
11659 if (k0 > 2)
11660 {
11661 pf = word + i + k + 1;
11662 for (j = 2; j < k0; ++j)
11663 if (*pf++ != s[j])
11664 break;
11665 if (j < k0)
11666 continue;
11667 }
11668 }
11669 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011670
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011671 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011672 {
11673 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011674 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011675 while (*pf != NUL && *pf != word[i + k0])
11676 ++pf;
11677 if (*pf == NUL)
11678 continue;
11679 ++k0;
11680 }
11681
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011682 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011683 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011684 while (*s == '-')
11685 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011686 /* "k0" gets NOT reduced because
11687 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011688 s++;
11689 }
11690 if (*s == '<')
11691 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011692 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011693 {
11694 p0 = *s - '0';
11695 s++;
11696 }
11697
11698 if (*s == NUL
11699 /* *s == '^' cuts */
11700 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011701 && !spell_iswordp(word + i + k0,
11702 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011703 {
11704 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011705 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011706 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011707
11708 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011709 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011710 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011711 /* rule fits; stop search */
11712 break;
11713 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011714 }
11715
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011716 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011717 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011718 }
11719
11720 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011721 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011722 if (s == NULL)
11723 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011724 pf = smp[n].sm_rules;
11725 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011726 if (p0 == 1 && z == 0)
11727 {
11728 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011729 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
11730 || res[reslen - 1] == *s))
11731 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011732 z0 = 1;
11733 z = 1;
11734 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011735 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011736 {
11737 word[i + k0] = *s;
11738 k0++;
11739 s++;
11740 }
11741 if (k > k0)
11742 mch_memmove(word + i + k0, word + i + k,
11743 STRLEN(word + i + k) + 1);
11744
11745 /* new "actual letter" */
11746 c = word[i];
11747 }
11748 else
11749 {
11750 /* no '<' rule used */
11751 i += k - 1;
11752 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011753 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011754 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011755 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011756 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011757 s++;
11758 }
11759 /* new "actual letter" */
11760 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011761 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011762 {
11763 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011764 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011765 mch_memmove(word, word + i + 1,
11766 STRLEN(word + i + 1) + 1);
11767 i = 0;
11768 z0 = 1;
11769 }
11770 }
11771 break;
11772 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011773 }
11774 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011775 else if (vim_iswhite(c))
11776 {
11777 c = ' ';
11778 k = 1;
11779 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011780
11781 if (z0 == 0)
11782 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011783 if (k && !p0 && reslen < MAXWLEN && c != NUL
11784 && (!slang->sl_collapse || reslen == 0
11785 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011786 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011787 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011788
11789 i++;
11790 z = 0;
11791 k = 0;
11792 }
11793 }
11794
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011795 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011796}
11797
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011798#ifdef FEAT_MBYTE
11799/*
11800 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
11801 * Multi-byte version of spell_soundfold().
11802 */
11803 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011804spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011805 slang_T *slang;
11806 char_u *inword;
11807 char_u *res;
11808{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011809 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011810 int word[MAXWLEN];
11811 int wres[MAXWLEN];
11812 int l;
11813 char_u *s;
11814 int *ws;
11815 char_u *t;
11816 int *pf;
11817 int i, j, z;
11818 int reslen;
11819 int n, k = 0;
11820 int z0;
11821 int k0;
11822 int n0;
11823 int c;
11824 int pri;
11825 int p0 = -333;
11826 int c0;
11827 int did_white = FALSE;
11828
11829 /*
11830 * Convert the multi-byte string to a wide-character string.
11831 * Remove accents, if wanted. We actually remove all non-word characters.
11832 * But keep white space.
11833 */
11834 n = 0;
11835 for (s = inword; *s != NUL; )
11836 {
11837 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011838 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011839 if (slang->sl_rem_accents)
11840 {
11841 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
11842 {
11843 if (did_white)
11844 continue;
11845 c = ' ';
11846 did_white = TRUE;
11847 }
11848 else
11849 {
11850 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011851 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011852 continue;
11853 }
11854 }
11855 word[n++] = c;
11856 }
11857 word[n] = NUL;
11858
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011859 /*
11860 * This comes from Aspell phonet.cpp.
11861 * Converted from C++ to C. Added support for multi-byte chars.
11862 * Changed to keep spaces.
11863 */
11864 i = reslen = z = 0;
11865 while ((c = word[i]) != NUL)
11866 {
11867 /* Start with the first rule that has the character in the word. */
11868 n = slang->sl_sal_first[c & 0xff];
11869 z0 = 0;
11870
11871 if (n >= 0)
11872 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011873 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011874 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
11875 {
11876 /* Quickly skip entries that don't match the word. Most
11877 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011878 if (c != ws[0])
11879 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011880 k = smp[n].sm_leadlen;
11881 if (k > 1)
11882 {
11883 if (word[i + 1] != ws[1])
11884 continue;
11885 if (k > 2)
11886 {
11887 for (j = 2; j < k; ++j)
11888 if (word[i + j] != ws[j])
11889 break;
11890 if (j < k)
11891 continue;
11892 }
11893 }
11894
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011895 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011896 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011897 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011898 while (*pf != NUL && *pf != word[i + k])
11899 ++pf;
11900 if (*pf == NUL)
11901 continue;
11902 ++k;
11903 }
11904 s = smp[n].sm_rules;
11905 pri = 5; /* default priority */
11906
11907 p0 = *s;
11908 k0 = k;
11909 while (*s == '-' && k > 1)
11910 {
11911 k--;
11912 s++;
11913 }
11914 if (*s == '<')
11915 s++;
11916 if (VIM_ISDIGIT(*s))
11917 {
11918 /* determine priority */
11919 pri = *s - '0';
11920 s++;
11921 }
11922 if (*s == '^' && *(s + 1) == '^')
11923 s++;
11924
11925 if (*s == NUL
11926 || (*s == '^'
11927 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011928 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011929 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011930 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011931 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011932 && spell_iswordp_w(word + i - 1, curbuf)
11933 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011934 {
11935 /* search for followup rules, if: */
11936 /* followup and k > 1 and NO '-' in searchstring */
11937 c0 = word[i + k - 1];
11938 n0 = slang->sl_sal_first[c0 & 0xff];
11939
11940 if (slang->sl_followup && k > 1 && n0 >= 0
11941 && p0 != '-' && word[i + k] != NUL)
11942 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011943 /* Test follow-up rule for "word[i + k]"; loop over
11944 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011945 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
11946 == (c0 & 0xff); ++n0)
11947 {
11948 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011949 */
11950 if (c0 != ws[0])
11951 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011952 k0 = smp[n0].sm_leadlen;
11953 if (k0 > 1)
11954 {
11955 if (word[i + k] != ws[1])
11956 continue;
11957 if (k0 > 2)
11958 {
11959 pf = word + i + k + 1;
11960 for (j = 2; j < k0; ++j)
11961 if (*pf++ != ws[j])
11962 break;
11963 if (j < k0)
11964 continue;
11965 }
11966 }
11967 k0 += k - 1;
11968
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011969 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011970 {
11971 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011972 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011973 while (*pf != NUL && *pf != word[i + k0])
11974 ++pf;
11975 if (*pf == NUL)
11976 continue;
11977 ++k0;
11978 }
11979
11980 p0 = 5;
11981 s = smp[n0].sm_rules;
11982 while (*s == '-')
11983 {
11984 /* "k0" gets NOT reduced because
11985 * "if (k0 == k)" */
11986 s++;
11987 }
11988 if (*s == '<')
11989 s++;
11990 if (VIM_ISDIGIT(*s))
11991 {
11992 p0 = *s - '0';
11993 s++;
11994 }
11995
11996 if (*s == NUL
11997 /* *s == '^' cuts */
11998 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011999 && !spell_iswordp_w(word + i + k0,
12000 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012001 {
12002 if (k0 == k)
12003 /* this is just a piece of the string */
12004 continue;
12005
12006 if (p0 < pri)
12007 /* priority too low */
12008 continue;
12009 /* rule fits; stop search */
12010 break;
12011 }
12012 }
12013
12014 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
12015 == (c0 & 0xff))
12016 continue;
12017 }
12018
12019 /* replace string */
12020 ws = smp[n].sm_to_w;
12021 s = smp[n].sm_rules;
12022 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
12023 if (p0 == 1 && z == 0)
12024 {
12025 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000012026 if (reslen > 0 && ws != NULL && *ws != NUL
12027 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012028 || wres[reslen - 1] == *ws))
12029 reslen--;
12030 z0 = 1;
12031 z = 1;
12032 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000012033 if (ws != NULL)
12034 while (*ws != NUL && word[i + k0] != NUL)
12035 {
12036 word[i + k0] = *ws;
12037 k0++;
12038 ws++;
12039 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012040 if (k > k0)
12041 mch_memmove(word + i + k0, word + i + k,
12042 sizeof(int) * (STRLEN(word + i + k) + 1));
12043
12044 /* new "actual letter" */
12045 c = word[i];
12046 }
12047 else
12048 {
12049 /* no '<' rule used */
12050 i += k - 1;
12051 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000012052 if (ws != NULL)
12053 while (*ws != NUL && ws[1] != NUL
12054 && reslen < MAXWLEN)
12055 {
12056 if (reslen == 0 || wres[reslen - 1] != *ws)
12057 wres[reslen++] = *ws;
12058 ws++;
12059 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012060 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000012061 if (ws == NULL)
12062 c = NUL;
12063 else
12064 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012065 if (strstr((char *)s, "^^") != NULL)
12066 {
12067 if (c != NUL)
12068 wres[reslen++] = c;
12069 mch_memmove(word, word + i + 1,
12070 sizeof(int) * (STRLEN(word + i + 1) + 1));
12071 i = 0;
12072 z0 = 1;
12073 }
12074 }
12075 break;
12076 }
12077 }
12078 }
12079 else if (vim_iswhite(c))
12080 {
12081 c = ' ';
12082 k = 1;
12083 }
12084
12085 if (z0 == 0)
12086 {
12087 if (k && !p0 && reslen < MAXWLEN && c != NUL
12088 && (!slang->sl_collapse || reslen == 0
12089 || wres[reslen - 1] != c))
12090 /* condense only double letters */
12091 wres[reslen++] = c;
12092
12093 i++;
12094 z = 0;
12095 k = 0;
12096 }
12097 }
12098
12099 /* Convert wide characters in "wres" to a multi-byte string in "res". */
12100 l = 0;
12101 for (n = 0; n < reslen; ++n)
12102 {
12103 l += mb_char2bytes(wres[n], res + l);
12104 if (l + MB_MAXBYTES > MAXWLEN)
12105 break;
12106 }
12107 res[l] = NUL;
12108}
12109#endif
12110
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012111/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012112 * Compute a score for two sound-a-like words.
12113 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
12114 * Instead of a generic loop we write out the code. That keeps it fast by
12115 * avoiding checks that will not be possible.
12116 */
12117 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012118soundalike_score(goodstart, badstart)
12119 char_u *goodstart; /* sound-folded good word */
12120 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012121{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012122 char_u *goodsound = goodstart;
12123 char_u *badsound = badstart;
12124 int goodlen;
12125 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012126 int n;
12127 char_u *pl, *ps;
12128 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012129 int score = 0;
12130
12131 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
12132 * counted so much, vowels halfway the word aren't counted at all. */
12133 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
12134 {
12135 score = SCORE_DEL / 2;
12136 if (*badsound == '*')
12137 ++badsound;
12138 else
12139 ++goodsound;
12140 }
12141
12142 goodlen = STRLEN(goodsound);
12143 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012144
12145 /* Return quickly if the lenghts are too different to be fixed by two
12146 * changes. */
12147 n = goodlen - badlen;
12148 if (n < -2 || n > 2)
12149 return SCORE_MAXMAX;
12150
12151 if (n > 0)
12152 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012153 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012154 ps = badsound;
12155 }
12156 else
12157 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012158 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012159 ps = goodsound;
12160 }
12161
12162 /* Skip over the identical part. */
12163 while (*pl == *ps && *pl != NUL)
12164 {
12165 ++pl;
12166 ++ps;
12167 }
12168
12169 switch (n)
12170 {
12171 case -2:
12172 case 2:
12173 /*
12174 * Must delete two characters from "pl".
12175 */
12176 ++pl; /* first delete */
12177 while (*pl == *ps)
12178 {
12179 ++pl;
12180 ++ps;
12181 }
12182 /* strings must be equal after second delete */
12183 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012184 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012185
12186 /* Failed to compare. */
12187 break;
12188
12189 case -1:
12190 case 1:
12191 /*
12192 * Minimal one delete from "pl" required.
12193 */
12194
12195 /* 1: delete */
12196 pl2 = pl + 1;
12197 ps2 = ps;
12198 while (*pl2 == *ps2)
12199 {
12200 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012201 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012202 ++pl2;
12203 ++ps2;
12204 }
12205
12206 /* 2: delete then swap, then rest must be equal */
12207 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
12208 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012209 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012210
12211 /* 3: delete then substitute, then the rest must be equal */
12212 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012213 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012214
12215 /* 4: first swap then delete */
12216 if (pl[0] == ps[1] && pl[1] == ps[0])
12217 {
12218 pl2 = pl + 2; /* swap, skip two chars */
12219 ps2 = ps + 2;
12220 while (*pl2 == *ps2)
12221 {
12222 ++pl2;
12223 ++ps2;
12224 }
12225 /* delete a char and then strings must be equal */
12226 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012227 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012228 }
12229
12230 /* 5: first substitute then delete */
12231 pl2 = pl + 1; /* substitute, skip one char */
12232 ps2 = ps + 1;
12233 while (*pl2 == *ps2)
12234 {
12235 ++pl2;
12236 ++ps2;
12237 }
12238 /* delete a char and then strings must be equal */
12239 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012240 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012241
12242 /* Failed to compare. */
12243 break;
12244
12245 case 0:
12246 /*
12247 * Lenghts are equal, thus changes must result in same length: An
12248 * insert is only possible in combination with a delete.
12249 * 1: check if for identical strings
12250 */
12251 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012252 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012253
12254 /* 2: swap */
12255 if (pl[0] == ps[1] && pl[1] == ps[0])
12256 {
12257 pl2 = pl + 2; /* swap, skip two chars */
12258 ps2 = ps + 2;
12259 while (*pl2 == *ps2)
12260 {
12261 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012262 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012263 ++pl2;
12264 ++ps2;
12265 }
12266 /* 3: swap and swap again */
12267 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
12268 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012269 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012270
12271 /* 4: swap and substitute */
12272 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012273 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012274 }
12275
12276 /* 5: substitute */
12277 pl2 = pl + 1;
12278 ps2 = ps + 1;
12279 while (*pl2 == *ps2)
12280 {
12281 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012282 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012283 ++pl2;
12284 ++ps2;
12285 }
12286
12287 /* 6: substitute and swap */
12288 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
12289 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012290 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012291
12292 /* 7: substitute and substitute */
12293 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012294 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012295
12296 /* 8: insert then delete */
12297 pl2 = pl;
12298 ps2 = ps + 1;
12299 while (*pl2 == *ps2)
12300 {
12301 ++pl2;
12302 ++ps2;
12303 }
12304 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012305 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012306
12307 /* 9: delete then insert */
12308 pl2 = pl + 1;
12309 ps2 = ps;
12310 while (*pl2 == *ps2)
12311 {
12312 ++pl2;
12313 ++ps2;
12314 }
12315 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012316 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012317
12318 /* Failed to compare. */
12319 break;
12320 }
12321
12322 return SCORE_MAXMAX;
12323}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012324
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012325/*
12326 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012327 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012328 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000012329 * The algorithm is described by Du and Chang, 1992.
12330 * The implementation of the algorithm comes from Aspell editdist.cpp,
12331 * edit_distance(). It has been converted from C++ to C and modified to
12332 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012333 */
12334 static int
12335spell_edit_score(badword, goodword)
12336 char_u *badword;
12337 char_u *goodword;
12338{
12339 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012340 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012341 int j, i;
12342 int t;
12343 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012344 int pbc, pgc;
12345#ifdef FEAT_MBYTE
12346 char_u *p;
12347 int wbadword[MAXWLEN];
12348 int wgoodword[MAXWLEN];
12349
12350 if (has_mbyte)
12351 {
12352 /* Get the characters from the multi-byte strings and put them in an
12353 * int array for easy access. */
12354 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012355 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000012356 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012357 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012358 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000012359 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012360 }
12361 else
12362#endif
12363 {
12364 badlen = STRLEN(badword) + 1;
12365 goodlen = STRLEN(goodword) + 1;
12366 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012367
12368 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
12369#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012370 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
12371 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012372 if (cnt == NULL)
12373 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012374
12375 CNT(0, 0) = 0;
12376 for (j = 1; j <= goodlen; ++j)
12377 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
12378
12379 for (i = 1; i <= badlen; ++i)
12380 {
12381 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
12382 for (j = 1; j <= goodlen; ++j)
12383 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012384#ifdef FEAT_MBYTE
12385 if (has_mbyte)
12386 {
12387 bc = wbadword[i - 1];
12388 gc = wgoodword[j - 1];
12389 }
12390 else
12391#endif
12392 {
12393 bc = badword[i - 1];
12394 gc = goodword[j - 1];
12395 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012396 if (bc == gc)
12397 CNT(i, j) = CNT(i - 1, j - 1);
12398 else
12399 {
12400 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012401 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012402 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
12403 else
12404 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
12405
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012406 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012407 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012408#ifdef FEAT_MBYTE
12409 if (has_mbyte)
12410 {
12411 pbc = wbadword[i - 2];
12412 pgc = wgoodword[j - 2];
12413 }
12414 else
12415#endif
12416 {
12417 pbc = badword[i - 2];
12418 pgc = goodword[j - 2];
12419 }
12420 if (bc == pgc && pbc == gc)
12421 {
12422 t = SCORE_SWAP + CNT(i - 2, j - 2);
12423 if (t < CNT(i, j))
12424 CNT(i, j) = t;
12425 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012426 }
12427 t = SCORE_DEL + CNT(i - 1, j);
12428 if (t < CNT(i, j))
12429 CNT(i, j) = t;
12430 t = SCORE_INS + CNT(i, j - 1);
12431 if (t < CNT(i, j))
12432 CNT(i, j) = t;
12433 }
12434 }
12435 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012436
12437 i = CNT(badlen - 1, goodlen - 1);
12438 vim_free(cnt);
12439 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012440}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000012441
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012442/*
12443 * ":spelldump"
12444 */
12445/*ARGSUSED*/
12446 void
12447ex_spelldump(eap)
12448 exarg_T *eap;
12449{
12450 buf_T *buf = curbuf;
12451 langp_T *lp;
12452 slang_T *slang;
12453 idx_T arridx[MAXWLEN];
12454 int curi[MAXWLEN];
12455 char_u word[MAXWLEN];
12456 int c;
12457 char_u *byts;
12458 idx_T *idxs;
12459 linenr_T lnum = 0;
12460 int round;
12461 int depth;
12462 int n;
12463 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000012464 char_u *region_names = NULL; /* region names being used */
12465 int do_region = TRUE; /* dump region names and numbers */
12466 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012467 int lpi;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012468
Bram Moolenaar95529562005-08-25 21:21:38 +000012469 if (no_spell_checking(curwin))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012470 return;
12471
12472 /* Create a new empty buffer by splitting the window. */
12473 do_cmdline_cmd((char_u *)"new");
12474 if (!bufempty() || !buf_valid(buf))
12475 return;
12476
Bram Moolenaar7887d882005-07-01 22:33:52 +000012477 /* Find out if we can support regions: All languages must support the same
12478 * regions or none at all. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012479 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000012480 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012481 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000012482 p = lp->lp_slang->sl_regions;
12483 if (p[0] != 0)
12484 {
12485 if (region_names == NULL) /* first language with regions */
12486 region_names = p;
12487 else if (STRCMP(region_names, p) != 0)
12488 {
12489 do_region = FALSE; /* region names are different */
12490 break;
12491 }
12492 }
12493 }
12494
12495 if (do_region && region_names != NULL)
12496 {
12497 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
12498 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
12499 }
12500 else
12501 do_region = FALSE;
12502
12503 /*
12504 * Loop over all files loaded for the entries in 'spelllang'.
12505 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012506 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012507 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012508 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012509 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012510 if (slang->sl_fbyts == NULL) /* reloading failed */
12511 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012512
12513 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
12514 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
12515
12516 /* round 1: case-folded tree
12517 * round 2: keep-case tree */
12518 for (round = 1; round <= 2; ++round)
12519 {
12520 if (round == 1)
12521 {
12522 byts = slang->sl_fbyts;
12523 idxs = slang->sl_fidxs;
12524 }
12525 else
12526 {
12527 byts = slang->sl_kbyts;
12528 idxs = slang->sl_kidxs;
12529 }
12530 if (byts == NULL)
12531 continue; /* array is empty */
12532
12533 depth = 0;
12534 arridx[0] = 0;
12535 curi[0] = 1;
12536 while (depth >= 0 && !got_int)
12537 {
12538 if (curi[depth] > byts[arridx[depth]])
12539 {
12540 /* Done all bytes at this node, go up one level. */
12541 --depth;
12542 line_breakcheck();
12543 }
12544 else
12545 {
12546 /* Do one more byte at this node. */
12547 n = arridx[depth] + curi[depth];
12548 ++curi[depth];
12549 c = byts[n];
12550 if (c == 0)
12551 {
12552 /* End of word, deal with the word.
12553 * Don't use keep-case words in the fold-case tree,
12554 * they will appear in the keep-case tree.
12555 * Only use the word when the region matches. */
12556 flags = (int)idxs[n];
12557 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012558 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000012559 && (do_region
12560 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012561 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012562 & lp->lp_region) != 0))
12563 {
12564 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000012565 if (!do_region)
12566 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000012567
12568 /* Dump the basic word if there is no prefix or
12569 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012570 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000012571 if (c == 0 || curi[depth] == 2)
12572 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012573
12574 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000012575 if (c != 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012576 lnum = dump_prefixes(slang, word, round,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012577 flags, lnum);
12578 }
12579 }
12580 else
12581 {
12582 /* Normal char, go one level deeper. */
12583 word[depth++] = c;
12584 arridx[depth] = idxs[n];
12585 curi[depth] = 1;
12586 }
12587 }
12588 }
12589 }
12590 }
12591
12592 /* Delete the empty line that we started with. */
12593 if (curbuf->b_ml.ml_line_count > 1)
12594 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
12595
12596 redraw_later(NOT_VALID);
12597}
12598
12599/*
12600 * Dump one word: apply case modifications and append a line to the buffer.
12601 */
12602 static void
12603dump_word(word, round, flags, lnum)
12604 char_u *word;
12605 int round;
12606 int flags;
12607 linenr_T lnum;
12608{
12609 int keepcap = FALSE;
12610 char_u *p;
12611 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000012612 char_u badword[MAXWLEN + 10];
12613 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012614
12615 if (round == 1 && (flags & WF_CAPMASK) != 0)
12616 {
12617 /* Need to fix case according to "flags". */
12618 make_case_word(word, cword, flags);
12619 p = cword;
12620 }
12621 else
12622 {
12623 p = word;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000012624 if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0
12625 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012626 keepcap = TRUE;
12627 }
12628
Bram Moolenaar7887d882005-07-01 22:33:52 +000012629 /* Add flags and regions after a slash. */
12630 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012631 {
Bram Moolenaar7887d882005-07-01 22:33:52 +000012632 STRCPY(badword, p);
12633 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012634 if (keepcap)
12635 STRCAT(badword, "=");
12636 if (flags & WF_BANNED)
12637 STRCAT(badword, "!");
12638 else if (flags & WF_RARE)
12639 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +000012640 if (flags & WF_REGION)
12641 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012642 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +000012643 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012644 p = badword;
12645 }
12646
12647 ml_append(lnum, p, (colnr_T)0, FALSE);
12648}
12649
12650/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012651 * For ":spelldump": Find matching prefixes for "word". Prepend each to
12652 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012653 * Return the updated line number.
12654 */
12655 static linenr_T
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012656dump_prefixes(slang, word, round, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012657 slang_T *slang;
12658 char_u *word; /* case-folded word */
12659 int round;
12660 int flags; /* flags with prefix ID */
12661 linenr_T startlnum;
12662{
12663 idx_T arridx[MAXWLEN];
12664 int curi[MAXWLEN];
12665 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000012666 char_u word_up[MAXWLEN];
12667 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012668 int c;
12669 char_u *byts;
12670 idx_T *idxs;
12671 linenr_T lnum = startlnum;
12672 int depth;
12673 int n;
12674 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012675 int i;
12676
Bram Moolenaar53805d12005-08-01 07:08:33 +000012677 /* if the word starts with a lower-case letter make the word with an
12678 * upper-case letter in word_up[]. */
12679 c = PTR2CHAR(word);
12680 if (SPELL_TOUPPER(c) != c)
12681 {
12682 onecap_copy(word, word_up, TRUE);
12683 has_word_up = TRUE;
12684 }
12685
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012686 byts = slang->sl_pbyts;
12687 idxs = slang->sl_pidxs;
12688 if (byts != NULL) /* array not is empty */
12689 {
12690 /*
12691 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012692 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012693 */
12694 depth = 0;
12695 arridx[0] = 0;
12696 curi[0] = 1;
12697 while (depth >= 0 && !got_int)
12698 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012699 n = arridx[depth];
12700 len = byts[n];
12701 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012702 {
12703 /* Done all bytes at this node, go up one level. */
12704 --depth;
12705 line_breakcheck();
12706 }
12707 else
12708 {
12709 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012710 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012711 ++curi[depth];
12712 c = byts[n];
12713 if (c == 0)
12714 {
12715 /* End of prefix, find out how many IDs there are. */
12716 for (i = 1; i < len; ++i)
12717 if (byts[n + i] != 0)
12718 break;
12719 curi[depth] += i - 1;
12720
Bram Moolenaar53805d12005-08-01 07:08:33 +000012721 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
12722 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012723 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000012724 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000012725 dump_word(prefix, round,
Bram Moolenaar53805d12005-08-01 07:08:33 +000012726 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000012727 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012728 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000012729
12730 /* Check for prefix that matches the word when the
12731 * first letter is upper-case, but only if the prefix has
12732 * a condition. */
12733 if (has_word_up)
12734 {
12735 c = valid_word_prefix(i, n, flags, word_up, slang,
12736 TRUE);
12737 if (c != 0)
12738 {
12739 vim_strncpy(prefix + depth, word_up,
12740 MAXWLEN - depth - 1);
12741 dump_word(prefix, round,
12742 (c & WF_RAREPFX) ? (flags | WF_RARE)
12743 : flags, lnum++);
12744 }
12745 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012746 }
12747 else
12748 {
12749 /* Normal char, go one level deeper. */
12750 prefix[depth++] = c;
12751 arridx[depth] = idxs[n];
12752 curi[depth] = 1;
12753 }
12754 }
12755 }
12756 }
12757
12758 return lnum;
12759}
12760
Bram Moolenaar95529562005-08-25 21:21:38 +000012761/*
12762 * Move "p" to end of word.
12763 */
12764 char_u *
12765spell_to_word_end(start, buf)
12766 char_u *start;
12767 buf_T *buf;
12768{
12769 char_u *p = start;
12770
12771 while (*p != NUL && spell_iswordp(p, buf))
12772 mb_ptr_adv(p);
12773 return p;
12774}
12775
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012776#if defined(FEAT_INS_EXPAND) || defined(PROTO)
12777static int spell_expand_need_cap;
12778
12779/*
12780 * Find start of the word in front of the cursor. We don't check if it is
12781 * badly spelled, with completion we can only change the word in front of the
12782 * cursor.
12783 * Used for Insert mode completion CTRL-X ?.
12784 * Returns the column number of the word.
12785 */
12786 int
12787spell_word_start(startcol)
12788 int startcol;
12789{
12790 char_u *line;
12791 char_u *p;
12792 int col = 0;
12793
Bram Moolenaar95529562005-08-25 21:21:38 +000012794 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012795 return startcol;
12796
12797 /* Find a word character before "startcol". */
12798 line = ml_get_curline();
12799 for (p = line + startcol; p > line; )
12800 {
12801 mb_ptr_back(line, p);
12802 if (spell_iswordp_nmw(p))
12803 break;
12804 }
12805
12806 /* Go back to start of the word. */
12807 while (p > line)
12808 {
12809 col = p - line;
12810 mb_ptr_back(line, p);
12811 if (!spell_iswordp(p, curbuf))
12812 break;
12813 col = 0;
12814 }
12815
12816 /* Need to check for 'spellcapcheck' now, the word is removed before
12817 * expand_spelling() is called. Therefore the ugly global variable. */
12818 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
12819
12820 return col;
12821}
12822
12823/*
12824 * Get list of spelling suggestions.
12825 * Used for Insert mode completion CTRL-X ?.
12826 * Returns the number of matches. The matches are in "matchp[]", array of
12827 * allocated strings.
12828 */
12829/*ARGSUSED*/
12830 int
12831expand_spelling(lnum, col, pat, matchp)
12832 linenr_T lnum;
12833 int col;
12834 char_u *pat;
12835 char_u ***matchp;
12836{
12837 garray_T ga;
12838
12839 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap);
12840 *matchp = ga.ga_data;
12841 return ga.ga_len;
12842}
12843#endif
12844
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000012845#endif /* FEAT_SYN_HL */