blob: ffe5a207c1bc1a319fb6793e0ad447fb5a80b3ab [file] [log] [blame]
Bram Moolenaare19defe2005-03-21 08:23:33 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * spell.c: code for spell checking
Bram Moolenaarfc735152005-03-22 22:54:12 +000012 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000013 * The spell checking mechanism uses a tree (aka trie). Each node in the tree
14 * has a list of bytes that can appear (siblings). For each byte there is a
15 * pointer to the node with the byte that follows in the word (child).
Bram Moolenaar9f30f502005-06-14 22:01:04 +000016 *
17 * A NUL byte is used where the word may end. The bytes are sorted, so that
18 * binary searching can be used and the NUL bytes are at the start. The
19 * number of possible bytes is stored before the list of bytes.
20 *
21 * The tree uses two arrays: "byts" stores the characters, "idxs" stores
22 * either the next index or flags. The tree starts at index 0. For example,
23 * to lookup "vi" this sequence is followed:
24 * i = 0
25 * len = byts[i]
26 * n = where "v" appears in byts[i + 1] to byts[i + len]
27 * i = idxs[n]
28 * len = byts[i]
29 * n = where "i" appears in byts[i + 1] to byts[i + len]
30 * i = idxs[n]
31 * len = byts[i]
32 * find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi".
Bram Moolenaar51485f02005-06-04 21:55:20 +000033 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000034 * There are two word trees: one with case-folded words and one with words in
Bram Moolenaar51485f02005-06-04 21:55:20 +000035 * original case. The second one is only used for keep-case words and is
36 * usually small.
37 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +000038 * There is one additional tree for when not all prefixes are applied when
Bram Moolenaar1d73c882005-06-19 22:48:47 +000039 * generating the .spl file. This tree stores all the possible prefixes, as
40 * if they were words. At each word (prefix) end the prefix nr is stored, the
41 * following word must support this prefix nr. And the condition nr is
42 * stored, used to lookup the condition that the word must match with.
43 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000044 * Thanks to Olaf Seibert for providing an example implementation of this tree
45 * and the compression mechanism.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000046 *
47 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000048 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000049 * Why doesn't Vim use aspell/ispell/myspell/etc.?
50 * See ":help develop-spell".
51 */
52
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000053/* Use SPELL_PRINTTREE for debugging: dump the word tree after adding a word.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000054 * Only use it for small word lists! */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000055#if 0
56# define SPELL_PRINTTREE
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000057#endif
58
Bram Moolenaar51485f02005-06-04 21:55:20 +000059/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000060 * Use this to adjust the score after finding suggestions, based on the
61 * suggested word sounding like the bad word. This is much faster than doing
62 * it for every possible suggestion.
63 * Disadvantage: When "the" is typed as "hte" it sounds different and goes
64 * down in the list.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000065 * Used when 'spellsuggest' is set to "best".
66 */
67#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
68
69/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000070 * Vim spell file format: <HEADER>
Bram Moolenaar5195e452005-08-19 20:32:47 +000071 * <SECTIONS>
Bram Moolenaar1d73c882005-06-19 22:48:47 +000072 * <LWORDTREE>
73 * <KWORDTREE>
74 * <PREFIXTREE>
Bram Moolenaar51485f02005-06-04 21:55:20 +000075 *
Bram Moolenaar5195e452005-08-19 20:32:47 +000076 * <HEADER>: <fileID> <versionnr>
Bram Moolenaar51485f02005-06-04 21:55:20 +000077 *
Bram Moolenaar5195e452005-08-19 20:32:47 +000078 * <fileID> 8 bytes "VIMspell"
79 * <versionnr> 1 byte VIMSPELLVERSION
80 *
81 *
82 * Sections make it possible to add information to the .spl file without
83 * making it incompatible with previous versions. There are two kinds of
84 * sections:
85 * 1. Not essential for correct spell checking. E.g. for making suggestions.
86 * These are skipped when not supported.
87 * 2. Optional information, but essential for spell checking when present.
88 * E.g. conditions for affixes. When this section is present but not
89 * supported an error message is given.
90 *
91 * <SECTIONS>: <section> ... <sectionend>
92 *
93 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
94 *
95 * <sectionID> 1 byte number from 0 to 254 identifying the section
96 *
97 * <sectionflags> 1 byte SNF_REQUIRED: this section is required for correct
98 * spell checking
99 *
100 * <sectionlen> 4 bytes length of section contents, MSB first
101 *
102 * <sectionend> 1 byte SN_END
103 *
104 *
105 * sectionID == SN_REGION: <regionname> ...
106 * <regionname> 2 bytes Up to 8 region names: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000107 * First <regionname> is region 1.
108 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000109 * sectionID == SN_CHARFLAGS: <charflagslen> <charflags>
110 * <folcharslen> <folchars>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000111 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
112 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000113 * 0x01 word character CF_WORD
114 * 0x02 upper-case character CF_UPPER
Bram Moolenaar5195e452005-08-19 20:32:47 +0000115 * <folcharslen> 2 bytes Number of bytes in <folchars>.
116 * <folchars> N bytes Folded characters, first one is for character 128.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000117 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000118 * sectionID == SN_MIDWORD: <midword>
119 * <midword> N bytes Characters that are word characters only when used
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000120 * in the middle of a word.
121 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000122 * sectionID == SN_PREFCOND: <prefcondcnt> <prefcond> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000123 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000124 * <prefcond> : <condlen> <condstr>
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000125 * <condlen> 1 byte Length of <condstr>.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000126 * <condstr> N bytes Condition for the prefix.
127 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000128 * sectionID == SN_REP: <repcount> <rep> ...
129 * <repcount> 2 bytes number of <rep> items, MSB first.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000130 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000131 * <repfromlen> 1 byte length of <repfrom>
132 * <repfrom> N bytes "from" part of replacement
133 * <reptolen> 1 byte length of <repto>
134 * <repto> N bytes "to" part of replacement
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000135 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000136 * sectionID == SN_SAL: <salflags> <salcount> <sal> ...
137 * <salflags> 1 byte flags for soundsalike conversion:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000138 * SAL_F0LLOWUP
139 * SAL_COLLAPSE
140 * SAL_REM_ACCENTS
Bram Moolenaar5195e452005-08-19 20:32:47 +0000141 * <salcount> 2 bytes number of <sal> items following
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000142 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000143 * <salfromlen> 1 byte length of <salfrom>
144 * <salfrom> N bytes "from" part of soundsalike
145 * <saltolen> 1 byte length of <salto>
146 * <salto> N bytes "to" part of soundsalike
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000147 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000148 * sectionID == SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
149 * <sofofromlen> 2 bytes length of <sofofrom>
150 * <sofofrom> N bytes "from" part of soundfold
151 * <sofotolen> 2 bytes length of <sofoto>
152 * <sofoto> N bytes "to" part of soundfold
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000153 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000154 * sectionID == SN_MAP: <mapstr>
155 * <mapstr> N bytes String with sequences of similar characters,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000156 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000157 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000158 * sectionID == SN_COMPOUND: <compmax> <compminlen> <compsylmax> <compflags>
159 * <compmax> 1 byte Maximum nr of words in compound word.
160 * <compminlen> 1 byte Minimal word length for compounding.
161 * <compsylmax> 1 byte Maximum nr of syllables in compound word.
162 * <compflags> N bytes Flags from COMPOUNDFLAGS items, separated by
163 * slashes.
164 *
Bram Moolenaar78622822005-08-23 21:00:13 +0000165 * sectionID == SN_NOBREAK: (empty, its presence is enough)
166 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000167 * sectionID == SN_SYLLABLE: <syllable>
168 * <syllable> N bytes String from SYLLABLE item.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000169 *
170 * <LWORDTREE>: <wordtree>
171 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000172 * <KWORDTREE>: <wordtree>
173 *
174 * <PREFIXTREE>: <wordtree>
175 *
176 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000177 * <wordtree>: <nodecount> <nodedata> ...
178 *
179 * <nodecount> 4 bytes Number of nodes following. MSB first.
180 *
181 * <nodedata>: <siblingcount> <sibling> ...
182 *
183 * <siblingcount> 1 byte Number of siblings in this node. The siblings
184 * follow in sorted order.
185 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000186 * <sibling>: <byte> [ <nodeidx> <xbyte>
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000187 * | <flags> [<flags2>] [<region>] [<affixID>]
188 * | [<pflags>] <affixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000189 *
190 * <byte> 1 byte Byte value of the sibling. Special cases:
191 * BY_NOFLAGS: End of word without flags and for all
192 * regions.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000193 * For PREFIXTREE <affixID> and
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000194 * <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000195 * BY_FLAGS: End of word, <flags> follow.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000196 * For PREFIXTREE <pflags>, <affixID>
Bram Moolenaar53805d12005-08-01 07:08:33 +0000197 * and <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000198 * BY_FLAGS2: End of word, <flags> and <flags2>
199 * follow. Not used in PREFIXTREE.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000200 * BY_INDEX: Child of sibling is shared, <nodeidx>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000201 * and <xbyte> follow.
202 *
203 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
204 *
205 * <xbyte> 1 byte byte value of the sibling.
206 *
207 * <flags> 1 byte bitmask of:
208 * WF_ALLCAP word must have only capitals
209 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000210 * WF_KEEPCAP keep-case word
211 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000212 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000213 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000214 * WF_REGION <region> follows
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000215 * WF_AFX <affixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000216 *
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000217 * <flags2> 1 byte Bitmask of:
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000218 * WF_HAS_AFF >> 8 word includes affix
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000219 * WF_NEEDCOMP >> 8 word only valid in compound
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000220 *
Bram Moolenaar53805d12005-08-01 07:08:33 +0000221 * <pflags> 1 byte bitmask of:
222 * WFP_RARE rare prefix
223 * WFP_NC non-combining prefix
224 * WFP_UP letter after prefix made upper case
225 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000226 * <region> 1 byte Bitmask for regions in which word is valid. When
227 * omitted it's valid in all regions.
228 * Lowest bit is for region 1.
229 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000230 * <affixID> 1 byte ID of affix that can be used with this word. In
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000231 * PREFIXTREE used for the required prefix ID.
232 *
233 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
234 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000235 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000236 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000237 */
238
Bram Moolenaare19defe2005-03-21 08:23:33 +0000239#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
240# include <io.h> /* for lseek(), must be before vim.h */
241#endif
242
243#include "vim.h"
244
245#if defined(FEAT_SYN_HL) || defined(PROTO)
246
247#ifdef HAVE_FCNTL_H
248# include <fcntl.h>
249#endif
250
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000251#define MAXWLEN 250 /* Assume max. word len is this many bytes.
252 Some places assume a word length fits in a
253 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000254
Bram Moolenaare52325c2005-08-22 22:54:29 +0000255/* Type used for indexes in the word tree need to be at least 4 bytes. If int
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000256 * is 8 bytes we could use something smaller, but what? */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000257#if SIZEOF_INT > 3
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000258typedef int idx_T;
259#else
260typedef long idx_T;
261#endif
262
263/* Flags used for a word. Only the lowest byte can be used, the region byte
264 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000265#define WF_REGION 0x01 /* region byte follows */
266#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
267#define WF_ALLCAP 0x04 /* word must be all capitals */
268#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000269#define WF_BANNED 0x10 /* bad word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000270#define WF_AFX 0x20 /* affix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000271#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000272#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000273
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000274/* for <flags2>, shifted up one byte to be used in wn_flags */
275#define WF_HAS_AFF 0x0100 /* word includes affix */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000276#define WF_NEEDCOMP 0x0200 /* word only valid in compound */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000277
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000278#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000279
Bram Moolenaar53805d12005-08-01 07:08:33 +0000280/* flags for <pflags> */
281#define WFP_RARE 0x01 /* rare prefix */
282#define WFP_NC 0x02 /* prefix is not combining */
283#define WFP_UP 0x04 /* to-upper prefix */
284
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000285/* Flags for postponed prefixes. Must be above affixID (one byte)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000286 * and prefcondnr (two bytes). */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000287#define WF_RAREPFX (WFP_RARE << 24) /* in sl_pidxs: flag for rare
288 * postponed prefix */
289#define WF_PFX_NC (WFP_NC << 24) /* in sl_pidxs: flag for non-combining
290 * postponed prefix */
291#define WF_PFX_UP (WFP_UP << 24) /* in sl_pidxs: flag for to-upper
292 * postponed prefix */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000293
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000294/* Special byte values for <byte>. Some are only used in the tree for
295 * postponed prefixes, some only in the other trees. This is a bit messy... */
296#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000297 * postponed prefix: no <pflags> */
298#define BY_INDEX 1 /* child is shared, index follows */
299#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
300 * postponed prefix: <pflags> follows */
301#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
302 * follow; never used in prefix tree */
303#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000305/* Info from "REP" and "SAL" entries in ".aff" file used in si_rep, sl_rep,
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000306 * and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000307 * One replacement: from "ft_from" to "ft_to". */
308typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000309{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000310 char_u *ft_from;
311 char_u *ft_to;
312} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000313
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000314/* Info from "SAL" entries in ".aff" file used in sl_sal.
315 * The info is split for quick processing by spell_soundfold().
316 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
317typedef struct salitem_S
318{
319 char_u *sm_lead; /* leading letters */
320 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000321 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000322 char_u *sm_rules; /* rules like ^, $, priority */
323 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000324#ifdef FEAT_MBYTE
325 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000326 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000327 int *sm_to_w; /* wide character copy of "sm_to" */
328#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000329} salitem_T;
330
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000331#ifdef FEAT_MBYTE
332typedef int salfirst_T;
333#else
334typedef short salfirst_T;
335#endif
336
Bram Moolenaar5195e452005-08-19 20:32:47 +0000337/* Values for SP_*ERROR are negative, positive values are used by
338 * read_cnt_string(). */
339#define SP_TRUNCERROR -1 /* spell file truncated error */
340#define SP_FORMERROR -2 /* format error in spell file */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000341#define SP_OTHERERROR -3 /* other error while reading spell file */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000342
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000343/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000344 * Structure used to store words and other info for one language, loaded from
345 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000346 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
347 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
348 *
349 * The "byts" array stores the possible bytes in each tree node, preceded by
350 * the number of possible bytes, sorted on byte value:
351 * <len> <byte1> <byte2> ...
352 * The "idxs" array stores the index of the child node corresponding to the
353 * byte in "byts".
354 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000355 * the flags, region mask and affixID for the word. There may be several
356 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000357 */
358typedef struct slang_S slang_T;
359struct slang_S
360{
361 slang_T *sl_next; /* next language */
362 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000363 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000364 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000365
Bram Moolenaar51485f02005-06-04 21:55:20 +0000366 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000367 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000368 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000369 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000370 char_u *sl_pbyts; /* prefix tree word bytes */
371 idx_T *sl_pidxs; /* prefix tree word indexes */
372
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000373 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000374
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000375 char_u *sl_midword; /* MIDWORD string or NULL */
376
Bram Moolenaar5195e452005-08-19 20:32:47 +0000377 int sl_compmax; /* COMPOUNDMAX (default: MAXWLEN) */
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000378 int sl_compminlen; /* COMPOUNDMIN (default: 0) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000379 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
380 regprog_T *sl_compprog; /* COMPOUNDFLAGS turned into a regexp progrm
381 * (NULL when no compounding) */
382 char_u *sl_compstartflags; /* flags for first compound word */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000383 char_u *sl_compallflags; /* all flags for compound words */
Bram Moolenaar78622822005-08-23 21:00:13 +0000384 char_u sl_nobreak; /* When TRUE: no spaces between words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000385 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
386 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000387
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000388 int sl_prefixcnt; /* number of items in "sl_prefprog" */
389 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
390
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000391 garray_T sl_rep; /* list of fromto_T entries from REP lines */
392 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
393 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000394 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000395 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000396 there is none */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000397 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
398 * "sl_sal_first" maps chars, when has_mbyte
399 * "sl_sal" is a list of wide char lists. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000400 int sl_followup; /* SAL followup */
401 int sl_collapse; /* SAL collapse_result */
402 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaarea424162005-06-16 21:51:00 +0000403 int sl_has_map; /* TRUE if there is a MAP line */
404#ifdef FEAT_MBYTE
405 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
406 int sl_map_array[256]; /* MAP for first 256 chars */
407#else
408 char_u sl_map_array[256]; /* MAP for first 256 chars */
409#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000410};
411
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000412/* First language that is loaded, start of the linked list of loaded
413 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000414static slang_T *first_lang = NULL;
415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000416/* Flags used in .spl file for soundsalike flags. */
417#define SAL_F0LLOWUP 1
418#define SAL_COLLAPSE 2
419#define SAL_REM_ACCENTS 4
420
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000421/*
422 * Structure used in "b_langp", filled from 'spelllang'.
423 */
424typedef struct langp_S
425{
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000426 slang_T *lp_slang; /* info for this language */
427 slang_T *lp_sallang; /* language used for sound folding or NULL */
428 slang_T *lp_replang; /* language used for REP items or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000429 int lp_region; /* bitmask for region or REGION_ALL */
430} langp_T;
431
432#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
433
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000434#define REGION_ALL 0xff /* word valid in all regions */
435
Bram Moolenaar5195e452005-08-19 20:32:47 +0000436#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
437#define VIMSPELLMAGICL 8
438#define VIMSPELLVERSION 50
439
440/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
441#define SN_REGION 0 /* <regionname> section */
442#define SN_CHARFLAGS 1 /* charflags section */
443#define SN_MIDWORD 2 /* <midword> section */
444#define SN_PREFCOND 3 /* <prefcond> section */
445#define SN_REP 4 /* REP items section */
446#define SN_SAL 5 /* SAL items section */
447#define SN_SOFO 6 /* soundfolding section */
448#define SN_MAP 7 /* MAP items section */
449#define SN_COMPOUND 8 /* compound words section */
450#define SN_SYLLABLE 9 /* syllable section */
Bram Moolenaar78622822005-08-23 21:00:13 +0000451#define SN_NOBREAK 10 /* NOBREAK section */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000452#define SN_END 255 /* end of sections */
453
454#define SNF_REQUIRED 1 /* <sectionflags>: required section */
455
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000456/* Result values. Lower number is accepted over higher one. */
457#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000458#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000459#define SP_RARE 1
460#define SP_LOCAL 2
461#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000462
Bram Moolenaar7887d882005-07-01 22:33:52 +0000463/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000464static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000465
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000466/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000467 * Information used when looking for suggestions.
468 */
469typedef struct suginfo_S
470{
471 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000472 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000473 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000474 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000475 char_u *su_badptr; /* start of bad word in line */
476 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000477 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000478 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
479 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000480 char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */
481 slang_T *su_slang_first; /* slang_T used for su_sal_badword */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000482 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000483 slang_T *su_sallang; /* default language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000484} suginfo_T;
485
486/* One word suggestion. Used in "si_ga". */
487typedef struct suggest_S
488{
489 char_u *st_word; /* suggested word, allocated string */
490 int st_orglen; /* length of replaced text */
491 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000492 int st_altscore; /* used when st_score compares equal */
493 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000494 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000495 slang_T *st_slang; /* language used for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000496} suggest_T;
497
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000498#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000499
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000500/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
501 * called the score may change, thus we need to keep more than what is
502 * displayed. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000503#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 50 ? 50 : (su)->su_maxcount)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000504
505/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
506 * of suggestions that are not going to be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000507#define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000508
509/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000510#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000511#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000512#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000513#define SCORE_RARE 180 /* rare word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000514#define SCORE_SWAP 90 /* swap two characters */
515#define SCORE_SWAP3 110 /* swap two characters in three */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000516#define SCORE_REP 65 /* REP replacement */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000517#define SCORE_SUBST 93 /* substitute a character */
518#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000519#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000520#define SCORE_DEL 94 /* delete a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000521#define SCORE_DELDUP 66 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000522#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000523#define SCORE_INS 96 /* insert a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000524#define SCORE_INSDUP 67 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000525#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000526#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000527
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000528#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000529#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
530 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000531
532#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000533#define SCORE_MAXMAX 999999 /* accept any score */
534
535/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000536 * Structure to store info for word matching.
537 */
538typedef struct matchinf_S
539{
540 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000541
542 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000543 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000544 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000545 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000546 char_u *mi_cend; /* char after what was used for
547 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000548
549 /* case-folded text */
550 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000551 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000552
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000553 /* for when checking word after a prefix */
554 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000555 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000556 int mi_prefcnt; /* number of entries at mi_prefarridx */
557 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000558#ifdef FEAT_MBYTE
559 int mi_cprefixlen; /* byte length of prefix in original
560 case */
561#else
562# define mi_cprefixlen mi_prefixlen /* it's the same value */
563#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000564
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000565 /* for when checking a compound word */
566 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000567 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
568 int mi_complen; /* nr of compound words used */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000569
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000570 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000571 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000572 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000573 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000574
575 /* for NOBREAK */
576 int mi_result2; /* "mi_resul" without following word */
577 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000578} matchinf_T;
579
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000580/*
581 * The tables used for recognizing word characters according to spelling.
582 * These are only used for the first 256 characters of 'encoding'.
583 */
584typedef struct spelltab_S
585{
586 char_u st_isw[256]; /* flags: is word char */
587 char_u st_isu[256]; /* flags: is uppercase char */
588 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000589 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000590} spelltab_T;
591
592static spelltab_T spelltab;
593static int did_set_spelltab;
594
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000595#define CF_WORD 0x01
596#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000597
598static void clear_spell_chartab __ARGS((spelltab_T *sp));
599static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000600static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
601static int spell_iswordp_nmw __ARGS((char_u *p));
602#ifdef FEAT_MBYTE
603static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
604#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000605static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000606
607/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000608 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000609 */
610typedef enum
611{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000612 STATE_START = 0, /* At start of node check for NUL bytes (goodword
613 * ends); if badword ends there is a match, otherwise
614 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000615 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000616 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000617 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
618 STATE_PLAIN, /* Use each byte of the node. */
619 STATE_DEL, /* Delete a byte from the bad word. */
620 STATE_INS, /* Insert a byte in the bad word. */
621 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000622 STATE_UNSWAP, /* Undo swap two characters. */
623 STATE_SWAP3, /* Swap two characters over three. */
624 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000625 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000626 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000627 STATE_REP_INI, /* Prepare for using REP items. */
628 STATE_REP, /* Use matching REP items from the .aff file. */
629 STATE_REP_UNDO, /* Undo a REP item replacement. */
630 STATE_FINAL /* End of this node. */
631} state_T;
632
633/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000634 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000635 */
636typedef struct trystate_S
637{
Bram Moolenaarea424162005-06-16 21:51:00 +0000638 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000639 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000640 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000641 short ts_curi; /* index in list of child nodes */
642 char_u ts_fidx; /* index in fword[], case-folded bad word */
643 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
644 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000645 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000646 * PFD_PREFIXTREE or PFD_NOPREFIX */
647 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000648#ifdef FEAT_MBYTE
649 char_u ts_tcharlen; /* number of bytes in tword character */
650 char_u ts_tcharidx; /* current byte index in tword character */
651 char_u ts_isdiff; /* DIFF_ values */
652 char_u ts_fcharstart; /* index in fword where badword char started */
653#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000654 char_u ts_prewordlen; /* length of word in "preword[]" */
655 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000656 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000657 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000658 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000659 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000660} trystate_T;
661
Bram Moolenaarea424162005-06-16 21:51:00 +0000662/* values for ts_isdiff */
663#define DIFF_NONE 0 /* no different byte (yet) */
664#define DIFF_YES 1 /* different byte found */
665#define DIFF_INSERT 2 /* inserting character */
666
Bram Moolenaard12a1322005-08-21 22:08:24 +0000667/* values for ts_flags */
668#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
669#define TSF_DIDSPLIT 2 /* tried split at this point */
670
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000671/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000672#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000673#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
674#define PFD_NOTSPECIAL 0xfd /* first value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000675
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000676/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000677#define FIND_FOLDWORD 0 /* find word case-folded */
678#define FIND_KEEPWORD 1 /* find keep-case word */
679#define FIND_PREFIX 2 /* find word after prefix */
680#define FIND_COMPOUND 3 /* find case-folded compound word */
681#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000682
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000683static slang_T *slang_alloc __ARGS((char_u *lang));
684static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000685static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000686static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000687static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000688static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req));
Bram Moolenaard12a1322005-08-21 22:08:24 +0000689static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000690static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000691static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000692static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000693static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000694static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000695static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000696static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000697static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000698static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000699static char_u *read_string __ARGS((FILE *fd, int cnt));
700static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
701static int read_charflags_section __ARGS((FILE *fd));
702static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
703static int read_rep_section __ARGS((FILE *fd, slang_T *slang));
704static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
705static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
706static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000707static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000708static int init_syl_tab __ARGS((slang_T *slang));
709static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000710static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
711static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000712#ifdef FEAT_MBYTE
713static int *mb_str2wide __ARGS((char_u *s));
714#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000715static idx_T read_tree __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, int startidx, int prefixtree, int maxprefcondnr));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000716static void clear_midword __ARGS((buf_T *buf));
717static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000718static int find_region __ARGS((char_u *rp, char_u *region));
719static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000720static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000721static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000722static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000723static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000724static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000725static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +0000726static void spell_find_suggest __ARGS((char_u *badptr, suginfo_T *su, int maxcount, int banbadword, int need_cap));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000727#ifdef FEAT_EVAL
728static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
729#endif
730static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
731static void spell_suggest_intern __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000732static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000733static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000734static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000735static void suggest_try_special __ARGS((suginfo_T *su));
736static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000737static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000738#ifdef FEAT_MBYTE
739static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
740#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000741static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000742static void score_comp_sal __ARGS((suginfo_T *su));
743static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000744static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000745static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000746static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000747static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000748static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000749static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus, slang_T *slang));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000750static void add_banned __ARGS((suginfo_T *su, char_u *word));
751static int was_banned __ARGS((suginfo_T *su, char_u *word));
752static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000753static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000754static void rescore_one __ARGS((suginfo_T *su, suggest_T *stp));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000755static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000756static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
757static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
758static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000759#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000760static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000761#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000762static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000763static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000764static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000765static linenr_T dump_prefixes __ARGS((slang_T *slang, char_u *word, int round, int flags, linenr_T startlnum));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000766
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000767/*
768 * Use our own character-case definitions, because the current locale may
769 * differ from what the .spl file uses.
770 * These must not be called with negative number!
771 */
772#ifndef FEAT_MBYTE
773/* Non-multi-byte implementation. */
774# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
775# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
776# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
777#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000778# if defined(HAVE_WCHAR_H)
779# include <wchar.h> /* for towupper() and towlower() */
780# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000781/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
782 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
783 * the "w" library function for characters above 255 if available. */
784# ifdef HAVE_TOWLOWER
785# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
786 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
787# else
788# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
789 : (c) < 256 ? spelltab.st_fold[c] : (c))
790# endif
791
792# ifdef HAVE_TOWUPPER
793# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
794 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
795# else
796# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
797 : (c) < 256 ? spelltab.st_upper[c] : (c))
798# endif
799
800# ifdef HAVE_ISWUPPER
801# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
802 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
803# else
804# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000805 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000806# endif
807#endif
808
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000809
810static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000811static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000812static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000813static char *e_affname = N_("Affix name too long in %s line %d: %s");
814static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
815static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000816static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000817
818/*
819 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000820 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000821 * "*attrp" is set to the highlight index for a badly spelled word. For a
822 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000823 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000824 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000825 * "capcol" is used to check for a Capitalised word after the end of a
826 * sentence. If it's zero then perform the check. Return the column where to
827 * check next, or -1 when no sentence end was found. If it's NULL then don't
828 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000829 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000830 * Returns the length of the word in bytes, also when it's OK, so that the
831 * caller can skip over the word.
832 */
833 int
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000834spell_check(wp, ptr, attrp, capcol)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000835 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000836 char_u *ptr;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000837 hlf_T *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000838 int *capcol; /* column to check for Capital */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000839{
840 matchinf_T mi; /* Most things are put in "mi" so that it can
841 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000842 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000843 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000844 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000845 int lpi;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000846
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000847 /* A word never starts at a space or a control character. Return quickly
848 * then, skipping over the character. */
849 if (*ptr <= ' ')
850 return 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000851 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000852
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000853 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +0000854 * 0X99FF. But always do check spelling to find "3GPP" and "11
855 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000856 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000857 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000858 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
859 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000860 else
861 mi.mi_end = skipdigits(ptr);
Bram Moolenaar43abc522005-12-10 20:15:02 +0000862 nrlen = mi.mi_end - ptr;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000863 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000864
Bram Moolenaar0c405862005-06-22 22:26:26 +0000865 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000866 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +0000867 mi.mi_fend = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000868 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000869 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000870 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000871 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000872 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000873 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000874
875 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
876 {
877 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000878 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000879 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000880 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000881 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000882 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000883 if (capcol != NULL)
884 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000885
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000886 /* We always use the characters up to the next non-word character,
887 * also for bad words. */
888 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000889
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000890 /* Check caps type later. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000891 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000892
Bram Moolenaar5195e452005-08-19 20:32:47 +0000893 /* case-fold the word with one non-word character, so that we can check
894 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000895 if (*mi.mi_fend != NUL)
896 mb_ptr_adv(mi.mi_fend);
897
898 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
899 MAXWLEN + 1);
900 mi.mi_fwordlen = STRLEN(mi.mi_fword);
901
902 /* The word is bad unless we recognize it. */
903 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +0000904 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000905
906 /*
907 * Loop over the languages specified in 'spelllang'.
908 * We check them all, because a matching word may be longer than an
909 * already found matching word.
910 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000911 for (lpi = 0; lpi < wp->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000912 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000913 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, lpi);
914
915 /* If reloading fails the language is still in the list but everything
916 * has been cleared. */
917 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
918 continue;
919
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000920 /* Check for a matching word in case-folded words. */
921 find_word(&mi, FIND_FOLDWORD);
922
923 /* Check for a matching word in keep-case words. */
924 find_word(&mi, FIND_KEEPWORD);
925
926 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000927 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +0000928
929 /* For a NOBREAK language, may want to use a word without a following
930 * word as a backup. */
931 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
932 && mi.mi_result2 != SP_BAD)
933 {
934 mi.mi_result = mi.mi_result2;
935 mi.mi_end = mi.mi_end2;
936 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000937 }
938
939 if (mi.mi_result != SP_OK)
940 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000941 /* If we found a number skip over it. Allows for "42nd". Do flag
942 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000943 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000944 {
945 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
946 return nrlen;
947 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000948
949 /* When we are at a non-word character there is no error, just
950 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000951 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000952 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000953 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
954 {
955 regmatch_T regmatch;
956
957 /* Check for end of sentence. */
958 regmatch.regprog = wp->w_buffer->b_cap_prog;
959 regmatch.rm_ic = FALSE;
960 if (vim_regexec(&regmatch, ptr, 0))
961 *capcol = (int)(regmatch.endp[0] - ptr);
962 }
963
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000964#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000965 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000966 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000967#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000968 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000969 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000970 else if (mi.mi_end == ptr)
971 /* Always include at least one character. Required for when there
972 * is a mixup in "midword". */
973 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +0000974 else if (mi.mi_result == SP_BAD
975 && LANGP_ENTRY(wp->w_buffer->b_langp, 0)->lp_slang->sl_nobreak)
976 {
977 char_u *p, *fp;
978 int save_result = mi.mi_result;
979
980 /* First language in 'spelllang' is NOBREAK. Find first position
981 * at which any word would be valid. */
982 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000983 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +0000984 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000985 p = mi.mi_word;
986 fp = mi.mi_fword;
987 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +0000988 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000989 mb_ptr_adv(p);
990 mb_ptr_adv(fp);
991 if (p >= mi.mi_end)
992 break;
993 mi.mi_compoff = fp - mi.mi_fword;
994 find_word(&mi, FIND_COMPOUND);
995 if (mi.mi_result != SP_BAD)
996 {
997 mi.mi_end = p;
998 break;
999 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001000 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001001 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001002 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001003 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001004
1005 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001006 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001007 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001008 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001009 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001010 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001011 }
1012
Bram Moolenaar5195e452005-08-19 20:32:47 +00001013 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1014 {
1015 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001016 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001017 return wrongcaplen;
1018 }
1019
Bram Moolenaar51485f02005-06-04 21:55:20 +00001020 return (int)(mi.mi_end - ptr);
1021}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001022
Bram Moolenaar51485f02005-06-04 21:55:20 +00001023/*
1024 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001025 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1026 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1027 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1028 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001029 *
1030 * For a match mip->mi_result is updated.
1031 */
1032 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001033find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001034 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001035 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001036{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001037 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001038 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001039 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001040 int endidxcnt = 0;
1041 int len;
1042 int wlen = 0;
1043 int flen;
1044 int c;
1045 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001046 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001047#ifdef FEAT_MBYTE
1048 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001049#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001050 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001051 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001052 slang_T *slang = mip->mi_lp->lp_slang;
1053 unsigned flags;
1054 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001055 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001056 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001057 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001058 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001059
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001060 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001061 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001062 /* Check for word with matching case in keep-case tree. */
1063 ptr = mip->mi_word;
1064 flen = 9999; /* no case folding, always enough bytes */
1065 byts = slang->sl_kbyts;
1066 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001067
1068 if (mode == FIND_KEEPCOMPOUND)
1069 /* Skip over the previously found word(s). */
1070 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001071 }
1072 else
1073 {
1074 /* Check for case-folded in case-folded tree. */
1075 ptr = mip->mi_fword;
1076 flen = mip->mi_fwordlen; /* available case-folded bytes */
1077 byts = slang->sl_fbyts;
1078 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001079
1080 if (mode == FIND_PREFIX)
1081 {
1082 /* Skip over the prefix. */
1083 wlen = mip->mi_prefixlen;
1084 flen -= mip->mi_prefixlen;
1085 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001086 else if (mode == FIND_COMPOUND)
1087 {
1088 /* Skip over the previously found word(s). */
1089 wlen = mip->mi_compoff;
1090 flen -= mip->mi_compoff;
1091 }
1092
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001093 }
1094
Bram Moolenaar51485f02005-06-04 21:55:20 +00001095 if (byts == NULL)
1096 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001097
Bram Moolenaar51485f02005-06-04 21:55:20 +00001098 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001099 * Repeat advancing in the tree until:
1100 * - there is a byte that doesn't match,
1101 * - we reach the end of the tree,
1102 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001103 */
1104 for (;;)
1105 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001106 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001107 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001108
1109 len = byts[arridx++];
1110
1111 /* If the first possible byte is a zero the word could end here.
1112 * Remember this index, we first check for the longest word. */
1113 if (byts[arridx] == 0)
1114 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001115 if (endidxcnt == MAXWLEN)
1116 {
1117 /* Must be a corrupted spell file. */
1118 EMSG(_(e_format));
1119 return;
1120 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001121 endlen[endidxcnt] = wlen;
1122 endidx[endidxcnt++] = arridx++;
1123 --len;
1124
1125 /* Skip over the zeros, there can be several flag/region
1126 * combinations. */
1127 while (len > 0 && byts[arridx] == 0)
1128 {
1129 ++arridx;
1130 --len;
1131 }
1132 if (len == 0)
1133 break; /* no children, word must end here */
1134 }
1135
1136 /* Stop looking at end of the line. */
1137 if (ptr[wlen] == NUL)
1138 break;
1139
1140 /* Perform a binary search in the list of accepted bytes. */
1141 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001142 if (c == TAB) /* <Tab> is handled like <Space> */
1143 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001144 lo = arridx;
1145 hi = arridx + len - 1;
1146 while (lo < hi)
1147 {
1148 m = (lo + hi) / 2;
1149 if (byts[m] > c)
1150 hi = m - 1;
1151 else if (byts[m] < c)
1152 lo = m + 1;
1153 else
1154 {
1155 lo = hi = m;
1156 break;
1157 }
1158 }
1159
1160 /* Stop if there is no matching byte. */
1161 if (hi < lo || byts[lo] != c)
1162 break;
1163
1164 /* Continue at the child (if there is one). */
1165 arridx = idxs[lo];
1166 ++wlen;
1167 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001168
1169 /* One space in the good word may stand for several spaces in the
1170 * checked word. */
1171 if (c == ' ')
1172 {
1173 for (;;)
1174 {
1175 if (flen <= 0 && *mip->mi_fend != NUL)
1176 flen = fold_more(mip);
1177 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1178 break;
1179 ++wlen;
1180 --flen;
1181 }
1182 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001183 }
1184
1185 /*
1186 * Verify that one of the possible endings is valid. Try the longest
1187 * first.
1188 */
1189 while (endidxcnt > 0)
1190 {
1191 --endidxcnt;
1192 arridx = endidx[endidxcnt];
1193 wlen = endlen[endidxcnt];
1194
1195#ifdef FEAT_MBYTE
1196 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1197 continue; /* not at first byte of character */
1198#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001199 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001200 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001201 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001202 continue; /* next char is a word character */
1203 word_ends = FALSE;
1204 }
1205 else
1206 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001207 /* The prefix flag is before compound flags. Once a valid prefix flag
1208 * has been found we try compound flags. */
1209 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001210
1211#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001212 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001213 {
1214 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001215 * when folding case. This can be slow, take a shortcut when the
1216 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001217 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001218 if (STRNCMP(ptr, p, wlen) != 0)
1219 {
1220 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1221 mb_ptr_adv(p);
1222 wlen = p - mip->mi_word;
1223 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001224 }
1225#endif
1226
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001227 /* Check flags and region. For FIND_PREFIX check the condition and
1228 * prefix ID.
1229 * Repeat this if there are more flags/region alternatives until there
1230 * is a match. */
1231 res = SP_BAD;
1232 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1233 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001234 {
1235 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001236
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001237 /* For the fold-case tree check that the case of the checked word
1238 * matches with what the word in the tree requires.
1239 * For keep-case tree the case is always right. For prefixes we
1240 * don't bother to check. */
1241 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001242 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001243 if (mip->mi_cend != mip->mi_word + wlen)
1244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001245 /* mi_capflags was set for a different word length, need
1246 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001247 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001248 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001249 }
1250
Bram Moolenaar0c405862005-06-22 22:26:26 +00001251 if (mip->mi_capflags == WF_KEEPCAP
1252 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001253 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001254 }
1255
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001256 /* When mode is FIND_PREFIX the word must support the prefix:
1257 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001258 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001259 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001260 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001261 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001262 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001263 mip->mi_word + mip->mi_cprefixlen, slang,
1264 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001265 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001266 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001267
1268 /* Use the WF_RARE flag for a rare prefix. */
1269 if (c & WF_RAREPFX)
1270 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001271 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001272 }
1273
Bram Moolenaar78622822005-08-23 21:00:13 +00001274 if (slang->sl_nobreak)
1275 {
1276 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1277 && (flags & WF_BANNED) == 0)
1278 {
1279 /* NOBREAK: found a valid following word. That's all we
1280 * need to know, so return. */
1281 mip->mi_result = SP_OK;
1282 break;
1283 }
1284 }
1285
1286 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1287 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001288 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00001289 /* If there is no flag or the word is shorter than
1290 * COMPOUNDMIN reject it quickly.
1291 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001292 * that's too short... Myspell compatibility requires this
1293 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001294 if (((unsigned)flags >> 24) == 0
1295 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001296 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001297#ifdef FEAT_MBYTE
1298 /* For multi-byte chars check character length against
1299 * COMPOUNDMIN. */
1300 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001301 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001302 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1303 wlen - mip->mi_compoff) < slang->sl_compminlen)
1304 continue;
1305#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001306
Bram Moolenaare52325c2005-08-22 22:54:29 +00001307 /* Limit the number of compound words to COMPOUNDMAX if no
1308 * maximum for syllables is specified. */
1309 if (!word_ends && mip->mi_complen + 2 > slang->sl_compmax
1310 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001311 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001312
Bram Moolenaard12a1322005-08-21 22:08:24 +00001313 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001314 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001315 ? slang->sl_compstartflags
1316 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001317 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001318 continue;
1319
Bram Moolenaare52325c2005-08-22 22:54:29 +00001320 if (mode == FIND_COMPOUND)
1321 {
1322 int capflags;
1323
1324 /* Need to check the caps type of the appended compound
1325 * word. */
1326#ifdef FEAT_MBYTE
1327 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1328 mip->mi_compoff) != 0)
1329 {
1330 /* case folding may have changed the length */
1331 p = mip->mi_word;
1332 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1333 mb_ptr_adv(p);
1334 }
1335 else
1336#endif
1337 p = mip->mi_word + mip->mi_compoff;
1338 capflags = captype(p, mip->mi_word + wlen);
1339 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1340 && (flags & WF_FIXCAP) != 0))
1341 continue;
1342
1343 if (capflags != WF_ALLCAP)
1344 {
1345 /* When the character before the word is a word
1346 * character we do not accept a Onecap word. We do
1347 * accept a no-caps word, even when the dictionary
1348 * word specifies ONECAP. */
1349 mb_ptr_back(mip->mi_word, p);
1350 if (spell_iswordp_nmw(p)
1351 ? capflags == WF_ONECAP
1352 : (flags & WF_ONECAP) != 0
1353 && capflags != WF_ONECAP)
1354 continue;
1355 }
1356 }
1357
Bram Moolenaar5195e452005-08-19 20:32:47 +00001358 /* If the word ends the sequence of compound flags of the
1359 * words must match with one of the COMPOUNDFLAGS items and
1360 * the number of syllables must not be too large. */
1361 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1362 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1363 if (word_ends)
1364 {
1365 char_u fword[MAXWLEN];
1366
1367 if (slang->sl_compsylmax < MAXWLEN)
1368 {
1369 /* "fword" is only needed for checking syllables. */
1370 if (ptr == mip->mi_word)
1371 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1372 else
1373 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1374 }
1375 if (!can_compound(slang, fword, mip->mi_compflags))
1376 continue;
1377 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001378 }
1379
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001380 /* Check NEEDCOMPOUND: can't use word without compounding. */
1381 else if (flags & WF_NEEDCOMP)
1382 continue;
1383
Bram Moolenaar78622822005-08-23 21:00:13 +00001384 nobreak_result = SP_OK;
1385
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001386 if (!word_ends)
1387 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001388 int save_result = mip->mi_result;
1389 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001390 langp_T *save_lp = mip->mi_lp;
1391 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001392
1393 /* Check that a valid word follows. If there is one and we
1394 * are compounding, it will set "mi_result", thus we are
1395 * always finished here. For NOBREAK we only check that a
1396 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001397 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001398 if (slang->sl_nobreak)
1399 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001400
1401 /* Find following word in case-folded tree. */
1402 mip->mi_compoff = endlen[endidxcnt];
1403#ifdef FEAT_MBYTE
1404 if (has_mbyte && mode == FIND_KEEPWORD)
1405 {
1406 /* Compute byte length in case-folded word from "wlen":
1407 * byte length in keep-case word. Length may change when
1408 * folding case. This can be slow, take a shortcut when
1409 * the case-folded word is equal to the keep-case word. */
1410 p = mip->mi_fword;
1411 if (STRNCMP(ptr, p, wlen) != 0)
1412 {
1413 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1414 mb_ptr_adv(p);
1415 mip->mi_compoff = p - mip->mi_fword;
1416 }
1417 }
1418#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001419 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001420 ++mip->mi_complen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001421
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001422 /* For NOBREAK we need to try all NOBREAK languages, at least
1423 * to find the ".add" file(s). */
1424 for (lpi = 0; lpi < mip->mi_buf->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001425 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001426 if (slang->sl_nobreak)
1427 {
1428 mip->mi_lp = LANGP_ENTRY(mip->mi_buf->b_langp, lpi);
1429 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1430 || !mip->mi_lp->lp_slang->sl_nobreak)
1431 continue;
1432 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001433
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001434 find_word(mip, FIND_COMPOUND);
1435
1436 /* When NOBREAK any word that matches is OK. Otherwise we
1437 * need to find the longest match, thus try with keep-case
1438 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001439 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1440 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001441 /* Find following word in keep-case tree. */
1442 mip->mi_compoff = wlen;
1443 find_word(mip, FIND_KEEPCOMPOUND);
1444
1445 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1446 {
1447 /* Check for following word with prefix. */
1448 mip->mi_compoff = c;
1449 find_prefix(mip, FIND_COMPOUND);
1450 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001451 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001452
1453 if (!slang->sl_nobreak)
1454 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001455 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001456 --mip->mi_complen;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001457 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001458
Bram Moolenaar78622822005-08-23 21:00:13 +00001459 if (slang->sl_nobreak)
1460 {
1461 nobreak_result = mip->mi_result;
1462 mip->mi_result = save_result;
1463 mip->mi_end = save_end;
1464 }
1465 else
1466 {
1467 if (mip->mi_result == SP_OK)
1468 break;
1469 continue;
1470 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001471 }
1472
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001473 if (flags & WF_BANNED)
1474 res = SP_BANNED;
1475 else if (flags & WF_REGION)
1476 {
1477 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001478 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001479 res = SP_OK;
1480 else
1481 res = SP_LOCAL;
1482 }
1483 else if (flags & WF_RARE)
1484 res = SP_RARE;
1485 else
1486 res = SP_OK;
1487
Bram Moolenaar78622822005-08-23 21:00:13 +00001488 /* Always use the longest match and the best result. For NOBREAK
1489 * we separately keep the longest match without a following good
1490 * word as a fall-back. */
1491 if (nobreak_result == SP_BAD)
1492 {
1493 if (mip->mi_result2 > res)
1494 {
1495 mip->mi_result2 = res;
1496 mip->mi_end2 = mip->mi_word + wlen;
1497 }
1498 else if (mip->mi_result2 == res
1499 && mip->mi_end2 < mip->mi_word + wlen)
1500 mip->mi_end2 = mip->mi_word + wlen;
1501 }
1502 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001503 {
1504 mip->mi_result = res;
1505 mip->mi_end = mip->mi_word + wlen;
1506 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001507 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001508 mip->mi_end = mip->mi_word + wlen;
1509
Bram Moolenaar78622822005-08-23 21:00:13 +00001510 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001511 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001512 }
1513
Bram Moolenaar78622822005-08-23 21:00:13 +00001514 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001515 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001516 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001517}
1518
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001519/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00001520 * Return TRUE if "flags" is a valid sequence of compound flags and
1521 * "word[len]" does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001522 */
1523 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001524can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001525 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001526 char_u *word;
1527 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001528{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001529 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001530#ifdef FEAT_MBYTE
1531 char_u uflags[MAXWLEN * 2];
1532 int i;
1533#endif
1534 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001535
1536 if (slang->sl_compprog == NULL)
1537 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001538#ifdef FEAT_MBYTE
1539 if (enc_utf8)
1540 {
1541 /* Need to convert the single byte flags to utf8 characters. */
1542 p = uflags;
1543 for (i = 0; flags[i] != NUL; ++i)
1544 p += mb_char2bytes(flags[i], p);
1545 *p = NUL;
1546 p = uflags;
1547 }
1548 else
1549#endif
1550 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001551 regmatch.regprog = slang->sl_compprog;
1552 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001553 if (!vim_regexec(&regmatch, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001554 return FALSE;
1555
Bram Moolenaare52325c2005-08-22 22:54:29 +00001556 /* Count the number of syllables. This may be slow, do it last. If there
1557 * are too many syllables AND the number of compound words is above
1558 * COMPOUNDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001559 if (slang->sl_compsylmax < MAXWLEN
1560 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001561 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001562 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001563}
1564
1565/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001566 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1567 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001568 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001569 */
1570 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001571valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001572 int totprefcnt; /* nr of prefix IDs */
1573 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001574 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001575 char_u *word;
1576 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001577 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001578{
1579 int prefcnt;
1580 int pidx;
1581 regprog_T *rp;
1582 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001583 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001584
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001585 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001586 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1587 {
1588 pidx = slang->sl_pidxs[arridx + prefcnt];
1589
1590 /* Check the prefix ID. */
1591 if (prefid != (pidx & 0xff))
1592 continue;
1593
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001594 /* Check if the prefix doesn't combine and the word already has a
1595 * suffix. */
1596 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1597 continue;
1598
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001599 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001600 * stored in the two bytes above the prefix ID byte. */
1601 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001602 if (rp != NULL)
1603 {
1604 regmatch.regprog = rp;
1605 regmatch.rm_ic = FALSE;
1606 if (!vim_regexec(&regmatch, word, 0))
1607 continue;
1608 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001609 else if (cond_req)
1610 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001611
Bram Moolenaar53805d12005-08-01 07:08:33 +00001612 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001613 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001614 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001615 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001616}
1617
1618/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001619 * Check if the word at "mip->mi_word" has a matching prefix.
1620 * If it does, then check the following word.
1621 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001622 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1623 * prefix in a compound word.
1624 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001625 * For a match mip->mi_result is updated.
1626 */
1627 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001628find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001629 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001630 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001631{
1632 idx_T arridx = 0;
1633 int len;
1634 int wlen = 0;
1635 int flen;
1636 int c;
1637 char_u *ptr;
1638 idx_T lo, hi, m;
1639 slang_T *slang = mip->mi_lp->lp_slang;
1640 char_u *byts;
1641 idx_T *idxs;
1642
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001643 byts = slang->sl_pbyts;
1644 if (byts == NULL)
1645 return; /* array is empty */
1646
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001647 /* We use the case-folded word here, since prefixes are always
1648 * case-folded. */
1649 ptr = mip->mi_fword;
1650 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001651 if (mode == FIND_COMPOUND)
1652 {
1653 /* Skip over the previously found word(s). */
1654 ptr += mip->mi_compoff;
1655 flen -= mip->mi_compoff;
1656 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001657 idxs = slang->sl_pidxs;
1658
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001659 /*
1660 * Repeat advancing in the tree until:
1661 * - there is a byte that doesn't match,
1662 * - we reach the end of the tree,
1663 * - or we reach the end of the line.
1664 */
1665 for (;;)
1666 {
1667 if (flen == 0 && *mip->mi_fend != NUL)
1668 flen = fold_more(mip);
1669
1670 len = byts[arridx++];
1671
1672 /* If the first possible byte is a zero the prefix could end here.
1673 * Check if the following word matches and supports the prefix. */
1674 if (byts[arridx] == 0)
1675 {
1676 /* There can be several prefixes with different conditions. We
1677 * try them all, since we don't know which one will give the
1678 * longest match. The word is the same each time, pass the list
1679 * of possible prefixes to find_word(). */
1680 mip->mi_prefarridx = arridx;
1681 mip->mi_prefcnt = len;
1682 while (len > 0 && byts[arridx] == 0)
1683 {
1684 ++arridx;
1685 --len;
1686 }
1687 mip->mi_prefcnt -= len;
1688
1689 /* Find the word that comes after the prefix. */
1690 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001691 if (mode == FIND_COMPOUND)
1692 /* Skip over the previously found word(s). */
1693 mip->mi_prefixlen += mip->mi_compoff;
1694
Bram Moolenaar53805d12005-08-01 07:08:33 +00001695#ifdef FEAT_MBYTE
1696 if (has_mbyte)
1697 {
1698 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001699 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1700 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001701 }
1702 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001703 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001704#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001705 find_word(mip, FIND_PREFIX);
1706
1707
1708 if (len == 0)
1709 break; /* no children, word must end here */
1710 }
1711
1712 /* Stop looking at end of the line. */
1713 if (ptr[wlen] == NUL)
1714 break;
1715
1716 /* Perform a binary search in the list of accepted bytes. */
1717 c = ptr[wlen];
1718 lo = arridx;
1719 hi = arridx + len - 1;
1720 while (lo < hi)
1721 {
1722 m = (lo + hi) / 2;
1723 if (byts[m] > c)
1724 hi = m - 1;
1725 else if (byts[m] < c)
1726 lo = m + 1;
1727 else
1728 {
1729 lo = hi = m;
1730 break;
1731 }
1732 }
1733
1734 /* Stop if there is no matching byte. */
1735 if (hi < lo || byts[lo] != c)
1736 break;
1737
1738 /* Continue at the child (if there is one). */
1739 arridx = idxs[lo];
1740 ++wlen;
1741 --flen;
1742 }
1743}
1744
1745/*
1746 * Need to fold at least one more character. Do until next non-word character
1747 * for efficiency.
1748 * Return the length of the folded chars in bytes.
1749 */
1750 static int
1751fold_more(mip)
1752 matchinf_T *mip;
1753{
1754 int flen;
1755 char_u *p;
1756
1757 p = mip->mi_fend;
1758 do
1759 {
1760 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001761 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001762
1763 /* Include the non-word character so that we can check for the
1764 * word end. */
1765 if (*mip->mi_fend != NUL)
1766 mb_ptr_adv(mip->mi_fend);
1767
1768 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1769 mip->mi_fword + mip->mi_fwordlen,
1770 MAXWLEN - mip->mi_fwordlen);
1771 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1772 mip->mi_fwordlen += flen;
1773 return flen;
1774}
1775
1776/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001777 * Check case flags for a word. Return TRUE if the word has the requested
1778 * case.
1779 */
1780 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001781spell_valid_case(wordflags, treeflags)
1782 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001783 int treeflags; /* flags for the word in the spell tree */
1784{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001785 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001786 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001787 && ((treeflags & WF_ONECAP) == 0
1788 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001789}
1790
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001791/*
1792 * Return TRUE if spell checking is not enabled.
1793 */
1794 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00001795no_spell_checking(wp)
1796 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001797{
Bram Moolenaar95529562005-08-25 21:21:38 +00001798 if (!wp->w_p_spell || *wp->w_buffer->b_p_spl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001799 {
1800 EMSG(_("E756: Spell checking is not enabled"));
1801 return TRUE;
1802 }
1803 return FALSE;
1804}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001805
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001806/*
1807 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001808 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
1809 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001810 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1811 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001812 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001813 */
1814 int
Bram Moolenaar95529562005-08-25 21:21:38 +00001815spell_move_to(wp, dir, allwords, curline, attrp)
1816 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001817 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001818 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001819 int curline;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001820 hlf_T *attrp; /* return: attributes of bad word or NULL
1821 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001822{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001823 linenr_T lnum;
1824 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001825 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001826 char_u *line;
1827 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001828 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001829 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001830 int len;
Bram Moolenaar95529562005-08-25 21:21:38 +00001831 int has_syntax = syntax_present(wp->w_buffer);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001832 int col;
1833 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001834 char_u *buf = NULL;
1835 int buflen = 0;
1836 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001837 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001838 int found_one = FALSE;
1839 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001840
Bram Moolenaar95529562005-08-25 21:21:38 +00001841 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00001842 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001843
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001844 /*
1845 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001846 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001847 *
1848 * When searching backwards, we continue in the line to find the last
1849 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001850 *
1851 * We concatenate the start of the next line, so that wrapped words work
1852 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1853 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001854 */
Bram Moolenaar95529562005-08-25 21:21:38 +00001855 lnum = wp->w_cursor.lnum;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001856 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001857
1858 while (!got_int)
1859 {
Bram Moolenaar95529562005-08-25 21:21:38 +00001860 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001861
Bram Moolenaar0c405862005-06-22 22:26:26 +00001862 len = STRLEN(line);
1863 if (buflen < len + MAXWLEN + 2)
1864 {
1865 vim_free(buf);
1866 buflen = len + MAXWLEN + 2;
1867 buf = alloc(buflen);
1868 if (buf == NULL)
1869 break;
1870 }
1871
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001872 /* In first line check first word for Capital. */
1873 if (lnum == 1)
1874 capcol = 0;
1875
1876 /* For checking first word with a capital skip white space. */
1877 if (capcol == 0)
1878 capcol = skipwhite(line) - line;
1879
Bram Moolenaar0c405862005-06-22 22:26:26 +00001880 /* Copy the line into "buf" and append the start of the next line if
1881 * possible. */
1882 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00001883 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001884 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1885
1886 p = buf + skip;
1887 endp = buf + len;
1888 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001889 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001890 /* When searching backward don't search after the cursor. Unless
1891 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001892 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00001893 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001894 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00001895 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001896 break;
1897
1898 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001899 attr = HLF_COUNT;
Bram Moolenaar95529562005-08-25 21:21:38 +00001900 len = spell_check(wp, p, &attr, &capcol);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001901
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001902 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001903 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001904 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001905 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001906 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001907 found_one = TRUE;
1908
Bram Moolenaar51485f02005-06-04 21:55:20 +00001909 /* When searching forward only accept a bad word after
1910 * the cursor. */
1911 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001912 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00001913 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001914 && (wrapped
1915 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00001916 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001917 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001918 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001919 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001920 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001921 col = p - buf;
Bram Moolenaar95529562005-08-25 21:21:38 +00001922 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar51485f02005-06-04 21:55:20 +00001923 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001924 }
1925 else
1926 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001927
Bram Moolenaar51485f02005-06-04 21:55:20 +00001928 if (can_spell)
1929 {
1930 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001931 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001932#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001933 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001934#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001935 if (dir == FORWARD)
1936 {
1937 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00001938 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001939 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001940 if (attrp != NULL)
1941 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001942 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001943 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001944 else if (curline)
1945 /* Insert mode completion: put cursor after
1946 * the bad word. */
1947 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001948 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001949 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001950 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001951 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001952 }
1953
Bram Moolenaar51485f02005-06-04 21:55:20 +00001954 /* advance to character after the word */
1955 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001956 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001957 }
1958
Bram Moolenaar5195e452005-08-19 20:32:47 +00001959 if (dir == BACKWARD && found_pos.lnum != 0)
1960 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001961 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00001962 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001963 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001964 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001965 }
1966
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001967 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001968 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001969
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001970 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001971 if (dir == BACKWARD)
1972 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001973 /* If we are back at the starting line and searched it again there
1974 * is no match, give up. */
1975 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001976 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001977
1978 if (lnum > 1)
1979 --lnum;
1980 else if (!p_ws)
1981 break; /* at first line and 'nowrapscan' */
1982 else
1983 {
1984 /* Wrap around to the end of the buffer. May search the
1985 * starting line again and accept the last match. */
1986 lnum = wp->w_buffer->b_ml.ml_line_count;
1987 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00001988 if (!shortmess(SHM_SEARCH))
1989 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001990 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001991 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001992 }
1993 else
1994 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001995 if (lnum < wp->w_buffer->b_ml.ml_line_count)
1996 ++lnum;
1997 else if (!p_ws)
1998 break; /* at first line and 'nowrapscan' */
1999 else
2000 {
2001 /* Wrap around to the start of the buffer. May search the
2002 * starting line again and accept the first match. */
2003 lnum = 1;
2004 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002005 if (!shortmess(SHM_SEARCH))
2006 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002007 }
2008
2009 /* If we are back at the starting line and there is no match then
2010 * give up. */
2011 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002012 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002013
2014 /* Skip the characters at the start of the next line that were
2015 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002016 if (attr == HLF_COUNT)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002017 skip = p - endp;
2018 else
2019 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002020
2021 /* Capscol skips over the inserted space. */
2022 --capcol;
2023
2024 /* But after empty line check first word in next line */
2025 if (*skipwhite(line) == NUL)
2026 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002027 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002028
2029 line_breakcheck();
2030 }
2031
Bram Moolenaar0c405862005-06-22 22:26:26 +00002032 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002033 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002034}
2035
2036/*
2037 * For spell checking: concatenate the start of the following line "line" into
2038 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
2039 */
2040 void
2041spell_cat_line(buf, line, maxlen)
2042 char_u *buf;
2043 char_u *line;
2044 int maxlen;
2045{
2046 char_u *p;
2047 int n;
2048
2049 p = skipwhite(line);
2050 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2051 p = skipwhite(p + 1);
2052
2053 if (*p != NUL)
2054 {
2055 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002056 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002057 n = p - line;
2058 if (n >= maxlen)
2059 n = maxlen - 1;
2060 vim_memset(buf + 1, ' ', n);
2061 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002062}
2063
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002064typedef struct spelload_S
2065{
2066 char_u sl_lang[MAXWLEN + 1]; /* language name */
2067 slang_T *sl_slang; /* resulting slang_T struct */
2068 int sl_nobreak; /* NOBREAK language found */
2069} spelload_T;
2070
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002071/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002072 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002073 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002074 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002075 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002076spell_load_lang(lang)
2077 char_u *lang;
2078{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002079 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002080 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002081 spelload_T sl;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002082
Bram Moolenaarb765d632005-06-07 21:00:02 +00002083 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002084 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002085 STRCPY(sl.sl_lang, lang);
2086 sl.sl_slang = NULL;
2087 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002088
Bram Moolenaarb765d632005-06-07 21:00:02 +00002089 /*
2090 * Find the first spell file for "lang" in 'runtimepath' and load it.
2091 */
2092 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2093 "spell/%s.%s.spl", lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002094 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002095
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002096 if (r == FAIL && *sl.sl_lang != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002097 {
2098 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002099 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002100 "spell/%s.ascii.spl", lang);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002101 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002102 }
2103
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002104 if (r == FAIL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002105 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2106 lang, spell_enc(), lang);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002107 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002108 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002109 /* At least one file was loaded, now load all the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002110 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002111 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002112 }
2113}
2114
2115/*
2116 * Return the encoding used for spell checking: Use 'encoding', except that we
2117 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2118 */
2119 static char_u *
2120spell_enc()
2121{
2122
2123#ifdef FEAT_MBYTE
2124 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2125 return p_enc;
2126#endif
2127 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002128}
2129
2130/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002131 * Get the name of the .spl file for the internal wordlist into
2132 * "fname[MAXPATHL]".
2133 */
2134 static void
2135int_wordlist_spl(fname)
2136 char_u *fname;
2137{
2138 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2139 int_wordlist, spell_enc());
2140}
2141
2142/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002143 * Allocate a new slang_T.
2144 * Caller must fill "sl_next".
2145 */
2146 static slang_T *
2147slang_alloc(lang)
2148 char_u *lang;
2149{
2150 slang_T *lp;
2151
Bram Moolenaar51485f02005-06-04 21:55:20 +00002152 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002153 if (lp != NULL)
2154 {
2155 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002156 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002157 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002158 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002159 }
2160 return lp;
2161}
2162
2163/*
2164 * Free the contents of an slang_T and the structure itself.
2165 */
2166 static void
2167slang_free(lp)
2168 slang_T *lp;
2169{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002170 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002171 vim_free(lp->sl_fname);
2172 slang_clear(lp);
2173 vim_free(lp);
2174}
2175
2176/*
2177 * Clear an slang_T so that the file can be reloaded.
2178 */
2179 static void
2180slang_clear(lp)
2181 slang_T *lp;
2182{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002183 garray_T *gap;
2184 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002185 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002186 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002187
Bram Moolenaar51485f02005-06-04 21:55:20 +00002188 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002189 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002190 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002191 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002192 vim_free(lp->sl_pbyts);
2193 lp->sl_pbyts = NULL;
2194
Bram Moolenaar51485f02005-06-04 21:55:20 +00002195 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002196 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002197 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002198 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002199 vim_free(lp->sl_pidxs);
2200 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002201
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002202 gap = &lp->sl_rep;
2203 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002204 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002205 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2206 vim_free(ftp->ft_from);
2207 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002208 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002209 ga_clear(gap);
2210
2211 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002212 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002213 {
2214 /* "ga_len" is set to 1 without adding an item for latin1 */
2215 if (gap->ga_data != NULL)
2216 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2217 for (i = 0; i < gap->ga_len; ++i)
2218 vim_free(((int **)gap->ga_data)[i]);
2219 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002220 else
2221 /* SAL items: free salitem_T items */
2222 while (gap->ga_len > 0)
2223 {
2224 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2225 vim_free(smp->sm_lead);
2226 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2227 vim_free(smp->sm_to);
2228#ifdef FEAT_MBYTE
2229 vim_free(smp->sm_lead_w);
2230 vim_free(smp->sm_oneof_w);
2231 vim_free(smp->sm_to_w);
2232#endif
2233 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002234 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002235
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002236 for (i = 0; i < lp->sl_prefixcnt; ++i)
2237 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002238 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002239 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002240 lp->sl_prefprog = NULL;
2241
2242 vim_free(lp->sl_midword);
2243 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002244
Bram Moolenaar5195e452005-08-19 20:32:47 +00002245 vim_free(lp->sl_compprog);
2246 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002247 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002248 lp->sl_compprog = NULL;
2249 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002250 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002251
2252 vim_free(lp->sl_syllable);
2253 lp->sl_syllable = NULL;
2254 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002255
Bram Moolenaarea424162005-06-16 21:51:00 +00002256#ifdef FEAT_MBYTE
2257 {
2258 int todo = lp->sl_map_hash.ht_used;
2259 hashitem_T *hi;
2260
2261 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
2262 if (!HASHITEM_EMPTY(hi))
2263 {
2264 --todo;
2265 vim_free(hi->hi_key);
2266 }
2267 }
2268 hash_clear(&lp->sl_map_hash);
2269#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002270
2271 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002272 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002273 lp->sl_compsylmax = MAXWLEN;
2274 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002275}
2276
2277/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002278 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002279 * Invoked through do_in_runtimepath().
2280 */
2281 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002282spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002283 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002284 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002285{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002286 spelload_T *slp = (spelload_T *)cookie;
2287 slang_T *slang;
2288
2289 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2290 if (slang != NULL)
2291 {
2292 /* When a previously loaded file has NOBREAK also use it for the
2293 * ".add" files. */
2294 if (slp->sl_nobreak && slang->sl_add)
2295 slang->sl_nobreak = TRUE;
2296 else if (slang->sl_nobreak)
2297 slp->sl_nobreak = TRUE;
2298
2299 slp->sl_slang = slang;
2300 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002301}
2302
2303/*
2304 * Load one spell file and store the info into a slang_T.
2305 *
2306 * This is invoked in two ways:
2307 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2308 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2309 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2310 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002311 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002312 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002313 static slang_T *
2314spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002315 char_u *fname;
2316 char_u *lang;
2317 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002318 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002319{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002320 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002321 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002322 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002323 char_u *bp;
2324 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002325 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002326 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002327 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002328 int round;
2329 char_u *save_sourcing_name = sourcing_name;
2330 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002331 slang_T *lp = NULL;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002332 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002333 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002334 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002335
Bram Moolenaarb765d632005-06-07 21:00:02 +00002336 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002337 if (fd == NULL)
2338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002339 if (!silent)
2340 EMSG2(_(e_notopen), fname);
2341 else if (p_verbose > 2)
2342 {
2343 verbose_enter();
2344 smsg((char_u *)e_notopen, fname);
2345 verbose_leave();
2346 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002347 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002348 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002349 if (p_verbose > 2)
2350 {
2351 verbose_enter();
2352 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2353 verbose_leave();
2354 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002355
Bram Moolenaarb765d632005-06-07 21:00:02 +00002356 if (old_lp == NULL)
2357 {
2358 lp = slang_alloc(lang);
2359 if (lp == NULL)
2360 goto endFAIL;
2361
2362 /* Remember the file name, used to reload the file when it's updated. */
2363 lp->sl_fname = vim_strsave(fname);
2364 if (lp->sl_fname == NULL)
2365 goto endFAIL;
2366
2367 /* Check for .add.spl. */
2368 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2369 }
2370 else
2371 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002372
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002373 /* Set sourcing_name, so that error messages mention the file name. */
2374 sourcing_name = fname;
2375 sourcing_lnum = 0;
2376
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002377 /* <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002378 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002379 for (i = 0; i < VIMSPELLMAGICL; ++i)
2380 buf[i] = getc(fd); /* <fileID> */
2381 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2382 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002383 EMSG(_("E757: This does not look like a spell file"));
2384 goto endFAIL;
2385 }
2386 c = getc(fd); /* <versionnr> */
2387 if (c < VIMSPELLVERSION)
2388 {
2389 EMSG(_("E771: Old spell file, needs to be updated"));
2390 goto endFAIL;
2391 }
2392 else if (c > VIMSPELLVERSION)
2393 {
2394 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002395 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002396 }
2397
Bram Moolenaar5195e452005-08-19 20:32:47 +00002398
2399 /*
2400 * <SECTIONS>: <section> ... <sectionend>
2401 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2402 */
2403 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002404 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002405 n = getc(fd); /* <sectionID> or <sectionend> */
2406 if (n == SN_END)
2407 break;
2408 c = getc(fd); /* <sectionflags> */
2409 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2410 /* <sectionlen> */
2411 if (len < 0)
2412 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002413
Bram Moolenaar5195e452005-08-19 20:32:47 +00002414 res = 0;
2415 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002416 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002417 case SN_REGION:
2418 res = read_region_section(fd, lp, len);
2419 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002420
Bram Moolenaar5195e452005-08-19 20:32:47 +00002421 case SN_CHARFLAGS:
2422 res = read_charflags_section(fd);
2423 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002424
Bram Moolenaar5195e452005-08-19 20:32:47 +00002425 case SN_MIDWORD:
2426 lp->sl_midword = read_string(fd, len); /* <midword> */
2427 if (lp->sl_midword == NULL)
2428 goto endFAIL;
2429 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002430
Bram Moolenaar5195e452005-08-19 20:32:47 +00002431 case SN_PREFCOND:
2432 res = read_prefcond_section(fd, lp);
2433 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002434
Bram Moolenaar5195e452005-08-19 20:32:47 +00002435 case SN_REP:
2436 res = read_rep_section(fd, lp);
2437 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002438
Bram Moolenaar5195e452005-08-19 20:32:47 +00002439 case SN_SAL:
2440 res = read_sal_section(fd, lp);
2441 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002442
Bram Moolenaar5195e452005-08-19 20:32:47 +00002443 case SN_SOFO:
2444 res = read_sofo_section(fd, lp);
2445 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002446
Bram Moolenaar5195e452005-08-19 20:32:47 +00002447 case SN_MAP:
2448 p = read_string(fd, len); /* <mapstr> */
2449 if (p == NULL)
2450 goto endFAIL;
2451 set_map_str(lp, p);
2452 vim_free(p);
2453 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002454
Bram Moolenaar5195e452005-08-19 20:32:47 +00002455 case SN_COMPOUND:
2456 res = read_compound(fd, lp, len);
2457 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002458
Bram Moolenaar78622822005-08-23 21:00:13 +00002459 case SN_NOBREAK:
2460 lp->sl_nobreak = TRUE;
2461 break;
2462
Bram Moolenaar5195e452005-08-19 20:32:47 +00002463 case SN_SYLLABLE:
2464 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2465 if (lp->sl_syllable == NULL)
2466 goto endFAIL;
2467 if (init_syl_tab(lp) == FAIL)
2468 goto endFAIL;
2469 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002470
Bram Moolenaar5195e452005-08-19 20:32:47 +00002471 default:
2472 /* Unsupported section. When it's required give an error
2473 * message. When it's not required skip the contents. */
2474 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002475 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002476 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002477 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002478 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002479 while (--len >= 0)
2480 if (getc(fd) < 0)
2481 goto truncerr;
2482 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002483 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002484 if (res == SP_FORMERROR)
2485 {
2486formerr:
2487 EMSG(_(e_format));
2488 goto endFAIL;
2489 }
2490 if (res == SP_TRUNCERROR)
2491 {
2492truncerr:
2493 EMSG(_(e_spell_trunc));
2494 goto endFAIL;
2495 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002496 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002497 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002498 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002499
Bram Moolenaar51485f02005-06-04 21:55:20 +00002500 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002501 * round 2: <KWORDTREE>
2502 * round 3: <PREFIXTREE> */
2503 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002504 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002505 /* The tree size was computed when writing the file, so that we can
2506 * allocate it as one long block. <nodecount> */
2507 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2508 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002509 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002510 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002511 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002512 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002513 bp = lalloc((long_u)len, TRUE);
2514 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002515 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002516 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002517 lp->sl_fbyts = bp;
2518 else if (round == 2)
2519 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002520 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002521 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002522
2523 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002524 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2525 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002526 goto endFAIL;
2527 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002528 lp->sl_fidxs = ip;
2529 else if (round == 2)
2530 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002531 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002532 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002533
2534 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002535 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002536 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002537 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002538 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002539 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002540 }
2541 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002542
Bram Moolenaarb765d632005-06-07 21:00:02 +00002543 /* For a new file link it in the list of spell files. */
2544 if (old_lp == NULL)
2545 {
2546 lp->sl_next = first_lang;
2547 first_lang = lp;
2548 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002549
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002550 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002551
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002552endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002553 if (lang != NULL)
2554 /* truncating the name signals the error to spell_load_lang() */
2555 *lang = NUL;
2556 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002557 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002558 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002559
2560endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002561 if (fd != NULL)
2562 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002563 sourcing_name = save_sourcing_name;
2564 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002565
2566 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002567}
2568
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002569/*
2570 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002571 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002572 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002573 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2574 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002575 */
2576 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002577read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002578 FILE *fd;
2579 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002580 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002581{
2582 int cnt = 0;
2583 int i;
2584 char_u *str;
2585
2586 /* read the length bytes, MSB first */
2587 for (i = 0; i < cnt_bytes; ++i)
2588 cnt = (cnt << 8) + getc(fd);
2589 if (cnt < 0)
2590 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002591 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002592 return NULL;
2593 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002594 *cntp = cnt;
2595 if (cnt == 0)
2596 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002597
Bram Moolenaar5195e452005-08-19 20:32:47 +00002598 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002599 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002600 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002601 return str;
2602}
2603
Bram Moolenaar7887d882005-07-01 22:33:52 +00002604/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002605 * Read a string of length "cnt" from "fd" into allocated memory.
2606 * Returns NULL when out of memory.
2607 */
2608 static char_u *
2609read_string(fd, cnt)
2610 FILE *fd;
2611 int cnt;
2612{
2613 char_u *str;
2614 int i;
2615
2616 /* allocate memory */
2617 str = alloc((unsigned)cnt + 1);
2618 if (str != NULL)
2619 {
2620 /* Read the string. Doesn't check for truncated file. */
2621 for (i = 0; i < cnt; ++i)
2622 str[i] = getc(fd);
2623 str[i] = NUL;
2624 }
2625 return str;
2626}
2627
2628/*
2629 * Read SN_REGION: <regionname> ...
2630 * Return SP_*ERROR flags.
2631 */
2632 static int
2633read_region_section(fd, lp, len)
2634 FILE *fd;
2635 slang_T *lp;
2636 int len;
2637{
2638 int i;
2639
2640 if (len > 16)
2641 return SP_FORMERROR;
2642 for (i = 0; i < len; ++i)
2643 lp->sl_regions[i] = getc(fd); /* <regionname> */
2644 lp->sl_regions[len] = NUL;
2645 return 0;
2646}
2647
2648/*
2649 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2650 * <folcharslen> <folchars>
2651 * Return SP_*ERROR flags.
2652 */
2653 static int
2654read_charflags_section(fd)
2655 FILE *fd;
2656{
2657 char_u *flags;
2658 char_u *fol;
2659 int flagslen, follen;
2660
2661 /* <charflagslen> <charflags> */
2662 flags = read_cnt_string(fd, 1, &flagslen);
2663 if (flagslen < 0)
2664 return flagslen;
2665
2666 /* <folcharslen> <folchars> */
2667 fol = read_cnt_string(fd, 2, &follen);
2668 if (follen < 0)
2669 {
2670 vim_free(flags);
2671 return follen;
2672 }
2673
2674 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
2675 if (flags != NULL && fol != NULL)
2676 set_spell_charflags(flags, flagslen, fol);
2677
2678 vim_free(flags);
2679 vim_free(fol);
2680
2681 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
2682 if ((flags == NULL) != (fol == NULL))
2683 return SP_FORMERROR;
2684 return 0;
2685}
2686
2687/*
2688 * Read SN_PREFCOND section.
2689 * Return SP_*ERROR flags.
2690 */
2691 static int
2692read_prefcond_section(fd, lp)
2693 FILE *fd;
2694 slang_T *lp;
2695{
2696 int cnt;
2697 int i;
2698 int n;
2699 char_u *p;
2700 char_u buf[MAXWLEN + 1];
2701
2702 /* <prefcondcnt> <prefcond> ... */
2703 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
2704 if (cnt <= 0)
2705 return SP_FORMERROR;
2706
2707 lp->sl_prefprog = (regprog_T **)alloc_clear(
2708 (unsigned)sizeof(regprog_T *) * cnt);
2709 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002710 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002711 lp->sl_prefixcnt = cnt;
2712
2713 for (i = 0; i < cnt; ++i)
2714 {
2715 /* <prefcond> : <condlen> <condstr> */
2716 n = getc(fd); /* <condlen> */
2717 if (n < 0 || n >= MAXWLEN)
2718 return SP_FORMERROR;
2719
2720 /* When <condlen> is zero we have an empty condition. Otherwise
2721 * compile the regexp program used to check for the condition. */
2722 if (n > 0)
2723 {
2724 buf[0] = '^'; /* always match at one position only */
2725 p = buf + 1;
2726 while (n-- > 0)
2727 *p++ = getc(fd); /* <condstr> */
2728 *p = NUL;
2729 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
2730 }
2731 }
2732 return 0;
2733}
2734
2735/*
2736 * Read REP items section from "fd": <repcount> <rep> ...
2737 * Return SP_*ERROR flags.
2738 */
2739 static int
2740read_rep_section(fd, slang)
2741 FILE *fd;
2742 slang_T *slang;
2743{
2744 int cnt;
2745 garray_T *gap;
2746 fromto_T *ftp;
2747 short *first;
2748 int i;
2749
2750 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
2751 if (cnt < 0)
2752 return SP_TRUNCERROR;
2753
2754 gap = &slang->sl_rep;
2755 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002756 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002757
2758 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
2759 for (; gap->ga_len < cnt; ++gap->ga_len)
2760 {
2761 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
2762 ftp->ft_from = read_cnt_string(fd, 1, &i);
2763 if (i < 0)
2764 return i;
2765 if (i == 0)
2766 return SP_FORMERROR;
2767 ftp->ft_to = read_cnt_string(fd, 1, &i);
2768 if (i <= 0)
2769 {
2770 vim_free(ftp->ft_from);
2771 if (i < 0)
2772 return i;
2773 return SP_FORMERROR;
2774 }
2775 }
2776
2777 /* Fill the first-index table. */
2778 first = slang->sl_rep_first;
2779 for (i = 0; i < 256; ++i)
2780 first[i] = -1;
2781 for (i = 0; i < gap->ga_len; ++i)
2782 {
2783 ftp = &((fromto_T *)gap->ga_data)[i];
2784 if (first[*ftp->ft_from] == -1)
2785 first[*ftp->ft_from] = i;
2786 }
2787 return 0;
2788}
2789
2790/*
2791 * Read SN_SAL section: <salflags> <salcount> <sal> ...
2792 * Return SP_*ERROR flags.
2793 */
2794 static int
2795read_sal_section(fd, slang)
2796 FILE *fd;
2797 slang_T *slang;
2798{
2799 int i;
2800 int cnt;
2801 garray_T *gap;
2802 salitem_T *smp;
2803 int ccnt;
2804 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002805 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002806
2807 slang->sl_sofo = FALSE;
2808
2809 i = getc(fd); /* <salflags> */
2810 if (i & SAL_F0LLOWUP)
2811 slang->sl_followup = TRUE;
2812 if (i & SAL_COLLAPSE)
2813 slang->sl_collapse = TRUE;
2814 if (i & SAL_REM_ACCENTS)
2815 slang->sl_rem_accents = TRUE;
2816
2817 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
2818 if (cnt < 0)
2819 return SP_TRUNCERROR;
2820
2821 gap = &slang->sl_sal;
2822 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002823 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002824 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002825
2826 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
2827 for (; gap->ga_len < cnt; ++gap->ga_len)
2828 {
2829 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2830 ccnt = getc(fd); /* <salfromlen> */
2831 if (ccnt < 0)
2832 return SP_TRUNCERROR;
2833 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002834 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002835 smp->sm_lead = p;
2836
2837 /* Read up to the first special char into sm_lead. */
2838 for (i = 0; i < ccnt; ++i)
2839 {
2840 c = getc(fd); /* <salfrom> */
2841 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
2842 break;
2843 *p++ = c;
2844 }
2845 smp->sm_leadlen = p - smp->sm_lead;
2846 *p++ = NUL;
2847
2848 /* Put (abc) chars in sm_oneof, if any. */
2849 if (c == '(')
2850 {
2851 smp->sm_oneof = p;
2852 for (++i; i < ccnt; ++i)
2853 {
2854 c = getc(fd); /* <salfrom> */
2855 if (c == ')')
2856 break;
2857 *p++ = c;
2858 }
2859 *p++ = NUL;
2860 if (++i < ccnt)
2861 c = getc(fd);
2862 }
2863 else
2864 smp->sm_oneof = NULL;
2865
2866 /* Any following chars go in sm_rules. */
2867 smp->sm_rules = p;
2868 if (i < ccnt)
2869 /* store the char we got while checking for end of sm_lead */
2870 *p++ = c;
2871 for (++i; i < ccnt; ++i)
2872 *p++ = getc(fd); /* <salfrom> */
2873 *p++ = NUL;
2874
2875 /* <saltolen> <salto> */
2876 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
2877 if (ccnt < 0)
2878 {
2879 vim_free(smp->sm_lead);
2880 return ccnt;
2881 }
2882
2883#ifdef FEAT_MBYTE
2884 if (has_mbyte)
2885 {
2886 /* convert the multi-byte strings to wide char strings */
2887 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2888 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2889 if (smp->sm_oneof == NULL)
2890 smp->sm_oneof_w = NULL;
2891 else
2892 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
2893 if (smp->sm_to == NULL)
2894 smp->sm_to_w = NULL;
2895 else
2896 smp->sm_to_w = mb_str2wide(smp->sm_to);
2897 if (smp->sm_lead_w == NULL
2898 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
2899 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
2900 {
2901 vim_free(smp->sm_lead);
2902 vim_free(smp->sm_to);
2903 vim_free(smp->sm_lead_w);
2904 vim_free(smp->sm_oneof_w);
2905 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002906 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002907 }
2908 }
2909#endif
2910 }
2911
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00002912 if (gap->ga_len > 0)
2913 {
2914 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
2915 * that we need to check the index every time. */
2916 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2917 if ((p = alloc(1)) == NULL)
2918 return SP_OTHERERROR;
2919 p[0] = NUL;
2920 smp->sm_lead = p;
2921 smp->sm_leadlen = 0;
2922 smp->sm_oneof = NULL;
2923 smp->sm_rules = p;
2924 smp->sm_to = NULL;
2925#ifdef FEAT_MBYTE
2926 if (has_mbyte)
2927 {
2928 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2929 smp->sm_leadlen = 0;
2930 smp->sm_oneof_w = NULL;
2931 smp->sm_to_w = NULL;
2932 }
2933#endif
2934 ++gap->ga_len;
2935 }
2936
Bram Moolenaar5195e452005-08-19 20:32:47 +00002937 /* Fill the first-index table. */
2938 set_sal_first(slang);
2939
2940 return 0;
2941}
2942
2943/*
2944 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
2945 * Return SP_*ERROR flags.
2946 */
2947 static int
2948read_sofo_section(fd, slang)
2949 FILE *fd;
2950 slang_T *slang;
2951{
2952 int cnt;
2953 char_u *from, *to;
2954 int res;
2955
2956 slang->sl_sofo = TRUE;
2957
2958 /* <sofofromlen> <sofofrom> */
2959 from = read_cnt_string(fd, 2, &cnt);
2960 if (cnt < 0)
2961 return cnt;
2962
2963 /* <sofotolen> <sofoto> */
2964 to = read_cnt_string(fd, 2, &cnt);
2965 if (cnt < 0)
2966 {
2967 vim_free(from);
2968 return cnt;
2969 }
2970
2971 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
2972 if (from != NULL && to != NULL)
2973 res = set_sofo(slang, from, to);
2974 else if (from != NULL || to != NULL)
2975 res = SP_FORMERROR; /* only one of two strings is an error */
2976 else
2977 res = 0;
2978
2979 vim_free(from);
2980 vim_free(to);
2981 return res;
2982}
2983
2984/*
2985 * Read the compound section from the .spl file:
2986 * <compmax> <compminlen> <compsylmax> <compflags>
2987 * Returns SP_*ERROR flags.
2988 */
2989 static int
2990read_compound(fd, slang, len)
2991 FILE *fd;
2992 slang_T *slang;
2993 int len;
2994{
2995 int todo = len;
2996 int c;
2997 int atstart;
2998 char_u *pat;
2999 char_u *pp;
3000 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003001 char_u *ap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003002
3003 if (todo < 2)
3004 return SP_FORMERROR; /* need at least two bytes */
3005
3006 --todo;
3007 c = getc(fd); /* <compmax> */
3008 if (c < 2)
3009 c = MAXWLEN;
3010 slang->sl_compmax = c;
3011
3012 --todo;
3013 c = getc(fd); /* <compminlen> */
3014 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003015 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003016 slang->sl_compminlen = c;
3017
3018 --todo;
3019 c = getc(fd); /* <compsylmax> */
3020 if (c < 1)
3021 c = MAXWLEN;
3022 slang->sl_compsylmax = c;
3023
3024 /* Turn the COMPOUNDFLAGS items into a regexp pattern:
3025 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003026 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3027 * Conversion to utf-8 may double the size. */
3028 c = todo * 2 + 7;
3029#ifdef FEAT_MBYTE
3030 if (enc_utf8)
3031 c += todo * 2;
3032#endif
3033 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003034 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003035 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003036
Bram Moolenaard12a1322005-08-21 22:08:24 +00003037 /* We also need a list of all flags that can appear at the start and one
3038 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003039 cp = alloc(todo + 1);
3040 if (cp == NULL)
3041 {
3042 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003043 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003044 }
3045 slang->sl_compstartflags = cp;
3046 *cp = NUL;
3047
Bram Moolenaard12a1322005-08-21 22:08:24 +00003048 ap = alloc(todo + 1);
3049 if (ap == NULL)
3050 {
3051 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003052 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003053 }
3054 slang->sl_compallflags = ap;
3055 *ap = NUL;
3056
Bram Moolenaar5195e452005-08-19 20:32:47 +00003057 pp = pat;
3058 *pp++ = '^';
3059 *pp++ = '\\';
3060 *pp++ = '(';
3061
3062 atstart = 1;
3063 while (todo-- > 0)
3064 {
3065 c = getc(fd); /* <compflags> */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003066
3067 /* Add all flags to "sl_compallflags". */
3068 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003069 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003070 {
3071 *ap++ = c;
3072 *ap = NUL;
3073 }
3074
Bram Moolenaar5195e452005-08-19 20:32:47 +00003075 if (atstart != 0)
3076 {
3077 /* At start of item: copy flags to "sl_compstartflags". For a
3078 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3079 if (c == '[')
3080 atstart = 2;
3081 else if (c == ']')
3082 atstart = 0;
3083 else
3084 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003085 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003086 {
3087 *cp++ = c;
3088 *cp = NUL;
3089 }
3090 if (atstart == 1)
3091 atstart = 0;
3092 }
3093 }
3094 if (c == '/') /* slash separates two items */
3095 {
3096 *pp++ = '\\';
3097 *pp++ = '|';
3098 atstart = 1;
3099 }
3100 else /* normal char, "[abc]" and '*' are copied as-is */
3101 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003102 if (c == '+' || c == '~')
Bram Moolenaar5195e452005-08-19 20:32:47 +00003103 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003104#ifdef FEAT_MBYTE
3105 if (enc_utf8)
3106 pp += mb_char2bytes(c, pp);
3107 else
3108#endif
3109 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003110 }
3111 }
3112
3113 *pp++ = '\\';
3114 *pp++ = ')';
3115 *pp++ = '$';
3116 *pp = NUL;
3117
3118 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3119 vim_free(pat);
3120 if (slang->sl_compprog == NULL)
3121 return SP_FORMERROR;
3122
3123 return 0;
3124}
3125
Bram Moolenaar6de68532005-08-24 22:08:48 +00003126/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003127 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003128 * Like strchr() but independent of locale.
3129 */
3130 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003131byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003132 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003133 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003134{
3135 char_u *p;
3136
3137 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003138 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003139 return TRUE;
3140 return FALSE;
3141}
3142
Bram Moolenaar5195e452005-08-19 20:32:47 +00003143#define SY_MAXLEN 30
3144typedef struct syl_item_S
3145{
3146 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3147 int sy_len;
3148} syl_item_T;
3149
3150/*
3151 * Truncate "slang->sl_syllable" at the first slash and put the following items
3152 * in "slang->sl_syl_items".
3153 */
3154 static int
3155init_syl_tab(slang)
3156 slang_T *slang;
3157{
3158 char_u *p;
3159 char_u *s;
3160 int l;
3161 syl_item_T *syl;
3162
3163 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3164 p = vim_strchr(slang->sl_syllable, '/');
3165 while (p != NULL)
3166 {
3167 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003168 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003169 break;
3170 s = p;
3171 p = vim_strchr(p, '/');
3172 if (p == NULL)
3173 l = STRLEN(s);
3174 else
3175 l = p - s;
3176 if (l >= SY_MAXLEN)
3177 return SP_FORMERROR;
3178 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003179 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003180 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3181 + slang->sl_syl_items.ga_len++;
3182 vim_strncpy(syl->sy_chars, s, l);
3183 syl->sy_len = l;
3184 }
3185 return OK;
3186}
3187
3188/*
3189 * Count the number of syllables in "word".
3190 * When "word" contains spaces the syllables after the last space are counted.
3191 * Returns zero if syllables are not defines.
3192 */
3193 static int
3194count_syllables(slang, word)
3195 slang_T *slang;
3196 char_u *word;
3197{
3198 int cnt = 0;
3199 int skip = FALSE;
3200 char_u *p;
3201 int len;
3202 int i;
3203 syl_item_T *syl;
3204 int c;
3205
3206 if (slang->sl_syllable == NULL)
3207 return 0;
3208
3209 for (p = word; *p != NUL; p += len)
3210 {
3211 /* When running into a space reset counter. */
3212 if (*p == ' ')
3213 {
3214 len = 1;
3215 cnt = 0;
3216 continue;
3217 }
3218
3219 /* Find longest match of syllable items. */
3220 len = 0;
3221 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3222 {
3223 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3224 if (syl->sy_len > len
3225 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3226 len = syl->sy_len;
3227 }
3228 if (len != 0) /* found a match, count syllable */
3229 {
3230 ++cnt;
3231 skip = FALSE;
3232 }
3233 else
3234 {
3235 /* No recognized syllable item, at least a syllable char then? */
3236#ifdef FEAT_MBYTE
3237 c = mb_ptr2char(p);
3238 len = (*mb_ptr2len)(p);
3239#else
3240 c = *p;
3241 len = 1;
3242#endif
3243 if (vim_strchr(slang->sl_syllable, c) == NULL)
3244 skip = FALSE; /* No, search for next syllable */
3245 else if (!skip)
3246 {
3247 ++cnt; /* Yes, count it */
3248 skip = TRUE; /* don't count following syllable chars */
3249 }
3250 }
3251 }
3252 return cnt;
3253}
3254
3255/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003256 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003257 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003258 */
3259 static int
3260set_sofo(lp, from, to)
3261 slang_T *lp;
3262 char_u *from;
3263 char_u *to;
3264{
3265 int i;
3266
3267#ifdef FEAT_MBYTE
3268 garray_T *gap;
3269 char_u *s;
3270 char_u *p;
3271 int c;
3272 int *inp;
3273
3274 if (has_mbyte)
3275 {
3276 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3277 * characters. The index is the low byte of the character.
3278 * The list contains from-to pairs with a terminating NUL.
3279 * sl_sal_first[] is used for latin1 "from" characters. */
3280 gap = &lp->sl_sal;
3281 ga_init2(gap, sizeof(int *), 1);
3282 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003283 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003284 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3285 gap->ga_len = 256;
3286
3287 /* First count the number of items for each list. Temporarily use
3288 * sl_sal_first[] for this. */
3289 for (p = from, s = to; *p != NUL && *s != NUL; )
3290 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003291 c = mb_cptr2char_adv(&p);
3292 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003293 if (c >= 256)
3294 ++lp->sl_sal_first[c & 0xff];
3295 }
3296 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003297 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003298
3299 /* Allocate the lists. */
3300 for (i = 0; i < 256; ++i)
3301 if (lp->sl_sal_first[i] > 0)
3302 {
3303 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3304 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003305 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003306 ((int **)gap->ga_data)[i] = (int *)p;
3307 *(int *)p = 0;
3308 }
3309
3310 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3311 * list. */
3312 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3313 for (p = from, s = to; *p != NUL && *s != NUL; )
3314 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003315 c = mb_cptr2char_adv(&p);
3316 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003317 if (c >= 256)
3318 {
3319 /* Append the from-to chars at the end of the list with
3320 * the low byte. */
3321 inp = ((int **)gap->ga_data)[c & 0xff];
3322 while (*inp != 0)
3323 ++inp;
3324 *inp++ = c; /* from char */
3325 *inp++ = i; /* to char */
3326 *inp++ = NUL; /* NUL at the end */
3327 }
3328 else
3329 /* mapping byte to char is done in sl_sal_first[] */
3330 lp->sl_sal_first[c] = i;
3331 }
3332 }
3333 else
3334#endif
3335 {
3336 /* mapping bytes to bytes is done in sl_sal_first[] */
3337 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003338 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003339
3340 for (i = 0; to[i] != NUL; ++i)
3341 lp->sl_sal_first[from[i]] = to[i];
3342 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3343 }
3344
Bram Moolenaar5195e452005-08-19 20:32:47 +00003345 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003346}
3347
3348/*
3349 * Fill the first-index table for "lp".
3350 */
3351 static void
3352set_sal_first(lp)
3353 slang_T *lp;
3354{
3355 salfirst_T *sfirst;
3356 int i;
3357 salitem_T *smp;
3358 int c;
3359 garray_T *gap = &lp->sl_sal;
3360
3361 sfirst = lp->sl_sal_first;
3362 for (i = 0; i < 256; ++i)
3363 sfirst[i] = -1;
3364 smp = (salitem_T *)gap->ga_data;
3365 for (i = 0; i < gap->ga_len; ++i)
3366 {
3367#ifdef FEAT_MBYTE
3368 if (has_mbyte)
3369 /* Use the lowest byte of the first character. For latin1 it's
3370 * the character, for other encodings it should differ for most
3371 * characters. */
3372 c = *smp[i].sm_lead_w & 0xff;
3373 else
3374#endif
3375 c = *smp[i].sm_lead;
3376 if (sfirst[c] == -1)
3377 {
3378 sfirst[c] = i;
3379#ifdef FEAT_MBYTE
3380 if (has_mbyte)
3381 {
3382 int n;
3383
3384 /* Make sure all entries with this byte are following each
3385 * other. Move the ones that are in the wrong position. Do
3386 * keep the same ordering! */
3387 while (i + 1 < gap->ga_len
3388 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3389 /* Skip over entry with same index byte. */
3390 ++i;
3391
3392 for (n = 1; i + n < gap->ga_len; ++n)
3393 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3394 {
3395 salitem_T tsal;
3396
3397 /* Move entry with same index byte after the entries
3398 * we already found. */
3399 ++i;
3400 --n;
3401 tsal = smp[i + n];
3402 mch_memmove(smp + i + 1, smp + i,
3403 sizeof(salitem_T) * n);
3404 smp[i] = tsal;
3405 }
3406 }
3407#endif
3408 }
3409 }
3410}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003411
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003412#ifdef FEAT_MBYTE
3413/*
3414 * Turn a multi-byte string into a wide character string.
3415 * Return it in allocated memory (NULL for out-of-memory)
3416 */
3417 static int *
3418mb_str2wide(s)
3419 char_u *s;
3420{
3421 int *res;
3422 char_u *p;
3423 int i = 0;
3424
3425 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3426 if (res != NULL)
3427 {
3428 for (p = s; *p != NUL; )
3429 res[i++] = mb_ptr2char_adv(&p);
3430 res[i] = NUL;
3431 }
3432 return res;
3433}
3434#endif
3435
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003436/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003437 * Read one row of siblings from the spell file and store it in the byte array
3438 * "byts" and index array "idxs". Recursively read the children.
3439 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00003440 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00003441 *
3442 * Returns the index follosing the siblings.
3443 * Returns -1 if the file is shorter than expected.
3444 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003445 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003446 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003447read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003448 FILE *fd;
3449 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003450 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003451 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003452 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003453 int prefixtree; /* TRUE for reading PREFIXTREE */
3454 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003455{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003456 int len;
3457 int i;
3458 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003459 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003460 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003461 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003462#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003463
Bram Moolenaar51485f02005-06-04 21:55:20 +00003464 len = getc(fd); /* <siblingcount> */
3465 if (len <= 0)
3466 return -1;
3467
3468 if (startidx + len >= maxidx)
3469 return -2;
3470 byts[idx++] = len;
3471
3472 /* Read the byte values, flag/region bytes and shared indexes. */
3473 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003474 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003475 c = getc(fd); /* <byte> */
3476 if (c < 0)
3477 return -1;
3478 if (c <= BY_SPECIAL)
3479 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003480 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003481 {
3482 /* No flags, all regions. */
3483 idxs[idx] = 0;
3484 c = 0;
3485 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003486 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003487 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003488 if (prefixtree)
3489 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003490 /* Read the optional pflags byte, the prefix ID and the
3491 * condition nr. In idxs[] store the prefix ID in the low
3492 * byte, the condition index shifted up 8 bits, the flags
3493 * shifted up 24 bits. */
3494 if (c == BY_FLAGS)
3495 c = getc(fd) << 24; /* <pflags> */
3496 else
3497 c = 0;
3498
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003499 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003500
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003501 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
3502 if (n >= maxprefcondnr)
3503 return -2;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003504 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003505 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003506 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003507 {
3508 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003509 * idxs[] the flags go in the low two bytes, region above
3510 * that and prefix ID above the region. */
3511 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003512 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003513 if (c2 == BY_FLAGS2)
3514 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003515 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003516 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003517 if (c & WF_AFX)
3518 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003519 }
3520
Bram Moolenaar51485f02005-06-04 21:55:20 +00003521 idxs[idx] = c;
3522 c = 0;
3523 }
3524 else /* c == BY_INDEX */
3525 {
3526 /* <nodeidx> */
3527 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
3528 if (n < 0 || n >= maxidx)
3529 return -2;
3530 idxs[idx] = n + SHARED_MASK;
3531 c = getc(fd); /* <xbyte> */
3532 }
3533 }
3534 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003535 }
3536
Bram Moolenaar51485f02005-06-04 21:55:20 +00003537 /* Recursively read the children for non-shared siblings.
3538 * Skip the end-of-word ones (zero byte value) and the shared ones (and
3539 * remove SHARED_MASK) */
3540 for (i = 1; i <= len; ++i)
3541 if (byts[startidx + i] != 0)
3542 {
3543 if (idxs[startidx + i] & SHARED_MASK)
3544 idxs[startidx + i] &= ~SHARED_MASK;
3545 else
3546 {
3547 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003548 idx = read_tree(fd, byts, idxs, maxidx, idx,
3549 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003550 if (idx < 0)
3551 break;
3552 }
3553 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003554
Bram Moolenaar51485f02005-06-04 21:55:20 +00003555 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003556}
3557
3558/*
3559 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003560 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003561 */
3562 char_u *
3563did_set_spelllang(buf)
3564 buf_T *buf;
3565{
3566 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003567 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003568 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00003569 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003570 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003571 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003572 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003573 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003574 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003575 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003576 int len;
3577 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003578 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003579 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003580 char_u *use_region = NULL;
3581 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003582 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003583 int i, j;
3584 langp_T *lp, *lp2;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003585
3586 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003587 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003588
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003589 /* loop over comma separated language names. */
3590 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003591 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003592 /* Get one language name. */
3593 copy_option_part(&splp, lang, MAXWLEN, ",");
3594
Bram Moolenaar5482f332005-04-17 20:18:43 +00003595 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003596 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003597
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003598 /* If the name ends in ".spl" use it as the name of the spell file.
3599 * If there is a region name let "region" point to it and remove it
3600 * from the name. */
3601 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
3602 {
3603 filename = TRUE;
3604
Bram Moolenaarb6356332005-07-18 21:40:44 +00003605 /* Locate a region and remove it from the file name. */
3606 p = vim_strchr(gettail(lang), '_');
3607 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
3608 && !ASCII_ISALPHA(p[3]))
3609 {
3610 vim_strncpy(region_cp, p + 1, 2);
3611 mch_memmove(p, p + 3, len - (p - lang) - 2);
3612 len -= 3;
3613 region = region_cp;
3614 }
3615 else
3616 dont_use_region = TRUE;
3617
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003618 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003619 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
3620 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003621 break;
3622 }
3623 else
3624 {
3625 filename = FALSE;
3626 if (len > 3 && lang[len - 3] == '_')
3627 {
3628 region = lang + len - 2;
3629 len -= 3;
3630 lang[len] = NUL;
3631 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003632 else
3633 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003634
3635 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003636 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
3637 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003638 break;
3639 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003640
Bram Moolenaarb6356332005-07-18 21:40:44 +00003641 if (region != NULL)
3642 {
3643 /* If the region differs from what was used before then don't
3644 * use it for 'spellfile'. */
3645 if (use_region != NULL && STRCMP(region, use_region) != 0)
3646 dont_use_region = TRUE;
3647 use_region = region;
3648 }
3649
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003650 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003651 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003652 {
3653 if (filename)
3654 (void)spell_load_file(lang, lang, NULL, FALSE);
3655 else
3656 spell_load_lang(lang);
3657 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003658
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003659 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003660 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003661 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003662 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
3663 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
3664 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003665 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003666 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003667 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003668 {
3669 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003670 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003671 if (c == REGION_ALL)
3672 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003673 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003674 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003675 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003676 /* This addition file is for other regions. */
3677 region_mask = 0;
3678 }
3679 else
3680 /* This is probably an error. Give a warning and
3681 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003682 smsg((char_u *)
3683 _("Warning: region %s not supported"),
3684 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003685 }
3686 else
3687 region_mask = 1 << c;
3688 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003689
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003690 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003691 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003692 if (ga_grow(&ga, 1) == FAIL)
3693 {
3694 ga_clear(&ga);
3695 return e_outofmem;
3696 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003697 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003698 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
3699 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003700 use_midword(slang, buf);
3701 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003702 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003703 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003704 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003705 }
3706
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003707 /* round 0: load int_wordlist, if possible.
3708 * round 1: load first name in 'spellfile'.
3709 * round 2: load second name in 'spellfile.
3710 * etc. */
3711 spf = curbuf->b_p_spf;
3712 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003713 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003714 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003715 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003716 /* Internal wordlist, if there is one. */
3717 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003718 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003719 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003720 }
3721 else
3722 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003723 /* One entry in 'spellfile'. */
3724 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
3725 STRCAT(spf_name, ".spl");
3726
3727 /* If it was already found above then skip it. */
3728 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003729 {
3730 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
3731 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003732 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003733 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003734 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003735 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003736 }
3737
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003738 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003739 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
3740 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003741 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003742 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003743 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003744 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003745 * region name, the region is ignored otherwise. for int_wordlist
3746 * use an arbitrary name. */
3747 if (round == 0)
3748 STRCPY(lang, "internal wordlist");
3749 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00003750 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003751 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003752 p = vim_strchr(lang, '.');
3753 if (p != NULL)
3754 *p = NUL; /* truncate at ".encoding.add" */
3755 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003756 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003757
3758 /* If one of the languages has NOBREAK we assume the addition
3759 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003760 if (slang != NULL && nobreak)
3761 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003762 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003763 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003764 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003765 region_mask = REGION_ALL;
3766 if (use_region != NULL && !dont_use_region)
3767 {
3768 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003769 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003770 if (c != REGION_ALL)
3771 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003772 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003773 /* This spell file is for other regions. */
3774 region_mask = 0;
3775 }
3776
3777 if (region_mask != 0)
3778 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003779 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
3780 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
3781 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003782 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
3783 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003784 use_midword(slang, buf);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003785 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003786 }
3787 }
3788
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003789 /* Everything is fine, store the new b_langp value. */
3790 ga_clear(&buf->b_langp);
3791 buf->b_langp = ga;
3792
Bram Moolenaar8b96d642005-09-05 22:05:30 +00003793 /* For each language figure out what language to use for sound folding and
3794 * REP items. If the language doesn't support it itself use another one
3795 * with the same name. E.g. for "en-math" use "en". */
3796 for (i = 0; i < ga.ga_len; ++i)
3797 {
3798 lp = LANGP_ENTRY(ga, i);
3799
3800 /* sound folding */
3801 if (lp->lp_slang->sl_sal.ga_len > 0)
3802 /* language does sound folding itself */
3803 lp->lp_sallang = lp->lp_slang;
3804 else
3805 /* find first similar language that does sound folding */
3806 for (j = 0; j < ga.ga_len; ++j)
3807 {
3808 lp2 = LANGP_ENTRY(ga, j);
3809 if (lp2->lp_slang->sl_sal.ga_len > 0
3810 && STRNCMP(lp->lp_slang->sl_name,
3811 lp2->lp_slang->sl_name, 2) == 0)
3812 {
3813 lp->lp_sallang = lp2->lp_slang;
3814 break;
3815 }
3816 }
3817
3818 /* REP items */
3819 if (lp->lp_slang->sl_rep.ga_len > 0)
3820 /* language has REP items itself */
3821 lp->lp_replang = lp->lp_slang;
3822 else
3823 /* find first similar language that does sound folding */
3824 for (j = 0; j < ga.ga_len; ++j)
3825 {
3826 lp2 = LANGP_ENTRY(ga, j);
3827 if (lp2->lp_slang->sl_rep.ga_len > 0
3828 && STRNCMP(lp->lp_slang->sl_name,
3829 lp2->lp_slang->sl_name, 2) == 0)
3830 {
3831 lp->lp_replang = lp2->lp_slang;
3832 break;
3833 }
3834 }
3835 }
3836
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003837 return NULL;
3838}
3839
3840/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003841 * Clear the midword characters for buffer "buf".
3842 */
3843 static void
3844clear_midword(buf)
3845 buf_T *buf;
3846{
3847 vim_memset(buf->b_spell_ismw, 0, 256);
3848#ifdef FEAT_MBYTE
3849 vim_free(buf->b_spell_ismw_mb);
3850 buf->b_spell_ismw_mb = NULL;
3851#endif
3852}
3853
3854/*
3855 * Use the "sl_midword" field of language "lp" for buffer "buf".
3856 * They add up to any currently used midword characters.
3857 */
3858 static void
3859use_midword(lp, buf)
3860 slang_T *lp;
3861 buf_T *buf;
3862{
3863 char_u *p;
3864
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003865 if (lp->sl_midword == NULL) /* there aren't any */
3866 return;
3867
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003868 for (p = lp->sl_midword; *p != NUL; )
3869#ifdef FEAT_MBYTE
3870 if (has_mbyte)
3871 {
3872 int c, l, n;
3873 char_u *bp;
3874
3875 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003876 l = (*mb_ptr2len)(p);
3877 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003878 buf->b_spell_ismw[c] = TRUE;
3879 else if (buf->b_spell_ismw_mb == NULL)
3880 /* First multi-byte char in "b_spell_ismw_mb". */
3881 buf->b_spell_ismw_mb = vim_strnsave(p, l);
3882 else
3883 {
3884 /* Append multi-byte chars to "b_spell_ismw_mb". */
3885 n = STRLEN(buf->b_spell_ismw_mb);
3886 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
3887 if (bp != NULL)
3888 {
3889 vim_free(buf->b_spell_ismw_mb);
3890 buf->b_spell_ismw_mb = bp;
3891 vim_strncpy(bp + n, p, l);
3892 }
3893 }
3894 p += l;
3895 }
3896 else
3897#endif
3898 buf->b_spell_ismw[*p++] = TRUE;
3899}
3900
3901/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003902 * Find the region "region[2]" in "rp" (points to "sl_regions").
3903 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003904 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003905 */
3906 static int
3907find_region(rp, region)
3908 char_u *rp;
3909 char_u *region;
3910{
3911 int i;
3912
3913 for (i = 0; ; i += 2)
3914 {
3915 if (rp[i] == NUL)
3916 return REGION_ALL;
3917 if (rp[i] == region[0] && rp[i + 1] == region[1])
3918 break;
3919 }
3920 return i / 2;
3921}
3922
3923/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003924 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003925 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00003926 * Word WF_ONECAP
3927 * W WORD WF_ALLCAP
3928 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003929 */
3930 static int
3931captype(word, end)
3932 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003933 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003934{
3935 char_u *p;
3936 int c;
3937 int firstcap;
3938 int allcap;
3939 int past_second = FALSE; /* past second word char */
3940
3941 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003942 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003943 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003944 return 0; /* only non-word characters, illegal word */
3945#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00003946 if (has_mbyte)
3947 c = mb_ptr2char_adv(&p);
3948 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003949#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00003950 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003951 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003952
3953 /*
3954 * Need to check all letters to find a word with mixed upper/lower.
3955 * But a word with an upper char only at start is a ONECAP.
3956 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003957 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003958 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003959 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003960 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003961 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003962 {
3963 /* UUl -> KEEPCAP */
3964 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003965 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003966 allcap = FALSE;
3967 }
3968 else if (!allcap)
3969 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003970 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003971 past_second = TRUE;
3972 }
3973
3974 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003975 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003976 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003977 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003978 return 0;
3979}
3980
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003981/*
3982 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
3983 * capital. So that make_case_word() can turn WOrd into Word.
3984 * Add ALLCAP for "WOrD".
3985 */
3986 static int
3987badword_captype(word, end)
3988 char_u *word;
3989 char_u *end;
3990{
3991 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003992 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003993 int l, u;
3994 int first;
3995 char_u *p;
3996
3997 if (flags & WF_KEEPCAP)
3998 {
3999 /* Count the number of UPPER and lower case letters. */
4000 l = u = 0;
4001 first = FALSE;
4002 for (p = word; p < end; mb_ptr_adv(p))
4003 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004004 c = PTR2CHAR(p);
4005 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004006 {
4007 ++u;
4008 if (p == word)
4009 first = TRUE;
4010 }
4011 else
4012 ++l;
4013 }
4014
4015 /* If there are more UPPER than lower case letters suggest an
4016 * ALLCAP word. Otherwise, if the first letter is UPPER then
4017 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4018 * require three upper case letters. */
4019 if (u > l && u > 2)
4020 flags |= WF_ALLCAP;
4021 else if (first)
4022 flags |= WF_ONECAP;
4023 }
4024 return flags;
4025}
4026
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004027# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4028/*
4029 * Free all languages.
4030 */
4031 void
4032spell_free_all()
4033{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004034 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004035 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004036 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004037
4038 /* Go through all buffers and handle 'spelllang'. */
4039 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4040 ga_clear(&buf->b_langp);
4041
4042 while (first_lang != NULL)
4043 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004044 slang = first_lang;
4045 first_lang = slang->sl_next;
4046 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004047 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004048
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004049 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004050 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004051 /* Delete the internal wordlist and its .spl file */
4052 mch_remove(int_wordlist);
4053 int_wordlist_spl(fname);
4054 mch_remove(fname);
4055 vim_free(int_wordlist);
4056 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004057 }
4058
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004059 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004060}
4061# endif
4062
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004063# if defined(FEAT_MBYTE) || defined(PROTO)
4064/*
4065 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004066 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004067 */
4068 void
4069spell_reload()
4070{
4071 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004072 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004073
Bram Moolenaarea408852005-06-25 22:49:46 +00004074 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004075 init_spell_chartab();
4076
4077 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004078 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004079
4080 /* Go through all buffers and handle 'spelllang'. */
4081 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4082 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004083 /* Only load the wordlists when 'spelllang' is set and there is a
4084 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004085 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004086 {
4087 FOR_ALL_WINDOWS(wp)
4088 if (wp->w_buffer == buf && wp->w_p_spell)
4089 {
4090 (void)did_set_spelllang(buf);
4091# ifdef FEAT_WINDOWS
4092 break;
4093# endif
4094 }
4095 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004096 }
4097}
4098# endif
4099
Bram Moolenaarb765d632005-06-07 21:00:02 +00004100/*
4101 * Reload the spell file "fname" if it's loaded.
4102 */
4103 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004104spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004105 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004106 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004107{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004108 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004109 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004110
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004111 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004112 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004113 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004114 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004115 slang_clear(slang);
4116 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004117 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004118 slang_clear(slang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004119 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004120 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004121 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004122 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004123
4124 /* When "zg" was used and the file wasn't loaded yet, should redo
4125 * 'spelllang' to get it loaded. */
4126 if (added_word && !didit)
4127 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004128}
4129
4130
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004131/*
4132 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004133 */
4134
Bram Moolenaar51485f02005-06-04 21:55:20 +00004135#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004136 and .dic file. */
4137/*
4138 * Main structure to store the contents of a ".aff" file.
4139 */
4140typedef struct afffile_S
4141{
4142 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004143 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004144 int af_slash; /* character used in word for slash */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004145 unsigned af_rare; /* RARE ID for rare word */
4146 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004147 unsigned af_bad; /* BAD ID for banned word */
4148 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004149 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004150 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004151 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4152 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004153 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004154} afffile_T;
4155
Bram Moolenaar6de68532005-08-24 22:08:48 +00004156#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004157#define AFT_LONG 1 /* flags are two characters */
4158#define AFT_CAPLONG 2 /* flags are one or two characters */
4159#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004160
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004161typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004162/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4163struct affentry_S
4164{
4165 affentry_T *ae_next; /* next affix with same name/number */
4166 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4167 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004168 char_u *ae_cond; /* condition (NULL for ".") */
4169 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004170 char_u ae_rare; /* rare affix */
4171 char_u ae_nocomp; /* word with affix not compoundable */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004172};
4173
Bram Moolenaar6de68532005-08-24 22:08:48 +00004174#ifdef FEAT_MBYTE
4175# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4176#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004177# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004178#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004179
Bram Moolenaar51485f02005-06-04 21:55:20 +00004180/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4181typedef struct affheader_S
4182{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004183 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4184 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4185 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004186 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004187 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004188 affentry_T *ah_first; /* first affix entry */
4189} affheader_T;
4190
4191#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4192
Bram Moolenaar6de68532005-08-24 22:08:48 +00004193/* Flag used in compound items. */
4194typedef struct compitem_S
4195{
4196 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4197 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4198 int ci_newID; /* affix ID after renumbering. */
4199} compitem_T;
4200
4201#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4202
Bram Moolenaar51485f02005-06-04 21:55:20 +00004203/*
4204 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004205 * the need to keep track of each allocated thing, everything is freed all at
4206 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004207 */
4208#define SBLOCKSIZE 16000 /* size of sb_data */
4209typedef struct sblock_S sblock_T;
4210struct sblock_S
4211{
4212 sblock_T *sb_next; /* next block in list */
4213 int sb_used; /* nr of bytes already in use */
4214 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004215};
4216
4217/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004218 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004219 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004220typedef struct wordnode_S wordnode_T;
4221struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004222{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004223 union /* shared to save space */
4224 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004225 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004226 int index; /* index in written nodes (valid after first
4227 round) */
4228 } wn_u1;
4229 union /* shared to save space */
4230 {
4231 wordnode_T *next; /* next node with same hash key */
4232 wordnode_T *wnode; /* parent node that will write this node */
4233 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004234 wordnode_T *wn_child; /* child (next byte in word) */
4235 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4236 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004237 int wn_refs; /* Nr. of references to this node. Only
4238 relevant for first node in a list of
4239 siblings, in following siblings it is
4240 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004241 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004242 char_u wn_affixID; /* when "wn_byte" is NUL: supported/required
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004243 prefix ID or 0 */
4244 short_u wn_flags; /* when "wn_byte" is NUL: WF_ flags */
4245 short wn_region; /* when "wn_byte" is NUL: region mask; for
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004246 PREFIXTREE it's the prefcondnr */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004247#ifdef SPELL_PRINTTREE
4248 int wn_nr; /* sequence nr for printing */
4249#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004250};
4251
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004252#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4253
Bram Moolenaar51485f02005-06-04 21:55:20 +00004254#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004255
Bram Moolenaar51485f02005-06-04 21:55:20 +00004256/*
4257 * Info used while reading the spell files.
4258 */
4259typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004260{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004261 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004262 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004263
Bram Moolenaar51485f02005-06-04 21:55:20 +00004264 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004265 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004266
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004267 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004268
Bram Moolenaar51485f02005-06-04 21:55:20 +00004269 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004270 long si_blocks_cnt; /* memory blocks allocated */
4271 long si_compress_cnt; /* words to add before lowering
4272 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004273 wordnode_T *si_first_free; /* List of nodes that have been freed during
4274 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004275 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004276#ifdef SPELL_PRINTTREE
4277 int si_wordnode_nr; /* sequence nr for nodes */
4278#endif
4279
4280
Bram Moolenaar51485f02005-06-04 21:55:20 +00004281 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004282 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004283 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004284 int si_region; /* region mask */
4285 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004286 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004287 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004288 int si_msg_count; /* number of words added since last message */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004289 int si_region_count; /* number of regions supported (1 when there
4290 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004291 char_u si_region_name[16]; /* region names; used only if
4292 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004293
4294 garray_T si_rep; /* list of fromto_T entries from REP lines */
4295 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004296 char_u *si_sofofr; /* SOFOFROM text */
4297 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004298 int si_followup; /* soundsalike: ? */
4299 int si_collapse; /* soundsalike: ? */
4300 int si_rem_accents; /* soundsalike: remove accents */
4301 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004302 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004303 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004304 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004305 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004306 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004307 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004308 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004309 garray_T si_prefcond; /* table with conditions for postponed
4310 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004311 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004312 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004313} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004314
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004315static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004316static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
4317static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
4318static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004319static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004320static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
4321static void aff_check_number __ARGS((int spinval, int affval, char *name));
4322static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004323static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004324static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4325static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00004326static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004327static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004328static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004329static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004330static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004331static int store_aff_word __ARGS((spellinfo_T *spin, char_u *word, char_u *afflist, afffile_T *affile, hashtab_T *ht, hashtab_T *xht, int comb, int flags, char_u *pfxlist, int pfxlen));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004332static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
4333static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
4334static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004335static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004336static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004337static int store_word __ARGS((spellinfo_T *spin, char_u *word, int flags, int region, char_u *pfxlist, int need_affix));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004338static int tree_add_word __ARGS((spellinfo_T *spin, char_u *word, wordnode_T *tree, int flags, int region, int affixID));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004339static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
4340static void deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
4341static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
4342static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
4343static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004344static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004345static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00004346static void clear_node __ARGS((wordnode_T *node));
4347static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004348static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004349static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004350
Bram Moolenaar53805d12005-08-01 07:08:33 +00004351/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
4352 * but it must be negative to indicate the prefix tree to tree_add_word().
4353 * Use a negative number with the lower 8 bits zero. */
4354#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004355
Bram Moolenaar5195e452005-08-19 20:32:47 +00004356/*
4357 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
4358 */
4359static long compress_start = 30000; /* memory / SBLOCKSIZE */
4360static long compress_inc = 100; /* memory / SBLOCKSIZE */
4361static long compress_added = 500000; /* word count */
4362
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004363#ifdef SPELL_PRINTTREE
4364/*
4365 * For debugging the tree code: print the current tree in a (more or less)
4366 * readable format, so that we can see what happens when adding a word and/or
4367 * compressing the tree.
4368 * Based on code from Olaf Seibert.
4369 */
4370#define PRINTLINESIZE 1000
4371#define PRINTWIDTH 6
4372
4373#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4374 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4375
4376static char line1[PRINTLINESIZE];
4377static char line2[PRINTLINESIZE];
4378static char line3[PRINTLINESIZE];
4379
4380 static void
4381spell_clear_flags(wordnode_T *node)
4382{
4383 wordnode_T *np;
4384
4385 for (np = node; np != NULL; np = np->wn_sibling)
4386 {
4387 np->wn_u1.index = FALSE;
4388 spell_clear_flags(np->wn_child);
4389 }
4390}
4391
4392 static void
4393spell_print_node(wordnode_T *node, int depth)
4394{
4395 if (node->wn_u1.index)
4396 {
4397 /* Done this node before, print the reference. */
4398 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
4399 PRINTSOME(line2, depth, " ", 0, 0);
4400 PRINTSOME(line3, depth, " ", 0, 0);
4401 msg(line1);
4402 msg(line2);
4403 msg(line3);
4404 }
4405 else
4406 {
4407 node->wn_u1.index = TRUE;
4408
4409 if (node->wn_byte != NUL)
4410 {
4411 if (node->wn_child != NULL)
4412 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
4413 else
4414 /* Cannot happen? */
4415 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
4416 }
4417 else
4418 PRINTSOME(line1, depth, " $ ", 0, 0);
4419
4420 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
4421
4422 if (node->wn_sibling != NULL)
4423 PRINTSOME(line3, depth, " | ", 0, 0);
4424 else
4425 PRINTSOME(line3, depth, " ", 0, 0);
4426
4427 if (node->wn_byte == NUL)
4428 {
4429 msg(line1);
4430 msg(line2);
4431 msg(line3);
4432 }
4433
4434 /* do the children */
4435 if (node->wn_byte != NUL && node->wn_child != NULL)
4436 spell_print_node(node->wn_child, depth + 1);
4437
4438 /* do the siblings */
4439 if (node->wn_sibling != NULL)
4440 {
4441 /* get rid of all parent details except | */
4442 STRCPY(line1, line3);
4443 STRCPY(line2, line3);
4444 spell_print_node(node->wn_sibling, depth);
4445 }
4446 }
4447}
4448
4449 static void
4450spell_print_tree(wordnode_T *root)
4451{
4452 if (root != NULL)
4453 {
4454 /* Clear the "wn_u1.index" fields, used to remember what has been
4455 * done. */
4456 spell_clear_flags(root);
4457
4458 /* Recursively print the tree. */
4459 spell_print_node(root, 0);
4460 }
4461}
4462#endif /* SPELL_PRINTTREE */
4463
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004464/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004465 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00004466 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004467 */
4468 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004469spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004470 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004471 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004472{
4473 FILE *fd;
4474 afffile_T *aff;
4475 char_u rline[MAXLINELEN];
4476 char_u *line;
4477 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004478#define MAXITEMCNT 7
4479 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004480 int itemcnt;
4481 char_u *p;
4482 int lnum = 0;
4483 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004484 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004485 int aff_todo = 0;
4486 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004487 char_u *low = NULL;
4488 char_u *fol = NULL;
4489 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004490 int do_rep;
4491 int do_sal;
4492 int do_map;
4493 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004494 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004495 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004496 int compminlen = 0; /* COMPOUNDMIN value */
4497 int compsylmax = 0; /* COMPOUNDSYLMAX value */
4498 int compmax = 0; /* COMPOUNDMAX value */
4499 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDFLAGS
4500 concatenated */
4501 char_u *midword = NULL; /* MIDWORD value */
4502 char_u *syllable = NULL; /* SYLLABLE value */
4503 char_u *sofofrom = NULL; /* SOFOFROM value */
4504 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004505
Bram Moolenaar51485f02005-06-04 21:55:20 +00004506 /*
4507 * Open the file.
4508 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004509 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004510 if (fd == NULL)
4511 {
4512 EMSG2(_(e_notopen), fname);
4513 return NULL;
4514 }
4515
Bram Moolenaarb765d632005-06-07 21:00:02 +00004516 if (spin->si_verbose || p_verbose > 2)
4517 {
4518 if (!spin->si_verbose)
4519 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004520 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004521 out_flush();
4522 if (!spin->si_verbose)
4523 verbose_leave();
4524 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004525
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004526 /* Only do REP lines when not done in another .aff file already. */
4527 do_rep = spin->si_rep.ga_len == 0;
4528
4529 /* Only do SAL lines when not done in another .aff file already. */
4530 do_sal = spin->si_sal.ga_len == 0;
4531
4532 /* Only do MAP lines when not done in another .aff file already. */
4533 do_map = spin->si_map.ga_len == 0;
4534
Bram Moolenaar51485f02005-06-04 21:55:20 +00004535 /*
4536 * Allocate and init the afffile_T structure.
4537 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004538 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004539 if (aff == NULL)
4540 return NULL;
4541 hash_init(&aff->af_pref);
4542 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004543 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004544
4545 /*
4546 * Read all the lines in the file one by one.
4547 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004548 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004549 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004550 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004551 ++lnum;
4552
4553 /* Skip comment lines. */
4554 if (*rline == '#')
4555 continue;
4556
4557 /* Convert from "SET" to 'encoding' when needed. */
4558 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004559#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004560 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004561 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004562 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004563 if (pc == NULL)
4564 {
4565 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4566 fname, lnum, rline);
4567 continue;
4568 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004569 line = pc;
4570 }
4571 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004572#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004573 {
4574 pc = NULL;
4575 line = rline;
4576 }
4577
4578 /* Split the line up in white separated items. Put a NUL after each
4579 * item. */
4580 itemcnt = 0;
4581 for (p = line; ; )
4582 {
4583 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
4584 ++p;
4585 if (*p == NUL)
4586 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004587 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004588 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004589 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004590 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004591 ++p;
4592 if (*p == NUL)
4593 break;
4594 *p++ = NUL;
4595 }
4596
4597 /* Handle non-empty lines. */
4598 if (itemcnt > 0)
4599 {
4600 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
4601 && aff->af_enc == NULL)
4602 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004603#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004604 /* Setup for conversion from "ENC" to 'encoding'. */
4605 aff->af_enc = enc_canonize(items[1]);
4606 if (aff->af_enc != NULL && !spin->si_ascii
4607 && convert_setup(&spin->si_conv, aff->af_enc,
4608 p_enc) == FAIL)
4609 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
4610 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004611 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004612#else
4613 smsg((char_u *)_("Conversion in %s not supported"), fname);
4614#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004615 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00004616 else if (STRCMP(items[0], "FLAG") == 0 && itemcnt == 2
4617 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004618 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004619 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00004620 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004621 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00004622 aff->af_flagtype = AFT_NUM;
4623 else if (STRCMP(items[1], "caplong") == 0)
4624 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004625 else
4626 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
4627 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00004628 if (aff->af_rare != 0
4629 || aff->af_keepcase != 0
4630 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004631 || aff->af_needaffix != 0
4632 || aff->af_needcomp != 0
4633 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00004634 || aff->af_suff.ht_used > 0
4635 || aff->af_pref.ht_used > 0)
4636 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
4637 fname, lnum, items[1]);
4638 }
4639 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2
4640 && midword == NULL)
4641 {
4642 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004643 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00004644 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
4645 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004646 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004647 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004648 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004649 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004650 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004651 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004652 else if (STRCMP(items[0], "SLASH") == 0 && itemcnt == 2
4653 && aff->af_slash == 0)
4654 {
4655 aff->af_slash = items[1][0];
4656 if (items[1][1] != NUL)
4657 smsg((char_u *)_("Character used for SLASH must be ASCII; in %s line %d: %s"),
4658 fname, lnum, items[1]);
4659 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00004660 /* TODO: remove "RAR" later */
4661 else if ((STRCMP(items[0], "RAR") == 0
4662 || STRCMP(items[0], "RARE") == 0) && itemcnt == 2
4663 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004664 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00004665 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00004666 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004667 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00004668 /* TODO: remove "KEP" later */
4669 else if ((STRCMP(items[0], "KEP") == 0
4670 || STRCMP(items[0], "KEEPCASE") == 0) && itemcnt == 2
4671 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004672 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00004673 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00004674 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004675 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00004676 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
4677 && aff->af_bad == 0)
4678 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004679 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
4680 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004681 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004682 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
4683 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004684 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004685 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
4686 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004687 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004688 else if (STRCMP(items[0], "NEEDCOMPOUND") == 0 && itemcnt == 2
4689 && aff->af_needcomp == 0)
4690 {
4691 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
4692 fname, lnum);
4693 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004694 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004695 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004696 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004697 /* Turn flag "c" into COMPOUNDFLAGS compatible string "c+",
4698 * "Na" into "Na+", "1234" into "1234+". */
4699 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004700 if (p != NULL)
4701 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004702 STRCPY(p, items[1]);
4703 STRCAT(p, "+");
4704 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004705 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004706 }
4707 else if (STRCMP(items[0], "COMPOUNDFLAGS") == 0 && itemcnt == 2)
4708 {
4709 /* Concatenate this string to previously defined ones, using a
4710 * slash to separate them. */
4711 l = STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004712 if (compflags != NULL)
4713 l += STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004714 p = getroom(spin, l, FALSE);
4715 if (p != NULL)
4716 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004717 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004718 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004719 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004720 STRCAT(p, "/");
4721 }
4722 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004723 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004724 }
4725 }
4726 else if (STRCMP(items[0], "COMPOUNDMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004727 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004728 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004729 compmax = atoi((char *)items[1]);
4730 if (compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004731 smsg((char_u *)_("Wrong COMPOUNDMAX value in %s line %d: %s"),
4732 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004733 }
4734 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004735 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004736 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004737 compminlen = atoi((char *)items[1]);
4738 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004739 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
4740 fname, lnum, items[1]);
4741 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004742 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004743 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004744 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004745 compsylmax = atoi((char *)items[1]);
4746 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004747 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
4748 fname, lnum, items[1]);
4749 }
4750 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004751 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004752 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004753 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004754 }
Bram Moolenaar78622822005-08-23 21:00:13 +00004755 else if (STRCMP(items[0], "NOBREAK") == 0 && itemcnt == 1)
4756 {
4757 spin->si_nobreak = TRUE;
4758 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004759 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
4760 {
4761 aff->af_pfxpostpone = TRUE;
4762 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004763 else if ((STRCMP(items[0], "PFX") == 0
4764 || STRCMP(items[0], "SFX") == 0)
4765 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00004766 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004767 {
Bram Moolenaar95529562005-08-25 21:21:38 +00004768 int lasti = 4;
4769 char_u key[AH_KEY_LEN];
4770
4771 if (*items[0] == 'P')
4772 tp = &aff->af_pref;
4773 else
4774 tp = &aff->af_suff;
4775
4776 /* Myspell allows the same affix name to be used multiple
4777 * times. The affix files that do this have an undocumented
4778 * "S" flag on all but the last block, thus we check for that
4779 * and store it in ah_follows. */
4780 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
4781 hi = hash_find(tp, key);
4782 if (!HASHITEM_EMPTY(hi))
4783 {
4784 cur_aff = HI2AH(hi);
4785 if (cur_aff->ah_combine != (*items[2] == 'Y'))
4786 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
4787 fname, lnum, items[1]);
4788 if (!cur_aff->ah_follows)
4789 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
4790 fname, lnum, items[1]);
4791 }
4792 else
4793 {
4794 /* New affix letter. */
4795 cur_aff = (affheader_T *)getroom(spin,
4796 sizeof(affheader_T), TRUE);
4797 if (cur_aff == NULL)
4798 break;
4799 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
4800 fname, lnum);
4801 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
4802 break;
4803 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00004804 || cur_aff->ah_flag == aff->af_rare
4805 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004806 || cur_aff->ah_flag == aff->af_needaffix
4807 || cur_aff->ah_flag == aff->af_needcomp)
Bram Moolenaar371baa92005-12-29 22:43:53 +00004808 smsg((char_u *)_("Affix also used for BAD/RARE/KEEPCASE/NEEDAFFIX/NEEDCOMPOUND in %s line %d: %s"),
Bram Moolenaar95529562005-08-25 21:21:38 +00004809 fname, lnum, items[1]);
4810 STRCPY(cur_aff->ah_key, items[1]);
4811 hash_add(tp, cur_aff->ah_key);
4812
4813 cur_aff->ah_combine = (*items[2] == 'Y');
4814 }
4815
4816 /* Check for the "S" flag, which apparently means that another
4817 * block with the same affix name is following. */
4818 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
4819 {
4820 ++lasti;
4821 cur_aff->ah_follows = TRUE;
4822 }
4823 else
4824 cur_aff->ah_follows = FALSE;
4825
Bram Moolenaar8db73182005-06-17 21:51:16 +00004826 /* Myspell allows extra text after the item, but that might
4827 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00004828 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00004829 smsg((char_u *)_("Trailing text in %s line %d: %s"),
4830 fname, lnum, items[4]);
4831
Bram Moolenaar95529562005-08-25 21:21:38 +00004832 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004833 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
4834 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004835
Bram Moolenaar95529562005-08-25 21:21:38 +00004836 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004837 {
Bram Moolenaar95529562005-08-25 21:21:38 +00004838 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00004839 {
4840 /* Use a new number in the .spl file later, to be able
4841 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004842 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004843 cur_aff->ah_newID = ++spin->si_newprefID;
4844
4845 /* We only really use ah_newID if the prefix is
4846 * postponed. We know that only after handling all
4847 * the items. */
4848 did_postpone_prefix = FALSE;
4849 }
Bram Moolenaar95529562005-08-25 21:21:38 +00004850 else
4851 /* Did use the ID in a previous block. */
4852 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004853 }
Bram Moolenaar95529562005-08-25 21:21:38 +00004854
Bram Moolenaar51485f02005-06-04 21:55:20 +00004855 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004856 }
4857 else if ((STRCMP(items[0], "PFX") == 0
4858 || STRCMP(items[0], "SFX") == 0)
4859 && aff_todo > 0
4860 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00004861 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004862 {
4863 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004864 int rare = FALSE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004865 int nocomp = FALSE;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004866 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004867 int lasti = 5;
4868
Bram Moolenaar5195e452005-08-19 20:32:47 +00004869 /* Check for "rare" and "nocomp" after the other info. */
4870 while (itemcnt > lasti)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004871 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004872 if (!rare && STRICMP(items[lasti], "rare") == 0)
4873 {
4874 rare = TRUE;
4875 ++lasti;
4876 }
4877 else if (!nocomp && STRICMP(items[lasti], "nocomp") == 0)
4878 {
4879 nocomp = TRUE;
4880 ++lasti;
4881 }
4882 else
4883 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004884 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004885
Bram Moolenaar8db73182005-06-17 21:51:16 +00004886 /* Myspell allows extra text after the item, but that might
4887 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004888 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004889 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00004890
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004891 /* New item for an affix letter. */
4892 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004893 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004894 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004895 if (aff_entry == NULL)
4896 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004897 aff_entry->ae_rare = rare;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004898 aff_entry->ae_nocomp = nocomp;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004899
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004900 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004901 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004902 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004903 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004904
Bram Moolenaar51485f02005-06-04 21:55:20 +00004905 /* Don't use an affix entry with non-ASCII characters when
4906 * "spin->si_ascii" is TRUE. */
4907 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004908 || has_non_ascii(aff_entry->ae_add)))
4909 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00004910 aff_entry->ae_next = cur_aff->ah_first;
4911 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004912
4913 if (STRCMP(items[4], ".") != 0)
4914 {
4915 char_u buf[MAXLINELEN];
4916
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004917 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004918 if (*items[0] == 'P')
4919 sprintf((char *)buf, "^%s", items[4]);
4920 else
4921 sprintf((char *)buf, "%s$", items[4]);
4922 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004923 RE_MAGIC + RE_STRING + RE_STRICT);
4924 if (aff_entry->ae_prog == NULL)
4925 smsg((char_u *)_("Broken condition in %s line %d: %s"),
4926 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004927 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004928
4929 /* For postponed prefixes we need an entry in si_prefcond
4930 * for the condition. Use an existing one if possible. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004931 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004932 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004933 /* When the chop string is one lower-case letter and
4934 * the add string ends in the upper-case letter we set
4935 * the "upper" flag, clear "ae_chop" and remove the
4936 * letters from "ae_add". The condition must either
4937 * be empty or start with the same letter. */
4938 if (aff_entry->ae_chop != NULL
4939 && aff_entry->ae_add != NULL
4940#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004941 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00004942 aff_entry->ae_chop)] == NUL
4943#else
4944 && aff_entry->ae_chop[1] == NUL
4945#endif
4946 )
4947 {
4948 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004949
Bram Moolenaar53805d12005-08-01 07:08:33 +00004950 c = PTR2CHAR(aff_entry->ae_chop);
4951 c_up = SPELL_TOUPPER(c);
4952 if (c_up != c
4953 && (aff_entry->ae_cond == NULL
4954 || PTR2CHAR(aff_entry->ae_cond) == c))
4955 {
4956 p = aff_entry->ae_add
4957 + STRLEN(aff_entry->ae_add);
4958 mb_ptr_back(aff_entry->ae_add, p);
4959 if (PTR2CHAR(p) == c_up)
4960 {
4961 upper = TRUE;
4962 aff_entry->ae_chop = NULL;
4963 *p = NUL;
4964
4965 /* The condition is matched with the
4966 * actual word, thus must check for the
4967 * upper-case letter. */
4968 if (aff_entry->ae_cond != NULL)
4969 {
4970 char_u buf[MAXLINELEN];
4971#ifdef FEAT_MBYTE
4972 if (has_mbyte)
4973 {
4974 onecap_copy(items[4], buf, TRUE);
4975 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004976 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004977 }
4978 else
4979#endif
4980 *aff_entry->ae_cond = c_up;
4981 if (aff_entry->ae_cond != NULL)
4982 {
4983 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004984 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004985 vim_free(aff_entry->ae_prog);
4986 aff_entry->ae_prog = vim_regcomp(
4987 buf, RE_MAGIC + RE_STRING);
4988 }
4989 }
4990 }
4991 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004992 }
4993
Bram Moolenaar53805d12005-08-01 07:08:33 +00004994 if (aff_entry->ae_chop == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004995 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004996 int idx;
4997 char_u **pp;
4998 int n;
4999
Bram Moolenaar6de68532005-08-24 22:08:48 +00005000 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005001 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5002 --idx)
5003 {
5004 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5005 if (str_equal(p, aff_entry->ae_cond))
5006 break;
5007 }
5008 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5009 {
5010 /* Not found, add a new condition. */
5011 idx = spin->si_prefcond.ga_len++;
5012 pp = ((char_u **)spin->si_prefcond.ga_data)
5013 + idx;
5014 if (aff_entry->ae_cond == NULL)
5015 *pp = NULL;
5016 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005017 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005018 aff_entry->ae_cond);
5019 }
5020
5021 /* Add the prefix to the prefix tree. */
5022 if (aff_entry->ae_add == NULL)
5023 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005024 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005025 p = aff_entry->ae_add;
5026 /* PFX_FLAGS is a negative number, so that
5027 * tree_add_word() knows this is the prefix tree. */
5028 n = PFX_FLAGS;
5029 if (rare)
5030 n |= WFP_RARE;
5031 if (!cur_aff->ah_combine)
5032 n |= WFP_NC;
5033 if (upper)
5034 n |= WFP_UP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005035 tree_add_word(spin, p, spin->si_prefroot, n,
5036 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005037 did_postpone_prefix = TRUE;
5038 }
5039
5040 /* Didn't actually use ah_newID, backup si_newprefID. */
5041 if (aff_todo == 0 && !did_postpone_prefix)
5042 {
5043 --spin->si_newprefID;
5044 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005045 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005046 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005047 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005048 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005049 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2
5050 && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005051 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005052 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005053 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005054 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2
5055 && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005056 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005057 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005058 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005059 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2
5060 && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005061 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005062 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005063 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005064 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005065 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005066 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005067 if (!isdigit(*items[1]))
5068 smsg((char_u *)_("Expected REP count in %s line %d"),
5069 fname, lnum);
5070 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005071 else if (STRCMP(items[0], "REP") == 0 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005072 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005073 /* REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005074 /* Myspell ignores extra arguments, we require it starts with
5075 * # to detect mistakes. */
5076 if (itemcnt > 3 && items[3][0] != '#')
5077 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005078 if (do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005079 {
5080 /* Replace underscore with space (can't include a space
5081 * directly). */
5082 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5083 if (*p == '_')
5084 *p = ' ';
5085 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5086 if (*p == '_')
5087 *p = ' ';
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005088 add_fromto(spin, &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005089 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005090 }
5091 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
5092 {
5093 /* MAP item or count */
5094 if (!found_map)
5095 {
5096 /* First line contains the count. */
5097 found_map = TRUE;
5098 if (!isdigit(*items[1]))
5099 smsg((char_u *)_("Expected MAP count in %s line %d"),
5100 fname, lnum);
5101 }
5102 else if (do_map)
5103 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005104 int c;
5105
5106 /* Check that every character appears only once. */
5107 for (p = items[1]; *p != NUL; )
5108 {
5109#ifdef FEAT_MBYTE
5110 c = mb_ptr2char_adv(&p);
5111#else
5112 c = *p++;
5113#endif
5114 if ((spin->si_map.ga_len > 0
5115 && vim_strchr(spin->si_map.ga_data, c)
5116 != NULL)
5117 || vim_strchr(p, c) != NULL)
5118 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5119 fname, lnum);
5120 }
5121
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005122 /* We simply concatenate all the MAP strings, separated by
5123 * slashes. */
5124 ga_concat(&spin->si_map, items[1]);
5125 ga_append(&spin->si_map, '/');
5126 }
5127 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005128 /* Accept "SAL from to" and "SAL from to # comment". */
5129 else if (STRCMP(items[0], "SAL") == 0
5130 && (itemcnt == 3 || (itemcnt > 3 && items[3][0] == '#')))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005131 {
5132 if (do_sal)
5133 {
5134 /* SAL item (sounds-a-like)
5135 * Either one of the known keys or a from-to pair. */
5136 if (STRCMP(items[1], "followup") == 0)
5137 spin->si_followup = sal_to_bool(items[2]);
5138 else if (STRCMP(items[1], "collapse_result") == 0)
5139 spin->si_collapse = sal_to_bool(items[2]);
5140 else if (STRCMP(items[1], "remove_accents") == 0)
5141 spin->si_rem_accents = sal_to_bool(items[2]);
5142 else
5143 /* when "to" is "_" it means empty */
5144 add_fromto(spin, &spin->si_sal, items[1],
5145 STRCMP(items[2], "_") == 0 ? (char_u *)""
5146 : items[2]);
5147 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005148 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005149 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005150 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005151 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005152 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005153 }
5154 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005155 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005156 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005157 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005158 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005159 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005160 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005161 fname, lnum, items[0]);
5162 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005163 }
5164
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005165 if (fol != NULL || low != NULL || upp != NULL)
5166 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005167 if (spin->si_clear_chartab)
5168 {
5169 /* Clear the char type tables, don't want to use any of the
5170 * currently used spell properties. */
5171 init_spell_chartab();
5172 spin->si_clear_chartab = FALSE;
5173 }
5174
Bram Moolenaar3982c542005-06-08 21:56:31 +00005175 /*
5176 * Don't write a word table for an ASCII file, so that we don't check
5177 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005178 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00005179 * mb_get_class(), the list of chars in the file will be incomplete.
5180 */
5181 if (!spin->si_ascii
5182#ifdef FEAT_MBYTE
5183 && !enc_utf8
5184#endif
5185 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005186 {
5187 if (fol == NULL || low == NULL || upp == NULL)
5188 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
5189 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00005190 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005191 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005192
5193 vim_free(fol);
5194 vim_free(low);
5195 vim_free(upp);
5196 }
5197
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005198 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005199 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005200 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005201 aff_check_number(spin->si_compmax, compmax, "COMPOUNDMAX");
5202 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005203 }
5204
Bram Moolenaar6de68532005-08-24 22:08:48 +00005205 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005206 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005207 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
5208 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005209 }
5210
Bram Moolenaar6de68532005-08-24 22:08:48 +00005211 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005212 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005213 if (syllable == NULL)
5214 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
5215 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
5216 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005217 }
5218
Bram Moolenaar6de68532005-08-24 22:08:48 +00005219 if (compflags != NULL)
5220 process_compflags(spin, aff, compflags);
5221
5222 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005223 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005224 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005225 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005226 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005227 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005228 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005229 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005230 MSG(_("Too many posponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005231 }
5232
Bram Moolenaar6de68532005-08-24 22:08:48 +00005233 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005234 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005235 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
5236 spin->si_syllable = syllable;
5237 }
5238
5239 if (sofofrom != NULL || sofoto != NULL)
5240 {
5241 if (sofofrom == NULL || sofoto == NULL)
5242 smsg((char_u *)_("Missing SOFO%s line in %s"),
5243 sofofrom == NULL ? "FROM" : "TO", fname);
5244 else if (spin->si_sal.ga_len > 0)
5245 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005246 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005247 {
5248 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
5249 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
5250 spin->si_sofofr = sofofrom;
5251 spin->si_sofoto = sofoto;
5252 }
5253 }
5254
5255 if (midword != NULL)
5256 {
5257 aff_check_string(spin->si_midword, midword, "MIDWORD");
5258 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005259 }
5260
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005261 vim_free(pc);
5262 fclose(fd);
5263 return aff;
5264}
5265
5266/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005267 * Turn an affix flag name into a number, according to the FLAG type.
5268 * returns zero for failure.
5269 */
5270 static unsigned
5271affitem2flag(flagtype, item, fname, lnum)
5272 int flagtype;
5273 char_u *item;
5274 char_u *fname;
5275 int lnum;
5276{
5277 unsigned res;
5278 char_u *p = item;
5279
5280 res = get_affitem(flagtype, &p);
5281 if (res == 0)
5282 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005283 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005284 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
5285 fname, lnum, item);
5286 else
5287 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
5288 fname, lnum, item);
5289 }
5290 if (*p != NUL)
5291 {
5292 smsg((char_u *)_(e_affname), fname, lnum, item);
5293 return 0;
5294 }
5295
5296 return res;
5297}
5298
5299/*
5300 * Get one affix name from "*pp" and advance the pointer.
5301 * Returns zero for an error, still advances the pointer then.
5302 */
5303 static unsigned
5304get_affitem(flagtype, pp)
5305 int flagtype;
5306 char_u **pp;
5307{
5308 int res;
5309
Bram Moolenaar95529562005-08-25 21:21:38 +00005310 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005311 {
5312 if (!VIM_ISDIGIT(**pp))
5313 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005314 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005315 return 0;
5316 }
5317 res = getdigits(pp);
5318 }
5319 else
5320 {
5321#ifdef FEAT_MBYTE
5322 res = mb_ptr2char_adv(pp);
5323#else
5324 res = *(*pp)++;
5325#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00005326 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00005327 && res >= 'A' && res <= 'Z'))
5328 {
5329 if (**pp == NUL)
5330 return 0;
5331#ifdef FEAT_MBYTE
5332 res = mb_ptr2char_adv(pp) + (res << 16);
5333#else
5334 res = *(*pp)++ + (res << 16);
5335#endif
5336 }
5337 }
5338 return res;
5339}
5340
5341/*
5342 * Process the "compflags" string used in an affix file and append it to
5343 * spin->si_compflags.
5344 * The processing involves changing the affix names to ID numbers, so that
5345 * they fit in one byte.
5346 */
5347 static void
5348process_compflags(spin, aff, compflags)
5349 spellinfo_T *spin;
5350 afffile_T *aff;
5351 char_u *compflags;
5352{
5353 char_u *p;
5354 char_u *prevp;
5355 unsigned flag;
5356 compitem_T *ci;
5357 int id;
5358 int len;
5359 char_u *tp;
5360 char_u key[AH_KEY_LEN];
5361 hashitem_T *hi;
5362
5363 /* Make room for the old and the new compflags, concatenated with a / in
5364 * between. Processing it makes it shorter, but we don't know by how
5365 * much, thus allocate the maximum. */
5366 len = STRLEN(compflags) + 1;
5367 if (spin->si_compflags != NULL)
5368 len += STRLEN(spin->si_compflags) + 1;
5369 p = getroom(spin, len, FALSE);
5370 if (p == NULL)
5371 return;
5372 if (spin->si_compflags != NULL)
5373 {
5374 STRCPY(p, spin->si_compflags);
5375 STRCAT(p, "/");
5376 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005377 spin->si_compflags = p;
5378 tp = p + STRLEN(p);
5379
5380 for (p = compflags; *p != NUL; )
5381 {
5382 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
5383 /* Copy non-flag characters directly. */
5384 *tp++ = *p++;
5385 else
5386 {
5387 /* First get the flag number, also checks validity. */
5388 prevp = p;
5389 flag = get_affitem(aff->af_flagtype, &p);
5390 if (flag != 0)
5391 {
5392 /* Find the flag in the hashtable. If it was used before, use
5393 * the existing ID. Otherwise add a new entry. */
5394 vim_strncpy(key, prevp, p - prevp);
5395 hi = hash_find(&aff->af_comp, key);
5396 if (!HASHITEM_EMPTY(hi))
5397 id = HI2CI(hi)->ci_newID;
5398 else
5399 {
5400 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
5401 if (ci == NULL)
5402 break;
5403 STRCPY(ci->ci_key, key);
5404 ci->ci_flag = flag;
5405 /* Avoid using a flag ID that has a special meaning in a
5406 * regexp (also inside []). */
5407 do
5408 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005409 check_renumber(spin);
5410 id = spin->si_newcompID--;
5411 } while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005412 ci->ci_newID = id;
5413 hash_add(&aff->af_comp, ci->ci_key);
5414 }
5415 *tp++ = id;
5416 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005417 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005418 ++p;
5419 }
5420 }
5421
5422 *tp = NUL;
5423}
5424
5425/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005426 * Check that the new IDs for postponed affixes and compounding don't overrun
5427 * each other. We have almost 255 available, but start at 0-127 to avoid
5428 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
5429 * When that is used up an error message is given.
5430 */
5431 static void
5432check_renumber(spin)
5433 spellinfo_T *spin;
5434{
5435 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
5436 {
5437 spin->si_newprefID = 127;
5438 spin->si_newcompID = 255;
5439 }
5440}
5441
5442/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005443 * Return TRUE if flag "flag" appears in affix list "afflist".
5444 */
5445 static int
5446flag_in_afflist(flagtype, afflist, flag)
5447 int flagtype;
5448 char_u *afflist;
5449 unsigned flag;
5450{
5451 char_u *p;
5452 unsigned n;
5453
5454 switch (flagtype)
5455 {
5456 case AFT_CHAR:
5457 return vim_strchr(afflist, flag) != NULL;
5458
Bram Moolenaar95529562005-08-25 21:21:38 +00005459 case AFT_CAPLONG:
5460 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00005461 for (p = afflist; *p != NUL; )
5462 {
5463#ifdef FEAT_MBYTE
5464 n = mb_ptr2char_adv(&p);
5465#else
5466 n = *p++;
5467#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00005468 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00005469 && *p != NUL)
5470#ifdef FEAT_MBYTE
5471 n = mb_ptr2char_adv(&p) + (n << 16);
5472#else
5473 n = *p++ + (n << 16);
5474#endif
5475 if (n == flag)
5476 return TRUE;
5477 }
5478 break;
5479
Bram Moolenaar95529562005-08-25 21:21:38 +00005480 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00005481 for (p = afflist; *p != NUL; )
5482 {
5483 n = getdigits(&p);
5484 if (n == flag)
5485 return TRUE;
5486 if (*p != NUL) /* skip over comma */
5487 ++p;
5488 }
5489 break;
5490 }
5491 return FALSE;
5492}
5493
5494/*
5495 * Give a warning when "spinval" and "affval" numbers are set and not the same.
5496 */
5497 static void
5498aff_check_number(spinval, affval, name)
5499 int spinval;
5500 int affval;
5501 char *name;
5502{
5503 if (spinval != 0 && spinval != affval)
5504 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
5505}
5506
5507/*
5508 * Give a warning when "spinval" and "affval" strings are set and not the same.
5509 */
5510 static void
5511aff_check_string(spinval, affval, name)
5512 char_u *spinval;
5513 char_u *affval;
5514 char *name;
5515{
5516 if (spinval != NULL && STRCMP(spinval, affval) != 0)
5517 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
5518}
5519
5520/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005521 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
5522 * NULL as equal.
5523 */
5524 static int
5525str_equal(s1, s2)
5526 char_u *s1;
5527 char_u *s2;
5528{
5529 if (s1 == NULL || s2 == NULL)
5530 return s1 == s2;
5531 return STRCMP(s1, s2) == 0;
5532}
5533
5534/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005535 * Add a from-to item to "gap". Used for REP and SAL items.
5536 * They are stored case-folded.
5537 */
5538 static void
5539add_fromto(spin, gap, from, to)
5540 spellinfo_T *spin;
5541 garray_T *gap;
5542 char_u *from;
5543 char_u *to;
5544{
5545 fromto_T *ftp;
5546 char_u word[MAXWLEN];
5547
5548 if (ga_grow(gap, 1) == OK)
5549 {
5550 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
5551 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005552 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005553 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005554 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005555 ++gap->ga_len;
5556 }
5557}
5558
5559/*
5560 * Convert a boolean argument in a SAL line to TRUE or FALSE;
5561 */
5562 static int
5563sal_to_bool(s)
5564 char_u *s;
5565{
5566 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
5567}
5568
5569/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00005570 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
5571 * When "s" is NULL FALSE is returned.
5572 */
5573 static int
5574has_non_ascii(s)
5575 char_u *s;
5576{
5577 char_u *p;
5578
5579 if (s != NULL)
5580 for (p = s; *p != NUL; ++p)
5581 if (*p >= 128)
5582 return TRUE;
5583 return FALSE;
5584}
5585
5586/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005587 * Free the structure filled by spell_read_aff().
5588 */
5589 static void
5590spell_free_aff(aff)
5591 afffile_T *aff;
5592{
5593 hashtab_T *ht;
5594 hashitem_T *hi;
5595 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005596 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005597 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005598
5599 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005600
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005601 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005602 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
5603 {
5604 todo = ht->ht_used;
5605 for (hi = ht->ht_array; todo > 0; ++hi)
5606 {
5607 if (!HASHITEM_EMPTY(hi))
5608 {
5609 --todo;
5610 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005611 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
5612 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005613 }
5614 }
5615 if (ht == &aff->af_suff)
5616 break;
5617 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005618
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005619 hash_clear(&aff->af_pref);
5620 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005621 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005622}
5623
5624/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005625 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005626 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005627 */
5628 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005629spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005630 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005631 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005632 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005633{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005634 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005635 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005636 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005637 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005638 char_u store_afflist[MAXWLEN];
5639 int pfxlen;
5640 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005641 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005642 char_u *pc;
5643 char_u *w;
5644 int l;
5645 hash_T hash;
5646 hashitem_T *hi;
5647 FILE *fd;
5648 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005649 int non_ascii = 0;
5650 int retval = OK;
5651 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005652 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005653 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005654
Bram Moolenaar51485f02005-06-04 21:55:20 +00005655 /*
5656 * Open the file.
5657 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005658 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005659 if (fd == NULL)
5660 {
5661 EMSG2(_(e_notopen), fname);
5662 return FAIL;
5663 }
5664
Bram Moolenaar51485f02005-06-04 21:55:20 +00005665 /* The hashtable is only used to detect duplicated words. */
5666 hash_init(&ht);
5667
Bram Moolenaarb765d632005-06-07 21:00:02 +00005668 if (spin->si_verbose || p_verbose > 2)
5669 {
5670 if (!spin->si_verbose)
5671 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005672 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005673 out_flush();
5674 if (!spin->si_verbose)
5675 verbose_leave();
5676 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005677
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005678 /* start with a message for the first line */
5679 spin->si_msg_count = 999999;
5680
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005681 /* Read and ignore the first line: word count. */
5682 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005683 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005684 EMSG2(_("E760: No word count in %s"), fname);
5685
5686 /*
5687 * Read all the lines in the file one by one.
5688 * The words are converted to 'encoding' here, before being added to
5689 * the hashtable.
5690 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005691 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005692 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005693 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005694 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005695 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005696 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005697
Bram Moolenaar51485f02005-06-04 21:55:20 +00005698 /* Remove CR, LF and white space from the end. White space halfway
5699 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005700 l = STRLEN(line);
5701 while (l > 0 && line[l - 1] <= ' ')
5702 --l;
5703 if (l == 0)
5704 continue; /* empty line */
5705 line[l] = NUL;
5706
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005707 /* Find the optional affix names. Replace the SLASH character by a
5708 * slash. */
5709 afflist = NULL;
5710 for (p = line; *p != NUL; mb_ptr_adv(p))
5711 {
5712 if (*p == affile->af_slash)
5713 *p = '/';
5714 else if (*p == '/')
5715 {
5716 *p = NUL;
5717 afflist = p + 1;
5718 break;
5719 }
5720 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005721
5722 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
5723 if (spin->si_ascii && has_non_ascii(line))
5724 {
5725 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005726 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005727 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005728
Bram Moolenaarb765d632005-06-07 21:00:02 +00005729#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005730 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005731 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005732 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005733 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005734 if (pc == NULL)
5735 {
5736 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5737 fname, lnum, line);
5738 continue;
5739 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005740 w = pc;
5741 }
5742 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005743#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005744 {
5745 pc = NULL;
5746 w = line;
5747 }
5748
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005749 /* This takes time, print a message every 10000 words. */
5750 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005751 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005752 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005753 vim_snprintf((char *)message, sizeof(message),
5754 _("line %6d, word %6d - %s"),
5755 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
5756 msg_start();
5757 msg_puts_long_attr(message, 0);
5758 msg_clr_eos();
5759 msg_didout = FALSE;
5760 msg_col = 0;
5761 out_flush();
5762 }
5763
Bram Moolenaar51485f02005-06-04 21:55:20 +00005764 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005765 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005766 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005767 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005768 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005769 if (retval == FAIL)
5770 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005771
Bram Moolenaar51485f02005-06-04 21:55:20 +00005772 hash = hash_hash(dw);
5773 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005774 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005775 {
5776 if (p_verbose > 0)
5777 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005778 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005779 else if (duplicate == 0)
5780 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
5781 fname, lnum, dw);
5782 ++duplicate;
5783 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005784 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00005785 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005786
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005787 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005788 store_afflist[0] = NUL;
5789 pfxlen = 0;
5790 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005791 if (afflist != NULL)
5792 {
5793 /* Check for affix name that stands for keep-case word and stands
5794 * for rare word (if defined). */
Bram Moolenaar371baa92005-12-29 22:43:53 +00005795 if (affile->af_keepcase != 0 && flag_in_afflist(
5796 affile->af_flagtype, afflist, affile->af_keepcase))
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005797 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar371baa92005-12-29 22:43:53 +00005798 if (affile->af_rare != 0 && flag_in_afflist(
5799 affile->af_flagtype, afflist, affile->af_rare))
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005800 flags |= WF_RARE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005801 if (affile->af_bad != 0 && flag_in_afflist(
5802 affile->af_flagtype, afflist, affile->af_bad))
Bram Moolenaar0c405862005-06-22 22:26:26 +00005803 flags |= WF_BANNED;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005804 if (affile->af_needaffix != 0 && flag_in_afflist(
5805 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00005806 need_affix = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005807 if (affile->af_needcomp != 0 && flag_in_afflist(
5808 affile->af_flagtype, afflist, affile->af_needcomp))
5809 flags |= WF_NEEDCOMP;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005810
5811 if (affile->af_pfxpostpone)
5812 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005813 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005814
Bram Moolenaar5195e452005-08-19 20:32:47 +00005815 if (spin->si_compflags != NULL)
5816 /* Need to store the list of compound flags with the word.
5817 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005818 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005819 }
5820
Bram Moolenaar51485f02005-06-04 21:55:20 +00005821 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005822 if (store_word(spin, dw, flags, spin->si_region,
5823 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005824 retval = FAIL;
5825
5826 if (afflist != NULL)
5827 {
5828 /* Find all matching suffixes and add the resulting words.
5829 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005830 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005831 &affile->af_suff, &affile->af_pref,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005832 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005833 retval = FAIL;
5834
5835 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005836 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005837 &affile->af_pref, NULL,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005838 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005839 retval = FAIL;
5840 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005841 }
5842
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005843 if (duplicate > 0)
5844 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005845 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005846 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
5847 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005848 hash_clear(&ht);
5849
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005850 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005851 return retval;
5852}
5853
5854/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005855 * Get the list of prefix IDs from the affix list "afflist".
5856 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00005857 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
5858 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005859 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005860 static int
5861get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005862 afffile_T *affile;
5863 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005864 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005865{
5866 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005867 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005868 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005869 int id;
5870 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005871 hashitem_T *hi;
5872
Bram Moolenaar6de68532005-08-24 22:08:48 +00005873 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005874 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005875 prevp = p;
5876 if (get_affitem(affile->af_flagtype, &p) != 0)
5877 {
5878 /* A flag is a postponed prefix flag if it appears in "af_pref"
5879 * and it's ID is not zero. */
5880 vim_strncpy(key, prevp, p - prevp);
5881 hi = hash_find(&affile->af_pref, key);
5882 if (!HASHITEM_EMPTY(hi))
5883 {
5884 id = HI2AH(hi)->ah_newID;
5885 if (id != 0)
5886 store_afflist[cnt++] = id;
5887 }
5888 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005889 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005890 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005891 }
5892
Bram Moolenaar5195e452005-08-19 20:32:47 +00005893 store_afflist[cnt] = NUL;
5894 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005895}
5896
5897/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005898 * Get the list of compound IDs from the affix list "afflist" that are used
5899 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00005900 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005901 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005902 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00005903get_compflags(affile, afflist, store_afflist)
5904 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005905 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005906 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005907{
5908 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005909 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005910 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005911 char_u key[AH_KEY_LEN];
5912 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005913
Bram Moolenaar6de68532005-08-24 22:08:48 +00005914 for (p = afflist; *p != NUL; )
5915 {
5916 prevp = p;
5917 if (get_affitem(affile->af_flagtype, &p) != 0)
5918 {
5919 /* A flag is a compound flag if it appears in "af_comp". */
5920 vim_strncpy(key, prevp, p - prevp);
5921 hi = hash_find(&affile->af_comp, key);
5922 if (!HASHITEM_EMPTY(hi))
5923 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
5924 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005925 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005926 ++p;
5927 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005928
Bram Moolenaar5195e452005-08-19 20:32:47 +00005929 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005930}
5931
5932/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005933 * Apply affixes to a word and store the resulting words.
5934 * "ht" is the hashtable with affentry_T that need to be applied, either
5935 * prefixes or suffixes.
5936 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
5937 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005938 *
5939 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005940 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005941 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00005942store_aff_word(spin, word, afflist, affile, ht, xht, comb, flags,
5943 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005944 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005945 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005946 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005947 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005948 hashtab_T *ht;
5949 hashtab_T *xht;
5950 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005951 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005952 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005953 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
5954 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005955{
5956 int todo;
5957 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005958 affheader_T *ah;
5959 affentry_T *ae;
5960 regmatch_T regmatch;
5961 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005962 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005963 int i;
5964 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005965 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005966 char_u *use_pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005967 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00005968 size_t wordlen = STRLEN(word);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005969
Bram Moolenaar51485f02005-06-04 21:55:20 +00005970 todo = ht->ht_used;
5971 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005972 {
5973 if (!HASHITEM_EMPTY(hi))
5974 {
5975 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005976 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00005977
Bram Moolenaar51485f02005-06-04 21:55:20 +00005978 /* Check that the affix combines, if required, and that the word
5979 * supports this affix. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005980 if ((!comb || ah->ah_combine) && flag_in_afflist(
5981 affile->af_flagtype, afflist, ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00005982 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005983 /* Loop over all affix entries with this name. */
5984 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005985 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005986 /* Check the condition. It's not logical to match case
5987 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005988 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005989 * Another requirement from Myspell is that the chop
5990 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005991 * For prefixes, when "PFXPOSTPONE" was used, only do
5992 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005993 regmatch.regprog = ae->ae_prog;
5994 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005995 if ((xht != NULL || !affile->af_pfxpostpone
5996 || ae->ae_chop != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005997 && (ae->ae_chop == NULL
5998 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005999 && (ae->ae_prog == NULL
6000 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006001 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006002 /* Match. Remove the chop and add the affix. */
6003 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006004 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006005 /* prefix: chop/add at the start of the word */
6006 if (ae->ae_add == NULL)
6007 *newword = NUL;
6008 else
6009 STRCPY(newword, ae->ae_add);
6010 p = word;
6011 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006012 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006013 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006014#ifdef FEAT_MBYTE
6015 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006016 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006017 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006018 for ( ; i > 0; --i)
6019 mb_ptr_adv(p);
6020 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006021 else
6022#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006023 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006024 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006025 STRCAT(newword, p);
6026 }
6027 else
6028 {
6029 /* suffix: chop/add at the end of the word */
6030 STRCPY(newword, word);
6031 if (ae->ae_chop != NULL)
6032 {
6033 /* Remove chop string. */
6034 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006035 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006036 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006037 mb_ptr_back(newword, p);
6038 *p = NUL;
6039 }
6040 if (ae->ae_add != NULL)
6041 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006042 }
6043
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006044 /* Obey the "rare" flag of the affix. */
6045 if (ae->ae_rare)
6046 use_flags = flags | WF_RARE;
6047 else
6048 use_flags = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006049
6050 /* Obey the "nocomp" flag of the affix: don't use the
6051 * compound flags. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006052 use_pfxlist = pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006053 if (ae->ae_nocomp && pfxlist != NULL)
6054 {
6055 vim_strncpy(pfx_pfxlist, pfxlist, pfxlen);
6056 use_pfxlist = pfx_pfxlist;
6057 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006058
6059 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00006060 if (spin->si_prefroot != NULL
6061 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006062 {
6063 /* ... add a flag to indicate an affix was used. */
6064 use_flags |= WF_HAS_AFF;
6065
6066 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00006067 * affixes is not allowed. But do use the
6068 * compound flags after them. */
6069 if ((!ah->ah_combine || comb) && pfxlist != NULL)
6070 use_pfxlist += pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006071 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006072
Bram Moolenaar51485f02005-06-04 21:55:20 +00006073 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006074 if (store_word(spin, newword, use_flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006075 spin->si_region, use_pfxlist, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006076 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006077
Bram Moolenaar51485f02005-06-04 21:55:20 +00006078 /* When added a suffix and combining is allowed also
6079 * try adding prefixes additionally. */
6080 if (xht != NULL && ah->ah_combine)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006081 if (store_aff_word(spin, newword, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006082 xht, NULL, TRUE,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006083 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006084 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006085 }
6086 }
6087 }
6088 }
6089 }
6090
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006091 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006092}
6093
6094/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006095 * Read a file with a list of words.
6096 */
6097 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006098spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006099 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006100 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006101{
6102 FILE *fd;
6103 long lnum = 0;
6104 char_u rline[MAXLINELEN];
6105 char_u *line;
6106 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006107 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006108 int l;
6109 int retval = OK;
6110 int did_word = FALSE;
6111 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006112 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006113 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006114
6115 /*
6116 * Open the file.
6117 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006118 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006119 if (fd == NULL)
6120 {
6121 EMSG2(_(e_notopen), fname);
6122 return FAIL;
6123 }
6124
Bram Moolenaarb765d632005-06-07 21:00:02 +00006125 if (spin->si_verbose || p_verbose > 2)
6126 {
6127 if (!spin->si_verbose)
6128 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006129 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006130 out_flush();
6131 if (!spin->si_verbose)
6132 verbose_leave();
6133 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006134
6135 /*
6136 * Read all the lines in the file one by one.
6137 */
6138 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
6139 {
6140 line_breakcheck();
6141 ++lnum;
6142
6143 /* Skip comment lines. */
6144 if (*rline == '#')
6145 continue;
6146
6147 /* Remove CR, LF and white space from the end. */
6148 l = STRLEN(rline);
6149 while (l > 0 && rline[l - 1] <= ' ')
6150 --l;
6151 if (l == 0)
6152 continue; /* empty or blank line */
6153 rline[l] = NUL;
6154
6155 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
6156 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006157#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00006158 if (spin->si_conv.vc_type != CONV_NONE)
6159 {
6160 pc = string_convert(&spin->si_conv, rline, NULL);
6161 if (pc == NULL)
6162 {
6163 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6164 fname, lnum, rline);
6165 continue;
6166 }
6167 line = pc;
6168 }
6169 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006170#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006171 {
6172 pc = NULL;
6173 line = rline;
6174 }
6175
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006176 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00006177 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006178 ++line;
6179 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006180 {
6181 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006182 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
6183 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006184 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006185 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
6186 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006187 else
6188 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006189#ifdef FEAT_MBYTE
6190 char_u *enc;
6191
Bram Moolenaar51485f02005-06-04 21:55:20 +00006192 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006193 line += 10;
6194 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006195 if (enc != NULL && !spin->si_ascii
6196 && convert_setup(&spin->si_conv, enc,
6197 p_enc) == FAIL)
6198 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00006199 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006200 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006201 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006202#else
6203 smsg((char_u *)_("Conversion in %s not supported"), fname);
6204#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006205 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006206 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006207 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006208
Bram Moolenaar3982c542005-06-08 21:56:31 +00006209 if (STRNCMP(line, "regions=", 8) == 0)
6210 {
6211 if (spin->si_region_count > 1)
6212 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
6213 fname, lnum, line);
6214 else
6215 {
6216 line += 8;
6217 if (STRLEN(line) > 16)
6218 smsg((char_u *)_("Too many regions in %s line %d: %s"),
6219 fname, lnum, line);
6220 else
6221 {
6222 spin->si_region_count = STRLEN(line) / 2;
6223 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006224
6225 /* Adjust the mask for a word valid in all regions. */
6226 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006227 }
6228 }
6229 continue;
6230 }
6231
Bram Moolenaar7887d882005-07-01 22:33:52 +00006232 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
6233 fname, lnum, line - 1);
6234 continue;
6235 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006236
Bram Moolenaar7887d882005-07-01 22:33:52 +00006237 flags = 0;
6238 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006239
Bram Moolenaar7887d882005-07-01 22:33:52 +00006240 /* Check for flags and region after a slash. */
6241 p = vim_strchr(line, '/');
6242 if (p != NULL)
6243 {
6244 *p++ = NUL;
6245 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006246 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006247 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006248 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006249 else if (*p == '!') /* Bad, bad, wicked word. */
6250 flags |= WF_BANNED;
6251 else if (*p == '?') /* Rare word. */
6252 flags |= WF_RARE;
6253 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006254 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006255 if ((flags & WF_REGION) == 0) /* first one */
6256 regionmask = 0;
6257 flags |= WF_REGION;
6258
6259 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00006260 if (l > spin->si_region_count)
6261 {
6262 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00006263 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006264 break;
6265 }
6266 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006267 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00006268 else
6269 {
6270 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
6271 fname, lnum, p);
6272 break;
6273 }
6274 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006275 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006276 }
6277
6278 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6279 if (spin->si_ascii && has_non_ascii(line))
6280 {
6281 ++non_ascii;
6282 continue;
6283 }
6284
6285 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006286 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006287 {
6288 retval = FAIL;
6289 break;
6290 }
6291 did_word = TRUE;
6292 }
6293
6294 vim_free(pc);
6295 fclose(fd);
6296
Bram Moolenaarb765d632005-06-07 21:00:02 +00006297 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
6298 {
6299 if (p_verbose > 2)
6300 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00006301 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
6302 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006303 if (p_verbose > 2)
6304 verbose_leave();
6305 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006306 return retval;
6307}
6308
6309/*
6310 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006311 * This avoids calling free() for every little struct we use (and keeping
6312 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00006313 * The memory is cleared to all zeros.
6314 * Returns NULL when out of memory.
6315 */
6316 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006317getroom(spin, len, align)
6318 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006319 size_t len; /* length needed */
6320 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006321{
6322 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006323 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006324
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006325 if (align && bl != NULL)
6326 /* Round size up for alignment. On some systems structures need to be
6327 * aligned to the size of a pointer (e.g., SPARC). */
6328 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
6329 & ~(sizeof(char *) - 1);
6330
Bram Moolenaar51485f02005-06-04 21:55:20 +00006331 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
6332 {
6333 /* Allocate a block of memory. This is not freed until much later. */
6334 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
6335 if (bl == NULL)
6336 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006337 bl->sb_next = spin->si_blocks;
6338 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006339 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006340 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006341 }
6342
6343 p = bl->sb_data + bl->sb_used;
6344 bl->sb_used += len;
6345
6346 return p;
6347}
6348
6349/*
6350 * Make a copy of a string into memory allocated with getroom().
6351 */
6352 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006353getroom_save(spin, s)
6354 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006355 char_u *s;
6356{
6357 char_u *sc;
6358
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006359 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006360 if (sc != NULL)
6361 STRCPY(sc, s);
6362 return sc;
6363}
6364
6365
6366/*
6367 * Free the list of allocated sblock_T.
6368 */
6369 static void
6370free_blocks(bl)
6371 sblock_T *bl;
6372{
6373 sblock_T *next;
6374
6375 while (bl != NULL)
6376 {
6377 next = bl->sb_next;
6378 vim_free(bl);
6379 bl = next;
6380 }
6381}
6382
6383/*
6384 * Allocate the root of a word tree.
6385 */
6386 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006387wordtree_alloc(spin)
6388 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006389{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006390 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006391}
6392
6393/*
6394 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006395 * Always store it in the case-folded tree. For a keep-case word this is
6396 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
6397 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006398 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006399 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
6400 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006401 */
6402 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00006403store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006404 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006405 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006406 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006407 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006408 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006409 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006410{
6411 int len = STRLEN(word);
6412 int ct = captype(word, word + len);
6413 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006414 int res = OK;
6415 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006416
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006417 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006418 for (p = pfxlist; res == OK; ++p)
6419 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006420 if (!need_affix || (p != NULL && *p != NUL))
6421 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006422 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006423 if (p == NULL || *p == NUL)
6424 break;
6425 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00006426 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006427
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006428 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00006429 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006430 for (p = pfxlist; res == OK; ++p)
6431 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006432 if (!need_affix || (p != NULL && *p != NUL))
6433 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006434 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006435 if (p == NULL || *p == NUL)
6436 break;
6437 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00006438 ++spin->si_keepwcount;
6439 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006440 return res;
6441}
6442
6443/*
6444 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006445 * When "flags" < 0 we are adding to the prefix tree where flags is used for
6446 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006447 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006448 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006449 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006450tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006451 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006452 char_u *word;
6453 wordnode_T *root;
6454 int flags;
6455 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006456 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006457{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006458 wordnode_T *node = root;
6459 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006460 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006461 wordnode_T **prev = NULL;
6462 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006463
Bram Moolenaar51485f02005-06-04 21:55:20 +00006464 /* Add each byte of the word to the tree, including the NUL at the end. */
6465 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006466 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006467 /* When there is more than one reference to this node we need to make
6468 * a copy, so that we can modify it. Copy the whole list of siblings
6469 * (we don't optimize for a partly shared list of siblings). */
6470 if (node != NULL && node->wn_refs > 1)
6471 {
6472 --node->wn_refs;
6473 copyprev = prev;
6474 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
6475 {
6476 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006477 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006478 if (np == NULL)
6479 return FAIL;
6480 np->wn_child = copyp->wn_child;
6481 if (np->wn_child != NULL)
6482 ++np->wn_child->wn_refs; /* child gets extra ref */
6483 np->wn_byte = copyp->wn_byte;
6484 if (np->wn_byte == NUL)
6485 {
6486 np->wn_flags = copyp->wn_flags;
6487 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006488 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006489 }
6490
6491 /* Link the new node in the list, there will be one ref. */
6492 np->wn_refs = 1;
6493 *copyprev = np;
6494 copyprev = &np->wn_sibling;
6495
6496 /* Let "node" point to the head of the copied list. */
6497 if (copyp == node)
6498 node = np;
6499 }
6500 }
6501
Bram Moolenaar51485f02005-06-04 21:55:20 +00006502 /* Look for the sibling that has the same character. They are sorted
6503 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006504 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006505 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006506 while (node != NULL
6507 && (node->wn_byte < word[i]
6508 || (node->wn_byte == NUL
6509 && (flags < 0
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006510 ? node->wn_affixID < affixID
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006511 : node->wn_flags < (flags & WN_MASK)
6512 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006513 && node->wn_affixID < affixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006514 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006515 prev = &node->wn_sibling;
6516 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006517 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006518 if (node == NULL
6519 || node->wn_byte != word[i]
6520 || (word[i] == NUL
6521 && (flags < 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006522 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006523 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006524 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006525 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006526 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006527 if (np == NULL)
6528 return FAIL;
6529 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006530
6531 /* If "node" is NULL this is a new child or the end of the sibling
6532 * list: ref count is one. Otherwise use ref count of sibling and
6533 * make ref count of sibling one (matters when inserting in front
6534 * of the list of siblings). */
6535 if (node == NULL)
6536 np->wn_refs = 1;
6537 else
6538 {
6539 np->wn_refs = node->wn_refs;
6540 node->wn_refs = 1;
6541 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006542 *prev = np;
6543 np->wn_sibling = node;
6544 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006545 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006546
Bram Moolenaar51485f02005-06-04 21:55:20 +00006547 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006548 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006549 node->wn_flags = flags;
6550 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006551 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006552 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00006553 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006554 prev = &node->wn_child;
6555 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006556 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006557#ifdef SPELL_PRINTTREE
6558 smsg("Added \"%s\"", word);
6559 spell_print_tree(root->wn_sibling);
6560#endif
6561
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006562 /* count nr of words added since last message */
6563 ++spin->si_msg_count;
6564
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006565 if (spin->si_compress_cnt > 1)
6566 {
6567 if (--spin->si_compress_cnt == 1)
6568 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006569 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006570 }
6571
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006572 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006573 * When we have allocated lots of memory we need to compress the word tree
6574 * to free up some room. But compression is slow, and we might actually
6575 * need that room, thus only compress in the following situations:
6576 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00006577 * "compress_start" blocks.
6578 * 2. When compressed before and used "compress_inc" blocks before
6579 * adding "compress_added" words (si_compress_cnt > 1).
6580 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006581 * (si_compress_cnt == 1) and the number of free nodes drops below the
6582 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006583 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006584#ifndef SPELL_PRINTTREE
6585 if (spin->si_compress_cnt == 1
6586 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00006587 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006588#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006589 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006590 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00006591 * when the freed up room has been used and another "compress_inc"
6592 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006593 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006594 spin->si_blocks_cnt -= compress_inc;
6595 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006596
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006597 if (spin->si_verbose)
6598 {
6599 msg_start();
6600 msg_puts((char_u *)_(msg_compressing));
6601 msg_clr_eos();
6602 msg_didout = FALSE;
6603 msg_col = 0;
6604 out_flush();
6605 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006606
6607 /* Compress both trees. Either they both have many nodes, which makes
6608 * compression useful, or one of them is small, which means
6609 * compression goes fast. */
6610 wordtree_compress(spin, spin->si_foldroot);
6611 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006612 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006613
6614 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006615}
6616
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006617/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00006618 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
6619 * Sets "sps_flags".
6620 */
6621 int
6622spell_check_msm()
6623{
6624 char_u *p = p_msm;
6625 long start = 0;
6626 long inc = 0;
6627 long added = 0;
6628
6629 if (!VIM_ISDIGIT(*p))
6630 return FAIL;
6631 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
6632 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
6633 if (*p != ',')
6634 return FAIL;
6635 ++p;
6636 if (!VIM_ISDIGIT(*p))
6637 return FAIL;
6638 inc = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
6639 if (*p != ',')
6640 return FAIL;
6641 ++p;
6642 if (!VIM_ISDIGIT(*p))
6643 return FAIL;
6644 added = getdigits(&p) * 1024;
6645 if (*p != NUL)
6646 return FAIL;
6647
6648 if (start == 0 || inc == 0 || added == 0 || inc > start)
6649 return FAIL;
6650
6651 compress_start = start;
6652 compress_inc = inc;
6653 compress_added = added;
6654 return OK;
6655}
6656
6657
6658/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006659 * Get a wordnode_T, either from the list of previously freed nodes or
6660 * allocate a new one.
6661 */
6662 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006663get_wordnode(spin)
6664 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006665{
6666 wordnode_T *n;
6667
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006668 if (spin->si_first_free == NULL)
6669 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006670 else
6671 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006672 n = spin->si_first_free;
6673 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006674 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006675 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006676 }
6677#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006678 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006679#endif
6680 return n;
6681}
6682
6683/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006684 * Decrement the reference count on a node (which is the head of a list of
6685 * siblings). If the reference count becomes zero free the node and its
6686 * siblings.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006687 */
6688 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006689deref_wordnode(spin, node)
6690 spellinfo_T *spin;
6691 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006692{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006693 wordnode_T *np;
6694
6695 if (--node->wn_refs == 0)
6696 for (np = node; np != NULL; np = np->wn_sibling)
6697 {
6698 if (np->wn_child != NULL)
6699 deref_wordnode(spin, np->wn_child);
6700 free_wordnode(spin, np);
6701 }
6702}
6703
6704/*
6705 * Free a wordnode_T for re-use later.
6706 * Only the "wn_child" field becomes invalid.
6707 */
6708 static void
6709free_wordnode(spin, n)
6710 spellinfo_T *spin;
6711 wordnode_T *n;
6712{
6713 n->wn_child = spin->si_first_free;
6714 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006715 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006716}
6717
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006718/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006719 * Compress a tree: find tails that are identical and can be shared.
6720 */
6721 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006722wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006723 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006724 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006725{
6726 hashtab_T ht;
6727 int n;
6728 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006729 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006730
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006731 /* Skip the root itself, it's not actually used. The first sibling is the
6732 * start of the tree. */
6733 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006734 {
6735 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006736 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006737
6738#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00006739 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006740#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00006741 {
6742 if (!spin->si_verbose)
6743 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006744 if (tot > 1000000)
6745 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006746 else if (tot == 0)
6747 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006748 else
6749 perc = (tot - n) * 100 / tot;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006750 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006751 n, tot, perc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006752 if (p_verbose > 2)
6753 verbose_leave();
6754 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006755#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006756 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006757#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006758 hash_clear(&ht);
6759 }
6760}
6761
6762/*
6763 * Compress a node, its siblings and its children, depth first.
6764 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006765 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006766 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006767node_compress(spin, node, ht, tot)
6768 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006769 wordnode_T *node;
6770 hashtab_T *ht;
6771 int *tot; /* total count of nodes before compressing,
6772 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006773{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006774 wordnode_T *np;
6775 wordnode_T *tp;
6776 wordnode_T *child;
6777 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006778 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006779 int len = 0;
6780 unsigned nr, n;
6781 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006782
Bram Moolenaar51485f02005-06-04 21:55:20 +00006783 /*
6784 * Go through the list of siblings. Compress each child and then try
6785 * finding an identical child to replace it.
6786 * Note that with "child" we mean not just the node that is pointed to,
6787 * but the whole list of siblings, of which the node is the first.
6788 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006789 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006790 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006791 ++len;
6792 if ((child = np->wn_child) != NULL)
6793 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006794 /* Compress the child. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006795 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006796
6797 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006798 hash = hash_hash(child->wn_u1.hashkey);
6799 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006800 tp = NULL;
6801 if (!HASHITEM_EMPTY(hi))
6802 {
6803 /* There are children with an identical hash value. Now check
6804 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006805 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006806 if (node_equal(child, tp))
6807 {
6808 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006809 * current one. This means the current child and all
6810 * its siblings is unlinked from the tree. */
6811 ++tp->wn_refs;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006812 deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006813 np->wn_child = tp;
6814 ++compressed;
6815 break;
6816 }
6817 if (tp == NULL)
6818 {
6819 /* No other child with this hash value equals the child of
6820 * the node, add it to the linked list after the first
6821 * item. */
6822 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006823 child->wn_u2.next = tp->wn_u2.next;
6824 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006825 }
6826 }
6827 else
6828 /* No other child has this hash value, add it to the
6829 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006830 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006831 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006832 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006833 *tot += len;
6834
6835 /*
6836 * Make a hash key for the node and its siblings, so that we can quickly
6837 * find a lookalike node. This must be done after compressing the sibling
6838 * list, otherwise the hash key would become invalid by the compression.
6839 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006840 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006841 nr = 0;
6842 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006843 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006844 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006845 /* end node: use wn_flags, wn_region and wn_affixID */
6846 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006847 else
6848 /* byte node: use the byte value and the child pointer */
6849 n = np->wn_byte + ((long_u)np->wn_child << 8);
6850 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006851 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006852
6853 /* Avoid NUL bytes, it terminates the hash key. */
6854 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006855 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006856 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006857 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006858 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006859 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006860 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006861 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
6862 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006863
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006864 /* Check for CTRL-C pressed now and then. */
6865 fast_breakcheck();
6866
Bram Moolenaar51485f02005-06-04 21:55:20 +00006867 return compressed;
6868}
6869
6870/*
6871 * Return TRUE when two nodes have identical siblings and children.
6872 */
6873 static int
6874node_equal(n1, n2)
6875 wordnode_T *n1;
6876 wordnode_T *n2;
6877{
6878 wordnode_T *p1;
6879 wordnode_T *p2;
6880
6881 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
6882 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
6883 if (p1->wn_byte != p2->wn_byte
6884 || (p1->wn_byte == NUL
6885 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006886 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006887 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006888 : (p1->wn_child != p2->wn_child)))
6889 break;
6890
6891 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006892}
6893
6894/*
6895 * Write a number to file "fd", MSB first, in "len" bytes.
6896 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006897 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006898put_bytes(fd, nr, len)
6899 FILE *fd;
6900 long_u nr;
6901 int len;
6902{
6903 int i;
6904
6905 for (i = len - 1; i >= 0; --i)
6906 putc((int)(nr >> (i * 8)), fd);
6907}
6908
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006909static int
6910#ifdef __BORLANDC__
6911_RTLENTRYF
6912#endif
6913rep_compare __ARGS((const void *s1, const void *s2));
6914
6915/*
6916 * Function given to qsort() to sort the REP items on "from" string.
6917 */
6918 static int
6919#ifdef __BORLANDC__
6920_RTLENTRYF
6921#endif
6922rep_compare(s1, s2)
6923 const void *s1;
6924 const void *s2;
6925{
6926 fromto_T *p1 = (fromto_T *)s1;
6927 fromto_T *p2 = (fromto_T *)s2;
6928
6929 return STRCMP(p1->ft_from, p2->ft_from);
6930}
6931
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006932/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00006933 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006934 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006935 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006936 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006937write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006938 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006939 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006940{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006941 FILE *fd;
6942 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006943 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006944 wordnode_T *tree;
6945 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006946 int i;
6947 int l;
6948 garray_T *gap;
6949 fromto_T *ftp;
6950 char_u *p;
6951 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006952 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006953
Bram Moolenaarb765d632005-06-07 21:00:02 +00006954 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006955 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006956 {
6957 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006958 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006959 }
6960
Bram Moolenaar5195e452005-08-19 20:32:47 +00006961 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006962 /* <fileID> */
6963 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006964 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006965 EMSG(_(e_write));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006966 retval = FAIL;
6967 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00006968 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006969
Bram Moolenaar5195e452005-08-19 20:32:47 +00006970 /*
6971 * <SECTIONS>: <section> ... <sectionend>
6972 */
6973
6974 /* SN_REGION: <regionname> ...
6975 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006976 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006977 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006978 putc(SN_REGION, fd); /* <sectionID> */
6979 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6980 l = spin->si_region_count * 2;
6981 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6982 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
6983 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006984 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006985 }
6986 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006987 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006988
Bram Moolenaar5195e452005-08-19 20:32:47 +00006989 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
6990 *
6991 * The table with character flags and the table for case folding.
6992 * This makes sure the same characters are recognized as word characters
6993 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006994 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006995 * 'encoding'.
6996 * Also skip this for an .add.spl file, the main spell file must contain
6997 * the table (avoids that it conflicts). File is shorter too.
6998 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006999 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007000 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007001 char_u folchars[128 * 8];
7002 int flags;
7003
Bram Moolenaard12a1322005-08-21 22:08:24 +00007004 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007005 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7006
7007 /* Form the <folchars> string first, we need to know its length. */
7008 l = 0;
7009 for (i = 128; i < 256; ++i)
7010 {
7011#ifdef FEAT_MBYTE
7012 if (has_mbyte)
7013 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
7014 else
7015#endif
7016 folchars[l++] = spelltab.st_fold[i];
7017 }
7018 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
7019
7020 fputc(128, fd); /* <charflagslen> */
7021 for (i = 128; i < 256; ++i)
7022 {
7023 flags = 0;
7024 if (spelltab.st_isw[i])
7025 flags |= CF_WORD;
7026 if (spelltab.st_isu[i])
7027 flags |= CF_UPPER;
7028 fputc(flags, fd); /* <charflags> */
7029 }
7030
7031 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
7032 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007033 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007034
Bram Moolenaar5195e452005-08-19 20:32:47 +00007035 /* SN_MIDWORD: <midword> */
7036 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007037 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007038 putc(SN_MIDWORD, fd); /* <sectionID> */
7039 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7040
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007041 i = STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007042 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007043 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
7044 }
7045
Bram Moolenaar5195e452005-08-19 20:32:47 +00007046 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
7047 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007048 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007049 putc(SN_PREFCOND, fd); /* <sectionID> */
7050 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7051
7052 l = write_spell_prefcond(NULL, &spin->si_prefcond);
7053 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7054
7055 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007056 }
7057
Bram Moolenaar5195e452005-08-19 20:32:47 +00007058 /* SN_REP: <repcount> <rep> ...
7059 * SN_SAL: <salflags> <salcount> <sal> ... */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007060
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007061 /* Sort the REP items. */
7062 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
7063 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007064
Bram Moolenaar5195e452005-08-19 20:32:47 +00007065 /* round 1: SN_REP section
7066 * round 2: SN_SAL section (unless SN_SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007067 for (round = 1; round <= 2; ++round)
7068 {
7069 if (round == 1)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007070 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007071 gap = &spin->si_rep;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007072 putc(SN_REP, fd); /* <sectionID> */
7073 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007074 else
7075 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007076 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
7077 /* using SN_SOFO section instead of SN_SAL */
7078 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007079 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007080 putc(SN_SAL, fd); /* <sectionID> */
7081 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007082
Bram Moolenaar5195e452005-08-19 20:32:47 +00007083 /* This is for making suggestions, section is not required. */
7084 putc(0, fd); /* <sectionflags> */
7085
7086 /* Compute the length of what follows. */
7087 l = 2; /* count <repcount> or <salcount> */
7088 for (i = 0; i < gap->ga_len; ++i)
7089 {
7090 ftp = &((fromto_T *)gap->ga_data)[i];
7091 l += 1 + STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
7092 l += 1 + STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
7093 }
7094 if (round == 2)
7095 ++l; /* count <salflags> */
7096 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7097
7098 if (round == 2)
7099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007100 i = 0;
7101 if (spin->si_followup)
7102 i |= SAL_F0LLOWUP;
7103 if (spin->si_collapse)
7104 i |= SAL_COLLAPSE;
7105 if (spin->si_rem_accents)
7106 i |= SAL_REM_ACCENTS;
7107 putc(i, fd); /* <salflags> */
7108 }
7109
7110 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
7111 for (i = 0; i < gap->ga_len; ++i)
7112 {
7113 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
7114 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
7115 ftp = &((fromto_T *)gap->ga_data)[i];
7116 for (rr = 1; rr <= 2; ++rr)
7117 {
7118 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
7119 l = STRLEN(p);
7120 putc(l, fd);
7121 fwrite(p, l, (size_t)1, fd);
7122 }
7123 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007124
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007125 }
7126
Bram Moolenaar5195e452005-08-19 20:32:47 +00007127 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
7128 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007129 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
7130 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007131 putc(SN_SOFO, fd); /* <sectionID> */
7132 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007133
7134 l = STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007135 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
7136 /* <sectionlen> */
7137
7138 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
7139 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007140
7141 l = STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007142 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
7143 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007144 }
7145
Bram Moolenaar5195e452005-08-19 20:32:47 +00007146 /* SN_MAP: <mapstr>
7147 * This is for making suggestions, section is not required. */
7148 if (spin->si_map.ga_len > 0)
7149 {
7150 putc(SN_MAP, fd); /* <sectionID> */
7151 putc(0, fd); /* <sectionflags> */
7152 l = spin->si_map.ga_len;
7153 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7154 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
7155 /* <mapstr> */
7156 }
7157
7158 /* SN_COMPOUND: compound info.
7159 * We don't mark it required, when not supported all compound words will
7160 * be bad words. */
7161 if (spin->si_compflags != NULL)
7162 {
7163 putc(SN_COMPOUND, fd); /* <sectionID> */
7164 putc(0, fd); /* <sectionflags> */
7165
7166 l = STRLEN(spin->si_compflags);
7167 put_bytes(fd, (long_u)(l + 3), 4); /* <sectionlen> */
7168 putc(spin->si_compmax, fd); /* <compmax> */
7169 putc(spin->si_compminlen, fd); /* <compminlen> */
7170 putc(spin->si_compsylmax, fd); /* <compsylmax> */
7171 /* <compflags> */
7172 fwrite(spin->si_compflags, (size_t)l, (size_t)1, fd);
7173 }
7174
Bram Moolenaar78622822005-08-23 21:00:13 +00007175 /* SN_NOBREAK: NOBREAK flag */
7176 if (spin->si_nobreak)
7177 {
7178 putc(SN_NOBREAK, fd); /* <sectionID> */
7179 putc(0, fd); /* <sectionflags> */
7180
7181 /* It's empty, the precense of the section flags the feature. */
7182 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
7183 }
7184
Bram Moolenaar5195e452005-08-19 20:32:47 +00007185 /* SN_SYLLABLE: syllable info.
7186 * We don't mark it required, when not supported syllables will not be
7187 * counted. */
7188 if (spin->si_syllable != NULL)
7189 {
7190 putc(SN_SYLLABLE, fd); /* <sectionID> */
7191 putc(0, fd); /* <sectionflags> */
7192
7193 l = STRLEN(spin->si_syllable);
7194 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7195 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
7196 }
7197
7198 /* end of <SECTIONS> */
7199 putc(SN_END, fd); /* <sectionend> */
7200
Bram Moolenaar50cde822005-06-05 21:54:54 +00007201
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007202 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007203 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007204 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007205 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007206 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007207 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007208 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007209 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007210 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007211 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007212 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007213 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007214
Bram Moolenaar0c405862005-06-22 22:26:26 +00007215 /* Clear the index and wnode fields in the tree. */
7216 clear_node(tree);
7217
Bram Moolenaar51485f02005-06-04 21:55:20 +00007218 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00007219 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00007220 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007221 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007222
Bram Moolenaar51485f02005-06-04 21:55:20 +00007223 /* number of nodes in 4 bytes */
7224 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00007225 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007226
Bram Moolenaar51485f02005-06-04 21:55:20 +00007227 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007228 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007229 }
7230
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007231 /* Write another byte to check for errors. */
7232 if (putc(0, fd) == EOF)
7233 retval = FAIL;
7234
7235 if (fclose(fd) == EOF)
7236 retval = FAIL;
7237
7238 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007239}
7240
7241/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007242 * Clear the index and wnode fields of "node", it siblings and its
7243 * children. This is needed because they are a union with other items to save
7244 * space.
7245 */
7246 static void
7247clear_node(node)
7248 wordnode_T *node;
7249{
7250 wordnode_T *np;
7251
7252 if (node != NULL)
7253 for (np = node; np != NULL; np = np->wn_sibling)
7254 {
7255 np->wn_u1.index = 0;
7256 np->wn_u2.wnode = NULL;
7257
7258 if (np->wn_byte != NUL)
7259 clear_node(np->wn_child);
7260 }
7261}
7262
7263
7264/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007265 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007266 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00007267 * This first writes the list of possible bytes (siblings). Then for each
7268 * byte recursively write the children.
7269 *
7270 * NOTE: The code here must match the code in read_tree(), since assumptions
7271 * are made about the indexes (so that we don't have to write them in the
7272 * file).
7273 *
7274 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007275 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007276 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00007277put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007278 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007279 wordnode_T *node;
7280 int index;
7281 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007282 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007283{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007284 int newindex = index;
7285 int siblingcount = 0;
7286 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007287 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007288
Bram Moolenaar51485f02005-06-04 21:55:20 +00007289 /* If "node" is zero the tree is empty. */
7290 if (node == NULL)
7291 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007292
Bram Moolenaar51485f02005-06-04 21:55:20 +00007293 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007294 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007295
7296 /* Count the number of siblings. */
7297 for (np = node; np != NULL; np = np->wn_sibling)
7298 ++siblingcount;
7299
7300 /* Write the sibling count. */
7301 if (fd != NULL)
7302 putc(siblingcount, fd); /* <siblingcount> */
7303
7304 /* Write each sibling byte and optionally extra info. */
7305 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007306 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007307 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007308 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007309 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007310 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007311 /* For a NUL byte (end of word) write the flags etc. */
7312 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007313 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007314 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007315 * associated condition nr (stored in wn_region). The
7316 * byte value is misused to store the "rare" and "not
7317 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00007318 if (np->wn_flags == (short_u)PFX_FLAGS)
7319 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007320 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00007321 {
7322 putc(BY_FLAGS, fd); /* <byte> */
7323 putc(np->wn_flags, fd); /* <pflags> */
7324 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007325 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007326 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007327 }
7328 else
7329 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007330 /* For word trees we write the flag/region items. */
7331 flags = np->wn_flags;
7332 if (regionmask != 0 && np->wn_region != regionmask)
7333 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007334 if (np->wn_affixID != 0)
7335 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007336 if (flags == 0)
7337 {
7338 /* word without flags or region */
7339 putc(BY_NOFLAGS, fd); /* <byte> */
7340 }
7341 else
7342 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007343 if (np->wn_flags >= 0x100)
7344 {
7345 putc(BY_FLAGS2, fd); /* <byte> */
7346 putc(flags, fd); /* <flags> */
7347 putc((unsigned)flags >> 8, fd); /* <flags2> */
7348 }
7349 else
7350 {
7351 putc(BY_FLAGS, fd); /* <byte> */
7352 putc(flags, fd); /* <flags> */
7353 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007354 if (flags & WF_REGION)
7355 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007356 if (flags & WF_AFX)
7357 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007358 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007359 }
7360 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007361 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007362 else
7363 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00007364 if (np->wn_child->wn_u1.index != 0
7365 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007366 {
7367 /* The child is written elsewhere, write the reference. */
7368 if (fd != NULL)
7369 {
7370 putc(BY_INDEX, fd); /* <byte> */
7371 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007372 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007373 }
7374 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00007375 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007376 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007377 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007378
Bram Moolenaar51485f02005-06-04 21:55:20 +00007379 if (fd != NULL)
7380 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
7381 {
7382 EMSG(_(e_write));
7383 return 0;
7384 }
7385 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007386 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007387
7388 /* Space used in the array when reading: one for each sibling and one for
7389 * the count. */
7390 newindex += siblingcount + 1;
7391
7392 /* Recursively dump the children of each sibling. */
7393 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007394 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
7395 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007396 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007397
7398 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007399}
7400
7401
7402/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00007403 * ":mkspell [-ascii] outfile infile ..."
7404 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007405 */
7406 void
7407ex_mkspell(eap)
7408 exarg_T *eap;
7409{
7410 int fcount;
7411 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007412 char_u *arg = eap->arg;
7413 int ascii = FALSE;
7414
7415 if (STRNCMP(arg, "-ascii", 6) == 0)
7416 {
7417 ascii = TRUE;
7418 arg = skipwhite(arg + 6);
7419 }
7420
7421 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
7422 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
7423 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007424 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007425 FreeWild(fcount, fnames);
7426 }
7427}
7428
7429/*
7430 * Create a Vim spell file from one or more word lists.
7431 * "fnames[0]" is the output file name.
7432 * "fnames[fcount - 1]" is the last input file name.
7433 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
7434 * and ".spl" is appended to make the output file name.
7435 */
7436 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007437mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007438 int fcount;
7439 char_u **fnames;
7440 int ascii; /* -ascii argument given */
7441 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007442 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007443{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007444 char_u fname[MAXPATHL];
7445 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00007446 char_u **innames;
7447 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007448 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007449 int i;
7450 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007451 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007452 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007453 spellinfo_T spin;
7454
7455 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007456 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007457 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007458 spin.si_followup = TRUE;
7459 spin.si_rem_accents = TRUE;
7460 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
7461 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
7462 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007463 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007464 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007465
Bram Moolenaarb765d632005-06-07 21:00:02 +00007466 /* default: fnames[0] is output file, following are input files */
7467 innames = &fnames[1];
7468 incount = fcount - 1;
7469
7470 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00007471 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007472 len = STRLEN(fnames[0]);
7473 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
7474 {
7475 /* For ":mkspell path/en.latin1.add" output file is
7476 * "path/en.latin1.add.spl". */
7477 innames = &fnames[0];
7478 incount = 1;
7479 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
7480 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007481 else if (fcount == 1)
7482 {
7483 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
7484 innames = &fnames[0];
7485 incount = 1;
7486 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
7487 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
7488 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007489 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
7490 {
7491 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007492 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007493 }
7494 else
7495 /* Name should be language, make the file name from it. */
7496 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
7497 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
7498
7499 /* Check for .ascii.spl. */
7500 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
7501 spin.si_ascii = TRUE;
7502
7503 /* Check for .add.spl. */
7504 if (strstr((char *)gettail(wfname), ".add.") != NULL)
7505 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00007506 }
7507
Bram Moolenaarb765d632005-06-07 21:00:02 +00007508 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007509 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007510 else if (vim_strchr(gettail(wfname), '_') != NULL)
7511 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00007512 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007513 EMSG(_("E754: Only up to 8 regions supported"));
7514 else
7515 {
7516 /* Check for overwriting before doing things that may take a lot of
7517 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007518 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007519 {
7520 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00007521 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007522 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007523 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007524 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007525 EMSG2(_(e_isadir2), wfname);
7526 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007527 }
7528
7529 /*
7530 * Init the aff and dic pointers.
7531 * Get the region names if there are more than 2 arguments.
7532 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007533 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007534 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007535 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007536
Bram Moolenaar3982c542005-06-08 21:56:31 +00007537 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007538 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007539 len = STRLEN(innames[i]);
7540 if (STRLEN(gettail(innames[i])) < 5
7541 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007542 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007543 EMSG2(_("E755: Invalid region in %s"), innames[i]);
7544 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007545 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00007546 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
7547 spin.si_region_name[i * 2 + 1] =
7548 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007549 }
7550 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00007551 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007552
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007553 spin.si_foldroot = wordtree_alloc(&spin);
7554 spin.si_keeproot = wordtree_alloc(&spin);
7555 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007556 if (spin.si_foldroot == NULL
7557 || spin.si_keeproot == NULL
7558 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007559 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007560 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007561 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007562 }
7563
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007564 /* When not producing a .add.spl file clear the character table when
7565 * we encounter one in the .aff file. This means we dump the current
7566 * one in the .spl file if the .aff file doesn't define one. That's
7567 * better than guessing the contents, the table will match a
7568 * previously loaded spell file. */
7569 if (!spin.si_add)
7570 spin.si_clear_chartab = TRUE;
7571
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007572 /*
7573 * Read all the .aff and .dic files.
7574 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007575 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007576 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007577 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007578 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007579 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007580 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007581
Bram Moolenaarb765d632005-06-07 21:00:02 +00007582 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007583 if (mch_stat((char *)fname, &st) >= 0)
7584 {
7585 /* Read the .aff file. Will init "spin->si_conv" based on the
7586 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007587 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007588 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007589 error = TRUE;
7590 else
7591 {
7592 /* Read the .dic file and store the words in the trees. */
7593 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00007594 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007595 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007596 error = TRUE;
7597 }
7598 }
7599 else
7600 {
7601 /* No .aff file, try reading the file as a word list. Store
7602 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007603 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007604 error = TRUE;
7605 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007606
Bram Moolenaarb765d632005-06-07 21:00:02 +00007607#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007608 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007609 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007610#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007611 }
7612
Bram Moolenaar78622822005-08-23 21:00:13 +00007613 if (spin.si_compflags != NULL && spin.si_nobreak)
7614 MSG(_("Warning: both compounding and NOBREAK specified"));
7615
Bram Moolenaar51485f02005-06-04 21:55:20 +00007616 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007617 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007618 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007619 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007620 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007621 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007622 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007623 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007624 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007625 MSG(_(msg_compressing));
Bram Moolenaarb765d632005-06-07 21:00:02 +00007626 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007627 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007628 verbose_leave();
7629 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007630 wordtree_compress(&spin, spin.si_foldroot);
7631 wordtree_compress(&spin, spin.si_keeproot);
7632 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007633 }
7634
Bram Moolenaar51485f02005-06-04 21:55:20 +00007635 if (!error)
7636 {
7637 /*
7638 * Write the info in the spell file.
7639 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007640 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007641 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007642 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007643 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007644 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007645 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007646 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007647 verbose_leave();
7648 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00007649
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007650 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007651
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007652 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007653 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007654 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007655 verbose_enter();
7656 MSG(_("Done!"));
7657 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00007658 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007659 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007660 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007661 verbose_leave();
7662 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007663
Bram Moolenaarb765d632005-06-07 21:00:02 +00007664 /* If the file is loaded need to reload it. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007665 if (!error)
7666 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007667 }
7668
7669 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007670 ga_clear(&spin.si_rep);
7671 ga_clear(&spin.si_sal);
7672 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007673 ga_clear(&spin.si_prefcond);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007674
7675 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007676 for (i = 0; i < incount; ++i)
7677 if (afile[i] != NULL)
7678 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007679
7680 /* Free all the bits and pieces at once. */
7681 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007682 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007683}
7684
Bram Moolenaarb765d632005-06-07 21:00:02 +00007685
7686/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007687 * ":[count]spellgood {word}"
7688 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00007689 */
7690 void
7691ex_spell(eap)
7692 exarg_T *eap;
7693{
Bram Moolenaar7887d882005-07-01 22:33:52 +00007694 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007695 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007696}
7697
7698/*
7699 * Add "word[len]" to 'spellfile' as a good or bad word.
7700 */
7701 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007702spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007703 char_u *word;
7704 int len;
7705 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007706 int index; /* "zG" and "zW": zero, otherwise index in
7707 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007708{
7709 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007710 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007711 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007712 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007713 char_u fnamebuf[MAXPATHL];
7714 char_u line[MAXWLEN * 2];
7715 long fpos, fpos_next = 0;
7716 int i;
7717 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007718
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007719 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007720 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007721 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00007722 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007723 int_wordlist = vim_tempname('s');
7724 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00007725 return;
7726 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007727 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007728 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007729 else
7730 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007731 /* If 'spellfile' isn't set figure out a good default value. */
7732 if (*curbuf->b_p_spf == NUL)
7733 {
7734 init_spellfile();
7735 new_spf = TRUE;
7736 }
7737
7738 if (*curbuf->b_p_spf == NUL)
7739 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00007740 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00007741 return;
7742 }
7743
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007744 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
7745 {
7746 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
7747 if (i == index)
7748 break;
7749 if (*spf == NUL)
7750 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00007751 EMSGN(_("E765: 'spellfile' does not have %ld entries"), index);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007752 return;
7753 }
7754 }
7755
Bram Moolenaarb765d632005-06-07 21:00:02 +00007756 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007757 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007758 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
7759 buf = NULL;
7760 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00007761 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007762 EMSG(_(e_bufloaded));
7763 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007764 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007765
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007766 fname = fnamebuf;
7767 }
7768
7769 if (bad)
7770 {
7771 /* When the word also appears as good word we need to remove that one,
7772 * since its flags sort before the one with WF_BANNED. */
7773 fd = mch_fopen((char *)fname, "r");
7774 if (fd != NULL)
7775 {
7776 while (!vim_fgets(line, MAXWLEN * 2, fd))
7777 {
7778 fpos = fpos_next;
7779 fpos_next = ftell(fd);
7780 if (STRNCMP(word, line, len) == 0
7781 && (line[len] == '/' || line[len] < ' '))
7782 {
7783 /* Found duplicate word. Remove it by writing a '#' at
7784 * the start of the line. Mixing reading and writing
7785 * doesn't work for all systems, close the file first. */
7786 fclose(fd);
7787 fd = mch_fopen((char *)fname, "r+");
7788 if (fd == NULL)
7789 break;
7790 if (fseek(fd, fpos, SEEK_SET) == 0)
7791 fputc('#', fd);
7792 fseek(fd, fpos_next, SEEK_SET);
7793 }
7794 }
7795 fclose(fd);
7796 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007797 }
7798
7799 fd = mch_fopen((char *)fname, "a");
7800 if (fd == NULL && new_spf)
7801 {
7802 /* We just initialized the 'spellfile' option and can't open the file.
7803 * We may need to create the "spell" directory first. We already
7804 * checked the runtime directory is writable in init_spellfile(). */
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00007805 if (!dir_of_file_exists(fname))
Bram Moolenaar7887d882005-07-01 22:33:52 +00007806 {
7807 /* The directory doesn't exist. Try creating it and opening the
7808 * file again. */
7809 vim_mkdir(NameBuff, 0755);
7810 fd = mch_fopen((char *)fname, "a");
7811 }
7812 }
7813
7814 if (fd == NULL)
7815 EMSG2(_(e_notopen), fname);
7816 else
7817 {
7818 if (bad)
7819 fprintf(fd, "%.*s/!\n", len, word);
7820 else
7821 fprintf(fd, "%.*s\n", len, word);
7822 fclose(fd);
7823
7824 /* Update the .add.spl file. */
7825 mkspell(1, &fname, FALSE, TRUE, TRUE);
7826
7827 /* If the .add file is edited somewhere, reload it. */
7828 if (buf != NULL)
7829 buf_reload(buf);
7830
7831 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007832 }
7833}
7834
7835/*
7836 * Initialize 'spellfile' for the current buffer.
7837 */
7838 static void
7839init_spellfile()
7840{
7841 char_u buf[MAXPATHL];
7842 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007843 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007844 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007845 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007846 int aspath = FALSE;
7847 char_u *lstart = curbuf->b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007848
7849 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
7850 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007851 /* Find the end of the language name. Exclude the region. If there
7852 * is a path separator remember the start of the tail. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007853 for (lend = curbuf->b_p_spl; *lend != NUL
7854 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007855 if (vim_ispathsep(*lend))
7856 {
7857 aspath = TRUE;
7858 lstart = lend + 1;
7859 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007860
7861 /* Loop over all entries in 'runtimepath'. Use the first one where we
7862 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007863 rtp = p_rtp;
7864 while (*rtp != NUL)
7865 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007866 if (aspath)
7867 /* Use directory of an entry with path, e.g., for
7868 * "/dir/lg.utf-8.spl" use "/dir". */
7869 vim_strncpy(buf, curbuf->b_p_spl, lstart - curbuf->b_p_spl - 1);
7870 else
7871 /* Copy the path from 'runtimepath' to buf[]. */
7872 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00007873 if (filewritable(buf) == 2)
7874 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00007875 /* Use the first language name from 'spelllang' and the
7876 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007877 if (aspath)
7878 vim_strncpy(buf, curbuf->b_p_spl, lend - curbuf->b_p_spl);
7879 else
7880 {
7881 l = STRLEN(buf);
7882 vim_snprintf((char *)buf + l, MAXPATHL - l,
7883 "/spell/%.*s", (int)(lend - lstart), lstart);
7884 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007885 l = STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007886 fname = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang->sl_fname;
7887 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
7888 fname != NULL
7889 && strstr((char *)gettail(fname), ".ascii.") != NULL
7890 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00007891 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
7892 break;
7893 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007894 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007895 }
7896 }
7897}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007898
Bram Moolenaar51485f02005-06-04 21:55:20 +00007899
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007900/*
7901 * Init the chartab used for spelling for ASCII.
7902 * EBCDIC is not supported!
7903 */
7904 static void
7905clear_spell_chartab(sp)
7906 spelltab_T *sp;
7907{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007908 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007909
7910 /* Init everything to FALSE. */
7911 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
7912 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
7913 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007914 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007915 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007916 sp->st_upper[i] = i;
7917 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007918
7919 /* We include digits. A word shouldn't start with a digit, but handling
7920 * that is done separately. */
7921 for (i = '0'; i <= '9'; ++i)
7922 sp->st_isw[i] = TRUE;
7923 for (i = 'A'; i <= 'Z'; ++i)
7924 {
7925 sp->st_isw[i] = TRUE;
7926 sp->st_isu[i] = TRUE;
7927 sp->st_fold[i] = i + 0x20;
7928 }
7929 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007930 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007931 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007932 sp->st_upper[i] = i - 0x20;
7933 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007934}
7935
7936/*
7937 * Init the chartab used for spelling. Only depends on 'encoding'.
7938 * Called once while starting up and when 'encoding' changes.
7939 * The default is to use isalpha(), but the spell file should define the word
7940 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007941 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007942 */
7943 void
7944init_spell_chartab()
7945{
7946 int i;
7947
7948 did_set_spelltab = FALSE;
7949 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007950#ifdef FEAT_MBYTE
7951 if (enc_dbcs)
7952 {
7953 /* DBCS: assume double-wide characters are word characters. */
7954 for (i = 128; i <= 255; ++i)
7955 if (MB_BYTE2LEN(i) == 2)
7956 spelltab.st_isw[i] = TRUE;
7957 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007958 else if (enc_utf8)
7959 {
7960 for (i = 128; i < 256; ++i)
7961 {
7962 spelltab.st_isu[i] = utf_isupper(i);
7963 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
7964 spelltab.st_fold[i] = utf_fold(i);
7965 spelltab.st_upper[i] = utf_toupper(i);
7966 }
7967 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007968 else
7969#endif
7970 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007971 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007972 for (i = 128; i < 256; ++i)
7973 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007974 if (MB_ISUPPER(i))
7975 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007976 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007977 spelltab.st_isu[i] = TRUE;
7978 spelltab.st_fold[i] = MB_TOLOWER(i);
7979 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007980 else if (MB_ISLOWER(i))
7981 {
7982 spelltab.st_isw[i] = TRUE;
7983 spelltab.st_upper[i] = MB_TOUPPER(i);
7984 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007985 }
7986 }
7987}
7988
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007989/*
7990 * Set the spell character tables from strings in the affix file.
7991 */
7992 static int
7993set_spell_chartab(fol, low, upp)
7994 char_u *fol;
7995 char_u *low;
7996 char_u *upp;
7997{
7998 /* We build the new tables here first, so that we can compare with the
7999 * previous one. */
8000 spelltab_T new_st;
8001 char_u *pf = fol, *pl = low, *pu = upp;
8002 int f, l, u;
8003
8004 clear_spell_chartab(&new_st);
8005
8006 while (*pf != NUL)
8007 {
8008 if (*pl == NUL || *pu == NUL)
8009 {
8010 EMSG(_(e_affform));
8011 return FAIL;
8012 }
8013#ifdef FEAT_MBYTE
8014 f = mb_ptr2char_adv(&pf);
8015 l = mb_ptr2char_adv(&pl);
8016 u = mb_ptr2char_adv(&pu);
8017#else
8018 f = *pf++;
8019 l = *pl++;
8020 u = *pu++;
8021#endif
8022 /* Every character that appears is a word character. */
8023 if (f < 256)
8024 new_st.st_isw[f] = TRUE;
8025 if (l < 256)
8026 new_st.st_isw[l] = TRUE;
8027 if (u < 256)
8028 new_st.st_isw[u] = TRUE;
8029
8030 /* if "LOW" and "FOL" are not the same the "LOW" char needs
8031 * case-folding */
8032 if (l < 256 && l != f)
8033 {
8034 if (f >= 256)
8035 {
8036 EMSG(_(e_affrange));
8037 return FAIL;
8038 }
8039 new_st.st_fold[l] = f;
8040 }
8041
8042 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008043 * case-folding, it's upper case and the "UPP" is the upper case of
8044 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008045 if (u < 256 && u != f)
8046 {
8047 if (f >= 256)
8048 {
8049 EMSG(_(e_affrange));
8050 return FAIL;
8051 }
8052 new_st.st_fold[u] = f;
8053 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008054 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008055 }
8056 }
8057
8058 if (*pl != NUL || *pu != NUL)
8059 {
8060 EMSG(_(e_affform));
8061 return FAIL;
8062 }
8063
8064 return set_spell_finish(&new_st);
8065}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008066
8067/*
8068 * Set the spell character tables from strings in the .spl file.
8069 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008070 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008071set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008072 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008073 int cnt; /* length of "flags" */
8074 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008075{
8076 /* We build the new tables here first, so that we can compare with the
8077 * previous one. */
8078 spelltab_T new_st;
8079 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008080 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008081 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008082
8083 clear_spell_chartab(&new_st);
8084
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008085 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008086 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008087 if (i < cnt)
8088 {
8089 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
8090 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
8091 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008092
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008093 if (*p != NUL)
8094 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008095#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008096 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008097#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008098 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008099#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008100 new_st.st_fold[i + 128] = c;
8101 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
8102 new_st.st_upper[c] = i + 128;
8103 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008104 }
8105
Bram Moolenaar5195e452005-08-19 20:32:47 +00008106 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008107}
8108
8109 static int
8110set_spell_finish(new_st)
8111 spelltab_T *new_st;
8112{
8113 int i;
8114
8115 if (did_set_spelltab)
8116 {
8117 /* check that it's the same table */
8118 for (i = 0; i < 256; ++i)
8119 {
8120 if (spelltab.st_isw[i] != new_st->st_isw[i]
8121 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008122 || spelltab.st_fold[i] != new_st->st_fold[i]
8123 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008124 {
8125 EMSG(_("E763: Word characters differ between spell files"));
8126 return FAIL;
8127 }
8128 }
8129 }
8130 else
8131 {
8132 /* copy the new spelltab into the one being used */
8133 spelltab = *new_st;
8134 did_set_spelltab = TRUE;
8135 }
8136
8137 return OK;
8138}
8139
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008140/*
Bram Moolenaarea408852005-06-25 22:49:46 +00008141 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008142 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00008143 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008144 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00008145 */
8146 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008147spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00008148 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008149 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00008150{
Bram Moolenaarea408852005-06-25 22:49:46 +00008151#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008152 char_u *s;
8153 int l;
8154 int c;
8155
8156 if (has_mbyte)
8157 {
8158 l = MB_BYTE2LEN(*p);
8159 s = p;
8160 if (l == 1)
8161 {
8162 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008163 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008164 {
8165 s = p + 1; /* skip a mid-word character */
8166 l = MB_BYTE2LEN(*s);
8167 }
8168 }
8169 else
8170 {
8171 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008172 if (c < 256 ? buf->b_spell_ismw[c]
8173 : (buf->b_spell_ismw_mb != NULL
8174 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008175 {
8176 s = p + l;
8177 l = MB_BYTE2LEN(*s);
8178 }
8179 }
8180
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008181 c = mb_ptr2char(s);
8182 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008183 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008184 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008185 }
Bram Moolenaarea408852005-06-25 22:49:46 +00008186#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008187
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008188 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
8189}
8190
8191/*
8192 * Return TRUE if "p" points to a word character.
8193 * Unlike spell_iswordp() this doesn't check for "midword" characters.
8194 */
8195 static int
8196spell_iswordp_nmw(p)
8197 char_u *p;
8198{
8199#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008200 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008201
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008202 if (has_mbyte)
8203 {
8204 c = mb_ptr2char(p);
8205 if (c > 255)
8206 return mb_get_class(p) >= 2;
8207 return spelltab.st_isw[c];
8208 }
8209#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008210 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00008211}
8212
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008213#ifdef FEAT_MBYTE
8214/*
8215 * Return TRUE if "p" points to a word character.
8216 * Wide version of spell_iswordp().
8217 */
8218 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008219spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008220 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008221 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008222{
8223 int *s;
8224
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008225 if (*p < 256 ? buf->b_spell_ismw[*p]
8226 : (buf->b_spell_ismw_mb != NULL
8227 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008228 s = p + 1;
8229 else
8230 s = p;
8231
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008232 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008233 {
8234 if (enc_utf8)
8235 return utf_class(*s) >= 2;
8236 if (enc_dbcs)
8237 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
8238 return 0;
8239 }
8240 return spelltab.st_isw[*s];
8241}
8242#endif
8243
Bram Moolenaarea408852005-06-25 22:49:46 +00008244/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008245 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00008246 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008247 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008248 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008249write_spell_prefcond(fd, gap)
8250 FILE *fd;
8251 garray_T *gap;
8252{
8253 int i;
8254 char_u *p;
8255 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008256 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008257
Bram Moolenaar5195e452005-08-19 20:32:47 +00008258 if (fd != NULL)
8259 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
8260
8261 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008262
8263 for (i = 0; i < gap->ga_len; ++i)
8264 {
8265 /* <prefcond> : <condlen> <condstr> */
8266 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00008267 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008268 {
8269 len = STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008270 if (fd != NULL)
8271 {
8272 fputc(len, fd);
8273 fwrite(p, (size_t)len, (size_t)1, fd);
8274 }
8275 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008276 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008277 else if (fd != NULL)
8278 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008279 }
8280
Bram Moolenaar5195e452005-08-19 20:32:47 +00008281 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008282}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008283
8284/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008285 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
8286 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008287 * When using a multi-byte 'encoding' the length may change!
8288 * Returns FAIL when something wrong.
8289 */
8290 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008291spell_casefold(str, len, buf, buflen)
8292 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008293 int len;
8294 char_u *buf;
8295 int buflen;
8296{
8297 int i;
8298
8299 if (len >= buflen)
8300 {
8301 buf[0] = NUL;
8302 return FAIL; /* result will not fit */
8303 }
8304
8305#ifdef FEAT_MBYTE
8306 if (has_mbyte)
8307 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008308 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008309 char_u *p;
8310 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008311
8312 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008313 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008314 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008315 if (outi + MB_MAXBYTES > buflen)
8316 {
8317 buf[outi] = NUL;
8318 return FAIL;
8319 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008320 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008321 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008322 }
8323 buf[outi] = NUL;
8324 }
8325 else
8326#endif
8327 {
8328 /* Be quick for non-multibyte encodings. */
8329 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008330 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008331 buf[i] = NUL;
8332 }
8333
8334 return OK;
8335}
8336
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008337#define SPS_BEST 1
8338#define SPS_FAST 2
8339#define SPS_DOUBLE 4
8340
8341static int sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008342static int sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008343
8344/*
8345 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00008346 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008347 */
8348 int
8349spell_check_sps()
8350{
8351 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008352 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008353 char_u buf[MAXPATHL];
8354 int f;
8355
8356 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008357 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008358
8359 for (p = p_sps; *p != NUL; )
8360 {
8361 copy_option_part(&p, buf, MAXPATHL, ",");
8362
8363 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008364 if (VIM_ISDIGIT(*buf))
8365 {
8366 s = buf;
8367 sps_limit = getdigits(&s);
8368 if (*s != NUL && !VIM_ISDIGIT(*s))
8369 f = -1;
8370 }
8371 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008372 f = SPS_BEST;
8373 else if (STRCMP(buf, "fast") == 0)
8374 f = SPS_FAST;
8375 else if (STRCMP(buf, "double") == 0)
8376 f = SPS_DOUBLE;
8377 else if (STRNCMP(buf, "expr:", 5) != 0
8378 && STRNCMP(buf, "file:", 5) != 0)
8379 f = -1;
8380
8381 if (f == -1 || (sps_flags != 0 && f != 0))
8382 {
8383 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008384 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008385 return FAIL;
8386 }
8387 if (f != 0)
8388 sps_flags = f;
8389 }
8390
8391 if (sps_flags == 0)
8392 sps_flags = SPS_BEST;
8393
8394 return OK;
8395}
8396
8397/* Remember what "z?" replaced. */
8398static char_u *repl_from = NULL;
8399static char_u *repl_to = NULL;
8400
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008401/*
8402 * "z?": Find badly spelled word under or after the cursor.
8403 * Give suggestions for the properly spelled word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00008404 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008405 */
8406 void
Bram Moolenaard12a1322005-08-21 22:08:24 +00008407spell_suggest(count)
8408 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008409{
8410 char_u *line;
8411 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008412 char_u wcopy[MAXWLEN + 2];
8413 char_u *p;
8414 int i;
8415 int c;
8416 suginfo_T sug;
8417 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008418 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008419 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008420 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00008421 int selected = count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008422
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008423 /* Find the start of the badly spelled word. */
Bram Moolenaar95529562005-08-25 21:21:38 +00008424 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00008425 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008426 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008427 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
8428 return;
8429
8430 /* No bad word or it starts after the cursor: use the word under the
8431 * cursor. */
8432 curwin->w_cursor = prev_cursor;
8433 line = ml_get_curline();
8434 p = line + curwin->w_cursor.col;
8435 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008436 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00008437 mb_ptr_back(line, p);
8438 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008439 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00008440 mb_ptr_adv(p);
8441
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008442 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008443 {
8444 beep_flush();
8445 return;
8446 }
8447 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008448 }
8449
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008450 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008451
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008452 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008453 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008454
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008455 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008456
Bram Moolenaar5195e452005-08-19 20:32:47 +00008457 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
8458 * 'spellsuggest', whatever is smaller. */
8459 if (sps_limit > (int)Rows - 2)
8460 limit = (int)Rows - 2;
8461 else
8462 limit = sps_limit;
8463 spell_find_suggest(line + curwin->w_cursor.col, &sug, limit,
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008464 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008465
8466 if (sug.su_ga.ga_len == 0)
8467 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00008468 else if (count > 0)
8469 {
8470 if (count > sug.su_ga.ga_len)
8471 smsg((char_u *)_("Sorry, only %ld suggestions"),
8472 (long)sug.su_ga.ga_len);
8473 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008474 else
8475 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008476 vim_free(repl_from);
8477 repl_from = NULL;
8478 vim_free(repl_to);
8479 repl_to = NULL;
8480
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008481#ifdef FEAT_RIGHTLEFT
8482 /* When 'rightleft' is set the list is drawn right-left. */
8483 cmdmsg_rl = curwin->w_p_rl;
8484 if (cmdmsg_rl)
8485 msg_col = Columns - 1;
8486#endif
8487
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008488 /* List the suggestions. */
8489 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008490 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008491 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
8492 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008493#ifdef FEAT_RIGHTLEFT
8494 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
8495 {
8496 /* And now the rabbit from the high hat: Avoid showing the
8497 * untranslated message rightleft. */
8498 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
8499 sug.su_badlen, sug.su_badptr);
8500 }
8501#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008502 msg_puts(IObuff);
8503 msg_clr_eos();
8504 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00008505
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008506 msg_scroll = TRUE;
8507 for (i = 0; i < sug.su_ga.ga_len; ++i)
8508 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008509 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008510
8511 /* The suggested word may replace only part of the bad word, add
8512 * the not replaced part. */
8513 STRCPY(wcopy, stp->st_word);
8514 if (sug.su_badlen > stp->st_orglen)
8515 vim_strncpy(wcopy + STRLEN(wcopy),
8516 sug.su_badptr + stp->st_orglen,
8517 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008518 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
8519#ifdef FEAT_RIGHTLEFT
8520 if (cmdmsg_rl)
8521 rl_mirror(IObuff);
8522#endif
8523 msg_puts(IObuff);
8524
8525 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008526 msg_puts(IObuff);
8527
8528 /* The word may replace more than "su_badlen". */
8529 if (sug.su_badlen < stp->st_orglen)
8530 {
8531 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
8532 stp->st_orglen, sug.su_badptr);
8533 msg_puts(IObuff);
8534 }
8535
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008536 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008537 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008538 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008539 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008540 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008541 stp->st_salscore ? "s " : "",
8542 stp->st_score, stp->st_altscore);
8543 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008544 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00008545 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008546#ifdef FEAT_RIGHTLEFT
8547 if (cmdmsg_rl)
8548 /* Mirror the numbers, but keep the leading space. */
8549 rl_mirror(IObuff + 1);
8550#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00008551 msg_advance(30);
8552 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008553 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008554 msg_putchar('\n');
8555 }
8556
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008557#ifdef FEAT_RIGHTLEFT
8558 cmdmsg_rl = FALSE;
8559 msg_col = 0;
8560#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008561 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00008562 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008563 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00008564 selected -= lines_left;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008565 }
8566
Bram Moolenaard12a1322005-08-21 22:08:24 +00008567 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
8568 {
8569 /* Save the from and to text for :spellrepall. */
8570 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00008571 if (sug.su_badlen > stp->st_orglen)
8572 {
8573 /* Replacing less than "su_badlen", append the remainder to
8574 * repl_to. */
8575 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
8576 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
8577 sug.su_badlen - stp->st_orglen,
8578 sug.su_badptr + stp->st_orglen);
8579 repl_to = vim_strsave(IObuff);
8580 }
8581 else
8582 {
8583 /* Replacing su_badlen or more, use the whole word. */
8584 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
8585 repl_to = vim_strsave(stp->st_word);
8586 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00008587
8588 /* Replace the word. */
8589 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
8590 if (p != NULL)
8591 {
8592 c = sug.su_badptr - line;
8593 mch_memmove(p, line, c);
8594 STRCPY(p + c, stp->st_word);
8595 STRCAT(p, sug.su_badptr + stp->st_orglen);
8596 ml_replace(curwin->w_cursor.lnum, p, FALSE);
8597 curwin->w_cursor.col = c;
8598 changed_bytes(curwin->w_cursor.lnum, c);
8599
8600 /* For redo we use a change-word command. */
8601 ResetRedobuff();
8602 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +00008603 AppendToRedobuffLit(p + c,
8604 STRLEN(stp->st_word) + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +00008605 AppendCharToRedobuff(ESC);
8606 }
8607 }
8608 else
8609 curwin->w_cursor = prev_cursor;
8610
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008611 spell_find_cleanup(&sug);
8612}
8613
8614/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008615 * Check if the word at line "lnum" column "col" is required to start with a
8616 * capital. This uses 'spellcapcheck' of the current buffer.
8617 */
8618 static int
8619check_need_cap(lnum, col)
8620 linenr_T lnum;
8621 colnr_T col;
8622{
8623 int need_cap = FALSE;
8624 char_u *line;
8625 char_u *line_copy = NULL;
8626 char_u *p;
8627 colnr_T endcol;
8628 regmatch_T regmatch;
8629
8630 if (curbuf->b_cap_prog == NULL)
8631 return FALSE;
8632
8633 line = ml_get_curline();
8634 endcol = 0;
8635 if ((int)(skipwhite(line) - line) >= (int)col)
8636 {
8637 /* At start of line, check if previous line is empty or sentence
8638 * ends there. */
8639 if (lnum == 1)
8640 need_cap = TRUE;
8641 else
8642 {
8643 line = ml_get(lnum - 1);
8644 if (*skipwhite(line) == NUL)
8645 need_cap = TRUE;
8646 else
8647 {
8648 /* Append a space in place of the line break. */
8649 line_copy = concat_str(line, (char_u *)" ");
8650 line = line_copy;
8651 endcol = STRLEN(line);
8652 }
8653 }
8654 }
8655 else
8656 endcol = col;
8657
8658 if (endcol > 0)
8659 {
8660 /* Check if sentence ends before the bad word. */
8661 regmatch.regprog = curbuf->b_cap_prog;
8662 regmatch.rm_ic = FALSE;
8663 p = line + endcol;
8664 for (;;)
8665 {
8666 mb_ptr_back(line, p);
8667 if (p == line || spell_iswordp_nmw(p))
8668 break;
8669 if (vim_regexec(&regmatch, p, 0)
8670 && regmatch.endp[0] == line + endcol)
8671 {
8672 need_cap = TRUE;
8673 break;
8674 }
8675 }
8676 }
8677
8678 vim_free(line_copy);
8679
8680 return need_cap;
8681}
8682
8683
8684/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008685 * ":spellrepall"
8686 */
8687/*ARGSUSED*/
8688 void
8689ex_spellrepall(eap)
8690 exarg_T *eap;
8691{
8692 pos_T pos = curwin->w_cursor;
8693 char_u *frompat;
8694 int addlen;
8695 char_u *line;
8696 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008697 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008698 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008699
8700 if (repl_from == NULL || repl_to == NULL)
8701 {
8702 EMSG(_("E752: No previous spell replacement"));
8703 return;
8704 }
8705 addlen = STRLEN(repl_to) - STRLEN(repl_from);
8706
8707 frompat = alloc(STRLEN(repl_from) + 7);
8708 if (frompat == NULL)
8709 return;
8710 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
8711 p_ws = FALSE;
8712
Bram Moolenaar5195e452005-08-19 20:32:47 +00008713 sub_nsubs = 0;
8714 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008715 curwin->w_cursor.lnum = 0;
8716 while (!got_int)
8717 {
8718 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
8719 || u_save_cursor() == FAIL)
8720 break;
8721
8722 /* Only replace when the right word isn't there yet. This happens
8723 * when changing "etc" to "etc.". */
8724 line = ml_get_curline();
8725 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
8726 repl_to, STRLEN(repl_to)) != 0)
8727 {
8728 p = alloc(STRLEN(line) + addlen + 1);
8729 if (p == NULL)
8730 break;
8731 mch_memmove(p, line, curwin->w_cursor.col);
8732 STRCPY(p + curwin->w_cursor.col, repl_to);
8733 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
8734 ml_replace(curwin->w_cursor.lnum, p, FALSE);
8735 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008736
8737 if (curwin->w_cursor.lnum != prev_lnum)
8738 {
8739 ++sub_nlines;
8740 prev_lnum = curwin->w_cursor.lnum;
8741 }
8742 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008743 }
8744 curwin->w_cursor.col += STRLEN(repl_to);
8745 }
8746
8747 p_ws = save_ws;
8748 curwin->w_cursor = pos;
8749 vim_free(frompat);
8750
Bram Moolenaar5195e452005-08-19 20:32:47 +00008751 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008752 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008753 else
8754 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008755}
8756
8757/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008758 * Find spell suggestions for "word". Return them in the growarray "*gap" as
8759 * a list of allocated strings.
8760 */
8761 void
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008762spell_suggest_list(gap, word, maxcount, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008763 garray_T *gap;
8764 char_u *word;
8765 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008766 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008767{
8768 suginfo_T sug;
8769 int i;
8770 suggest_T *stp;
8771 char_u *wcopy;
8772
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008773 spell_find_suggest(word, &sug, maxcount, FALSE, need_cap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008774
8775 /* Make room in "gap". */
8776 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
8777 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
8778 return;
8779
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008780 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008781 {
8782 stp = &SUG(sug.su_ga, i);
8783
8784 /* The suggested word may replace only part of "word", add the not
8785 * replaced part. */
8786 wcopy = alloc(STRLEN(stp->st_word)
8787 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
8788 if (wcopy == NULL)
8789 break;
8790 STRCPY(wcopy, stp->st_word);
8791 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
8792 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
8793 }
8794
8795 spell_find_cleanup(&sug);
8796}
8797
8798/*
8799 * Find spell suggestions for the word at the start of "badptr".
8800 * Return the suggestions in "su->su_ga".
8801 * The maximum number of suggestions is "maxcount".
8802 * Note: does use info for the current window.
8803 * This is based on the mechanisms of Aspell, but completely reimplemented.
8804 */
8805 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008806spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008807 char_u *badptr;
8808 suginfo_T *su;
8809 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00008810 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008811 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008812{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008813 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008814 char_u buf[MAXPATHL];
8815 char_u *p;
8816 int do_combine = FALSE;
8817 char_u *sps_copy;
8818#ifdef FEAT_EVAL
8819 static int expr_busy = FALSE;
8820#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008821 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00008822 int i;
8823 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008824
8825 /*
8826 * Set the info in "*su".
8827 */
8828 vim_memset(su, 0, sizeof(suginfo_T));
8829 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
8830 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008831 if (*badptr == NUL)
8832 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008833 hash_init(&su->su_banned);
8834
8835 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008836 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008837 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008838 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008839
8840 if (su->su_badlen >= MAXWLEN)
8841 su->su_badlen = MAXWLEN - 1; /* just in case */
8842 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
8843 (void)spell_casefold(su->su_badptr, su->su_badlen,
8844 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008845 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008846 su->su_badflags = badword_captype(su->su_badptr,
8847 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008848 if (need_cap)
8849 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008850
Bram Moolenaar8b96d642005-09-05 22:05:30 +00008851 /* Find the default language for sound folding. We simply use the first
8852 * one in 'spelllang' that supports sound folding. That's good for when
8853 * using multiple files for one language, it's not that bad when mixing
8854 * languages (e.g., "pl,en"). */
8855 for (i = 0; i < curbuf->b_langp.ga_len; ++i)
8856 {
8857 lp = LANGP_ENTRY(curbuf->b_langp, i);
8858 if (lp->lp_sallang != NULL)
8859 {
8860 su->su_sallang = lp->lp_sallang;
8861 break;
8862 }
8863 }
8864
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008865 /* Soundfold the bad word with the default sound folding, so that we don't
8866 * have to do this many times. */
8867 if (su->su_sallang != NULL)
8868 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
8869 su->su_sal_badword);
8870
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008871 /* If the word is not capitalised and spell_check() doesn't consider the
8872 * word to be bad then it might need to be capitalised. Add a suggestion
8873 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008874 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00008875 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008876 {
8877 make_case_word(su->su_badword, buf, WF_ONECAP);
8878 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00008879 0, TRUE, su->su_sallang);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008880 }
8881
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008882 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00008883 if (banbadword)
8884 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008885
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008886 /* Make a copy of 'spellsuggest', because the expression may change it. */
8887 sps_copy = vim_strsave(p_sps);
8888 if (sps_copy == NULL)
8889 return;
8890
8891 /* Loop over the items in 'spellsuggest'. */
8892 for (p = sps_copy; *p != NUL; )
8893 {
8894 copy_option_part(&p, buf, MAXPATHL, ",");
8895
8896 if (STRNCMP(buf, "expr:", 5) == 0)
8897 {
8898#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008899 /* Evaluate an expression. Skip this when called recursively,
8900 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008901 if (!expr_busy)
8902 {
8903 expr_busy = TRUE;
8904 spell_suggest_expr(su, buf + 5);
8905 expr_busy = FALSE;
8906 }
8907#endif
8908 }
8909 else if (STRNCMP(buf, "file:", 5) == 0)
8910 /* Use list of suggestions in a file. */
8911 spell_suggest_file(su, buf + 5);
8912 else
8913 {
8914 /* Use internal method. */
8915 spell_suggest_intern(su);
8916 if (sps_flags & SPS_DOUBLE)
8917 do_combine = TRUE;
8918 }
8919 }
8920
8921 vim_free(sps_copy);
8922
8923 if (do_combine)
8924 /* Combine the two list of suggestions. This must be done last,
8925 * because sorting changes the order again. */
8926 score_combine(su);
8927}
8928
8929#ifdef FEAT_EVAL
8930/*
8931 * Find suggestions by evaluating expression "expr".
8932 */
8933 static void
8934spell_suggest_expr(su, expr)
8935 suginfo_T *su;
8936 char_u *expr;
8937{
8938 list_T *list;
8939 listitem_T *li;
8940 int score;
8941 char_u *p;
8942
8943 /* The work is split up in a few parts to avoid having to export
8944 * suginfo_T.
8945 * First evaluate the expression and get the resulting list. */
8946 list = eval_spell_expr(su->su_badword, expr);
8947 if (list != NULL)
8948 {
8949 /* Loop over the items in the list. */
8950 for (li = list->lv_first; li != NULL; li = li->li_next)
8951 if (li->li_tv.v_type == VAR_LIST)
8952 {
8953 /* Get the word and the score from the items. */
8954 score = get_spellword(li->li_tv.vval.v_list, &p);
8955 if (score >= 0)
8956 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00008957 su->su_badlen, score, 0, TRUE, su->su_sallang);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008958 }
8959 list_unref(list);
8960 }
8961
8962 /* Sort the suggestions and truncate at "maxcount". */
8963 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8964}
8965#endif
8966
8967/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008968 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008969 */
8970 static void
8971spell_suggest_file(su, fname)
8972 suginfo_T *su;
8973 char_u *fname;
8974{
8975 FILE *fd;
8976 char_u line[MAXWLEN * 2];
8977 char_u *p;
8978 int len;
8979 char_u cword[MAXWLEN];
8980
8981 /* Open the file. */
8982 fd = mch_fopen((char *)fname, "r");
8983 if (fd == NULL)
8984 {
8985 EMSG2(_(e_notopen), fname);
8986 return;
8987 }
8988
8989 /* Read it line by line. */
8990 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
8991 {
8992 line_breakcheck();
8993
8994 p = vim_strchr(line, '/');
8995 if (p == NULL)
8996 continue; /* No Tab found, just skip the line. */
8997 *p++ = NUL;
8998 if (STRICMP(su->su_badword, line) == 0)
8999 {
9000 /* Match! Isolate the good word, until CR or NL. */
9001 for (len = 0; p[len] >= ' '; ++len)
9002 ;
9003 p[len] = NUL;
9004
9005 /* If the suggestion doesn't have specific case duplicate the case
9006 * of the bad word. */
9007 if (captype(p, NULL) == 0)
9008 {
9009 make_case_word(p, cword, su->su_badflags);
9010 p = cword;
9011 }
9012
9013 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00009014 SCORE_FILE, 0, TRUE, su->su_sallang);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009015 }
9016 }
9017
9018 fclose(fd);
9019
9020 /* Sort the suggestions and truncate at "maxcount". */
9021 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
9022}
9023
9024/*
9025 * Find suggestions for the internal method indicated by "sps_flags".
9026 */
9027 static void
9028spell_suggest_intern(su)
9029 suginfo_T *su;
9030{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009031 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00009032 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009033 *
9034 * Set a maximum score to limit the combination of operations that is
9035 * tried.
9036 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009037 suggest_try_special(su);
9038
9039 /*
9040 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
9041 * from the .aff file and inserting a space (split the word).
9042 */
9043 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009044
9045 /* For the resulting top-scorers compute the sound-a-like score. */
9046 if (sps_flags & SPS_DOUBLE)
9047 score_comp_sal(su);
9048
9049 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00009050 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009051 *
9052 * Only do this when we don't have a lot of suggestions yet, because it's
9053 * very slow and often doesn't find new suggestions.
9054 */
9055 if ((sps_flags & SPS_DOUBLE)
9056 || (!(sps_flags & SPS_FAST)
9057 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
9058 {
9059 /* Allow a higher score now. */
9060 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009061 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009062 }
9063
9064 /* When CTRL-C was hit while searching do show the results. */
9065 ui_breakcheck();
9066 if (got_int)
9067 {
9068 (void)vgetc();
9069 got_int = FALSE;
9070 }
9071
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009072 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009073 {
9074 if (sps_flags & SPS_BEST)
9075 /* Adjust the word score for how it sounds like. */
9076 rescore_suggestions(su);
9077
9078 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009079 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009080 }
9081}
9082
9083/*
9084 * Free the info put in "*su" by spell_find_suggest().
9085 */
9086 static void
9087spell_find_cleanup(su)
9088 suginfo_T *su;
9089{
9090 int i;
9091
9092 /* Free the suggestions. */
9093 for (i = 0; i < su->su_ga.ga_len; ++i)
9094 vim_free(SUG(su->su_ga, i).st_word);
9095 ga_clear(&su->su_ga);
9096 for (i = 0; i < su->su_sga.ga_len; ++i)
9097 vim_free(SUG(su->su_sga, i).st_word);
9098 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009099
9100 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009101 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009102}
9103
9104/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009105 * Make a copy of "word", with the first letter upper or lower cased, to
9106 * "wcopy[MAXWLEN]". "word" must not be empty.
9107 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009108 */
9109 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009110onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009111 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009112 char_u *wcopy;
9113 int upper; /* TRUE: first letter made upper case */
9114{
9115 char_u *p;
9116 int c;
9117 int l;
9118
9119 p = word;
9120#ifdef FEAT_MBYTE
9121 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009122 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009123 else
9124#endif
9125 c = *p++;
9126 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009127 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009128 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009129 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009130#ifdef FEAT_MBYTE
9131 if (has_mbyte)
9132 l = mb_char2bytes(c, wcopy);
9133 else
9134#endif
9135 {
9136 l = 1;
9137 wcopy[0] = c;
9138 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009139 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009140}
9141
9142/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009143 * Make a copy of "word" with all the letters upper cased into
9144 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009145 */
9146 static void
9147allcap_copy(word, wcopy)
9148 char_u *word;
9149 char_u *wcopy;
9150{
9151 char_u *s;
9152 char_u *d;
9153 int c;
9154
9155 d = wcopy;
9156 for (s = word; *s != NUL; )
9157 {
9158#ifdef FEAT_MBYTE
9159 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009160 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009161 else
9162#endif
9163 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00009164
9165#ifdef FEAT_MBYTE
9166 /* We only change ß to SS when we are certain latin1 is used. It
9167 * would cause weird errors in other 8-bit encodings. */
9168 if (enc_latin1like && c == 0xdf)
9169 {
9170 c = 'S';
9171 if (d - wcopy >= MAXWLEN - 1)
9172 break;
9173 *d++ = c;
9174 }
9175 else
9176#endif
9177 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009178
9179#ifdef FEAT_MBYTE
9180 if (has_mbyte)
9181 {
9182 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
9183 break;
9184 d += mb_char2bytes(c, d);
9185 }
9186 else
9187#endif
9188 {
9189 if (d - wcopy >= MAXWLEN - 1)
9190 break;
9191 *d++ = c;
9192 }
9193 }
9194 *d = NUL;
9195}
9196
9197/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00009198 * Try finding suggestions by recognizing specific situations.
9199 */
9200 static void
9201suggest_try_special(su)
9202 suginfo_T *su;
9203{
9204 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009205 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009206 int c;
9207 char_u word[MAXWLEN];
9208
9209 /*
9210 * Recognize a word that is repeated: "the the".
9211 */
9212 p = skiptowhite(su->su_fbadword);
9213 len = p - su->su_fbadword;
9214 p = skipwhite(p);
9215 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
9216 {
9217 /* Include badflags: if the badword is onecap or allcap
9218 * use that for the goodword too: "The the" -> "The". */
9219 c = su->su_fbadword[len];
9220 su->su_fbadword[len] = NUL;
9221 make_case_word(su->su_fbadword, word, su->su_badflags);
9222 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009223
9224 /* Give a soundalike score of 0, compute the score as if deleting one
9225 * character. */
9226 add_suggestion(su, &su->su_ga, word, su->su_badlen,
9227 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009228 }
9229}
9230
9231/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009232 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00009233 *
9234 * This uses a state machine. At each node in the tree we try various
9235 * operations. When trying if an operation work "depth" is increased and the
9236 * stack[] is used to store info. This allows combinations, thus insert one
9237 * character, replace one and delete another. The number of changes is
9238 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaard12a1322005-08-21 22:08:24 +00009239 *
9240 * After implementing this I noticed an article by Kemal Oflazer that
9241 * describes something similar: "Error-tolerant Finite State Recognition with
9242 * Applications to Morphological Analysis and Spelling Correction" (1996).
9243 * The implementation in the article is simplified and requires a stack of
9244 * unknown depth. The implementation here only needs a stack depth of the
9245 * length of the word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009246 */
9247 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00009248suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009249 suginfo_T *su;
9250{
9251 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
9252 char_u tword[MAXWLEN]; /* good word collected so far */
9253 trystate_T stack[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009254 char_u preword[MAXWLEN * 3]; /* word found with proper case;
9255 * concatanation of prefix compound
9256 * words and split word. NUL terminated
9257 * when going deeper but not when coming
9258 * back. */
9259 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009260 trystate_T *sp;
9261 int newscore;
9262 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009263 char_u *byts, *fbyts, *pbyts;
9264 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009265 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009266 int c, c2, c3;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009267 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009268 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009269 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009270 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009271 int len;
9272 char_u *p;
9273 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00009274 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009275 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009276 slang_T *slang;
9277 int fword_ends;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009278 int lpi;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009279 int maysplit;
9280 int goodword_ends;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009281
9282 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00009283 * to find matches (esp. REP items). Append some more text, changing
9284 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009285 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009286 n = STRLEN(fword);
9287 p = su->su_badptr + su->su_badlen;
9288 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009289
Bram Moolenaar8b96d642005-09-05 22:05:30 +00009290 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009291 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00009292 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009293 slang = lp->lp_slang;
9294
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009295 /* If reloading a spell file fails it's still in the list but
9296 * everything has been cleared. */
9297 if (slang->sl_fbyts == NULL)
9298 continue;
9299
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009300 /*
9301 * Go through the whole case-fold tree, try changes at each node.
9302 * "tword[]" contains the word collected from nodes in the tree.
9303 * "fword[]" the word we are trying to match with (initially the bad
9304 * word).
9305 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009306 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009307 sp = &stack[0];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009308 vim_memset(sp, 0, sizeof(trystate_T));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009309 sp->ts_curi = 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009310
Bram Moolenaarea424162005-06-16 21:51:00 +00009311 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009312 * When there are postponed prefixes we need to use these first. At
9313 * the end of the prefix we continue in the case-fold tree.
9314 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009315 fbyts = slang->sl_fbyts;
9316 fidxs = slang->sl_fidxs;
9317 pbyts = slang->sl_pbyts;
9318 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009319 if (pbyts != NULL)
9320 {
9321 byts = pbyts;
9322 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009323 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009324 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
9325 }
9326 else
9327 {
9328 byts = fbyts;
9329 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009330 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009331 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009332 }
9333
9334 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00009335 * Loop to find all suggestions. At each round we either:
9336 * - For the current state try one operation, advance "ts_curi",
9337 * increase "depth".
9338 * - When a state is done go to the next, set "ts_state".
9339 * - When all states are tried decrease "depth".
9340 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009341 while (depth >= 0 && !got_int)
9342 {
9343 sp = &stack[depth];
9344 switch (sp->ts_state)
9345 {
9346 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009347 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009348 /*
9349 * Start of node: Deal with NUL bytes, which means
9350 * tword[] may end here.
9351 */
9352 arridx = sp->ts_arridx; /* current node in the tree */
9353 len = byts[arridx]; /* bytes in this node */
9354 arridx += sp->ts_curi; /* index of current byte */
9355
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009356 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009357 {
9358 /* Skip over the NUL bytes, we use them later. */
9359 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
9360 ;
9361 sp->ts_curi += n;
9362
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009363 /* Always past NUL bytes now. */
9364 n = (int)sp->ts_state;
9365 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00009366 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009367
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009368 /* At end of a prefix or at start of prefixtree: check for
9369 * following word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009370 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009371 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00009372 /* Set su->su_badflags to the caps type at this
9373 * position. Use the caps type until here for the
9374 * prefix itself. */
9375#ifdef FEAT_MBYTE
9376 if (has_mbyte)
9377 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
9378 else
9379#endif
9380 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009381 flags = badword_captype(su->su_badptr,
9382 su->su_badptr + n);
9383 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00009384 su->su_badptr + su->su_badlen);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009385 ++depth;
9386 stack[depth] = stack[depth - 1];
9387 sp = &stack[depth];
9388 sp->ts_prefixdepth = depth - 1;
9389 byts = fbyts;
9390 idxs = fidxs;
9391 sp->ts_state = STATE_START;
9392 sp->ts_curi = 1; /* start just after length byte */
9393 sp->ts_arridx = 0;
9394
Bram Moolenaar53805d12005-08-01 07:08:33 +00009395 /* Move the prefix to preword[] with the right case
9396 * and make find_keepcap_word() works. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00009397 tword[sp->ts_twordlen] = NUL;
9398 make_case_word(tword + sp->ts_splitoff,
9399 preword + sp->ts_prewordlen,
9400 flags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009401 sp->ts_prewordlen = STRLEN(preword);
Bram Moolenaard12a1322005-08-21 22:08:24 +00009402 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009403 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009404 break;
9405 }
9406
Bram Moolenaar0c405862005-06-22 22:26:26 +00009407 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009408 {
9409 /* Past bytes in node and/or past NUL bytes. */
9410 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00009411 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009412 break;
9413 }
9414
9415 /*
9416 * End of word in tree.
9417 */
9418 ++sp->ts_curi; /* eat one NUL byte */
9419
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009420 flags = (int)idxs[arridx];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009421 fword_ends = (fword[sp->ts_fidx] == NUL
9422 || !spell_iswordp(fword + sp->ts_fidx, curbuf));
9423 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009424
Bram Moolenaard12a1322005-08-21 22:08:24 +00009425 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
9426 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009427 {
9428 /* There was a prefix before the word. Check that the
9429 * prefix can be used with this word. */
9430 /* Count the length of the NULs in the prefix. If there
9431 * are none this must be the first try without a prefix.
9432 */
9433 n = stack[sp->ts_prefixdepth].ts_arridx;
9434 len = pbyts[n++];
9435 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
9436 ;
9437 if (c > 0)
9438 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009439 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00009440 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009441 if (c == 0)
9442 break;
9443
9444 /* Use the WF_RARE flag for a rare prefix. */
9445 if (c & WF_RAREPFX)
9446 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009447
9448 /* Tricky: when checking for both prefix and
9449 * compounding we run into the prefix flag first.
9450 * Remember that it's OK, so that we accept the prefix
9451 * when arriving at a compound flag. */
9452 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009453 }
9454 }
9455
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009456 /* Check NEEDCOMPOUND: can't use word without compounding. Do
9457 * try appending another compound word below. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009458 if (sp->ts_complen == sp->ts_compsplit && fword_ends
9459 && (flags & WF_NEEDCOMP))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009460 goodword_ends = FALSE;
9461 else
9462 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009463
Bram Moolenaard12a1322005-08-21 22:08:24 +00009464 if (sp->ts_complen > sp->ts_compsplit)
9465 {
Bram Moolenaar78622822005-08-23 21:00:13 +00009466 if (slang->sl_nobreak)
9467 {
9468 /* There was a word before this word. When there was
9469 * no change in this word (it was correct) add the
9470 * first word as a suggestion. If this word was
9471 * corrected too, we need to check if a correct word
9472 * follows. */
9473 if (sp->ts_fidx - sp->ts_splitfidx
9474 == sp->ts_twordlen - sp->ts_splitoff
9475 && STRNCMP(fword + sp->ts_splitfidx,
9476 tword + sp->ts_splitoff,
9477 sp->ts_fidx - sp->ts_splitfidx) == 0)
9478 {
9479 preword[sp->ts_prewordlen] = NUL;
9480 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +00009481 sp->ts_splitfidx - repextra,
9482 sp->ts_score, 0, FALSE,
9483 lp->lp_sallang);
Bram Moolenaar78622822005-08-23 21:00:13 +00009484 break;
9485 }
9486 }
9487 else
9488 {
9489 /* There was a compound word before this word. If
9490 * this word does not support compounding then give up
9491 * (splitting is tried for the word without compound
9492 * flag). */
9493 if (((unsigned)flags >> 24) == 0
9494 || sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaard12a1322005-08-21 22:08:24 +00009495 < slang->sl_compminlen)
Bram Moolenaar78622822005-08-23 21:00:13 +00009496 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009497#ifdef FEAT_MBYTE
9498 /* For multi-byte chars check character length against
9499 * COMPOUNDMIN. */
9500 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009501 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009502 && mb_charlen(tword + sp->ts_splitoff)
9503 < slang->sl_compminlen)
9504 break;
9505#endif
9506
Bram Moolenaar78622822005-08-23 21:00:13 +00009507 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
9508 compflags[sp->ts_complen + 1] = NUL;
9509 vim_strncpy(preword + sp->ts_prewordlen,
9510 tword + sp->ts_splitoff,
9511 sp->ts_twordlen - sp->ts_splitoff);
9512 p = preword;
9513 while (*skiptowhite(p) != NUL)
9514 p = skipwhite(skiptowhite(p));
9515 if (fword_ends && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00009516 compflags + sp->ts_compsplit))
Bram Moolenaar78622822005-08-23 21:00:13 +00009517 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00009518
Bram Moolenaar78622822005-08-23 21:00:13 +00009519 /* Get pointer to last char of previous word. */
9520 p = preword + sp->ts_prewordlen;
9521 mb_ptr_back(preword, p);
9522 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00009523 }
Bram Moolenaare52325c2005-08-22 22:54:29 +00009524 else
9525 p = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009526
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009527 /*
9528 * Form the word with proper case in preword.
9529 * If there is a word from a previous split, append.
9530 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009531 if (flags & WF_KEEPCAP)
9532 /* Must find the word in the keep-case tree. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009533 find_keepcap_word(slang, tword + sp->ts_splitoff,
9534 preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009535 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00009536 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009537 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00009538 * use that for the goodword too. But if the badword is
9539 * allcap and it's only one char long use onecap. */
9540 c = su->su_badflags;
9541 if ((c & WF_ALLCAP)
9542#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009543 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
Bram Moolenaar0c405862005-06-22 22:26:26 +00009544#else
9545 && su->su_badlen == 1
9546#endif
9547 )
9548 c = WF_ONECAP;
Bram Moolenaare52325c2005-08-22 22:54:29 +00009549 c |= flags;
9550
9551 /* When appending a compound word after a word character
9552 * don't use Onecap. */
9553 if (p != NULL && spell_iswordp_nmw(p))
9554 c &= ~WF_ONECAP;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009555 make_case_word(tword + sp->ts_splitoff,
Bram Moolenaare52325c2005-08-22 22:54:29 +00009556 preword + sp->ts_prewordlen, c);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009557 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009558
9559 /* Don't use a banned word. It may appear again as a good
9560 * word, thus remember it. */
9561 if (flags & WF_BANNED)
9562 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00009563 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009564 break;
9565 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009566 if ((sp->ts_complen == sp->ts_compsplit
9567 && was_banned(su, preword + sp->ts_prewordlen))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009568 || was_banned(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009569 {
9570 if (slang->sl_compprog == NULL)
9571 break;
9572 /* the word so far was banned but we may try compounding */
9573 goodword_ends = FALSE;
9574 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009575
9576 newscore = 0;
9577 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009578 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009579 newscore += SCORE_REGION;
9580 if (flags & WF_RARE)
9581 newscore += SCORE_RARE;
9582
Bram Moolenaar0c405862005-06-22 22:26:26 +00009583 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00009584 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009585 newscore += SCORE_ICASE;
9586
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009587 maysplit = TRUE;
9588 if (fword_ends && goodword_ends
9589 && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009590 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009591 /* The badword also ends: add suggestions. Give a penalty
9592 * when changing non-word char to word char, e.g., "thes,"
9593 * -> "these". */
9594 p = fword + sp->ts_fidx;
9595#ifdef FEAT_MBYTE
9596 if (has_mbyte)
9597 mb_ptr_back(fword, p);
9598 else
9599#endif
9600 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009601 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009602 {
9603 p = preword + STRLEN(preword);
9604#ifdef FEAT_MBYTE
9605 if (has_mbyte)
9606 mb_ptr_back(preword, p);
9607 else
9608#endif
9609 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009610 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009611 newscore += SCORE_NONWORD;
9612 }
9613
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009614 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009615 sp->ts_fidx - repextra,
9616 sp->ts_score + newscore, 0, FALSE,
9617 lp->lp_sallang);
9618
9619 /* When the bad word doesn't end yet, try changing the
9620 * next word. E.g., find suggestions for "the the" where
9621 * the second "the" is different. It's done like a split.
9622 */
9623 if (sp->ts_fidx - repextra >= su->su_badlen)
9624 maysplit = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009625 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009626
9627 if (maysplit
9628 && (sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +00009629#ifdef FEAT_MBYTE
9630 /* Don't split halfway a character. */
9631 && (!has_mbyte || sp->ts_tcharlen == 0)
9632#endif
9633 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009634 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009635 int try_compound;
9636
9637 /* Get here in two situations:
9638 * 1. The word in the tree ends but the badword continues:
9639 * If the word allows compounding try that. Otherwise
9640 * try a split by inserting a space. For both check
9641 * that a valid words starts at fword[sp->ts_fidx].
Bram Moolenaar78622822005-08-23 21:00:13 +00009642 * For NOBREAK do like compounding to be able to check
9643 * if the next word is valid.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009644 * 2. The badword does end, but it was due to a change
9645 * (e.g., a swap). No need to split, but do check that
9646 * the following word is valid.
9647 */
Bram Moolenaard12a1322005-08-21 22:08:24 +00009648 try_compound = FALSE;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009649 if ((!fword_ends || !goodword_ends)
Bram Moolenaar6de68532005-08-24 22:08:48 +00009650 && slang->sl_compprog != NULL
Bram Moolenaar5195e452005-08-19 20:32:47 +00009651 && ((unsigned)flags >> 24) != 0
9652 && sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009653 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009654#ifdef FEAT_MBYTE
9655 && (!has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009656 || slang->sl_compminlen == 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009657 || mb_charlen(tword + sp->ts_splitoff)
9658 >= slang->sl_compminlen)
9659#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00009660 && (slang->sl_compsylmax < MAXWLEN
9661 || sp->ts_complen + 1 - sp->ts_compsplit
9662 < slang->sl_compmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00009663 && (byte_in_str(sp->ts_complen == sp->ts_compsplit
Bram Moolenaard12a1322005-08-21 22:08:24 +00009664 ? slang->sl_compstartflags
9665 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00009666 ((unsigned)flags >> 24))))
Bram Moolenaar5195e452005-08-19 20:32:47 +00009667 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009668 try_compound = TRUE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009669 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
9670 compflags[sp->ts_complen + 1] = NUL;
9671 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00009672
Bram Moolenaar78622822005-08-23 21:00:13 +00009673 /* For NOBREAK we never try splitting, it won't make any
9674 * word valid. */
9675 if (slang->sl_nobreak)
9676 try_compound = TRUE;
9677
Bram Moolenaard12a1322005-08-21 22:08:24 +00009678 /* If we could add a compound word, and it's also possible
9679 * to split at this point, do the split first and set
9680 * TSF_DIDSPLIT to avoid doing it again. */
Bram Moolenaar78622822005-08-23 21:00:13 +00009681 else if (!fword_ends
Bram Moolenaard12a1322005-08-21 22:08:24 +00009682 && try_compound
9683 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009684 {
9685 try_compound = FALSE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009686 sp->ts_flags |= TSF_DIDSPLIT;
9687 --sp->ts_curi; /* do the same NUL again */
9688 compflags[sp->ts_complen] = NUL;
9689 }
9690 else
9691 sp->ts_flags &= ~TSF_DIDSPLIT;
9692
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009693 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +00009694 {
9695 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009696 * words so far are valid for compounding. If there
9697 * is only one word it must not have the NEEDCOMPOUND
9698 * flag. */
9699 if (sp->ts_complen == sp->ts_compsplit
9700 && (flags & WF_NEEDCOMP))
9701 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00009702 p = preword;
9703 while (*skiptowhite(p) != NUL)
9704 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +00009705 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +00009706 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00009707 compflags + sp->ts_compsplit))
9708 break;
9709 newscore += SCORE_SPLIT;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009710 }
9711
9712 if (try_deeper(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009713 {
9714 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009715 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009716 sp->ts_state = STATE_SPLITUNDO;
9717
9718 ++depth;
9719 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009720
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009721 /* Append a space to preword when splitting. */
9722 if (!try_compound && !fword_ends)
9723 STRCAT(preword, " ");
Bram Moolenaar5195e452005-08-19 20:32:47 +00009724 sp->ts_prewordlen = STRLEN(preword);
9725 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +00009726 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009727
9728 /* If the badword has a non-word character at this
9729 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009730 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009731 * character when the word ends. But only when the
9732 * good word can end. */
9733 if (((!try_compound
9734 && !spell_iswordp_nmw(fword + sp->ts_fidx))
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009735 || fword_ends)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009736 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009737 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009738 int l;
9739
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009740#ifdef FEAT_MBYTE
9741 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009742 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009743 else
9744#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009745 l = 1;
9746 if (fword_ends)
9747 {
9748 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009749 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009750 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009751 sp->ts_prewordlen += l;
9752 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009753 }
9754 else
9755 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
9756 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009757 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00009758
Bram Moolenaard12a1322005-08-21 22:08:24 +00009759 /* When compounding include compound flag in
9760 * compflags[] (already set above). When splitting we
9761 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009762 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +00009763 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009764 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00009765 sp->ts_compsplit = sp->ts_complen;
9766 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009767
Bram Moolenaar53805d12005-08-01 07:08:33 +00009768 /* set su->su_badflags to the caps type at this
9769 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009770#ifdef FEAT_MBYTE
9771 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00009772 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009773 else
9774#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00009775 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009776 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00009777 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009778
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009779 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009780 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009781
9782 /* If there are postponed prefixes, try these too. */
9783 if (pbyts != NULL)
9784 {
9785 byts = pbyts;
9786 idxs = pidxs;
9787 sp->ts_prefixdepth = PFD_PREFIXTREE;
9788 sp->ts_state = STATE_NOPREFIX;
9789 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009790 }
9791 }
9792 break;
9793
9794 case STATE_SPLITUNDO:
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009795 /* Undo the changes done for word split or compound word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009796 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009797
9798 /* Continue looking for NUL bytes. */
9799 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009800
9801 /* In case we went into the prefix tree. */
9802 byts = fbyts;
9803 idxs = fidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009804 break;
9805
9806 case STATE_ENDNUL:
9807 /* Past the NUL bytes in the node. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00009808 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009809 if (fword[sp->ts_fidx] == NUL
9810#ifdef FEAT_MBYTE
9811 && sp->ts_tcharlen == 0
9812#endif
9813 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009814 {
9815 /* The badword ends, can't use the bytes in this node. */
9816 sp->ts_state = STATE_DEL;
9817 break;
9818 }
9819 sp->ts_state = STATE_PLAIN;
9820 /*FALLTHROUGH*/
9821
9822 case STATE_PLAIN:
9823 /*
9824 * Go over all possible bytes at this node, add each to
9825 * tword[] and use child node. "ts_curi" is the index.
9826 */
9827 arridx = sp->ts_arridx;
9828 if (sp->ts_curi > byts[arridx])
9829 {
9830 /* Done all bytes at this node, do next state. When still
9831 * at already changed bytes skip the other tricks. */
9832 if (sp->ts_fidx >= sp->ts_fidxtry)
9833 sp->ts_state = STATE_DEL;
9834 else
9835 sp->ts_state = STATE_FINAL;
9836 }
9837 else
9838 {
9839 arridx += sp->ts_curi++;
9840 c = byts[arridx];
9841
9842 /* Normal byte, go one level deeper. If it's not equal to
9843 * the byte in the bad word adjust the score. But don't
9844 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00009845 if (c == fword[sp->ts_fidx]
9846#ifdef FEAT_MBYTE
9847 || (sp->ts_tcharlen > 0
9848 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009849#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00009850 )
9851 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009852 else
9853 newscore = SCORE_SUBST;
9854 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
9855 && try_deeper(su, stack, depth, newscore))
9856 {
9857 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009858 sp = &stack[depth];
9859 ++sp->ts_fidx;
9860 tword[sp->ts_twordlen++] = c;
9861 sp->ts_arridx = idxs[arridx];
9862#ifdef FEAT_MBYTE
9863 if (newscore == SCORE_SUBST)
9864 sp->ts_isdiff = DIFF_YES;
9865 if (has_mbyte)
9866 {
9867 /* Multi-byte characters are a bit complicated to
9868 * handle: They differ when any of the bytes
9869 * differ and then their length may also differ. */
9870 if (sp->ts_tcharlen == 0)
9871 {
9872 /* First byte. */
9873 sp->ts_tcharidx = 0;
9874 sp->ts_tcharlen = MB_BYTE2LEN(c);
9875 sp->ts_fcharstart = sp->ts_fidx - 1;
9876 sp->ts_isdiff = (newscore != 0)
9877 ? DIFF_YES : DIFF_NONE;
9878 }
9879 else if (sp->ts_isdiff == DIFF_INSERT)
9880 /* When inserting trail bytes don't advance in
9881 * the bad word. */
9882 --sp->ts_fidx;
9883 if (++sp->ts_tcharidx == sp->ts_tcharlen)
9884 {
9885 /* Last byte of character. */
9886 if (sp->ts_isdiff == DIFF_YES)
9887 {
9888 /* Correct ts_fidx for the byte length of
9889 * the character (we didn't check that
9890 * before). */
9891 sp->ts_fidx = sp->ts_fcharstart
9892 + MB_BYTE2LEN(
9893 fword[sp->ts_fcharstart]);
9894
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00009895 /* For changing a composing character
9896 * adjust the score from SCORE_SUBST to
9897 * SCORE_SUBCOMP. */
9898 if (enc_utf8
9899 && utf_iscomposing(
9900 mb_ptr2char(tword
9901 + sp->ts_twordlen
9902 - sp->ts_tcharlen))
9903 && utf_iscomposing(
9904 mb_ptr2char(fword
9905 + sp->ts_fcharstart)))
9906 sp->ts_score -=
9907 SCORE_SUBST - SCORE_SUBCOMP;
9908
Bram Moolenaarea424162005-06-16 21:51:00 +00009909 /* For a similar character adjust score
9910 * from SCORE_SUBST to SCORE_SIMILAR. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009911 else if (slang->sl_has_map
9912 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00009913 mb_ptr2char(tword
9914 + sp->ts_twordlen
9915 - sp->ts_tcharlen),
9916 mb_ptr2char(fword
9917 + sp->ts_fcharstart)))
9918 sp->ts_score -=
9919 SCORE_SUBST - SCORE_SIMILAR;
9920 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009921 else if (sp->ts_isdiff == DIFF_INSERT
9922 && sp->ts_twordlen > sp->ts_tcharlen)
9923 {
Bram Moolenaarea408852005-06-25 22:49:46 +00009924 p = tword + sp->ts_twordlen
9925 - sp->ts_tcharlen;
9926 c = mb_ptr2char(p);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009927 if (enc_utf8 && utf_iscomposing(c))
9928 {
9929 /* Inserting a composing char doesn't
9930 * count that much. */
Bram Moolenaarea408852005-06-25 22:49:46 +00009931 sp->ts_score -= SCORE_INS
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009932 - SCORE_INSCOMP;
9933 }
9934 else
9935 {
9936 /* If the previous character was the
9937 * same, thus doubling a character,
9938 * give a bonus to the score. */
9939 mb_ptr_back(tword, p);
9940 if (c == mb_ptr2char(p))
9941 sp->ts_score -= SCORE_INS
Bram Moolenaarea408852005-06-25 22:49:46 +00009942 - SCORE_INSDUP;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009943 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009944 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009945
9946 /* Starting a new char, reset the length. */
9947 sp->ts_tcharlen = 0;
9948 }
9949 }
9950 else
9951#endif
9952 {
9953 /* If we found a similar char adjust the score.
9954 * We do this after calling try_deeper() because
9955 * it's slow. */
9956 if (newscore != 0
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009957 && slang->sl_has_map
9958 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00009959 c, fword[sp->ts_fidx - 1]))
9960 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
9961 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009962 }
9963 }
9964 break;
9965
9966 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00009967#ifdef FEAT_MBYTE
9968 /* When past the first byte of a multi-byte char don't try
9969 * delete/insert/swap a character. */
9970 if (has_mbyte && sp->ts_tcharlen > 0)
9971 {
9972 sp->ts_state = STATE_FINAL;
9973 break;
9974 }
9975#endif
9976 /*
9977 * Try skipping one character in the bad word (delete it).
9978 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009979 sp->ts_state = STATE_INS;
9980 sp->ts_curi = 1;
9981 if (fword[sp->ts_fidx] != NUL
9982 && try_deeper(su, stack, depth, SCORE_DEL))
9983 {
9984 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00009985
9986 /* Advance over the character in fword[]. Give a bonus to
9987 * the score if the same character is following "nn" ->
9988 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00009989#ifdef FEAT_MBYTE
9990 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00009991 {
9992 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00009993 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00009994 if (enc_utf8 && utf_iscomposing(c))
9995 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
9996 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
Bram Moolenaarea408852005-06-25 22:49:46 +00009997 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
9998 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009999 else
10000#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000010001 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010002 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +000010003 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
10004 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
10005 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010006 break;
10007 }
10008 /*FALLTHROUGH*/
10009
10010 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +000010011 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010012 * node. */
10013 n = sp->ts_arridx;
10014 if (sp->ts_curi > byts[n])
10015 {
10016 /* Done all bytes at this node, do next state. */
10017 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010018 }
10019 else
10020 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010021 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010022 n += sp->ts_curi++;
10023 c = byts[n];
10024 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
10025 {
10026 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000010027 sp = &stack[depth];
10028 tword[sp->ts_twordlen++] = c;
10029 sp->ts_arridx = idxs[n];
10030#ifdef FEAT_MBYTE
10031 if (has_mbyte)
10032 {
10033 fl = MB_BYTE2LEN(c);
10034 if (fl > 1)
10035 {
10036 /* There are following bytes for the same
10037 * character. We must find all bytes before
10038 * trying delete/insert/swap/etc. */
10039 sp->ts_tcharlen = fl;
10040 sp->ts_tcharidx = 1;
10041 sp->ts_isdiff = DIFF_INSERT;
10042 }
10043 }
Bram Moolenaarea408852005-06-25 22:49:46 +000010044 else
10045 fl = 1;
10046 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000010047#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000010048 {
10049 /* If the previous character was the same, thus
10050 * doubling a character, give a bonus to the
10051 * score. */
10052 if (sp->ts_twordlen >= 2
10053 && tword[sp->ts_twordlen - 2] == c)
10054 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
10055 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010056 }
10057 }
10058 break;
10059
10060 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +000010061 /*
10062 * Swap two bytes in the bad word: "12" -> "21".
10063 * We change "fword" here, it's changed back afterwards.
10064 */
10065 p = fword + sp->ts_fidx;
10066 c = *p;
10067 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010068 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010069 /* End of word, can't swap or replace. */
10070 sp->ts_state = STATE_FINAL;
10071 break;
10072 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000010073
10074 /* Don't swap if the first character is not a word character.
10075 * SWAP3 etc. also don't make sense then. */
10076 if (!spell_iswordp(p, curbuf))
10077 {
10078 sp->ts_state = STATE_REP_INI;
10079 break;
10080 }
10081
Bram Moolenaarea424162005-06-16 21:51:00 +000010082#ifdef FEAT_MBYTE
10083 if (has_mbyte)
10084 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010085 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000010086 c = mb_ptr2char(p);
Bram Moolenaarbb15b652005-10-03 21:52:09 +000010087 if (!spell_iswordp(p + n, curbuf))
10088 c2 = c; /* don't swap non-word char */
10089 else
10090 c2 = mb_ptr2char(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +000010091 }
10092 else
10093#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000010094 {
10095 if (!spell_iswordp(p + 1, curbuf))
10096 c2 = c; /* don't swap non-word char */
10097 else
10098 c2 = p[1];
10099 }
10100
10101 /* When characters are identical, swap won't do anything.
10102 * Also get here if the second char is not a word character. */
Bram Moolenaarea424162005-06-16 21:51:00 +000010103 if (c == c2)
10104 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010105 sp->ts_state = STATE_SWAP3;
10106 break;
10107 }
10108 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
10109 {
10110 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010111 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000010112#ifdef FEAT_MBYTE
10113 if (has_mbyte)
10114 {
10115 fl = mb_char2len(c2);
10116 mch_memmove(p, p + n, fl);
10117 mb_char2bytes(c, p + fl);
10118 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
10119 }
10120 else
10121#endif
10122 {
10123 p[0] = c2;
10124 p[1] = c;
10125 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
10126 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010127 }
10128 else
10129 /* If this swap doesn't work then SWAP3 won't either. */
10130 sp->ts_state = STATE_REP_INI;
10131 break;
10132
Bram Moolenaarea424162005-06-16 21:51:00 +000010133 case STATE_UNSWAP:
10134 /* Undo the STATE_SWAP swap: "21" -> "12". */
10135 p = fword + sp->ts_fidx;
10136#ifdef FEAT_MBYTE
10137 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010138 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010139 n = MB_BYTE2LEN(*p);
10140 c = mb_ptr2char(p + n);
10141 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
10142 mb_char2bytes(c, p);
10143 }
10144 else
10145#endif
10146 {
10147 c = *p;
10148 *p = p[1];
10149 p[1] = c;
10150 }
10151 /*FALLTHROUGH*/
10152
10153 case STATE_SWAP3:
10154 /* Swap two bytes, skipping one: "123" -> "321". We change
10155 * "fword" here, it's changed back afterwards. */
10156 p = fword + sp->ts_fidx;
10157#ifdef FEAT_MBYTE
10158 if (has_mbyte)
10159 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010160 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000010161 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010162 fl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +000010163 c2 = mb_ptr2char(p + n);
Bram Moolenaarbb15b652005-10-03 21:52:09 +000010164 if (!spell_iswordp(p + n + fl, curbuf))
10165 c3 = c; /* don't swap non-word char */
10166 else
10167 c3 = mb_ptr2char(p + n + fl);
Bram Moolenaarea424162005-06-16 21:51:00 +000010168 }
10169 else
10170#endif
10171 {
10172 c = *p;
10173 c2 = p[1];
Bram Moolenaarbb15b652005-10-03 21:52:09 +000010174 if (!spell_iswordp(p + 2, curbuf))
10175 c3 = c; /* don't swap non-word char */
10176 else
10177 c3 = p[2];
Bram Moolenaarea424162005-06-16 21:51:00 +000010178 }
10179
10180 /* When characters are identical: "121" then SWAP3 result is
10181 * identical, ROT3L result is same as SWAP: "211", ROT3L
10182 * result is same as SWAP on next char: "112". Thus skip all
Bram Moolenaarbb15b652005-10-03 21:52:09 +000010183 * swapping. Also skip when c3 is NUL.
10184 * Also get here when the third character is not a word
10185 * character. Second character may any char: "a.b" -> "b.a" */
Bram Moolenaarea424162005-06-16 21:51:00 +000010186 if (c == c3 || c3 == NUL)
10187 {
10188 sp->ts_state = STATE_REP_INI;
10189 break;
10190 }
10191 if (try_deeper(su, stack, depth, SCORE_SWAP3))
10192 {
10193 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010194 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000010195#ifdef FEAT_MBYTE
10196 if (has_mbyte)
10197 {
10198 tl = mb_char2len(c3);
10199 mch_memmove(p, p + n + fl, tl);
10200 mb_char2bytes(c2, p + tl);
10201 mb_char2bytes(c, p + fl + tl);
10202 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
10203 }
10204 else
10205#endif
10206 {
10207 p[0] = p[2];
10208 p[2] = c;
10209 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
10210 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010211 }
10212 else
10213 sp->ts_state = STATE_REP_INI;
10214 break;
10215
Bram Moolenaarea424162005-06-16 21:51:00 +000010216 case STATE_UNSWAP3:
10217 /* Undo STATE_SWAP3: "321" -> "123" */
10218 p = fword + sp->ts_fidx;
10219#ifdef FEAT_MBYTE
10220 if (has_mbyte)
10221 {
10222 n = MB_BYTE2LEN(*p);
10223 c2 = mb_ptr2char(p + n);
10224 fl = MB_BYTE2LEN(p[n]);
10225 c = mb_ptr2char(p + n + fl);
10226 tl = MB_BYTE2LEN(p[n + fl]);
10227 mch_memmove(p + fl + tl, p, n);
10228 mb_char2bytes(c, p);
10229 mb_char2bytes(c2, p + tl);
Bram Moolenaarbb15b652005-10-03 21:52:09 +000010230 p = p + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +000010231 }
10232 else
10233#endif
10234 {
10235 c = *p;
10236 *p = p[2];
10237 p[2] = c;
Bram Moolenaarbb15b652005-10-03 21:52:09 +000010238 ++p;
10239 }
10240
10241 if (!spell_iswordp(p, curbuf))
10242 {
10243 /* Middle char is not a word char, skip the rotate.
10244 * First and third char were already checked at swap
10245 * and swap3. */
10246 sp->ts_state = STATE_REP_INI;
10247 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000010248 }
Bram Moolenaarea424162005-06-16 21:51:00 +000010249
Bram Moolenaarea424162005-06-16 21:51:00 +000010250 /* Rotate three characters left: "123" -> "231". We change
10251 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010252 if (try_deeper(su, stack, depth, SCORE_SWAP3))
10253 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010254 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010255 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000010256 p = fword + sp->ts_fidx;
10257#ifdef FEAT_MBYTE
10258 if (has_mbyte)
10259 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010260 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000010261 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010262 fl = mb_cptr2len(p + n);
10263 fl += mb_cptr2len(p + n + fl);
Bram Moolenaarea424162005-06-16 21:51:00 +000010264 mch_memmove(p, p + n, fl);
10265 mb_char2bytes(c, p + fl);
10266 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
10267 }
10268 else
10269#endif
10270 {
10271 c = *p;
10272 *p = p[1];
10273 p[1] = p[2];
10274 p[2] = c;
10275 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
10276 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010277 }
10278 else
10279 sp->ts_state = STATE_REP_INI;
10280 break;
10281
Bram Moolenaarea424162005-06-16 21:51:00 +000010282 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +000010283 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +000010284 p = fword + sp->ts_fidx;
10285#ifdef FEAT_MBYTE
10286 if (has_mbyte)
10287 {
10288 n = MB_BYTE2LEN(*p);
10289 n += MB_BYTE2LEN(p[n]);
10290 c = mb_ptr2char(p + n);
10291 tl = MB_BYTE2LEN(p[n]);
10292 mch_memmove(p + tl, p, n);
10293 mb_char2bytes(c, p);
10294 }
10295 else
10296#endif
10297 {
10298 c = p[2];
10299 p[2] = p[1];
10300 p[1] = *p;
10301 *p = c;
10302 }
Bram Moolenaarea424162005-06-16 21:51:00 +000010303
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010304 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +000010305 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010306 if (try_deeper(su, stack, depth, SCORE_SWAP3))
10307 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010308 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010309 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000010310 p = fword + sp->ts_fidx;
10311#ifdef FEAT_MBYTE
10312 if (has_mbyte)
10313 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010314 n = mb_cptr2len(p);
10315 n += mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +000010316 c = mb_ptr2char(p + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010317 tl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +000010318 mch_memmove(p + tl, p, n);
10319 mb_char2bytes(c, p);
10320 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
10321 }
10322 else
10323#endif
10324 {
10325 c = p[2];
10326 p[2] = p[1];
10327 p[1] = *p;
10328 *p = c;
10329 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
10330 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010331 }
10332 else
10333 sp->ts_state = STATE_REP_INI;
10334 break;
10335
Bram Moolenaarea424162005-06-16 21:51:00 +000010336 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +000010337 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +000010338 p = fword + sp->ts_fidx;
10339#ifdef FEAT_MBYTE
10340 if (has_mbyte)
10341 {
10342 c = mb_ptr2char(p);
10343 tl = MB_BYTE2LEN(*p);
10344 n = MB_BYTE2LEN(p[tl]);
10345 n += MB_BYTE2LEN(p[tl + n]);
10346 mch_memmove(p, p + tl, n);
10347 mb_char2bytes(c, p + n);
10348 }
10349 else
10350#endif
10351 {
10352 c = *p;
10353 *p = p[1];
10354 p[1] = p[2];
10355 p[2] = c;
10356 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010357 /*FALLTHROUGH*/
10358
10359 case STATE_REP_INI:
10360 /* Check if matching with REP items from the .aff file would
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010361 * work. Quickly skip if:
10362 * - there are no REP items
10363 * - the score is going to be too high anyway
10364 * - already applied a REP item or swapped here */
10365 if (lp->lp_replang == NULL
10366 || sp->ts_score + SCORE_REP >= su->su_maxscore
10367 || sp->ts_fidx < sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010368 {
10369 sp->ts_state = STATE_FINAL;
10370 break;
10371 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010372 gap = &lp->lp_replang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010373
10374 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +000010375 * may match. If the index is -1 there is none. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010376 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010377 if (sp->ts_curi < 0)
10378 {
10379 sp->ts_state = STATE_FINAL;
10380 break;
10381 }
10382
10383 sp->ts_state = STATE_REP;
10384 /*FALLTHROUGH*/
10385
10386 case STATE_REP:
10387 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +000010388 * match replace the characters and check if the resulting
10389 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010390 p = fword + sp->ts_fidx;
10391
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010392 gap = &lp->lp_replang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010393 while (sp->ts_curi < gap->ga_len)
10394 {
10395 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
10396 if (*ftp->ft_from != *p)
10397 {
10398 /* past possible matching entries */
10399 sp->ts_curi = gap->ga_len;
10400 break;
10401 }
10402 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
10403 && try_deeper(su, stack, depth, SCORE_REP))
10404 {
10405 /* Need to undo this afterwards. */
10406 sp->ts_state = STATE_REP_UNDO;
10407
10408 /* Change the "from" to the "to" string. */
10409 ++depth;
10410 fl = STRLEN(ftp->ft_from);
10411 tl = STRLEN(ftp->ft_to);
10412 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +000010413 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010414 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010415 repextra += tl - fl;
10416 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010417 mch_memmove(p, ftp->ft_to, tl);
10418 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +000010419#ifdef FEAT_MBYTE
10420 stack[depth].ts_tcharlen = 0;
10421#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010422 break;
10423 }
10424 }
10425
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010426 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010427 /* No (more) matches. */
10428 sp->ts_state = STATE_FINAL;
10429
10430 break;
10431
10432 case STATE_REP_UNDO:
10433 /* Undo a REP replacement and continue with the next one. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010434 ftp = (fromto_T *)lp->lp_replang->sl_rep.ga_data
10435 + sp->ts_curi - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010436 fl = STRLEN(ftp->ft_from);
10437 tl = STRLEN(ftp->ft_to);
10438 p = fword + sp->ts_fidx;
10439 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +000010440 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010441 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010442 repextra -= tl - fl;
10443 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010444 mch_memmove(p, ftp->ft_from, fl);
10445 sp->ts_state = STATE_REP;
10446 break;
10447
10448 default:
10449 /* Did all possible states at this level, go up one level. */
10450 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010451
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010452 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010453 {
10454 /* Continue in or go back to the prefix tree. */
10455 byts = pbyts;
10456 idxs = pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010457 }
10458
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010459 /* Don't check for CTRL-C too often, it takes time. */
10460 line_breakcheck();
10461 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010462 }
10463 }
10464}
10465
10466/*
10467 * Try going one level deeper in the tree.
10468 */
10469 static int
10470try_deeper(su, stack, depth, score_add)
10471 suginfo_T *su;
10472 trystate_T *stack;
10473 int depth;
10474 int score_add;
10475{
10476 int newscore;
10477
10478 /* Refuse to go deeper if the scrore is getting too big. */
10479 newscore = stack[depth].ts_score + score_add;
10480 if (newscore >= su->su_maxscore)
10481 return FALSE;
10482
Bram Moolenaarea424162005-06-16 21:51:00 +000010483 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010484 stack[depth + 1].ts_state = STATE_START;
10485 stack[depth + 1].ts_score = newscore;
10486 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010487 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010488 return TRUE;
10489}
10490
Bram Moolenaar53805d12005-08-01 07:08:33 +000010491#ifdef FEAT_MBYTE
10492/*
10493 * Case-folding may change the number of bytes: Count nr of chars in
10494 * fword[flen] and return the byte length of that many chars in "word".
10495 */
10496 static int
10497nofold_len(fword, flen, word)
10498 char_u *fword;
10499 int flen;
10500 char_u *word;
10501{
10502 char_u *p;
10503 int i = 0;
10504
10505 for (p = fword; p < fword + flen; mb_ptr_adv(p))
10506 ++i;
10507 for (p = word; i > 0; mb_ptr_adv(p))
10508 --i;
10509 return (int)(p - word);
10510}
10511#endif
10512
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010513/*
10514 * "fword" is a good word with case folded. Find the matching keep-case
10515 * words and put it in "kword".
10516 * Theoretically there could be several keep-case words that result in the
10517 * same case-folded word, but we only find one...
10518 */
10519 static void
10520find_keepcap_word(slang, fword, kword)
10521 slang_T *slang;
10522 char_u *fword;
10523 char_u *kword;
10524{
10525 char_u uword[MAXWLEN]; /* "fword" in upper-case */
10526 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010527 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010528
10529 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010530 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010531 int round[MAXWLEN];
10532 int fwordidx[MAXWLEN];
10533 int uwordidx[MAXWLEN];
10534 int kwordlen[MAXWLEN];
10535
10536 int flen, ulen;
10537 int l;
10538 int len;
10539 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010540 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010541 char_u *p;
10542 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010543 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010544
10545 if (byts == NULL)
10546 {
10547 /* array is empty: "cannot happen" */
10548 *kword = NUL;
10549 return;
10550 }
10551
10552 /* Make an all-cap version of "fword". */
10553 allcap_copy(fword, uword);
10554
10555 /*
10556 * Each character needs to be tried both case-folded and upper-case.
10557 * All this gets very complicated if we keep in mind that changing case
10558 * may change the byte length of a multi-byte character...
10559 */
10560 depth = 0;
10561 arridx[0] = 0;
10562 round[0] = 0;
10563 fwordidx[0] = 0;
10564 uwordidx[0] = 0;
10565 kwordlen[0] = 0;
10566 while (depth >= 0)
10567 {
10568 if (fword[fwordidx[depth]] == NUL)
10569 {
10570 /* We are at the end of "fword". If the tree allows a word to end
10571 * here we have found a match. */
10572 if (byts[arridx[depth] + 1] == 0)
10573 {
10574 kword[kwordlen[depth]] = NUL;
10575 return;
10576 }
10577
10578 /* kword is getting too long, continue one level up */
10579 --depth;
10580 }
10581 else if (++round[depth] > 2)
10582 {
10583 /* tried both fold-case and upper-case character, continue one
10584 * level up */
10585 --depth;
10586 }
10587 else
10588 {
10589 /*
10590 * round[depth] == 1: Try using the folded-case character.
10591 * round[depth] == 2: Try using the upper-case character.
10592 */
10593#ifdef FEAT_MBYTE
10594 if (has_mbyte)
10595 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010596 flen = mb_cptr2len(fword + fwordidx[depth]);
10597 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010598 }
10599 else
10600#endif
10601 ulen = flen = 1;
10602 if (round[depth] == 1)
10603 {
10604 p = fword + fwordidx[depth];
10605 l = flen;
10606 }
10607 else
10608 {
10609 p = uword + uwordidx[depth];
10610 l = ulen;
10611 }
10612
10613 for (tryidx = arridx[depth]; l > 0; --l)
10614 {
10615 /* Perform a binary search in the list of accepted bytes. */
10616 len = byts[tryidx++];
10617 c = *p++;
10618 lo = tryidx;
10619 hi = tryidx + len - 1;
10620 while (lo < hi)
10621 {
10622 m = (lo + hi) / 2;
10623 if (byts[m] > c)
10624 hi = m - 1;
10625 else if (byts[m] < c)
10626 lo = m + 1;
10627 else
10628 {
10629 lo = hi = m;
10630 break;
10631 }
10632 }
10633
10634 /* Stop if there is no matching byte. */
10635 if (hi < lo || byts[lo] != c)
10636 break;
10637
10638 /* Continue at the child (if there is one). */
10639 tryidx = idxs[lo];
10640 }
10641
10642 if (l == 0)
10643 {
10644 /*
10645 * Found the matching char. Copy it to "kword" and go a
10646 * level deeper.
10647 */
10648 if (round[depth] == 1)
10649 {
10650 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
10651 flen);
10652 kwordlen[depth + 1] = kwordlen[depth] + flen;
10653 }
10654 else
10655 {
10656 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
10657 ulen);
10658 kwordlen[depth + 1] = kwordlen[depth] + ulen;
10659 }
10660 fwordidx[depth + 1] = fwordidx[depth] + flen;
10661 uwordidx[depth + 1] = uwordidx[depth] + ulen;
10662
10663 ++depth;
10664 arridx[depth] = tryidx;
10665 round[depth] = 0;
10666 }
10667 }
10668 }
10669
10670 /* Didn't find it: "cannot happen". */
10671 *kword = NUL;
10672}
10673
10674/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010675 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
10676 * su->su_sga.
10677 */
10678 static void
10679score_comp_sal(su)
10680 suginfo_T *su;
10681{
10682 langp_T *lp;
10683 char_u badsound[MAXWLEN];
10684 int i;
10685 suggest_T *stp;
10686 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010687 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010688 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010689
10690 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
10691 return;
10692
10693 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010694 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010695 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010696 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010697 if (lp->lp_slang->sl_sal.ga_len > 0)
10698 {
10699 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010700 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010701
10702 for (i = 0; i < su->su_ga.ga_len; ++i)
10703 {
10704 stp = &SUG(su->su_ga, i);
10705
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010706 /* Case-fold the suggested word, sound-fold it and compute the
10707 * sound-a-like score. */
10708 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010709 if (score < SCORE_MAXMAX)
10710 {
10711 /* Add the suggestion. */
10712 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
10713 sstp->st_word = vim_strsave(stp->st_word);
10714 if (sstp->st_word != NULL)
10715 {
10716 sstp->st_score = score;
10717 sstp->st_altscore = 0;
10718 sstp->st_orglen = stp->st_orglen;
10719 ++su->su_sga.ga_len;
10720 }
10721 }
10722 }
10723 break;
10724 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010725 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010726}
10727
10728/*
10729 * Combine the list of suggestions in su->su_ga and su->su_sga.
10730 * They are intwined.
10731 */
10732 static void
10733score_combine(su)
10734 suginfo_T *su;
10735{
10736 int i;
10737 int j;
10738 garray_T ga;
10739 garray_T *gap;
10740 langp_T *lp;
10741 suggest_T *stp;
10742 char_u *p;
10743 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010744 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010745 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010746
10747 /* Add the alternate score to su_ga. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010748 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010749 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010750 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010751 if (lp->lp_slang->sl_sal.ga_len > 0)
10752 {
10753 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010754 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010755
10756 for (i = 0; i < su->su_ga.ga_len; ++i)
10757 {
10758 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010759 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
10760 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010761 if (stp->st_altscore == SCORE_MAXMAX)
10762 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
10763 else
10764 stp->st_score = (stp->st_score * 3
10765 + stp->st_altscore) / 4;
10766 stp->st_salscore = FALSE;
10767 }
10768 break;
10769 }
10770 }
10771
10772 /* Add the alternate score to su_sga. */
10773 for (i = 0; i < su->su_sga.ga_len; ++i)
10774 {
10775 stp = &SUG(su->su_sga, i);
10776 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
10777 if (stp->st_score == SCORE_MAXMAX)
10778 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
10779 else
10780 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
10781 stp->st_salscore = TRUE;
10782 }
10783
10784 /* Sort the suggestions and truncate at "maxcount" for both lists. */
10785 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10786 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
10787
10788 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
10789 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
10790 return;
10791
10792 stp = &SUG(ga, 0);
10793 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
10794 {
10795 /* round 1: get a suggestion from su_ga
10796 * round 2: get a suggestion from su_sga */
10797 for (round = 1; round <= 2; ++round)
10798 {
10799 gap = round == 1 ? &su->su_ga : &su->su_sga;
10800 if (i < gap->ga_len)
10801 {
10802 /* Don't add a word if it's already there. */
10803 p = SUG(*gap, i).st_word;
10804 for (j = 0; j < ga.ga_len; ++j)
10805 if (STRCMP(stp[j].st_word, p) == 0)
10806 break;
10807 if (j == ga.ga_len)
10808 stp[ga.ga_len++] = SUG(*gap, i);
10809 else
10810 vim_free(p);
10811 }
10812 }
10813 }
10814
10815 ga_clear(&su->su_ga);
10816 ga_clear(&su->su_sga);
10817
10818 /* Truncate the list to the number of suggestions that will be displayed. */
10819 if (ga.ga_len > su->su_maxcount)
10820 {
10821 for (i = su->su_maxcount; i < ga.ga_len; ++i)
10822 vim_free(stp[i].st_word);
10823 ga.ga_len = su->su_maxcount;
10824 }
10825
10826 su->su_ga = ga;
10827}
10828
10829/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010830 * For the goodword in "stp" compute the soundalike score compared to the
10831 * badword.
10832 */
10833 static int
10834stp_sal_score(stp, su, slang, badsound)
10835 suggest_T *stp;
10836 suginfo_T *su;
10837 slang_T *slang;
10838 char_u *badsound; /* sound-folded badword */
10839{
10840 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010841 char_u *pbad;
10842 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010843 char_u badsound2[MAXWLEN];
10844 char_u fword[MAXWLEN];
10845 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010846 char_u goodword[MAXWLEN];
10847 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010848
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010849 lendiff = (int)(su->su_badlen - stp->st_orglen);
10850 if (lendiff >= 0)
10851 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010852 else
10853 {
10854 /* soundfold the bad word with more characters following */
10855 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
10856
10857 /* When joining two words the sound often changes a lot. E.g., "t he"
10858 * sounds like "t h" while "the" sounds like "@". Avoid that by
10859 * removing the space. Don't do it when the good word also contains a
10860 * space. */
10861 if (vim_iswhite(su->su_badptr[su->su_badlen])
10862 && *skiptowhite(stp->st_word) == NUL)
10863 for (p = fword; *(p = skiptowhite(p)) != NUL; )
10864 mch_memmove(p, p + 1, STRLEN(p));
10865
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010866 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010867 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010868 }
10869
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010870 if (lendiff > 0)
10871 {
10872 /* Add part of the bad word to the good word, so that we soundfold
10873 * what replaces the bad word. */
10874 STRCPY(goodword, stp->st_word);
10875 STRNCAT(goodword, su->su_badptr + su->su_badlen - lendiff, lendiff);
10876 pgood = goodword;
10877 }
10878 else
10879 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010880
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010881 /* Sound-fold the word and compute the score for the difference. */
10882 spell_soundfold(slang, pgood, FALSE, goodsound);
10883
10884 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010885}
10886
10887/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010888 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010889 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010890 */
10891 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000010892suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010893 suginfo_T *su;
10894{
10895 char_u salword[MAXWLEN];
10896 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010897 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010898 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010899 int curi[MAXWLEN];
10900 langp_T *lp;
10901 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010902 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010903 int depth;
10904 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010905 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010906 int round;
10907 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010908 int sound_score;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010909 int local_score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010910 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010911 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010912
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010913 /* Do this for all languages that support sound folding. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010914 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010915 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010916 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
10917 slang = lp->lp_slang;
10918 if (slang->sl_sal.ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010919 {
10920 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010921 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010922
10923 /*
10924 * Go through the whole tree, soundfold each word and compare.
10925 * round 1: use the case-folded tree.
10926 * round 2: use the keep-case tree.
10927 */
10928 for (round = 1; round <= 2; ++round)
10929 {
10930 if (round == 1)
10931 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010932 byts = slang->sl_fbyts;
10933 idxs = slang->sl_fidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010934 }
10935 else
10936 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010937 byts = slang->sl_kbyts;
10938 idxs = slang->sl_kidxs;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010939 if (byts == NULL) /* no keep-case words */
10940 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010941 }
10942
10943 depth = 0;
10944 arridx[0] = 0;
10945 curi[0] = 1;
10946 while (depth >= 0 && !got_int)
10947 {
10948 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010949 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010950 /* Done all bytes at this node, go up one level. */
10951 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010952 line_breakcheck();
10953 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010954 else
10955 {
10956 /* Do one more byte at this node. */
10957 n = arridx[depth] + curi[depth];
10958 ++curi[depth];
10959 c = byts[n];
10960 if (c == 0)
10961 {
10962 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010963 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010964 if (round == 2 || (flags & WF_KEEPCAP) == 0)
10965 {
10966 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010967 /* Sound-fold. Only in keep-case tree need to
10968 * case-fold the word. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010969 spell_soundfold(slang, tword,
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010970 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010971
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010972 /* Compute the edit distance between the
10973 * sound-a-like words. */
10974 sound_score = soundalike_score(salword,
10975 tsalword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010976
10977 /* Add a penalty for words in another region. */
10978 if ((flags & WF_REGION) && (((unsigned)flags
10979 >> 16) & lp->lp_region) == 0)
10980 local_score = SCORE_REGION;
10981 else
10982 local_score = 0;
10983 sound_score += local_score;
10984
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010985 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010986 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010987 char_u cword[MAXWLEN];
10988 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010989 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010990
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010991 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010992 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010993 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010994 /* Need to fix case according to
10995 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010996 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010997 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010998 }
10999 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011000 p = tword;
11001
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011002 if (sps_flags & SPS_DOUBLE)
11003 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000011004 su->su_badlen,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011005 sound_score, 0, FALSE,
11006 lp->lp_sallang);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011007 else
11008 {
11009 /* Compute the score. */
11010 score = spell_edit_score(
Bram Moolenaar5195e452005-08-19 20:32:47 +000011011 su->su_badword, p)
11012 + local_score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011013 if (sps_flags & SPS_BEST)
11014 /* give a bonus for the good word
11015 * sounding the same as the bad
11016 * word */
11017 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000011018 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011019 RESCORE(score, sound_score),
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011020 sound_score, TRUE,
11021 lp->lp_sallang);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011022 else
11023 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000011024 su->su_badlen,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011025 score + sound_score,
11026 0, FALSE,
11027 lp->lp_sallang);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011028 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011029 }
11030 }
11031
11032 /* Skip over other NUL bytes. */
11033 while (byts[n + 1] == 0)
11034 {
11035 ++n;
11036 ++curi[depth];
11037 }
11038 }
11039 else
11040 {
11041 /* Normal char, go one level deeper. */
11042 tword[depth++] = c;
11043 arridx[depth] = idxs[n];
11044 curi[depth] = 1;
11045 }
11046 }
11047 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011048 }
11049 }
11050 }
11051}
11052
11053/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011054 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011055 */
11056 static void
11057make_case_word(fword, cword, flags)
11058 char_u *fword;
11059 char_u *cword;
11060 int flags;
11061{
11062 if (flags & WF_ALLCAP)
11063 /* Make it all upper-case */
11064 allcap_copy(fword, cword);
11065 else if (flags & WF_ONECAP)
11066 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011067 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011068 else
11069 /* Use goodword as-is. */
11070 STRCPY(cword, fword);
11071}
11072
Bram Moolenaarea424162005-06-16 21:51:00 +000011073/*
11074 * Use map string "map" for languages "lp".
11075 */
11076 static void
11077set_map_str(lp, map)
11078 slang_T *lp;
11079 char_u *map;
11080{
11081 char_u *p;
11082 int headc = 0;
11083 int c;
11084 int i;
11085
11086 if (*map == NUL)
11087 {
11088 lp->sl_has_map = FALSE;
11089 return;
11090 }
11091 lp->sl_has_map = TRUE;
11092
11093 /* Init the array and hash table empty. */
11094 for (i = 0; i < 256; ++i)
11095 lp->sl_map_array[i] = 0;
11096#ifdef FEAT_MBYTE
11097 hash_init(&lp->sl_map_hash);
11098#endif
11099
11100 /*
11101 * The similar characters are stored separated with slashes:
11102 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
11103 * before the same slash. For characters above 255 sl_map_hash is used.
11104 */
11105 for (p = map; *p != NUL; )
11106 {
11107#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011108 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000011109#else
11110 c = *p++;
11111#endif
11112 if (c == '/')
11113 headc = 0;
11114 else
11115 {
11116 if (headc == 0)
11117 headc = c;
11118
11119#ifdef FEAT_MBYTE
11120 /* Characters above 255 don't fit in sl_map_array[], put them in
11121 * the hash table. Each entry is the char, a NUL the headchar and
11122 * a NUL. */
11123 if (c >= 256)
11124 {
11125 int cl = mb_char2len(c);
11126 int headcl = mb_char2len(headc);
11127 char_u *b;
11128 hash_T hash;
11129 hashitem_T *hi;
11130
11131 b = alloc((unsigned)(cl + headcl + 2));
11132 if (b == NULL)
11133 return;
11134 mb_char2bytes(c, b);
11135 b[cl] = NUL;
11136 mb_char2bytes(headc, b + cl + 1);
11137 b[cl + 1 + headcl] = NUL;
11138 hash = hash_hash(b);
11139 hi = hash_lookup(&lp->sl_map_hash, b, hash);
11140 if (HASHITEM_EMPTY(hi))
11141 hash_add_item(&lp->sl_map_hash, hi, b, hash);
11142 else
11143 {
11144 /* This should have been checked when generating the .spl
11145 * file. */
11146 EMSG(_("E999: duplicate char in MAP entry"));
11147 vim_free(b);
11148 }
11149 }
11150 else
11151#endif
11152 lp->sl_map_array[c] = headc;
11153 }
11154 }
11155}
11156
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011157/*
11158 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
11159 * lines in the .aff file.
11160 */
11161 static int
11162similar_chars(slang, c1, c2)
11163 slang_T *slang;
11164 int c1;
11165 int c2;
11166{
Bram Moolenaarea424162005-06-16 21:51:00 +000011167 int m1, m2;
11168#ifdef FEAT_MBYTE
11169 char_u buf[MB_MAXBYTES];
11170 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011171
Bram Moolenaarea424162005-06-16 21:51:00 +000011172 if (c1 >= 256)
11173 {
11174 buf[mb_char2bytes(c1, buf)] = 0;
11175 hi = hash_find(&slang->sl_map_hash, buf);
11176 if (HASHITEM_EMPTY(hi))
11177 m1 = 0;
11178 else
11179 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
11180 }
11181 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011182#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000011183 m1 = slang->sl_map_array[c1];
11184 if (m1 == 0)
11185 return FALSE;
11186
11187
11188#ifdef FEAT_MBYTE
11189 if (c2 >= 256)
11190 {
11191 buf[mb_char2bytes(c2, buf)] = 0;
11192 hi = hash_find(&slang->sl_map_hash, buf);
11193 if (HASHITEM_EMPTY(hi))
11194 m2 = 0;
11195 else
11196 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
11197 }
11198 else
11199#endif
11200 m2 = slang->sl_map_array[c2];
11201
11202 return m1 == m2;
11203}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011204
11205/*
11206 * Add a suggestion to the list of suggestions.
11207 * Do not add a duplicate suggestion or suggestions with a bad score.
11208 * When "use_score" is not zero it's used, otherwise the score is computed
11209 * with spell_edit_score().
11210 */
11211 static void
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011212add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus, slang)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011213 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011214 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011215 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011216 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011217 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011218 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011219 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011220 slang_T *slang; /* language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011221{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011222 int goodlen = STRLEN(goodword); /* len of goodword changed */
11223 int badlen = badlenarg; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011224 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011225 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011226 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011227 hlf_T attr = HLF_COUNT;
Bram Moolenaar1e015462005-09-25 22:16:38 +000011228 char_u longword[MAXWLEN + 1];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011229 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011230
Bram Moolenaar1e015462005-09-25 22:16:38 +000011231 /* Check that the word really is valid. Esp. for banned words and for
11232 * split words, such as "the the". Need to append what follows to check
11233 * for that. */
11234 STRCPY(longword, goodword);
11235 vim_strncpy(longword + goodlen, su->su_badptr + badlen, MAXWLEN - goodlen);
11236 (void)spell_check(curwin, longword, &attr, NULL);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011237 if (attr != HLF_COUNT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011238 return;
11239
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011240 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
11241 * "thee the" is added next to changing the first "the" the "thee". */
11242 pgood = goodword + STRLEN(goodword);
11243 pbad = su->su_badptr + badlen;
11244 while (pgood > goodword && pbad > su->su_badptr)
Bram Moolenaar0c405862005-06-22 22:26:26 +000011245 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011246 mb_ptr_back(goodword, pgood);
11247 mb_ptr_back(su->su_badptr, pbad);
11248#ifdef FEAT_MBYTE
11249 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000011250 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011251 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
11252 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011253 }
11254 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011255#endif
11256 if (*pgood != *pbad)
11257 break;
11258 badlen = pbad - su->su_badptr;
11259 goodlen = pgood - goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011260 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011261 if (badlen == 0 && goodlen == 0)
11262 /* goodword doesn't change anything; may happen for "the the" changing
11263 * the first "the" to itself. */
11264 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011265
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011266 if (score <= su->su_maxscore)
11267 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011268 /* Check if the word is already there. Also check the length that is
11269 * being replaced "thes," -> "these" is a different suggestion from
11270 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011271 stp = &SUG(*gap, 0);
11272 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaar4effc802005-09-30 21:12:02 +000011273 if ((int)STRLEN(stp[i].st_word) == goodlen
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011274 && STRNCMP(stp[i].st_word, goodword, goodlen) == 0
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011275 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011276 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011277 /*
Bram Moolenaar4effc802005-09-30 21:12:02 +000011278 * Found it. Remember the word with the lowest score.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011279 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011280 if (stp[i].st_slang == NULL)
11281 stp[i].st_slang = slang;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011282
11283 new_sug.st_score = score;
11284 new_sug.st_altscore = altscore;
11285 new_sug.st_had_bonus = had_bonus;
11286
11287 if (stp[i].st_had_bonus != had_bonus)
11288 {
11289 /* Only one of the two had the soundalike score computed.
11290 * Need to do that for the other one now, otherwise the
11291 * scores can't be compared. This happens because
11292 * suggest_try_change() doesn't compute the soundalike
Bram Moolenaar4effc802005-09-30 21:12:02 +000011293 * word to keep it fast, while some special methods set
11294 * the soundalike score to zero. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011295 if (had_bonus)
11296 rescore_one(su, &stp[i]);
11297 else
11298 {
11299 new_sug.st_word = goodword;
11300 new_sug.st_slang = stp[i].st_slang;
11301 new_sug.st_orglen = badlen;
11302 rescore_one(su, &new_sug);
11303 }
11304 }
11305
11306 if (stp[i].st_score > new_sug.st_score)
11307 {
11308 stp[i].st_score = new_sug.st_score;
11309 stp[i].st_altscore = new_sug.st_altscore;
11310 stp[i].st_had_bonus = new_sug.st_had_bonus;
11311 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011312 break;
11313 }
11314
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011315 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011316 {
11317 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011318 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011319 stp->st_word = vim_strnsave(goodword, goodlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011320 if (stp->st_word != NULL)
11321 {
11322 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011323 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011324 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011325 stp->st_orglen = badlen;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011326 stp->st_slang = slang;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011327 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011328
11329 /* If we have too many suggestions now, sort the list and keep
11330 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011331 if (gap->ga_len > SUG_MAX_COUNT(su))
11332 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
11333 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011334 }
11335 }
11336 }
11337}
11338
11339/*
11340 * Add a word to be banned.
11341 */
11342 static void
11343add_banned(su, word)
11344 suginfo_T *su;
11345 char_u *word;
11346{
11347 char_u *s = vim_strsave(word);
11348 hash_T hash;
11349 hashitem_T *hi;
11350
11351 if (s != NULL)
11352 {
11353 hash = hash_hash(s);
11354 hi = hash_lookup(&su->su_banned, s, hash);
11355 if (HASHITEM_EMPTY(hi))
11356 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000011357 else
11358 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011359 }
11360}
11361
11362/*
11363 * Return TRUE if a word appears in the list of banned words.
11364 */
11365 static int
11366was_banned(su, word)
11367 suginfo_T *su;
11368 char_u *word;
11369{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011370 hashitem_T *hi = hash_find(&su->su_banned, word);
11371
11372 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011373}
11374
11375/*
11376 * Free the banned words in "su".
11377 */
11378 static void
11379free_banned(su)
11380 suginfo_T *su;
11381{
11382 int todo;
11383 hashitem_T *hi;
11384
11385 todo = su->su_banned.ht_used;
11386 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
11387 {
11388 if (!HASHITEM_EMPTY(hi))
11389 {
11390 vim_free(hi->hi_key);
11391 --todo;
11392 }
11393 }
11394 hash_clear(&su->su_banned);
11395}
11396
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011397/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011398 * Recompute the score for all suggestions if sound-folding is possible. This
11399 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011400 */
11401 static void
11402rescore_suggestions(su)
11403 suginfo_T *su;
11404{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011405 int i;
11406
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011407 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011408 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011409 rescore_one(su, &SUG(su->su_ga, i));
11410}
11411
11412/*
11413 * Recompute the score for one suggestion if sound-folding is possible.
11414 */
11415 static void
11416rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000011417 suginfo_T *su;
11418 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011419{
11420 slang_T *slang = stp->st_slang;
11421 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000011422 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011423
11424 /* Only rescore suggestions that have no sal score yet and do have a
11425 * language. */
11426 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
11427 {
11428 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000011429 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011430 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011431 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011432 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000011433 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011434 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000011435
11436 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011437 if (stp->st_altscore == SCORE_MAXMAX)
11438 stp->st_altscore = SCORE_BIG;
11439 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
11440 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011441 }
11442}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011443
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011444static int
11445#ifdef __BORLANDC__
11446_RTLENTRYF
11447#endif
11448sug_compare __ARGS((const void *s1, const void *s2));
11449
11450/*
11451 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000011452 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011453 */
11454 static int
11455#ifdef __BORLANDC__
11456_RTLENTRYF
11457#endif
11458sug_compare(s1, s2)
11459 const void *s1;
11460 const void *s2;
11461{
11462 suggest_T *p1 = (suggest_T *)s1;
11463 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011464 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011465
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011466 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000011467 {
11468 n = p1->st_altscore - p2->st_altscore;
11469 if (n == 0)
11470 n = STRICMP(p1->st_word, p2->st_word);
11471 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011472 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011473}
11474
11475/*
11476 * Cleanup the suggestions:
11477 * - Sort on score.
11478 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011479 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011480 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011481 static int
11482cleanup_suggestions(gap, maxscore, keep)
11483 garray_T *gap;
11484 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011485 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011486{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011487 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011488 int i;
11489
11490 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011491 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011492
11493 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011494 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011495 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011496 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011497 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011498 gap->ga_len = keep;
11499 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011500 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011501 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011502}
11503
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011504#if defined(FEAT_EVAL) || defined(PROTO)
11505/*
11506 * Soundfold a string, for soundfold().
11507 * Result is in allocated memory, NULL for an error.
11508 */
11509 char_u *
11510eval_soundfold(word)
11511 char_u *word;
11512{
11513 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011514 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011515 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011516
11517 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
11518 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011519 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011520 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011521 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011522 if (lp->lp_slang->sl_sal.ga_len > 0)
11523 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011524 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011525 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011526 return vim_strsave(sound);
11527 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011528 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011529
11530 /* No language with sound folding, return word as-is. */
11531 return vim_strsave(word);
11532}
11533#endif
11534
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011535/*
11536 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000011537 *
11538 * There are many ways to turn a word into a sound-a-like representation. The
11539 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
11540 * swedish name matching - survey and test of different algorithms" by Klas
11541 * Erikson.
11542 *
11543 * We support two methods:
11544 * 1. SOFOFROM/SOFOTO do a simple character mapping.
11545 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011546 */
11547 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011548spell_soundfold(slang, inword, folded, res)
11549 slang_T *slang;
11550 char_u *inword;
11551 int folded; /* "inword" is already case-folded */
11552 char_u *res;
11553{
11554 char_u fword[MAXWLEN];
11555 char_u *word;
11556
11557 if (slang->sl_sofo)
11558 /* SOFOFROM and SOFOTO used */
11559 spell_soundfold_sofo(slang, inword, res);
11560 else
11561 {
11562 /* SAL items used. Requires the word to be case-folded. */
11563 if (folded)
11564 word = inword;
11565 else
11566 {
11567 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
11568 word = fword;
11569 }
11570
11571#ifdef FEAT_MBYTE
11572 if (has_mbyte)
11573 spell_soundfold_wsal(slang, word, res);
11574 else
11575#endif
11576 spell_soundfold_sal(slang, word, res);
11577 }
11578}
11579
11580/*
11581 * Perform sound folding of "inword" into "res" according to SOFOFROM and
11582 * SOFOTO lines.
11583 */
11584 static void
11585spell_soundfold_sofo(slang, inword, res)
11586 slang_T *slang;
11587 char_u *inword;
11588 char_u *res;
11589{
11590 char_u *s;
11591 int ri = 0;
11592 int c;
11593
11594#ifdef FEAT_MBYTE
11595 if (has_mbyte)
11596 {
11597 int prevc = 0;
11598 int *ip;
11599
11600 /* The sl_sal_first[] table contains the translation for chars up to
11601 * 255, sl_sal the rest. */
11602 for (s = inword; *s != NUL; )
11603 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011604 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011605 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
11606 c = ' ';
11607 else if (c < 256)
11608 c = slang->sl_sal_first[c];
11609 else
11610 {
11611 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
11612 if (ip == NULL) /* empty list, can't match */
11613 c = NUL;
11614 else
11615 for (;;) /* find "c" in the list */
11616 {
11617 if (*ip == 0) /* not found */
11618 {
11619 c = NUL;
11620 break;
11621 }
11622 if (*ip == c) /* match! */
11623 {
11624 c = ip[1];
11625 break;
11626 }
11627 ip += 2;
11628 }
11629 }
11630
11631 if (c != NUL && c != prevc)
11632 {
11633 ri += mb_char2bytes(c, res + ri);
11634 if (ri + MB_MAXBYTES > MAXWLEN)
11635 break;
11636 prevc = c;
11637 }
11638 }
11639 }
11640 else
11641#endif
11642 {
11643 /* The sl_sal_first[] table contains the translation. */
11644 for (s = inword; (c = *s) != NUL; ++s)
11645 {
11646 if (vim_iswhite(c))
11647 c = ' ';
11648 else
11649 c = slang->sl_sal_first[c];
11650 if (c != NUL && (ri == 0 || res[ri - 1] != c))
11651 res[ri++] = c;
11652 }
11653 }
11654
11655 res[ri] = NUL;
11656}
11657
11658 static void
11659spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011660 slang_T *slang;
11661 char_u *inword;
11662 char_u *res;
11663{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011664 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011665 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011666 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011667 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011668 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011669 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011670 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011671 int n, k = 0;
11672 int z0;
11673 int k0;
11674 int n0;
11675 int c;
11676 int pri;
11677 int p0 = -333;
11678 int c0;
11679
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011680 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011681 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011682 if (slang->sl_rem_accents)
11683 {
11684 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011685 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011686 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011687 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011688 {
11689 *t++ = ' ';
11690 s = skipwhite(s);
11691 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011692 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011693 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011694 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011695 *t++ = *s;
11696 ++s;
11697 }
11698 }
11699 *t = NUL;
11700 }
11701 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011702 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011703
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011704 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011705
11706 /*
11707 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011708 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011709 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011710 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011711 while ((c = word[i]) != NUL)
11712 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011713 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011714 n = slang->sl_sal_first[c];
11715 z0 = 0;
11716
11717 if (n >= 0)
11718 {
11719 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011720 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011721 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011722 /* Quickly skip entries that don't match the word. Most
11723 * entries are less then three chars, optimize for that. */
11724 k = smp[n].sm_leadlen;
11725 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011726 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011727 if (word[i + 1] != s[1])
11728 continue;
11729 if (k > 2)
11730 {
11731 for (j = 2; j < k; ++j)
11732 if (word[i + j] != s[j])
11733 break;
11734 if (j < k)
11735 continue;
11736 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011737 }
11738
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011739 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011740 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011741 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011742 while (*pf != NUL && *pf != word[i + k])
11743 ++pf;
11744 if (*pf == NUL)
11745 continue;
11746 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011747 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011748 s = smp[n].sm_rules;
11749 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011750
11751 p0 = *s;
11752 k0 = k;
11753 while (*s == '-' && k > 1)
11754 {
11755 k--;
11756 s++;
11757 }
11758 if (*s == '<')
11759 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011760 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011761 {
11762 /* determine priority */
11763 pri = *s - '0';
11764 s++;
11765 }
11766 if (*s == '^' && *(s + 1) == '^')
11767 s++;
11768
11769 if (*s == NUL
11770 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011771 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011772 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011773 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011774 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011775 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011776 && spell_iswordp(word + i - 1, curbuf)
11777 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011778 {
11779 /* search for followup rules, if: */
11780 /* followup and k > 1 and NO '-' in searchstring */
11781 c0 = word[i + k - 1];
11782 n0 = slang->sl_sal_first[c0];
11783
11784 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011785 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011786 {
11787 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011788 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011789 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011790 /* Quickly skip entries that don't match the word.
11791 * */
11792 k0 = smp[n0].sm_leadlen;
11793 if (k0 > 1)
11794 {
11795 if (word[i + k] != s[1])
11796 continue;
11797 if (k0 > 2)
11798 {
11799 pf = word + i + k + 1;
11800 for (j = 2; j < k0; ++j)
11801 if (*pf++ != s[j])
11802 break;
11803 if (j < k0)
11804 continue;
11805 }
11806 }
11807 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011808
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011809 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011810 {
11811 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011812 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011813 while (*pf != NUL && *pf != word[i + k0])
11814 ++pf;
11815 if (*pf == NUL)
11816 continue;
11817 ++k0;
11818 }
11819
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011820 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011821 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011822 while (*s == '-')
11823 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011824 /* "k0" gets NOT reduced because
11825 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011826 s++;
11827 }
11828 if (*s == '<')
11829 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011830 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011831 {
11832 p0 = *s - '0';
11833 s++;
11834 }
11835
11836 if (*s == NUL
11837 /* *s == '^' cuts */
11838 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011839 && !spell_iswordp(word + i + k0,
11840 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011841 {
11842 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011843 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011844 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011845
11846 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011847 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011848 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011849 /* rule fits; stop search */
11850 break;
11851 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011852 }
11853
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011854 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011855 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011856 }
11857
11858 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011859 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011860 if (s == NULL)
11861 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011862 pf = smp[n].sm_rules;
11863 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011864 if (p0 == 1 && z == 0)
11865 {
11866 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011867 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
11868 || res[reslen - 1] == *s))
11869 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011870 z0 = 1;
11871 z = 1;
11872 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011873 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011874 {
11875 word[i + k0] = *s;
11876 k0++;
11877 s++;
11878 }
11879 if (k > k0)
11880 mch_memmove(word + i + k0, word + i + k,
11881 STRLEN(word + i + k) + 1);
11882
11883 /* new "actual letter" */
11884 c = word[i];
11885 }
11886 else
11887 {
11888 /* no '<' rule used */
11889 i += k - 1;
11890 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011891 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011892 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011893 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011894 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011895 s++;
11896 }
11897 /* new "actual letter" */
11898 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011899 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011900 {
11901 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011902 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011903 mch_memmove(word, word + i + 1,
11904 STRLEN(word + i + 1) + 1);
11905 i = 0;
11906 z0 = 1;
11907 }
11908 }
11909 break;
11910 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011911 }
11912 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011913 else if (vim_iswhite(c))
11914 {
11915 c = ' ';
11916 k = 1;
11917 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011918
11919 if (z0 == 0)
11920 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011921 if (k && !p0 && reslen < MAXWLEN && c != NUL
11922 && (!slang->sl_collapse || reslen == 0
11923 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011924 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011925 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011926
11927 i++;
11928 z = 0;
11929 k = 0;
11930 }
11931 }
11932
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011933 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011934}
11935
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011936#ifdef FEAT_MBYTE
11937/*
11938 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
11939 * Multi-byte version of spell_soundfold().
11940 */
11941 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011942spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011943 slang_T *slang;
11944 char_u *inword;
11945 char_u *res;
11946{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011947 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011948 int word[MAXWLEN];
11949 int wres[MAXWLEN];
11950 int l;
11951 char_u *s;
11952 int *ws;
11953 char_u *t;
11954 int *pf;
11955 int i, j, z;
11956 int reslen;
11957 int n, k = 0;
11958 int z0;
11959 int k0;
11960 int n0;
11961 int c;
11962 int pri;
11963 int p0 = -333;
11964 int c0;
11965 int did_white = FALSE;
11966
11967 /*
11968 * Convert the multi-byte string to a wide-character string.
11969 * Remove accents, if wanted. We actually remove all non-word characters.
11970 * But keep white space.
11971 */
11972 n = 0;
11973 for (s = inword; *s != NUL; )
11974 {
11975 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011976 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011977 if (slang->sl_rem_accents)
11978 {
11979 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
11980 {
11981 if (did_white)
11982 continue;
11983 c = ' ';
11984 did_white = TRUE;
11985 }
11986 else
11987 {
11988 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011989 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011990 continue;
11991 }
11992 }
11993 word[n++] = c;
11994 }
11995 word[n] = NUL;
11996
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011997 /*
11998 * This comes from Aspell phonet.cpp.
11999 * Converted from C++ to C. Added support for multi-byte chars.
12000 * Changed to keep spaces.
12001 */
12002 i = reslen = z = 0;
12003 while ((c = word[i]) != NUL)
12004 {
12005 /* Start with the first rule that has the character in the word. */
12006 n = slang->sl_sal_first[c & 0xff];
12007 z0 = 0;
12008
12009 if (n >= 0)
12010 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012011 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012012 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
12013 {
12014 /* Quickly skip entries that don't match the word. Most
12015 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012016 if (c != ws[0])
12017 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012018 k = smp[n].sm_leadlen;
12019 if (k > 1)
12020 {
12021 if (word[i + 1] != ws[1])
12022 continue;
12023 if (k > 2)
12024 {
12025 for (j = 2; j < k; ++j)
12026 if (word[i + j] != ws[j])
12027 break;
12028 if (j < k)
12029 continue;
12030 }
12031 }
12032
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012033 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012034 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012035 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012036 while (*pf != NUL && *pf != word[i + k])
12037 ++pf;
12038 if (*pf == NUL)
12039 continue;
12040 ++k;
12041 }
12042 s = smp[n].sm_rules;
12043 pri = 5; /* default priority */
12044
12045 p0 = *s;
12046 k0 = k;
12047 while (*s == '-' && k > 1)
12048 {
12049 k--;
12050 s++;
12051 }
12052 if (*s == '<')
12053 s++;
12054 if (VIM_ISDIGIT(*s))
12055 {
12056 /* determine priority */
12057 pri = *s - '0';
12058 s++;
12059 }
12060 if (*s == '^' && *(s + 1) == '^')
12061 s++;
12062
12063 if (*s == NUL
12064 || (*s == '^'
12065 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000012066 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012067 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000012068 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012069 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000012070 && spell_iswordp_w(word + i - 1, curbuf)
12071 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012072 {
12073 /* search for followup rules, if: */
12074 /* followup and k > 1 and NO '-' in searchstring */
12075 c0 = word[i + k - 1];
12076 n0 = slang->sl_sal_first[c0 & 0xff];
12077
12078 if (slang->sl_followup && k > 1 && n0 >= 0
12079 && p0 != '-' && word[i + k] != NUL)
12080 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012081 /* Test follow-up rule for "word[i + k]"; loop over
12082 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012083 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
12084 == (c0 & 0xff); ++n0)
12085 {
12086 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012087 */
12088 if (c0 != ws[0])
12089 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012090 k0 = smp[n0].sm_leadlen;
12091 if (k0 > 1)
12092 {
12093 if (word[i + k] != ws[1])
12094 continue;
12095 if (k0 > 2)
12096 {
12097 pf = word + i + k + 1;
12098 for (j = 2; j < k0; ++j)
12099 if (*pf++ != ws[j])
12100 break;
12101 if (j < k0)
12102 continue;
12103 }
12104 }
12105 k0 += k - 1;
12106
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012107 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012108 {
12109 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012110 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012111 while (*pf != NUL && *pf != word[i + k0])
12112 ++pf;
12113 if (*pf == NUL)
12114 continue;
12115 ++k0;
12116 }
12117
12118 p0 = 5;
12119 s = smp[n0].sm_rules;
12120 while (*s == '-')
12121 {
12122 /* "k0" gets NOT reduced because
12123 * "if (k0 == k)" */
12124 s++;
12125 }
12126 if (*s == '<')
12127 s++;
12128 if (VIM_ISDIGIT(*s))
12129 {
12130 p0 = *s - '0';
12131 s++;
12132 }
12133
12134 if (*s == NUL
12135 /* *s == '^' cuts */
12136 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000012137 && !spell_iswordp_w(word + i + k0,
12138 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012139 {
12140 if (k0 == k)
12141 /* this is just a piece of the string */
12142 continue;
12143
12144 if (p0 < pri)
12145 /* priority too low */
12146 continue;
12147 /* rule fits; stop search */
12148 break;
12149 }
12150 }
12151
12152 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
12153 == (c0 & 0xff))
12154 continue;
12155 }
12156
12157 /* replace string */
12158 ws = smp[n].sm_to_w;
12159 s = smp[n].sm_rules;
12160 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
12161 if (p0 == 1 && z == 0)
12162 {
12163 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000012164 if (reslen > 0 && ws != NULL && *ws != NUL
12165 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012166 || wres[reslen - 1] == *ws))
12167 reslen--;
12168 z0 = 1;
12169 z = 1;
12170 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000012171 if (ws != NULL)
12172 while (*ws != NUL && word[i + k0] != NUL)
12173 {
12174 word[i + k0] = *ws;
12175 k0++;
12176 ws++;
12177 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012178 if (k > k0)
12179 mch_memmove(word + i + k0, word + i + k,
12180 sizeof(int) * (STRLEN(word + i + k) + 1));
12181
12182 /* new "actual letter" */
12183 c = word[i];
12184 }
12185 else
12186 {
12187 /* no '<' rule used */
12188 i += k - 1;
12189 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000012190 if (ws != NULL)
12191 while (*ws != NUL && ws[1] != NUL
12192 && reslen < MAXWLEN)
12193 {
12194 if (reslen == 0 || wres[reslen - 1] != *ws)
12195 wres[reslen++] = *ws;
12196 ws++;
12197 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012198 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000012199 if (ws == NULL)
12200 c = NUL;
12201 else
12202 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012203 if (strstr((char *)s, "^^") != NULL)
12204 {
12205 if (c != NUL)
12206 wres[reslen++] = c;
12207 mch_memmove(word, word + i + 1,
12208 sizeof(int) * (STRLEN(word + i + 1) + 1));
12209 i = 0;
12210 z0 = 1;
12211 }
12212 }
12213 break;
12214 }
12215 }
12216 }
12217 else if (vim_iswhite(c))
12218 {
12219 c = ' ';
12220 k = 1;
12221 }
12222
12223 if (z0 == 0)
12224 {
12225 if (k && !p0 && reslen < MAXWLEN && c != NUL
12226 && (!slang->sl_collapse || reslen == 0
12227 || wres[reslen - 1] != c))
12228 /* condense only double letters */
12229 wres[reslen++] = c;
12230
12231 i++;
12232 z = 0;
12233 k = 0;
12234 }
12235 }
12236
12237 /* Convert wide characters in "wres" to a multi-byte string in "res". */
12238 l = 0;
12239 for (n = 0; n < reslen; ++n)
12240 {
12241 l += mb_char2bytes(wres[n], res + l);
12242 if (l + MB_MAXBYTES > MAXWLEN)
12243 break;
12244 }
12245 res[l] = NUL;
12246}
12247#endif
12248
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012249/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012250 * Compute a score for two sound-a-like words.
12251 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
12252 * Instead of a generic loop we write out the code. That keeps it fast by
12253 * avoiding checks that will not be possible.
12254 */
12255 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012256soundalike_score(goodstart, badstart)
12257 char_u *goodstart; /* sound-folded good word */
12258 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012259{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012260 char_u *goodsound = goodstart;
12261 char_u *badsound = badstart;
12262 int goodlen;
12263 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012264 int n;
12265 char_u *pl, *ps;
12266 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012267 int score = 0;
12268
12269 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
12270 * counted so much, vowels halfway the word aren't counted at all. */
12271 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
12272 {
12273 score = SCORE_DEL / 2;
12274 if (*badsound == '*')
12275 ++badsound;
12276 else
12277 ++goodsound;
12278 }
12279
12280 goodlen = STRLEN(goodsound);
12281 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012282
12283 /* Return quickly if the lenghts are too different to be fixed by two
12284 * changes. */
12285 n = goodlen - badlen;
12286 if (n < -2 || n > 2)
12287 return SCORE_MAXMAX;
12288
12289 if (n > 0)
12290 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012291 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012292 ps = badsound;
12293 }
12294 else
12295 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012296 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012297 ps = goodsound;
12298 }
12299
12300 /* Skip over the identical part. */
12301 while (*pl == *ps && *pl != NUL)
12302 {
12303 ++pl;
12304 ++ps;
12305 }
12306
12307 switch (n)
12308 {
12309 case -2:
12310 case 2:
12311 /*
12312 * Must delete two characters from "pl".
12313 */
12314 ++pl; /* first delete */
12315 while (*pl == *ps)
12316 {
12317 ++pl;
12318 ++ps;
12319 }
12320 /* strings must be equal after second delete */
12321 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012322 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012323
12324 /* Failed to compare. */
12325 break;
12326
12327 case -1:
12328 case 1:
12329 /*
12330 * Minimal one delete from "pl" required.
12331 */
12332
12333 /* 1: delete */
12334 pl2 = pl + 1;
12335 ps2 = ps;
12336 while (*pl2 == *ps2)
12337 {
12338 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012339 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012340 ++pl2;
12341 ++ps2;
12342 }
12343
12344 /* 2: delete then swap, then rest must be equal */
12345 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
12346 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012347 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012348
12349 /* 3: delete then substitute, then the rest must be equal */
12350 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012351 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012352
12353 /* 4: first swap then delete */
12354 if (pl[0] == ps[1] && pl[1] == ps[0])
12355 {
12356 pl2 = pl + 2; /* swap, skip two chars */
12357 ps2 = ps + 2;
12358 while (*pl2 == *ps2)
12359 {
12360 ++pl2;
12361 ++ps2;
12362 }
12363 /* delete a char and then strings must be equal */
12364 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012365 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012366 }
12367
12368 /* 5: first substitute then delete */
12369 pl2 = pl + 1; /* substitute, skip one char */
12370 ps2 = ps + 1;
12371 while (*pl2 == *ps2)
12372 {
12373 ++pl2;
12374 ++ps2;
12375 }
12376 /* delete a char and then strings must be equal */
12377 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012378 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012379
12380 /* Failed to compare. */
12381 break;
12382
12383 case 0:
12384 /*
12385 * Lenghts are equal, thus changes must result in same length: An
12386 * insert is only possible in combination with a delete.
12387 * 1: check if for identical strings
12388 */
12389 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012390 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012391
12392 /* 2: swap */
12393 if (pl[0] == ps[1] && pl[1] == ps[0])
12394 {
12395 pl2 = pl + 2; /* swap, skip two chars */
12396 ps2 = ps + 2;
12397 while (*pl2 == *ps2)
12398 {
12399 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012400 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012401 ++pl2;
12402 ++ps2;
12403 }
12404 /* 3: swap and swap again */
12405 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
12406 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012407 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012408
12409 /* 4: swap and substitute */
12410 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012411 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012412 }
12413
12414 /* 5: substitute */
12415 pl2 = pl + 1;
12416 ps2 = ps + 1;
12417 while (*pl2 == *ps2)
12418 {
12419 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012420 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012421 ++pl2;
12422 ++ps2;
12423 }
12424
12425 /* 6: substitute and swap */
12426 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
12427 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012428 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012429
12430 /* 7: substitute and substitute */
12431 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012432 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012433
12434 /* 8: insert then delete */
12435 pl2 = pl;
12436 ps2 = ps + 1;
12437 while (*pl2 == *ps2)
12438 {
12439 ++pl2;
12440 ++ps2;
12441 }
12442 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012443 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012444
12445 /* 9: delete then insert */
12446 pl2 = pl + 1;
12447 ps2 = ps;
12448 while (*pl2 == *ps2)
12449 {
12450 ++pl2;
12451 ++ps2;
12452 }
12453 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012454 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012455
12456 /* Failed to compare. */
12457 break;
12458 }
12459
12460 return SCORE_MAXMAX;
12461}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012462
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012463/*
12464 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012465 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012466 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000012467 * The algorithm is described by Du and Chang, 1992.
12468 * The implementation of the algorithm comes from Aspell editdist.cpp,
12469 * edit_distance(). It has been converted from C++ to C and modified to
12470 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012471 */
12472 static int
12473spell_edit_score(badword, goodword)
12474 char_u *badword;
12475 char_u *goodword;
12476{
12477 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012478 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012479 int j, i;
12480 int t;
12481 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012482 int pbc, pgc;
12483#ifdef FEAT_MBYTE
12484 char_u *p;
12485 int wbadword[MAXWLEN];
12486 int wgoodword[MAXWLEN];
12487
12488 if (has_mbyte)
12489 {
12490 /* Get the characters from the multi-byte strings and put them in an
12491 * int array for easy access. */
12492 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012493 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000012494 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012495 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012496 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000012497 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012498 }
12499 else
12500#endif
12501 {
12502 badlen = STRLEN(badword) + 1;
12503 goodlen = STRLEN(goodword) + 1;
12504 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012505
12506 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
12507#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012508 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
12509 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012510 if (cnt == NULL)
12511 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012512
12513 CNT(0, 0) = 0;
12514 for (j = 1; j <= goodlen; ++j)
12515 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
12516
12517 for (i = 1; i <= badlen; ++i)
12518 {
12519 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
12520 for (j = 1; j <= goodlen; ++j)
12521 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012522#ifdef FEAT_MBYTE
12523 if (has_mbyte)
12524 {
12525 bc = wbadword[i - 1];
12526 gc = wgoodword[j - 1];
12527 }
12528 else
12529#endif
12530 {
12531 bc = badword[i - 1];
12532 gc = goodword[j - 1];
12533 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012534 if (bc == gc)
12535 CNT(i, j) = CNT(i - 1, j - 1);
12536 else
12537 {
12538 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012539 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012540 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
12541 else
12542 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
12543
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012544 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012545 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012546#ifdef FEAT_MBYTE
12547 if (has_mbyte)
12548 {
12549 pbc = wbadword[i - 2];
12550 pgc = wgoodword[j - 2];
12551 }
12552 else
12553#endif
12554 {
12555 pbc = badword[i - 2];
12556 pgc = goodword[j - 2];
12557 }
12558 if (bc == pgc && pbc == gc)
12559 {
12560 t = SCORE_SWAP + CNT(i - 2, j - 2);
12561 if (t < CNT(i, j))
12562 CNT(i, j) = t;
12563 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012564 }
12565 t = SCORE_DEL + CNT(i - 1, j);
12566 if (t < CNT(i, j))
12567 CNT(i, j) = t;
12568 t = SCORE_INS + CNT(i, j - 1);
12569 if (t < CNT(i, j))
12570 CNT(i, j) = t;
12571 }
12572 }
12573 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012574
12575 i = CNT(badlen - 1, goodlen - 1);
12576 vim_free(cnt);
12577 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012578}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000012579
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012580/*
12581 * ":spelldump"
12582 */
12583/*ARGSUSED*/
12584 void
12585ex_spelldump(eap)
12586 exarg_T *eap;
12587{
12588 buf_T *buf = curbuf;
12589 langp_T *lp;
12590 slang_T *slang;
12591 idx_T arridx[MAXWLEN];
12592 int curi[MAXWLEN];
12593 char_u word[MAXWLEN];
12594 int c;
12595 char_u *byts;
12596 idx_T *idxs;
12597 linenr_T lnum = 0;
12598 int round;
12599 int depth;
12600 int n;
12601 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000012602 char_u *region_names = NULL; /* region names being used */
12603 int do_region = TRUE; /* dump region names and numbers */
12604 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012605 int lpi;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012606
Bram Moolenaar95529562005-08-25 21:21:38 +000012607 if (no_spell_checking(curwin))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012608 return;
12609
12610 /* Create a new empty buffer by splitting the window. */
12611 do_cmdline_cmd((char_u *)"new");
12612 if (!bufempty() || !buf_valid(buf))
12613 return;
12614
Bram Moolenaar7887d882005-07-01 22:33:52 +000012615 /* Find out if we can support regions: All languages must support the same
12616 * regions or none at all. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012617 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000012618 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012619 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000012620 p = lp->lp_slang->sl_regions;
12621 if (p[0] != 0)
12622 {
12623 if (region_names == NULL) /* first language with regions */
12624 region_names = p;
12625 else if (STRCMP(region_names, p) != 0)
12626 {
12627 do_region = FALSE; /* region names are different */
12628 break;
12629 }
12630 }
12631 }
12632
12633 if (do_region && region_names != NULL)
12634 {
12635 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
12636 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
12637 }
12638 else
12639 do_region = FALSE;
12640
12641 /*
12642 * Loop over all files loaded for the entries in 'spelllang'.
12643 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012644 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012645 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012646 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012647 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012648 if (slang->sl_fbyts == NULL) /* reloading failed */
12649 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012650
12651 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
12652 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
12653
12654 /* round 1: case-folded tree
12655 * round 2: keep-case tree */
12656 for (round = 1; round <= 2; ++round)
12657 {
12658 if (round == 1)
12659 {
12660 byts = slang->sl_fbyts;
12661 idxs = slang->sl_fidxs;
12662 }
12663 else
12664 {
12665 byts = slang->sl_kbyts;
12666 idxs = slang->sl_kidxs;
12667 }
12668 if (byts == NULL)
12669 continue; /* array is empty */
12670
12671 depth = 0;
12672 arridx[0] = 0;
12673 curi[0] = 1;
12674 while (depth >= 0 && !got_int)
12675 {
12676 if (curi[depth] > byts[arridx[depth]])
12677 {
12678 /* Done all bytes at this node, go up one level. */
12679 --depth;
12680 line_breakcheck();
12681 }
12682 else
12683 {
12684 /* Do one more byte at this node. */
12685 n = arridx[depth] + curi[depth];
12686 ++curi[depth];
12687 c = byts[n];
12688 if (c == 0)
12689 {
12690 /* End of word, deal with the word.
12691 * Don't use keep-case words in the fold-case tree,
12692 * they will appear in the keep-case tree.
12693 * Only use the word when the region matches. */
12694 flags = (int)idxs[n];
12695 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012696 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000012697 && (do_region
12698 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012699 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012700 & lp->lp_region) != 0))
12701 {
12702 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000012703 if (!do_region)
12704 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000012705
12706 /* Dump the basic word if there is no prefix or
12707 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012708 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000012709 if (c == 0 || curi[depth] == 2)
12710 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012711
12712 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000012713 if (c != 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012714 lnum = dump_prefixes(slang, word, round,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012715 flags, lnum);
12716 }
12717 }
12718 else
12719 {
12720 /* Normal char, go one level deeper. */
12721 word[depth++] = c;
12722 arridx[depth] = idxs[n];
12723 curi[depth] = 1;
12724 }
12725 }
12726 }
12727 }
12728 }
12729
12730 /* Delete the empty line that we started with. */
12731 if (curbuf->b_ml.ml_line_count > 1)
12732 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
12733
12734 redraw_later(NOT_VALID);
12735}
12736
12737/*
12738 * Dump one word: apply case modifications and append a line to the buffer.
12739 */
12740 static void
12741dump_word(word, round, flags, lnum)
12742 char_u *word;
12743 int round;
12744 int flags;
12745 linenr_T lnum;
12746{
12747 int keepcap = FALSE;
12748 char_u *p;
12749 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000012750 char_u badword[MAXWLEN + 10];
12751 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012752
12753 if (round == 1 && (flags & WF_CAPMASK) != 0)
12754 {
12755 /* Need to fix case according to "flags". */
12756 make_case_word(word, cword, flags);
12757 p = cword;
12758 }
12759 else
12760 {
12761 p = word;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000012762 if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0
12763 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012764 keepcap = TRUE;
12765 }
12766
Bram Moolenaar7887d882005-07-01 22:33:52 +000012767 /* Add flags and regions after a slash. */
12768 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012769 {
Bram Moolenaar7887d882005-07-01 22:33:52 +000012770 STRCPY(badword, p);
12771 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012772 if (keepcap)
12773 STRCAT(badword, "=");
12774 if (flags & WF_BANNED)
12775 STRCAT(badword, "!");
12776 else if (flags & WF_RARE)
12777 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +000012778 if (flags & WF_REGION)
12779 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012780 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +000012781 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012782 p = badword;
12783 }
12784
12785 ml_append(lnum, p, (colnr_T)0, FALSE);
12786}
12787
12788/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012789 * For ":spelldump": Find matching prefixes for "word". Prepend each to
12790 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012791 * Return the updated line number.
12792 */
12793 static linenr_T
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012794dump_prefixes(slang, word, round, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012795 slang_T *slang;
12796 char_u *word; /* case-folded word */
12797 int round;
12798 int flags; /* flags with prefix ID */
12799 linenr_T startlnum;
12800{
12801 idx_T arridx[MAXWLEN];
12802 int curi[MAXWLEN];
12803 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000012804 char_u word_up[MAXWLEN];
12805 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012806 int c;
12807 char_u *byts;
12808 idx_T *idxs;
12809 linenr_T lnum = startlnum;
12810 int depth;
12811 int n;
12812 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012813 int i;
12814
Bram Moolenaar53805d12005-08-01 07:08:33 +000012815 /* if the word starts with a lower-case letter make the word with an
12816 * upper-case letter in word_up[]. */
12817 c = PTR2CHAR(word);
12818 if (SPELL_TOUPPER(c) != c)
12819 {
12820 onecap_copy(word, word_up, TRUE);
12821 has_word_up = TRUE;
12822 }
12823
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012824 byts = slang->sl_pbyts;
12825 idxs = slang->sl_pidxs;
12826 if (byts != NULL) /* array not is empty */
12827 {
12828 /*
12829 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012830 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012831 */
12832 depth = 0;
12833 arridx[0] = 0;
12834 curi[0] = 1;
12835 while (depth >= 0 && !got_int)
12836 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012837 n = arridx[depth];
12838 len = byts[n];
12839 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012840 {
12841 /* Done all bytes at this node, go up one level. */
12842 --depth;
12843 line_breakcheck();
12844 }
12845 else
12846 {
12847 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012848 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012849 ++curi[depth];
12850 c = byts[n];
12851 if (c == 0)
12852 {
12853 /* End of prefix, find out how many IDs there are. */
12854 for (i = 1; i < len; ++i)
12855 if (byts[n + i] != 0)
12856 break;
12857 curi[depth] += i - 1;
12858
Bram Moolenaar53805d12005-08-01 07:08:33 +000012859 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
12860 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012861 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000012862 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000012863 dump_word(prefix, round,
Bram Moolenaar53805d12005-08-01 07:08:33 +000012864 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000012865 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012866 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000012867
12868 /* Check for prefix that matches the word when the
12869 * first letter is upper-case, but only if the prefix has
12870 * a condition. */
12871 if (has_word_up)
12872 {
12873 c = valid_word_prefix(i, n, flags, word_up, slang,
12874 TRUE);
12875 if (c != 0)
12876 {
12877 vim_strncpy(prefix + depth, word_up,
12878 MAXWLEN - depth - 1);
12879 dump_word(prefix, round,
12880 (c & WF_RAREPFX) ? (flags | WF_RARE)
12881 : flags, lnum++);
12882 }
12883 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012884 }
12885 else
12886 {
12887 /* Normal char, go one level deeper. */
12888 prefix[depth++] = c;
12889 arridx[depth] = idxs[n];
12890 curi[depth] = 1;
12891 }
12892 }
12893 }
12894 }
12895
12896 return lnum;
12897}
12898
Bram Moolenaar95529562005-08-25 21:21:38 +000012899/*
12900 * Move "p" to end of word.
12901 */
12902 char_u *
12903spell_to_word_end(start, buf)
12904 char_u *start;
12905 buf_T *buf;
12906{
12907 char_u *p = start;
12908
12909 while (*p != NUL && spell_iswordp(p, buf))
12910 mb_ptr_adv(p);
12911 return p;
12912}
12913
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012914#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012915/*
12916 * Find start of the word in front of the cursor. We don't check if it is
12917 * badly spelled, with completion we can only change the word in front of the
12918 * cursor.
12919 * Used for Insert mode completion CTRL-X ?.
12920 * Returns the column number of the word.
12921 */
12922 int
12923spell_word_start(startcol)
12924 int startcol;
12925{
12926 char_u *line;
12927 char_u *p;
12928 int col = 0;
12929
Bram Moolenaar95529562005-08-25 21:21:38 +000012930 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012931 return startcol;
12932
12933 /* Find a word character before "startcol". */
12934 line = ml_get_curline();
12935 for (p = line + startcol; p > line; )
12936 {
12937 mb_ptr_back(line, p);
12938 if (spell_iswordp_nmw(p))
12939 break;
12940 }
12941
12942 /* Go back to start of the word. */
12943 while (p > line)
12944 {
12945 col = p - line;
12946 mb_ptr_back(line, p);
12947 if (!spell_iswordp(p, curbuf))
12948 break;
12949 col = 0;
12950 }
12951
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012952 return col;
12953}
12954
12955/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000012956 * Need to check for 'spellcapcheck' now, the word is removed before
12957 * expand_spelling() is called. Therefore the ugly global variable.
12958 */
12959static int spell_expand_need_cap;
12960
12961 void
12962spell_expand_check_cap(col)
12963 colnr_T col;
12964{
12965 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
12966}
12967
12968/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012969 * Get list of spelling suggestions.
12970 * Used for Insert mode completion CTRL-X ?.
12971 * Returns the number of matches. The matches are in "matchp[]", array of
12972 * allocated strings.
12973 */
12974/*ARGSUSED*/
12975 int
12976expand_spelling(lnum, col, pat, matchp)
12977 linenr_T lnum;
12978 int col;
12979 char_u *pat;
12980 char_u ***matchp;
12981{
12982 garray_T ga;
12983
12984 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap);
12985 *matchp = ga.ga_data;
12986 return ga.ga_len;
12987}
12988#endif
12989
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000012990#endif /* FEAT_SYN_HL */