blob: 2b04bf8b3b54718e26601245baa8e7cde44f3de4 [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) */
378 int sl_compminlen; /* COMPOUNDMIN (default: MAXWLEN) */
379 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{
426 slang_T *lp_slang; /* info for this language (NULL for last one) */
427 int lp_region; /* bitmask for region or REGION_ALL */
428} langp_T;
429
430#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
431
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000432#define REGION_ALL 0xff /* word valid in all regions */
433
Bram Moolenaar5195e452005-08-19 20:32:47 +0000434#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
435#define VIMSPELLMAGICL 8
436#define VIMSPELLVERSION 50
437
438/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
439#define SN_REGION 0 /* <regionname> section */
440#define SN_CHARFLAGS 1 /* charflags section */
441#define SN_MIDWORD 2 /* <midword> section */
442#define SN_PREFCOND 3 /* <prefcond> section */
443#define SN_REP 4 /* REP items section */
444#define SN_SAL 5 /* SAL items section */
445#define SN_SOFO 6 /* soundfolding section */
446#define SN_MAP 7 /* MAP items section */
447#define SN_COMPOUND 8 /* compound words section */
448#define SN_SYLLABLE 9 /* syllable section */
Bram Moolenaar78622822005-08-23 21:00:13 +0000449#define SN_NOBREAK 10 /* NOBREAK section */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000450#define SN_END 255 /* end of sections */
451
452#define SNF_REQUIRED 1 /* <sectionflags>: required section */
453
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000454/* Result values. Lower number is accepted over higher one. */
455#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000456#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000457#define SP_RARE 1
458#define SP_LOCAL 2
459#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000460
Bram Moolenaar7887d882005-07-01 22:33:52 +0000461/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000462static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000463
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000464/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000465 * Information used when looking for suggestions.
466 */
467typedef struct suginfo_S
468{
469 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000470 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000471 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000472 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000473 char_u *su_badptr; /* start of bad word in line */
474 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000475 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000476 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
477 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
478 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000479} suginfo_T;
480
481/* One word suggestion. Used in "si_ga". */
482typedef struct suggest_S
483{
484 char_u *st_word; /* suggested word, allocated string */
485 int st_orglen; /* length of replaced text */
486 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000487 int st_altscore; /* used when st_score compares equal */
488 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000489 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000490} suggest_T;
491
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000492#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000493
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000494/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
495 * called the score may change, thus we need to keep more than what is
496 * displayed. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000497#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 50 ? 50 : (su)->su_maxcount)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000498
499/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
500 * of suggestions that are not going to be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000501#define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000502
503/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000504#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000505#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000506#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000507#define SCORE_RARE 180 /* rare word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000508#define SCORE_SWAP 90 /* swap two characters */
509#define SCORE_SWAP3 110 /* swap two characters in three */
510#define SCORE_REP 87 /* REP replacement */
511#define SCORE_SUBST 93 /* substitute a character */
512#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000513#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000514#define SCORE_DEL 94 /* delete a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000515#define SCORE_DELDUP 64 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000516#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000517#define SCORE_INS 96 /* insert a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000518#define SCORE_INSDUP 66 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000519#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000520#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000521
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000522#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000523#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
524 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000525
526#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000527#define SCORE_MAXMAX 999999 /* accept any score */
528
529/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000530 * Structure to store info for word matching.
531 */
532typedef struct matchinf_S
533{
534 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000535
536 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000537 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000538 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000539 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000540 char_u *mi_cend; /* char after what was used for
541 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000542
543 /* case-folded text */
544 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000545 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000546
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000547 /* for when checking word after a prefix */
548 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000549 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000550 int mi_prefcnt; /* number of entries at mi_prefarridx */
551 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000552#ifdef FEAT_MBYTE
553 int mi_cprefixlen; /* byte length of prefix in original
554 case */
555#else
556# define mi_cprefixlen mi_prefixlen /* it's the same value */
557#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000558
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000559 /* for when checking a compound word */
560 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000561 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
562 int mi_complen; /* nr of compound words used */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000563
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000564 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000565 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000566 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000567 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000568
569 /* for NOBREAK */
570 int mi_result2; /* "mi_resul" without following word */
571 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000572} matchinf_T;
573
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000574/*
575 * The tables used for recognizing word characters according to spelling.
576 * These are only used for the first 256 characters of 'encoding'.
577 */
578typedef struct spelltab_S
579{
580 char_u st_isw[256]; /* flags: is word char */
581 char_u st_isu[256]; /* flags: is uppercase char */
582 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000583 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000584} spelltab_T;
585
586static spelltab_T spelltab;
587static int did_set_spelltab;
588
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000589#define CF_WORD 0x01
590#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000591
592static void clear_spell_chartab __ARGS((spelltab_T *sp));
593static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000594static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
595static int spell_iswordp_nmw __ARGS((char_u *p));
596#ifdef FEAT_MBYTE
597static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
598#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000599static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000600
601/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000602 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000603 */
604typedef enum
605{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000606 STATE_START = 0, /* At start of node check for NUL bytes (goodword
607 * ends); if badword ends there is a match, otherwise
608 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000609 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000610 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000611 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
612 STATE_PLAIN, /* Use each byte of the node. */
613 STATE_DEL, /* Delete a byte from the bad word. */
614 STATE_INS, /* Insert a byte in the bad word. */
615 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000616 STATE_UNSWAP, /* Undo swap two characters. */
617 STATE_SWAP3, /* Swap two characters over three. */
618 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000619 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000620 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000621 STATE_REP_INI, /* Prepare for using REP items. */
622 STATE_REP, /* Use matching REP items from the .aff file. */
623 STATE_REP_UNDO, /* Undo a REP item replacement. */
624 STATE_FINAL /* End of this node. */
625} state_T;
626
627/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000628 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000629 */
630typedef struct trystate_S
631{
Bram Moolenaarea424162005-06-16 21:51:00 +0000632 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000633 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000634 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000635 short ts_curi; /* index in list of child nodes */
636 char_u ts_fidx; /* index in fword[], case-folded bad word */
637 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
638 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000639 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000640 * PFD_PREFIXTREE or PFD_NOPREFIX */
641 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000642#ifdef FEAT_MBYTE
643 char_u ts_tcharlen; /* number of bytes in tword character */
644 char_u ts_tcharidx; /* current byte index in tword character */
645 char_u ts_isdiff; /* DIFF_ values */
646 char_u ts_fcharstart; /* index in fword where badword char started */
647#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000648 char_u ts_prewordlen; /* length of word in "preword[]" */
649 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000650 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000651 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000652 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000653 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000654} trystate_T;
655
Bram Moolenaarea424162005-06-16 21:51:00 +0000656/* values for ts_isdiff */
657#define DIFF_NONE 0 /* no different byte (yet) */
658#define DIFF_YES 1 /* different byte found */
659#define DIFF_INSERT 2 /* inserting character */
660
Bram Moolenaard12a1322005-08-21 22:08:24 +0000661/* values for ts_flags */
662#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
663#define TSF_DIDSPLIT 2 /* tried split at this point */
664
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000665/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000666#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000667#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
668#define PFD_NOTSPECIAL 0xfd /* first value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000669
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000670/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000671#define FIND_FOLDWORD 0 /* find word case-folded */
672#define FIND_KEEPWORD 1 /* find keep-case word */
673#define FIND_PREFIX 2 /* find word after prefix */
674#define FIND_COMPOUND 3 /* find case-folded compound word */
675#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000676
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000677static slang_T *slang_alloc __ARGS((char_u *lang));
678static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000679static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000680static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000681static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000682static 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 +0000683static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000684static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000685static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000686static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000687static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000688static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000689static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000690static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000691static 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 +0000692static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000693static char_u *read_string __ARGS((FILE *fd, int cnt));
694static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
695static int read_charflags_section __ARGS((FILE *fd));
696static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
697static int read_rep_section __ARGS((FILE *fd, slang_T *slang));
698static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
699static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
700static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000701static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000702static int init_syl_tab __ARGS((slang_T *slang));
703static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000704static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
705static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000706#ifdef FEAT_MBYTE
707static int *mb_str2wide __ARGS((char_u *s));
708#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000709static 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 +0000710static void clear_midword __ARGS((buf_T *buf));
711static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000712static int find_region __ARGS((char_u *rp, char_u *region));
713static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000714static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000715static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000716static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000717static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000718static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000719static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +0000720static 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 +0000721#ifdef FEAT_EVAL
722static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
723#endif
724static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
725static void spell_suggest_intern __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000726static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000727static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000728static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000729static void suggest_try_special __ARGS((suginfo_T *su));
730static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000731static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000732#ifdef FEAT_MBYTE
733static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
734#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000735static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000736static void score_comp_sal __ARGS((suginfo_T *su));
737static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000738static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000739static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000740static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000741static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000742static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000743static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000744static void add_banned __ARGS((suginfo_T *su, char_u *word));
745static int was_banned __ARGS((suginfo_T *su, char_u *word));
746static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000747static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000748static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000749static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
750static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
751static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000752#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000753static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000754#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000755static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000756static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000757static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000758static 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 +0000759
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000760/*
761 * Use our own character-case definitions, because the current locale may
762 * differ from what the .spl file uses.
763 * These must not be called with negative number!
764 */
765#ifndef FEAT_MBYTE
766/* Non-multi-byte implementation. */
767# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
768# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
769# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
770#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000771# if defined(HAVE_WCHAR_H)
772# include <wchar.h> /* for towupper() and towlower() */
773# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000774/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
775 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
776 * the "w" library function for characters above 255 if available. */
777# ifdef HAVE_TOWLOWER
778# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
779 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
780# else
781# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
782 : (c) < 256 ? spelltab.st_fold[c] : (c))
783# endif
784
785# ifdef HAVE_TOWUPPER
786# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
787 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
788# else
789# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
790 : (c) < 256 ? spelltab.st_upper[c] : (c))
791# endif
792
793# ifdef HAVE_ISWUPPER
794# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
795 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
796# else
797# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000798 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000799# endif
800#endif
801
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000802
803static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000804static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000805static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000806static char *e_affname = N_("Affix name too long in %s line %d: %s");
807static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
808static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000809static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000810
811/*
812 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000813 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000814 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
815 * or when it's OK it remains unchanged.
816 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000817 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000818 * "capcol" is used to check for a Capitalised word after the end of a
819 * sentence. If it's zero then perform the check. Return the column where to
820 * check next, or -1 when no sentence end was found. If it's NULL then don't
821 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000822 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000823 * Returns the length of the word in bytes, also when it's OK, so that the
824 * caller can skip over the word.
825 */
826 int
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000827spell_check(wp, ptr, attrp, capcol)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000828 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000829 char_u *ptr;
830 int *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000831 int *capcol; /* column to check for Capital */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000832{
833 matchinf_T mi; /* Most things are put in "mi" so that it can
834 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000835 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000836 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000837 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000838 int lpi;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000839
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000840 /* A word never starts at a space or a control character. Return quickly
841 * then, skipping over the character. */
842 if (*ptr <= ' ')
843 return 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000844 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000845
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000846 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000847 * 0X99FF. But when a word character follows do check spelling to find
848 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000849 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000850 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000851 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
852 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000853 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000854 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000855 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000856 nrlen = mi.mi_end - ptr;
857 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000858 if (!spell_iswordp(mi.mi_end, wp->w_buffer))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000859 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000860
861 /* Try including the digits in the word. */
862 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000863 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000864 else
865 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000866
Bram Moolenaar0c405862005-06-22 22:26:26 +0000867 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000868 mi.mi_word = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000869 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000870 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000871 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000872 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000873 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000874 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000875
876 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
877 {
878 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000879 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000880 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000881 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000882 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000883 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000884 if (capcol != NULL)
885 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000886
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000887 /* We always use the characters up to the next non-word character,
888 * also for bad words. */
889 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000890
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000891 /* Check caps type later. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000892 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000893
Bram Moolenaar5195e452005-08-19 20:32:47 +0000894 /* case-fold the word with one non-word character, so that we can check
895 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000896 if (*mi.mi_fend != NUL)
897 mb_ptr_adv(mi.mi_fend);
898
899 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
900 MAXWLEN + 1);
901 mi.mi_fwordlen = STRLEN(mi.mi_fword);
902
903 /* The word is bad unless we recognize it. */
904 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +0000905 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000906
907 /*
908 * Loop over the languages specified in 'spelllang'.
909 * We check them all, because a matching word may be longer than an
910 * already found matching word.
911 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000912 for (lpi = 0; lpi < wp->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000913 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000914 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, lpi);
915
916 /* If reloading fails the language is still in the list but everything
917 * has been cleared. */
918 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
919 continue;
920
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000921 /* Check for a matching word in case-folded words. */
922 find_word(&mi, FIND_FOLDWORD);
923
924 /* Check for a matching word in keep-case words. */
925 find_word(&mi, FIND_KEEPWORD);
926
927 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000928 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +0000929
930 /* For a NOBREAK language, may want to use a word without a following
931 * word as a backup. */
932 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
933 && mi.mi_result2 != SP_BAD)
934 {
935 mi.mi_result = mi.mi_result2;
936 mi.mi_end = mi.mi_end2;
937 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000938 }
939
940 if (mi.mi_result != SP_OK)
941 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000942 /* If we found a number skip over it. Allows for "42nd". Do flag
943 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000944 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000945 {
946 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
947 return nrlen;
948 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000949
950 /* When we are at a non-word character there is no error, just
951 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000952 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000953 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000954 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
955 {
956 regmatch_T regmatch;
957
958 /* Check for end of sentence. */
959 regmatch.regprog = wp->w_buffer->b_cap_prog;
960 regmatch.rm_ic = FALSE;
961 if (vim_regexec(&regmatch, ptr, 0))
962 *capcol = (int)(regmatch.endp[0] - ptr);
963 }
964
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000965#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000966 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000967 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000968#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000969 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000970 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000971 else if (mi.mi_end == ptr)
972 /* Always include at least one character. Required for when there
973 * is a mixup in "midword". */
974 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +0000975 else if (mi.mi_result == SP_BAD
976 && LANGP_ENTRY(wp->w_buffer->b_langp, 0)->lp_slang->sl_nobreak)
977 {
978 char_u *p, *fp;
979 int save_result = mi.mi_result;
980
981 /* First language in 'spelllang' is NOBREAK. Find first position
982 * at which any word would be valid. */
983 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000984 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +0000985 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000986 p = mi.mi_word;
987 fp = mi.mi_fword;
988 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +0000989 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000990 mb_ptr_adv(p);
991 mb_ptr_adv(fp);
992 if (p >= mi.mi_end)
993 break;
994 mi.mi_compoff = fp - mi.mi_fword;
995 find_word(&mi, FIND_COMPOUND);
996 if (mi.mi_result != SP_BAD)
997 {
998 mi.mi_end = p;
999 break;
1000 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001001 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001002 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001003 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001004 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001005
1006 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1007 *attrp = highlight_attr[HLF_SPB];
1008 else if (mi.mi_result == SP_RARE)
1009 *attrp = highlight_attr[HLF_SPR];
1010 else
1011 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001012 }
1013
Bram Moolenaar5195e452005-08-19 20:32:47 +00001014 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1015 {
1016 /* Report SpellCap only when the word isn't badly spelled. */
1017 *attrp = highlight_attr[HLF_SPC];
1018 return wrongcaplen;
1019 }
1020
Bram Moolenaar51485f02005-06-04 21:55:20 +00001021 return (int)(mi.mi_end - ptr);
1022}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001023
Bram Moolenaar51485f02005-06-04 21:55:20 +00001024/*
1025 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001026 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1027 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1028 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1029 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001030 *
1031 * For a match mip->mi_result is updated.
1032 */
1033 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001034find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001035 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001036 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001037{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001038 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001039 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001040 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001041 int endidxcnt = 0;
1042 int len;
1043 int wlen = 0;
1044 int flen;
1045 int c;
1046 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001047 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001048#ifdef FEAT_MBYTE
1049 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001050#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001051 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001052 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001053 slang_T *slang = mip->mi_lp->lp_slang;
1054 unsigned flags;
1055 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001056 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001057 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001058 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001059 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001060
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001061 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001062 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001063 /* Check for word with matching case in keep-case tree. */
1064 ptr = mip->mi_word;
1065 flen = 9999; /* no case folding, always enough bytes */
1066 byts = slang->sl_kbyts;
1067 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001068
1069 if (mode == FIND_KEEPCOMPOUND)
1070 /* Skip over the previously found word(s). */
1071 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001072 }
1073 else
1074 {
1075 /* Check for case-folded in case-folded tree. */
1076 ptr = mip->mi_fword;
1077 flen = mip->mi_fwordlen; /* available case-folded bytes */
1078 byts = slang->sl_fbyts;
1079 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001080
1081 if (mode == FIND_PREFIX)
1082 {
1083 /* Skip over the prefix. */
1084 wlen = mip->mi_prefixlen;
1085 flen -= mip->mi_prefixlen;
1086 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001087 else if (mode == FIND_COMPOUND)
1088 {
1089 /* Skip over the previously found word(s). */
1090 wlen = mip->mi_compoff;
1091 flen -= mip->mi_compoff;
1092 }
1093
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001094 }
1095
Bram Moolenaar51485f02005-06-04 21:55:20 +00001096 if (byts == NULL)
1097 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001098
Bram Moolenaar51485f02005-06-04 21:55:20 +00001099 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001100 * Repeat advancing in the tree until:
1101 * - there is a byte that doesn't match,
1102 * - we reach the end of the tree,
1103 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001104 */
1105 for (;;)
1106 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001107 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001108 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001109
1110 len = byts[arridx++];
1111
1112 /* If the first possible byte is a zero the word could end here.
1113 * Remember this index, we first check for the longest word. */
1114 if (byts[arridx] == 0)
1115 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001116 if (endidxcnt == MAXWLEN)
1117 {
1118 /* Must be a corrupted spell file. */
1119 EMSG(_(e_format));
1120 return;
1121 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001122 endlen[endidxcnt] = wlen;
1123 endidx[endidxcnt++] = arridx++;
1124 --len;
1125
1126 /* Skip over the zeros, there can be several flag/region
1127 * combinations. */
1128 while (len > 0 && byts[arridx] == 0)
1129 {
1130 ++arridx;
1131 --len;
1132 }
1133 if (len == 0)
1134 break; /* no children, word must end here */
1135 }
1136
1137 /* Stop looking at end of the line. */
1138 if (ptr[wlen] == NUL)
1139 break;
1140
1141 /* Perform a binary search in the list of accepted bytes. */
1142 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001143 if (c == TAB) /* <Tab> is handled like <Space> */
1144 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001145 lo = arridx;
1146 hi = arridx + len - 1;
1147 while (lo < hi)
1148 {
1149 m = (lo + hi) / 2;
1150 if (byts[m] > c)
1151 hi = m - 1;
1152 else if (byts[m] < c)
1153 lo = m + 1;
1154 else
1155 {
1156 lo = hi = m;
1157 break;
1158 }
1159 }
1160
1161 /* Stop if there is no matching byte. */
1162 if (hi < lo || byts[lo] != c)
1163 break;
1164
1165 /* Continue at the child (if there is one). */
1166 arridx = idxs[lo];
1167 ++wlen;
1168 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001169
1170 /* One space in the good word may stand for several spaces in the
1171 * checked word. */
1172 if (c == ' ')
1173 {
1174 for (;;)
1175 {
1176 if (flen <= 0 && *mip->mi_fend != NUL)
1177 flen = fold_more(mip);
1178 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1179 break;
1180 ++wlen;
1181 --flen;
1182 }
1183 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001184 }
1185
1186 /*
1187 * Verify that one of the possible endings is valid. Try the longest
1188 * first.
1189 */
1190 while (endidxcnt > 0)
1191 {
1192 --endidxcnt;
1193 arridx = endidx[endidxcnt];
1194 wlen = endlen[endidxcnt];
1195
1196#ifdef FEAT_MBYTE
1197 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1198 continue; /* not at first byte of character */
1199#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001200 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001201 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001202 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001203 continue; /* next char is a word character */
1204 word_ends = FALSE;
1205 }
1206 else
1207 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001208 /* The prefix flag is before compound flags. Once a valid prefix flag
1209 * has been found we try compound flags. */
1210 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001211
1212#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001213 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001214 {
1215 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001216 * when folding case. This can be slow, take a shortcut when the
1217 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001218 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001219 if (STRNCMP(ptr, p, wlen) != 0)
1220 {
1221 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1222 mb_ptr_adv(p);
1223 wlen = p - mip->mi_word;
1224 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001225 }
1226#endif
1227
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001228 /* Check flags and region. For FIND_PREFIX check the condition and
1229 * prefix ID.
1230 * Repeat this if there are more flags/region alternatives until there
1231 * is a match. */
1232 res = SP_BAD;
1233 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1234 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001235 {
1236 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001237
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001238 /* For the fold-case tree check that the case of the checked word
1239 * matches with what the word in the tree requires.
1240 * For keep-case tree the case is always right. For prefixes we
1241 * don't bother to check. */
1242 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001243 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001244 if (mip->mi_cend != mip->mi_word + wlen)
1245 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001246 /* mi_capflags was set for a different word length, need
1247 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001248 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001249 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001250 }
1251
Bram Moolenaar0c405862005-06-22 22:26:26 +00001252 if (mip->mi_capflags == WF_KEEPCAP
1253 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001254 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001255 }
1256
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001257 /* When mode is FIND_PREFIX the word must support the prefix:
1258 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001259 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001260 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001261 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001262 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001263 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001264 mip->mi_word + mip->mi_cprefixlen, slang,
1265 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001266 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001267 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001268
1269 /* Use the WF_RARE flag for a rare prefix. */
1270 if (c & WF_RAREPFX)
1271 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001272 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001273 }
1274
Bram Moolenaar78622822005-08-23 21:00:13 +00001275 if (slang->sl_nobreak)
1276 {
1277 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1278 && (flags & WF_BANNED) == 0)
1279 {
1280 /* NOBREAK: found a valid following word. That's all we
1281 * need to know, so return. */
1282 mip->mi_result = SP_OK;
1283 break;
1284 }
1285 }
1286
1287 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1288 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001289 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00001290 /* If there is no flag or the word is shorter than
1291 * COMPOUNDMIN reject it quickly.
1292 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001293 * that's too short... Myspell compatibility requires this
1294 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001295 if (((unsigned)flags >> 24) == 0
1296 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001297 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001298#ifdef FEAT_MBYTE
1299 /* For multi-byte chars check character length against
1300 * COMPOUNDMIN. */
1301 if (has_mbyte
1302 && slang->sl_compminlen < MAXWLEN
1303 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1304 wlen - mip->mi_compoff) < slang->sl_compminlen)
1305 continue;
1306#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001307
Bram Moolenaare52325c2005-08-22 22:54:29 +00001308 /* Limit the number of compound words to COMPOUNDMAX if no
1309 * maximum for syllables is specified. */
1310 if (!word_ends && mip->mi_complen + 2 > slang->sl_compmax
1311 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001312 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001313
Bram Moolenaard12a1322005-08-21 22:08:24 +00001314 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001315 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001316 ? slang->sl_compstartflags
1317 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001318 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001319 continue;
1320
Bram Moolenaare52325c2005-08-22 22:54:29 +00001321 if (mode == FIND_COMPOUND)
1322 {
1323 int capflags;
1324
1325 /* Need to check the caps type of the appended compound
1326 * word. */
1327#ifdef FEAT_MBYTE
1328 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1329 mip->mi_compoff) != 0)
1330 {
1331 /* case folding may have changed the length */
1332 p = mip->mi_word;
1333 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1334 mb_ptr_adv(p);
1335 }
1336 else
1337#endif
1338 p = mip->mi_word + mip->mi_compoff;
1339 capflags = captype(p, mip->mi_word + wlen);
1340 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1341 && (flags & WF_FIXCAP) != 0))
1342 continue;
1343
1344 if (capflags != WF_ALLCAP)
1345 {
1346 /* When the character before the word is a word
1347 * character we do not accept a Onecap word. We do
1348 * accept a no-caps word, even when the dictionary
1349 * word specifies ONECAP. */
1350 mb_ptr_back(mip->mi_word, p);
1351 if (spell_iswordp_nmw(p)
1352 ? capflags == WF_ONECAP
1353 : (flags & WF_ONECAP) != 0
1354 && capflags != WF_ONECAP)
1355 continue;
1356 }
1357 }
1358
Bram Moolenaar5195e452005-08-19 20:32:47 +00001359 /* If the word ends the sequence of compound flags of the
1360 * words must match with one of the COMPOUNDFLAGS items and
1361 * the number of syllables must not be too large. */
1362 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1363 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1364 if (word_ends)
1365 {
1366 char_u fword[MAXWLEN];
1367
1368 if (slang->sl_compsylmax < MAXWLEN)
1369 {
1370 /* "fword" is only needed for checking syllables. */
1371 if (ptr == mip->mi_word)
1372 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1373 else
1374 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1375 }
1376 if (!can_compound(slang, fword, mip->mi_compflags))
1377 continue;
1378 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001379 }
1380
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001381 /* Check NEEDCOMPOUND: can't use word without compounding. */
1382 else if (flags & WF_NEEDCOMP)
1383 continue;
1384
Bram Moolenaar78622822005-08-23 21:00:13 +00001385 nobreak_result = SP_OK;
1386
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001387 if (!word_ends)
1388 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001389 int save_result = mip->mi_result;
1390 char_u *save_end = mip->mi_end;
1391
1392 /* Check that a valid word follows. If there is one and we
1393 * are compounding, it will set "mi_result", thus we are
1394 * always finished here. For NOBREAK we only check that a
1395 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001396 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001397 if (slang->sl_nobreak)
1398 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001399
1400 /* Find following word in case-folded tree. */
1401 mip->mi_compoff = endlen[endidxcnt];
1402#ifdef FEAT_MBYTE
1403 if (has_mbyte && mode == FIND_KEEPWORD)
1404 {
1405 /* Compute byte length in case-folded word from "wlen":
1406 * byte length in keep-case word. Length may change when
1407 * folding case. This can be slow, take a shortcut when
1408 * the case-folded word is equal to the keep-case word. */
1409 p = mip->mi_fword;
1410 if (STRNCMP(ptr, p, wlen) != 0)
1411 {
1412 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1413 mb_ptr_adv(p);
1414 mip->mi_compoff = p - mip->mi_fword;
1415 }
1416 }
1417#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001418 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001419 ++mip->mi_complen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001420 find_word(mip, FIND_COMPOUND);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001421
Bram Moolenaar78622822005-08-23 21:00:13 +00001422 /* When NOBREAK any word that matches is OK. Otherwise we
1423 * need to find the longest match, thus try with keep-case and
1424 * prefix too. */
1425 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1426 {
1427 /* Find following word in keep-case tree. */
1428 mip->mi_compoff = wlen;
1429 find_word(mip, FIND_KEEPCOMPOUND);
Bram Moolenaard12a1322005-08-21 22:08:24 +00001430
Bram Moolenaar78622822005-08-23 21:00:13 +00001431 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1432 {
1433 /* Check for following word with prefix. */
1434 mip->mi_compoff = c;
1435 find_prefix(mip, FIND_COMPOUND);
1436 }
1437 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001438 --mip->mi_complen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001439
Bram Moolenaar78622822005-08-23 21:00:13 +00001440 if (slang->sl_nobreak)
1441 {
1442 nobreak_result = mip->mi_result;
1443 mip->mi_result = save_result;
1444 mip->mi_end = save_end;
1445 }
1446 else
1447 {
1448 if (mip->mi_result == SP_OK)
1449 break;
1450 continue;
1451 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001452 }
1453
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001454 if (flags & WF_BANNED)
1455 res = SP_BANNED;
1456 else if (flags & WF_REGION)
1457 {
1458 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001459 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001460 res = SP_OK;
1461 else
1462 res = SP_LOCAL;
1463 }
1464 else if (flags & WF_RARE)
1465 res = SP_RARE;
1466 else
1467 res = SP_OK;
1468
Bram Moolenaar78622822005-08-23 21:00:13 +00001469 /* Always use the longest match and the best result. For NOBREAK
1470 * we separately keep the longest match without a following good
1471 * word as a fall-back. */
1472 if (nobreak_result == SP_BAD)
1473 {
1474 if (mip->mi_result2 > res)
1475 {
1476 mip->mi_result2 = res;
1477 mip->mi_end2 = mip->mi_word + wlen;
1478 }
1479 else if (mip->mi_result2 == res
1480 && mip->mi_end2 < mip->mi_word + wlen)
1481 mip->mi_end2 = mip->mi_word + wlen;
1482 }
1483 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001484 {
1485 mip->mi_result = res;
1486 mip->mi_end = mip->mi_word + wlen;
1487 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001488 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001489 mip->mi_end = mip->mi_word + wlen;
1490
Bram Moolenaar78622822005-08-23 21:00:13 +00001491 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001492 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001493 }
1494
Bram Moolenaar78622822005-08-23 21:00:13 +00001495 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001496 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001497 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001498}
1499
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001500/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00001501 * Return TRUE if "flags" is a valid sequence of compound flags and
1502 * "word[len]" does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001503 */
1504 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001505can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001506 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001507 char_u *word;
1508 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001509{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001510 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001511#ifdef FEAT_MBYTE
1512 char_u uflags[MAXWLEN * 2];
1513 int i;
1514#endif
1515 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001516
1517 if (slang->sl_compprog == NULL)
1518 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001519#ifdef FEAT_MBYTE
1520 if (enc_utf8)
1521 {
1522 /* Need to convert the single byte flags to utf8 characters. */
1523 p = uflags;
1524 for (i = 0; flags[i] != NUL; ++i)
1525 p += mb_char2bytes(flags[i], p);
1526 *p = NUL;
1527 p = uflags;
1528 }
1529 else
1530#endif
1531 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001532 regmatch.regprog = slang->sl_compprog;
1533 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001534 if (!vim_regexec(&regmatch, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001535 return FALSE;
1536
Bram Moolenaare52325c2005-08-22 22:54:29 +00001537 /* Count the number of syllables. This may be slow, do it last. If there
1538 * are too many syllables AND the number of compound words is above
1539 * COMPOUNDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001540 if (slang->sl_compsylmax < MAXWLEN
1541 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001542 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001543 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001544}
1545
1546/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001547 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1548 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001549 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001550 */
1551 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001552valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001553 int totprefcnt; /* nr of prefix IDs */
1554 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001555 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001556 char_u *word;
1557 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001558 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001559{
1560 int prefcnt;
1561 int pidx;
1562 regprog_T *rp;
1563 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001564 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001565
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001566 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001567 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1568 {
1569 pidx = slang->sl_pidxs[arridx + prefcnt];
1570
1571 /* Check the prefix ID. */
1572 if (prefid != (pidx & 0xff))
1573 continue;
1574
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001575 /* Check if the prefix doesn't combine and the word already has a
1576 * suffix. */
1577 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1578 continue;
1579
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001580 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001581 * stored in the two bytes above the prefix ID byte. */
1582 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001583 if (rp != NULL)
1584 {
1585 regmatch.regprog = rp;
1586 regmatch.rm_ic = FALSE;
1587 if (!vim_regexec(&regmatch, word, 0))
1588 continue;
1589 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001590 else if (cond_req)
1591 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001592
Bram Moolenaar53805d12005-08-01 07:08:33 +00001593 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001594 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001595 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001596 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001597}
1598
1599/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001600 * Check if the word at "mip->mi_word" has a matching prefix.
1601 * If it does, then check the following word.
1602 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001603 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1604 * prefix in a compound word.
1605 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001606 * For a match mip->mi_result is updated.
1607 */
1608 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001609find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001610 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001611 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001612{
1613 idx_T arridx = 0;
1614 int len;
1615 int wlen = 0;
1616 int flen;
1617 int c;
1618 char_u *ptr;
1619 idx_T lo, hi, m;
1620 slang_T *slang = mip->mi_lp->lp_slang;
1621 char_u *byts;
1622 idx_T *idxs;
1623
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001624 byts = slang->sl_pbyts;
1625 if (byts == NULL)
1626 return; /* array is empty */
1627
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001628 /* We use the case-folded word here, since prefixes are always
1629 * case-folded. */
1630 ptr = mip->mi_fword;
1631 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001632 if (mode == FIND_COMPOUND)
1633 {
1634 /* Skip over the previously found word(s). */
1635 ptr += mip->mi_compoff;
1636 flen -= mip->mi_compoff;
1637 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001638 idxs = slang->sl_pidxs;
1639
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001640 /*
1641 * Repeat advancing in the tree until:
1642 * - there is a byte that doesn't match,
1643 * - we reach the end of the tree,
1644 * - or we reach the end of the line.
1645 */
1646 for (;;)
1647 {
1648 if (flen == 0 && *mip->mi_fend != NUL)
1649 flen = fold_more(mip);
1650
1651 len = byts[arridx++];
1652
1653 /* If the first possible byte is a zero the prefix could end here.
1654 * Check if the following word matches and supports the prefix. */
1655 if (byts[arridx] == 0)
1656 {
1657 /* There can be several prefixes with different conditions. We
1658 * try them all, since we don't know which one will give the
1659 * longest match. The word is the same each time, pass the list
1660 * of possible prefixes to find_word(). */
1661 mip->mi_prefarridx = arridx;
1662 mip->mi_prefcnt = len;
1663 while (len > 0 && byts[arridx] == 0)
1664 {
1665 ++arridx;
1666 --len;
1667 }
1668 mip->mi_prefcnt -= len;
1669
1670 /* Find the word that comes after the prefix. */
1671 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001672 if (mode == FIND_COMPOUND)
1673 /* Skip over the previously found word(s). */
1674 mip->mi_prefixlen += mip->mi_compoff;
1675
Bram Moolenaar53805d12005-08-01 07:08:33 +00001676#ifdef FEAT_MBYTE
1677 if (has_mbyte)
1678 {
1679 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001680 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1681 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001682 }
1683 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001684 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001685#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001686 find_word(mip, FIND_PREFIX);
1687
1688
1689 if (len == 0)
1690 break; /* no children, word must end here */
1691 }
1692
1693 /* Stop looking at end of the line. */
1694 if (ptr[wlen] == NUL)
1695 break;
1696
1697 /* Perform a binary search in the list of accepted bytes. */
1698 c = ptr[wlen];
1699 lo = arridx;
1700 hi = arridx + len - 1;
1701 while (lo < hi)
1702 {
1703 m = (lo + hi) / 2;
1704 if (byts[m] > c)
1705 hi = m - 1;
1706 else if (byts[m] < c)
1707 lo = m + 1;
1708 else
1709 {
1710 lo = hi = m;
1711 break;
1712 }
1713 }
1714
1715 /* Stop if there is no matching byte. */
1716 if (hi < lo || byts[lo] != c)
1717 break;
1718
1719 /* Continue at the child (if there is one). */
1720 arridx = idxs[lo];
1721 ++wlen;
1722 --flen;
1723 }
1724}
1725
1726/*
1727 * Need to fold at least one more character. Do until next non-word character
1728 * for efficiency.
1729 * Return the length of the folded chars in bytes.
1730 */
1731 static int
1732fold_more(mip)
1733 matchinf_T *mip;
1734{
1735 int flen;
1736 char_u *p;
1737
1738 p = mip->mi_fend;
1739 do
1740 {
1741 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001742 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001743
1744 /* Include the non-word character so that we can check for the
1745 * word end. */
1746 if (*mip->mi_fend != NUL)
1747 mb_ptr_adv(mip->mi_fend);
1748
1749 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1750 mip->mi_fword + mip->mi_fwordlen,
1751 MAXWLEN - mip->mi_fwordlen);
1752 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1753 mip->mi_fwordlen += flen;
1754 return flen;
1755}
1756
1757/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001758 * Check case flags for a word. Return TRUE if the word has the requested
1759 * case.
1760 */
1761 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001762spell_valid_case(wordflags, treeflags)
1763 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001764 int treeflags; /* flags for the word in the spell tree */
1765{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001766 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001767 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001768 && ((treeflags & WF_ONECAP) == 0
1769 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001770}
1771
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001772/*
1773 * Return TRUE if spell checking is not enabled.
1774 */
1775 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00001776no_spell_checking(wp)
1777 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001778{
Bram Moolenaar95529562005-08-25 21:21:38 +00001779 if (!wp->w_p_spell || *wp->w_buffer->b_p_spl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001780 {
1781 EMSG(_("E756: Spell checking is not enabled"));
1782 return TRUE;
1783 }
1784 return FALSE;
1785}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001786
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001787/*
1788 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001789 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
1790 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001791 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1792 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001793 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001794 */
1795 int
Bram Moolenaar95529562005-08-25 21:21:38 +00001796spell_move_to(wp, dir, allwords, curline, attrp)
1797 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001798 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001799 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001800 int curline;
Bram Moolenaar95529562005-08-25 21:21:38 +00001801 int *attrp; /* return: attributes of bad word or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001802{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001803 linenr_T lnum;
1804 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001805 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001806 char_u *line;
1807 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001808 char_u *endp;
1809 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001810 int len;
Bram Moolenaar95529562005-08-25 21:21:38 +00001811 int has_syntax = syntax_present(wp->w_buffer);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001812 int col;
1813 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001814 char_u *buf = NULL;
1815 int buflen = 0;
1816 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001817 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001818 int found_one = FALSE;
1819 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001820
Bram Moolenaar95529562005-08-25 21:21:38 +00001821 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00001822 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001823
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001824 /*
1825 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001826 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001827 *
1828 * When searching backwards, we continue in the line to find the last
1829 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001830 *
1831 * We concatenate the start of the next line, so that wrapped words work
1832 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1833 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001834 */
Bram Moolenaar95529562005-08-25 21:21:38 +00001835 lnum = wp->w_cursor.lnum;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001836 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001837
1838 while (!got_int)
1839 {
Bram Moolenaar95529562005-08-25 21:21:38 +00001840 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001841
Bram Moolenaar0c405862005-06-22 22:26:26 +00001842 len = STRLEN(line);
1843 if (buflen < len + MAXWLEN + 2)
1844 {
1845 vim_free(buf);
1846 buflen = len + MAXWLEN + 2;
1847 buf = alloc(buflen);
1848 if (buf == NULL)
1849 break;
1850 }
1851
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001852 /* In first line check first word for Capital. */
1853 if (lnum == 1)
1854 capcol = 0;
1855
1856 /* For checking first word with a capital skip white space. */
1857 if (capcol == 0)
1858 capcol = skipwhite(line) - line;
1859
Bram Moolenaar0c405862005-06-22 22:26:26 +00001860 /* Copy the line into "buf" and append the start of the next line if
1861 * possible. */
1862 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00001863 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001864 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1865
1866 p = buf + skip;
1867 endp = buf + len;
1868 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001869 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001870 /* When searching backward don't search after the cursor. Unless
1871 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001872 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00001873 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001874 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00001875 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001876 break;
1877
1878 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001879 attr = 0;
Bram Moolenaar95529562005-08-25 21:21:38 +00001880 len = spell_check(wp, p, &attr, &capcol);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001881
1882 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001883 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001884 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001885 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001886 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001887 found_one = TRUE;
1888
Bram Moolenaar51485f02005-06-04 21:55:20 +00001889 /* When searching forward only accept a bad word after
1890 * the cursor. */
1891 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001892 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00001893 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001894 && (wrapped
1895 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00001896 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001897 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001898 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001899 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001900 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001901 col = p - buf;
Bram Moolenaar95529562005-08-25 21:21:38 +00001902 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar51485f02005-06-04 21:55:20 +00001903 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001904 }
1905 else
1906 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001907
Bram Moolenaar51485f02005-06-04 21:55:20 +00001908 if (can_spell)
1909 {
1910 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001911 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001912#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001913 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001914#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001915 if (dir == FORWARD)
1916 {
1917 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00001918 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001919 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001920 if (attrp != NULL)
1921 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001922 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001923 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001924 else if (curline)
1925 /* Insert mode completion: put cursor after
1926 * the bad word. */
1927 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001928 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001929 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001930 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001931 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001932 }
1933
Bram Moolenaar51485f02005-06-04 21:55:20 +00001934 /* advance to character after the word */
1935 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001936 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001937 }
1938
Bram Moolenaar5195e452005-08-19 20:32:47 +00001939 if (dir == BACKWARD && found_pos.lnum != 0)
1940 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001941 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00001942 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001943 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001944 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001945 }
1946
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001947 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001948 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001949
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001950 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001951 if (dir == BACKWARD)
1952 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001953 /* If we are back at the starting line and searched it again there
1954 * is no match, give up. */
1955 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001956 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001957
1958 if (lnum > 1)
1959 --lnum;
1960 else if (!p_ws)
1961 break; /* at first line and 'nowrapscan' */
1962 else
1963 {
1964 /* Wrap around to the end of the buffer. May search the
1965 * starting line again and accept the last match. */
1966 lnum = wp->w_buffer->b_ml.ml_line_count;
1967 wrapped = TRUE;
1968 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001969 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001970 }
1971 else
1972 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001973 if (lnum < wp->w_buffer->b_ml.ml_line_count)
1974 ++lnum;
1975 else if (!p_ws)
1976 break; /* at first line and 'nowrapscan' */
1977 else
1978 {
1979 /* Wrap around to the start of the buffer. May search the
1980 * starting line again and accept the first match. */
1981 lnum = 1;
1982 wrapped = TRUE;
1983 }
1984
1985 /* If we are back at the starting line and there is no match then
1986 * give up. */
1987 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001988 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001989
1990 /* Skip the characters at the start of the next line that were
1991 * included in a match crossing line boundaries. */
1992 if (attr == 0)
1993 skip = p - endp;
1994 else
1995 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001996
1997 /* Capscol skips over the inserted space. */
1998 --capcol;
1999
2000 /* But after empty line check first word in next line */
2001 if (*skipwhite(line) == NUL)
2002 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002003 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002004
2005 line_breakcheck();
2006 }
2007
Bram Moolenaar0c405862005-06-22 22:26:26 +00002008 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002009 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002010}
2011
2012/*
2013 * For spell checking: concatenate the start of the following line "line" into
2014 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
2015 */
2016 void
2017spell_cat_line(buf, line, maxlen)
2018 char_u *buf;
2019 char_u *line;
2020 int maxlen;
2021{
2022 char_u *p;
2023 int n;
2024
2025 p = skipwhite(line);
2026 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2027 p = skipwhite(p + 1);
2028
2029 if (*p != NUL)
2030 {
2031 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002032 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002033 n = p - line;
2034 if (n >= maxlen)
2035 n = maxlen - 1;
2036 vim_memset(buf + 1, ' ', n);
2037 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002038}
2039
2040/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002041 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002042 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002043 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002044 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002045spell_load_lang(lang)
2046 char_u *lang;
2047{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002048 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002049 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002050 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002051
Bram Moolenaarb765d632005-06-07 21:00:02 +00002052 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002053 * It's truncated when an error is detected. */
2054 STRCPY(langcp, lang);
2055
Bram Moolenaarb765d632005-06-07 21:00:02 +00002056 /*
2057 * Find the first spell file for "lang" in 'runtimepath' and load it.
2058 */
2059 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2060 "spell/%s.%s.spl", lang, spell_enc());
2061 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002062
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002063 if (r == FAIL && *langcp != NUL)
2064 {
2065 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002066 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002067 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002068 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002069 }
2070
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002071 if (r == FAIL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002072 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2073 lang, spell_enc(), lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002074 else if (*langcp != NUL)
2075 {
2076 /* Load all the additions. */
2077 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
2078 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
2079 }
2080}
2081
2082/*
2083 * Return the encoding used for spell checking: Use 'encoding', except that we
2084 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2085 */
2086 static char_u *
2087spell_enc()
2088{
2089
2090#ifdef FEAT_MBYTE
2091 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2092 return p_enc;
2093#endif
2094 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002095}
2096
2097/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002098 * Get the name of the .spl file for the internal wordlist into
2099 * "fname[MAXPATHL]".
2100 */
2101 static void
2102int_wordlist_spl(fname)
2103 char_u *fname;
2104{
2105 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2106 int_wordlist, spell_enc());
2107}
2108
2109/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002110 * Allocate a new slang_T.
2111 * Caller must fill "sl_next".
2112 */
2113 static slang_T *
2114slang_alloc(lang)
2115 char_u *lang;
2116{
2117 slang_T *lp;
2118
Bram Moolenaar51485f02005-06-04 21:55:20 +00002119 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002120 if (lp != NULL)
2121 {
2122 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002123 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002124 lp->sl_compmax = MAXWLEN;
2125 lp->sl_compminlen = MAXWLEN;
2126 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002127 }
2128 return lp;
2129}
2130
2131/*
2132 * Free the contents of an slang_T and the structure itself.
2133 */
2134 static void
2135slang_free(lp)
2136 slang_T *lp;
2137{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002138 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002139 vim_free(lp->sl_fname);
2140 slang_clear(lp);
2141 vim_free(lp);
2142}
2143
2144/*
2145 * Clear an slang_T so that the file can be reloaded.
2146 */
2147 static void
2148slang_clear(lp)
2149 slang_T *lp;
2150{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002151 garray_T *gap;
2152 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002153 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002154 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002155
Bram Moolenaar51485f02005-06-04 21:55:20 +00002156 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002157 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002158 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002159 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002160 vim_free(lp->sl_pbyts);
2161 lp->sl_pbyts = NULL;
2162
Bram Moolenaar51485f02005-06-04 21:55:20 +00002163 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002164 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002165 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002166 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002167 vim_free(lp->sl_pidxs);
2168 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002169
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002170 gap = &lp->sl_rep;
2171 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002172 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002173 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2174 vim_free(ftp->ft_from);
2175 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002176 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002177 ga_clear(gap);
2178
2179 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002180 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002181 {
2182 /* "ga_len" is set to 1 without adding an item for latin1 */
2183 if (gap->ga_data != NULL)
2184 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2185 for (i = 0; i < gap->ga_len; ++i)
2186 vim_free(((int **)gap->ga_data)[i]);
2187 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002188 else
2189 /* SAL items: free salitem_T items */
2190 while (gap->ga_len > 0)
2191 {
2192 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2193 vim_free(smp->sm_lead);
2194 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2195 vim_free(smp->sm_to);
2196#ifdef FEAT_MBYTE
2197 vim_free(smp->sm_lead_w);
2198 vim_free(smp->sm_oneof_w);
2199 vim_free(smp->sm_to_w);
2200#endif
2201 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002202 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002203
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002204 for (i = 0; i < lp->sl_prefixcnt; ++i)
2205 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002206 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002207 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002208 lp->sl_prefprog = NULL;
2209
2210 vim_free(lp->sl_midword);
2211 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002212
Bram Moolenaar5195e452005-08-19 20:32:47 +00002213 vim_free(lp->sl_compprog);
2214 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002215 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002216 lp->sl_compprog = NULL;
2217 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002218 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002219
2220 vim_free(lp->sl_syllable);
2221 lp->sl_syllable = NULL;
2222 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002223
Bram Moolenaarea424162005-06-16 21:51:00 +00002224#ifdef FEAT_MBYTE
2225 {
2226 int todo = lp->sl_map_hash.ht_used;
2227 hashitem_T *hi;
2228
2229 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
2230 if (!HASHITEM_EMPTY(hi))
2231 {
2232 --todo;
2233 vim_free(hi->hi_key);
2234 }
2235 }
2236 hash_clear(&lp->sl_map_hash);
2237#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002238
2239 lp->sl_compmax = MAXWLEN;
2240 lp->sl_compminlen = MAXWLEN;
2241 lp->sl_compsylmax = MAXWLEN;
2242 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002243}
2244
2245/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002246 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002247 * Invoked through do_in_runtimepath().
2248 */
2249 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002250spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002251 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002252 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002253{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002254 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002255}
2256
2257/*
2258 * Load one spell file and store the info into a slang_T.
2259 *
2260 * This is invoked in two ways:
2261 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2262 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2263 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2264 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002265 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002266 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002267 static slang_T *
2268spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002269 char_u *fname;
2270 char_u *lang;
2271 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002272 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002273{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002274 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002275 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002276 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002277 char_u *bp;
2278 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002279 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002280 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002281 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002282 int round;
2283 char_u *save_sourcing_name = sourcing_name;
2284 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002285 slang_T *lp = NULL;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002286 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002287 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002288 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002289
Bram Moolenaarb765d632005-06-07 21:00:02 +00002290 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002291 if (fd == NULL)
2292 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002293 if (!silent)
2294 EMSG2(_(e_notopen), fname);
2295 else if (p_verbose > 2)
2296 {
2297 verbose_enter();
2298 smsg((char_u *)e_notopen, fname);
2299 verbose_leave();
2300 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002301 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002302 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002303 if (p_verbose > 2)
2304 {
2305 verbose_enter();
2306 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2307 verbose_leave();
2308 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002309
Bram Moolenaarb765d632005-06-07 21:00:02 +00002310 if (old_lp == NULL)
2311 {
2312 lp = slang_alloc(lang);
2313 if (lp == NULL)
2314 goto endFAIL;
2315
2316 /* Remember the file name, used to reload the file when it's updated. */
2317 lp->sl_fname = vim_strsave(fname);
2318 if (lp->sl_fname == NULL)
2319 goto endFAIL;
2320
2321 /* Check for .add.spl. */
2322 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2323 }
2324 else
2325 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002326
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002327 /* Set sourcing_name, so that error messages mention the file name. */
2328 sourcing_name = fname;
2329 sourcing_lnum = 0;
2330
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002331 /* <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002332 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002333 for (i = 0; i < VIMSPELLMAGICL; ++i)
2334 buf[i] = getc(fd); /* <fileID> */
2335 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2336 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002337 EMSG(_("E757: This does not look like a spell file"));
2338 goto endFAIL;
2339 }
2340 c = getc(fd); /* <versionnr> */
2341 if (c < VIMSPELLVERSION)
2342 {
2343 EMSG(_("E771: Old spell file, needs to be updated"));
2344 goto endFAIL;
2345 }
2346 else if (c > VIMSPELLVERSION)
2347 {
2348 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002349 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002350 }
2351
Bram Moolenaar5195e452005-08-19 20:32:47 +00002352
2353 /*
2354 * <SECTIONS>: <section> ... <sectionend>
2355 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2356 */
2357 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002358 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002359 n = getc(fd); /* <sectionID> or <sectionend> */
2360 if (n == SN_END)
2361 break;
2362 c = getc(fd); /* <sectionflags> */
2363 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2364 /* <sectionlen> */
2365 if (len < 0)
2366 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002367
Bram Moolenaar5195e452005-08-19 20:32:47 +00002368 res = 0;
2369 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002370 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002371 case SN_REGION:
2372 res = read_region_section(fd, lp, len);
2373 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002374
Bram Moolenaar5195e452005-08-19 20:32:47 +00002375 case SN_CHARFLAGS:
2376 res = read_charflags_section(fd);
2377 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002378
Bram Moolenaar5195e452005-08-19 20:32:47 +00002379 case SN_MIDWORD:
2380 lp->sl_midword = read_string(fd, len); /* <midword> */
2381 if (lp->sl_midword == NULL)
2382 goto endFAIL;
2383 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002384
Bram Moolenaar5195e452005-08-19 20:32:47 +00002385 case SN_PREFCOND:
2386 res = read_prefcond_section(fd, lp);
2387 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002388
Bram Moolenaar5195e452005-08-19 20:32:47 +00002389 case SN_REP:
2390 res = read_rep_section(fd, lp);
2391 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002392
Bram Moolenaar5195e452005-08-19 20:32:47 +00002393 case SN_SAL:
2394 res = read_sal_section(fd, lp);
2395 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002396
Bram Moolenaar5195e452005-08-19 20:32:47 +00002397 case SN_SOFO:
2398 res = read_sofo_section(fd, lp);
2399 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002400
Bram Moolenaar5195e452005-08-19 20:32:47 +00002401 case SN_MAP:
2402 p = read_string(fd, len); /* <mapstr> */
2403 if (p == NULL)
2404 goto endFAIL;
2405 set_map_str(lp, p);
2406 vim_free(p);
2407 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002408
Bram Moolenaar5195e452005-08-19 20:32:47 +00002409 case SN_COMPOUND:
2410 res = read_compound(fd, lp, len);
2411 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002412
Bram Moolenaar78622822005-08-23 21:00:13 +00002413 case SN_NOBREAK:
2414 lp->sl_nobreak = TRUE;
2415 break;
2416
Bram Moolenaar5195e452005-08-19 20:32:47 +00002417 case SN_SYLLABLE:
2418 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2419 if (lp->sl_syllable == NULL)
2420 goto endFAIL;
2421 if (init_syl_tab(lp) == FAIL)
2422 goto endFAIL;
2423 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002424
Bram Moolenaar5195e452005-08-19 20:32:47 +00002425 default:
2426 /* Unsupported section. When it's required give an error
2427 * message. When it's not required skip the contents. */
2428 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002429 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002430 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002431 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002432 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002433 while (--len >= 0)
2434 if (getc(fd) < 0)
2435 goto truncerr;
2436 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002437 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002438 if (res == SP_FORMERROR)
2439 {
2440formerr:
2441 EMSG(_(e_format));
2442 goto endFAIL;
2443 }
2444 if (res == SP_TRUNCERROR)
2445 {
2446truncerr:
2447 EMSG(_(e_spell_trunc));
2448 goto endFAIL;
2449 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002450 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002451 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002452 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002453
Bram Moolenaar51485f02005-06-04 21:55:20 +00002454 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002455 * round 2: <KWORDTREE>
2456 * round 3: <PREFIXTREE> */
2457 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002458 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002459 /* The tree size was computed when writing the file, so that we can
2460 * allocate it as one long block. <nodecount> */
2461 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2462 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002463 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002464 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002465 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002466 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002467 bp = lalloc((long_u)len, TRUE);
2468 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002469 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002470 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002471 lp->sl_fbyts = bp;
2472 else if (round == 2)
2473 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002474 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002475 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002476
2477 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002478 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2479 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002480 goto endFAIL;
2481 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002482 lp->sl_fidxs = ip;
2483 else if (round == 2)
2484 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002485 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002486 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002487
2488 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002489 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002490 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002491 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002492 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002493 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002494 }
2495 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002496
Bram Moolenaarb765d632005-06-07 21:00:02 +00002497 /* For a new file link it in the list of spell files. */
2498 if (old_lp == NULL)
2499 {
2500 lp->sl_next = first_lang;
2501 first_lang = lp;
2502 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002503
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002504 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002505
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002506endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002507 if (lang != NULL)
2508 /* truncating the name signals the error to spell_load_lang() */
2509 *lang = NUL;
2510 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002511 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002512 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002513
2514endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002515 if (fd != NULL)
2516 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002517 sourcing_name = save_sourcing_name;
2518 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002519
2520 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002521}
2522
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002523/*
2524 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002525 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002526 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002527 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2528 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002529 */
2530 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002531read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002532 FILE *fd;
2533 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002534 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002535{
2536 int cnt = 0;
2537 int i;
2538 char_u *str;
2539
2540 /* read the length bytes, MSB first */
2541 for (i = 0; i < cnt_bytes; ++i)
2542 cnt = (cnt << 8) + getc(fd);
2543 if (cnt < 0)
2544 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002545 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002546 return NULL;
2547 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002548 *cntp = cnt;
2549 if (cnt == 0)
2550 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002551
Bram Moolenaar5195e452005-08-19 20:32:47 +00002552 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002553 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002554 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002555 return str;
2556}
2557
Bram Moolenaar7887d882005-07-01 22:33:52 +00002558/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002559 * Read a string of length "cnt" from "fd" into allocated memory.
2560 * Returns NULL when out of memory.
2561 */
2562 static char_u *
2563read_string(fd, cnt)
2564 FILE *fd;
2565 int cnt;
2566{
2567 char_u *str;
2568 int i;
2569
2570 /* allocate memory */
2571 str = alloc((unsigned)cnt + 1);
2572 if (str != NULL)
2573 {
2574 /* Read the string. Doesn't check for truncated file. */
2575 for (i = 0; i < cnt; ++i)
2576 str[i] = getc(fd);
2577 str[i] = NUL;
2578 }
2579 return str;
2580}
2581
2582/*
2583 * Read SN_REGION: <regionname> ...
2584 * Return SP_*ERROR flags.
2585 */
2586 static int
2587read_region_section(fd, lp, len)
2588 FILE *fd;
2589 slang_T *lp;
2590 int len;
2591{
2592 int i;
2593
2594 if (len > 16)
2595 return SP_FORMERROR;
2596 for (i = 0; i < len; ++i)
2597 lp->sl_regions[i] = getc(fd); /* <regionname> */
2598 lp->sl_regions[len] = NUL;
2599 return 0;
2600}
2601
2602/*
2603 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2604 * <folcharslen> <folchars>
2605 * Return SP_*ERROR flags.
2606 */
2607 static int
2608read_charflags_section(fd)
2609 FILE *fd;
2610{
2611 char_u *flags;
2612 char_u *fol;
2613 int flagslen, follen;
2614
2615 /* <charflagslen> <charflags> */
2616 flags = read_cnt_string(fd, 1, &flagslen);
2617 if (flagslen < 0)
2618 return flagslen;
2619
2620 /* <folcharslen> <folchars> */
2621 fol = read_cnt_string(fd, 2, &follen);
2622 if (follen < 0)
2623 {
2624 vim_free(flags);
2625 return follen;
2626 }
2627
2628 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
2629 if (flags != NULL && fol != NULL)
2630 set_spell_charflags(flags, flagslen, fol);
2631
2632 vim_free(flags);
2633 vim_free(fol);
2634
2635 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
2636 if ((flags == NULL) != (fol == NULL))
2637 return SP_FORMERROR;
2638 return 0;
2639}
2640
2641/*
2642 * Read SN_PREFCOND section.
2643 * Return SP_*ERROR flags.
2644 */
2645 static int
2646read_prefcond_section(fd, lp)
2647 FILE *fd;
2648 slang_T *lp;
2649{
2650 int cnt;
2651 int i;
2652 int n;
2653 char_u *p;
2654 char_u buf[MAXWLEN + 1];
2655
2656 /* <prefcondcnt> <prefcond> ... */
2657 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
2658 if (cnt <= 0)
2659 return SP_FORMERROR;
2660
2661 lp->sl_prefprog = (regprog_T **)alloc_clear(
2662 (unsigned)sizeof(regprog_T *) * cnt);
2663 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002664 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002665 lp->sl_prefixcnt = cnt;
2666
2667 for (i = 0; i < cnt; ++i)
2668 {
2669 /* <prefcond> : <condlen> <condstr> */
2670 n = getc(fd); /* <condlen> */
2671 if (n < 0 || n >= MAXWLEN)
2672 return SP_FORMERROR;
2673
2674 /* When <condlen> is zero we have an empty condition. Otherwise
2675 * compile the regexp program used to check for the condition. */
2676 if (n > 0)
2677 {
2678 buf[0] = '^'; /* always match at one position only */
2679 p = buf + 1;
2680 while (n-- > 0)
2681 *p++ = getc(fd); /* <condstr> */
2682 *p = NUL;
2683 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
2684 }
2685 }
2686 return 0;
2687}
2688
2689/*
2690 * Read REP items section from "fd": <repcount> <rep> ...
2691 * Return SP_*ERROR flags.
2692 */
2693 static int
2694read_rep_section(fd, slang)
2695 FILE *fd;
2696 slang_T *slang;
2697{
2698 int cnt;
2699 garray_T *gap;
2700 fromto_T *ftp;
2701 short *first;
2702 int i;
2703
2704 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
2705 if (cnt < 0)
2706 return SP_TRUNCERROR;
2707
2708 gap = &slang->sl_rep;
2709 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002710 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002711
2712 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
2713 for (; gap->ga_len < cnt; ++gap->ga_len)
2714 {
2715 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
2716 ftp->ft_from = read_cnt_string(fd, 1, &i);
2717 if (i < 0)
2718 return i;
2719 if (i == 0)
2720 return SP_FORMERROR;
2721 ftp->ft_to = read_cnt_string(fd, 1, &i);
2722 if (i <= 0)
2723 {
2724 vim_free(ftp->ft_from);
2725 if (i < 0)
2726 return i;
2727 return SP_FORMERROR;
2728 }
2729 }
2730
2731 /* Fill the first-index table. */
2732 first = slang->sl_rep_first;
2733 for (i = 0; i < 256; ++i)
2734 first[i] = -1;
2735 for (i = 0; i < gap->ga_len; ++i)
2736 {
2737 ftp = &((fromto_T *)gap->ga_data)[i];
2738 if (first[*ftp->ft_from] == -1)
2739 first[*ftp->ft_from] = i;
2740 }
2741 return 0;
2742}
2743
2744/*
2745 * Read SN_SAL section: <salflags> <salcount> <sal> ...
2746 * Return SP_*ERROR flags.
2747 */
2748 static int
2749read_sal_section(fd, slang)
2750 FILE *fd;
2751 slang_T *slang;
2752{
2753 int i;
2754 int cnt;
2755 garray_T *gap;
2756 salitem_T *smp;
2757 int ccnt;
2758 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002759 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002760
2761 slang->sl_sofo = FALSE;
2762
2763 i = getc(fd); /* <salflags> */
2764 if (i & SAL_F0LLOWUP)
2765 slang->sl_followup = TRUE;
2766 if (i & SAL_COLLAPSE)
2767 slang->sl_collapse = TRUE;
2768 if (i & SAL_REM_ACCENTS)
2769 slang->sl_rem_accents = TRUE;
2770
2771 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
2772 if (cnt < 0)
2773 return SP_TRUNCERROR;
2774
2775 gap = &slang->sl_sal;
2776 ga_init2(gap, sizeof(salitem_T), 10);
2777 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002778 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002779
2780 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
2781 for (; gap->ga_len < cnt; ++gap->ga_len)
2782 {
2783 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2784 ccnt = getc(fd); /* <salfromlen> */
2785 if (ccnt < 0)
2786 return SP_TRUNCERROR;
2787 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002788 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002789 smp->sm_lead = p;
2790
2791 /* Read up to the first special char into sm_lead. */
2792 for (i = 0; i < ccnt; ++i)
2793 {
2794 c = getc(fd); /* <salfrom> */
2795 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
2796 break;
2797 *p++ = c;
2798 }
2799 smp->sm_leadlen = p - smp->sm_lead;
2800 *p++ = NUL;
2801
2802 /* Put (abc) chars in sm_oneof, if any. */
2803 if (c == '(')
2804 {
2805 smp->sm_oneof = p;
2806 for (++i; i < ccnt; ++i)
2807 {
2808 c = getc(fd); /* <salfrom> */
2809 if (c == ')')
2810 break;
2811 *p++ = c;
2812 }
2813 *p++ = NUL;
2814 if (++i < ccnt)
2815 c = getc(fd);
2816 }
2817 else
2818 smp->sm_oneof = NULL;
2819
2820 /* Any following chars go in sm_rules. */
2821 smp->sm_rules = p;
2822 if (i < ccnt)
2823 /* store the char we got while checking for end of sm_lead */
2824 *p++ = c;
2825 for (++i; i < ccnt; ++i)
2826 *p++ = getc(fd); /* <salfrom> */
2827 *p++ = NUL;
2828
2829 /* <saltolen> <salto> */
2830 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
2831 if (ccnt < 0)
2832 {
2833 vim_free(smp->sm_lead);
2834 return ccnt;
2835 }
2836
2837#ifdef FEAT_MBYTE
2838 if (has_mbyte)
2839 {
2840 /* convert the multi-byte strings to wide char strings */
2841 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2842 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2843 if (smp->sm_oneof == NULL)
2844 smp->sm_oneof_w = NULL;
2845 else
2846 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
2847 if (smp->sm_to == NULL)
2848 smp->sm_to_w = NULL;
2849 else
2850 smp->sm_to_w = mb_str2wide(smp->sm_to);
2851 if (smp->sm_lead_w == NULL
2852 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
2853 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
2854 {
2855 vim_free(smp->sm_lead);
2856 vim_free(smp->sm_to);
2857 vim_free(smp->sm_lead_w);
2858 vim_free(smp->sm_oneof_w);
2859 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002860 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002861 }
2862 }
2863#endif
2864 }
2865
2866 /* Fill the first-index table. */
2867 set_sal_first(slang);
2868
2869 return 0;
2870}
2871
2872/*
2873 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
2874 * Return SP_*ERROR flags.
2875 */
2876 static int
2877read_sofo_section(fd, slang)
2878 FILE *fd;
2879 slang_T *slang;
2880{
2881 int cnt;
2882 char_u *from, *to;
2883 int res;
2884
2885 slang->sl_sofo = TRUE;
2886
2887 /* <sofofromlen> <sofofrom> */
2888 from = read_cnt_string(fd, 2, &cnt);
2889 if (cnt < 0)
2890 return cnt;
2891
2892 /* <sofotolen> <sofoto> */
2893 to = read_cnt_string(fd, 2, &cnt);
2894 if (cnt < 0)
2895 {
2896 vim_free(from);
2897 return cnt;
2898 }
2899
2900 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
2901 if (from != NULL && to != NULL)
2902 res = set_sofo(slang, from, to);
2903 else if (from != NULL || to != NULL)
2904 res = SP_FORMERROR; /* only one of two strings is an error */
2905 else
2906 res = 0;
2907
2908 vim_free(from);
2909 vim_free(to);
2910 return res;
2911}
2912
2913/*
2914 * Read the compound section from the .spl file:
2915 * <compmax> <compminlen> <compsylmax> <compflags>
2916 * Returns SP_*ERROR flags.
2917 */
2918 static int
2919read_compound(fd, slang, len)
2920 FILE *fd;
2921 slang_T *slang;
2922 int len;
2923{
2924 int todo = len;
2925 int c;
2926 int atstart;
2927 char_u *pat;
2928 char_u *pp;
2929 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002930 char_u *ap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002931
2932 if (todo < 2)
2933 return SP_FORMERROR; /* need at least two bytes */
2934
2935 --todo;
2936 c = getc(fd); /* <compmax> */
2937 if (c < 2)
2938 c = MAXWLEN;
2939 slang->sl_compmax = c;
2940
2941 --todo;
2942 c = getc(fd); /* <compminlen> */
2943 if (c < 1)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002944 c = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002945 slang->sl_compminlen = c;
2946
2947 --todo;
2948 c = getc(fd); /* <compsylmax> */
2949 if (c < 1)
2950 c = MAXWLEN;
2951 slang->sl_compsylmax = c;
2952
2953 /* Turn the COMPOUNDFLAGS items into a regexp pattern:
2954 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00002955 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
2956 * Conversion to utf-8 may double the size. */
2957 c = todo * 2 + 7;
2958#ifdef FEAT_MBYTE
2959 if (enc_utf8)
2960 c += todo * 2;
2961#endif
2962 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002963 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002964 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002965
Bram Moolenaard12a1322005-08-21 22:08:24 +00002966 /* We also need a list of all flags that can appear at the start and one
2967 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002968 cp = alloc(todo + 1);
2969 if (cp == NULL)
2970 {
2971 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002972 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002973 }
2974 slang->sl_compstartflags = cp;
2975 *cp = NUL;
2976
Bram Moolenaard12a1322005-08-21 22:08:24 +00002977 ap = alloc(todo + 1);
2978 if (ap == NULL)
2979 {
2980 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002981 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002982 }
2983 slang->sl_compallflags = ap;
2984 *ap = NUL;
2985
Bram Moolenaar5195e452005-08-19 20:32:47 +00002986 pp = pat;
2987 *pp++ = '^';
2988 *pp++ = '\\';
2989 *pp++ = '(';
2990
2991 atstart = 1;
2992 while (todo-- > 0)
2993 {
2994 c = getc(fd); /* <compflags> */
Bram Moolenaard12a1322005-08-21 22:08:24 +00002995
2996 /* Add all flags to "sl_compallflags". */
2997 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00002998 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00002999 {
3000 *ap++ = c;
3001 *ap = NUL;
3002 }
3003
Bram Moolenaar5195e452005-08-19 20:32:47 +00003004 if (atstart != 0)
3005 {
3006 /* At start of item: copy flags to "sl_compstartflags". For a
3007 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3008 if (c == '[')
3009 atstart = 2;
3010 else if (c == ']')
3011 atstart = 0;
3012 else
3013 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003014 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003015 {
3016 *cp++ = c;
3017 *cp = NUL;
3018 }
3019 if (atstart == 1)
3020 atstart = 0;
3021 }
3022 }
3023 if (c == '/') /* slash separates two items */
3024 {
3025 *pp++ = '\\';
3026 *pp++ = '|';
3027 atstart = 1;
3028 }
3029 else /* normal char, "[abc]" and '*' are copied as-is */
3030 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003031 if (c == '+' || c == '~')
Bram Moolenaar5195e452005-08-19 20:32:47 +00003032 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003033#ifdef FEAT_MBYTE
3034 if (enc_utf8)
3035 pp += mb_char2bytes(c, pp);
3036 else
3037#endif
3038 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003039 }
3040 }
3041
3042 *pp++ = '\\';
3043 *pp++ = ')';
3044 *pp++ = '$';
3045 *pp = NUL;
3046
3047 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3048 vim_free(pat);
3049 if (slang->sl_compprog == NULL)
3050 return SP_FORMERROR;
3051
3052 return 0;
3053}
3054
Bram Moolenaar6de68532005-08-24 22:08:48 +00003055/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003056 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003057 * Like strchr() but independent of locale.
3058 */
3059 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003060byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003061 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003062 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003063{
3064 char_u *p;
3065
3066 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003067 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003068 return TRUE;
3069 return FALSE;
3070}
3071
Bram Moolenaar5195e452005-08-19 20:32:47 +00003072#define SY_MAXLEN 30
3073typedef struct syl_item_S
3074{
3075 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3076 int sy_len;
3077} syl_item_T;
3078
3079/*
3080 * Truncate "slang->sl_syllable" at the first slash and put the following items
3081 * in "slang->sl_syl_items".
3082 */
3083 static int
3084init_syl_tab(slang)
3085 slang_T *slang;
3086{
3087 char_u *p;
3088 char_u *s;
3089 int l;
3090 syl_item_T *syl;
3091
3092 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3093 p = vim_strchr(slang->sl_syllable, '/');
3094 while (p != NULL)
3095 {
3096 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003097 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003098 break;
3099 s = p;
3100 p = vim_strchr(p, '/');
3101 if (p == NULL)
3102 l = STRLEN(s);
3103 else
3104 l = p - s;
3105 if (l >= SY_MAXLEN)
3106 return SP_FORMERROR;
3107 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003108 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003109 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3110 + slang->sl_syl_items.ga_len++;
3111 vim_strncpy(syl->sy_chars, s, l);
3112 syl->sy_len = l;
3113 }
3114 return OK;
3115}
3116
3117/*
3118 * Count the number of syllables in "word".
3119 * When "word" contains spaces the syllables after the last space are counted.
3120 * Returns zero if syllables are not defines.
3121 */
3122 static int
3123count_syllables(slang, word)
3124 slang_T *slang;
3125 char_u *word;
3126{
3127 int cnt = 0;
3128 int skip = FALSE;
3129 char_u *p;
3130 int len;
3131 int i;
3132 syl_item_T *syl;
3133 int c;
3134
3135 if (slang->sl_syllable == NULL)
3136 return 0;
3137
3138 for (p = word; *p != NUL; p += len)
3139 {
3140 /* When running into a space reset counter. */
3141 if (*p == ' ')
3142 {
3143 len = 1;
3144 cnt = 0;
3145 continue;
3146 }
3147
3148 /* Find longest match of syllable items. */
3149 len = 0;
3150 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3151 {
3152 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3153 if (syl->sy_len > len
3154 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3155 len = syl->sy_len;
3156 }
3157 if (len != 0) /* found a match, count syllable */
3158 {
3159 ++cnt;
3160 skip = FALSE;
3161 }
3162 else
3163 {
3164 /* No recognized syllable item, at least a syllable char then? */
3165#ifdef FEAT_MBYTE
3166 c = mb_ptr2char(p);
3167 len = (*mb_ptr2len)(p);
3168#else
3169 c = *p;
3170 len = 1;
3171#endif
3172 if (vim_strchr(slang->sl_syllable, c) == NULL)
3173 skip = FALSE; /* No, search for next syllable */
3174 else if (!skip)
3175 {
3176 ++cnt; /* Yes, count it */
3177 skip = TRUE; /* don't count following syllable chars */
3178 }
3179 }
3180 }
3181 return cnt;
3182}
3183
3184/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003185 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003186 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003187 */
3188 static int
3189set_sofo(lp, from, to)
3190 slang_T *lp;
3191 char_u *from;
3192 char_u *to;
3193{
3194 int i;
3195
3196#ifdef FEAT_MBYTE
3197 garray_T *gap;
3198 char_u *s;
3199 char_u *p;
3200 int c;
3201 int *inp;
3202
3203 if (has_mbyte)
3204 {
3205 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3206 * characters. The index is the low byte of the character.
3207 * The list contains from-to pairs with a terminating NUL.
3208 * sl_sal_first[] is used for latin1 "from" characters. */
3209 gap = &lp->sl_sal;
3210 ga_init2(gap, sizeof(int *), 1);
3211 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003212 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003213 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3214 gap->ga_len = 256;
3215
3216 /* First count the number of items for each list. Temporarily use
3217 * sl_sal_first[] for this. */
3218 for (p = from, s = to; *p != NUL && *s != NUL; )
3219 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003220 c = mb_cptr2char_adv(&p);
3221 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003222 if (c >= 256)
3223 ++lp->sl_sal_first[c & 0xff];
3224 }
3225 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003226 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003227
3228 /* Allocate the lists. */
3229 for (i = 0; i < 256; ++i)
3230 if (lp->sl_sal_first[i] > 0)
3231 {
3232 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3233 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003234 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003235 ((int **)gap->ga_data)[i] = (int *)p;
3236 *(int *)p = 0;
3237 }
3238
3239 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3240 * list. */
3241 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3242 for (p = from, s = to; *p != NUL && *s != NUL; )
3243 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003244 c = mb_cptr2char_adv(&p);
3245 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003246 if (c >= 256)
3247 {
3248 /* Append the from-to chars at the end of the list with
3249 * the low byte. */
3250 inp = ((int **)gap->ga_data)[c & 0xff];
3251 while (*inp != 0)
3252 ++inp;
3253 *inp++ = c; /* from char */
3254 *inp++ = i; /* to char */
3255 *inp++ = NUL; /* NUL at the end */
3256 }
3257 else
3258 /* mapping byte to char is done in sl_sal_first[] */
3259 lp->sl_sal_first[c] = i;
3260 }
3261 }
3262 else
3263#endif
3264 {
3265 /* mapping bytes to bytes is done in sl_sal_first[] */
3266 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003267 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003268
3269 for (i = 0; to[i] != NUL; ++i)
3270 lp->sl_sal_first[from[i]] = to[i];
3271 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3272 }
3273
Bram Moolenaar5195e452005-08-19 20:32:47 +00003274 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003275}
3276
3277/*
3278 * Fill the first-index table for "lp".
3279 */
3280 static void
3281set_sal_first(lp)
3282 slang_T *lp;
3283{
3284 salfirst_T *sfirst;
3285 int i;
3286 salitem_T *smp;
3287 int c;
3288 garray_T *gap = &lp->sl_sal;
3289
3290 sfirst = lp->sl_sal_first;
3291 for (i = 0; i < 256; ++i)
3292 sfirst[i] = -1;
3293 smp = (salitem_T *)gap->ga_data;
3294 for (i = 0; i < gap->ga_len; ++i)
3295 {
3296#ifdef FEAT_MBYTE
3297 if (has_mbyte)
3298 /* Use the lowest byte of the first character. For latin1 it's
3299 * the character, for other encodings it should differ for most
3300 * characters. */
3301 c = *smp[i].sm_lead_w & 0xff;
3302 else
3303#endif
3304 c = *smp[i].sm_lead;
3305 if (sfirst[c] == -1)
3306 {
3307 sfirst[c] = i;
3308#ifdef FEAT_MBYTE
3309 if (has_mbyte)
3310 {
3311 int n;
3312
3313 /* Make sure all entries with this byte are following each
3314 * other. Move the ones that are in the wrong position. Do
3315 * keep the same ordering! */
3316 while (i + 1 < gap->ga_len
3317 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3318 /* Skip over entry with same index byte. */
3319 ++i;
3320
3321 for (n = 1; i + n < gap->ga_len; ++n)
3322 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3323 {
3324 salitem_T tsal;
3325
3326 /* Move entry with same index byte after the entries
3327 * we already found. */
3328 ++i;
3329 --n;
3330 tsal = smp[i + n];
3331 mch_memmove(smp + i + 1, smp + i,
3332 sizeof(salitem_T) * n);
3333 smp[i] = tsal;
3334 }
3335 }
3336#endif
3337 }
3338 }
3339}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003340
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003341#ifdef FEAT_MBYTE
3342/*
3343 * Turn a multi-byte string into a wide character string.
3344 * Return it in allocated memory (NULL for out-of-memory)
3345 */
3346 static int *
3347mb_str2wide(s)
3348 char_u *s;
3349{
3350 int *res;
3351 char_u *p;
3352 int i = 0;
3353
3354 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3355 if (res != NULL)
3356 {
3357 for (p = s; *p != NUL; )
3358 res[i++] = mb_ptr2char_adv(&p);
3359 res[i] = NUL;
3360 }
3361 return res;
3362}
3363#endif
3364
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003365/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003366 * Read one row of siblings from the spell file and store it in the byte array
3367 * "byts" and index array "idxs". Recursively read the children.
3368 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00003369 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00003370 *
3371 * Returns the index follosing the siblings.
3372 * Returns -1 if the file is shorter than expected.
3373 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003374 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003375 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003376read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003377 FILE *fd;
3378 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003379 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003380 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003381 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003382 int prefixtree; /* TRUE for reading PREFIXTREE */
3383 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003384{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003385 int len;
3386 int i;
3387 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003388 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003389 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003390 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003391#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003392
Bram Moolenaar51485f02005-06-04 21:55:20 +00003393 len = getc(fd); /* <siblingcount> */
3394 if (len <= 0)
3395 return -1;
3396
3397 if (startidx + len >= maxidx)
3398 return -2;
3399 byts[idx++] = len;
3400
3401 /* Read the byte values, flag/region bytes and shared indexes. */
3402 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003403 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003404 c = getc(fd); /* <byte> */
3405 if (c < 0)
3406 return -1;
3407 if (c <= BY_SPECIAL)
3408 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003409 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003410 {
3411 /* No flags, all regions. */
3412 idxs[idx] = 0;
3413 c = 0;
3414 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003415 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003416 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003417 if (prefixtree)
3418 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003419 /* Read the optional pflags byte, the prefix ID and the
3420 * condition nr. In idxs[] store the prefix ID in the low
3421 * byte, the condition index shifted up 8 bits, the flags
3422 * shifted up 24 bits. */
3423 if (c == BY_FLAGS)
3424 c = getc(fd) << 24; /* <pflags> */
3425 else
3426 c = 0;
3427
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003428 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003429
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003430 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
3431 if (n >= maxprefcondnr)
3432 return -2;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003433 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003434 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003435 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003436 {
3437 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003438 * idxs[] the flags go in the low two bytes, region above
3439 * that and prefix ID above the region. */
3440 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003441 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003442 if (c2 == BY_FLAGS2)
3443 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003444 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003445 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003446 if (c & WF_AFX)
3447 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003448 }
3449
Bram Moolenaar51485f02005-06-04 21:55:20 +00003450 idxs[idx] = c;
3451 c = 0;
3452 }
3453 else /* c == BY_INDEX */
3454 {
3455 /* <nodeidx> */
3456 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
3457 if (n < 0 || n >= maxidx)
3458 return -2;
3459 idxs[idx] = n + SHARED_MASK;
3460 c = getc(fd); /* <xbyte> */
3461 }
3462 }
3463 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003464 }
3465
Bram Moolenaar51485f02005-06-04 21:55:20 +00003466 /* Recursively read the children for non-shared siblings.
3467 * Skip the end-of-word ones (zero byte value) and the shared ones (and
3468 * remove SHARED_MASK) */
3469 for (i = 1; i <= len; ++i)
3470 if (byts[startidx + i] != 0)
3471 {
3472 if (idxs[startidx + i] & SHARED_MASK)
3473 idxs[startidx + i] &= ~SHARED_MASK;
3474 else
3475 {
3476 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003477 idx = read_tree(fd, byts, idxs, maxidx, idx,
3478 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003479 if (idx < 0)
3480 break;
3481 }
3482 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003483
Bram Moolenaar51485f02005-06-04 21:55:20 +00003484 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003485}
3486
3487/*
3488 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003489 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003490 */
3491 char_u *
3492did_set_spelllang(buf)
3493 buf_T *buf;
3494{
3495 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003496 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003497 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00003498 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003499 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003500 int region_mask;
3501 slang_T *lp;
3502 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003503 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003504 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003505 int len;
3506 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003507 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003508 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003509 char_u *use_region = NULL;
3510 int dont_use_region = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003511
3512 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003513 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003514
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003515 /* loop over comma separated language names. */
3516 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003517 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003518 /* Get one language name. */
3519 copy_option_part(&splp, lang, MAXWLEN, ",");
3520
Bram Moolenaar5482f332005-04-17 20:18:43 +00003521 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003522 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003523
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003524 /* If the name ends in ".spl" use it as the name of the spell file.
3525 * If there is a region name let "region" point to it and remove it
3526 * from the name. */
3527 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
3528 {
3529 filename = TRUE;
3530
Bram Moolenaarb6356332005-07-18 21:40:44 +00003531 /* Locate a region and remove it from the file name. */
3532 p = vim_strchr(gettail(lang), '_');
3533 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
3534 && !ASCII_ISALPHA(p[3]))
3535 {
3536 vim_strncpy(region_cp, p + 1, 2);
3537 mch_memmove(p, p + 3, len - (p - lang) - 2);
3538 len -= 3;
3539 region = region_cp;
3540 }
3541 else
3542 dont_use_region = TRUE;
3543
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003544 /* Check if we loaded this language before. */
3545 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3546 if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME)
3547 break;
3548 }
3549 else
3550 {
3551 filename = FALSE;
3552 if (len > 3 && lang[len - 3] == '_')
3553 {
3554 region = lang + len - 2;
3555 len -= 3;
3556 lang[len] = NUL;
3557 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003558 else
3559 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003560
3561 /* Check if we loaded this language before. */
3562 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3563 if (STRICMP(lang, lp->sl_name) == 0)
3564 break;
3565 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003566
Bram Moolenaarb6356332005-07-18 21:40:44 +00003567 if (region != NULL)
3568 {
3569 /* If the region differs from what was used before then don't
3570 * use it for 'spellfile'. */
3571 if (use_region != NULL && STRCMP(region, use_region) != 0)
3572 dont_use_region = TRUE;
3573 use_region = region;
3574 }
3575
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003576 /* If not found try loading the language now. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003577 if (lp == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003578 {
3579 if (filename)
3580 (void)spell_load_file(lang, lang, NULL, FALSE);
3581 else
3582 spell_load_lang(lang);
3583 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003584
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003585 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003586 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003587 */
3588 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003589 if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME
3590 : STRICMP(lang, lp->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003591 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003592 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003593 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003594 {
3595 /* find region in sl_regions */
3596 c = find_region(lp->sl_regions, region);
3597 if (c == REGION_ALL)
3598 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003599 if (lp->sl_add)
3600 {
3601 if (*lp->sl_regions != NUL)
3602 /* This addition file is for other regions. */
3603 region_mask = 0;
3604 }
3605 else
3606 /* This is probably an error. Give a warning and
3607 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003608 smsg((char_u *)
3609 _("Warning: region %s not supported"),
3610 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003611 }
3612 else
3613 region_mask = 1 << c;
3614 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003615
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003616 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003617 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003618 if (ga_grow(&ga, 1) == FAIL)
3619 {
3620 ga_clear(&ga);
3621 return e_outofmem;
3622 }
3623 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
3624 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
3625 ++ga.ga_len;
3626 use_midword(lp, buf);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003627 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003628 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003629 }
3630
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003631 /* round 0: load int_wordlist, if possible.
3632 * round 1: load first name in 'spellfile'.
3633 * round 2: load second name in 'spellfile.
3634 * etc. */
3635 spf = curbuf->b_p_spf;
3636 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003637 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003638 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003639 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003640 /* Internal wordlist, if there is one. */
3641 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003642 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003643 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003644 }
3645 else
3646 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003647 /* One entry in 'spellfile'. */
3648 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
3649 STRCAT(spf_name, ".spl");
3650
3651 /* If it was already found above then skip it. */
3652 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003653 {
3654 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
3655 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003656 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003657 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003658 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003659 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003660 }
3661
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003662 /* Check if it was loaded already. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003663 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3664 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
3665 break;
3666 if (lp == NULL)
3667 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003668 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003669 * region name, the region is ignored otherwise. for int_wordlist
3670 * use an arbitrary name. */
3671 if (round == 0)
3672 STRCPY(lang, "internal wordlist");
3673 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00003674 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003675 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003676 p = vim_strchr(lang, '.');
3677 if (p != NULL)
3678 *p = NUL; /* truncate at ".encoding.add" */
3679 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003680 lp = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003681 }
3682 if (lp != NULL && ga_grow(&ga, 1) == OK)
3683 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003684 region_mask = REGION_ALL;
3685 if (use_region != NULL && !dont_use_region)
3686 {
3687 /* find region in sl_regions */
3688 c = find_region(lp->sl_regions, use_region);
3689 if (c != REGION_ALL)
3690 region_mask = 1 << c;
3691 else if (*lp->sl_regions != NUL)
3692 /* This spell file is for other regions. */
3693 region_mask = 0;
3694 }
3695
3696 if (region_mask != 0)
3697 {
3698 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
3699 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
3700 ++ga.ga_len;
3701 use_midword(lp, buf);
3702 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003703 }
3704 }
3705
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003706 /* Everything is fine, store the new b_langp value. */
3707 ga_clear(&buf->b_langp);
3708 buf->b_langp = ga;
3709
3710 return NULL;
3711}
3712
3713/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003714 * Clear the midword characters for buffer "buf".
3715 */
3716 static void
3717clear_midword(buf)
3718 buf_T *buf;
3719{
3720 vim_memset(buf->b_spell_ismw, 0, 256);
3721#ifdef FEAT_MBYTE
3722 vim_free(buf->b_spell_ismw_mb);
3723 buf->b_spell_ismw_mb = NULL;
3724#endif
3725}
3726
3727/*
3728 * Use the "sl_midword" field of language "lp" for buffer "buf".
3729 * They add up to any currently used midword characters.
3730 */
3731 static void
3732use_midword(lp, buf)
3733 slang_T *lp;
3734 buf_T *buf;
3735{
3736 char_u *p;
3737
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003738 if (lp->sl_midword == NULL) /* there aren't any */
3739 return;
3740
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003741 for (p = lp->sl_midword; *p != NUL; )
3742#ifdef FEAT_MBYTE
3743 if (has_mbyte)
3744 {
3745 int c, l, n;
3746 char_u *bp;
3747
3748 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003749 l = (*mb_ptr2len)(p);
3750 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003751 buf->b_spell_ismw[c] = TRUE;
3752 else if (buf->b_spell_ismw_mb == NULL)
3753 /* First multi-byte char in "b_spell_ismw_mb". */
3754 buf->b_spell_ismw_mb = vim_strnsave(p, l);
3755 else
3756 {
3757 /* Append multi-byte chars to "b_spell_ismw_mb". */
3758 n = STRLEN(buf->b_spell_ismw_mb);
3759 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
3760 if (bp != NULL)
3761 {
3762 vim_free(buf->b_spell_ismw_mb);
3763 buf->b_spell_ismw_mb = bp;
3764 vim_strncpy(bp + n, p, l);
3765 }
3766 }
3767 p += l;
3768 }
3769 else
3770#endif
3771 buf->b_spell_ismw[*p++] = TRUE;
3772}
3773
3774/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003775 * Find the region "region[2]" in "rp" (points to "sl_regions").
3776 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003777 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003778 */
3779 static int
3780find_region(rp, region)
3781 char_u *rp;
3782 char_u *region;
3783{
3784 int i;
3785
3786 for (i = 0; ; i += 2)
3787 {
3788 if (rp[i] == NUL)
3789 return REGION_ALL;
3790 if (rp[i] == region[0] && rp[i + 1] == region[1])
3791 break;
3792 }
3793 return i / 2;
3794}
3795
3796/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003797 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003798 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00003799 * Word WF_ONECAP
3800 * W WORD WF_ALLCAP
3801 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003802 */
3803 static int
3804captype(word, end)
3805 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003806 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003807{
3808 char_u *p;
3809 int c;
3810 int firstcap;
3811 int allcap;
3812 int past_second = FALSE; /* past second word char */
3813
3814 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003815 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003816 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003817 return 0; /* only non-word characters, illegal word */
3818#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00003819 if (has_mbyte)
3820 c = mb_ptr2char_adv(&p);
3821 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003822#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00003823 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003824 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003825
3826 /*
3827 * Need to check all letters to find a word with mixed upper/lower.
3828 * But a word with an upper char only at start is a ONECAP.
3829 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003830 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003831 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003832 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003833 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003834 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003835 {
3836 /* UUl -> KEEPCAP */
3837 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003838 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003839 allcap = FALSE;
3840 }
3841 else if (!allcap)
3842 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003843 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003844 past_second = TRUE;
3845 }
3846
3847 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003848 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003849 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003850 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003851 return 0;
3852}
3853
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003854/*
3855 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
3856 * capital. So that make_case_word() can turn WOrd into Word.
3857 * Add ALLCAP for "WOrD".
3858 */
3859 static int
3860badword_captype(word, end)
3861 char_u *word;
3862 char_u *end;
3863{
3864 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003865 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003866 int l, u;
3867 int first;
3868 char_u *p;
3869
3870 if (flags & WF_KEEPCAP)
3871 {
3872 /* Count the number of UPPER and lower case letters. */
3873 l = u = 0;
3874 first = FALSE;
3875 for (p = word; p < end; mb_ptr_adv(p))
3876 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003877 c = PTR2CHAR(p);
3878 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003879 {
3880 ++u;
3881 if (p == word)
3882 first = TRUE;
3883 }
3884 else
3885 ++l;
3886 }
3887
3888 /* If there are more UPPER than lower case letters suggest an
3889 * ALLCAP word. Otherwise, if the first letter is UPPER then
3890 * suggest ONECAP. Exception: "ALl" most likely should be "All",
3891 * require three upper case letters. */
3892 if (u > l && u > 2)
3893 flags |= WF_ALLCAP;
3894 else if (first)
3895 flags |= WF_ONECAP;
3896 }
3897 return flags;
3898}
3899
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003900# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
3901/*
3902 * Free all languages.
3903 */
3904 void
3905spell_free_all()
3906{
3907 slang_T *lp;
3908 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003909 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003910
3911 /* Go through all buffers and handle 'spelllang'. */
3912 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3913 ga_clear(&buf->b_langp);
3914
3915 while (first_lang != NULL)
3916 {
3917 lp = first_lang;
3918 first_lang = lp->sl_next;
3919 slang_free(lp);
3920 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003921
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003922 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003923 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003924 /* Delete the internal wordlist and its .spl file */
3925 mch_remove(int_wordlist);
3926 int_wordlist_spl(fname);
3927 mch_remove(fname);
3928 vim_free(int_wordlist);
3929 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003930 }
3931
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003932 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003933}
3934# endif
3935
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003936# if defined(FEAT_MBYTE) || defined(PROTO)
3937/*
3938 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003939 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003940 */
3941 void
3942spell_reload()
3943{
3944 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003945 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003946
Bram Moolenaarea408852005-06-25 22:49:46 +00003947 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003948 init_spell_chartab();
3949
3950 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003951 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003952
3953 /* Go through all buffers and handle 'spelllang'. */
3954 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3955 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003956 /* Only load the wordlists when 'spelllang' is set and there is a
3957 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003958 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00003959 {
3960 FOR_ALL_WINDOWS(wp)
3961 if (wp->w_buffer == buf && wp->w_p_spell)
3962 {
3963 (void)did_set_spelllang(buf);
3964# ifdef FEAT_WINDOWS
3965 break;
3966# endif
3967 }
3968 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003969 }
3970}
3971# endif
3972
Bram Moolenaarb765d632005-06-07 21:00:02 +00003973/*
3974 * Reload the spell file "fname" if it's loaded.
3975 */
3976 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003977spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003978 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003979 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003980{
3981 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003982 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003983
Bram Moolenaarb765d632005-06-07 21:00:02 +00003984 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003985 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003986 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
3987 {
3988 slang_clear(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003989 if (spell_load_file(fname, NULL, lp, FALSE) == NULL)
3990 /* reloading failed, clear the language */
3991 slang_clear(lp);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003992 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003993 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003994 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003995 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003996
3997 /* When "zg" was used and the file wasn't loaded yet, should redo
3998 * 'spelllang' to get it loaded. */
3999 if (added_word && !didit)
4000 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004001}
4002
4003
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004004/*
4005 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004006 */
4007
Bram Moolenaar51485f02005-06-04 21:55:20 +00004008#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004009 and .dic file. */
4010/*
4011 * Main structure to store the contents of a ".aff" file.
4012 */
4013typedef struct afffile_S
4014{
4015 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004016 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004017 int af_slash; /* character used in word for slash */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004018 unsigned af_rar; /* RAR ID for rare word */
4019 unsigned af_kep; /* KEP ID for keep-case word */
4020 unsigned af_bad; /* BAD ID for banned word */
4021 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004022 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004023 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004024 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4025 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004026 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004027} afffile_T;
4028
Bram Moolenaar6de68532005-08-24 22:08:48 +00004029#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004030#define AFT_LONG 1 /* flags are two characters */
4031#define AFT_CAPLONG 2 /* flags are one or two characters */
4032#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004033
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004034typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004035/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4036struct affentry_S
4037{
4038 affentry_T *ae_next; /* next affix with same name/number */
4039 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4040 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004041 char_u *ae_cond; /* condition (NULL for ".") */
4042 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004043 char_u ae_rare; /* rare affix */
4044 char_u ae_nocomp; /* word with affix not compoundable */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004045};
4046
Bram Moolenaar6de68532005-08-24 22:08:48 +00004047#ifdef FEAT_MBYTE
4048# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4049#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004050# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004051#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004052
Bram Moolenaar51485f02005-06-04 21:55:20 +00004053/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4054typedef struct affheader_S
4055{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004056 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4057 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4058 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004059 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004060 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004061 affentry_T *ah_first; /* first affix entry */
4062} affheader_T;
4063
4064#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4065
Bram Moolenaar6de68532005-08-24 22:08:48 +00004066/* Flag used in compound items. */
4067typedef struct compitem_S
4068{
4069 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4070 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4071 int ci_newID; /* affix ID after renumbering. */
4072} compitem_T;
4073
4074#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4075
Bram Moolenaar51485f02005-06-04 21:55:20 +00004076/*
4077 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004078 * the need to keep track of each allocated thing, everything is freed all at
4079 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004080 */
4081#define SBLOCKSIZE 16000 /* size of sb_data */
4082typedef struct sblock_S sblock_T;
4083struct sblock_S
4084{
4085 sblock_T *sb_next; /* next block in list */
4086 int sb_used; /* nr of bytes already in use */
4087 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004088};
4089
4090/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004091 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004092 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004093typedef struct wordnode_S wordnode_T;
4094struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004095{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004096 union /* shared to save space */
4097 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004098 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004099 int index; /* index in written nodes (valid after first
4100 round) */
4101 } wn_u1;
4102 union /* shared to save space */
4103 {
4104 wordnode_T *next; /* next node with same hash key */
4105 wordnode_T *wnode; /* parent node that will write this node */
4106 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004107 wordnode_T *wn_child; /* child (next byte in word) */
4108 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4109 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004110 int wn_refs; /* Nr. of references to this node. Only
4111 relevant for first node in a list of
4112 siblings, in following siblings it is
4113 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004114 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004115 char_u wn_affixID; /* when "wn_byte" is NUL: supported/required
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004116 prefix ID or 0 */
4117 short_u wn_flags; /* when "wn_byte" is NUL: WF_ flags */
4118 short wn_region; /* when "wn_byte" is NUL: region mask; for
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004119 PREFIXTREE it's the prefcondnr */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004120#ifdef SPELL_PRINTTREE
4121 int wn_nr; /* sequence nr for printing */
4122#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004123};
4124
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004125#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4126
Bram Moolenaar51485f02005-06-04 21:55:20 +00004127#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004128
Bram Moolenaar51485f02005-06-04 21:55:20 +00004129/*
4130 * Info used while reading the spell files.
4131 */
4132typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004133{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004134 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004135 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004136
Bram Moolenaar51485f02005-06-04 21:55:20 +00004137 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004138 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004139
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004140 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004141
Bram Moolenaar51485f02005-06-04 21:55:20 +00004142 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004143 long si_blocks_cnt; /* memory blocks allocated */
4144 long si_compress_cnt; /* words to add before lowering
4145 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004146 wordnode_T *si_first_free; /* List of nodes that have been freed during
4147 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004148 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004149#ifdef SPELL_PRINTTREE
4150 int si_wordnode_nr; /* sequence nr for nodes */
4151#endif
4152
4153
Bram Moolenaar51485f02005-06-04 21:55:20 +00004154 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004155 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004156 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004157 int si_region; /* region mask */
4158 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004159 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004160 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004161 int si_msg_count; /* number of words added since last message */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004162 int si_region_count; /* number of regions supported (1 when there
4163 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004164 char_u si_region_name[16]; /* region names; used only if
4165 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004166
4167 garray_T si_rep; /* list of fromto_T entries from REP lines */
4168 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004169 char_u *si_sofofr; /* SOFOFROM text */
4170 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004171 int si_followup; /* soundsalike: ? */
4172 int si_collapse; /* soundsalike: ? */
4173 int si_rem_accents; /* soundsalike: remove accents */
4174 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004175 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004176 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004177 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004178 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004179 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004180 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004181 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004182 garray_T si_prefcond; /* table with conditions for postponed
4183 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004184 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004185 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004186} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004187
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004188static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004189static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
4190static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
4191static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004192static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004193static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
4194static void aff_check_number __ARGS((int spinval, int affval, char *name));
4195static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004196static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004197static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4198static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00004199static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004200static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004201static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004202static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004203static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004204static 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 +00004205static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
4206static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
4207static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004208static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004209static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004210static 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 +00004211static 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 +00004212static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
4213static void deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
4214static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
4215static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
4216static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004217static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004218static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00004219static void clear_node __ARGS((wordnode_T *node));
4220static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004221static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004222static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004223
Bram Moolenaar53805d12005-08-01 07:08:33 +00004224/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
4225 * but it must be negative to indicate the prefix tree to tree_add_word().
4226 * Use a negative number with the lower 8 bits zero. */
4227#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004228
Bram Moolenaar5195e452005-08-19 20:32:47 +00004229/*
4230 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
4231 */
4232static long compress_start = 30000; /* memory / SBLOCKSIZE */
4233static long compress_inc = 100; /* memory / SBLOCKSIZE */
4234static long compress_added = 500000; /* word count */
4235
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004236#ifdef SPELL_PRINTTREE
4237/*
4238 * For debugging the tree code: print the current tree in a (more or less)
4239 * readable format, so that we can see what happens when adding a word and/or
4240 * compressing the tree.
4241 * Based on code from Olaf Seibert.
4242 */
4243#define PRINTLINESIZE 1000
4244#define PRINTWIDTH 6
4245
4246#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4247 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4248
4249static char line1[PRINTLINESIZE];
4250static char line2[PRINTLINESIZE];
4251static char line3[PRINTLINESIZE];
4252
4253 static void
4254spell_clear_flags(wordnode_T *node)
4255{
4256 wordnode_T *np;
4257
4258 for (np = node; np != NULL; np = np->wn_sibling)
4259 {
4260 np->wn_u1.index = FALSE;
4261 spell_clear_flags(np->wn_child);
4262 }
4263}
4264
4265 static void
4266spell_print_node(wordnode_T *node, int depth)
4267{
4268 if (node->wn_u1.index)
4269 {
4270 /* Done this node before, print the reference. */
4271 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
4272 PRINTSOME(line2, depth, " ", 0, 0);
4273 PRINTSOME(line3, depth, " ", 0, 0);
4274 msg(line1);
4275 msg(line2);
4276 msg(line3);
4277 }
4278 else
4279 {
4280 node->wn_u1.index = TRUE;
4281
4282 if (node->wn_byte != NUL)
4283 {
4284 if (node->wn_child != NULL)
4285 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
4286 else
4287 /* Cannot happen? */
4288 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
4289 }
4290 else
4291 PRINTSOME(line1, depth, " $ ", 0, 0);
4292
4293 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
4294
4295 if (node->wn_sibling != NULL)
4296 PRINTSOME(line3, depth, " | ", 0, 0);
4297 else
4298 PRINTSOME(line3, depth, " ", 0, 0);
4299
4300 if (node->wn_byte == NUL)
4301 {
4302 msg(line1);
4303 msg(line2);
4304 msg(line3);
4305 }
4306
4307 /* do the children */
4308 if (node->wn_byte != NUL && node->wn_child != NULL)
4309 spell_print_node(node->wn_child, depth + 1);
4310
4311 /* do the siblings */
4312 if (node->wn_sibling != NULL)
4313 {
4314 /* get rid of all parent details except | */
4315 STRCPY(line1, line3);
4316 STRCPY(line2, line3);
4317 spell_print_node(node->wn_sibling, depth);
4318 }
4319 }
4320}
4321
4322 static void
4323spell_print_tree(wordnode_T *root)
4324{
4325 if (root != NULL)
4326 {
4327 /* Clear the "wn_u1.index" fields, used to remember what has been
4328 * done. */
4329 spell_clear_flags(root);
4330
4331 /* Recursively print the tree. */
4332 spell_print_node(root, 0);
4333 }
4334}
4335#endif /* SPELL_PRINTTREE */
4336
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004337/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004338 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00004339 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004340 */
4341 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004342spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004343 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004344 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004345{
4346 FILE *fd;
4347 afffile_T *aff;
4348 char_u rline[MAXLINELEN];
4349 char_u *line;
4350 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004351#define MAXITEMCNT 7
4352 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004353 int itemcnt;
4354 char_u *p;
4355 int lnum = 0;
4356 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004357 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004358 int aff_todo = 0;
4359 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004360 char_u *low = NULL;
4361 char_u *fol = NULL;
4362 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004363 int do_rep;
4364 int do_sal;
4365 int do_map;
4366 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004367 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004368 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004369 int compminlen = 0; /* COMPOUNDMIN value */
4370 int compsylmax = 0; /* COMPOUNDSYLMAX value */
4371 int compmax = 0; /* COMPOUNDMAX value */
4372 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDFLAGS
4373 concatenated */
4374 char_u *midword = NULL; /* MIDWORD value */
4375 char_u *syllable = NULL; /* SYLLABLE value */
4376 char_u *sofofrom = NULL; /* SOFOFROM value */
4377 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004378
Bram Moolenaar51485f02005-06-04 21:55:20 +00004379 /*
4380 * Open the file.
4381 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004382 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004383 if (fd == NULL)
4384 {
4385 EMSG2(_(e_notopen), fname);
4386 return NULL;
4387 }
4388
Bram Moolenaarb765d632005-06-07 21:00:02 +00004389 if (spin->si_verbose || p_verbose > 2)
4390 {
4391 if (!spin->si_verbose)
4392 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004393 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004394 out_flush();
4395 if (!spin->si_verbose)
4396 verbose_leave();
4397 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004398
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004399 /* Only do REP lines when not done in another .aff file already. */
4400 do_rep = spin->si_rep.ga_len == 0;
4401
4402 /* Only do SAL lines when not done in another .aff file already. */
4403 do_sal = spin->si_sal.ga_len == 0;
4404
4405 /* Only do MAP lines when not done in another .aff file already. */
4406 do_map = spin->si_map.ga_len == 0;
4407
Bram Moolenaar51485f02005-06-04 21:55:20 +00004408 /*
4409 * Allocate and init the afffile_T structure.
4410 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004411 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004412 if (aff == NULL)
4413 return NULL;
4414 hash_init(&aff->af_pref);
4415 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004416 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004417
4418 /*
4419 * Read all the lines in the file one by one.
4420 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004421 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004422 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004423 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004424 ++lnum;
4425
4426 /* Skip comment lines. */
4427 if (*rline == '#')
4428 continue;
4429
4430 /* Convert from "SET" to 'encoding' when needed. */
4431 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004432#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004433 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004434 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004435 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004436 if (pc == NULL)
4437 {
4438 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4439 fname, lnum, rline);
4440 continue;
4441 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004442 line = pc;
4443 }
4444 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004445#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004446 {
4447 pc = NULL;
4448 line = rline;
4449 }
4450
4451 /* Split the line up in white separated items. Put a NUL after each
4452 * item. */
4453 itemcnt = 0;
4454 for (p = line; ; )
4455 {
4456 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
4457 ++p;
4458 if (*p == NUL)
4459 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004460 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004461 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004462 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004463 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004464 ++p;
4465 if (*p == NUL)
4466 break;
4467 *p++ = NUL;
4468 }
4469
4470 /* Handle non-empty lines. */
4471 if (itemcnt > 0)
4472 {
4473 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
4474 && aff->af_enc == NULL)
4475 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004476#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004477 /* Setup for conversion from "ENC" to 'encoding'. */
4478 aff->af_enc = enc_canonize(items[1]);
4479 if (aff->af_enc != NULL && !spin->si_ascii
4480 && convert_setup(&spin->si_conv, aff->af_enc,
4481 p_enc) == FAIL)
4482 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
4483 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004484 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004485#else
4486 smsg((char_u *)_("Conversion in %s not supported"), fname);
4487#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004488 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00004489 else if (STRCMP(items[0], "FLAG") == 0 && itemcnt == 2
4490 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004491 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004492 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00004493 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004494 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00004495 aff->af_flagtype = AFT_NUM;
4496 else if (STRCMP(items[1], "caplong") == 0)
4497 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004498 else
4499 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
4500 fname, lnum, items[1]);
4501 if (aff->af_rar != 0 || aff->af_kep != 0 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004502 || aff->af_needaffix != 0
4503 || aff->af_needcomp != 0
4504 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00004505 || aff->af_suff.ht_used > 0
4506 || aff->af_pref.ht_used > 0)
4507 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
4508 fname, lnum, items[1]);
4509 }
4510 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2
4511 && midword == NULL)
4512 {
4513 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004514 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00004515 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
4516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004517 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004518 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004519 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004520 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004521 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004522 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004523 else if (STRCMP(items[0], "SLASH") == 0 && itemcnt == 2
4524 && aff->af_slash == 0)
4525 {
4526 aff->af_slash = items[1][0];
4527 if (items[1][1] != NUL)
4528 smsg((char_u *)_("Character used for SLASH must be ASCII; in %s line %d: %s"),
4529 fname, lnum, items[1]);
4530 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004531 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
4532 && aff->af_rar == 0)
4533 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004534 aff->af_rar = affitem2flag(aff->af_flagtype, items[1],
4535 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004536 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004537 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
4538 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004539 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004540 aff->af_kep = affitem2flag(aff->af_flagtype, items[1],
4541 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004542 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00004543 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
4544 && aff->af_bad == 0)
4545 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004546 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
4547 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004548 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004549 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
4550 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004551 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004552 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
4553 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004554 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004555 else if (STRCMP(items[0], "NEEDCOMPOUND") == 0 && itemcnt == 2
4556 && aff->af_needcomp == 0)
4557 {
4558 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
4559 fname, lnum);
4560 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004561 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004562 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004563 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004564 /* Turn flag "c" into COMPOUNDFLAGS compatible string "c+",
4565 * "Na" into "Na+", "1234" into "1234+". */
4566 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004567 if (p != NULL)
4568 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004569 STRCPY(p, items[1]);
4570 STRCAT(p, "+");
4571 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004572 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004573 }
4574 else if (STRCMP(items[0], "COMPOUNDFLAGS") == 0 && itemcnt == 2)
4575 {
4576 /* Concatenate this string to previously defined ones, using a
4577 * slash to separate them. */
4578 l = STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004579 if (compflags != NULL)
4580 l += STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004581 p = getroom(spin, l, FALSE);
4582 if (p != NULL)
4583 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004584 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004585 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004586 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004587 STRCAT(p, "/");
4588 }
4589 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004590 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004591 }
4592 }
4593 else if (STRCMP(items[0], "COMPOUNDMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004594 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004595 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004596 compmax = atoi((char *)items[1]);
4597 if (compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004598 smsg((char_u *)_("Wrong COMPOUNDMAX value in %s line %d: %s"),
4599 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004600 }
4601 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004602 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004603 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004604 compminlen = atoi((char *)items[1]);
4605 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004606 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
4607 fname, lnum, items[1]);
4608 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004609 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004610 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004611 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004612 compsylmax = atoi((char *)items[1]);
4613 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004614 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
4615 fname, lnum, items[1]);
4616 }
4617 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004618 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004619 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004620 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004621 }
Bram Moolenaar78622822005-08-23 21:00:13 +00004622 else if (STRCMP(items[0], "NOBREAK") == 0 && itemcnt == 1)
4623 {
4624 spin->si_nobreak = TRUE;
4625 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004626 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
4627 {
4628 aff->af_pfxpostpone = TRUE;
4629 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004630 else if ((STRCMP(items[0], "PFX") == 0
4631 || STRCMP(items[0], "SFX") == 0)
4632 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00004633 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004634 {
Bram Moolenaar95529562005-08-25 21:21:38 +00004635 int lasti = 4;
4636 char_u key[AH_KEY_LEN];
4637
4638 if (*items[0] == 'P')
4639 tp = &aff->af_pref;
4640 else
4641 tp = &aff->af_suff;
4642
4643 /* Myspell allows the same affix name to be used multiple
4644 * times. The affix files that do this have an undocumented
4645 * "S" flag on all but the last block, thus we check for that
4646 * and store it in ah_follows. */
4647 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
4648 hi = hash_find(tp, key);
4649 if (!HASHITEM_EMPTY(hi))
4650 {
4651 cur_aff = HI2AH(hi);
4652 if (cur_aff->ah_combine != (*items[2] == 'Y'))
4653 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
4654 fname, lnum, items[1]);
4655 if (!cur_aff->ah_follows)
4656 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
4657 fname, lnum, items[1]);
4658 }
4659 else
4660 {
4661 /* New affix letter. */
4662 cur_aff = (affheader_T *)getroom(spin,
4663 sizeof(affheader_T), TRUE);
4664 if (cur_aff == NULL)
4665 break;
4666 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
4667 fname, lnum);
4668 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
4669 break;
4670 if (cur_aff->ah_flag == aff->af_bad
4671 || cur_aff->ah_flag == aff->af_rar
4672 || cur_aff->ah_flag == aff->af_kep
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004673 || cur_aff->ah_flag == aff->af_needaffix
4674 || cur_aff->ah_flag == aff->af_needcomp)
4675 smsg((char_u *)_("Affix also used for BAD/RAR/KEP/NEEDAFFIX/NEEDCOMPOUND in %s line %d: %s"),
Bram Moolenaar95529562005-08-25 21:21:38 +00004676 fname, lnum, items[1]);
4677 STRCPY(cur_aff->ah_key, items[1]);
4678 hash_add(tp, cur_aff->ah_key);
4679
4680 cur_aff->ah_combine = (*items[2] == 'Y');
4681 }
4682
4683 /* Check for the "S" flag, which apparently means that another
4684 * block with the same affix name is following. */
4685 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
4686 {
4687 ++lasti;
4688 cur_aff->ah_follows = TRUE;
4689 }
4690 else
4691 cur_aff->ah_follows = FALSE;
4692
Bram Moolenaar8db73182005-06-17 21:51:16 +00004693 /* Myspell allows extra text after the item, but that might
4694 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00004695 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00004696 smsg((char_u *)_("Trailing text in %s line %d: %s"),
4697 fname, lnum, items[4]);
4698
Bram Moolenaar95529562005-08-25 21:21:38 +00004699 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004700 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
4701 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004702
Bram Moolenaar95529562005-08-25 21:21:38 +00004703 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004704 {
Bram Moolenaar95529562005-08-25 21:21:38 +00004705 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00004706 {
4707 /* Use a new number in the .spl file later, to be able
4708 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004709 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004710 cur_aff->ah_newID = ++spin->si_newprefID;
4711
4712 /* We only really use ah_newID if the prefix is
4713 * postponed. We know that only after handling all
4714 * the items. */
4715 did_postpone_prefix = FALSE;
4716 }
Bram Moolenaar95529562005-08-25 21:21:38 +00004717 else
4718 /* Did use the ID in a previous block. */
4719 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004720 }
Bram Moolenaar95529562005-08-25 21:21:38 +00004721
Bram Moolenaar51485f02005-06-04 21:55:20 +00004722 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004723 }
4724 else if ((STRCMP(items[0], "PFX") == 0
4725 || STRCMP(items[0], "SFX") == 0)
4726 && aff_todo > 0
4727 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00004728 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004729 {
4730 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004731 int rare = FALSE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004732 int nocomp = FALSE;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004733 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004734 int lasti = 5;
4735
Bram Moolenaar5195e452005-08-19 20:32:47 +00004736 /* Check for "rare" and "nocomp" after the other info. */
4737 while (itemcnt > lasti)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004738 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004739 if (!rare && STRICMP(items[lasti], "rare") == 0)
4740 {
4741 rare = TRUE;
4742 ++lasti;
4743 }
4744 else if (!nocomp && STRICMP(items[lasti], "nocomp") == 0)
4745 {
4746 nocomp = TRUE;
4747 ++lasti;
4748 }
4749 else
4750 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004751 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004752
Bram Moolenaar8db73182005-06-17 21:51:16 +00004753 /* Myspell allows extra text after the item, but that might
4754 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004755 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004756 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00004757
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004758 /* New item for an affix letter. */
4759 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004760 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004761 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004762 if (aff_entry == NULL)
4763 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004764 aff_entry->ae_rare = rare;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004765 aff_entry->ae_nocomp = nocomp;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004766
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004767 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004768 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004769 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004770 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004771
Bram Moolenaar51485f02005-06-04 21:55:20 +00004772 /* Don't use an affix entry with non-ASCII characters when
4773 * "spin->si_ascii" is TRUE. */
4774 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004775 || has_non_ascii(aff_entry->ae_add)))
4776 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00004777 aff_entry->ae_next = cur_aff->ah_first;
4778 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004779
4780 if (STRCMP(items[4], ".") != 0)
4781 {
4782 char_u buf[MAXLINELEN];
4783
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004784 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004785 if (*items[0] == 'P')
4786 sprintf((char *)buf, "^%s", items[4]);
4787 else
4788 sprintf((char *)buf, "%s$", items[4]);
4789 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004790 RE_MAGIC + RE_STRING + RE_STRICT);
4791 if (aff_entry->ae_prog == NULL)
4792 smsg((char_u *)_("Broken condition in %s line %d: %s"),
4793 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004794 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004795
4796 /* For postponed prefixes we need an entry in si_prefcond
4797 * for the condition. Use an existing one if possible. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004798 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004799 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004800 /* When the chop string is one lower-case letter and
4801 * the add string ends in the upper-case letter we set
4802 * the "upper" flag, clear "ae_chop" and remove the
4803 * letters from "ae_add". The condition must either
4804 * be empty or start with the same letter. */
4805 if (aff_entry->ae_chop != NULL
4806 && aff_entry->ae_add != NULL
4807#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004808 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00004809 aff_entry->ae_chop)] == NUL
4810#else
4811 && aff_entry->ae_chop[1] == NUL
4812#endif
4813 )
4814 {
4815 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004816
Bram Moolenaar53805d12005-08-01 07:08:33 +00004817 c = PTR2CHAR(aff_entry->ae_chop);
4818 c_up = SPELL_TOUPPER(c);
4819 if (c_up != c
4820 && (aff_entry->ae_cond == NULL
4821 || PTR2CHAR(aff_entry->ae_cond) == c))
4822 {
4823 p = aff_entry->ae_add
4824 + STRLEN(aff_entry->ae_add);
4825 mb_ptr_back(aff_entry->ae_add, p);
4826 if (PTR2CHAR(p) == c_up)
4827 {
4828 upper = TRUE;
4829 aff_entry->ae_chop = NULL;
4830 *p = NUL;
4831
4832 /* The condition is matched with the
4833 * actual word, thus must check for the
4834 * upper-case letter. */
4835 if (aff_entry->ae_cond != NULL)
4836 {
4837 char_u buf[MAXLINELEN];
4838#ifdef FEAT_MBYTE
4839 if (has_mbyte)
4840 {
4841 onecap_copy(items[4], buf, TRUE);
4842 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004843 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004844 }
4845 else
4846#endif
4847 *aff_entry->ae_cond = c_up;
4848 if (aff_entry->ae_cond != NULL)
4849 {
4850 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004851 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004852 vim_free(aff_entry->ae_prog);
4853 aff_entry->ae_prog = vim_regcomp(
4854 buf, RE_MAGIC + RE_STRING);
4855 }
4856 }
4857 }
4858 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004859 }
4860
Bram Moolenaar53805d12005-08-01 07:08:33 +00004861 if (aff_entry->ae_chop == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004862 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004863 int idx;
4864 char_u **pp;
4865 int n;
4866
Bram Moolenaar6de68532005-08-24 22:08:48 +00004867 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004868 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
4869 --idx)
4870 {
4871 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
4872 if (str_equal(p, aff_entry->ae_cond))
4873 break;
4874 }
4875 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
4876 {
4877 /* Not found, add a new condition. */
4878 idx = spin->si_prefcond.ga_len++;
4879 pp = ((char_u **)spin->si_prefcond.ga_data)
4880 + idx;
4881 if (aff_entry->ae_cond == NULL)
4882 *pp = NULL;
4883 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004884 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004885 aff_entry->ae_cond);
4886 }
4887
4888 /* Add the prefix to the prefix tree. */
4889 if (aff_entry->ae_add == NULL)
4890 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004891 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00004892 p = aff_entry->ae_add;
4893 /* PFX_FLAGS is a negative number, so that
4894 * tree_add_word() knows this is the prefix tree. */
4895 n = PFX_FLAGS;
4896 if (rare)
4897 n |= WFP_RARE;
4898 if (!cur_aff->ah_combine)
4899 n |= WFP_NC;
4900 if (upper)
4901 n |= WFP_UP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004902 tree_add_word(spin, p, spin->si_prefroot, n,
4903 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004904 did_postpone_prefix = TRUE;
4905 }
4906
4907 /* Didn't actually use ah_newID, backup si_newprefID. */
4908 if (aff_todo == 0 && !did_postpone_prefix)
4909 {
4910 --spin->si_newprefID;
4911 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004912 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004913 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00004914 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004915 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00004916 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2
4917 && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004918 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004919 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004920 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00004921 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2
4922 && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004923 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004924 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004925 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00004926 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2
4927 && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004928 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004929 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004930 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004931 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004932 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004933 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004934 if (!isdigit(*items[1]))
4935 smsg((char_u *)_("Expected REP count in %s line %d"),
4936 fname, lnum);
4937 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004938 else if (STRCMP(items[0], "REP") == 0 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004939 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004940 /* REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004941 /* Myspell ignores extra arguments, we require it starts with
4942 * # to detect mistakes. */
4943 if (itemcnt > 3 && items[3][0] != '#')
4944 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004945 if (do_rep)
4946 add_fromto(spin, &spin->si_rep, items[1], items[2]);
4947 }
4948 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
4949 {
4950 /* MAP item or count */
4951 if (!found_map)
4952 {
4953 /* First line contains the count. */
4954 found_map = TRUE;
4955 if (!isdigit(*items[1]))
4956 smsg((char_u *)_("Expected MAP count in %s line %d"),
4957 fname, lnum);
4958 }
4959 else if (do_map)
4960 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004961 int c;
4962
4963 /* Check that every character appears only once. */
4964 for (p = items[1]; *p != NUL; )
4965 {
4966#ifdef FEAT_MBYTE
4967 c = mb_ptr2char_adv(&p);
4968#else
4969 c = *p++;
4970#endif
4971 if ((spin->si_map.ga_len > 0
4972 && vim_strchr(spin->si_map.ga_data, c)
4973 != NULL)
4974 || vim_strchr(p, c) != NULL)
4975 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
4976 fname, lnum);
4977 }
4978
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004979 /* We simply concatenate all the MAP strings, separated by
4980 * slashes. */
4981 ga_concat(&spin->si_map, items[1]);
4982 ga_append(&spin->si_map, '/');
4983 }
4984 }
4985 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
4986 {
4987 if (do_sal)
4988 {
4989 /* SAL item (sounds-a-like)
4990 * Either one of the known keys or a from-to pair. */
4991 if (STRCMP(items[1], "followup") == 0)
4992 spin->si_followup = sal_to_bool(items[2]);
4993 else if (STRCMP(items[1], "collapse_result") == 0)
4994 spin->si_collapse = sal_to_bool(items[2]);
4995 else if (STRCMP(items[1], "remove_accents") == 0)
4996 spin->si_rem_accents = sal_to_bool(items[2]);
4997 else
4998 /* when "to" is "_" it means empty */
4999 add_fromto(spin, &spin->si_sal, items[1],
5000 STRCMP(items[2], "_") == 0 ? (char_u *)""
5001 : items[2]);
5002 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005003 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005004 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005005 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005006 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005007 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005008 }
5009 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005010 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005011 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005012 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005013 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005014 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005015 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005016 fname, lnum, items[0]);
5017 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005018 }
5019
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005020 if (fol != NULL || low != NULL || upp != NULL)
5021 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005022 if (spin->si_clear_chartab)
5023 {
5024 /* Clear the char type tables, don't want to use any of the
5025 * currently used spell properties. */
5026 init_spell_chartab();
5027 spin->si_clear_chartab = FALSE;
5028 }
5029
Bram Moolenaar3982c542005-06-08 21:56:31 +00005030 /*
5031 * Don't write a word table for an ASCII file, so that we don't check
5032 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005033 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00005034 * mb_get_class(), the list of chars in the file will be incomplete.
5035 */
5036 if (!spin->si_ascii
5037#ifdef FEAT_MBYTE
5038 && !enc_utf8
5039#endif
5040 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005041 {
5042 if (fol == NULL || low == NULL || upp == NULL)
5043 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
5044 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00005045 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005046 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005047
5048 vim_free(fol);
5049 vim_free(low);
5050 vim_free(upp);
5051 }
5052
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005053 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005054 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005055 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005056 aff_check_number(spin->si_compmax, compmax, "COMPOUNDMAX");
5057 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005058 }
5059
Bram Moolenaar6de68532005-08-24 22:08:48 +00005060 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005061 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005062 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
5063 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005064 }
5065
Bram Moolenaar6de68532005-08-24 22:08:48 +00005066 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005067 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005068 if (syllable == NULL)
5069 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
5070 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
5071 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005072 }
5073
Bram Moolenaar6de68532005-08-24 22:08:48 +00005074 if (compflags != NULL)
5075 process_compflags(spin, aff, compflags);
5076
5077 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005078 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005079 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005080 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005081 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005082 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005083 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005084 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005085 MSG(_("Too many posponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005086 }
5087
Bram Moolenaar6de68532005-08-24 22:08:48 +00005088 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005089 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005090 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
5091 spin->si_syllable = syllable;
5092 }
5093
5094 if (sofofrom != NULL || sofoto != NULL)
5095 {
5096 if (sofofrom == NULL || sofoto == NULL)
5097 smsg((char_u *)_("Missing SOFO%s line in %s"),
5098 sofofrom == NULL ? "FROM" : "TO", fname);
5099 else if (spin->si_sal.ga_len > 0)
5100 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005101 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005102 {
5103 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
5104 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
5105 spin->si_sofofr = sofofrom;
5106 spin->si_sofoto = sofoto;
5107 }
5108 }
5109
5110 if (midword != NULL)
5111 {
5112 aff_check_string(spin->si_midword, midword, "MIDWORD");
5113 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005114 }
5115
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005116 vim_free(pc);
5117 fclose(fd);
5118 return aff;
5119}
5120
5121/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005122 * Turn an affix flag name into a number, according to the FLAG type.
5123 * returns zero for failure.
5124 */
5125 static unsigned
5126affitem2flag(flagtype, item, fname, lnum)
5127 int flagtype;
5128 char_u *item;
5129 char_u *fname;
5130 int lnum;
5131{
5132 unsigned res;
5133 char_u *p = item;
5134
5135 res = get_affitem(flagtype, &p);
5136 if (res == 0)
5137 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005138 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005139 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
5140 fname, lnum, item);
5141 else
5142 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
5143 fname, lnum, item);
5144 }
5145 if (*p != NUL)
5146 {
5147 smsg((char_u *)_(e_affname), fname, lnum, item);
5148 return 0;
5149 }
5150
5151 return res;
5152}
5153
5154/*
5155 * Get one affix name from "*pp" and advance the pointer.
5156 * Returns zero for an error, still advances the pointer then.
5157 */
5158 static unsigned
5159get_affitem(flagtype, pp)
5160 int flagtype;
5161 char_u **pp;
5162{
5163 int res;
5164
Bram Moolenaar95529562005-08-25 21:21:38 +00005165 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005166 {
5167 if (!VIM_ISDIGIT(**pp))
5168 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005169 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005170 return 0;
5171 }
5172 res = getdigits(pp);
5173 }
5174 else
5175 {
5176#ifdef FEAT_MBYTE
5177 res = mb_ptr2char_adv(pp);
5178#else
5179 res = *(*pp)++;
5180#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00005181 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00005182 && res >= 'A' && res <= 'Z'))
5183 {
5184 if (**pp == NUL)
5185 return 0;
5186#ifdef FEAT_MBYTE
5187 res = mb_ptr2char_adv(pp) + (res << 16);
5188#else
5189 res = *(*pp)++ + (res << 16);
5190#endif
5191 }
5192 }
5193 return res;
5194}
5195
5196/*
5197 * Process the "compflags" string used in an affix file and append it to
5198 * spin->si_compflags.
5199 * The processing involves changing the affix names to ID numbers, so that
5200 * they fit in one byte.
5201 */
5202 static void
5203process_compflags(spin, aff, compflags)
5204 spellinfo_T *spin;
5205 afffile_T *aff;
5206 char_u *compflags;
5207{
5208 char_u *p;
5209 char_u *prevp;
5210 unsigned flag;
5211 compitem_T *ci;
5212 int id;
5213 int len;
5214 char_u *tp;
5215 char_u key[AH_KEY_LEN];
5216 hashitem_T *hi;
5217
5218 /* Make room for the old and the new compflags, concatenated with a / in
5219 * between. Processing it makes it shorter, but we don't know by how
5220 * much, thus allocate the maximum. */
5221 len = STRLEN(compflags) + 1;
5222 if (spin->si_compflags != NULL)
5223 len += STRLEN(spin->si_compflags) + 1;
5224 p = getroom(spin, len, FALSE);
5225 if (p == NULL)
5226 return;
5227 if (spin->si_compflags != NULL)
5228 {
5229 STRCPY(p, spin->si_compflags);
5230 STRCAT(p, "/");
5231 }
5232 else
5233 *p = NUL;
5234 spin->si_compflags = p;
5235 tp = p + STRLEN(p);
5236
5237 for (p = compflags; *p != NUL; )
5238 {
5239 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
5240 /* Copy non-flag characters directly. */
5241 *tp++ = *p++;
5242 else
5243 {
5244 /* First get the flag number, also checks validity. */
5245 prevp = p;
5246 flag = get_affitem(aff->af_flagtype, &p);
5247 if (flag != 0)
5248 {
5249 /* Find the flag in the hashtable. If it was used before, use
5250 * the existing ID. Otherwise add a new entry. */
5251 vim_strncpy(key, prevp, p - prevp);
5252 hi = hash_find(&aff->af_comp, key);
5253 if (!HASHITEM_EMPTY(hi))
5254 id = HI2CI(hi)->ci_newID;
5255 else
5256 {
5257 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
5258 if (ci == NULL)
5259 break;
5260 STRCPY(ci->ci_key, key);
5261 ci->ci_flag = flag;
5262 /* Avoid using a flag ID that has a special meaning in a
5263 * regexp (also inside []). */
5264 do
5265 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005266 check_renumber(spin);
5267 id = spin->si_newcompID--;
5268 } while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005269 ci->ci_newID = id;
5270 hash_add(&aff->af_comp, ci->ci_key);
5271 }
5272 *tp++ = id;
5273 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005274 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005275 ++p;
5276 }
5277 }
5278
5279 *tp = NUL;
5280}
5281
5282/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005283 * Check that the new IDs for postponed affixes and compounding don't overrun
5284 * each other. We have almost 255 available, but start at 0-127 to avoid
5285 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
5286 * When that is used up an error message is given.
5287 */
5288 static void
5289check_renumber(spin)
5290 spellinfo_T *spin;
5291{
5292 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
5293 {
5294 spin->si_newprefID = 127;
5295 spin->si_newcompID = 255;
5296 }
5297}
5298
5299/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005300 * Return TRUE if flag "flag" appears in affix list "afflist".
5301 */
5302 static int
5303flag_in_afflist(flagtype, afflist, flag)
5304 int flagtype;
5305 char_u *afflist;
5306 unsigned flag;
5307{
5308 char_u *p;
5309 unsigned n;
5310
5311 switch (flagtype)
5312 {
5313 case AFT_CHAR:
5314 return vim_strchr(afflist, flag) != NULL;
5315
Bram Moolenaar95529562005-08-25 21:21:38 +00005316 case AFT_CAPLONG:
5317 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00005318 for (p = afflist; *p != NUL; )
5319 {
5320#ifdef FEAT_MBYTE
5321 n = mb_ptr2char_adv(&p);
5322#else
5323 n = *p++;
5324#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00005325 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00005326 && *p != NUL)
5327#ifdef FEAT_MBYTE
5328 n = mb_ptr2char_adv(&p) + (n << 16);
5329#else
5330 n = *p++ + (n << 16);
5331#endif
5332 if (n == flag)
5333 return TRUE;
5334 }
5335 break;
5336
Bram Moolenaar95529562005-08-25 21:21:38 +00005337 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00005338 for (p = afflist; *p != NUL; )
5339 {
5340 n = getdigits(&p);
5341 if (n == flag)
5342 return TRUE;
5343 if (*p != NUL) /* skip over comma */
5344 ++p;
5345 }
5346 break;
5347 }
5348 return FALSE;
5349}
5350
5351/*
5352 * Give a warning when "spinval" and "affval" numbers are set and not the same.
5353 */
5354 static void
5355aff_check_number(spinval, affval, name)
5356 int spinval;
5357 int affval;
5358 char *name;
5359{
5360 if (spinval != 0 && spinval != affval)
5361 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
5362}
5363
5364/*
5365 * Give a warning when "spinval" and "affval" strings are set and not the same.
5366 */
5367 static void
5368aff_check_string(spinval, affval, name)
5369 char_u *spinval;
5370 char_u *affval;
5371 char *name;
5372{
5373 if (spinval != NULL && STRCMP(spinval, affval) != 0)
5374 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
5375}
5376
5377/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005378 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
5379 * NULL as equal.
5380 */
5381 static int
5382str_equal(s1, s2)
5383 char_u *s1;
5384 char_u *s2;
5385{
5386 if (s1 == NULL || s2 == NULL)
5387 return s1 == s2;
5388 return STRCMP(s1, s2) == 0;
5389}
5390
5391/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005392 * Add a from-to item to "gap". Used for REP and SAL items.
5393 * They are stored case-folded.
5394 */
5395 static void
5396add_fromto(spin, gap, from, to)
5397 spellinfo_T *spin;
5398 garray_T *gap;
5399 char_u *from;
5400 char_u *to;
5401{
5402 fromto_T *ftp;
5403 char_u word[MAXWLEN];
5404
5405 if (ga_grow(gap, 1) == OK)
5406 {
5407 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
5408 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005409 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005410 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005411 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005412 ++gap->ga_len;
5413 }
5414}
5415
5416/*
5417 * Convert a boolean argument in a SAL line to TRUE or FALSE;
5418 */
5419 static int
5420sal_to_bool(s)
5421 char_u *s;
5422{
5423 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
5424}
5425
5426/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00005427 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
5428 * When "s" is NULL FALSE is returned.
5429 */
5430 static int
5431has_non_ascii(s)
5432 char_u *s;
5433{
5434 char_u *p;
5435
5436 if (s != NULL)
5437 for (p = s; *p != NUL; ++p)
5438 if (*p >= 128)
5439 return TRUE;
5440 return FALSE;
5441}
5442
5443/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005444 * Free the structure filled by spell_read_aff().
5445 */
5446 static void
5447spell_free_aff(aff)
5448 afffile_T *aff;
5449{
5450 hashtab_T *ht;
5451 hashitem_T *hi;
5452 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005453 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005454 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005455
5456 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005457
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005458 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005459 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
5460 {
5461 todo = ht->ht_used;
5462 for (hi = ht->ht_array; todo > 0; ++hi)
5463 {
5464 if (!HASHITEM_EMPTY(hi))
5465 {
5466 --todo;
5467 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005468 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
5469 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005470 }
5471 }
5472 if (ht == &aff->af_suff)
5473 break;
5474 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005475
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005476 hash_clear(&aff->af_pref);
5477 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005478 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005479}
5480
5481/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005482 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005483 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005484 */
5485 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005486spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005487 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005488 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005489 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005490{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005491 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005492 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005493 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005494 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005495 char_u store_afflist[MAXWLEN];
5496 int pfxlen;
5497 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005498 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005499 char_u *pc;
5500 char_u *w;
5501 int l;
5502 hash_T hash;
5503 hashitem_T *hi;
5504 FILE *fd;
5505 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005506 int non_ascii = 0;
5507 int retval = OK;
5508 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005509 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005510 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005511
Bram Moolenaar51485f02005-06-04 21:55:20 +00005512 /*
5513 * Open the file.
5514 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005515 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005516 if (fd == NULL)
5517 {
5518 EMSG2(_(e_notopen), fname);
5519 return FAIL;
5520 }
5521
Bram Moolenaar51485f02005-06-04 21:55:20 +00005522 /* The hashtable is only used to detect duplicated words. */
5523 hash_init(&ht);
5524
Bram Moolenaarb765d632005-06-07 21:00:02 +00005525 if (spin->si_verbose || p_verbose > 2)
5526 {
5527 if (!spin->si_verbose)
5528 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005529 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005530 out_flush();
5531 if (!spin->si_verbose)
5532 verbose_leave();
5533 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005534
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005535 /* start with a message for the first line */
5536 spin->si_msg_count = 999999;
5537
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005538 /* Read and ignore the first line: word count. */
5539 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005540 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005541 EMSG2(_("E760: No word count in %s"), fname);
5542
5543 /*
5544 * Read all the lines in the file one by one.
5545 * The words are converted to 'encoding' here, before being added to
5546 * the hashtable.
5547 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005548 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005549 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005550 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005551 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005552 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005553 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005554
Bram Moolenaar51485f02005-06-04 21:55:20 +00005555 /* Remove CR, LF and white space from the end. White space halfway
5556 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005557 l = STRLEN(line);
5558 while (l > 0 && line[l - 1] <= ' ')
5559 --l;
5560 if (l == 0)
5561 continue; /* empty line */
5562 line[l] = NUL;
5563
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005564 /* Find the optional affix names. Replace the SLASH character by a
5565 * slash. */
5566 afflist = NULL;
5567 for (p = line; *p != NUL; mb_ptr_adv(p))
5568 {
5569 if (*p == affile->af_slash)
5570 *p = '/';
5571 else if (*p == '/')
5572 {
5573 *p = NUL;
5574 afflist = p + 1;
5575 break;
5576 }
5577 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005578
5579 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
5580 if (spin->si_ascii && has_non_ascii(line))
5581 {
5582 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005583 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005584 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005585
Bram Moolenaarb765d632005-06-07 21:00:02 +00005586#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005587 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005588 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005589 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005590 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005591 if (pc == NULL)
5592 {
5593 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5594 fname, lnum, line);
5595 continue;
5596 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005597 w = pc;
5598 }
5599 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005600#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005601 {
5602 pc = NULL;
5603 w = line;
5604 }
5605
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005606 /* This takes time, print a message every 10000 words. */
5607 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005608 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005609 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005610 vim_snprintf((char *)message, sizeof(message),
5611 _("line %6d, word %6d - %s"),
5612 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
5613 msg_start();
5614 msg_puts_long_attr(message, 0);
5615 msg_clr_eos();
5616 msg_didout = FALSE;
5617 msg_col = 0;
5618 out_flush();
5619 }
5620
Bram Moolenaar51485f02005-06-04 21:55:20 +00005621 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005622 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005623 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005624 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005625 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005626 if (retval == FAIL)
5627 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005628
Bram Moolenaar51485f02005-06-04 21:55:20 +00005629 hash = hash_hash(dw);
5630 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005631 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005632 {
5633 if (p_verbose > 0)
5634 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005635 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005636 else if (duplicate == 0)
5637 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
5638 fname, lnum, dw);
5639 ++duplicate;
5640 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005641 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00005642 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005643
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005644 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005645 store_afflist[0] = NUL;
5646 pfxlen = 0;
5647 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005648 if (afflist != NULL)
5649 {
5650 /* Check for affix name that stands for keep-case word and stands
5651 * for rare word (if defined). */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005652 if (affile->af_kep != 0 && flag_in_afflist(
5653 affile->af_flagtype, afflist, affile->af_kep))
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005654 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005655 if (affile->af_rar != 0 && flag_in_afflist(
5656 affile->af_flagtype, afflist, affile->af_rar))
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005657 flags |= WF_RARE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005658 if (affile->af_bad != 0 && flag_in_afflist(
5659 affile->af_flagtype, afflist, affile->af_bad))
Bram Moolenaar0c405862005-06-22 22:26:26 +00005660 flags |= WF_BANNED;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005661 if (affile->af_needaffix != 0 && flag_in_afflist(
5662 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00005663 need_affix = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005664 if (affile->af_needcomp != 0 && flag_in_afflist(
5665 affile->af_flagtype, afflist, affile->af_needcomp))
5666 flags |= WF_NEEDCOMP;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005667
5668 if (affile->af_pfxpostpone)
5669 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005670 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005671
Bram Moolenaar5195e452005-08-19 20:32:47 +00005672 if (spin->si_compflags != NULL)
5673 /* Need to store the list of compound flags with the word.
5674 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005675 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005676 }
5677
Bram Moolenaar51485f02005-06-04 21:55:20 +00005678 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005679 if (store_word(spin, dw, flags, spin->si_region,
5680 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005681 retval = FAIL;
5682
5683 if (afflist != NULL)
5684 {
5685 /* Find all matching suffixes and add the resulting words.
5686 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005687 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005688 &affile->af_suff, &affile->af_pref,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005689 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005690 retval = FAIL;
5691
5692 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005693 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005694 &affile->af_pref, NULL,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005695 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005696 retval = FAIL;
5697 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005698 }
5699
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005700 if (duplicate > 0)
5701 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005702 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005703 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
5704 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005705 hash_clear(&ht);
5706
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005707 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005708 return retval;
5709}
5710
5711/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005712 * Get the list of prefix IDs from the affix list "afflist".
5713 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00005714 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
5715 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005716 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005717 static int
5718get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005719 afffile_T *affile;
5720 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005721 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005722{
5723 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005724 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005725 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005726 int id;
5727 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005728 hashitem_T *hi;
5729
Bram Moolenaar6de68532005-08-24 22:08:48 +00005730 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005731 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005732 prevp = p;
5733 if (get_affitem(affile->af_flagtype, &p) != 0)
5734 {
5735 /* A flag is a postponed prefix flag if it appears in "af_pref"
5736 * and it's ID is not zero. */
5737 vim_strncpy(key, prevp, p - prevp);
5738 hi = hash_find(&affile->af_pref, key);
5739 if (!HASHITEM_EMPTY(hi))
5740 {
5741 id = HI2AH(hi)->ah_newID;
5742 if (id != 0)
5743 store_afflist[cnt++] = id;
5744 }
5745 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005746 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005747 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005748 }
5749
Bram Moolenaar5195e452005-08-19 20:32:47 +00005750 store_afflist[cnt] = NUL;
5751 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005752}
5753
5754/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005755 * Get the list of compound IDs from the affix list "afflist" that are used
5756 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00005757 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005758 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005759 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00005760get_compflags(affile, afflist, store_afflist)
5761 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005762 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005763 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005764{
5765 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005766 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005767 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005768 char_u key[AH_KEY_LEN];
5769 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005770
Bram Moolenaar6de68532005-08-24 22:08:48 +00005771 for (p = afflist; *p != NUL; )
5772 {
5773 prevp = p;
5774 if (get_affitem(affile->af_flagtype, &p) != 0)
5775 {
5776 /* A flag is a compound flag if it appears in "af_comp". */
5777 vim_strncpy(key, prevp, p - prevp);
5778 hi = hash_find(&affile->af_comp, key);
5779 if (!HASHITEM_EMPTY(hi))
5780 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
5781 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005782 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005783 ++p;
5784 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005785
Bram Moolenaar5195e452005-08-19 20:32:47 +00005786 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005787}
5788
5789/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005790 * Apply affixes to a word and store the resulting words.
5791 * "ht" is the hashtable with affentry_T that need to be applied, either
5792 * prefixes or suffixes.
5793 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
5794 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005795 *
5796 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005797 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005798 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00005799store_aff_word(spin, word, afflist, affile, ht, xht, comb, flags,
5800 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005801 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005802 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005803 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005804 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005805 hashtab_T *ht;
5806 hashtab_T *xht;
5807 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005808 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005809 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005810 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
5811 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005812{
5813 int todo;
5814 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005815 affheader_T *ah;
5816 affentry_T *ae;
5817 regmatch_T regmatch;
5818 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005819 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005820 int i;
5821 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005822 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005823 char_u *use_pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005824 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00005825 size_t wordlen = STRLEN(word);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005826
Bram Moolenaar51485f02005-06-04 21:55:20 +00005827 todo = ht->ht_used;
5828 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005829 {
5830 if (!HASHITEM_EMPTY(hi))
5831 {
5832 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005833 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00005834
Bram Moolenaar51485f02005-06-04 21:55:20 +00005835 /* Check that the affix combines, if required, and that the word
5836 * supports this affix. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005837 if ((!comb || ah->ah_combine) && flag_in_afflist(
5838 affile->af_flagtype, afflist, ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00005839 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005840 /* Loop over all affix entries with this name. */
5841 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005842 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005843 /* Check the condition. It's not logical to match case
5844 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005845 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005846 * Another requirement from Myspell is that the chop
5847 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005848 * For prefixes, when "PFXPOSTPONE" was used, only do
5849 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005850 regmatch.regprog = ae->ae_prog;
5851 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005852 if ((xht != NULL || !affile->af_pfxpostpone
5853 || ae->ae_chop != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005854 && (ae->ae_chop == NULL
5855 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005856 && (ae->ae_prog == NULL
5857 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005858 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005859 /* Match. Remove the chop and add the affix. */
5860 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005861 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005862 /* prefix: chop/add at the start of the word */
5863 if (ae->ae_add == NULL)
5864 *newword = NUL;
5865 else
5866 STRCPY(newword, ae->ae_add);
5867 p = word;
5868 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005869 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005870 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005871#ifdef FEAT_MBYTE
5872 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005873 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005874 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005875 for ( ; i > 0; --i)
5876 mb_ptr_adv(p);
5877 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005878 else
5879#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005880 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005881 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005882 STRCAT(newword, p);
5883 }
5884 else
5885 {
5886 /* suffix: chop/add at the end of the word */
5887 STRCPY(newword, word);
5888 if (ae->ae_chop != NULL)
5889 {
5890 /* Remove chop string. */
5891 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005892 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005893 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005894 mb_ptr_back(newword, p);
5895 *p = NUL;
5896 }
5897 if (ae->ae_add != NULL)
5898 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005899 }
5900
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005901 /* Obey the "rare" flag of the affix. */
5902 if (ae->ae_rare)
5903 use_flags = flags | WF_RARE;
5904 else
5905 use_flags = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005906
5907 /* Obey the "nocomp" flag of the affix: don't use the
5908 * compound flags. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005909 use_pfxlist = pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005910 if (ae->ae_nocomp && pfxlist != NULL)
5911 {
5912 vim_strncpy(pfx_pfxlist, pfxlist, pfxlen);
5913 use_pfxlist = pfx_pfxlist;
5914 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005915
5916 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00005917 if (spin->si_prefroot != NULL
5918 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005919 {
5920 /* ... add a flag to indicate an affix was used. */
5921 use_flags |= WF_HAS_AFF;
5922
5923 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00005924 * affixes is not allowed. But do use the
5925 * compound flags after them. */
5926 if ((!ah->ah_combine || comb) && pfxlist != NULL)
5927 use_pfxlist += pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005928 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005929
Bram Moolenaar51485f02005-06-04 21:55:20 +00005930 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005931 if (store_word(spin, newword, use_flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005932 spin->si_region, use_pfxlist, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005933 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005934
Bram Moolenaar51485f02005-06-04 21:55:20 +00005935 /* When added a suffix and combining is allowed also
5936 * try adding prefixes additionally. */
5937 if (xht != NULL && ah->ah_combine)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005938 if (store_aff_word(spin, newword, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005939 xht, NULL, TRUE,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005940 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005941 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005942 }
5943 }
5944 }
5945 }
5946 }
5947
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005948 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005949}
5950
5951/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005952 * Read a file with a list of words.
5953 */
5954 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005955spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005956 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005957 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005958{
5959 FILE *fd;
5960 long lnum = 0;
5961 char_u rline[MAXLINELEN];
5962 char_u *line;
5963 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005964 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005965 int l;
5966 int retval = OK;
5967 int did_word = FALSE;
5968 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005969 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00005970 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005971
5972 /*
5973 * Open the file.
5974 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005975 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00005976 if (fd == NULL)
5977 {
5978 EMSG2(_(e_notopen), fname);
5979 return FAIL;
5980 }
5981
Bram Moolenaarb765d632005-06-07 21:00:02 +00005982 if (spin->si_verbose || p_verbose > 2)
5983 {
5984 if (!spin->si_verbose)
5985 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005986 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005987 out_flush();
5988 if (!spin->si_verbose)
5989 verbose_leave();
5990 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005991
5992 /*
5993 * Read all the lines in the file one by one.
5994 */
5995 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
5996 {
5997 line_breakcheck();
5998 ++lnum;
5999
6000 /* Skip comment lines. */
6001 if (*rline == '#')
6002 continue;
6003
6004 /* Remove CR, LF and white space from the end. */
6005 l = STRLEN(rline);
6006 while (l > 0 && rline[l - 1] <= ' ')
6007 --l;
6008 if (l == 0)
6009 continue; /* empty or blank line */
6010 rline[l] = NUL;
6011
6012 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
6013 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006014#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00006015 if (spin->si_conv.vc_type != CONV_NONE)
6016 {
6017 pc = string_convert(&spin->si_conv, rline, NULL);
6018 if (pc == NULL)
6019 {
6020 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6021 fname, lnum, rline);
6022 continue;
6023 }
6024 line = pc;
6025 }
6026 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006027#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006028 {
6029 pc = NULL;
6030 line = rline;
6031 }
6032
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006033 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00006034 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006035 ++line;
6036 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006037 {
6038 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006039 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
6040 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006041 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006042 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
6043 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006044 else
6045 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006046#ifdef FEAT_MBYTE
6047 char_u *enc;
6048
Bram Moolenaar51485f02005-06-04 21:55:20 +00006049 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006050 line += 10;
6051 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006052 if (enc != NULL && !spin->si_ascii
6053 && convert_setup(&spin->si_conv, enc,
6054 p_enc) == FAIL)
6055 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00006056 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006057 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006058 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006059#else
6060 smsg((char_u *)_("Conversion in %s not supported"), fname);
6061#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006062 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006063 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006064 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006065
Bram Moolenaar3982c542005-06-08 21:56:31 +00006066 if (STRNCMP(line, "regions=", 8) == 0)
6067 {
6068 if (spin->si_region_count > 1)
6069 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
6070 fname, lnum, line);
6071 else
6072 {
6073 line += 8;
6074 if (STRLEN(line) > 16)
6075 smsg((char_u *)_("Too many regions in %s line %d: %s"),
6076 fname, lnum, line);
6077 else
6078 {
6079 spin->si_region_count = STRLEN(line) / 2;
6080 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006081
6082 /* Adjust the mask for a word valid in all regions. */
6083 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006084 }
6085 }
6086 continue;
6087 }
6088
Bram Moolenaar7887d882005-07-01 22:33:52 +00006089 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
6090 fname, lnum, line - 1);
6091 continue;
6092 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006093
Bram Moolenaar7887d882005-07-01 22:33:52 +00006094 flags = 0;
6095 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006096
Bram Moolenaar7887d882005-07-01 22:33:52 +00006097 /* Check for flags and region after a slash. */
6098 p = vim_strchr(line, '/');
6099 if (p != NULL)
6100 {
6101 *p++ = NUL;
6102 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006103 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006104 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006105 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006106 else if (*p == '!') /* Bad, bad, wicked word. */
6107 flags |= WF_BANNED;
6108 else if (*p == '?') /* Rare word. */
6109 flags |= WF_RARE;
6110 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006111 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006112 if ((flags & WF_REGION) == 0) /* first one */
6113 regionmask = 0;
6114 flags |= WF_REGION;
6115
6116 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00006117 if (l > spin->si_region_count)
6118 {
6119 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00006120 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006121 break;
6122 }
6123 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006124 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00006125 else
6126 {
6127 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
6128 fname, lnum, p);
6129 break;
6130 }
6131 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006132 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006133 }
6134
6135 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6136 if (spin->si_ascii && has_non_ascii(line))
6137 {
6138 ++non_ascii;
6139 continue;
6140 }
6141
6142 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006143 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006144 {
6145 retval = FAIL;
6146 break;
6147 }
6148 did_word = TRUE;
6149 }
6150
6151 vim_free(pc);
6152 fclose(fd);
6153
Bram Moolenaarb765d632005-06-07 21:00:02 +00006154 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
6155 {
6156 if (p_verbose > 2)
6157 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00006158 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
6159 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006160 if (p_verbose > 2)
6161 verbose_leave();
6162 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006163 return retval;
6164}
6165
6166/*
6167 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006168 * This avoids calling free() for every little struct we use (and keeping
6169 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00006170 * The memory is cleared to all zeros.
6171 * Returns NULL when out of memory.
6172 */
6173 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006174getroom(spin, len, align)
6175 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006176 size_t len; /* length needed */
6177 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006178{
6179 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006180 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006181
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006182 if (align && bl != NULL)
6183 /* Round size up for alignment. On some systems structures need to be
6184 * aligned to the size of a pointer (e.g., SPARC). */
6185 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
6186 & ~(sizeof(char *) - 1);
6187
Bram Moolenaar51485f02005-06-04 21:55:20 +00006188 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
6189 {
6190 /* Allocate a block of memory. This is not freed until much later. */
6191 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
6192 if (bl == NULL)
6193 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006194 bl->sb_next = spin->si_blocks;
6195 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006196 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006197 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006198 }
6199
6200 p = bl->sb_data + bl->sb_used;
6201 bl->sb_used += len;
6202
6203 return p;
6204}
6205
6206/*
6207 * Make a copy of a string into memory allocated with getroom().
6208 */
6209 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006210getroom_save(spin, s)
6211 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006212 char_u *s;
6213{
6214 char_u *sc;
6215
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006216 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006217 if (sc != NULL)
6218 STRCPY(sc, s);
6219 return sc;
6220}
6221
6222
6223/*
6224 * Free the list of allocated sblock_T.
6225 */
6226 static void
6227free_blocks(bl)
6228 sblock_T *bl;
6229{
6230 sblock_T *next;
6231
6232 while (bl != NULL)
6233 {
6234 next = bl->sb_next;
6235 vim_free(bl);
6236 bl = next;
6237 }
6238}
6239
6240/*
6241 * Allocate the root of a word tree.
6242 */
6243 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006244wordtree_alloc(spin)
6245 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006246{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006247 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006248}
6249
6250/*
6251 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006252 * Always store it in the case-folded tree. For a keep-case word this is
6253 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
6254 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006255 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006256 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
6257 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006258 */
6259 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00006260store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006261 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006262 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006263 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006264 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006265 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006266 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006267{
6268 int len = STRLEN(word);
6269 int ct = captype(word, word + len);
6270 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006271 int res = OK;
6272 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006273
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006274 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006275 for (p = pfxlist; res == OK; ++p)
6276 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006277 if (!need_affix || (p != NULL && *p != NUL))
6278 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006279 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006280 if (p == NULL || *p == NUL)
6281 break;
6282 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00006283 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006284
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006285 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00006286 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006287 for (p = pfxlist; res == OK; ++p)
6288 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006289 if (!need_affix || (p != NULL && *p != NUL))
6290 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006291 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006292 if (p == NULL || *p == NUL)
6293 break;
6294 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00006295 ++spin->si_keepwcount;
6296 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006297 return res;
6298}
6299
6300/*
6301 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006302 * When "flags" < 0 we are adding to the prefix tree where flags is used for
6303 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006304 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006305 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006306 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006307tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006308 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006309 char_u *word;
6310 wordnode_T *root;
6311 int flags;
6312 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006313 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006314{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006315 wordnode_T *node = root;
6316 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006317 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006318 wordnode_T **prev = NULL;
6319 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006320
Bram Moolenaar51485f02005-06-04 21:55:20 +00006321 /* Add each byte of the word to the tree, including the NUL at the end. */
6322 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006323 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006324 /* When there is more than one reference to this node we need to make
6325 * a copy, so that we can modify it. Copy the whole list of siblings
6326 * (we don't optimize for a partly shared list of siblings). */
6327 if (node != NULL && node->wn_refs > 1)
6328 {
6329 --node->wn_refs;
6330 copyprev = prev;
6331 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
6332 {
6333 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006334 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006335 if (np == NULL)
6336 return FAIL;
6337 np->wn_child = copyp->wn_child;
6338 if (np->wn_child != NULL)
6339 ++np->wn_child->wn_refs; /* child gets extra ref */
6340 np->wn_byte = copyp->wn_byte;
6341 if (np->wn_byte == NUL)
6342 {
6343 np->wn_flags = copyp->wn_flags;
6344 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006345 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006346 }
6347
6348 /* Link the new node in the list, there will be one ref. */
6349 np->wn_refs = 1;
6350 *copyprev = np;
6351 copyprev = &np->wn_sibling;
6352
6353 /* Let "node" point to the head of the copied list. */
6354 if (copyp == node)
6355 node = np;
6356 }
6357 }
6358
Bram Moolenaar51485f02005-06-04 21:55:20 +00006359 /* Look for the sibling that has the same character. They are sorted
6360 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006361 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006362 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006363 while (node != NULL
6364 && (node->wn_byte < word[i]
6365 || (node->wn_byte == NUL
6366 && (flags < 0
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006367 ? node->wn_affixID < affixID
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006368 : node->wn_flags < (flags & WN_MASK)
6369 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006370 && node->wn_affixID < affixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006371 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006372 prev = &node->wn_sibling;
6373 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006374 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006375 if (node == NULL
6376 || node->wn_byte != word[i]
6377 || (word[i] == NUL
6378 && (flags < 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006379 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006380 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006381 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006382 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006383 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006384 if (np == NULL)
6385 return FAIL;
6386 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006387
6388 /* If "node" is NULL this is a new child or the end of the sibling
6389 * list: ref count is one. Otherwise use ref count of sibling and
6390 * make ref count of sibling one (matters when inserting in front
6391 * of the list of siblings). */
6392 if (node == NULL)
6393 np->wn_refs = 1;
6394 else
6395 {
6396 np->wn_refs = node->wn_refs;
6397 node->wn_refs = 1;
6398 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006399 *prev = np;
6400 np->wn_sibling = node;
6401 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006402 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006403
Bram Moolenaar51485f02005-06-04 21:55:20 +00006404 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006405 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006406 node->wn_flags = flags;
6407 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006408 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006409 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00006410 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006411 prev = &node->wn_child;
6412 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006413 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006414#ifdef SPELL_PRINTTREE
6415 smsg("Added \"%s\"", word);
6416 spell_print_tree(root->wn_sibling);
6417#endif
6418
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006419 /* count nr of words added since last message */
6420 ++spin->si_msg_count;
6421
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006422 if (spin->si_compress_cnt > 1)
6423 {
6424 if (--spin->si_compress_cnt == 1)
6425 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006426 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006427 }
6428
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006429 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006430 * When we have allocated lots of memory we need to compress the word tree
6431 * to free up some room. But compression is slow, and we might actually
6432 * need that room, thus only compress in the following situations:
6433 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00006434 * "compress_start" blocks.
6435 * 2. When compressed before and used "compress_inc" blocks before
6436 * adding "compress_added" words (si_compress_cnt > 1).
6437 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006438 * (si_compress_cnt == 1) and the number of free nodes drops below the
6439 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006440 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006441#ifndef SPELL_PRINTTREE
6442 if (spin->si_compress_cnt == 1
6443 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00006444 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006445#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006446 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006447 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00006448 * when the freed up room has been used and another "compress_inc"
6449 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006450 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006451 spin->si_blocks_cnt -= compress_inc;
6452 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006453
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006454 if (spin->si_verbose)
6455 {
6456 msg_start();
6457 msg_puts((char_u *)_(msg_compressing));
6458 msg_clr_eos();
6459 msg_didout = FALSE;
6460 msg_col = 0;
6461 out_flush();
6462 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006463
6464 /* Compress both trees. Either they both have many nodes, which makes
6465 * compression useful, or one of them is small, which means
6466 * compression goes fast. */
6467 wordtree_compress(spin, spin->si_foldroot);
6468 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006469 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006470
6471 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006472}
6473
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006474/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00006475 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
6476 * Sets "sps_flags".
6477 */
6478 int
6479spell_check_msm()
6480{
6481 char_u *p = p_msm;
6482 long start = 0;
6483 long inc = 0;
6484 long added = 0;
6485
6486 if (!VIM_ISDIGIT(*p))
6487 return FAIL;
6488 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
6489 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
6490 if (*p != ',')
6491 return FAIL;
6492 ++p;
6493 if (!VIM_ISDIGIT(*p))
6494 return FAIL;
6495 inc = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
6496 if (*p != ',')
6497 return FAIL;
6498 ++p;
6499 if (!VIM_ISDIGIT(*p))
6500 return FAIL;
6501 added = getdigits(&p) * 1024;
6502 if (*p != NUL)
6503 return FAIL;
6504
6505 if (start == 0 || inc == 0 || added == 0 || inc > start)
6506 return FAIL;
6507
6508 compress_start = start;
6509 compress_inc = inc;
6510 compress_added = added;
6511 return OK;
6512}
6513
6514
6515/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006516 * Get a wordnode_T, either from the list of previously freed nodes or
6517 * allocate a new one.
6518 */
6519 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006520get_wordnode(spin)
6521 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006522{
6523 wordnode_T *n;
6524
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006525 if (spin->si_first_free == NULL)
6526 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006527 else
6528 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006529 n = spin->si_first_free;
6530 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006531 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006532 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006533 }
6534#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006535 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006536#endif
6537 return n;
6538}
6539
6540/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006541 * Decrement the reference count on a node (which is the head of a list of
6542 * siblings). If the reference count becomes zero free the node and its
6543 * siblings.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006544 */
6545 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006546deref_wordnode(spin, node)
6547 spellinfo_T *spin;
6548 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006549{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006550 wordnode_T *np;
6551
6552 if (--node->wn_refs == 0)
6553 for (np = node; np != NULL; np = np->wn_sibling)
6554 {
6555 if (np->wn_child != NULL)
6556 deref_wordnode(spin, np->wn_child);
6557 free_wordnode(spin, np);
6558 }
6559}
6560
6561/*
6562 * Free a wordnode_T for re-use later.
6563 * Only the "wn_child" field becomes invalid.
6564 */
6565 static void
6566free_wordnode(spin, n)
6567 spellinfo_T *spin;
6568 wordnode_T *n;
6569{
6570 n->wn_child = spin->si_first_free;
6571 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006572 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006573}
6574
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006575/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006576 * Compress a tree: find tails that are identical and can be shared.
6577 */
6578 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006579wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006580 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006581 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006582{
6583 hashtab_T ht;
6584 int n;
6585 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006586 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006587
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006588 /* Skip the root itself, it's not actually used. The first sibling is the
6589 * start of the tree. */
6590 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006591 {
6592 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006593 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006594
6595#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00006596 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006597#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00006598 {
6599 if (!spin->si_verbose)
6600 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006601 if (tot > 1000000)
6602 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006603 else if (tot == 0)
6604 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006605 else
6606 perc = (tot - n) * 100 / tot;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006607 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006608 n, tot, perc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006609 if (p_verbose > 2)
6610 verbose_leave();
6611 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006612#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006613 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006614#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006615 hash_clear(&ht);
6616 }
6617}
6618
6619/*
6620 * Compress a node, its siblings and its children, depth first.
6621 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006622 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006623 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006624node_compress(spin, node, ht, tot)
6625 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006626 wordnode_T *node;
6627 hashtab_T *ht;
6628 int *tot; /* total count of nodes before compressing,
6629 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006630{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006631 wordnode_T *np;
6632 wordnode_T *tp;
6633 wordnode_T *child;
6634 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006635 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006636 int len = 0;
6637 unsigned nr, n;
6638 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006639
Bram Moolenaar51485f02005-06-04 21:55:20 +00006640 /*
6641 * Go through the list of siblings. Compress each child and then try
6642 * finding an identical child to replace it.
6643 * Note that with "child" we mean not just the node that is pointed to,
6644 * but the whole list of siblings, of which the node is the first.
6645 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006646 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006647 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006648 ++len;
6649 if ((child = np->wn_child) != NULL)
6650 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006651 /* Compress the child. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006652 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006653
6654 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006655 hash = hash_hash(child->wn_u1.hashkey);
6656 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006657 tp = NULL;
6658 if (!HASHITEM_EMPTY(hi))
6659 {
6660 /* There are children with an identical hash value. Now check
6661 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006662 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006663 if (node_equal(child, tp))
6664 {
6665 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006666 * current one. This means the current child and all
6667 * its siblings is unlinked from the tree. */
6668 ++tp->wn_refs;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006669 deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006670 np->wn_child = tp;
6671 ++compressed;
6672 break;
6673 }
6674 if (tp == NULL)
6675 {
6676 /* No other child with this hash value equals the child of
6677 * the node, add it to the linked list after the first
6678 * item. */
6679 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006680 child->wn_u2.next = tp->wn_u2.next;
6681 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006682 }
6683 }
6684 else
6685 /* No other child has this hash value, add it to the
6686 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006687 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006688 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006689 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006690 *tot += len;
6691
6692 /*
6693 * Make a hash key for the node and its siblings, so that we can quickly
6694 * find a lookalike node. This must be done after compressing the sibling
6695 * list, otherwise the hash key would become invalid by the compression.
6696 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006697 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006698 nr = 0;
6699 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006700 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006701 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006702 /* end node: use wn_flags, wn_region and wn_affixID */
6703 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006704 else
6705 /* byte node: use the byte value and the child pointer */
6706 n = np->wn_byte + ((long_u)np->wn_child << 8);
6707 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006708 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006709
6710 /* Avoid NUL bytes, it terminates the hash key. */
6711 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006712 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006713 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006714 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006715 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006716 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006717 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006718 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
6719 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006720
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006721 /* Check for CTRL-C pressed now and then. */
6722 fast_breakcheck();
6723
Bram Moolenaar51485f02005-06-04 21:55:20 +00006724 return compressed;
6725}
6726
6727/*
6728 * Return TRUE when two nodes have identical siblings and children.
6729 */
6730 static int
6731node_equal(n1, n2)
6732 wordnode_T *n1;
6733 wordnode_T *n2;
6734{
6735 wordnode_T *p1;
6736 wordnode_T *p2;
6737
6738 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
6739 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
6740 if (p1->wn_byte != p2->wn_byte
6741 || (p1->wn_byte == NUL
6742 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006743 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006744 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006745 : (p1->wn_child != p2->wn_child)))
6746 break;
6747
6748 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006749}
6750
6751/*
6752 * Write a number to file "fd", MSB first, in "len" bytes.
6753 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006754 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006755put_bytes(fd, nr, len)
6756 FILE *fd;
6757 long_u nr;
6758 int len;
6759{
6760 int i;
6761
6762 for (i = len - 1; i >= 0; --i)
6763 putc((int)(nr >> (i * 8)), fd);
6764}
6765
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006766static int
6767#ifdef __BORLANDC__
6768_RTLENTRYF
6769#endif
6770rep_compare __ARGS((const void *s1, const void *s2));
6771
6772/*
6773 * Function given to qsort() to sort the REP items on "from" string.
6774 */
6775 static int
6776#ifdef __BORLANDC__
6777_RTLENTRYF
6778#endif
6779rep_compare(s1, s2)
6780 const void *s1;
6781 const void *s2;
6782{
6783 fromto_T *p1 = (fromto_T *)s1;
6784 fromto_T *p2 = (fromto_T *)s2;
6785
6786 return STRCMP(p1->ft_from, p2->ft_from);
6787}
6788
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006789/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00006790 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006791 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006792 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006793 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006794write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006795 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006796 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006797{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006798 FILE *fd;
6799 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006800 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006801 wordnode_T *tree;
6802 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006803 int i;
6804 int l;
6805 garray_T *gap;
6806 fromto_T *ftp;
6807 char_u *p;
6808 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006809 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006810
Bram Moolenaarb765d632005-06-07 21:00:02 +00006811 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006812 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006813 {
6814 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006815 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006816 }
6817
Bram Moolenaar5195e452005-08-19 20:32:47 +00006818 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006819 /* <fileID> */
6820 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006821 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006822 EMSG(_(e_write));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006823 retval = FAIL;
6824 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00006825 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006826
Bram Moolenaar5195e452005-08-19 20:32:47 +00006827 /*
6828 * <SECTIONS>: <section> ... <sectionend>
6829 */
6830
6831 /* SN_REGION: <regionname> ...
6832 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006833 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006834 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006835 putc(SN_REGION, fd); /* <sectionID> */
6836 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6837 l = spin->si_region_count * 2;
6838 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6839 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
6840 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006841 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006842 }
6843 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006844 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006845
Bram Moolenaar5195e452005-08-19 20:32:47 +00006846 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
6847 *
6848 * The table with character flags and the table for case folding.
6849 * This makes sure the same characters are recognized as word characters
6850 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006851 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006852 * 'encoding'.
6853 * Also skip this for an .add.spl file, the main spell file must contain
6854 * the table (avoids that it conflicts). File is shorter too.
6855 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006856 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006857 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006858 char_u folchars[128 * 8];
6859 int flags;
6860
Bram Moolenaard12a1322005-08-21 22:08:24 +00006861 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006862 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6863
6864 /* Form the <folchars> string first, we need to know its length. */
6865 l = 0;
6866 for (i = 128; i < 256; ++i)
6867 {
6868#ifdef FEAT_MBYTE
6869 if (has_mbyte)
6870 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
6871 else
6872#endif
6873 folchars[l++] = spelltab.st_fold[i];
6874 }
6875 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
6876
6877 fputc(128, fd); /* <charflagslen> */
6878 for (i = 128; i < 256; ++i)
6879 {
6880 flags = 0;
6881 if (spelltab.st_isw[i])
6882 flags |= CF_WORD;
6883 if (spelltab.st_isu[i])
6884 flags |= CF_UPPER;
6885 fputc(flags, fd); /* <charflags> */
6886 }
6887
6888 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
6889 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006890 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006891
Bram Moolenaar5195e452005-08-19 20:32:47 +00006892 /* SN_MIDWORD: <midword> */
6893 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006894 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006895 putc(SN_MIDWORD, fd); /* <sectionID> */
6896 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6897
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006898 i = STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006899 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006900 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
6901 }
6902
Bram Moolenaar5195e452005-08-19 20:32:47 +00006903 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
6904 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006905 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006906 putc(SN_PREFCOND, fd); /* <sectionID> */
6907 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6908
6909 l = write_spell_prefcond(NULL, &spin->si_prefcond);
6910 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6911
6912 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006913 }
6914
Bram Moolenaar5195e452005-08-19 20:32:47 +00006915 /* SN_REP: <repcount> <rep> ...
6916 * SN_SAL: <salflags> <salcount> <sal> ... */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006917
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006918 /* Sort the REP items. */
6919 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
6920 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006921
Bram Moolenaar5195e452005-08-19 20:32:47 +00006922 /* round 1: SN_REP section
6923 * round 2: SN_SAL section (unless SN_SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006924 for (round = 1; round <= 2; ++round)
6925 {
6926 if (round == 1)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006927 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006928 gap = &spin->si_rep;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006929 putc(SN_REP, fd); /* <sectionID> */
6930 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006931 else
6932 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006933 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
6934 /* using SN_SOFO section instead of SN_SAL */
6935 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006936 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006937 putc(SN_SAL, fd); /* <sectionID> */
6938 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006939
Bram Moolenaar5195e452005-08-19 20:32:47 +00006940 /* This is for making suggestions, section is not required. */
6941 putc(0, fd); /* <sectionflags> */
6942
6943 /* Compute the length of what follows. */
6944 l = 2; /* count <repcount> or <salcount> */
6945 for (i = 0; i < gap->ga_len; ++i)
6946 {
6947 ftp = &((fromto_T *)gap->ga_data)[i];
6948 l += 1 + STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
6949 l += 1 + STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
6950 }
6951 if (round == 2)
6952 ++l; /* count <salflags> */
6953 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6954
6955 if (round == 2)
6956 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006957 i = 0;
6958 if (spin->si_followup)
6959 i |= SAL_F0LLOWUP;
6960 if (spin->si_collapse)
6961 i |= SAL_COLLAPSE;
6962 if (spin->si_rem_accents)
6963 i |= SAL_REM_ACCENTS;
6964 putc(i, fd); /* <salflags> */
6965 }
6966
6967 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
6968 for (i = 0; i < gap->ga_len; ++i)
6969 {
6970 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
6971 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
6972 ftp = &((fromto_T *)gap->ga_data)[i];
6973 for (rr = 1; rr <= 2; ++rr)
6974 {
6975 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
6976 l = STRLEN(p);
6977 putc(l, fd);
6978 fwrite(p, l, (size_t)1, fd);
6979 }
6980 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00006981
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006982 }
6983
Bram Moolenaar5195e452005-08-19 20:32:47 +00006984 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
6985 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006986 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
6987 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006988 putc(SN_SOFO, fd); /* <sectionID> */
6989 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006990
6991 l = STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006992 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
6993 /* <sectionlen> */
6994
6995 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
6996 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006997
6998 l = STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006999 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
7000 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007001 }
7002
Bram Moolenaar5195e452005-08-19 20:32:47 +00007003 /* SN_MAP: <mapstr>
7004 * This is for making suggestions, section is not required. */
7005 if (spin->si_map.ga_len > 0)
7006 {
7007 putc(SN_MAP, fd); /* <sectionID> */
7008 putc(0, fd); /* <sectionflags> */
7009 l = spin->si_map.ga_len;
7010 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7011 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
7012 /* <mapstr> */
7013 }
7014
7015 /* SN_COMPOUND: compound info.
7016 * We don't mark it required, when not supported all compound words will
7017 * be bad words. */
7018 if (spin->si_compflags != NULL)
7019 {
7020 putc(SN_COMPOUND, fd); /* <sectionID> */
7021 putc(0, fd); /* <sectionflags> */
7022
7023 l = STRLEN(spin->si_compflags);
7024 put_bytes(fd, (long_u)(l + 3), 4); /* <sectionlen> */
7025 putc(spin->si_compmax, fd); /* <compmax> */
7026 putc(spin->si_compminlen, fd); /* <compminlen> */
7027 putc(spin->si_compsylmax, fd); /* <compsylmax> */
7028 /* <compflags> */
7029 fwrite(spin->si_compflags, (size_t)l, (size_t)1, fd);
7030 }
7031
Bram Moolenaar78622822005-08-23 21:00:13 +00007032 /* SN_NOBREAK: NOBREAK flag */
7033 if (spin->si_nobreak)
7034 {
7035 putc(SN_NOBREAK, fd); /* <sectionID> */
7036 putc(0, fd); /* <sectionflags> */
7037
7038 /* It's empty, the precense of the section flags the feature. */
7039 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
7040 }
7041
Bram Moolenaar5195e452005-08-19 20:32:47 +00007042 /* SN_SYLLABLE: syllable info.
7043 * We don't mark it required, when not supported syllables will not be
7044 * counted. */
7045 if (spin->si_syllable != NULL)
7046 {
7047 putc(SN_SYLLABLE, fd); /* <sectionID> */
7048 putc(0, fd); /* <sectionflags> */
7049
7050 l = STRLEN(spin->si_syllable);
7051 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7052 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
7053 }
7054
7055 /* end of <SECTIONS> */
7056 putc(SN_END, fd); /* <sectionend> */
7057
Bram Moolenaar50cde822005-06-05 21:54:54 +00007058
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007059 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007060 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007061 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007062 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007063 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007064 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007065 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007066 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007067 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007068 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007069 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007070 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007071
Bram Moolenaar0c405862005-06-22 22:26:26 +00007072 /* Clear the index and wnode fields in the tree. */
7073 clear_node(tree);
7074
Bram Moolenaar51485f02005-06-04 21:55:20 +00007075 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00007076 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00007077 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007078 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007079
Bram Moolenaar51485f02005-06-04 21:55:20 +00007080 /* number of nodes in 4 bytes */
7081 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00007082 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007083
Bram Moolenaar51485f02005-06-04 21:55:20 +00007084 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007085 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007086 }
7087
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007088 /* Write another byte to check for errors. */
7089 if (putc(0, fd) == EOF)
7090 retval = FAIL;
7091
7092 if (fclose(fd) == EOF)
7093 retval = FAIL;
7094
7095 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007096}
7097
7098/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007099 * Clear the index and wnode fields of "node", it siblings and its
7100 * children. This is needed because they are a union with other items to save
7101 * space.
7102 */
7103 static void
7104clear_node(node)
7105 wordnode_T *node;
7106{
7107 wordnode_T *np;
7108
7109 if (node != NULL)
7110 for (np = node; np != NULL; np = np->wn_sibling)
7111 {
7112 np->wn_u1.index = 0;
7113 np->wn_u2.wnode = NULL;
7114
7115 if (np->wn_byte != NUL)
7116 clear_node(np->wn_child);
7117 }
7118}
7119
7120
7121/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007122 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007123 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00007124 * This first writes the list of possible bytes (siblings). Then for each
7125 * byte recursively write the children.
7126 *
7127 * NOTE: The code here must match the code in read_tree(), since assumptions
7128 * are made about the indexes (so that we don't have to write them in the
7129 * file).
7130 *
7131 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007132 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007133 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00007134put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007135 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007136 wordnode_T *node;
7137 int index;
7138 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007139 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007140{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007141 int newindex = index;
7142 int siblingcount = 0;
7143 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007144 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007145
Bram Moolenaar51485f02005-06-04 21:55:20 +00007146 /* If "node" is zero the tree is empty. */
7147 if (node == NULL)
7148 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007149
Bram Moolenaar51485f02005-06-04 21:55:20 +00007150 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007151 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007152
7153 /* Count the number of siblings. */
7154 for (np = node; np != NULL; np = np->wn_sibling)
7155 ++siblingcount;
7156
7157 /* Write the sibling count. */
7158 if (fd != NULL)
7159 putc(siblingcount, fd); /* <siblingcount> */
7160
7161 /* Write each sibling byte and optionally extra info. */
7162 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007163 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007164 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007165 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007166 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007167 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007168 /* For a NUL byte (end of word) write the flags etc. */
7169 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007170 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007171 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007172 * associated condition nr (stored in wn_region). The
7173 * byte value is misused to store the "rare" and "not
7174 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00007175 if (np->wn_flags == (short_u)PFX_FLAGS)
7176 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007177 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00007178 {
7179 putc(BY_FLAGS, fd); /* <byte> */
7180 putc(np->wn_flags, fd); /* <pflags> */
7181 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007182 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007183 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007184 }
7185 else
7186 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007187 /* For word trees we write the flag/region items. */
7188 flags = np->wn_flags;
7189 if (regionmask != 0 && np->wn_region != regionmask)
7190 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007191 if (np->wn_affixID != 0)
7192 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007193 if (flags == 0)
7194 {
7195 /* word without flags or region */
7196 putc(BY_NOFLAGS, fd); /* <byte> */
7197 }
7198 else
7199 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007200 if (np->wn_flags >= 0x100)
7201 {
7202 putc(BY_FLAGS2, fd); /* <byte> */
7203 putc(flags, fd); /* <flags> */
7204 putc((unsigned)flags >> 8, fd); /* <flags2> */
7205 }
7206 else
7207 {
7208 putc(BY_FLAGS, fd); /* <byte> */
7209 putc(flags, fd); /* <flags> */
7210 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007211 if (flags & WF_REGION)
7212 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007213 if (flags & WF_AFX)
7214 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007215 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007216 }
7217 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007218 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007219 else
7220 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00007221 if (np->wn_child->wn_u1.index != 0
7222 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007223 {
7224 /* The child is written elsewhere, write the reference. */
7225 if (fd != NULL)
7226 {
7227 putc(BY_INDEX, fd); /* <byte> */
7228 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007229 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007230 }
7231 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00007232 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007233 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007234 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007235
Bram Moolenaar51485f02005-06-04 21:55:20 +00007236 if (fd != NULL)
7237 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
7238 {
7239 EMSG(_(e_write));
7240 return 0;
7241 }
7242 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007243 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007244
7245 /* Space used in the array when reading: one for each sibling and one for
7246 * the count. */
7247 newindex += siblingcount + 1;
7248
7249 /* Recursively dump the children of each sibling. */
7250 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007251 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
7252 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007253 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007254
7255 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007256}
7257
7258
7259/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00007260 * ":mkspell [-ascii] outfile infile ..."
7261 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007262 */
7263 void
7264ex_mkspell(eap)
7265 exarg_T *eap;
7266{
7267 int fcount;
7268 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007269 char_u *arg = eap->arg;
7270 int ascii = FALSE;
7271
7272 if (STRNCMP(arg, "-ascii", 6) == 0)
7273 {
7274 ascii = TRUE;
7275 arg = skipwhite(arg + 6);
7276 }
7277
7278 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
7279 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
7280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007281 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007282 FreeWild(fcount, fnames);
7283 }
7284}
7285
7286/*
7287 * Create a Vim spell file from one or more word lists.
7288 * "fnames[0]" is the output file name.
7289 * "fnames[fcount - 1]" is the last input file name.
7290 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
7291 * and ".spl" is appended to make the output file name.
7292 */
7293 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007294mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007295 int fcount;
7296 char_u **fnames;
7297 int ascii; /* -ascii argument given */
7298 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007299 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007300{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007301 char_u fname[MAXPATHL];
7302 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00007303 char_u **innames;
7304 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007305 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007306 int i;
7307 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007308 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007309 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007310 spellinfo_T spin;
7311
7312 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007313 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007314 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007315 spin.si_followup = TRUE;
7316 spin.si_rem_accents = TRUE;
7317 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
7318 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
7319 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007320 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007321 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007322
Bram Moolenaarb765d632005-06-07 21:00:02 +00007323 /* default: fnames[0] is output file, following are input files */
7324 innames = &fnames[1];
7325 incount = fcount - 1;
7326
7327 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00007328 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007329 len = STRLEN(fnames[0]);
7330 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
7331 {
7332 /* For ":mkspell path/en.latin1.add" output file is
7333 * "path/en.latin1.add.spl". */
7334 innames = &fnames[0];
7335 incount = 1;
7336 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
7337 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007338 else if (fcount == 1)
7339 {
7340 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
7341 innames = &fnames[0];
7342 incount = 1;
7343 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
7344 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
7345 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007346 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
7347 {
7348 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007349 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007350 }
7351 else
7352 /* Name should be language, make the file name from it. */
7353 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
7354 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
7355
7356 /* Check for .ascii.spl. */
7357 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
7358 spin.si_ascii = TRUE;
7359
7360 /* Check for .add.spl. */
7361 if (strstr((char *)gettail(wfname), ".add.") != NULL)
7362 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00007363 }
7364
Bram Moolenaarb765d632005-06-07 21:00:02 +00007365 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007366 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007367 else if (vim_strchr(gettail(wfname), '_') != NULL)
7368 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00007369 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007370 EMSG(_("E754: Only up to 8 regions supported"));
7371 else
7372 {
7373 /* Check for overwriting before doing things that may take a lot of
7374 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007375 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007376 {
7377 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00007378 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007379 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007380 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007381 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007382 EMSG2(_(e_isadir2), wfname);
7383 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007384 }
7385
7386 /*
7387 * Init the aff and dic pointers.
7388 * Get the region names if there are more than 2 arguments.
7389 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007390 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007391 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007392 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007393
Bram Moolenaar3982c542005-06-08 21:56:31 +00007394 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007395 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007396 len = STRLEN(innames[i]);
7397 if (STRLEN(gettail(innames[i])) < 5
7398 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007399 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007400 EMSG2(_("E755: Invalid region in %s"), innames[i]);
7401 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007402 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00007403 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
7404 spin.si_region_name[i * 2 + 1] =
7405 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007406 }
7407 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00007408 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007409
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007410 spin.si_foldroot = wordtree_alloc(&spin);
7411 spin.si_keeproot = wordtree_alloc(&spin);
7412 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007413 if (spin.si_foldroot == NULL
7414 || spin.si_keeproot == NULL
7415 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007416 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007417 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007418 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007419 }
7420
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007421 /* When not producing a .add.spl file clear the character table when
7422 * we encounter one in the .aff file. This means we dump the current
7423 * one in the .spl file if the .aff file doesn't define one. That's
7424 * better than guessing the contents, the table will match a
7425 * previously loaded spell file. */
7426 if (!spin.si_add)
7427 spin.si_clear_chartab = TRUE;
7428
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007429 /*
7430 * Read all the .aff and .dic files.
7431 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007432 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007433 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007434 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007435 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007436 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007437 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007438
Bram Moolenaarb765d632005-06-07 21:00:02 +00007439 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007440 if (mch_stat((char *)fname, &st) >= 0)
7441 {
7442 /* Read the .aff file. Will init "spin->si_conv" based on the
7443 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007444 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007445 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007446 error = TRUE;
7447 else
7448 {
7449 /* Read the .dic file and store the words in the trees. */
7450 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00007451 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007452 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007453 error = TRUE;
7454 }
7455 }
7456 else
7457 {
7458 /* No .aff file, try reading the file as a word list. Store
7459 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007460 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007461 error = TRUE;
7462 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007463
Bram Moolenaarb765d632005-06-07 21:00:02 +00007464#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007465 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007466 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007467#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007468 }
7469
Bram Moolenaar78622822005-08-23 21:00:13 +00007470 if (spin.si_compflags != NULL && spin.si_nobreak)
7471 MSG(_("Warning: both compounding and NOBREAK specified"));
7472
Bram Moolenaar51485f02005-06-04 21:55:20 +00007473 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007474 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007475 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007476 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007477 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007478 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007479 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007480 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007481 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007482 MSG(_(msg_compressing));
Bram Moolenaarb765d632005-06-07 21:00:02 +00007483 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007484 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007485 verbose_leave();
7486 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007487 wordtree_compress(&spin, spin.si_foldroot);
7488 wordtree_compress(&spin, spin.si_keeproot);
7489 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007490 }
7491
Bram Moolenaar51485f02005-06-04 21:55:20 +00007492 if (!error)
7493 {
7494 /*
7495 * Write the info in the spell file.
7496 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007497 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007498 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007499 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007500 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007501 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007502 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007503 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007504 verbose_leave();
7505 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00007506
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007507 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007508
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007509 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007510 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007511 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007512 verbose_enter();
7513 MSG(_("Done!"));
7514 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00007515 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007516 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007517 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007518 verbose_leave();
7519 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007520
Bram Moolenaarb765d632005-06-07 21:00:02 +00007521 /* If the file is loaded need to reload it. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007522 if (!error)
7523 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007524 }
7525
7526 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007527 ga_clear(&spin.si_rep);
7528 ga_clear(&spin.si_sal);
7529 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007530 ga_clear(&spin.si_prefcond);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007531
7532 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007533 for (i = 0; i < incount; ++i)
7534 if (afile[i] != NULL)
7535 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007536
7537 /* Free all the bits and pieces at once. */
7538 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007539 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007540}
7541
Bram Moolenaarb765d632005-06-07 21:00:02 +00007542
7543/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007544 * ":[count]spellgood {word}"
7545 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00007546 */
7547 void
7548ex_spell(eap)
7549 exarg_T *eap;
7550{
Bram Moolenaar7887d882005-07-01 22:33:52 +00007551 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007552 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007553}
7554
7555/*
7556 * Add "word[len]" to 'spellfile' as a good or bad word.
7557 */
7558 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007559spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007560 char_u *word;
7561 int len;
7562 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007563 int index; /* "zG" and "zW": zero, otherwise index in
7564 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007565{
7566 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007567 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007568 int new_spf = FALSE;
7569 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007570 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007571 char_u fnamebuf[MAXPATHL];
7572 char_u line[MAXWLEN * 2];
7573 long fpos, fpos_next = 0;
7574 int i;
7575 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007576
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007577 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007578 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007579 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00007580 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007581 int_wordlist = vim_tempname('s');
7582 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00007583 return;
7584 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007585 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007586 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007587 else
7588 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007589 /* If 'spellfile' isn't set figure out a good default value. */
7590 if (*curbuf->b_p_spf == NUL)
7591 {
7592 init_spellfile();
7593 new_spf = TRUE;
7594 }
7595
7596 if (*curbuf->b_p_spf == NUL)
7597 {
7598 EMSG(_("E764: 'spellfile' is not set"));
7599 return;
7600 }
7601
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007602 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
7603 {
7604 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
7605 if (i == index)
7606 break;
7607 if (*spf == NUL)
7608 {
7609 EMSGN(_("E765: 'spellfile' does not have %ld enties"), index);
7610 return;
7611 }
7612 }
7613
Bram Moolenaarb765d632005-06-07 21:00:02 +00007614 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007615 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007616 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
7617 buf = NULL;
7618 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00007619 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007620 EMSG(_(e_bufloaded));
7621 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007622 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007623
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007624 fname = fnamebuf;
7625 }
7626
7627 if (bad)
7628 {
7629 /* When the word also appears as good word we need to remove that one,
7630 * since its flags sort before the one with WF_BANNED. */
7631 fd = mch_fopen((char *)fname, "r");
7632 if (fd != NULL)
7633 {
7634 while (!vim_fgets(line, MAXWLEN * 2, fd))
7635 {
7636 fpos = fpos_next;
7637 fpos_next = ftell(fd);
7638 if (STRNCMP(word, line, len) == 0
7639 && (line[len] == '/' || line[len] < ' '))
7640 {
7641 /* Found duplicate word. Remove it by writing a '#' at
7642 * the start of the line. Mixing reading and writing
7643 * doesn't work for all systems, close the file first. */
7644 fclose(fd);
7645 fd = mch_fopen((char *)fname, "r+");
7646 if (fd == NULL)
7647 break;
7648 if (fseek(fd, fpos, SEEK_SET) == 0)
7649 fputc('#', fd);
7650 fseek(fd, fpos_next, SEEK_SET);
7651 }
7652 }
7653 fclose(fd);
7654 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007655 }
7656
7657 fd = mch_fopen((char *)fname, "a");
7658 if (fd == NULL && new_spf)
7659 {
7660 /* We just initialized the 'spellfile' option and can't open the file.
7661 * We may need to create the "spell" directory first. We already
7662 * checked the runtime directory is writable in init_spellfile(). */
7663 STRCPY(NameBuff, fname);
7664 *gettail_sep(NameBuff) = NUL;
7665 if (mch_stat((char *)NameBuff, &st) < 0)
7666 {
7667 /* The directory doesn't exist. Try creating it and opening the
7668 * file again. */
7669 vim_mkdir(NameBuff, 0755);
7670 fd = mch_fopen((char *)fname, "a");
7671 }
7672 }
7673
7674 if (fd == NULL)
7675 EMSG2(_(e_notopen), fname);
7676 else
7677 {
7678 if (bad)
7679 fprintf(fd, "%.*s/!\n", len, word);
7680 else
7681 fprintf(fd, "%.*s\n", len, word);
7682 fclose(fd);
7683
7684 /* Update the .add.spl file. */
7685 mkspell(1, &fname, FALSE, TRUE, TRUE);
7686
7687 /* If the .add file is edited somewhere, reload it. */
7688 if (buf != NULL)
7689 buf_reload(buf);
7690
7691 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007692 }
7693}
7694
7695/*
7696 * Initialize 'spellfile' for the current buffer.
7697 */
7698 static void
7699init_spellfile()
7700{
7701 char_u buf[MAXPATHL];
7702 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007703 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007704 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007705 char_u *lend;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007706
7707 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
7708 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007709 /* Find the end of the language name. Exclude the region. */
7710 for (lend = curbuf->b_p_spl; *lend != NUL
7711 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
7712 ;
7713
7714 /* Loop over all entries in 'runtimepath'. Use the first one where we
7715 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007716 rtp = p_rtp;
7717 while (*rtp != NUL)
7718 {
7719 /* Copy the path from 'runtimepath' to buf[]. */
7720 copy_option_part(&rtp, buf, MAXPATHL, ",");
7721 if (filewritable(buf) == 2)
7722 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00007723 /* Use the first language name from 'spelllang' and the
7724 * encoding used in the first loaded .spl file. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007725 fname = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang->sl_fname;
7726 if (fname == NULL)
7727 break;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007728 l = STRLEN(buf);
7729 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00007730 "/spell/%.*s.%s.add",
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007731 (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl,
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007732 strstr((char *)gettail(fname), ".ascii.") != NULL
Bram Moolenaarb765d632005-06-07 21:00:02 +00007733 ? (char_u *)"ascii" : spell_enc());
7734 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
7735 break;
7736 }
7737 }
7738 }
7739}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007740
Bram Moolenaar51485f02005-06-04 21:55:20 +00007741
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007742/*
7743 * Init the chartab used for spelling for ASCII.
7744 * EBCDIC is not supported!
7745 */
7746 static void
7747clear_spell_chartab(sp)
7748 spelltab_T *sp;
7749{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007750 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007751
7752 /* Init everything to FALSE. */
7753 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
7754 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
7755 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007756 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007757 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007758 sp->st_upper[i] = i;
7759 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007760
7761 /* We include digits. A word shouldn't start with a digit, but handling
7762 * that is done separately. */
7763 for (i = '0'; i <= '9'; ++i)
7764 sp->st_isw[i] = TRUE;
7765 for (i = 'A'; i <= 'Z'; ++i)
7766 {
7767 sp->st_isw[i] = TRUE;
7768 sp->st_isu[i] = TRUE;
7769 sp->st_fold[i] = i + 0x20;
7770 }
7771 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007772 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007773 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007774 sp->st_upper[i] = i - 0x20;
7775 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007776}
7777
7778/*
7779 * Init the chartab used for spelling. Only depends on 'encoding'.
7780 * Called once while starting up and when 'encoding' changes.
7781 * The default is to use isalpha(), but the spell file should define the word
7782 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007783 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007784 */
7785 void
7786init_spell_chartab()
7787{
7788 int i;
7789
7790 did_set_spelltab = FALSE;
7791 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007792#ifdef FEAT_MBYTE
7793 if (enc_dbcs)
7794 {
7795 /* DBCS: assume double-wide characters are word characters. */
7796 for (i = 128; i <= 255; ++i)
7797 if (MB_BYTE2LEN(i) == 2)
7798 spelltab.st_isw[i] = TRUE;
7799 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007800 else if (enc_utf8)
7801 {
7802 for (i = 128; i < 256; ++i)
7803 {
7804 spelltab.st_isu[i] = utf_isupper(i);
7805 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
7806 spelltab.st_fold[i] = utf_fold(i);
7807 spelltab.st_upper[i] = utf_toupper(i);
7808 }
7809 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007810 else
7811#endif
7812 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007813 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007814 for (i = 128; i < 256; ++i)
7815 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007816 if (MB_ISUPPER(i))
7817 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007818 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007819 spelltab.st_isu[i] = TRUE;
7820 spelltab.st_fold[i] = MB_TOLOWER(i);
7821 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007822 else if (MB_ISLOWER(i))
7823 {
7824 spelltab.st_isw[i] = TRUE;
7825 spelltab.st_upper[i] = MB_TOUPPER(i);
7826 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007827 }
7828 }
7829}
7830
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007831/*
7832 * Set the spell character tables from strings in the affix file.
7833 */
7834 static int
7835set_spell_chartab(fol, low, upp)
7836 char_u *fol;
7837 char_u *low;
7838 char_u *upp;
7839{
7840 /* We build the new tables here first, so that we can compare with the
7841 * previous one. */
7842 spelltab_T new_st;
7843 char_u *pf = fol, *pl = low, *pu = upp;
7844 int f, l, u;
7845
7846 clear_spell_chartab(&new_st);
7847
7848 while (*pf != NUL)
7849 {
7850 if (*pl == NUL || *pu == NUL)
7851 {
7852 EMSG(_(e_affform));
7853 return FAIL;
7854 }
7855#ifdef FEAT_MBYTE
7856 f = mb_ptr2char_adv(&pf);
7857 l = mb_ptr2char_adv(&pl);
7858 u = mb_ptr2char_adv(&pu);
7859#else
7860 f = *pf++;
7861 l = *pl++;
7862 u = *pu++;
7863#endif
7864 /* Every character that appears is a word character. */
7865 if (f < 256)
7866 new_st.st_isw[f] = TRUE;
7867 if (l < 256)
7868 new_st.st_isw[l] = TRUE;
7869 if (u < 256)
7870 new_st.st_isw[u] = TRUE;
7871
7872 /* if "LOW" and "FOL" are not the same the "LOW" char needs
7873 * case-folding */
7874 if (l < 256 && l != f)
7875 {
7876 if (f >= 256)
7877 {
7878 EMSG(_(e_affrange));
7879 return FAIL;
7880 }
7881 new_st.st_fold[l] = f;
7882 }
7883
7884 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007885 * case-folding, it's upper case and the "UPP" is the upper case of
7886 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007887 if (u < 256 && u != f)
7888 {
7889 if (f >= 256)
7890 {
7891 EMSG(_(e_affrange));
7892 return FAIL;
7893 }
7894 new_st.st_fold[u] = f;
7895 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007896 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007897 }
7898 }
7899
7900 if (*pl != NUL || *pu != NUL)
7901 {
7902 EMSG(_(e_affform));
7903 return FAIL;
7904 }
7905
7906 return set_spell_finish(&new_st);
7907}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007908
7909/*
7910 * Set the spell character tables from strings in the .spl file.
7911 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007912 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007913set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007914 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007915 int cnt; /* length of "flags" */
7916 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007917{
7918 /* We build the new tables here first, so that we can compare with the
7919 * previous one. */
7920 spelltab_T new_st;
7921 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007922 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007923 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007924
7925 clear_spell_chartab(&new_st);
7926
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007927 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007928 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007929 if (i < cnt)
7930 {
7931 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
7932 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
7933 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007934
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007935 if (*p != NUL)
7936 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007937#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007938 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007939#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007940 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007941#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007942 new_st.st_fold[i + 128] = c;
7943 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
7944 new_st.st_upper[c] = i + 128;
7945 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007946 }
7947
Bram Moolenaar5195e452005-08-19 20:32:47 +00007948 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007949}
7950
7951 static int
7952set_spell_finish(new_st)
7953 spelltab_T *new_st;
7954{
7955 int i;
7956
7957 if (did_set_spelltab)
7958 {
7959 /* check that it's the same table */
7960 for (i = 0; i < 256; ++i)
7961 {
7962 if (spelltab.st_isw[i] != new_st->st_isw[i]
7963 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007964 || spelltab.st_fold[i] != new_st->st_fold[i]
7965 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007966 {
7967 EMSG(_("E763: Word characters differ between spell files"));
7968 return FAIL;
7969 }
7970 }
7971 }
7972 else
7973 {
7974 /* copy the new spelltab into the one being used */
7975 spelltab = *new_st;
7976 did_set_spelltab = TRUE;
7977 }
7978
7979 return OK;
7980}
7981
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007982/*
Bram Moolenaarea408852005-06-25 22:49:46 +00007983 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007984 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00007985 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007986 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00007987 */
7988 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007989spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00007990 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007991 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00007992{
Bram Moolenaarea408852005-06-25 22:49:46 +00007993#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007994 char_u *s;
7995 int l;
7996 int c;
7997
7998 if (has_mbyte)
7999 {
8000 l = MB_BYTE2LEN(*p);
8001 s = p;
8002 if (l == 1)
8003 {
8004 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008005 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008006 {
8007 s = p + 1; /* skip a mid-word character */
8008 l = MB_BYTE2LEN(*s);
8009 }
8010 }
8011 else
8012 {
8013 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008014 if (c < 256 ? buf->b_spell_ismw[c]
8015 : (buf->b_spell_ismw_mb != NULL
8016 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008017 {
8018 s = p + l;
8019 l = MB_BYTE2LEN(*s);
8020 }
8021 }
8022
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008023 c = mb_ptr2char(s);
8024 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008025 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008026 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008027 }
Bram Moolenaarea408852005-06-25 22:49:46 +00008028#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008029
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008030 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
8031}
8032
8033/*
8034 * Return TRUE if "p" points to a word character.
8035 * Unlike spell_iswordp() this doesn't check for "midword" characters.
8036 */
8037 static int
8038spell_iswordp_nmw(p)
8039 char_u *p;
8040{
8041#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008042 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008043
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008044 if (has_mbyte)
8045 {
8046 c = mb_ptr2char(p);
8047 if (c > 255)
8048 return mb_get_class(p) >= 2;
8049 return spelltab.st_isw[c];
8050 }
8051#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008052 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00008053}
8054
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008055#ifdef FEAT_MBYTE
8056/*
8057 * Return TRUE if "p" points to a word character.
8058 * Wide version of spell_iswordp().
8059 */
8060 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008061spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008062 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008063 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008064{
8065 int *s;
8066
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008067 if (*p < 256 ? buf->b_spell_ismw[*p]
8068 : (buf->b_spell_ismw_mb != NULL
8069 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008070 s = p + 1;
8071 else
8072 s = p;
8073
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008074 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008075 {
8076 if (enc_utf8)
8077 return utf_class(*s) >= 2;
8078 if (enc_dbcs)
8079 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
8080 return 0;
8081 }
8082 return spelltab.st_isw[*s];
8083}
8084#endif
8085
Bram Moolenaarea408852005-06-25 22:49:46 +00008086/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008087 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00008088 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008089 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008090 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008091write_spell_prefcond(fd, gap)
8092 FILE *fd;
8093 garray_T *gap;
8094{
8095 int i;
8096 char_u *p;
8097 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008098 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008099
Bram Moolenaar5195e452005-08-19 20:32:47 +00008100 if (fd != NULL)
8101 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
8102
8103 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008104
8105 for (i = 0; i < gap->ga_len; ++i)
8106 {
8107 /* <prefcond> : <condlen> <condstr> */
8108 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00008109 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008110 {
8111 len = STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008112 if (fd != NULL)
8113 {
8114 fputc(len, fd);
8115 fwrite(p, (size_t)len, (size_t)1, fd);
8116 }
8117 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008118 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008119 else if (fd != NULL)
8120 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008121 }
8122
Bram Moolenaar5195e452005-08-19 20:32:47 +00008123 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008124}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008125
8126/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008127 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
8128 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008129 * When using a multi-byte 'encoding' the length may change!
8130 * Returns FAIL when something wrong.
8131 */
8132 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008133spell_casefold(str, len, buf, buflen)
8134 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008135 int len;
8136 char_u *buf;
8137 int buflen;
8138{
8139 int i;
8140
8141 if (len >= buflen)
8142 {
8143 buf[0] = NUL;
8144 return FAIL; /* result will not fit */
8145 }
8146
8147#ifdef FEAT_MBYTE
8148 if (has_mbyte)
8149 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008150 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008151 char_u *p;
8152 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008153
8154 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008155 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008156 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008157 if (outi + MB_MAXBYTES > buflen)
8158 {
8159 buf[outi] = NUL;
8160 return FAIL;
8161 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008162 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008163 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008164 }
8165 buf[outi] = NUL;
8166 }
8167 else
8168#endif
8169 {
8170 /* Be quick for non-multibyte encodings. */
8171 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008172 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008173 buf[i] = NUL;
8174 }
8175
8176 return OK;
8177}
8178
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008179#define SPS_BEST 1
8180#define SPS_FAST 2
8181#define SPS_DOUBLE 4
8182
8183static int sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008184static int sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008185
8186/*
8187 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00008188 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008189 */
8190 int
8191spell_check_sps()
8192{
8193 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008194 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008195 char_u buf[MAXPATHL];
8196 int f;
8197
8198 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008199 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008200
8201 for (p = p_sps; *p != NUL; )
8202 {
8203 copy_option_part(&p, buf, MAXPATHL, ",");
8204
8205 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008206 if (VIM_ISDIGIT(*buf))
8207 {
8208 s = buf;
8209 sps_limit = getdigits(&s);
8210 if (*s != NUL && !VIM_ISDIGIT(*s))
8211 f = -1;
8212 }
8213 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008214 f = SPS_BEST;
8215 else if (STRCMP(buf, "fast") == 0)
8216 f = SPS_FAST;
8217 else if (STRCMP(buf, "double") == 0)
8218 f = SPS_DOUBLE;
8219 else if (STRNCMP(buf, "expr:", 5) != 0
8220 && STRNCMP(buf, "file:", 5) != 0)
8221 f = -1;
8222
8223 if (f == -1 || (sps_flags != 0 && f != 0))
8224 {
8225 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008226 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008227 return FAIL;
8228 }
8229 if (f != 0)
8230 sps_flags = f;
8231 }
8232
8233 if (sps_flags == 0)
8234 sps_flags = SPS_BEST;
8235
8236 return OK;
8237}
8238
8239/* Remember what "z?" replaced. */
8240static char_u *repl_from = NULL;
8241static char_u *repl_to = NULL;
8242
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008243/*
8244 * "z?": Find badly spelled word under or after the cursor.
8245 * Give suggestions for the properly spelled word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00008246 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008247 */
8248 void
Bram Moolenaard12a1322005-08-21 22:08:24 +00008249spell_suggest(count)
8250 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008251{
8252 char_u *line;
8253 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008254 char_u wcopy[MAXWLEN + 2];
8255 char_u *p;
8256 int i;
8257 int c;
8258 suginfo_T sug;
8259 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008260 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008261 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008262 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00008263 int selected = count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008264
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008265 /* Find the start of the badly spelled word. */
Bram Moolenaar95529562005-08-25 21:21:38 +00008266 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00008267 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008268 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008269 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
8270 return;
8271
8272 /* No bad word or it starts after the cursor: use the word under the
8273 * cursor. */
8274 curwin->w_cursor = prev_cursor;
8275 line = ml_get_curline();
8276 p = line + curwin->w_cursor.col;
8277 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008278 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00008279 mb_ptr_back(line, p);
8280 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008281 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00008282 mb_ptr_adv(p);
8283
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008284 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008285 {
8286 beep_flush();
8287 return;
8288 }
8289 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008290 }
8291
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008292 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008293
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008294 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008295 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008296
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008297 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008298
Bram Moolenaar5195e452005-08-19 20:32:47 +00008299 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
8300 * 'spellsuggest', whatever is smaller. */
8301 if (sps_limit > (int)Rows - 2)
8302 limit = (int)Rows - 2;
8303 else
8304 limit = sps_limit;
8305 spell_find_suggest(line + curwin->w_cursor.col, &sug, limit,
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008306 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008307
8308 if (sug.su_ga.ga_len == 0)
8309 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00008310 else if (count > 0)
8311 {
8312 if (count > sug.su_ga.ga_len)
8313 smsg((char_u *)_("Sorry, only %ld suggestions"),
8314 (long)sug.su_ga.ga_len);
8315 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008316 else
8317 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008318 vim_free(repl_from);
8319 repl_from = NULL;
8320 vim_free(repl_to);
8321 repl_to = NULL;
8322
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008323#ifdef FEAT_RIGHTLEFT
8324 /* When 'rightleft' is set the list is drawn right-left. */
8325 cmdmsg_rl = curwin->w_p_rl;
8326 if (cmdmsg_rl)
8327 msg_col = Columns - 1;
8328#endif
8329
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008330 /* List the suggestions. */
8331 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008332 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008333 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
8334 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008335#ifdef FEAT_RIGHTLEFT
8336 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
8337 {
8338 /* And now the rabbit from the high hat: Avoid showing the
8339 * untranslated message rightleft. */
8340 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
8341 sug.su_badlen, sug.su_badptr);
8342 }
8343#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008344 msg_puts(IObuff);
8345 msg_clr_eos();
8346 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00008347
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008348 msg_scroll = TRUE;
8349 for (i = 0; i < sug.su_ga.ga_len; ++i)
8350 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008351 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008352
8353 /* The suggested word may replace only part of the bad word, add
8354 * the not replaced part. */
8355 STRCPY(wcopy, stp->st_word);
8356 if (sug.su_badlen > stp->st_orglen)
8357 vim_strncpy(wcopy + STRLEN(wcopy),
8358 sug.su_badptr + stp->st_orglen,
8359 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008360 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
8361#ifdef FEAT_RIGHTLEFT
8362 if (cmdmsg_rl)
8363 rl_mirror(IObuff);
8364#endif
8365 msg_puts(IObuff);
8366
8367 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008368 msg_puts(IObuff);
8369
8370 /* The word may replace more than "su_badlen". */
8371 if (sug.su_badlen < stp->st_orglen)
8372 {
8373 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
8374 stp->st_orglen, sug.su_badptr);
8375 msg_puts(IObuff);
8376 }
8377
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008378 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008379 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008380 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008381 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008382 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008383 stp->st_salscore ? "s " : "",
8384 stp->st_score, stp->st_altscore);
8385 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008386 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00008387 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008388#ifdef FEAT_RIGHTLEFT
8389 if (cmdmsg_rl)
8390 /* Mirror the numbers, but keep the leading space. */
8391 rl_mirror(IObuff + 1);
8392#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00008393 msg_advance(30);
8394 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008395 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008396 msg_putchar('\n');
8397 }
8398
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008399#ifdef FEAT_RIGHTLEFT
8400 cmdmsg_rl = FALSE;
8401 msg_col = 0;
8402#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008403 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00008404 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008405 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00008406 selected -= lines_left;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008407 }
8408
Bram Moolenaard12a1322005-08-21 22:08:24 +00008409 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
8410 {
8411 /* Save the from and to text for :spellrepall. */
8412 stp = &SUG(sug.su_ga, selected - 1);
8413 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
8414 repl_to = vim_strsave(stp->st_word);
8415
8416 /* Replace the word. */
8417 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
8418 if (p != NULL)
8419 {
8420 c = sug.su_badptr - line;
8421 mch_memmove(p, line, c);
8422 STRCPY(p + c, stp->st_word);
8423 STRCAT(p, sug.su_badptr + stp->st_orglen);
8424 ml_replace(curwin->w_cursor.lnum, p, FALSE);
8425 curwin->w_cursor.col = c;
8426 changed_bytes(curwin->w_cursor.lnum, c);
8427
8428 /* For redo we use a change-word command. */
8429 ResetRedobuff();
8430 AppendToRedobuff((char_u *)"ciw");
8431 AppendToRedobuff(stp->st_word);
8432 AppendCharToRedobuff(ESC);
8433 }
8434 }
8435 else
8436 curwin->w_cursor = prev_cursor;
8437
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008438 spell_find_cleanup(&sug);
8439}
8440
8441/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008442 * Check if the word at line "lnum" column "col" is required to start with a
8443 * capital. This uses 'spellcapcheck' of the current buffer.
8444 */
8445 static int
8446check_need_cap(lnum, col)
8447 linenr_T lnum;
8448 colnr_T col;
8449{
8450 int need_cap = FALSE;
8451 char_u *line;
8452 char_u *line_copy = NULL;
8453 char_u *p;
8454 colnr_T endcol;
8455 regmatch_T regmatch;
8456
8457 if (curbuf->b_cap_prog == NULL)
8458 return FALSE;
8459
8460 line = ml_get_curline();
8461 endcol = 0;
8462 if ((int)(skipwhite(line) - line) >= (int)col)
8463 {
8464 /* At start of line, check if previous line is empty or sentence
8465 * ends there. */
8466 if (lnum == 1)
8467 need_cap = TRUE;
8468 else
8469 {
8470 line = ml_get(lnum - 1);
8471 if (*skipwhite(line) == NUL)
8472 need_cap = TRUE;
8473 else
8474 {
8475 /* Append a space in place of the line break. */
8476 line_copy = concat_str(line, (char_u *)" ");
8477 line = line_copy;
8478 endcol = STRLEN(line);
8479 }
8480 }
8481 }
8482 else
8483 endcol = col;
8484
8485 if (endcol > 0)
8486 {
8487 /* Check if sentence ends before the bad word. */
8488 regmatch.regprog = curbuf->b_cap_prog;
8489 regmatch.rm_ic = FALSE;
8490 p = line + endcol;
8491 for (;;)
8492 {
8493 mb_ptr_back(line, p);
8494 if (p == line || spell_iswordp_nmw(p))
8495 break;
8496 if (vim_regexec(&regmatch, p, 0)
8497 && regmatch.endp[0] == line + endcol)
8498 {
8499 need_cap = TRUE;
8500 break;
8501 }
8502 }
8503 }
8504
8505 vim_free(line_copy);
8506
8507 return need_cap;
8508}
8509
8510
8511/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008512 * ":spellrepall"
8513 */
8514/*ARGSUSED*/
8515 void
8516ex_spellrepall(eap)
8517 exarg_T *eap;
8518{
8519 pos_T pos = curwin->w_cursor;
8520 char_u *frompat;
8521 int addlen;
8522 char_u *line;
8523 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008524 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008525 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008526
8527 if (repl_from == NULL || repl_to == NULL)
8528 {
8529 EMSG(_("E752: No previous spell replacement"));
8530 return;
8531 }
8532 addlen = STRLEN(repl_to) - STRLEN(repl_from);
8533
8534 frompat = alloc(STRLEN(repl_from) + 7);
8535 if (frompat == NULL)
8536 return;
8537 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
8538 p_ws = FALSE;
8539
Bram Moolenaar5195e452005-08-19 20:32:47 +00008540 sub_nsubs = 0;
8541 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008542 curwin->w_cursor.lnum = 0;
8543 while (!got_int)
8544 {
8545 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
8546 || u_save_cursor() == FAIL)
8547 break;
8548
8549 /* Only replace when the right word isn't there yet. This happens
8550 * when changing "etc" to "etc.". */
8551 line = ml_get_curline();
8552 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
8553 repl_to, STRLEN(repl_to)) != 0)
8554 {
8555 p = alloc(STRLEN(line) + addlen + 1);
8556 if (p == NULL)
8557 break;
8558 mch_memmove(p, line, curwin->w_cursor.col);
8559 STRCPY(p + curwin->w_cursor.col, repl_to);
8560 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
8561 ml_replace(curwin->w_cursor.lnum, p, FALSE);
8562 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008563
8564 if (curwin->w_cursor.lnum != prev_lnum)
8565 {
8566 ++sub_nlines;
8567 prev_lnum = curwin->w_cursor.lnum;
8568 }
8569 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008570 }
8571 curwin->w_cursor.col += STRLEN(repl_to);
8572 }
8573
8574 p_ws = save_ws;
8575 curwin->w_cursor = pos;
8576 vim_free(frompat);
8577
Bram Moolenaar5195e452005-08-19 20:32:47 +00008578 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008579 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008580 else
8581 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008582}
8583
8584/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008585 * Find spell suggestions for "word". Return them in the growarray "*gap" as
8586 * a list of allocated strings.
8587 */
8588 void
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008589spell_suggest_list(gap, word, maxcount, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008590 garray_T *gap;
8591 char_u *word;
8592 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008593 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008594{
8595 suginfo_T sug;
8596 int i;
8597 suggest_T *stp;
8598 char_u *wcopy;
8599
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008600 spell_find_suggest(word, &sug, maxcount, FALSE, need_cap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008601
8602 /* Make room in "gap". */
8603 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
8604 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
8605 return;
8606
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008607 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008608 {
8609 stp = &SUG(sug.su_ga, i);
8610
8611 /* The suggested word may replace only part of "word", add the not
8612 * replaced part. */
8613 wcopy = alloc(STRLEN(stp->st_word)
8614 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
8615 if (wcopy == NULL)
8616 break;
8617 STRCPY(wcopy, stp->st_word);
8618 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
8619 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
8620 }
8621
8622 spell_find_cleanup(&sug);
8623}
8624
8625/*
8626 * Find spell suggestions for the word at the start of "badptr".
8627 * Return the suggestions in "su->su_ga".
8628 * The maximum number of suggestions is "maxcount".
8629 * Note: does use info for the current window.
8630 * This is based on the mechanisms of Aspell, but completely reimplemented.
8631 */
8632 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008633spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008634 char_u *badptr;
8635 suginfo_T *su;
8636 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00008637 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008638 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008639{
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008640 int attr = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008641 char_u buf[MAXPATHL];
8642 char_u *p;
8643 int do_combine = FALSE;
8644 char_u *sps_copy;
8645#ifdef FEAT_EVAL
8646 static int expr_busy = FALSE;
8647#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008648 int c;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008649
8650 /*
8651 * Set the info in "*su".
8652 */
8653 vim_memset(su, 0, sizeof(suginfo_T));
8654 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
8655 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008656 if (*badptr == NUL)
8657 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008658 hash_init(&su->su_banned);
8659
8660 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008661 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008662 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008663 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008664
8665 if (su->su_badlen >= MAXWLEN)
8666 su->su_badlen = MAXWLEN - 1; /* just in case */
8667 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
8668 (void)spell_casefold(su->su_badptr, su->su_badlen,
8669 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008670 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008671 su->su_badflags = badword_captype(su->su_badptr,
8672 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008673 if (need_cap)
8674 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008675
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008676 /* If the word is not capitalised and spell_check() doesn't consider the
8677 * word to be bad then it might need to be capitalised. Add a suggestion
8678 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008679 c = PTR2CHAR(su->su_badptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008680 if (!SPELL_ISUPPER(c) && attr == 0)
8681 {
8682 make_case_word(su->su_badword, buf, WF_ONECAP);
8683 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
8684 0, TRUE);
8685 }
8686
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008687 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00008688 if (banbadword)
8689 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008690
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008691 /* Make a copy of 'spellsuggest', because the expression may change it. */
8692 sps_copy = vim_strsave(p_sps);
8693 if (sps_copy == NULL)
8694 return;
8695
8696 /* Loop over the items in 'spellsuggest'. */
8697 for (p = sps_copy; *p != NUL; )
8698 {
8699 copy_option_part(&p, buf, MAXPATHL, ",");
8700
8701 if (STRNCMP(buf, "expr:", 5) == 0)
8702 {
8703#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008704 /* Evaluate an expression. Skip this when called recursively,
8705 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008706 if (!expr_busy)
8707 {
8708 expr_busy = TRUE;
8709 spell_suggest_expr(su, buf + 5);
8710 expr_busy = FALSE;
8711 }
8712#endif
8713 }
8714 else if (STRNCMP(buf, "file:", 5) == 0)
8715 /* Use list of suggestions in a file. */
8716 spell_suggest_file(su, buf + 5);
8717 else
8718 {
8719 /* Use internal method. */
8720 spell_suggest_intern(su);
8721 if (sps_flags & SPS_DOUBLE)
8722 do_combine = TRUE;
8723 }
8724 }
8725
8726 vim_free(sps_copy);
8727
8728 if (do_combine)
8729 /* Combine the two list of suggestions. This must be done last,
8730 * because sorting changes the order again. */
8731 score_combine(su);
8732}
8733
8734#ifdef FEAT_EVAL
8735/*
8736 * Find suggestions by evaluating expression "expr".
8737 */
8738 static void
8739spell_suggest_expr(su, expr)
8740 suginfo_T *su;
8741 char_u *expr;
8742{
8743 list_T *list;
8744 listitem_T *li;
8745 int score;
8746 char_u *p;
8747
8748 /* The work is split up in a few parts to avoid having to export
8749 * suginfo_T.
8750 * First evaluate the expression and get the resulting list. */
8751 list = eval_spell_expr(su->su_badword, expr);
8752 if (list != NULL)
8753 {
8754 /* Loop over the items in the list. */
8755 for (li = list->lv_first; li != NULL; li = li->li_next)
8756 if (li->li_tv.v_type == VAR_LIST)
8757 {
8758 /* Get the word and the score from the items. */
8759 score = get_spellword(li->li_tv.vval.v_list, &p);
8760 if (score >= 0)
8761 add_suggestion(su, &su->su_ga, p,
8762 su->su_badlen, score, 0, TRUE);
8763 }
8764 list_unref(list);
8765 }
8766
8767 /* Sort the suggestions and truncate at "maxcount". */
8768 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8769}
8770#endif
8771
8772/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008773 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008774 */
8775 static void
8776spell_suggest_file(su, fname)
8777 suginfo_T *su;
8778 char_u *fname;
8779{
8780 FILE *fd;
8781 char_u line[MAXWLEN * 2];
8782 char_u *p;
8783 int len;
8784 char_u cword[MAXWLEN];
8785
8786 /* Open the file. */
8787 fd = mch_fopen((char *)fname, "r");
8788 if (fd == NULL)
8789 {
8790 EMSG2(_(e_notopen), fname);
8791 return;
8792 }
8793
8794 /* Read it line by line. */
8795 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
8796 {
8797 line_breakcheck();
8798
8799 p = vim_strchr(line, '/');
8800 if (p == NULL)
8801 continue; /* No Tab found, just skip the line. */
8802 *p++ = NUL;
8803 if (STRICMP(su->su_badword, line) == 0)
8804 {
8805 /* Match! Isolate the good word, until CR or NL. */
8806 for (len = 0; p[len] >= ' '; ++len)
8807 ;
8808 p[len] = NUL;
8809
8810 /* If the suggestion doesn't have specific case duplicate the case
8811 * of the bad word. */
8812 if (captype(p, NULL) == 0)
8813 {
8814 make_case_word(p, cword, su->su_badflags);
8815 p = cword;
8816 }
8817
8818 add_suggestion(su, &su->su_ga, p, su->su_badlen,
8819 SCORE_FILE, 0, TRUE);
8820 }
8821 }
8822
8823 fclose(fd);
8824
8825 /* Sort the suggestions and truncate at "maxcount". */
8826 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8827}
8828
8829/*
8830 * Find suggestions for the internal method indicated by "sps_flags".
8831 */
8832 static void
8833spell_suggest_intern(su)
8834 suginfo_T *su;
8835{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008836 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008837 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008838 *
8839 * Set a maximum score to limit the combination of operations that is
8840 * tried.
8841 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008842 suggest_try_special(su);
8843
8844 /*
8845 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
8846 * from the .aff file and inserting a space (split the word).
8847 */
8848 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008849
8850 /* For the resulting top-scorers compute the sound-a-like score. */
8851 if (sps_flags & SPS_DOUBLE)
8852 score_comp_sal(su);
8853
8854 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008855 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008856 *
8857 * Only do this when we don't have a lot of suggestions yet, because it's
8858 * very slow and often doesn't find new suggestions.
8859 */
8860 if ((sps_flags & SPS_DOUBLE)
8861 || (!(sps_flags & SPS_FAST)
8862 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
8863 {
8864 /* Allow a higher score now. */
8865 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008866 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008867 }
8868
8869 /* When CTRL-C was hit while searching do show the results. */
8870 ui_breakcheck();
8871 if (got_int)
8872 {
8873 (void)vgetc();
8874 got_int = FALSE;
8875 }
8876
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008877 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008878 {
8879 if (sps_flags & SPS_BEST)
8880 /* Adjust the word score for how it sounds like. */
8881 rescore_suggestions(su);
8882
8883 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008884 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008885 }
8886}
8887
8888/*
8889 * Free the info put in "*su" by spell_find_suggest().
8890 */
8891 static void
8892spell_find_cleanup(su)
8893 suginfo_T *su;
8894{
8895 int i;
8896
8897 /* Free the suggestions. */
8898 for (i = 0; i < su->su_ga.ga_len; ++i)
8899 vim_free(SUG(su->su_ga, i).st_word);
8900 ga_clear(&su->su_ga);
8901 for (i = 0; i < su->su_sga.ga_len; ++i)
8902 vim_free(SUG(su->su_sga, i).st_word);
8903 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008904
8905 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008906 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008907}
8908
8909/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008910 * Make a copy of "word", with the first letter upper or lower cased, to
8911 * "wcopy[MAXWLEN]". "word" must not be empty.
8912 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008913 */
8914 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008915onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008916 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008917 char_u *wcopy;
8918 int upper; /* TRUE: first letter made upper case */
8919{
8920 char_u *p;
8921 int c;
8922 int l;
8923
8924 p = word;
8925#ifdef FEAT_MBYTE
8926 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008927 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008928 else
8929#endif
8930 c = *p++;
8931 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008932 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008933 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008934 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008935#ifdef FEAT_MBYTE
8936 if (has_mbyte)
8937 l = mb_char2bytes(c, wcopy);
8938 else
8939#endif
8940 {
8941 l = 1;
8942 wcopy[0] = c;
8943 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008944 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008945}
8946
8947/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008948 * Make a copy of "word" with all the letters upper cased into
8949 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008950 */
8951 static void
8952allcap_copy(word, wcopy)
8953 char_u *word;
8954 char_u *wcopy;
8955{
8956 char_u *s;
8957 char_u *d;
8958 int c;
8959
8960 d = wcopy;
8961 for (s = word; *s != NUL; )
8962 {
8963#ifdef FEAT_MBYTE
8964 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008965 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008966 else
8967#endif
8968 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00008969
8970#ifdef FEAT_MBYTE
8971 /* We only change ß to SS when we are certain latin1 is used. It
8972 * would cause weird errors in other 8-bit encodings. */
8973 if (enc_latin1like && c == 0xdf)
8974 {
8975 c = 'S';
8976 if (d - wcopy >= MAXWLEN - 1)
8977 break;
8978 *d++ = c;
8979 }
8980 else
8981#endif
8982 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008983
8984#ifdef FEAT_MBYTE
8985 if (has_mbyte)
8986 {
8987 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
8988 break;
8989 d += mb_char2bytes(c, d);
8990 }
8991 else
8992#endif
8993 {
8994 if (d - wcopy >= MAXWLEN - 1)
8995 break;
8996 *d++ = c;
8997 }
8998 }
8999 *d = NUL;
9000}
9001
9002/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00009003 * Try finding suggestions by recognizing specific situations.
9004 */
9005 static void
9006suggest_try_special(su)
9007 suginfo_T *su;
9008{
9009 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009010 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009011 int c;
9012 char_u word[MAXWLEN];
9013
9014 /*
9015 * Recognize a word that is repeated: "the the".
9016 */
9017 p = skiptowhite(su->su_fbadword);
9018 len = p - su->su_fbadword;
9019 p = skipwhite(p);
9020 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
9021 {
9022 /* Include badflags: if the badword is onecap or allcap
9023 * use that for the goodword too: "The the" -> "The". */
9024 c = su->su_fbadword[len];
9025 su->su_fbadword[len] = NUL;
9026 make_case_word(su->su_fbadword, word, su->su_badflags);
9027 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009028 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009029 }
9030}
9031
9032/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009033 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00009034 *
9035 * This uses a state machine. At each node in the tree we try various
9036 * operations. When trying if an operation work "depth" is increased and the
9037 * stack[] is used to store info. This allows combinations, thus insert one
9038 * character, replace one and delete another. The number of changes is
9039 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaard12a1322005-08-21 22:08:24 +00009040 *
9041 * After implementing this I noticed an article by Kemal Oflazer that
9042 * describes something similar: "Error-tolerant Finite State Recognition with
9043 * Applications to Morphological Analysis and Spelling Correction" (1996).
9044 * The implementation in the article is simplified and requires a stack of
9045 * unknown depth. The implementation here only needs a stack depth of the
9046 * length of the word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009047 */
9048 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00009049suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009050 suginfo_T *su;
9051{
9052 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
9053 char_u tword[MAXWLEN]; /* good word collected so far */
9054 trystate_T stack[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009055 char_u preword[MAXWLEN * 3]; /* word found with proper case;
9056 * concatanation of prefix compound
9057 * words and split word. NUL terminated
9058 * when going deeper but not when coming
9059 * back. */
9060 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009061 trystate_T *sp;
9062 int newscore;
9063 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009064 char_u *byts, *fbyts, *pbyts;
9065 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009066 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009067 int c, c2, c3;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009068 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009069 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009070 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009071 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009072 int len;
9073 char_u *p;
9074 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00009075 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009076 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009077 slang_T *slang;
9078 int fword_ends;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009079 int lpi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009080
9081 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00009082 * to find matches (esp. REP items). Append some more text, changing
9083 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009084 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009085 n = STRLEN(fword);
9086 p = su->su_badptr + su->su_badlen;
9087 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009088
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009089 for (lpi = 0; lpi < curwin->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009090 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009091 lp = LANGP_ENTRY(curwin->w_buffer->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009092 slang = lp->lp_slang;
9093
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009094 /* If reloading a spell file fails it's still in the list but
9095 * everything has been cleared. */
9096 if (slang->sl_fbyts == NULL)
9097 continue;
9098
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009099 /*
9100 * Go through the whole case-fold tree, try changes at each node.
9101 * "tword[]" contains the word collected from nodes in the tree.
9102 * "fword[]" the word we are trying to match with (initially the bad
9103 * word).
9104 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009105 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009106 sp = &stack[0];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009107 vim_memset(sp, 0, sizeof(trystate_T));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009108 sp->ts_curi = 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009109
Bram Moolenaarea424162005-06-16 21:51:00 +00009110 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009111 * When there are postponed prefixes we need to use these first. At
9112 * the end of the prefix we continue in the case-fold tree.
9113 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009114 fbyts = slang->sl_fbyts;
9115 fidxs = slang->sl_fidxs;
9116 pbyts = slang->sl_pbyts;
9117 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009118 if (pbyts != NULL)
9119 {
9120 byts = pbyts;
9121 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009122 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009123 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
9124 }
9125 else
9126 {
9127 byts = fbyts;
9128 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009129 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009130 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009131 }
9132
9133 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00009134 * Loop to find all suggestions. At each round we either:
9135 * - For the current state try one operation, advance "ts_curi",
9136 * increase "depth".
9137 * - When a state is done go to the next, set "ts_state".
9138 * - When all states are tried decrease "depth".
9139 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009140 while (depth >= 0 && !got_int)
9141 {
9142 sp = &stack[depth];
9143 switch (sp->ts_state)
9144 {
9145 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009146 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009147 /*
9148 * Start of node: Deal with NUL bytes, which means
9149 * tword[] may end here.
9150 */
9151 arridx = sp->ts_arridx; /* current node in the tree */
9152 len = byts[arridx]; /* bytes in this node */
9153 arridx += sp->ts_curi; /* index of current byte */
9154
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009155 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009156 {
9157 /* Skip over the NUL bytes, we use them later. */
9158 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
9159 ;
9160 sp->ts_curi += n;
9161
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009162 /* Always past NUL bytes now. */
9163 n = (int)sp->ts_state;
9164 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00009165 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009166
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009167 /* At end of a prefix or at start of prefixtree: check for
9168 * following word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009169 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009170 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00009171 /* Set su->su_badflags to the caps type at this
9172 * position. Use the caps type until here for the
9173 * prefix itself. */
9174#ifdef FEAT_MBYTE
9175 if (has_mbyte)
9176 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
9177 else
9178#endif
9179 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009180 flags = badword_captype(su->su_badptr,
9181 su->su_badptr + n);
9182 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00009183 su->su_badptr + su->su_badlen);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009184 ++depth;
9185 stack[depth] = stack[depth - 1];
9186 sp = &stack[depth];
9187 sp->ts_prefixdepth = depth - 1;
9188 byts = fbyts;
9189 idxs = fidxs;
9190 sp->ts_state = STATE_START;
9191 sp->ts_curi = 1; /* start just after length byte */
9192 sp->ts_arridx = 0;
9193
Bram Moolenaar53805d12005-08-01 07:08:33 +00009194 /* Move the prefix to preword[] with the right case
9195 * and make find_keepcap_word() works. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00009196 tword[sp->ts_twordlen] = NUL;
9197 make_case_word(tword + sp->ts_splitoff,
9198 preword + sp->ts_prewordlen,
9199 flags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009200 sp->ts_prewordlen = STRLEN(preword);
Bram Moolenaard12a1322005-08-21 22:08:24 +00009201 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009202 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009203 break;
9204 }
9205
Bram Moolenaar0c405862005-06-22 22:26:26 +00009206 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009207 {
9208 /* Past bytes in node and/or past NUL bytes. */
9209 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00009210 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009211 break;
9212 }
9213
9214 /*
9215 * End of word in tree.
9216 */
9217 ++sp->ts_curi; /* eat one NUL byte */
9218
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009219 flags = (int)idxs[arridx];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009220 fword_ends = (fword[sp->ts_fidx] == NUL
9221 || !spell_iswordp(fword + sp->ts_fidx, curbuf));
9222 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009223
Bram Moolenaard12a1322005-08-21 22:08:24 +00009224 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
9225 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009226 {
9227 /* There was a prefix before the word. Check that the
9228 * prefix can be used with this word. */
9229 /* Count the length of the NULs in the prefix. If there
9230 * are none this must be the first try without a prefix.
9231 */
9232 n = stack[sp->ts_prefixdepth].ts_arridx;
9233 len = pbyts[n++];
9234 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
9235 ;
9236 if (c > 0)
9237 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009238 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00009239 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009240 if (c == 0)
9241 break;
9242
9243 /* Use the WF_RARE flag for a rare prefix. */
9244 if (c & WF_RAREPFX)
9245 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009246
9247 /* Tricky: when checking for both prefix and
9248 * compounding we run into the prefix flag first.
9249 * Remember that it's OK, so that we accept the prefix
9250 * when arriving at a compound flag. */
9251 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009252 }
9253 }
9254
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009255 /* Check NEEDCOMPOUND: can't use word without compounding. */
9256 if (sp->ts_complen == sp->ts_compsplit && fword_ends
9257 && (flags & WF_NEEDCOMP))
9258 break;
9259
Bram Moolenaard12a1322005-08-21 22:08:24 +00009260 if (sp->ts_complen > sp->ts_compsplit)
9261 {
Bram Moolenaar78622822005-08-23 21:00:13 +00009262 if (slang->sl_nobreak)
9263 {
9264 /* There was a word before this word. When there was
9265 * no change in this word (it was correct) add the
9266 * first word as a suggestion. If this word was
9267 * corrected too, we need to check if a correct word
9268 * follows. */
9269 if (sp->ts_fidx - sp->ts_splitfidx
9270 == sp->ts_twordlen - sp->ts_splitoff
9271 && STRNCMP(fword + sp->ts_splitfidx,
9272 tword + sp->ts_splitoff,
9273 sp->ts_fidx - sp->ts_splitfidx) == 0)
9274 {
9275 preword[sp->ts_prewordlen] = NUL;
9276 add_suggestion(su, &su->su_ga, preword,
9277 sp->ts_splitfidx - repextra,
9278 sp->ts_score, 0, FALSE);
9279 break;
9280 }
9281 }
9282 else
9283 {
9284 /* There was a compound word before this word. If
9285 * this word does not support compounding then give up
9286 * (splitting is tried for the word without compound
9287 * flag). */
9288 if (((unsigned)flags >> 24) == 0
9289 || sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaard12a1322005-08-21 22:08:24 +00009290 < slang->sl_compminlen)
Bram Moolenaar78622822005-08-23 21:00:13 +00009291 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009292#ifdef FEAT_MBYTE
9293 /* For multi-byte chars check character length against
9294 * COMPOUNDMIN. */
9295 if (has_mbyte
9296 && slang->sl_compminlen < MAXWLEN
9297 && mb_charlen(tword + sp->ts_splitoff)
9298 < slang->sl_compminlen)
9299 break;
9300#endif
9301
Bram Moolenaar78622822005-08-23 21:00:13 +00009302 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
9303 compflags[sp->ts_complen + 1] = NUL;
9304 vim_strncpy(preword + sp->ts_prewordlen,
9305 tword + sp->ts_splitoff,
9306 sp->ts_twordlen - sp->ts_splitoff);
9307 p = preword;
9308 while (*skiptowhite(p) != NUL)
9309 p = skipwhite(skiptowhite(p));
9310 if (fword_ends && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00009311 compflags + sp->ts_compsplit))
Bram Moolenaar78622822005-08-23 21:00:13 +00009312 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00009313
Bram Moolenaar78622822005-08-23 21:00:13 +00009314 /* Get pointer to last char of previous word. */
9315 p = preword + sp->ts_prewordlen;
9316 mb_ptr_back(preword, p);
9317 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00009318 }
Bram Moolenaare52325c2005-08-22 22:54:29 +00009319 else
9320 p = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009321
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009322 /*
9323 * Form the word with proper case in preword.
9324 * If there is a word from a previous split, append.
9325 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009326 if (flags & WF_KEEPCAP)
9327 /* Must find the word in the keep-case tree. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009328 find_keepcap_word(slang, tword + sp->ts_splitoff,
9329 preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009330 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00009331 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009332 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00009333 * use that for the goodword too. But if the badword is
9334 * allcap and it's only one char long use onecap. */
9335 c = su->su_badflags;
9336 if ((c & WF_ALLCAP)
9337#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009338 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
Bram Moolenaar0c405862005-06-22 22:26:26 +00009339#else
9340 && su->su_badlen == 1
9341#endif
9342 )
9343 c = WF_ONECAP;
Bram Moolenaare52325c2005-08-22 22:54:29 +00009344 c |= flags;
9345
9346 /* When appending a compound word after a word character
9347 * don't use Onecap. */
9348 if (p != NULL && spell_iswordp_nmw(p))
9349 c &= ~WF_ONECAP;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009350 make_case_word(tword + sp->ts_splitoff,
Bram Moolenaare52325c2005-08-22 22:54:29 +00009351 preword + sp->ts_prewordlen, c);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009352 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009353
9354 /* Don't use a banned word. It may appear again as a good
9355 * word, thus remember it. */
9356 if (flags & WF_BANNED)
9357 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00009358 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009359 break;
9360 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009361 if (was_banned(su, preword + sp->ts_prewordlen)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009362 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009363 break;
9364
9365 newscore = 0;
9366 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009367 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009368 newscore += SCORE_REGION;
9369 if (flags & WF_RARE)
9370 newscore += SCORE_RARE;
9371
Bram Moolenaar0c405862005-06-22 22:26:26 +00009372 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00009373 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009374 newscore += SCORE_ICASE;
9375
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009376 if (fword_ends && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009377 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009378 /* The badword also ends: add suggestions. Give a penalty
9379 * when changing non-word char to word char, e.g., "thes,"
9380 * -> "these". */
9381 p = fword + sp->ts_fidx;
9382#ifdef FEAT_MBYTE
9383 if (has_mbyte)
9384 mb_ptr_back(fword, p);
9385 else
9386#endif
9387 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009388 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009389 {
9390 p = preword + STRLEN(preword);
9391#ifdef FEAT_MBYTE
9392 if (has_mbyte)
9393 mb_ptr_back(preword, p);
9394 else
9395#endif
9396 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009397 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009398 newscore += SCORE_NONWORD;
9399 }
9400
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009401 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00009402 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009403 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009404 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009405 else if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +00009406#ifdef FEAT_MBYTE
9407 /* Don't split halfway a character. */
9408 && (!has_mbyte || sp->ts_tcharlen == 0)
9409#endif
9410 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009411 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009412 int try_compound;
9413
9414 /* Get here in two situations:
9415 * 1. The word in the tree ends but the badword continues:
9416 * If the word allows compounding try that. Otherwise
9417 * try a split by inserting a space. For both check
9418 * that a valid words starts at fword[sp->ts_fidx].
Bram Moolenaar78622822005-08-23 21:00:13 +00009419 * For NOBREAK do like compounding to be able to check
9420 * if the next word is valid.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009421 * 2. The badword does end, but it was due to a change
9422 * (e.g., a swap). No need to split, but do check that
9423 * the following word is valid.
9424 */
Bram Moolenaard12a1322005-08-21 22:08:24 +00009425 try_compound = FALSE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009426 if (!fword_ends
Bram Moolenaar6de68532005-08-24 22:08:48 +00009427 && slang->sl_compprog != NULL
Bram Moolenaar5195e452005-08-19 20:32:47 +00009428 && ((unsigned)flags >> 24) != 0
9429 && sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009430 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009431#ifdef FEAT_MBYTE
9432 && (!has_mbyte
9433 || slang->sl_compminlen == MAXWLEN
9434 || mb_charlen(tword + sp->ts_splitoff)
9435 >= slang->sl_compminlen)
9436#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00009437 && (slang->sl_compsylmax < MAXWLEN
9438 || sp->ts_complen + 1 - sp->ts_compsplit
9439 < slang->sl_compmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00009440 && (byte_in_str(sp->ts_complen == sp->ts_compsplit
Bram Moolenaard12a1322005-08-21 22:08:24 +00009441 ? slang->sl_compstartflags
9442 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00009443 ((unsigned)flags >> 24))))
Bram Moolenaar5195e452005-08-19 20:32:47 +00009444 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009445 try_compound = TRUE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009446 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
9447 compflags[sp->ts_complen + 1] = NUL;
9448 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00009449
Bram Moolenaar78622822005-08-23 21:00:13 +00009450 /* For NOBREAK we never try splitting, it won't make any
9451 * word valid. */
9452 if (slang->sl_nobreak)
9453 try_compound = TRUE;
9454
Bram Moolenaard12a1322005-08-21 22:08:24 +00009455 /* If we could add a compound word, and it's also possible
9456 * to split at this point, do the split first and set
9457 * TSF_DIDSPLIT to avoid doing it again. */
Bram Moolenaar78622822005-08-23 21:00:13 +00009458 else if (!fword_ends
Bram Moolenaard12a1322005-08-21 22:08:24 +00009459 && try_compound
9460 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009461 {
9462 try_compound = FALSE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009463 sp->ts_flags |= TSF_DIDSPLIT;
9464 --sp->ts_curi; /* do the same NUL again */
9465 compflags[sp->ts_complen] = NUL;
9466 }
9467 else
9468 sp->ts_flags &= ~TSF_DIDSPLIT;
9469
9470 if (!try_compound && !fword_ends)
9471 {
9472 /* If we're going to split need to check that the
9473 * words so far are valid for compounding. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00009474 p = preword;
9475 while (*skiptowhite(p) != NUL)
9476 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +00009477 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +00009478 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00009479 compflags + sp->ts_compsplit))
9480 break;
9481 newscore += SCORE_SPLIT;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009482 }
9483
9484 if (try_deeper(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009485 {
9486 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009487 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009488 sp->ts_state = STATE_SPLITUNDO;
9489
9490 ++depth;
9491 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009492
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009493 /* Append a space to preword when splitting. */
9494 if (!try_compound && !fword_ends)
9495 STRCAT(preword, " ");
Bram Moolenaar5195e452005-08-19 20:32:47 +00009496 sp->ts_prewordlen = STRLEN(preword);
9497 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +00009498 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009499
9500 /* If the badword has a non-word character at this
9501 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009502 * non-word character with a space. Always skip a
9503 * character when the word ends. */
9504 if ((!try_compound
9505 && !spell_iswordp_nmw(fword + sp->ts_fidx))
9506 || fword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009507 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009508 int l;
9509
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009510#ifdef FEAT_MBYTE
9511 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009512 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009513 else
9514#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009515 l = 1;
9516 if (fword_ends)
9517 {
9518 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009519 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009520 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009521 sp->ts_prewordlen += l;
9522 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009523 }
9524 else
9525 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
9526 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009527 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00009528
Bram Moolenaard12a1322005-08-21 22:08:24 +00009529 /* When compounding include compound flag in
9530 * compflags[] (already set above). When splitting we
9531 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009532 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +00009533 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009534 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00009535 sp->ts_compsplit = sp->ts_complen;
9536 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009537
Bram Moolenaar53805d12005-08-01 07:08:33 +00009538 /* set su->su_badflags to the caps type at this
9539 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009540#ifdef FEAT_MBYTE
9541 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00009542 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009543 else
9544#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00009545 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009546 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00009547 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009548
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009549 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009550 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009551
9552 /* If there are postponed prefixes, try these too. */
9553 if (pbyts != NULL)
9554 {
9555 byts = pbyts;
9556 idxs = pidxs;
9557 sp->ts_prefixdepth = PFD_PREFIXTREE;
9558 sp->ts_state = STATE_NOPREFIX;
9559 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009560 }
9561 }
9562 break;
9563
9564 case STATE_SPLITUNDO:
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009565 /* Undo the changes done for word split or compound word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009566 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009567
9568 /* Continue looking for NUL bytes. */
9569 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009570
9571 /* In case we went into the prefix tree. */
9572 byts = fbyts;
9573 idxs = fidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009574 break;
9575
9576 case STATE_ENDNUL:
9577 /* Past the NUL bytes in the node. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00009578 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009579 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009580 {
9581 /* The badword ends, can't use the bytes in this node. */
9582 sp->ts_state = STATE_DEL;
9583 break;
9584 }
9585 sp->ts_state = STATE_PLAIN;
9586 /*FALLTHROUGH*/
9587
9588 case STATE_PLAIN:
9589 /*
9590 * Go over all possible bytes at this node, add each to
9591 * tword[] and use child node. "ts_curi" is the index.
9592 */
9593 arridx = sp->ts_arridx;
9594 if (sp->ts_curi > byts[arridx])
9595 {
9596 /* Done all bytes at this node, do next state. When still
9597 * at already changed bytes skip the other tricks. */
9598 if (sp->ts_fidx >= sp->ts_fidxtry)
9599 sp->ts_state = STATE_DEL;
9600 else
9601 sp->ts_state = STATE_FINAL;
9602 }
9603 else
9604 {
9605 arridx += sp->ts_curi++;
9606 c = byts[arridx];
9607
9608 /* Normal byte, go one level deeper. If it's not equal to
9609 * the byte in the bad word adjust the score. But don't
9610 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00009611 if (c == fword[sp->ts_fidx]
9612#ifdef FEAT_MBYTE
9613 || (sp->ts_tcharlen > 0
9614 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009615#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00009616 )
9617 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009618 else
9619 newscore = SCORE_SUBST;
9620 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
9621 && try_deeper(su, stack, depth, newscore))
9622 {
9623 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009624 sp = &stack[depth];
9625 ++sp->ts_fidx;
9626 tword[sp->ts_twordlen++] = c;
9627 sp->ts_arridx = idxs[arridx];
9628#ifdef FEAT_MBYTE
9629 if (newscore == SCORE_SUBST)
9630 sp->ts_isdiff = DIFF_YES;
9631 if (has_mbyte)
9632 {
9633 /* Multi-byte characters are a bit complicated to
9634 * handle: They differ when any of the bytes
9635 * differ and then their length may also differ. */
9636 if (sp->ts_tcharlen == 0)
9637 {
9638 /* First byte. */
9639 sp->ts_tcharidx = 0;
9640 sp->ts_tcharlen = MB_BYTE2LEN(c);
9641 sp->ts_fcharstart = sp->ts_fidx - 1;
9642 sp->ts_isdiff = (newscore != 0)
9643 ? DIFF_YES : DIFF_NONE;
9644 }
9645 else if (sp->ts_isdiff == DIFF_INSERT)
9646 /* When inserting trail bytes don't advance in
9647 * the bad word. */
9648 --sp->ts_fidx;
9649 if (++sp->ts_tcharidx == sp->ts_tcharlen)
9650 {
9651 /* Last byte of character. */
9652 if (sp->ts_isdiff == DIFF_YES)
9653 {
9654 /* Correct ts_fidx for the byte length of
9655 * the character (we didn't check that
9656 * before). */
9657 sp->ts_fidx = sp->ts_fcharstart
9658 + MB_BYTE2LEN(
9659 fword[sp->ts_fcharstart]);
9660
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00009661 /* For changing a composing character
9662 * adjust the score from SCORE_SUBST to
9663 * SCORE_SUBCOMP. */
9664 if (enc_utf8
9665 && utf_iscomposing(
9666 mb_ptr2char(tword
9667 + sp->ts_twordlen
9668 - sp->ts_tcharlen))
9669 && utf_iscomposing(
9670 mb_ptr2char(fword
9671 + sp->ts_fcharstart)))
9672 sp->ts_score -=
9673 SCORE_SUBST - SCORE_SUBCOMP;
9674
Bram Moolenaarea424162005-06-16 21:51:00 +00009675 /* For a similar character adjust score
9676 * from SCORE_SUBST to SCORE_SIMILAR. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009677 else if (slang->sl_has_map
9678 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00009679 mb_ptr2char(tword
9680 + sp->ts_twordlen
9681 - sp->ts_tcharlen),
9682 mb_ptr2char(fword
9683 + sp->ts_fcharstart)))
9684 sp->ts_score -=
9685 SCORE_SUBST - SCORE_SIMILAR;
9686 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009687 else if (sp->ts_isdiff == DIFF_INSERT
9688 && sp->ts_twordlen > sp->ts_tcharlen)
9689 {
Bram Moolenaarea408852005-06-25 22:49:46 +00009690 p = tword + sp->ts_twordlen
9691 - sp->ts_tcharlen;
9692 c = mb_ptr2char(p);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009693 if (enc_utf8 && utf_iscomposing(c))
9694 {
9695 /* Inserting a composing char doesn't
9696 * count that much. */
Bram Moolenaarea408852005-06-25 22:49:46 +00009697 sp->ts_score -= SCORE_INS
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009698 - SCORE_INSCOMP;
9699 }
9700 else
9701 {
9702 /* If the previous character was the
9703 * same, thus doubling a character,
9704 * give a bonus to the score. */
9705 mb_ptr_back(tword, p);
9706 if (c == mb_ptr2char(p))
9707 sp->ts_score -= SCORE_INS
Bram Moolenaarea408852005-06-25 22:49:46 +00009708 - SCORE_INSDUP;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009709 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009710 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009711
9712 /* Starting a new char, reset the length. */
9713 sp->ts_tcharlen = 0;
9714 }
9715 }
9716 else
9717#endif
9718 {
9719 /* If we found a similar char adjust the score.
9720 * We do this after calling try_deeper() because
9721 * it's slow. */
9722 if (newscore != 0
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009723 && slang->sl_has_map
9724 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00009725 c, fword[sp->ts_fidx - 1]))
9726 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
9727 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009728 }
9729 }
9730 break;
9731
9732 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00009733#ifdef FEAT_MBYTE
9734 /* When past the first byte of a multi-byte char don't try
9735 * delete/insert/swap a character. */
9736 if (has_mbyte && sp->ts_tcharlen > 0)
9737 {
9738 sp->ts_state = STATE_FINAL;
9739 break;
9740 }
9741#endif
9742 /*
9743 * Try skipping one character in the bad word (delete it).
9744 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009745 sp->ts_state = STATE_INS;
9746 sp->ts_curi = 1;
9747 if (fword[sp->ts_fidx] != NUL
9748 && try_deeper(su, stack, depth, SCORE_DEL))
9749 {
9750 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00009751
9752 /* Advance over the character in fword[]. Give a bonus to
9753 * the score if the same character is following "nn" ->
9754 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00009755#ifdef FEAT_MBYTE
9756 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00009757 {
9758 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00009759 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00009760 if (enc_utf8 && utf_iscomposing(c))
9761 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
9762 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
Bram Moolenaarea408852005-06-25 22:49:46 +00009763 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
9764 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009765 else
9766#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00009767 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009768 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00009769 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
9770 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
9771 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009772 break;
9773 }
9774 /*FALLTHROUGH*/
9775
9776 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00009777 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009778 * node. */
9779 n = sp->ts_arridx;
9780 if (sp->ts_curi > byts[n])
9781 {
9782 /* Done all bytes at this node, do next state. */
9783 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009784 }
9785 else
9786 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009787 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009788 n += sp->ts_curi++;
9789 c = byts[n];
9790 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
9791 {
9792 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009793 sp = &stack[depth];
9794 tword[sp->ts_twordlen++] = c;
9795 sp->ts_arridx = idxs[n];
9796#ifdef FEAT_MBYTE
9797 if (has_mbyte)
9798 {
9799 fl = MB_BYTE2LEN(c);
9800 if (fl > 1)
9801 {
9802 /* There are following bytes for the same
9803 * character. We must find all bytes before
9804 * trying delete/insert/swap/etc. */
9805 sp->ts_tcharlen = fl;
9806 sp->ts_tcharidx = 1;
9807 sp->ts_isdiff = DIFF_INSERT;
9808 }
9809 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009810 else
9811 fl = 1;
9812 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00009813#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00009814 {
9815 /* If the previous character was the same, thus
9816 * doubling a character, give a bonus to the
9817 * score. */
9818 if (sp->ts_twordlen >= 2
9819 && tword[sp->ts_twordlen - 2] == c)
9820 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
9821 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009822 }
9823 }
9824 break;
9825
9826 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00009827 /*
9828 * Swap two bytes in the bad word: "12" -> "21".
9829 * We change "fword" here, it's changed back afterwards.
9830 */
9831 p = fword + sp->ts_fidx;
9832 c = *p;
9833 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009834 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009835 /* End of word, can't swap or replace. */
9836 sp->ts_state = STATE_FINAL;
9837 break;
9838 }
9839#ifdef FEAT_MBYTE
9840 if (has_mbyte)
9841 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009842 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009843 c = mb_ptr2char(p);
9844 c2 = mb_ptr2char(p + n);
9845 }
9846 else
9847#endif
9848 c2 = p[1];
9849 if (c == c2)
9850 {
9851 /* Characters are identical, swap won't do anything. */
9852 sp->ts_state = STATE_SWAP3;
9853 break;
9854 }
9855 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
9856 {
9857 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009858 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009859#ifdef FEAT_MBYTE
9860 if (has_mbyte)
9861 {
9862 fl = mb_char2len(c2);
9863 mch_memmove(p, p + n, fl);
9864 mb_char2bytes(c, p + fl);
9865 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
9866 }
9867 else
9868#endif
9869 {
9870 p[0] = c2;
9871 p[1] = c;
9872 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
9873 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009874 }
9875 else
9876 /* If this swap doesn't work then SWAP3 won't either. */
9877 sp->ts_state = STATE_REP_INI;
9878 break;
9879
Bram Moolenaarea424162005-06-16 21:51:00 +00009880 case STATE_UNSWAP:
9881 /* Undo the STATE_SWAP swap: "21" -> "12". */
9882 p = fword + sp->ts_fidx;
9883#ifdef FEAT_MBYTE
9884 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009885 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009886 n = MB_BYTE2LEN(*p);
9887 c = mb_ptr2char(p + n);
9888 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
9889 mb_char2bytes(c, p);
9890 }
9891 else
9892#endif
9893 {
9894 c = *p;
9895 *p = p[1];
9896 p[1] = c;
9897 }
9898 /*FALLTHROUGH*/
9899
9900 case STATE_SWAP3:
9901 /* Swap two bytes, skipping one: "123" -> "321". We change
9902 * "fword" here, it's changed back afterwards. */
9903 p = fword + sp->ts_fidx;
9904#ifdef FEAT_MBYTE
9905 if (has_mbyte)
9906 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009907 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009908 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009909 fl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00009910 c2 = mb_ptr2char(p + n);
9911 c3 = mb_ptr2char(p + n + fl);
9912 }
9913 else
9914#endif
9915 {
9916 c = *p;
9917 c2 = p[1];
9918 c3 = p[2];
9919 }
9920
9921 /* When characters are identical: "121" then SWAP3 result is
9922 * identical, ROT3L result is same as SWAP: "211", ROT3L
9923 * result is same as SWAP on next char: "112". Thus skip all
9924 * swapping. Also skip when c3 is NUL. */
9925 if (c == c3 || c3 == NUL)
9926 {
9927 sp->ts_state = STATE_REP_INI;
9928 break;
9929 }
9930 if (try_deeper(su, stack, depth, SCORE_SWAP3))
9931 {
9932 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009933 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009934#ifdef FEAT_MBYTE
9935 if (has_mbyte)
9936 {
9937 tl = mb_char2len(c3);
9938 mch_memmove(p, p + n + fl, tl);
9939 mb_char2bytes(c2, p + tl);
9940 mb_char2bytes(c, p + fl + tl);
9941 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
9942 }
9943 else
9944#endif
9945 {
9946 p[0] = p[2];
9947 p[2] = c;
9948 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
9949 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009950 }
9951 else
9952 sp->ts_state = STATE_REP_INI;
9953 break;
9954
Bram Moolenaarea424162005-06-16 21:51:00 +00009955 case STATE_UNSWAP3:
9956 /* Undo STATE_SWAP3: "321" -> "123" */
9957 p = fword + sp->ts_fidx;
9958#ifdef FEAT_MBYTE
9959 if (has_mbyte)
9960 {
9961 n = MB_BYTE2LEN(*p);
9962 c2 = mb_ptr2char(p + n);
9963 fl = MB_BYTE2LEN(p[n]);
9964 c = mb_ptr2char(p + n + fl);
9965 tl = MB_BYTE2LEN(p[n + fl]);
9966 mch_memmove(p + fl + tl, p, n);
9967 mb_char2bytes(c, p);
9968 mb_char2bytes(c2, p + tl);
9969 }
9970 else
9971#endif
9972 {
9973 c = *p;
9974 *p = p[2];
9975 p[2] = c;
9976 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009977
Bram Moolenaarea424162005-06-16 21:51:00 +00009978 /* Rotate three characters left: "123" -> "231". We change
9979 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009980 if (try_deeper(su, stack, depth, SCORE_SWAP3))
9981 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009982 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009983 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009984 p = fword + sp->ts_fidx;
9985#ifdef FEAT_MBYTE
9986 if (has_mbyte)
9987 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009988 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009989 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009990 fl = mb_cptr2len(p + n);
9991 fl += mb_cptr2len(p + n + fl);
Bram Moolenaarea424162005-06-16 21:51:00 +00009992 mch_memmove(p, p + n, fl);
9993 mb_char2bytes(c, p + fl);
9994 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
9995 }
9996 else
9997#endif
9998 {
9999 c = *p;
10000 *p = p[1];
10001 p[1] = p[2];
10002 p[2] = c;
10003 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
10004 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010005 }
10006 else
10007 sp->ts_state = STATE_REP_INI;
10008 break;
10009
Bram Moolenaarea424162005-06-16 21:51:00 +000010010 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +000010011 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +000010012 p = fword + sp->ts_fidx;
10013#ifdef FEAT_MBYTE
10014 if (has_mbyte)
10015 {
10016 n = MB_BYTE2LEN(*p);
10017 n += MB_BYTE2LEN(p[n]);
10018 c = mb_ptr2char(p + n);
10019 tl = MB_BYTE2LEN(p[n]);
10020 mch_memmove(p + tl, p, n);
10021 mb_char2bytes(c, p);
10022 }
10023 else
10024#endif
10025 {
10026 c = p[2];
10027 p[2] = p[1];
10028 p[1] = *p;
10029 *p = c;
10030 }
Bram Moolenaarea424162005-06-16 21:51:00 +000010031
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010032 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +000010033 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010034 if (try_deeper(su, stack, depth, SCORE_SWAP3))
10035 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010036 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010037 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000010038 p = fword + sp->ts_fidx;
10039#ifdef FEAT_MBYTE
10040 if (has_mbyte)
10041 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010042 n = mb_cptr2len(p);
10043 n += mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +000010044 c = mb_ptr2char(p + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010045 tl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +000010046 mch_memmove(p + tl, p, n);
10047 mb_char2bytes(c, p);
10048 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
10049 }
10050 else
10051#endif
10052 {
10053 c = p[2];
10054 p[2] = p[1];
10055 p[1] = *p;
10056 *p = c;
10057 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
10058 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010059 }
10060 else
10061 sp->ts_state = STATE_REP_INI;
10062 break;
10063
Bram Moolenaarea424162005-06-16 21:51:00 +000010064 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +000010065 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +000010066 p = fword + sp->ts_fidx;
10067#ifdef FEAT_MBYTE
10068 if (has_mbyte)
10069 {
10070 c = mb_ptr2char(p);
10071 tl = MB_BYTE2LEN(*p);
10072 n = MB_BYTE2LEN(p[tl]);
10073 n += MB_BYTE2LEN(p[tl + n]);
10074 mch_memmove(p, p + tl, n);
10075 mb_char2bytes(c, p + n);
10076 }
10077 else
10078#endif
10079 {
10080 c = *p;
10081 *p = p[1];
10082 p[1] = p[2];
10083 p[2] = c;
10084 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010085 /*FALLTHROUGH*/
10086
10087 case STATE_REP_INI:
10088 /* Check if matching with REP items from the .aff file would
10089 * work. Quickly skip if there are no REP items or the score
10090 * is going to be too high anyway. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010091 gap = &slang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010092 if (gap->ga_len == 0
10093 || sp->ts_score + SCORE_REP >= su->su_maxscore)
10094 {
10095 sp->ts_state = STATE_FINAL;
10096 break;
10097 }
10098
10099 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +000010100 * may match. If the index is -1 there is none. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010101 sp->ts_curi = slang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010102 if (sp->ts_curi < 0)
10103 {
10104 sp->ts_state = STATE_FINAL;
10105 break;
10106 }
10107
10108 sp->ts_state = STATE_REP;
10109 /*FALLTHROUGH*/
10110
10111 case STATE_REP:
10112 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +000010113 * match replace the characters and check if the resulting
10114 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010115 p = fword + sp->ts_fidx;
10116
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010117 gap = &slang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010118 while (sp->ts_curi < gap->ga_len)
10119 {
10120 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
10121 if (*ftp->ft_from != *p)
10122 {
10123 /* past possible matching entries */
10124 sp->ts_curi = gap->ga_len;
10125 break;
10126 }
10127 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
10128 && try_deeper(su, stack, depth, SCORE_REP))
10129 {
10130 /* Need to undo this afterwards. */
10131 sp->ts_state = STATE_REP_UNDO;
10132
10133 /* Change the "from" to the "to" string. */
10134 ++depth;
10135 fl = STRLEN(ftp->ft_from);
10136 tl = STRLEN(ftp->ft_to);
10137 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +000010138 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010139 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010140 repextra += tl - fl;
10141 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010142 mch_memmove(p, ftp->ft_to, tl);
10143 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +000010144#ifdef FEAT_MBYTE
10145 stack[depth].ts_tcharlen = 0;
10146#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010147 break;
10148 }
10149 }
10150
10151 if (sp->ts_curi >= gap->ga_len)
10152 /* No (more) matches. */
10153 sp->ts_state = STATE_FINAL;
10154
10155 break;
10156
10157 case STATE_REP_UNDO:
10158 /* Undo a REP replacement and continue with the next one. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010159 ftp = (fromto_T *)slang->sl_rep.ga_data + sp->ts_curi - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010160 fl = STRLEN(ftp->ft_from);
10161 tl = STRLEN(ftp->ft_to);
10162 p = fword + sp->ts_fidx;
10163 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +000010164 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010165 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010166 repextra -= tl - fl;
10167 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010168 mch_memmove(p, ftp->ft_from, fl);
10169 sp->ts_state = STATE_REP;
10170 break;
10171
10172 default:
10173 /* Did all possible states at this level, go up one level. */
10174 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010175
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010176 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010177 {
10178 /* Continue in or go back to the prefix tree. */
10179 byts = pbyts;
10180 idxs = pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010181 }
10182
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010183 /* Don't check for CTRL-C too often, it takes time. */
10184 line_breakcheck();
10185 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010186 }
10187 }
10188}
10189
10190/*
10191 * Try going one level deeper in the tree.
10192 */
10193 static int
10194try_deeper(su, stack, depth, score_add)
10195 suginfo_T *su;
10196 trystate_T *stack;
10197 int depth;
10198 int score_add;
10199{
10200 int newscore;
10201
10202 /* Refuse to go deeper if the scrore is getting too big. */
10203 newscore = stack[depth].ts_score + score_add;
10204 if (newscore >= su->su_maxscore)
10205 return FALSE;
10206
Bram Moolenaarea424162005-06-16 21:51:00 +000010207 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010208 stack[depth + 1].ts_state = STATE_START;
10209 stack[depth + 1].ts_score = newscore;
10210 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010211 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010212 return TRUE;
10213}
10214
Bram Moolenaar53805d12005-08-01 07:08:33 +000010215#ifdef FEAT_MBYTE
10216/*
10217 * Case-folding may change the number of bytes: Count nr of chars in
10218 * fword[flen] and return the byte length of that many chars in "word".
10219 */
10220 static int
10221nofold_len(fword, flen, word)
10222 char_u *fword;
10223 int flen;
10224 char_u *word;
10225{
10226 char_u *p;
10227 int i = 0;
10228
10229 for (p = fword; p < fword + flen; mb_ptr_adv(p))
10230 ++i;
10231 for (p = word; i > 0; mb_ptr_adv(p))
10232 --i;
10233 return (int)(p - word);
10234}
10235#endif
10236
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010237/*
10238 * "fword" is a good word with case folded. Find the matching keep-case
10239 * words and put it in "kword".
10240 * Theoretically there could be several keep-case words that result in the
10241 * same case-folded word, but we only find one...
10242 */
10243 static void
10244find_keepcap_word(slang, fword, kword)
10245 slang_T *slang;
10246 char_u *fword;
10247 char_u *kword;
10248{
10249 char_u uword[MAXWLEN]; /* "fword" in upper-case */
10250 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010251 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010252
10253 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010254 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010255 int round[MAXWLEN];
10256 int fwordidx[MAXWLEN];
10257 int uwordidx[MAXWLEN];
10258 int kwordlen[MAXWLEN];
10259
10260 int flen, ulen;
10261 int l;
10262 int len;
10263 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010264 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010265 char_u *p;
10266 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010267 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010268
10269 if (byts == NULL)
10270 {
10271 /* array is empty: "cannot happen" */
10272 *kword = NUL;
10273 return;
10274 }
10275
10276 /* Make an all-cap version of "fword". */
10277 allcap_copy(fword, uword);
10278
10279 /*
10280 * Each character needs to be tried both case-folded and upper-case.
10281 * All this gets very complicated if we keep in mind that changing case
10282 * may change the byte length of a multi-byte character...
10283 */
10284 depth = 0;
10285 arridx[0] = 0;
10286 round[0] = 0;
10287 fwordidx[0] = 0;
10288 uwordidx[0] = 0;
10289 kwordlen[0] = 0;
10290 while (depth >= 0)
10291 {
10292 if (fword[fwordidx[depth]] == NUL)
10293 {
10294 /* We are at the end of "fword". If the tree allows a word to end
10295 * here we have found a match. */
10296 if (byts[arridx[depth] + 1] == 0)
10297 {
10298 kword[kwordlen[depth]] = NUL;
10299 return;
10300 }
10301
10302 /* kword is getting too long, continue one level up */
10303 --depth;
10304 }
10305 else if (++round[depth] > 2)
10306 {
10307 /* tried both fold-case and upper-case character, continue one
10308 * level up */
10309 --depth;
10310 }
10311 else
10312 {
10313 /*
10314 * round[depth] == 1: Try using the folded-case character.
10315 * round[depth] == 2: Try using the upper-case character.
10316 */
10317#ifdef FEAT_MBYTE
10318 if (has_mbyte)
10319 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010320 flen = mb_cptr2len(fword + fwordidx[depth]);
10321 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010322 }
10323 else
10324#endif
10325 ulen = flen = 1;
10326 if (round[depth] == 1)
10327 {
10328 p = fword + fwordidx[depth];
10329 l = flen;
10330 }
10331 else
10332 {
10333 p = uword + uwordidx[depth];
10334 l = ulen;
10335 }
10336
10337 for (tryidx = arridx[depth]; l > 0; --l)
10338 {
10339 /* Perform a binary search in the list of accepted bytes. */
10340 len = byts[tryidx++];
10341 c = *p++;
10342 lo = tryidx;
10343 hi = tryidx + len - 1;
10344 while (lo < hi)
10345 {
10346 m = (lo + hi) / 2;
10347 if (byts[m] > c)
10348 hi = m - 1;
10349 else if (byts[m] < c)
10350 lo = m + 1;
10351 else
10352 {
10353 lo = hi = m;
10354 break;
10355 }
10356 }
10357
10358 /* Stop if there is no matching byte. */
10359 if (hi < lo || byts[lo] != c)
10360 break;
10361
10362 /* Continue at the child (if there is one). */
10363 tryidx = idxs[lo];
10364 }
10365
10366 if (l == 0)
10367 {
10368 /*
10369 * Found the matching char. Copy it to "kword" and go a
10370 * level deeper.
10371 */
10372 if (round[depth] == 1)
10373 {
10374 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
10375 flen);
10376 kwordlen[depth + 1] = kwordlen[depth] + flen;
10377 }
10378 else
10379 {
10380 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
10381 ulen);
10382 kwordlen[depth + 1] = kwordlen[depth] + ulen;
10383 }
10384 fwordidx[depth + 1] = fwordidx[depth] + flen;
10385 uwordidx[depth + 1] = uwordidx[depth] + ulen;
10386
10387 ++depth;
10388 arridx[depth] = tryidx;
10389 round[depth] = 0;
10390 }
10391 }
10392 }
10393
10394 /* Didn't find it: "cannot happen". */
10395 *kword = NUL;
10396}
10397
10398/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010399 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
10400 * su->su_sga.
10401 */
10402 static void
10403score_comp_sal(su)
10404 suginfo_T *su;
10405{
10406 langp_T *lp;
10407 char_u badsound[MAXWLEN];
10408 int i;
10409 suggest_T *stp;
10410 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010411 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010412 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010413
10414 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
10415 return;
10416
10417 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010418 for (lpi = 0; lpi < curwin->w_buffer->b_langp.ga_len; ++lpi)
10419 {
10420 lp = LANGP_ENTRY(curwin->w_buffer->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010421 if (lp->lp_slang->sl_sal.ga_len > 0)
10422 {
10423 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010424 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010425
10426 for (i = 0; i < su->su_ga.ga_len; ++i)
10427 {
10428 stp = &SUG(su->su_ga, i);
10429
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010430 /* Case-fold the suggested word, sound-fold it and compute the
10431 * sound-a-like score. */
10432 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010433 if (score < SCORE_MAXMAX)
10434 {
10435 /* Add the suggestion. */
10436 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
10437 sstp->st_word = vim_strsave(stp->st_word);
10438 if (sstp->st_word != NULL)
10439 {
10440 sstp->st_score = score;
10441 sstp->st_altscore = 0;
10442 sstp->st_orglen = stp->st_orglen;
10443 ++su->su_sga.ga_len;
10444 }
10445 }
10446 }
10447 break;
10448 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010449 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010450}
10451
10452/*
10453 * Combine the list of suggestions in su->su_ga and su->su_sga.
10454 * They are intwined.
10455 */
10456 static void
10457score_combine(su)
10458 suginfo_T *su;
10459{
10460 int i;
10461 int j;
10462 garray_T ga;
10463 garray_T *gap;
10464 langp_T *lp;
10465 suggest_T *stp;
10466 char_u *p;
10467 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010468 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010469 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010470
10471 /* Add the alternate score to su_ga. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010472 for (lpi = 0; lpi < curwin->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010473 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010474 lp = LANGP_ENTRY(curwin->w_buffer->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010475 if (lp->lp_slang->sl_sal.ga_len > 0)
10476 {
10477 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010478 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010479
10480 for (i = 0; i < su->su_ga.ga_len; ++i)
10481 {
10482 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010483 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
10484 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010485 if (stp->st_altscore == SCORE_MAXMAX)
10486 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
10487 else
10488 stp->st_score = (stp->st_score * 3
10489 + stp->st_altscore) / 4;
10490 stp->st_salscore = FALSE;
10491 }
10492 break;
10493 }
10494 }
10495
10496 /* Add the alternate score to su_sga. */
10497 for (i = 0; i < su->su_sga.ga_len; ++i)
10498 {
10499 stp = &SUG(su->su_sga, i);
10500 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
10501 if (stp->st_score == SCORE_MAXMAX)
10502 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
10503 else
10504 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
10505 stp->st_salscore = TRUE;
10506 }
10507
10508 /* Sort the suggestions and truncate at "maxcount" for both lists. */
10509 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10510 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
10511
10512 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
10513 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
10514 return;
10515
10516 stp = &SUG(ga, 0);
10517 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
10518 {
10519 /* round 1: get a suggestion from su_ga
10520 * round 2: get a suggestion from su_sga */
10521 for (round = 1; round <= 2; ++round)
10522 {
10523 gap = round == 1 ? &su->su_ga : &su->su_sga;
10524 if (i < gap->ga_len)
10525 {
10526 /* Don't add a word if it's already there. */
10527 p = SUG(*gap, i).st_word;
10528 for (j = 0; j < ga.ga_len; ++j)
10529 if (STRCMP(stp[j].st_word, p) == 0)
10530 break;
10531 if (j == ga.ga_len)
10532 stp[ga.ga_len++] = SUG(*gap, i);
10533 else
10534 vim_free(p);
10535 }
10536 }
10537 }
10538
10539 ga_clear(&su->su_ga);
10540 ga_clear(&su->su_sga);
10541
10542 /* Truncate the list to the number of suggestions that will be displayed. */
10543 if (ga.ga_len > su->su_maxcount)
10544 {
10545 for (i = su->su_maxcount; i < ga.ga_len; ++i)
10546 vim_free(stp[i].st_word);
10547 ga.ga_len = su->su_maxcount;
10548 }
10549
10550 su->su_ga = ga;
10551}
10552
10553/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010554 * For the goodword in "stp" compute the soundalike score compared to the
10555 * badword.
10556 */
10557 static int
10558stp_sal_score(stp, su, slang, badsound)
10559 suggest_T *stp;
10560 suginfo_T *su;
10561 slang_T *slang;
10562 char_u *badsound; /* sound-folded badword */
10563{
10564 char_u *p;
10565 char_u badsound2[MAXWLEN];
10566 char_u fword[MAXWLEN];
10567 char_u goodsound[MAXWLEN];
10568
10569 if (stp->st_orglen <= su->su_badlen)
10570 p = badsound;
10571 else
10572 {
10573 /* soundfold the bad word with more characters following */
10574 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
10575
10576 /* When joining two words the sound often changes a lot. E.g., "t he"
10577 * sounds like "t h" while "the" sounds like "@". Avoid that by
10578 * removing the space. Don't do it when the good word also contains a
10579 * space. */
10580 if (vim_iswhite(su->su_badptr[su->su_badlen])
10581 && *skiptowhite(stp->st_word) == NUL)
10582 for (p = fword; *(p = skiptowhite(p)) != NUL; )
10583 mch_memmove(p, p + 1, STRLEN(p));
10584
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010585 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010586 p = badsound2;
10587 }
10588
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010589 /* Sound-fold the word and compute the score for the difference. */
10590 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010591
10592 return soundalike_score(goodsound, p);
10593}
10594
10595/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010596 * Find suggestions by comparing the word in a sound-a-like form.
10597 */
10598 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000010599suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010600 suginfo_T *su;
10601{
10602 char_u salword[MAXWLEN];
10603 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010604 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010605 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010606 int curi[MAXWLEN];
10607 langp_T *lp;
10608 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010609 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010610 int depth;
10611 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010612 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010613 int round;
10614 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010615 int sound_score;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010616 int local_score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010617 int lpi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010618
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010619 /* Do this for all languages that support sound folding. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010620 for (lpi = 0; lpi < curwin->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010621 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010622 lp = LANGP_ENTRY(curwin->w_buffer->b_langp, lpi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010623 if (lp->lp_slang->sl_sal.ga_len > 0)
10624 {
10625 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010626 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010627
10628 /*
10629 * Go through the whole tree, soundfold each word and compare.
10630 * round 1: use the case-folded tree.
10631 * round 2: use the keep-case tree.
10632 */
10633 for (round = 1; round <= 2; ++round)
10634 {
10635 if (round == 1)
10636 {
10637 byts = lp->lp_slang->sl_fbyts;
10638 idxs = lp->lp_slang->sl_fidxs;
10639 }
10640 else
10641 {
10642 byts = lp->lp_slang->sl_kbyts;
10643 idxs = lp->lp_slang->sl_kidxs;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010644 if (byts == NULL) /* no keep-case words */
10645 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010646 }
10647
10648 depth = 0;
10649 arridx[0] = 0;
10650 curi[0] = 1;
10651 while (depth >= 0 && !got_int)
10652 {
10653 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010654 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010655 /* Done all bytes at this node, go up one level. */
10656 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010657 line_breakcheck();
10658 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010659 else
10660 {
10661 /* Do one more byte at this node. */
10662 n = arridx[depth] + curi[depth];
10663 ++curi[depth];
10664 c = byts[n];
10665 if (c == 0)
10666 {
10667 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010668 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010669 if (round == 2 || (flags & WF_KEEPCAP) == 0)
10670 {
10671 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010672 /* Sound-fold. Only in keep-case tree need to
10673 * case-fold the word. */
10674 spell_soundfold(lp->lp_slang, tword,
10675 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010676
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010677 /* Compute the edit distance between the
10678 * sound-a-like words. */
10679 sound_score = soundalike_score(salword,
10680 tsalword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010681
10682 /* Add a penalty for words in another region. */
10683 if ((flags & WF_REGION) && (((unsigned)flags
10684 >> 16) & lp->lp_region) == 0)
10685 local_score = SCORE_REGION;
10686 else
10687 local_score = 0;
10688 sound_score += local_score;
10689
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010690 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010691 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010692 char_u cword[MAXWLEN];
10693 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010694 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010695
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010696 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010697 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010698 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010699 /* Need to fix case according to
10700 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010701 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010702 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010703 }
10704 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010705 p = tword;
10706
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010707 if (sps_flags & SPS_DOUBLE)
10708 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000010709 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010710 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010711 else
10712 {
10713 /* Compute the score. */
10714 score = spell_edit_score(
Bram Moolenaar5195e452005-08-19 20:32:47 +000010715 su->su_badword, p)
10716 + local_score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010717 if (sps_flags & SPS_BEST)
10718 /* give a bonus for the good word
10719 * sounding the same as the bad
10720 * word */
10721 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000010722 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010723 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010724 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010725 else
10726 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000010727 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010728 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010729 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010730 }
10731 }
10732
10733 /* Skip over other NUL bytes. */
10734 while (byts[n + 1] == 0)
10735 {
10736 ++n;
10737 ++curi[depth];
10738 }
10739 }
10740 else
10741 {
10742 /* Normal char, go one level deeper. */
10743 tword[depth++] = c;
10744 arridx[depth] = idxs[n];
10745 curi[depth] = 1;
10746 }
10747 }
10748 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010749 }
10750 }
10751 }
10752}
10753
10754/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010755 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010756 */
10757 static void
10758make_case_word(fword, cword, flags)
10759 char_u *fword;
10760 char_u *cword;
10761 int flags;
10762{
10763 if (flags & WF_ALLCAP)
10764 /* Make it all upper-case */
10765 allcap_copy(fword, cword);
10766 else if (flags & WF_ONECAP)
10767 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010768 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010769 else
10770 /* Use goodword as-is. */
10771 STRCPY(cword, fword);
10772}
10773
Bram Moolenaarea424162005-06-16 21:51:00 +000010774/*
10775 * Use map string "map" for languages "lp".
10776 */
10777 static void
10778set_map_str(lp, map)
10779 slang_T *lp;
10780 char_u *map;
10781{
10782 char_u *p;
10783 int headc = 0;
10784 int c;
10785 int i;
10786
10787 if (*map == NUL)
10788 {
10789 lp->sl_has_map = FALSE;
10790 return;
10791 }
10792 lp->sl_has_map = TRUE;
10793
10794 /* Init the array and hash table empty. */
10795 for (i = 0; i < 256; ++i)
10796 lp->sl_map_array[i] = 0;
10797#ifdef FEAT_MBYTE
10798 hash_init(&lp->sl_map_hash);
10799#endif
10800
10801 /*
10802 * The similar characters are stored separated with slashes:
10803 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
10804 * before the same slash. For characters above 255 sl_map_hash is used.
10805 */
10806 for (p = map; *p != NUL; )
10807 {
10808#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010809 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000010810#else
10811 c = *p++;
10812#endif
10813 if (c == '/')
10814 headc = 0;
10815 else
10816 {
10817 if (headc == 0)
10818 headc = c;
10819
10820#ifdef FEAT_MBYTE
10821 /* Characters above 255 don't fit in sl_map_array[], put them in
10822 * the hash table. Each entry is the char, a NUL the headchar and
10823 * a NUL. */
10824 if (c >= 256)
10825 {
10826 int cl = mb_char2len(c);
10827 int headcl = mb_char2len(headc);
10828 char_u *b;
10829 hash_T hash;
10830 hashitem_T *hi;
10831
10832 b = alloc((unsigned)(cl + headcl + 2));
10833 if (b == NULL)
10834 return;
10835 mb_char2bytes(c, b);
10836 b[cl] = NUL;
10837 mb_char2bytes(headc, b + cl + 1);
10838 b[cl + 1 + headcl] = NUL;
10839 hash = hash_hash(b);
10840 hi = hash_lookup(&lp->sl_map_hash, b, hash);
10841 if (HASHITEM_EMPTY(hi))
10842 hash_add_item(&lp->sl_map_hash, hi, b, hash);
10843 else
10844 {
10845 /* This should have been checked when generating the .spl
10846 * file. */
10847 EMSG(_("E999: duplicate char in MAP entry"));
10848 vim_free(b);
10849 }
10850 }
10851 else
10852#endif
10853 lp->sl_map_array[c] = headc;
10854 }
10855 }
10856}
10857
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010858/*
10859 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
10860 * lines in the .aff file.
10861 */
10862 static int
10863similar_chars(slang, c1, c2)
10864 slang_T *slang;
10865 int c1;
10866 int c2;
10867{
Bram Moolenaarea424162005-06-16 21:51:00 +000010868 int m1, m2;
10869#ifdef FEAT_MBYTE
10870 char_u buf[MB_MAXBYTES];
10871 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010872
Bram Moolenaarea424162005-06-16 21:51:00 +000010873 if (c1 >= 256)
10874 {
10875 buf[mb_char2bytes(c1, buf)] = 0;
10876 hi = hash_find(&slang->sl_map_hash, buf);
10877 if (HASHITEM_EMPTY(hi))
10878 m1 = 0;
10879 else
10880 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
10881 }
10882 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010883#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000010884 m1 = slang->sl_map_array[c1];
10885 if (m1 == 0)
10886 return FALSE;
10887
10888
10889#ifdef FEAT_MBYTE
10890 if (c2 >= 256)
10891 {
10892 buf[mb_char2bytes(c2, buf)] = 0;
10893 hi = hash_find(&slang->sl_map_hash, buf);
10894 if (HASHITEM_EMPTY(hi))
10895 m2 = 0;
10896 else
10897 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
10898 }
10899 else
10900#endif
10901 m2 = slang->sl_map_array[c2];
10902
10903 return m1 == m2;
10904}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010905
10906/*
10907 * Add a suggestion to the list of suggestions.
10908 * Do not add a duplicate suggestion or suggestions with a bad score.
10909 * When "use_score" is not zero it's used, otherwise the score is computed
10910 * with spell_edit_score().
10911 */
10912 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010913add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010914 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010915 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010916 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010917 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010918 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010919 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010920 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010921{
10922 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010923 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010924 char_u *p = NULL;
10925 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010926
10927 /* Check that the word wasn't banned. */
10928 if (was_banned(su, goodword))
10929 return;
10930
Bram Moolenaar0c405862005-06-22 22:26:26 +000010931 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
10932 * Remove the common part from "goodword". */
10933 i = badlen - su->su_badlen;
10934 if (i > 0)
10935 {
10936 /* This assumes there was no case folding or it didn't change the
10937 * length... */
10938 p = goodword + STRLEN(goodword) - i;
10939 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
10940 {
10941 badlen = su->su_badlen;
10942 c = *p;
10943 *p = NUL;
10944 }
10945 else
10946 p = NULL;
10947 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010948 else if (i < 0)
10949 {
10950 /* When replacing part of the word check that we actually change
10951 * something. For "the the" a suggestion can be replacing the first
10952 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010953 if (badlen == (int)STRLEN(goodword)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010954 && STRNCMP(su->su_badword, goodword, badlen) == 0)
10955 return;
10956 }
10957
Bram Moolenaar0c405862005-06-22 22:26:26 +000010958
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010959 if (score <= su->su_maxscore)
10960 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010961 /* Check if the word is already there. Also check the length that is
10962 * being replaced "thes," -> "these" is a different suggestion from
10963 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010964 stp = &SUG(*gap, 0);
10965 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010966 if (STRCMP(stp[i].st_word, goodword) == 0
10967 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010968 {
10969 /* Found it. Remember the lowest score. */
10970 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010971 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010972 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010973 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010974 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010975 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010976 break;
10977 }
10978
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010979 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010980 {
10981 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010982 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010983 stp->st_word = vim_strsave(goodword);
10984 if (stp->st_word != NULL)
10985 {
10986 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010987 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010988 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010989 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010990 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010991
10992 /* If we have too many suggestions now, sort the list and keep
10993 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010994 if (gap->ga_len > SUG_MAX_COUNT(su))
10995 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
10996 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010997 }
10998 }
10999 }
Bram Moolenaar0c405862005-06-22 22:26:26 +000011000
11001 if (p != NULL)
11002 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011003}
11004
11005/*
11006 * Add a word to be banned.
11007 */
11008 static void
11009add_banned(su, word)
11010 suginfo_T *su;
11011 char_u *word;
11012{
11013 char_u *s = vim_strsave(word);
11014 hash_T hash;
11015 hashitem_T *hi;
11016
11017 if (s != NULL)
11018 {
11019 hash = hash_hash(s);
11020 hi = hash_lookup(&su->su_banned, s, hash);
11021 if (HASHITEM_EMPTY(hi))
11022 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000011023 else
11024 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011025 }
11026}
11027
11028/*
11029 * Return TRUE if a word appears in the list of banned words.
11030 */
11031 static int
11032was_banned(su, word)
11033 suginfo_T *su;
11034 char_u *word;
11035{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011036 hashitem_T *hi = hash_find(&su->su_banned, word);
11037
11038 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011039}
11040
11041/*
11042 * Free the banned words in "su".
11043 */
11044 static void
11045free_banned(su)
11046 suginfo_T *su;
11047{
11048 int todo;
11049 hashitem_T *hi;
11050
11051 todo = su->su_banned.ht_used;
11052 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
11053 {
11054 if (!HASHITEM_EMPTY(hi))
11055 {
11056 vim_free(hi->hi_key);
11057 --todo;
11058 }
11059 }
11060 hash_clear(&su->su_banned);
11061}
11062
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011063/*
11064 * Recompute the score if sound-folding is possible. This is slow,
11065 * thus only done for the final results.
11066 */
11067 static void
11068rescore_suggestions(su)
11069 suginfo_T *su;
11070{
11071 langp_T *lp;
11072 suggest_T *stp;
11073 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011074 int i;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011075 int lpi;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011076
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011077 for (lpi = 0; lpi < curwin->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011078 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011079 lp = LANGP_ENTRY(curwin->w_buffer->b_langp, lpi);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011080 if (lp->lp_slang->sl_sal.ga_len > 0)
11081 {
11082 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011083 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011084
11085 for (i = 0; i < su->su_ga.ga_len; ++i)
11086 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011087 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011088 if (!stp->st_had_bonus)
11089 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011090 stp->st_altscore = stp_sal_score(stp, su,
11091 lp->lp_slang, sal_badword);
11092 if (stp->st_altscore == SCORE_MAXMAX)
11093 stp->st_altscore = SCORE_BIG;
11094 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011095 }
11096 }
11097 break;
11098 }
11099 }
11100}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011101
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011102static int
11103#ifdef __BORLANDC__
11104_RTLENTRYF
11105#endif
11106sug_compare __ARGS((const void *s1, const void *s2));
11107
11108/*
11109 * Function given to qsort() to sort the suggestions on st_score.
11110 */
11111 static int
11112#ifdef __BORLANDC__
11113_RTLENTRYF
11114#endif
11115sug_compare(s1, s2)
11116 const void *s1;
11117 const void *s2;
11118{
11119 suggest_T *p1 = (suggest_T *)s1;
11120 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011121 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011122
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011123 if (n == 0)
11124 return p1->st_altscore - p2->st_altscore;
11125 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011126}
11127
11128/*
11129 * Cleanup the suggestions:
11130 * - Sort on score.
11131 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011132 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011133 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011134 static int
11135cleanup_suggestions(gap, maxscore, keep)
11136 garray_T *gap;
11137 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011138 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011139{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011140 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011141 int i;
11142
11143 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011144 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011145
11146 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011147 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011148 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011149 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011150 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011151 gap->ga_len = keep;
11152 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011153 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011154 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011155}
11156
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011157#if defined(FEAT_EVAL) || defined(PROTO)
11158/*
11159 * Soundfold a string, for soundfold().
11160 * Result is in allocated memory, NULL for an error.
11161 */
11162 char_u *
11163eval_soundfold(word)
11164 char_u *word;
11165{
11166 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011167 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011168 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011169
11170 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
11171 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011172 for (lpi = 0; lpi < curwin->w_buffer->b_langp.ga_len; ++lpi)
11173 {
11174 lp = LANGP_ENTRY(curwin->w_buffer->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011175 if (lp->lp_slang->sl_sal.ga_len > 0)
11176 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011177 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011178 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011179 return vim_strsave(sound);
11180 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011181 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011182
11183 /* No language with sound folding, return word as-is. */
11184 return vim_strsave(word);
11185}
11186#endif
11187
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011188/*
11189 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000011190 *
11191 * There are many ways to turn a word into a sound-a-like representation. The
11192 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
11193 * swedish name matching - survey and test of different algorithms" by Klas
11194 * Erikson.
11195 *
11196 * We support two methods:
11197 * 1. SOFOFROM/SOFOTO do a simple character mapping.
11198 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011199 */
11200 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011201spell_soundfold(slang, inword, folded, res)
11202 slang_T *slang;
11203 char_u *inword;
11204 int folded; /* "inword" is already case-folded */
11205 char_u *res;
11206{
11207 char_u fword[MAXWLEN];
11208 char_u *word;
11209
11210 if (slang->sl_sofo)
11211 /* SOFOFROM and SOFOTO used */
11212 spell_soundfold_sofo(slang, inword, res);
11213 else
11214 {
11215 /* SAL items used. Requires the word to be case-folded. */
11216 if (folded)
11217 word = inword;
11218 else
11219 {
11220 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
11221 word = fword;
11222 }
11223
11224#ifdef FEAT_MBYTE
11225 if (has_mbyte)
11226 spell_soundfold_wsal(slang, word, res);
11227 else
11228#endif
11229 spell_soundfold_sal(slang, word, res);
11230 }
11231}
11232
11233/*
11234 * Perform sound folding of "inword" into "res" according to SOFOFROM and
11235 * SOFOTO lines.
11236 */
11237 static void
11238spell_soundfold_sofo(slang, inword, res)
11239 slang_T *slang;
11240 char_u *inword;
11241 char_u *res;
11242{
11243 char_u *s;
11244 int ri = 0;
11245 int c;
11246
11247#ifdef FEAT_MBYTE
11248 if (has_mbyte)
11249 {
11250 int prevc = 0;
11251 int *ip;
11252
11253 /* The sl_sal_first[] table contains the translation for chars up to
11254 * 255, sl_sal the rest. */
11255 for (s = inword; *s != NUL; )
11256 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011257 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011258 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
11259 c = ' ';
11260 else if (c < 256)
11261 c = slang->sl_sal_first[c];
11262 else
11263 {
11264 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
11265 if (ip == NULL) /* empty list, can't match */
11266 c = NUL;
11267 else
11268 for (;;) /* find "c" in the list */
11269 {
11270 if (*ip == 0) /* not found */
11271 {
11272 c = NUL;
11273 break;
11274 }
11275 if (*ip == c) /* match! */
11276 {
11277 c = ip[1];
11278 break;
11279 }
11280 ip += 2;
11281 }
11282 }
11283
11284 if (c != NUL && c != prevc)
11285 {
11286 ri += mb_char2bytes(c, res + ri);
11287 if (ri + MB_MAXBYTES > MAXWLEN)
11288 break;
11289 prevc = c;
11290 }
11291 }
11292 }
11293 else
11294#endif
11295 {
11296 /* The sl_sal_first[] table contains the translation. */
11297 for (s = inword; (c = *s) != NUL; ++s)
11298 {
11299 if (vim_iswhite(c))
11300 c = ' ';
11301 else
11302 c = slang->sl_sal_first[c];
11303 if (c != NUL && (ri == 0 || res[ri - 1] != c))
11304 res[ri++] = c;
11305 }
11306 }
11307
11308 res[ri] = NUL;
11309}
11310
11311 static void
11312spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011313 slang_T *slang;
11314 char_u *inword;
11315 char_u *res;
11316{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011317 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011318 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011319 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011320 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011321 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011322 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011323 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011324 int n, k = 0;
11325 int z0;
11326 int k0;
11327 int n0;
11328 int c;
11329 int pri;
11330 int p0 = -333;
11331 int c0;
11332
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011333 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011334 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011335 if (slang->sl_rem_accents)
11336 {
11337 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011338 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011339 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011340 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011341 {
11342 *t++ = ' ';
11343 s = skipwhite(s);
11344 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011345 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011346 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011347 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011348 *t++ = *s;
11349 ++s;
11350 }
11351 }
11352 *t = NUL;
11353 }
11354 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011355 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011356
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011357 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011358
11359 /*
11360 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011361 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011362 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011363 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011364 while ((c = word[i]) != NUL)
11365 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011366 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011367 n = slang->sl_sal_first[c];
11368 z0 = 0;
11369
11370 if (n >= 0)
11371 {
11372 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011373 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011374 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011375 /* Quickly skip entries that don't match the word. Most
11376 * entries are less then three chars, optimize for that. */
11377 k = smp[n].sm_leadlen;
11378 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011379 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011380 if (word[i + 1] != s[1])
11381 continue;
11382 if (k > 2)
11383 {
11384 for (j = 2; j < k; ++j)
11385 if (word[i + j] != s[j])
11386 break;
11387 if (j < k)
11388 continue;
11389 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011390 }
11391
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011392 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011393 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011394 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011395 while (*pf != NUL && *pf != word[i + k])
11396 ++pf;
11397 if (*pf == NUL)
11398 continue;
11399 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011400 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011401 s = smp[n].sm_rules;
11402 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011403
11404 p0 = *s;
11405 k0 = k;
11406 while (*s == '-' && k > 1)
11407 {
11408 k--;
11409 s++;
11410 }
11411 if (*s == '<')
11412 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011413 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011414 {
11415 /* determine priority */
11416 pri = *s - '0';
11417 s++;
11418 }
11419 if (*s == '^' && *(s + 1) == '^')
11420 s++;
11421
11422 if (*s == NUL
11423 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011424 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011425 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011426 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011427 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011428 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011429 && spell_iswordp(word + i - 1, curbuf)
11430 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011431 {
11432 /* search for followup rules, if: */
11433 /* followup and k > 1 and NO '-' in searchstring */
11434 c0 = word[i + k - 1];
11435 n0 = slang->sl_sal_first[c0];
11436
11437 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011438 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011439 {
11440 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011441 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011442 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011443 /* Quickly skip entries that don't match the word.
11444 * */
11445 k0 = smp[n0].sm_leadlen;
11446 if (k0 > 1)
11447 {
11448 if (word[i + k] != s[1])
11449 continue;
11450 if (k0 > 2)
11451 {
11452 pf = word + i + k + 1;
11453 for (j = 2; j < k0; ++j)
11454 if (*pf++ != s[j])
11455 break;
11456 if (j < k0)
11457 continue;
11458 }
11459 }
11460 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011461
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011462 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011463 {
11464 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011465 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011466 while (*pf != NUL && *pf != word[i + k0])
11467 ++pf;
11468 if (*pf == NUL)
11469 continue;
11470 ++k0;
11471 }
11472
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011473 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011474 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011475 while (*s == '-')
11476 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011477 /* "k0" gets NOT reduced because
11478 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011479 s++;
11480 }
11481 if (*s == '<')
11482 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011483 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011484 {
11485 p0 = *s - '0';
11486 s++;
11487 }
11488
11489 if (*s == NUL
11490 /* *s == '^' cuts */
11491 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011492 && !spell_iswordp(word + i + k0,
11493 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011494 {
11495 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011496 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011497 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011498
11499 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011500 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011501 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011502 /* rule fits; stop search */
11503 break;
11504 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011505 }
11506
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011507 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011508 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011509 }
11510
11511 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011512 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011513 if (s == NULL)
11514 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011515 pf = smp[n].sm_rules;
11516 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011517 if (p0 == 1 && z == 0)
11518 {
11519 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011520 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
11521 || res[reslen - 1] == *s))
11522 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011523 z0 = 1;
11524 z = 1;
11525 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011526 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011527 {
11528 word[i + k0] = *s;
11529 k0++;
11530 s++;
11531 }
11532 if (k > k0)
11533 mch_memmove(word + i + k0, word + i + k,
11534 STRLEN(word + i + k) + 1);
11535
11536 /* new "actual letter" */
11537 c = word[i];
11538 }
11539 else
11540 {
11541 /* no '<' rule used */
11542 i += k - 1;
11543 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011544 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011545 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011546 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011547 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011548 s++;
11549 }
11550 /* new "actual letter" */
11551 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011552 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011553 {
11554 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011555 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011556 mch_memmove(word, word + i + 1,
11557 STRLEN(word + i + 1) + 1);
11558 i = 0;
11559 z0 = 1;
11560 }
11561 }
11562 break;
11563 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011564 }
11565 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011566 else if (vim_iswhite(c))
11567 {
11568 c = ' ';
11569 k = 1;
11570 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011571
11572 if (z0 == 0)
11573 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011574 if (k && !p0 && reslen < MAXWLEN && c != NUL
11575 && (!slang->sl_collapse || reslen == 0
11576 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011577 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011578 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011579
11580 i++;
11581 z = 0;
11582 k = 0;
11583 }
11584 }
11585
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011586 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011587}
11588
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011589#ifdef FEAT_MBYTE
11590/*
11591 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
11592 * Multi-byte version of spell_soundfold().
11593 */
11594 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011595spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011596 slang_T *slang;
11597 char_u *inword;
11598 char_u *res;
11599{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011600 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011601 int word[MAXWLEN];
11602 int wres[MAXWLEN];
11603 int l;
11604 char_u *s;
11605 int *ws;
11606 char_u *t;
11607 int *pf;
11608 int i, j, z;
11609 int reslen;
11610 int n, k = 0;
11611 int z0;
11612 int k0;
11613 int n0;
11614 int c;
11615 int pri;
11616 int p0 = -333;
11617 int c0;
11618 int did_white = FALSE;
11619
11620 /*
11621 * Convert the multi-byte string to a wide-character string.
11622 * Remove accents, if wanted. We actually remove all non-word characters.
11623 * But keep white space.
11624 */
11625 n = 0;
11626 for (s = inword; *s != NUL; )
11627 {
11628 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011629 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011630 if (slang->sl_rem_accents)
11631 {
11632 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
11633 {
11634 if (did_white)
11635 continue;
11636 c = ' ';
11637 did_white = TRUE;
11638 }
11639 else
11640 {
11641 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011642 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011643 continue;
11644 }
11645 }
11646 word[n++] = c;
11647 }
11648 word[n] = NUL;
11649
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011650 /*
11651 * This comes from Aspell phonet.cpp.
11652 * Converted from C++ to C. Added support for multi-byte chars.
11653 * Changed to keep spaces.
11654 */
11655 i = reslen = z = 0;
11656 while ((c = word[i]) != NUL)
11657 {
11658 /* Start with the first rule that has the character in the word. */
11659 n = slang->sl_sal_first[c & 0xff];
11660 z0 = 0;
11661
11662 if (n >= 0)
11663 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011664 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011665 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
11666 {
11667 /* Quickly skip entries that don't match the word. Most
11668 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011669 if (c != ws[0])
11670 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011671 k = smp[n].sm_leadlen;
11672 if (k > 1)
11673 {
11674 if (word[i + 1] != ws[1])
11675 continue;
11676 if (k > 2)
11677 {
11678 for (j = 2; j < k; ++j)
11679 if (word[i + j] != ws[j])
11680 break;
11681 if (j < k)
11682 continue;
11683 }
11684 }
11685
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011686 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011687 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011688 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011689 while (*pf != NUL && *pf != word[i + k])
11690 ++pf;
11691 if (*pf == NUL)
11692 continue;
11693 ++k;
11694 }
11695 s = smp[n].sm_rules;
11696 pri = 5; /* default priority */
11697
11698 p0 = *s;
11699 k0 = k;
11700 while (*s == '-' && k > 1)
11701 {
11702 k--;
11703 s++;
11704 }
11705 if (*s == '<')
11706 s++;
11707 if (VIM_ISDIGIT(*s))
11708 {
11709 /* determine priority */
11710 pri = *s - '0';
11711 s++;
11712 }
11713 if (*s == '^' && *(s + 1) == '^')
11714 s++;
11715
11716 if (*s == NUL
11717 || (*s == '^'
11718 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011719 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011720 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011721 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011722 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011723 && spell_iswordp_w(word + i - 1, curbuf)
11724 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011725 {
11726 /* search for followup rules, if: */
11727 /* followup and k > 1 and NO '-' in searchstring */
11728 c0 = word[i + k - 1];
11729 n0 = slang->sl_sal_first[c0 & 0xff];
11730
11731 if (slang->sl_followup && k > 1 && n0 >= 0
11732 && p0 != '-' && word[i + k] != NUL)
11733 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011734 /* Test follow-up rule for "word[i + k]"; loop over
11735 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011736 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
11737 == (c0 & 0xff); ++n0)
11738 {
11739 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011740 */
11741 if (c0 != ws[0])
11742 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011743 k0 = smp[n0].sm_leadlen;
11744 if (k0 > 1)
11745 {
11746 if (word[i + k] != ws[1])
11747 continue;
11748 if (k0 > 2)
11749 {
11750 pf = word + i + k + 1;
11751 for (j = 2; j < k0; ++j)
11752 if (*pf++ != ws[j])
11753 break;
11754 if (j < k0)
11755 continue;
11756 }
11757 }
11758 k0 += k - 1;
11759
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011760 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011761 {
11762 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011763 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011764 while (*pf != NUL && *pf != word[i + k0])
11765 ++pf;
11766 if (*pf == NUL)
11767 continue;
11768 ++k0;
11769 }
11770
11771 p0 = 5;
11772 s = smp[n0].sm_rules;
11773 while (*s == '-')
11774 {
11775 /* "k0" gets NOT reduced because
11776 * "if (k0 == k)" */
11777 s++;
11778 }
11779 if (*s == '<')
11780 s++;
11781 if (VIM_ISDIGIT(*s))
11782 {
11783 p0 = *s - '0';
11784 s++;
11785 }
11786
11787 if (*s == NUL
11788 /* *s == '^' cuts */
11789 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011790 && !spell_iswordp_w(word + i + k0,
11791 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011792 {
11793 if (k0 == k)
11794 /* this is just a piece of the string */
11795 continue;
11796
11797 if (p0 < pri)
11798 /* priority too low */
11799 continue;
11800 /* rule fits; stop search */
11801 break;
11802 }
11803 }
11804
11805 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
11806 == (c0 & 0xff))
11807 continue;
11808 }
11809
11810 /* replace string */
11811 ws = smp[n].sm_to_w;
11812 s = smp[n].sm_rules;
11813 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
11814 if (p0 == 1 && z == 0)
11815 {
11816 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011817 if (reslen > 0 && ws != NULL && *ws != NUL
11818 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011819 || wres[reslen - 1] == *ws))
11820 reslen--;
11821 z0 = 1;
11822 z = 1;
11823 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011824 if (ws != NULL)
11825 while (*ws != NUL && word[i + k0] != NUL)
11826 {
11827 word[i + k0] = *ws;
11828 k0++;
11829 ws++;
11830 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011831 if (k > k0)
11832 mch_memmove(word + i + k0, word + i + k,
11833 sizeof(int) * (STRLEN(word + i + k) + 1));
11834
11835 /* new "actual letter" */
11836 c = word[i];
11837 }
11838 else
11839 {
11840 /* no '<' rule used */
11841 i += k - 1;
11842 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011843 if (ws != NULL)
11844 while (*ws != NUL && ws[1] != NUL
11845 && reslen < MAXWLEN)
11846 {
11847 if (reslen == 0 || wres[reslen - 1] != *ws)
11848 wres[reslen++] = *ws;
11849 ws++;
11850 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011851 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011852 if (ws == NULL)
11853 c = NUL;
11854 else
11855 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011856 if (strstr((char *)s, "^^") != NULL)
11857 {
11858 if (c != NUL)
11859 wres[reslen++] = c;
11860 mch_memmove(word, word + i + 1,
11861 sizeof(int) * (STRLEN(word + i + 1) + 1));
11862 i = 0;
11863 z0 = 1;
11864 }
11865 }
11866 break;
11867 }
11868 }
11869 }
11870 else if (vim_iswhite(c))
11871 {
11872 c = ' ';
11873 k = 1;
11874 }
11875
11876 if (z0 == 0)
11877 {
11878 if (k && !p0 && reslen < MAXWLEN && c != NUL
11879 && (!slang->sl_collapse || reslen == 0
11880 || wres[reslen - 1] != c))
11881 /* condense only double letters */
11882 wres[reslen++] = c;
11883
11884 i++;
11885 z = 0;
11886 k = 0;
11887 }
11888 }
11889
11890 /* Convert wide characters in "wres" to a multi-byte string in "res". */
11891 l = 0;
11892 for (n = 0; n < reslen; ++n)
11893 {
11894 l += mb_char2bytes(wres[n], res + l);
11895 if (l + MB_MAXBYTES > MAXWLEN)
11896 break;
11897 }
11898 res[l] = NUL;
11899}
11900#endif
11901
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011902/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011903 * Compute a score for two sound-a-like words.
11904 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
11905 * Instead of a generic loop we write out the code. That keeps it fast by
11906 * avoiding checks that will not be possible.
11907 */
11908 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011909soundalike_score(goodstart, badstart)
11910 char_u *goodstart; /* sound-folded good word */
11911 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011912{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011913 char_u *goodsound = goodstart;
11914 char_u *badsound = badstart;
11915 int goodlen;
11916 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011917 int n;
11918 char_u *pl, *ps;
11919 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011920 int score = 0;
11921
11922 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
11923 * counted so much, vowels halfway the word aren't counted at all. */
11924 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
11925 {
11926 score = SCORE_DEL / 2;
11927 if (*badsound == '*')
11928 ++badsound;
11929 else
11930 ++goodsound;
11931 }
11932
11933 goodlen = STRLEN(goodsound);
11934 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011935
11936 /* Return quickly if the lenghts are too different to be fixed by two
11937 * changes. */
11938 n = goodlen - badlen;
11939 if (n < -2 || n > 2)
11940 return SCORE_MAXMAX;
11941
11942 if (n > 0)
11943 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011944 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011945 ps = badsound;
11946 }
11947 else
11948 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011949 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011950 ps = goodsound;
11951 }
11952
11953 /* Skip over the identical part. */
11954 while (*pl == *ps && *pl != NUL)
11955 {
11956 ++pl;
11957 ++ps;
11958 }
11959
11960 switch (n)
11961 {
11962 case -2:
11963 case 2:
11964 /*
11965 * Must delete two characters from "pl".
11966 */
11967 ++pl; /* first delete */
11968 while (*pl == *ps)
11969 {
11970 ++pl;
11971 ++ps;
11972 }
11973 /* strings must be equal after second delete */
11974 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011975 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011976
11977 /* Failed to compare. */
11978 break;
11979
11980 case -1:
11981 case 1:
11982 /*
11983 * Minimal one delete from "pl" required.
11984 */
11985
11986 /* 1: delete */
11987 pl2 = pl + 1;
11988 ps2 = ps;
11989 while (*pl2 == *ps2)
11990 {
11991 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011992 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011993 ++pl2;
11994 ++ps2;
11995 }
11996
11997 /* 2: delete then swap, then rest must be equal */
11998 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
11999 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012000 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012001
12002 /* 3: delete then substitute, then the rest must be equal */
12003 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012004 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012005
12006 /* 4: first swap then delete */
12007 if (pl[0] == ps[1] && pl[1] == ps[0])
12008 {
12009 pl2 = pl + 2; /* swap, skip two chars */
12010 ps2 = ps + 2;
12011 while (*pl2 == *ps2)
12012 {
12013 ++pl2;
12014 ++ps2;
12015 }
12016 /* delete a char and then strings must be equal */
12017 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012018 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012019 }
12020
12021 /* 5: first substitute then delete */
12022 pl2 = pl + 1; /* substitute, skip one char */
12023 ps2 = ps + 1;
12024 while (*pl2 == *ps2)
12025 {
12026 ++pl2;
12027 ++ps2;
12028 }
12029 /* delete a char and then strings must be equal */
12030 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012031 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012032
12033 /* Failed to compare. */
12034 break;
12035
12036 case 0:
12037 /*
12038 * Lenghts are equal, thus changes must result in same length: An
12039 * insert is only possible in combination with a delete.
12040 * 1: check if for identical strings
12041 */
12042 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012043 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012044
12045 /* 2: swap */
12046 if (pl[0] == ps[1] && pl[1] == ps[0])
12047 {
12048 pl2 = pl + 2; /* swap, skip two chars */
12049 ps2 = ps + 2;
12050 while (*pl2 == *ps2)
12051 {
12052 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012053 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012054 ++pl2;
12055 ++ps2;
12056 }
12057 /* 3: swap and swap again */
12058 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
12059 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012060 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012061
12062 /* 4: swap and substitute */
12063 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012064 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012065 }
12066
12067 /* 5: substitute */
12068 pl2 = pl + 1;
12069 ps2 = ps + 1;
12070 while (*pl2 == *ps2)
12071 {
12072 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012073 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012074 ++pl2;
12075 ++ps2;
12076 }
12077
12078 /* 6: substitute and swap */
12079 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
12080 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012081 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012082
12083 /* 7: substitute and substitute */
12084 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012085 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012086
12087 /* 8: insert then delete */
12088 pl2 = pl;
12089 ps2 = ps + 1;
12090 while (*pl2 == *ps2)
12091 {
12092 ++pl2;
12093 ++ps2;
12094 }
12095 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012096 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012097
12098 /* 9: delete then insert */
12099 pl2 = pl + 1;
12100 ps2 = ps;
12101 while (*pl2 == *ps2)
12102 {
12103 ++pl2;
12104 ++ps2;
12105 }
12106 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012107 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012108
12109 /* Failed to compare. */
12110 break;
12111 }
12112
12113 return SCORE_MAXMAX;
12114}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012115
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012116/*
12117 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012118 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012119 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000012120 * The algorithm is described by Du and Chang, 1992.
12121 * The implementation of the algorithm comes from Aspell editdist.cpp,
12122 * edit_distance(). It has been converted from C++ to C and modified to
12123 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012124 */
12125 static int
12126spell_edit_score(badword, goodword)
12127 char_u *badword;
12128 char_u *goodword;
12129{
12130 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012131 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012132 int j, i;
12133 int t;
12134 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012135 int pbc, pgc;
12136#ifdef FEAT_MBYTE
12137 char_u *p;
12138 int wbadword[MAXWLEN];
12139 int wgoodword[MAXWLEN];
12140
12141 if (has_mbyte)
12142 {
12143 /* Get the characters from the multi-byte strings and put them in an
12144 * int array for easy access. */
12145 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012146 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000012147 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012148 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012149 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000012150 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012151 }
12152 else
12153#endif
12154 {
12155 badlen = STRLEN(badword) + 1;
12156 goodlen = STRLEN(goodword) + 1;
12157 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012158
12159 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
12160#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012161 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
12162 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012163 if (cnt == NULL)
12164 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012165
12166 CNT(0, 0) = 0;
12167 for (j = 1; j <= goodlen; ++j)
12168 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
12169
12170 for (i = 1; i <= badlen; ++i)
12171 {
12172 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
12173 for (j = 1; j <= goodlen; ++j)
12174 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012175#ifdef FEAT_MBYTE
12176 if (has_mbyte)
12177 {
12178 bc = wbadword[i - 1];
12179 gc = wgoodword[j - 1];
12180 }
12181 else
12182#endif
12183 {
12184 bc = badword[i - 1];
12185 gc = goodword[j - 1];
12186 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012187 if (bc == gc)
12188 CNT(i, j) = CNT(i - 1, j - 1);
12189 else
12190 {
12191 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012192 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012193 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
12194 else
12195 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
12196
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012197 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012198 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012199#ifdef FEAT_MBYTE
12200 if (has_mbyte)
12201 {
12202 pbc = wbadword[i - 2];
12203 pgc = wgoodword[j - 2];
12204 }
12205 else
12206#endif
12207 {
12208 pbc = badword[i - 2];
12209 pgc = goodword[j - 2];
12210 }
12211 if (bc == pgc && pbc == gc)
12212 {
12213 t = SCORE_SWAP + CNT(i - 2, j - 2);
12214 if (t < CNT(i, j))
12215 CNT(i, j) = t;
12216 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012217 }
12218 t = SCORE_DEL + CNT(i - 1, j);
12219 if (t < CNT(i, j))
12220 CNT(i, j) = t;
12221 t = SCORE_INS + CNT(i, j - 1);
12222 if (t < CNT(i, j))
12223 CNT(i, j) = t;
12224 }
12225 }
12226 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012227
12228 i = CNT(badlen - 1, goodlen - 1);
12229 vim_free(cnt);
12230 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012231}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000012232
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012233/*
12234 * ":spelldump"
12235 */
12236/*ARGSUSED*/
12237 void
12238ex_spelldump(eap)
12239 exarg_T *eap;
12240{
12241 buf_T *buf = curbuf;
12242 langp_T *lp;
12243 slang_T *slang;
12244 idx_T arridx[MAXWLEN];
12245 int curi[MAXWLEN];
12246 char_u word[MAXWLEN];
12247 int c;
12248 char_u *byts;
12249 idx_T *idxs;
12250 linenr_T lnum = 0;
12251 int round;
12252 int depth;
12253 int n;
12254 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000012255 char_u *region_names = NULL; /* region names being used */
12256 int do_region = TRUE; /* dump region names and numbers */
12257 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012258 int lpi;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012259
Bram Moolenaar95529562005-08-25 21:21:38 +000012260 if (no_spell_checking(curwin))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012261 return;
12262
12263 /* Create a new empty buffer by splitting the window. */
12264 do_cmdline_cmd((char_u *)"new");
12265 if (!bufempty() || !buf_valid(buf))
12266 return;
12267
Bram Moolenaar7887d882005-07-01 22:33:52 +000012268 /* Find out if we can support regions: All languages must support the same
12269 * regions or none at all. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012270 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000012271 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012272 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000012273 p = lp->lp_slang->sl_regions;
12274 if (p[0] != 0)
12275 {
12276 if (region_names == NULL) /* first language with regions */
12277 region_names = p;
12278 else if (STRCMP(region_names, p) != 0)
12279 {
12280 do_region = FALSE; /* region names are different */
12281 break;
12282 }
12283 }
12284 }
12285
12286 if (do_region && region_names != NULL)
12287 {
12288 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
12289 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
12290 }
12291 else
12292 do_region = FALSE;
12293
12294 /*
12295 * Loop over all files loaded for the entries in 'spelllang'.
12296 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012297 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012298 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012299 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012300 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012301 if (slang->sl_fbyts == NULL) /* reloading failed */
12302 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012303
12304 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
12305 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
12306
12307 /* round 1: case-folded tree
12308 * round 2: keep-case tree */
12309 for (round = 1; round <= 2; ++round)
12310 {
12311 if (round == 1)
12312 {
12313 byts = slang->sl_fbyts;
12314 idxs = slang->sl_fidxs;
12315 }
12316 else
12317 {
12318 byts = slang->sl_kbyts;
12319 idxs = slang->sl_kidxs;
12320 }
12321 if (byts == NULL)
12322 continue; /* array is empty */
12323
12324 depth = 0;
12325 arridx[0] = 0;
12326 curi[0] = 1;
12327 while (depth >= 0 && !got_int)
12328 {
12329 if (curi[depth] > byts[arridx[depth]])
12330 {
12331 /* Done all bytes at this node, go up one level. */
12332 --depth;
12333 line_breakcheck();
12334 }
12335 else
12336 {
12337 /* Do one more byte at this node. */
12338 n = arridx[depth] + curi[depth];
12339 ++curi[depth];
12340 c = byts[n];
12341 if (c == 0)
12342 {
12343 /* End of word, deal with the word.
12344 * Don't use keep-case words in the fold-case tree,
12345 * they will appear in the keep-case tree.
12346 * Only use the word when the region matches. */
12347 flags = (int)idxs[n];
12348 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012349 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000012350 && (do_region
12351 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012352 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012353 & lp->lp_region) != 0))
12354 {
12355 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000012356 if (!do_region)
12357 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000012358
12359 /* Dump the basic word if there is no prefix or
12360 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012361 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000012362 if (c == 0 || curi[depth] == 2)
12363 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012364
12365 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000012366 if (c != 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012367 lnum = dump_prefixes(slang, word, round,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012368 flags, lnum);
12369 }
12370 }
12371 else
12372 {
12373 /* Normal char, go one level deeper. */
12374 word[depth++] = c;
12375 arridx[depth] = idxs[n];
12376 curi[depth] = 1;
12377 }
12378 }
12379 }
12380 }
12381 }
12382
12383 /* Delete the empty line that we started with. */
12384 if (curbuf->b_ml.ml_line_count > 1)
12385 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
12386
12387 redraw_later(NOT_VALID);
12388}
12389
12390/*
12391 * Dump one word: apply case modifications and append a line to the buffer.
12392 */
12393 static void
12394dump_word(word, round, flags, lnum)
12395 char_u *word;
12396 int round;
12397 int flags;
12398 linenr_T lnum;
12399{
12400 int keepcap = FALSE;
12401 char_u *p;
12402 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000012403 char_u badword[MAXWLEN + 10];
12404 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012405
12406 if (round == 1 && (flags & WF_CAPMASK) != 0)
12407 {
12408 /* Need to fix case according to "flags". */
12409 make_case_word(word, cword, flags);
12410 p = cword;
12411 }
12412 else
12413 {
12414 p = word;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000012415 if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0
12416 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012417 keepcap = TRUE;
12418 }
12419
Bram Moolenaar7887d882005-07-01 22:33:52 +000012420 /* Add flags and regions after a slash. */
12421 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012422 {
Bram Moolenaar7887d882005-07-01 22:33:52 +000012423 STRCPY(badword, p);
12424 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012425 if (keepcap)
12426 STRCAT(badword, "=");
12427 if (flags & WF_BANNED)
12428 STRCAT(badword, "!");
12429 else if (flags & WF_RARE)
12430 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +000012431 if (flags & WF_REGION)
12432 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012433 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +000012434 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012435 p = badword;
12436 }
12437
12438 ml_append(lnum, p, (colnr_T)0, FALSE);
12439}
12440
12441/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012442 * For ":spelldump": Find matching prefixes for "word". Prepend each to
12443 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012444 * Return the updated line number.
12445 */
12446 static linenr_T
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012447dump_prefixes(slang, word, round, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012448 slang_T *slang;
12449 char_u *word; /* case-folded word */
12450 int round;
12451 int flags; /* flags with prefix ID */
12452 linenr_T startlnum;
12453{
12454 idx_T arridx[MAXWLEN];
12455 int curi[MAXWLEN];
12456 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000012457 char_u word_up[MAXWLEN];
12458 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012459 int c;
12460 char_u *byts;
12461 idx_T *idxs;
12462 linenr_T lnum = startlnum;
12463 int depth;
12464 int n;
12465 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012466 int i;
12467
Bram Moolenaar53805d12005-08-01 07:08:33 +000012468 /* if the word starts with a lower-case letter make the word with an
12469 * upper-case letter in word_up[]. */
12470 c = PTR2CHAR(word);
12471 if (SPELL_TOUPPER(c) != c)
12472 {
12473 onecap_copy(word, word_up, TRUE);
12474 has_word_up = TRUE;
12475 }
12476
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012477 byts = slang->sl_pbyts;
12478 idxs = slang->sl_pidxs;
12479 if (byts != NULL) /* array not is empty */
12480 {
12481 /*
12482 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012483 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012484 */
12485 depth = 0;
12486 arridx[0] = 0;
12487 curi[0] = 1;
12488 while (depth >= 0 && !got_int)
12489 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012490 n = arridx[depth];
12491 len = byts[n];
12492 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012493 {
12494 /* Done all bytes at this node, go up one level. */
12495 --depth;
12496 line_breakcheck();
12497 }
12498 else
12499 {
12500 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012501 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012502 ++curi[depth];
12503 c = byts[n];
12504 if (c == 0)
12505 {
12506 /* End of prefix, find out how many IDs there are. */
12507 for (i = 1; i < len; ++i)
12508 if (byts[n + i] != 0)
12509 break;
12510 curi[depth] += i - 1;
12511
Bram Moolenaar53805d12005-08-01 07:08:33 +000012512 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
12513 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012514 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000012515 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000012516 dump_word(prefix, round,
Bram Moolenaar53805d12005-08-01 07:08:33 +000012517 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000012518 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012519 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000012520
12521 /* Check for prefix that matches the word when the
12522 * first letter is upper-case, but only if the prefix has
12523 * a condition. */
12524 if (has_word_up)
12525 {
12526 c = valid_word_prefix(i, n, flags, word_up, slang,
12527 TRUE);
12528 if (c != 0)
12529 {
12530 vim_strncpy(prefix + depth, word_up,
12531 MAXWLEN - depth - 1);
12532 dump_word(prefix, round,
12533 (c & WF_RAREPFX) ? (flags | WF_RARE)
12534 : flags, lnum++);
12535 }
12536 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012537 }
12538 else
12539 {
12540 /* Normal char, go one level deeper. */
12541 prefix[depth++] = c;
12542 arridx[depth] = idxs[n];
12543 curi[depth] = 1;
12544 }
12545 }
12546 }
12547 }
12548
12549 return lnum;
12550}
12551
Bram Moolenaar95529562005-08-25 21:21:38 +000012552/*
12553 * Move "p" to end of word.
12554 */
12555 char_u *
12556spell_to_word_end(start, buf)
12557 char_u *start;
12558 buf_T *buf;
12559{
12560 char_u *p = start;
12561
12562 while (*p != NUL && spell_iswordp(p, buf))
12563 mb_ptr_adv(p);
12564 return p;
12565}
12566
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012567#if defined(FEAT_INS_EXPAND) || defined(PROTO)
12568static int spell_expand_need_cap;
12569
12570/*
12571 * Find start of the word in front of the cursor. We don't check if it is
12572 * badly spelled, with completion we can only change the word in front of the
12573 * cursor.
12574 * Used for Insert mode completion CTRL-X ?.
12575 * Returns the column number of the word.
12576 */
12577 int
12578spell_word_start(startcol)
12579 int startcol;
12580{
12581 char_u *line;
12582 char_u *p;
12583 int col = 0;
12584
Bram Moolenaar95529562005-08-25 21:21:38 +000012585 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012586 return startcol;
12587
12588 /* Find a word character before "startcol". */
12589 line = ml_get_curline();
12590 for (p = line + startcol; p > line; )
12591 {
12592 mb_ptr_back(line, p);
12593 if (spell_iswordp_nmw(p))
12594 break;
12595 }
12596
12597 /* Go back to start of the word. */
12598 while (p > line)
12599 {
12600 col = p - line;
12601 mb_ptr_back(line, p);
12602 if (!spell_iswordp(p, curbuf))
12603 break;
12604 col = 0;
12605 }
12606
12607 /* Need to check for 'spellcapcheck' now, the word is removed before
12608 * expand_spelling() is called. Therefore the ugly global variable. */
12609 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
12610
12611 return col;
12612}
12613
12614/*
12615 * Get list of spelling suggestions.
12616 * Used for Insert mode completion CTRL-X ?.
12617 * Returns the number of matches. The matches are in "matchp[]", array of
12618 * allocated strings.
12619 */
12620/*ARGSUSED*/
12621 int
12622expand_spelling(lnum, col, pat, matchp)
12623 linenr_T lnum;
12624 int col;
12625 char_u *pat;
12626 char_u ***matchp;
12627{
12628 garray_T ga;
12629
12630 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap);
12631 *matchp = ga.ga_data;
12632 return ga.ga_len;
12633}
12634#endif
12635
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000012636#endif /* FEAT_SYN_HL */