blob: 128373a7fe9dcd2dc2f4b18cb411050d12aa3dc2 [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 Moolenaardfb9ac02005-07-05 21:36:03 +0000217 * <flags2> 1 byte Only used when there are postponed prefixes.
218 * Bitmask of:
219 * WF_HAS_AFF >> 8 word includes affix
220 *
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 */
276
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000277#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000278
Bram Moolenaar53805d12005-08-01 07:08:33 +0000279/* flags for <pflags> */
280#define WFP_RARE 0x01 /* rare prefix */
281#define WFP_NC 0x02 /* prefix is not combining */
282#define WFP_UP 0x04 /* to-upper prefix */
283
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000284/* Flags for postponed prefixes. Must be above affixID (one byte)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000285 * and prefcondnr (two bytes). */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000286#define WF_RAREPFX (WFP_RARE << 24) /* in sl_pidxs: flag for rare
287 * postponed prefix */
288#define WF_PFX_NC (WFP_NC << 24) /* in sl_pidxs: flag for non-combining
289 * postponed prefix */
290#define WF_PFX_UP (WFP_UP << 24) /* in sl_pidxs: flag for to-upper
291 * postponed prefix */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000292
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000293/* Special byte values for <byte>. Some are only used in the tree for
294 * postponed prefixes, some only in the other trees. This is a bit messy... */
295#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000296 * postponed prefix: no <pflags> */
297#define BY_INDEX 1 /* child is shared, index follows */
298#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
299 * postponed prefix: <pflags> follows */
300#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
301 * follow; never used in prefix tree */
302#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000303
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000304/* Info from "REP" and "SAL" entries in ".aff" file used in si_rep, sl_rep,
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000305 * and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000306 * One replacement: from "ft_from" to "ft_to". */
307typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000308{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000309 char_u *ft_from;
310 char_u *ft_to;
311} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000312
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000313/* Info from "SAL" entries in ".aff" file used in sl_sal.
314 * The info is split for quick processing by spell_soundfold().
315 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
316typedef struct salitem_S
317{
318 char_u *sm_lead; /* leading letters */
319 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000320 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000321 char_u *sm_rules; /* rules like ^, $, priority */
322 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000323#ifdef FEAT_MBYTE
324 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000325 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000326 int *sm_to_w; /* wide character copy of "sm_to" */
327#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000328} salitem_T;
329
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000330#ifdef FEAT_MBYTE
331typedef int salfirst_T;
332#else
333typedef short salfirst_T;
334#endif
335
Bram Moolenaar5195e452005-08-19 20:32:47 +0000336/* Values for SP_*ERROR are negative, positive values are used by
337 * read_cnt_string(). */
338#define SP_TRUNCERROR -1 /* spell file truncated error */
339#define SP_FORMERROR -2 /* format error in spell file */
340#define SP_ERROR -3 /* other error while reading spell file */
341
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000342/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000343 * Structure used to store words and other info for one language, loaded from
344 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000345 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
346 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
347 *
348 * The "byts" array stores the possible bytes in each tree node, preceded by
349 * the number of possible bytes, sorted on byte value:
350 * <len> <byte1> <byte2> ...
351 * The "idxs" array stores the index of the child node corresponding to the
352 * byte in "byts".
353 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000354 * the flags, region mask and affixID for the word. There may be several
355 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000356 */
357typedef struct slang_S slang_T;
358struct slang_S
359{
360 slang_T *sl_next; /* next language */
361 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000362 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000363 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000364
Bram Moolenaar51485f02005-06-04 21:55:20 +0000365 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000366 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000367 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000368 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000369 char_u *sl_pbyts; /* prefix tree word bytes */
370 idx_T *sl_pidxs; /* prefix tree word indexes */
371
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000372 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000373
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000374 char_u *sl_midword; /* MIDWORD string or NULL */
375
Bram Moolenaar5195e452005-08-19 20:32:47 +0000376 int sl_compmax; /* COMPOUNDMAX (default: MAXWLEN) */
377 int sl_compminlen; /* COMPOUNDMIN (default: MAXWLEN) */
378 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
379 regprog_T *sl_compprog; /* COMPOUNDFLAGS turned into a regexp progrm
380 * (NULL when no compounding) */
381 char_u *sl_compstartflags; /* flags for first compound word */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000382 char_u *sl_compallflags; /* all flags for compound words */
Bram Moolenaar78622822005-08-23 21:00:13 +0000383 char_u sl_nobreak; /* When TRUE: no spaces between words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000384 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
385 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000386
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000387 int sl_prefixcnt; /* number of items in "sl_prefprog" */
388 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
389
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000390 garray_T sl_rep; /* list of fromto_T entries from REP lines */
391 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
392 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000393 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000394 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000395 there is none */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000396 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
397 * "sl_sal_first" maps chars, when has_mbyte
398 * "sl_sal" is a list of wide char lists. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000399 int sl_followup; /* SAL followup */
400 int sl_collapse; /* SAL collapse_result */
401 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaarea424162005-06-16 21:51:00 +0000402 int sl_has_map; /* TRUE if there is a MAP line */
403#ifdef FEAT_MBYTE
404 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
405 int sl_map_array[256]; /* MAP for first 256 chars */
406#else
407 char_u sl_map_array[256]; /* MAP for first 256 chars */
408#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000409};
410
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000411/* First language that is loaded, start of the linked list of loaded
412 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000413static slang_T *first_lang = NULL;
414
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000415/* Flags used in .spl file for soundsalike flags. */
416#define SAL_F0LLOWUP 1
417#define SAL_COLLAPSE 2
418#define SAL_REM_ACCENTS 4
419
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000420/*
421 * Structure used in "b_langp", filled from 'spelllang'.
422 */
423typedef struct langp_S
424{
425 slang_T *lp_slang; /* info for this language (NULL for last one) */
426 int lp_region; /* bitmask for region or REGION_ALL */
427} langp_T;
428
429#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
430
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000431#define REGION_ALL 0xff /* word valid in all regions */
432
Bram Moolenaar5195e452005-08-19 20:32:47 +0000433#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
434#define VIMSPELLMAGICL 8
435#define VIMSPELLVERSION 50
436
437/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
438#define SN_REGION 0 /* <regionname> section */
439#define SN_CHARFLAGS 1 /* charflags section */
440#define SN_MIDWORD 2 /* <midword> section */
441#define SN_PREFCOND 3 /* <prefcond> section */
442#define SN_REP 4 /* REP items section */
443#define SN_SAL 5 /* SAL items section */
444#define SN_SOFO 6 /* soundfolding section */
445#define SN_MAP 7 /* MAP items section */
446#define SN_COMPOUND 8 /* compound words section */
447#define SN_SYLLABLE 9 /* syllable section */
Bram Moolenaar78622822005-08-23 21:00:13 +0000448#define SN_NOBREAK 10 /* NOBREAK section */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000449#define SN_END 255 /* end of sections */
450
451#define SNF_REQUIRED 1 /* <sectionflags>: required section */
452
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000453/* Result values. Lower number is accepted over higher one. */
454#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000455#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000456#define SP_RARE 1
457#define SP_LOCAL 2
458#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000459
Bram Moolenaar7887d882005-07-01 22:33:52 +0000460/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000461static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000462
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000463/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000464 * Information used when looking for suggestions.
465 */
466typedef struct suginfo_S
467{
468 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000469 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000470 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000471 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000472 char_u *su_badptr; /* start of bad word in line */
473 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000474 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000475 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
476 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
477 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000478} suginfo_T;
479
480/* One word suggestion. Used in "si_ga". */
481typedef struct suggest_S
482{
483 char_u *st_word; /* suggested word, allocated string */
484 int st_orglen; /* length of replaced text */
485 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000486 int st_altscore; /* used when st_score compares equal */
487 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000488 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000489} suggest_T;
490
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000491#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000492
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000493/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
494 * called the score may change, thus we need to keep more than what is
495 * displayed. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000496#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 50 ? 50 : (su)->su_maxcount)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000497
498/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
499 * of suggestions that are not going to be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000500#define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000501
502/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000503#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000504#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000505#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000506#define SCORE_RARE 180 /* rare word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000507#define SCORE_SWAP 90 /* swap two characters */
508#define SCORE_SWAP3 110 /* swap two characters in three */
509#define SCORE_REP 87 /* REP replacement */
510#define SCORE_SUBST 93 /* substitute a character */
511#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000512#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000513#define SCORE_DEL 94 /* delete a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000514#define SCORE_DELDUP 64 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000515#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000516#define SCORE_INS 96 /* insert a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000517#define SCORE_INSDUP 66 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000518#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000519#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000520
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000521#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000522#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
523 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000524
525#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000526#define SCORE_MAXMAX 999999 /* accept any score */
527
528/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000529 * Structure to store info for word matching.
530 */
531typedef struct matchinf_S
532{
533 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000534
535 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000536 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000537 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000538 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000539 char_u *mi_cend; /* char after what was used for
540 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000541
542 /* case-folded text */
543 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000544 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000545
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000546 /* for when checking word after a prefix */
547 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000548 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000549 int mi_prefcnt; /* number of entries at mi_prefarridx */
550 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000551#ifdef FEAT_MBYTE
552 int mi_cprefixlen; /* byte length of prefix in original
553 case */
554#else
555# define mi_cprefixlen mi_prefixlen /* it's the same value */
556#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000557
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000558 /* for when checking a compound word */
559 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000560 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
561 int mi_complen; /* nr of compound words used */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000562
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000563 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000564 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000565 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000566 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000567
568 /* for NOBREAK */
569 int mi_result2; /* "mi_resul" without following word */
570 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000571} matchinf_T;
572
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000573/*
574 * The tables used for recognizing word characters according to spelling.
575 * These are only used for the first 256 characters of 'encoding'.
576 */
577typedef struct spelltab_S
578{
579 char_u st_isw[256]; /* flags: is word char */
580 char_u st_isu[256]; /* flags: is uppercase char */
581 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000582 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000583} spelltab_T;
584
585static spelltab_T spelltab;
586static int did_set_spelltab;
587
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000588#define CF_WORD 0x01
589#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000590
591static void clear_spell_chartab __ARGS((spelltab_T *sp));
592static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000593static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
594static int spell_iswordp_nmw __ARGS((char_u *p));
595#ifdef FEAT_MBYTE
596static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
597#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000598static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000599
600/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000601 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000602 */
603typedef enum
604{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000605 STATE_START = 0, /* At start of node check for NUL bytes (goodword
606 * ends); if badword ends there is a match, otherwise
607 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000608 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000609 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000610 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
611 STATE_PLAIN, /* Use each byte of the node. */
612 STATE_DEL, /* Delete a byte from the bad word. */
613 STATE_INS, /* Insert a byte in the bad word. */
614 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000615 STATE_UNSWAP, /* Undo swap two characters. */
616 STATE_SWAP3, /* Swap two characters over three. */
617 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000618 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000619 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000620 STATE_REP_INI, /* Prepare for using REP items. */
621 STATE_REP, /* Use matching REP items from the .aff file. */
622 STATE_REP_UNDO, /* Undo a REP item replacement. */
623 STATE_FINAL /* End of this node. */
624} state_T;
625
626/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000627 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000628 */
629typedef struct trystate_S
630{
Bram Moolenaarea424162005-06-16 21:51:00 +0000631 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000632 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000633 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000634 short ts_curi; /* index in list of child nodes */
635 char_u ts_fidx; /* index in fword[], case-folded bad word */
636 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
637 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000638 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000639 * PFD_PREFIXTREE or PFD_NOPREFIX */
640 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000641#ifdef FEAT_MBYTE
642 char_u ts_tcharlen; /* number of bytes in tword character */
643 char_u ts_tcharidx; /* current byte index in tword character */
644 char_u ts_isdiff; /* DIFF_ values */
645 char_u ts_fcharstart; /* index in fword where badword char started */
646#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000647 char_u ts_prewordlen; /* length of word in "preword[]" */
648 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000649 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000650 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000651 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000652 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000653} trystate_T;
654
Bram Moolenaarea424162005-06-16 21:51:00 +0000655/* values for ts_isdiff */
656#define DIFF_NONE 0 /* no different byte (yet) */
657#define DIFF_YES 1 /* different byte found */
658#define DIFF_INSERT 2 /* inserting character */
659
Bram Moolenaard12a1322005-08-21 22:08:24 +0000660/* values for ts_flags */
661#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
662#define TSF_DIDSPLIT 2 /* tried split at this point */
663
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000664/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000665#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000666#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
667#define PFD_NOTSPECIAL 0xfd /* first value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000668
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000669/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000670#define FIND_FOLDWORD 0 /* find word case-folded */
671#define FIND_KEEPWORD 1 /* find keep-case word */
672#define FIND_PREFIX 2 /* find word after prefix */
673#define FIND_COMPOUND 3 /* find case-folded compound word */
674#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000675
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000676static slang_T *slang_alloc __ARGS((char_u *lang));
677static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000678static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000679static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000680static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000681static 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 +0000682static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000683static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000684static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000685static int no_spell_checking __ARGS((void));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000686static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000687static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000688static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000689static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000690static 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 +0000691static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000692static char_u *read_string __ARGS((FILE *fd, int cnt));
693static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
694static int read_charflags_section __ARGS((FILE *fd));
695static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
696static int read_rep_section __ARGS((FILE *fd, slang_T *slang));
697static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
698static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
699static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
700static int init_syl_tab __ARGS((slang_T *slang));
701static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000702static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
703static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000704#ifdef FEAT_MBYTE
705static int *mb_str2wide __ARGS((char_u *s));
706#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000707static 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 +0000708static void clear_midword __ARGS((buf_T *buf));
709static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000710static int find_region __ARGS((char_u *rp, char_u *region));
711static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000712static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000713static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000714static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000715static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000716static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000717static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +0000718static 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 +0000719#ifdef FEAT_EVAL
720static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
721#endif
722static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
723static void spell_suggest_intern __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000724static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000725static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000726static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000727static void suggest_try_special __ARGS((suginfo_T *su));
728static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000729static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000730#ifdef FEAT_MBYTE
731static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
732#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000733static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000734static void score_comp_sal __ARGS((suginfo_T *su));
735static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000736static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000737static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000738static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000739static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000740static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000741static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000742static void add_banned __ARGS((suginfo_T *su, char_u *word));
743static int was_banned __ARGS((suginfo_T *su, char_u *word));
744static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000745static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000746static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000747static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
748static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
749static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000750#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000751static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000752#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000753static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000754static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000755static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
756static linenr_T apply_prefixes __ARGS((slang_T *slang, char_u *word, int round, int flags, linenr_T startlnum));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000757
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000758/*
759 * Use our own character-case definitions, because the current locale may
760 * differ from what the .spl file uses.
761 * These must not be called with negative number!
762 */
763#ifndef FEAT_MBYTE
764/* Non-multi-byte implementation. */
765# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
766# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
767# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
768#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000769# if defined(HAVE_WCHAR_H)
770# include <wchar.h> /* for towupper() and towlower() */
771# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000772/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
773 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
774 * the "w" library function for characters above 255 if available. */
775# ifdef HAVE_TOWLOWER
776# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
777 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
778# else
779# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
780 : (c) < 256 ? spelltab.st_fold[c] : (c))
781# endif
782
783# ifdef HAVE_TOWUPPER
784# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
785 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
786# else
787# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
788 : (c) < 256 ? spelltab.st_upper[c] : (c))
789# endif
790
791# ifdef HAVE_ISWUPPER
792# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
793 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
794# else
795# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000796 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000797# endif
798#endif
799
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000800
801static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000802static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000803static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000804static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000805
806/*
807 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000808 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000809 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
810 * or when it's OK it remains unchanged.
811 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000812 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000813 * "capcol" is used to check for a Capitalised word after the end of a
814 * sentence. If it's zero then perform the check. Return the column where to
815 * check next, or -1 when no sentence end was found. If it's NULL then don't
816 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000817 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000818 * Returns the length of the word in bytes, also when it's OK, so that the
819 * caller can skip over the word.
820 */
821 int
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000822spell_check(wp, ptr, attrp, capcol)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000823 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000824 char_u *ptr;
825 int *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000826 int *capcol; /* column to check for Capital */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000827{
828 matchinf_T mi; /* Most things are put in "mi" so that it can
829 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000830 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000831 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000832 int wrongcaplen = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000833
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000834 /* A word never starts at a space or a control character. Return quickly
835 * then, skipping over the character. */
836 if (*ptr <= ' ')
837 return 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000838 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000839
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000840 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000841 * 0X99FF. But when a word character follows do check spelling to find
842 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000843 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000844 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000845 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
846 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000847 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000848 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000849 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000850 nrlen = mi.mi_end - ptr;
851 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000852 if (!spell_iswordp(mi.mi_end, wp->w_buffer))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000853 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000854
855 /* Try including the digits in the word. */
856 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000857 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000858 else
859 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000860
Bram Moolenaar0c405862005-06-22 22:26:26 +0000861 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000862 mi.mi_word = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000863 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000864 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000865 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000866 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000867 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000868 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000869
870 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
871 {
872 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000873 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000874 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000875 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000876 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000877 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000878 if (capcol != NULL)
879 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000880
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000881 /* We always use the characters up to the next non-word character,
882 * also for bad words. */
883 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000884
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000885 /* Check caps type later. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000886 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000887
Bram Moolenaar5195e452005-08-19 20:32:47 +0000888 /* case-fold the word with one non-word character, so that we can check
889 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000890 if (*mi.mi_fend != NUL)
891 mb_ptr_adv(mi.mi_fend);
892
893 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
894 MAXWLEN + 1);
895 mi.mi_fwordlen = STRLEN(mi.mi_fword);
896
897 /* The word is bad unless we recognize it. */
898 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +0000899 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000900
901 /*
902 * Loop over the languages specified in 'spelllang'.
903 * We check them all, because a matching word may be longer than an
904 * already found matching word.
905 */
906 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
907 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
908 {
909 /* Check for a matching word in case-folded words. */
910 find_word(&mi, FIND_FOLDWORD);
911
912 /* Check for a matching word in keep-case words. */
913 find_word(&mi, FIND_KEEPWORD);
914
915 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000916 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +0000917
918 /* For a NOBREAK language, may want to use a word without a following
919 * word as a backup. */
920 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
921 && mi.mi_result2 != SP_BAD)
922 {
923 mi.mi_result = mi.mi_result2;
924 mi.mi_end = mi.mi_end2;
925 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000926 }
927
928 if (mi.mi_result != SP_OK)
929 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000930 /* If we found a number skip over it. Allows for "42nd". Do flag
931 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000932 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000933 {
934 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
935 return nrlen;
936 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000937
938 /* When we are at a non-word character there is no error, just
939 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000940 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000941 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000942 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
943 {
944 regmatch_T regmatch;
945
946 /* Check for end of sentence. */
947 regmatch.regprog = wp->w_buffer->b_cap_prog;
948 regmatch.rm_ic = FALSE;
949 if (vim_regexec(&regmatch, ptr, 0))
950 *capcol = (int)(regmatch.endp[0] - ptr);
951 }
952
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000953#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000954 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000955 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000956#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000957 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000958 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000959 else if (mi.mi_end == ptr)
960 /* Always include at least one character. Required for when there
961 * is a mixup in "midword". */
962 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +0000963 else if (mi.mi_result == SP_BAD
964 && LANGP_ENTRY(wp->w_buffer->b_langp, 0)->lp_slang->sl_nobreak)
965 {
966 char_u *p, *fp;
967 int save_result = mi.mi_result;
968
969 /* First language in 'spelllang' is NOBREAK. Find first position
970 * at which any word would be valid. */
971 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
972 p = mi.mi_word;
973 fp = mi.mi_fword;
974 for (;;)
975 {
976 mb_ptr_adv(p);
977 mb_ptr_adv(fp);
978 if (p >= mi.mi_end)
979 break;
980 mi.mi_compoff = fp - mi.mi_fword;
981 find_word(&mi, FIND_COMPOUND);
982 if (mi.mi_result != SP_BAD)
983 {
984 mi.mi_end = p;
985 break;
986 }
987 }
988 mi.mi_result = save_result;
989 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000990
991 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
992 *attrp = highlight_attr[HLF_SPB];
993 else if (mi.mi_result == SP_RARE)
994 *attrp = highlight_attr[HLF_SPR];
995 else
996 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000997 }
998
Bram Moolenaar5195e452005-08-19 20:32:47 +0000999 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1000 {
1001 /* Report SpellCap only when the word isn't badly spelled. */
1002 *attrp = highlight_attr[HLF_SPC];
1003 return wrongcaplen;
1004 }
1005
Bram Moolenaar51485f02005-06-04 21:55:20 +00001006 return (int)(mi.mi_end - ptr);
1007}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001008
Bram Moolenaar51485f02005-06-04 21:55:20 +00001009/*
1010 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001011 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1012 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1013 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1014 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001015 *
1016 * For a match mip->mi_result is updated.
1017 */
1018 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001019find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001020 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001021 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001022{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001023 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001024 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001025 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001026 int endidxcnt = 0;
1027 int len;
1028 int wlen = 0;
1029 int flen;
1030 int c;
1031 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001032 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001033#ifdef FEAT_MBYTE
1034 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001035#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001036 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001037 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001038 slang_T *slang = mip->mi_lp->lp_slang;
1039 unsigned flags;
1040 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001041 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001042 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001043 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001044 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001045
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001046 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001047 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001048 /* Check for word with matching case in keep-case tree. */
1049 ptr = mip->mi_word;
1050 flen = 9999; /* no case folding, always enough bytes */
1051 byts = slang->sl_kbyts;
1052 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001053
1054 if (mode == FIND_KEEPCOMPOUND)
1055 /* Skip over the previously found word(s). */
1056 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001057 }
1058 else
1059 {
1060 /* Check for case-folded in case-folded tree. */
1061 ptr = mip->mi_fword;
1062 flen = mip->mi_fwordlen; /* available case-folded bytes */
1063 byts = slang->sl_fbyts;
1064 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001065
1066 if (mode == FIND_PREFIX)
1067 {
1068 /* Skip over the prefix. */
1069 wlen = mip->mi_prefixlen;
1070 flen -= mip->mi_prefixlen;
1071 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001072 else if (mode == FIND_COMPOUND)
1073 {
1074 /* Skip over the previously found word(s). */
1075 wlen = mip->mi_compoff;
1076 flen -= mip->mi_compoff;
1077 }
1078
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001079 }
1080
Bram Moolenaar51485f02005-06-04 21:55:20 +00001081 if (byts == NULL)
1082 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001083
Bram Moolenaar51485f02005-06-04 21:55:20 +00001084 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001085 * Repeat advancing in the tree until:
1086 * - there is a byte that doesn't match,
1087 * - we reach the end of the tree,
1088 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001089 */
1090 for (;;)
1091 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001092 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001093 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001094
1095 len = byts[arridx++];
1096
1097 /* If the first possible byte is a zero the word could end here.
1098 * Remember this index, we first check for the longest word. */
1099 if (byts[arridx] == 0)
1100 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001101 if (endidxcnt == MAXWLEN)
1102 {
1103 /* Must be a corrupted spell file. */
1104 EMSG(_(e_format));
1105 return;
1106 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001107 endlen[endidxcnt] = wlen;
1108 endidx[endidxcnt++] = arridx++;
1109 --len;
1110
1111 /* Skip over the zeros, there can be several flag/region
1112 * combinations. */
1113 while (len > 0 && byts[arridx] == 0)
1114 {
1115 ++arridx;
1116 --len;
1117 }
1118 if (len == 0)
1119 break; /* no children, word must end here */
1120 }
1121
1122 /* Stop looking at end of the line. */
1123 if (ptr[wlen] == NUL)
1124 break;
1125
1126 /* Perform a binary search in the list of accepted bytes. */
1127 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001128 if (c == TAB) /* <Tab> is handled like <Space> */
1129 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001130 lo = arridx;
1131 hi = arridx + len - 1;
1132 while (lo < hi)
1133 {
1134 m = (lo + hi) / 2;
1135 if (byts[m] > c)
1136 hi = m - 1;
1137 else if (byts[m] < c)
1138 lo = m + 1;
1139 else
1140 {
1141 lo = hi = m;
1142 break;
1143 }
1144 }
1145
1146 /* Stop if there is no matching byte. */
1147 if (hi < lo || byts[lo] != c)
1148 break;
1149
1150 /* Continue at the child (if there is one). */
1151 arridx = idxs[lo];
1152 ++wlen;
1153 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001154
1155 /* One space in the good word may stand for several spaces in the
1156 * checked word. */
1157 if (c == ' ')
1158 {
1159 for (;;)
1160 {
1161 if (flen <= 0 && *mip->mi_fend != NUL)
1162 flen = fold_more(mip);
1163 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1164 break;
1165 ++wlen;
1166 --flen;
1167 }
1168 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001169 }
1170
1171 /*
1172 * Verify that one of the possible endings is valid. Try the longest
1173 * first.
1174 */
1175 while (endidxcnt > 0)
1176 {
1177 --endidxcnt;
1178 arridx = endidx[endidxcnt];
1179 wlen = endlen[endidxcnt];
1180
1181#ifdef FEAT_MBYTE
1182 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1183 continue; /* not at first byte of character */
1184#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001185 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001186 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001187 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001188 continue; /* next char is a word character */
1189 word_ends = FALSE;
1190 }
1191 else
1192 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001193 /* The prefix flag is before compound flags. Once a valid prefix flag
1194 * has been found we try compound flags. */
1195 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001196
1197#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001198 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001199 {
1200 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001201 * when folding case. This can be slow, take a shortcut when the
1202 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001203 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001204 if (STRNCMP(ptr, p, wlen) != 0)
1205 {
1206 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1207 mb_ptr_adv(p);
1208 wlen = p - mip->mi_word;
1209 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001210 }
1211#endif
1212
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001213 /* Check flags and region. For FIND_PREFIX check the condition and
1214 * prefix ID.
1215 * Repeat this if there are more flags/region alternatives until there
1216 * is a match. */
1217 res = SP_BAD;
1218 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1219 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001220 {
1221 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001222
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001223 /* For the fold-case tree check that the case of the checked word
1224 * matches with what the word in the tree requires.
1225 * For keep-case tree the case is always right. For prefixes we
1226 * don't bother to check. */
1227 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001228 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001229 if (mip->mi_cend != mip->mi_word + wlen)
1230 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001231 /* mi_capflags was set for a different word length, need
1232 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001233 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001234 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001235 }
1236
Bram Moolenaar0c405862005-06-22 22:26:26 +00001237 if (mip->mi_capflags == WF_KEEPCAP
1238 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001239 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001240 }
1241
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001242 /* When mode is FIND_PREFIX the word must support the prefix:
1243 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001244 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001245 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001246 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001247 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001248 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001249 mip->mi_word + mip->mi_cprefixlen, slang,
1250 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001251 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001252 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001253
1254 /* Use the WF_RARE flag for a rare prefix. */
1255 if (c & WF_RAREPFX)
1256 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001257 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001258 }
1259
Bram Moolenaar78622822005-08-23 21:00:13 +00001260 if (slang->sl_nobreak)
1261 {
1262 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1263 && (flags & WF_BANNED) == 0)
1264 {
1265 /* NOBREAK: found a valid following word. That's all we
1266 * need to know, so return. */
1267 mip->mi_result = SP_OK;
1268 break;
1269 }
1270 }
1271
1272 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1273 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001274 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00001275 /* If there is no flag or the word is shorter than
1276 * COMPOUNDMIN reject it quickly.
1277 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001278 * that's too short... Myspell compatibility requires this
1279 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001280 if (((unsigned)flags >> 24) == 0
1281 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001282 continue;
1283
Bram Moolenaare52325c2005-08-22 22:54:29 +00001284 /* Limit the number of compound words to COMPOUNDMAX if no
1285 * maximum for syllables is specified. */
1286 if (!word_ends && mip->mi_complen + 2 > slang->sl_compmax
1287 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001288 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001289
Bram Moolenaard12a1322005-08-21 22:08:24 +00001290 /* Quickly check if compounding is possible with this flag. */
1291 if (vim_strchr(mip->mi_complen == 0
1292 ? slang->sl_compstartflags
1293 : slang->sl_compallflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00001294 ((unsigned)flags >> 24)) == NULL)
1295 continue;
1296
Bram Moolenaare52325c2005-08-22 22:54:29 +00001297 if (mode == FIND_COMPOUND)
1298 {
1299 int capflags;
1300
1301 /* Need to check the caps type of the appended compound
1302 * word. */
1303#ifdef FEAT_MBYTE
1304 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1305 mip->mi_compoff) != 0)
1306 {
1307 /* case folding may have changed the length */
1308 p = mip->mi_word;
1309 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1310 mb_ptr_adv(p);
1311 }
1312 else
1313#endif
1314 p = mip->mi_word + mip->mi_compoff;
1315 capflags = captype(p, mip->mi_word + wlen);
1316 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1317 && (flags & WF_FIXCAP) != 0))
1318 continue;
1319
1320 if (capflags != WF_ALLCAP)
1321 {
1322 /* When the character before the word is a word
1323 * character we do not accept a Onecap word. We do
1324 * accept a no-caps word, even when the dictionary
1325 * word specifies ONECAP. */
1326 mb_ptr_back(mip->mi_word, p);
1327 if (spell_iswordp_nmw(p)
1328 ? capflags == WF_ONECAP
1329 : (flags & WF_ONECAP) != 0
1330 && capflags != WF_ONECAP)
1331 continue;
1332 }
1333 }
1334
Bram Moolenaar5195e452005-08-19 20:32:47 +00001335 /* If the word ends the sequence of compound flags of the
1336 * words must match with one of the COMPOUNDFLAGS items and
1337 * the number of syllables must not be too large. */
1338 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1339 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1340 if (word_ends)
1341 {
1342 char_u fword[MAXWLEN];
1343
1344 if (slang->sl_compsylmax < MAXWLEN)
1345 {
1346 /* "fword" is only needed for checking syllables. */
1347 if (ptr == mip->mi_word)
1348 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1349 else
1350 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1351 }
1352 if (!can_compound(slang, fword, mip->mi_compflags))
1353 continue;
1354 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001355 }
1356
Bram Moolenaar78622822005-08-23 21:00:13 +00001357 nobreak_result = SP_OK;
1358
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001359 if (!word_ends)
1360 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001361 int save_result = mip->mi_result;
1362 char_u *save_end = mip->mi_end;
1363
1364 /* Check that a valid word follows. If there is one and we
1365 * are compounding, it will set "mi_result", thus we are
1366 * always finished here. For NOBREAK we only check that a
1367 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001368 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001369 if (slang->sl_nobreak)
1370 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001371
1372 /* Find following word in case-folded tree. */
1373 mip->mi_compoff = endlen[endidxcnt];
1374#ifdef FEAT_MBYTE
1375 if (has_mbyte && mode == FIND_KEEPWORD)
1376 {
1377 /* Compute byte length in case-folded word from "wlen":
1378 * byte length in keep-case word. Length may change when
1379 * folding case. This can be slow, take a shortcut when
1380 * the case-folded word is equal to the keep-case word. */
1381 p = mip->mi_fword;
1382 if (STRNCMP(ptr, p, wlen) != 0)
1383 {
1384 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1385 mb_ptr_adv(p);
1386 mip->mi_compoff = p - mip->mi_fword;
1387 }
1388 }
1389#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001390 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001391 ++mip->mi_complen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001392 find_word(mip, FIND_COMPOUND);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001393
Bram Moolenaar78622822005-08-23 21:00:13 +00001394 /* When NOBREAK any word that matches is OK. Otherwise we
1395 * need to find the longest match, thus try with keep-case and
1396 * prefix too. */
1397 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1398 {
1399 /* Find following word in keep-case tree. */
1400 mip->mi_compoff = wlen;
1401 find_word(mip, FIND_KEEPCOMPOUND);
Bram Moolenaard12a1322005-08-21 22:08:24 +00001402
Bram Moolenaar78622822005-08-23 21:00:13 +00001403 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1404 {
1405 /* Check for following word with prefix. */
1406 mip->mi_compoff = c;
1407 find_prefix(mip, FIND_COMPOUND);
1408 }
1409 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001410 --mip->mi_complen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001411
Bram Moolenaar78622822005-08-23 21:00:13 +00001412 if (slang->sl_nobreak)
1413 {
1414 nobreak_result = mip->mi_result;
1415 mip->mi_result = save_result;
1416 mip->mi_end = save_end;
1417 }
1418 else
1419 {
1420 if (mip->mi_result == SP_OK)
1421 break;
1422 continue;
1423 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001424 }
1425
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001426 if (flags & WF_BANNED)
1427 res = SP_BANNED;
1428 else if (flags & WF_REGION)
1429 {
1430 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001431 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001432 res = SP_OK;
1433 else
1434 res = SP_LOCAL;
1435 }
1436 else if (flags & WF_RARE)
1437 res = SP_RARE;
1438 else
1439 res = SP_OK;
1440
Bram Moolenaar78622822005-08-23 21:00:13 +00001441 /* Always use the longest match and the best result. For NOBREAK
1442 * we separately keep the longest match without a following good
1443 * word as a fall-back. */
1444 if (nobreak_result == SP_BAD)
1445 {
1446 if (mip->mi_result2 > res)
1447 {
1448 mip->mi_result2 = res;
1449 mip->mi_end2 = mip->mi_word + wlen;
1450 }
1451 else if (mip->mi_result2 == res
1452 && mip->mi_end2 < mip->mi_word + wlen)
1453 mip->mi_end2 = mip->mi_word + wlen;
1454 }
1455 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001456 {
1457 mip->mi_result = res;
1458 mip->mi_end = mip->mi_word + wlen;
1459 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001460 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001461 mip->mi_end = mip->mi_word + wlen;
1462
Bram Moolenaar78622822005-08-23 21:00:13 +00001463 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001464 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001465 }
1466
Bram Moolenaar78622822005-08-23 21:00:13 +00001467 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001468 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001469 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001470}
1471
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001472/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00001473 * Return TRUE if "flags" is a valid sequence of compound flags and
1474 * "word[len]" does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001475 */
1476 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001477can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001478 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001479 char_u *word;
1480 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001481{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001482 regmatch_T regmatch;
1483
1484 if (slang->sl_compprog == NULL)
1485 return FALSE;
1486 regmatch.regprog = slang->sl_compprog;
1487 regmatch.rm_ic = FALSE;
1488 if (!vim_regexec(&regmatch, flags, 0))
1489 return FALSE;
1490
Bram Moolenaare52325c2005-08-22 22:54:29 +00001491 /* Count the number of syllables. This may be slow, do it last. If there
1492 * are too many syllables AND the number of compound words is above
1493 * COMPOUNDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001494 if (slang->sl_compsylmax < MAXWLEN
1495 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaare52325c2005-08-22 22:54:29 +00001496 return STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001497 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001498}
1499
1500/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001501 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1502 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001503 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001504 */
1505 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001506valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001507 int totprefcnt; /* nr of prefix IDs */
1508 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001509 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001510 char_u *word;
1511 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001512 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001513{
1514 int prefcnt;
1515 int pidx;
1516 regprog_T *rp;
1517 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001518 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001519
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001520 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001521 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1522 {
1523 pidx = slang->sl_pidxs[arridx + prefcnt];
1524
1525 /* Check the prefix ID. */
1526 if (prefid != (pidx & 0xff))
1527 continue;
1528
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001529 /* Check if the prefix doesn't combine and the word already has a
1530 * suffix. */
1531 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1532 continue;
1533
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001534 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001535 * stored in the two bytes above the prefix ID byte. */
1536 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001537 if (rp != NULL)
1538 {
1539 regmatch.regprog = rp;
1540 regmatch.rm_ic = FALSE;
1541 if (!vim_regexec(&regmatch, word, 0))
1542 continue;
1543 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001544 else if (cond_req)
1545 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001546
Bram Moolenaar53805d12005-08-01 07:08:33 +00001547 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001548 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001549 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001550 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001551}
1552
1553/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001554 * Check if the word at "mip->mi_word" has a matching prefix.
1555 * If it does, then check the following word.
1556 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001557 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1558 * prefix in a compound word.
1559 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001560 * For a match mip->mi_result is updated.
1561 */
1562 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001563find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001564 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001565 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001566{
1567 idx_T arridx = 0;
1568 int len;
1569 int wlen = 0;
1570 int flen;
1571 int c;
1572 char_u *ptr;
1573 idx_T lo, hi, m;
1574 slang_T *slang = mip->mi_lp->lp_slang;
1575 char_u *byts;
1576 idx_T *idxs;
1577
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001578 byts = slang->sl_pbyts;
1579 if (byts == NULL)
1580 return; /* array is empty */
1581
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001582 /* We use the case-folded word here, since prefixes are always
1583 * case-folded. */
1584 ptr = mip->mi_fword;
1585 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001586 if (mode == FIND_COMPOUND)
1587 {
1588 /* Skip over the previously found word(s). */
1589 ptr += mip->mi_compoff;
1590 flen -= mip->mi_compoff;
1591 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001592 idxs = slang->sl_pidxs;
1593
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001594 /*
1595 * Repeat advancing in the tree until:
1596 * - there is a byte that doesn't match,
1597 * - we reach the end of the tree,
1598 * - or we reach the end of the line.
1599 */
1600 for (;;)
1601 {
1602 if (flen == 0 && *mip->mi_fend != NUL)
1603 flen = fold_more(mip);
1604
1605 len = byts[arridx++];
1606
1607 /* If the first possible byte is a zero the prefix could end here.
1608 * Check if the following word matches and supports the prefix. */
1609 if (byts[arridx] == 0)
1610 {
1611 /* There can be several prefixes with different conditions. We
1612 * try them all, since we don't know which one will give the
1613 * longest match. The word is the same each time, pass the list
1614 * of possible prefixes to find_word(). */
1615 mip->mi_prefarridx = arridx;
1616 mip->mi_prefcnt = len;
1617 while (len > 0 && byts[arridx] == 0)
1618 {
1619 ++arridx;
1620 --len;
1621 }
1622 mip->mi_prefcnt -= len;
1623
1624 /* Find the word that comes after the prefix. */
1625 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001626 if (mode == FIND_COMPOUND)
1627 /* Skip over the previously found word(s). */
1628 mip->mi_prefixlen += mip->mi_compoff;
1629
Bram Moolenaar53805d12005-08-01 07:08:33 +00001630#ifdef FEAT_MBYTE
1631 if (has_mbyte)
1632 {
1633 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001634 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1635 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001636 }
1637 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001638 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001639#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001640 find_word(mip, FIND_PREFIX);
1641
1642
1643 if (len == 0)
1644 break; /* no children, word must end here */
1645 }
1646
1647 /* Stop looking at end of the line. */
1648 if (ptr[wlen] == NUL)
1649 break;
1650
1651 /* Perform a binary search in the list of accepted bytes. */
1652 c = ptr[wlen];
1653 lo = arridx;
1654 hi = arridx + len - 1;
1655 while (lo < hi)
1656 {
1657 m = (lo + hi) / 2;
1658 if (byts[m] > c)
1659 hi = m - 1;
1660 else if (byts[m] < c)
1661 lo = m + 1;
1662 else
1663 {
1664 lo = hi = m;
1665 break;
1666 }
1667 }
1668
1669 /* Stop if there is no matching byte. */
1670 if (hi < lo || byts[lo] != c)
1671 break;
1672
1673 /* Continue at the child (if there is one). */
1674 arridx = idxs[lo];
1675 ++wlen;
1676 --flen;
1677 }
1678}
1679
1680/*
1681 * Need to fold at least one more character. Do until next non-word character
1682 * for efficiency.
1683 * Return the length of the folded chars in bytes.
1684 */
1685 static int
1686fold_more(mip)
1687 matchinf_T *mip;
1688{
1689 int flen;
1690 char_u *p;
1691
1692 p = mip->mi_fend;
1693 do
1694 {
1695 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001696 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001697
1698 /* Include the non-word character so that we can check for the
1699 * word end. */
1700 if (*mip->mi_fend != NUL)
1701 mb_ptr_adv(mip->mi_fend);
1702
1703 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1704 mip->mi_fword + mip->mi_fwordlen,
1705 MAXWLEN - mip->mi_fwordlen);
1706 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1707 mip->mi_fwordlen += flen;
1708 return flen;
1709}
1710
1711/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001712 * Check case flags for a word. Return TRUE if the word has the requested
1713 * case.
1714 */
1715 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001716spell_valid_case(wordflags, treeflags)
1717 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001718 int treeflags; /* flags for the word in the spell tree */
1719{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001720 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001721 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001722 && ((treeflags & WF_ONECAP) == 0
1723 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001724}
1725
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001726/*
1727 * Return TRUE if spell checking is not enabled.
1728 */
1729 static int
1730no_spell_checking()
1731{
1732 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
1733 {
1734 EMSG(_("E756: Spell checking is not enabled"));
1735 return TRUE;
1736 }
1737 return FALSE;
1738}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001739
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001740/*
1741 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001742 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001743 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1744 * to after badly spelled word before the cursor.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001745 * Return OK if found, FAIL otherwise.
1746 */
1747 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001748spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001749 int dir; /* FORWARD or BACKWARD */
1750 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001751 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001752{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001753 linenr_T lnum;
1754 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001755 char_u *line;
1756 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001757 char_u *endp;
1758 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001759 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001760 int has_syntax = syntax_present(curbuf);
1761 int col;
1762 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001763 char_u *buf = NULL;
1764 int buflen = 0;
1765 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001766 int capcol = -1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001767
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001768 if (no_spell_checking())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001769 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001770
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001771 /*
1772 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001773 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001774 *
1775 * When searching backwards, we continue in the line to find the last
1776 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001777 *
1778 * We concatenate the start of the next line, so that wrapped words work
1779 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1780 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001781 */
1782 lnum = curwin->w_cursor.lnum;
1783 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001784
1785 while (!got_int)
1786 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001787 line = ml_get(lnum);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001788
Bram Moolenaar0c405862005-06-22 22:26:26 +00001789 len = STRLEN(line);
1790 if (buflen < len + MAXWLEN + 2)
1791 {
1792 vim_free(buf);
1793 buflen = len + MAXWLEN + 2;
1794 buf = alloc(buflen);
1795 if (buf == NULL)
1796 break;
1797 }
1798
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001799 /* In first line check first word for Capital. */
1800 if (lnum == 1)
1801 capcol = 0;
1802
1803 /* For checking first word with a capital skip white space. */
1804 if (capcol == 0)
1805 capcol = skipwhite(line) - line;
1806
Bram Moolenaar0c405862005-06-22 22:26:26 +00001807 /* Copy the line into "buf" and append the start of the next line if
1808 * possible. */
1809 STRCPY(buf, line);
1810 if (lnum < curbuf->b_ml.ml_line_count)
1811 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1812
1813 p = buf + skip;
1814 endp = buf + len;
1815 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001816 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001817 /* When searching backward don't search after the cursor. */
1818 if (dir == BACKWARD
1819 && lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001820 && (colnr_T)(p - buf) >= curwin->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001821 break;
1822
1823 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001824 attr = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001825 len = spell_check(curwin, p, &attr, &capcol);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001826
1827 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001828 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001829 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001830 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001831 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001832 /* When searching forward only accept a bad word after
1833 * the cursor. */
1834 if (dir == BACKWARD
1835 || lnum > curwin->w_cursor.lnum
1836 || (lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001837 && (colnr_T)(curline ? p - buf + len
1838 : p - buf)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001839 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001840 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001841 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001842 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001843 col = p - buf;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001844 (void)syn_get_id(lnum, (colnr_T)col,
1845 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001846 }
1847 else
1848 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001849
Bram Moolenaar51485f02005-06-04 21:55:20 +00001850 if (can_spell)
1851 {
1852 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001853 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001854#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001855 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001856#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001857 if (dir == FORWARD)
1858 {
1859 /* No need to search further. */
1860 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001861 vim_free(buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001862 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001863 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001864 else if (curline)
1865 /* Insert mode completion: put cursor after
1866 * the bad word. */
1867 found_pos.col += len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001868 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001869 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001870 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001871 }
1872
Bram Moolenaar51485f02005-06-04 21:55:20 +00001873 /* advance to character after the word */
1874 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001875 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001876 }
1877
Bram Moolenaar5195e452005-08-19 20:32:47 +00001878 if (dir == BACKWARD && found_pos.lnum != 0)
1879 {
1880 /* Use the last match in the line. */
1881 curwin->w_cursor = found_pos;
1882 vim_free(buf);
1883 return OK;
1884 }
1885
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001886 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001887 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001888
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001889 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001890 if (dir == BACKWARD)
1891 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001892 if (lnum == 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001893 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001894 --lnum;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001895 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001896 }
1897 else
1898 {
1899 if (lnum == curbuf->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001900 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001901 ++lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001902
1903 /* Skip the characters at the start of the next line that were
1904 * included in a match crossing line boundaries. */
1905 if (attr == 0)
1906 skip = p - endp;
1907 else
1908 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001909
1910 /* Capscol skips over the inserted space. */
1911 --capcol;
1912
1913 /* But after empty line check first word in next line */
1914 if (*skipwhite(line) == NUL)
1915 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001916 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001917
1918 line_breakcheck();
1919 }
1920
Bram Moolenaar0c405862005-06-22 22:26:26 +00001921 vim_free(buf);
1922 return FAIL;
1923}
1924
1925/*
1926 * For spell checking: concatenate the start of the following line "line" into
1927 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
1928 */
1929 void
1930spell_cat_line(buf, line, maxlen)
1931 char_u *buf;
1932 char_u *line;
1933 int maxlen;
1934{
1935 char_u *p;
1936 int n;
1937
1938 p = skipwhite(line);
1939 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1940 p = skipwhite(p + 1);
1941
1942 if (*p != NUL)
1943 {
1944 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001945 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001946 n = p - line;
1947 if (n >= maxlen)
1948 n = maxlen - 1;
1949 vim_memset(buf + 1, ' ', n);
1950 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001951}
1952
1953/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001954 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001955 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001956 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001957 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001958spell_load_lang(lang)
1959 char_u *lang;
1960{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001961 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001962 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001963 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001964
Bram Moolenaarb765d632005-06-07 21:00:02 +00001965 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001966 * It's truncated when an error is detected. */
1967 STRCPY(langcp, lang);
1968
Bram Moolenaarb765d632005-06-07 21:00:02 +00001969 /*
1970 * Find the first spell file for "lang" in 'runtimepath' and load it.
1971 */
1972 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1973 "spell/%s.%s.spl", lang, spell_enc());
1974 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001975
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001976 if (r == FAIL && *langcp != NUL)
1977 {
1978 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001979 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001980 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001981 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001982 }
1983
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001984 if (r == FAIL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00001985 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
1986 lang, spell_enc(), lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001987 else if (*langcp != NUL)
1988 {
1989 /* Load all the additions. */
1990 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
1991 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
1992 }
1993}
1994
1995/*
1996 * Return the encoding used for spell checking: Use 'encoding', except that we
1997 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1998 */
1999 static char_u *
2000spell_enc()
2001{
2002
2003#ifdef FEAT_MBYTE
2004 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2005 return p_enc;
2006#endif
2007 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002008}
2009
2010/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002011 * Get the name of the .spl file for the internal wordlist into
2012 * "fname[MAXPATHL]".
2013 */
2014 static void
2015int_wordlist_spl(fname)
2016 char_u *fname;
2017{
2018 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2019 int_wordlist, spell_enc());
2020}
2021
2022/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002023 * Allocate a new slang_T.
2024 * Caller must fill "sl_next".
2025 */
2026 static slang_T *
2027slang_alloc(lang)
2028 char_u *lang;
2029{
2030 slang_T *lp;
2031
Bram Moolenaar51485f02005-06-04 21:55:20 +00002032 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002033 if (lp != NULL)
2034 {
2035 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002036 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002037 lp->sl_compmax = MAXWLEN;
2038 lp->sl_compminlen = MAXWLEN;
2039 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002040 }
2041 return lp;
2042}
2043
2044/*
2045 * Free the contents of an slang_T and the structure itself.
2046 */
2047 static void
2048slang_free(lp)
2049 slang_T *lp;
2050{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002051 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002052 vim_free(lp->sl_fname);
2053 slang_clear(lp);
2054 vim_free(lp);
2055}
2056
2057/*
2058 * Clear an slang_T so that the file can be reloaded.
2059 */
2060 static void
2061slang_clear(lp)
2062 slang_T *lp;
2063{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002064 garray_T *gap;
2065 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002066 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002067 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002068
Bram Moolenaar51485f02005-06-04 21:55:20 +00002069 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002070 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002071 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002072 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002073 vim_free(lp->sl_pbyts);
2074 lp->sl_pbyts = NULL;
2075
Bram Moolenaar51485f02005-06-04 21:55:20 +00002076 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002077 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002078 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002079 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002080 vim_free(lp->sl_pidxs);
2081 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002082
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002083 gap = &lp->sl_rep;
2084 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002085 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002086 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2087 vim_free(ftp->ft_from);
2088 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002089 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002090 ga_clear(gap);
2091
2092 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002093 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002094 {
2095 /* "ga_len" is set to 1 without adding an item for latin1 */
2096 if (gap->ga_data != NULL)
2097 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2098 for (i = 0; i < gap->ga_len; ++i)
2099 vim_free(((int **)gap->ga_data)[i]);
2100 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002101 else
2102 /* SAL items: free salitem_T items */
2103 while (gap->ga_len > 0)
2104 {
2105 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2106 vim_free(smp->sm_lead);
2107 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2108 vim_free(smp->sm_to);
2109#ifdef FEAT_MBYTE
2110 vim_free(smp->sm_lead_w);
2111 vim_free(smp->sm_oneof_w);
2112 vim_free(smp->sm_to_w);
2113#endif
2114 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002115 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002116
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002117 for (i = 0; i < lp->sl_prefixcnt; ++i)
2118 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002119 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002120 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002121 lp->sl_prefprog = NULL;
2122
2123 vim_free(lp->sl_midword);
2124 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002125
Bram Moolenaar5195e452005-08-19 20:32:47 +00002126 vim_free(lp->sl_compprog);
2127 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002128 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002129 lp->sl_compprog = NULL;
2130 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002131 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002132
2133 vim_free(lp->sl_syllable);
2134 lp->sl_syllable = NULL;
2135 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002136
Bram Moolenaarea424162005-06-16 21:51:00 +00002137#ifdef FEAT_MBYTE
2138 {
2139 int todo = lp->sl_map_hash.ht_used;
2140 hashitem_T *hi;
2141
2142 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
2143 if (!HASHITEM_EMPTY(hi))
2144 {
2145 --todo;
2146 vim_free(hi->hi_key);
2147 }
2148 }
2149 hash_clear(&lp->sl_map_hash);
2150#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002151
2152 lp->sl_compmax = MAXWLEN;
2153 lp->sl_compminlen = MAXWLEN;
2154 lp->sl_compsylmax = MAXWLEN;
2155 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002156}
2157
2158/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002159 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002160 * Invoked through do_in_runtimepath().
2161 */
2162 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002163spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002164 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002165 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002166{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002167 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002168}
2169
2170/*
2171 * Load one spell file and store the info into a slang_T.
2172 *
2173 * This is invoked in two ways:
2174 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2175 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2176 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2177 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002178 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002179 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002180 static slang_T *
2181spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002182 char_u *fname;
2183 char_u *lang;
2184 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002185 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002186{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002187 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002188 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002189 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002190 char_u *bp;
2191 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002192 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002193 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002194 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002195 int round;
2196 char_u *save_sourcing_name = sourcing_name;
2197 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002198 slang_T *lp = NULL;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002199 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002200 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002201 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002202
Bram Moolenaarb765d632005-06-07 21:00:02 +00002203 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002204 if (fd == NULL)
2205 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002206 if (!silent)
2207 EMSG2(_(e_notopen), fname);
2208 else if (p_verbose > 2)
2209 {
2210 verbose_enter();
2211 smsg((char_u *)e_notopen, fname);
2212 verbose_leave();
2213 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002214 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002215 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002216 if (p_verbose > 2)
2217 {
2218 verbose_enter();
2219 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2220 verbose_leave();
2221 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002222
Bram Moolenaarb765d632005-06-07 21:00:02 +00002223 if (old_lp == NULL)
2224 {
2225 lp = slang_alloc(lang);
2226 if (lp == NULL)
2227 goto endFAIL;
2228
2229 /* Remember the file name, used to reload the file when it's updated. */
2230 lp->sl_fname = vim_strsave(fname);
2231 if (lp->sl_fname == NULL)
2232 goto endFAIL;
2233
2234 /* Check for .add.spl. */
2235 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2236 }
2237 else
2238 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002239
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002240 /* Set sourcing_name, so that error messages mention the file name. */
2241 sourcing_name = fname;
2242 sourcing_lnum = 0;
2243
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002244 /* <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002245 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002246 for (i = 0; i < VIMSPELLMAGICL; ++i)
2247 buf[i] = getc(fd); /* <fileID> */
2248 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2249 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002250 EMSG(_("E757: This does not look like a spell file"));
2251 goto endFAIL;
2252 }
2253 c = getc(fd); /* <versionnr> */
2254 if (c < VIMSPELLVERSION)
2255 {
2256 EMSG(_("E771: Old spell file, needs to be updated"));
2257 goto endFAIL;
2258 }
2259 else if (c > VIMSPELLVERSION)
2260 {
2261 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002262 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002263 }
2264
Bram Moolenaar5195e452005-08-19 20:32:47 +00002265
2266 /*
2267 * <SECTIONS>: <section> ... <sectionend>
2268 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2269 */
2270 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002271 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002272 n = getc(fd); /* <sectionID> or <sectionend> */
2273 if (n == SN_END)
2274 break;
2275 c = getc(fd); /* <sectionflags> */
2276 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2277 /* <sectionlen> */
2278 if (len < 0)
2279 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002280
Bram Moolenaar5195e452005-08-19 20:32:47 +00002281 res = 0;
2282 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002283 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002284 case SN_REGION:
2285 res = read_region_section(fd, lp, len);
2286 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002287
Bram Moolenaar5195e452005-08-19 20:32:47 +00002288 case SN_CHARFLAGS:
2289 res = read_charflags_section(fd);
2290 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002291
Bram Moolenaar5195e452005-08-19 20:32:47 +00002292 case SN_MIDWORD:
2293 lp->sl_midword = read_string(fd, len); /* <midword> */
2294 if (lp->sl_midword == NULL)
2295 goto endFAIL;
2296 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002297
Bram Moolenaar5195e452005-08-19 20:32:47 +00002298 case SN_PREFCOND:
2299 res = read_prefcond_section(fd, lp);
2300 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002301
Bram Moolenaar5195e452005-08-19 20:32:47 +00002302 case SN_REP:
2303 res = read_rep_section(fd, lp);
2304 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002305
Bram Moolenaar5195e452005-08-19 20:32:47 +00002306 case SN_SAL:
2307 res = read_sal_section(fd, lp);
2308 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002309
Bram Moolenaar5195e452005-08-19 20:32:47 +00002310 case SN_SOFO:
2311 res = read_sofo_section(fd, lp);
2312 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002313
Bram Moolenaar5195e452005-08-19 20:32:47 +00002314 case SN_MAP:
2315 p = read_string(fd, len); /* <mapstr> */
2316 if (p == NULL)
2317 goto endFAIL;
2318 set_map_str(lp, p);
2319 vim_free(p);
2320 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002321
Bram Moolenaar5195e452005-08-19 20:32:47 +00002322 case SN_COMPOUND:
2323 res = read_compound(fd, lp, len);
2324 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002325
Bram Moolenaar78622822005-08-23 21:00:13 +00002326 case SN_NOBREAK:
2327 lp->sl_nobreak = TRUE;
2328 break;
2329
Bram Moolenaar5195e452005-08-19 20:32:47 +00002330 case SN_SYLLABLE:
2331 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2332 if (lp->sl_syllable == NULL)
2333 goto endFAIL;
2334 if (init_syl_tab(lp) == FAIL)
2335 goto endFAIL;
2336 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002337
Bram Moolenaar5195e452005-08-19 20:32:47 +00002338 default:
2339 /* Unsupported section. When it's required give an error
2340 * message. When it's not required skip the contents. */
2341 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002342 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002343 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002344 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002345 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002346 while (--len >= 0)
2347 if (getc(fd) < 0)
2348 goto truncerr;
2349 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002350 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002351 if (res == SP_FORMERROR)
2352 {
2353formerr:
2354 EMSG(_(e_format));
2355 goto endFAIL;
2356 }
2357 if (res == SP_TRUNCERROR)
2358 {
2359truncerr:
2360 EMSG(_(e_spell_trunc));
2361 goto endFAIL;
2362 }
2363 if (res == SP_ERROR)
2364 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002365 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002366
Bram Moolenaar51485f02005-06-04 21:55:20 +00002367 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002368 * round 2: <KWORDTREE>
2369 * round 3: <PREFIXTREE> */
2370 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002371 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002372 /* The tree size was computed when writing the file, so that we can
2373 * allocate it as one long block. <nodecount> */
2374 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2375 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002376 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002377 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002378 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002379 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002380 bp = lalloc((long_u)len, TRUE);
2381 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002382 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002383 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002384 lp->sl_fbyts = bp;
2385 else if (round == 2)
2386 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002387 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002388 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002389
2390 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002391 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2392 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002393 goto endFAIL;
2394 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002395 lp->sl_fidxs = ip;
2396 else if (round == 2)
2397 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002398 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002399 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002400
2401 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002402 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002403 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002404 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002405 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002406 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002407 }
2408 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002409
Bram Moolenaarb765d632005-06-07 21:00:02 +00002410 /* For a new file link it in the list of spell files. */
2411 if (old_lp == NULL)
2412 {
2413 lp->sl_next = first_lang;
2414 first_lang = lp;
2415 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002416
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002417 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002418
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002419endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002420 if (lang != NULL)
2421 /* truncating the name signals the error to spell_load_lang() */
2422 *lang = NUL;
2423 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002424 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002425 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002426 lp = NULL;
2427 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002428
2429endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002430 if (fd != NULL)
2431 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002432 sourcing_name = save_sourcing_name;
2433 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002434
2435 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002436}
2437
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002438/*
2439 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002440 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002441 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002442 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2443 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002444 */
2445 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002446read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002447 FILE *fd;
2448 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002449 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002450{
2451 int cnt = 0;
2452 int i;
2453 char_u *str;
2454
2455 /* read the length bytes, MSB first */
2456 for (i = 0; i < cnt_bytes; ++i)
2457 cnt = (cnt << 8) + getc(fd);
2458 if (cnt < 0)
2459 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002460 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002461 return NULL;
2462 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002463 *cntp = cnt;
2464 if (cnt == 0)
2465 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002466
Bram Moolenaar5195e452005-08-19 20:32:47 +00002467 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002468 if (str == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002469 *cntp = SP_ERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002470 return str;
2471}
2472
Bram Moolenaar7887d882005-07-01 22:33:52 +00002473/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002474 * Read a string of length "cnt" from "fd" into allocated memory.
2475 * Returns NULL when out of memory.
2476 */
2477 static char_u *
2478read_string(fd, cnt)
2479 FILE *fd;
2480 int cnt;
2481{
2482 char_u *str;
2483 int i;
2484
2485 /* allocate memory */
2486 str = alloc((unsigned)cnt + 1);
2487 if (str != NULL)
2488 {
2489 /* Read the string. Doesn't check for truncated file. */
2490 for (i = 0; i < cnt; ++i)
2491 str[i] = getc(fd);
2492 str[i] = NUL;
2493 }
2494 return str;
2495}
2496
2497/*
2498 * Read SN_REGION: <regionname> ...
2499 * Return SP_*ERROR flags.
2500 */
2501 static int
2502read_region_section(fd, lp, len)
2503 FILE *fd;
2504 slang_T *lp;
2505 int len;
2506{
2507 int i;
2508
2509 if (len > 16)
2510 return SP_FORMERROR;
2511 for (i = 0; i < len; ++i)
2512 lp->sl_regions[i] = getc(fd); /* <regionname> */
2513 lp->sl_regions[len] = NUL;
2514 return 0;
2515}
2516
2517/*
2518 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2519 * <folcharslen> <folchars>
2520 * Return SP_*ERROR flags.
2521 */
2522 static int
2523read_charflags_section(fd)
2524 FILE *fd;
2525{
2526 char_u *flags;
2527 char_u *fol;
2528 int flagslen, follen;
2529
2530 /* <charflagslen> <charflags> */
2531 flags = read_cnt_string(fd, 1, &flagslen);
2532 if (flagslen < 0)
2533 return flagslen;
2534
2535 /* <folcharslen> <folchars> */
2536 fol = read_cnt_string(fd, 2, &follen);
2537 if (follen < 0)
2538 {
2539 vim_free(flags);
2540 return follen;
2541 }
2542
2543 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
2544 if (flags != NULL && fol != NULL)
2545 set_spell_charflags(flags, flagslen, fol);
2546
2547 vim_free(flags);
2548 vim_free(fol);
2549
2550 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
2551 if ((flags == NULL) != (fol == NULL))
2552 return SP_FORMERROR;
2553 return 0;
2554}
2555
2556/*
2557 * Read SN_PREFCOND section.
2558 * Return SP_*ERROR flags.
2559 */
2560 static int
2561read_prefcond_section(fd, lp)
2562 FILE *fd;
2563 slang_T *lp;
2564{
2565 int cnt;
2566 int i;
2567 int n;
2568 char_u *p;
2569 char_u buf[MAXWLEN + 1];
2570
2571 /* <prefcondcnt> <prefcond> ... */
2572 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
2573 if (cnt <= 0)
2574 return SP_FORMERROR;
2575
2576 lp->sl_prefprog = (regprog_T **)alloc_clear(
2577 (unsigned)sizeof(regprog_T *) * cnt);
2578 if (lp->sl_prefprog == NULL)
2579 return SP_ERROR;
2580 lp->sl_prefixcnt = cnt;
2581
2582 for (i = 0; i < cnt; ++i)
2583 {
2584 /* <prefcond> : <condlen> <condstr> */
2585 n = getc(fd); /* <condlen> */
2586 if (n < 0 || n >= MAXWLEN)
2587 return SP_FORMERROR;
2588
2589 /* When <condlen> is zero we have an empty condition. Otherwise
2590 * compile the regexp program used to check for the condition. */
2591 if (n > 0)
2592 {
2593 buf[0] = '^'; /* always match at one position only */
2594 p = buf + 1;
2595 while (n-- > 0)
2596 *p++ = getc(fd); /* <condstr> */
2597 *p = NUL;
2598 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
2599 }
2600 }
2601 return 0;
2602}
2603
2604/*
2605 * Read REP items section from "fd": <repcount> <rep> ...
2606 * Return SP_*ERROR flags.
2607 */
2608 static int
2609read_rep_section(fd, slang)
2610 FILE *fd;
2611 slang_T *slang;
2612{
2613 int cnt;
2614 garray_T *gap;
2615 fromto_T *ftp;
2616 short *first;
2617 int i;
2618
2619 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
2620 if (cnt < 0)
2621 return SP_TRUNCERROR;
2622
2623 gap = &slang->sl_rep;
2624 if (ga_grow(gap, cnt) == FAIL)
2625 return SP_ERROR;
2626
2627 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
2628 for (; gap->ga_len < cnt; ++gap->ga_len)
2629 {
2630 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
2631 ftp->ft_from = read_cnt_string(fd, 1, &i);
2632 if (i < 0)
2633 return i;
2634 if (i == 0)
2635 return SP_FORMERROR;
2636 ftp->ft_to = read_cnt_string(fd, 1, &i);
2637 if (i <= 0)
2638 {
2639 vim_free(ftp->ft_from);
2640 if (i < 0)
2641 return i;
2642 return SP_FORMERROR;
2643 }
2644 }
2645
2646 /* Fill the first-index table. */
2647 first = slang->sl_rep_first;
2648 for (i = 0; i < 256; ++i)
2649 first[i] = -1;
2650 for (i = 0; i < gap->ga_len; ++i)
2651 {
2652 ftp = &((fromto_T *)gap->ga_data)[i];
2653 if (first[*ftp->ft_from] == -1)
2654 first[*ftp->ft_from] = i;
2655 }
2656 return 0;
2657}
2658
2659/*
2660 * Read SN_SAL section: <salflags> <salcount> <sal> ...
2661 * Return SP_*ERROR flags.
2662 */
2663 static int
2664read_sal_section(fd, slang)
2665 FILE *fd;
2666 slang_T *slang;
2667{
2668 int i;
2669 int cnt;
2670 garray_T *gap;
2671 salitem_T *smp;
2672 int ccnt;
2673 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002674 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002675
2676 slang->sl_sofo = FALSE;
2677
2678 i = getc(fd); /* <salflags> */
2679 if (i & SAL_F0LLOWUP)
2680 slang->sl_followup = TRUE;
2681 if (i & SAL_COLLAPSE)
2682 slang->sl_collapse = TRUE;
2683 if (i & SAL_REM_ACCENTS)
2684 slang->sl_rem_accents = TRUE;
2685
2686 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
2687 if (cnt < 0)
2688 return SP_TRUNCERROR;
2689
2690 gap = &slang->sl_sal;
2691 ga_init2(gap, sizeof(salitem_T), 10);
2692 if (ga_grow(gap, cnt) == FAIL)
2693 return SP_ERROR;
2694
2695 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
2696 for (; gap->ga_len < cnt; ++gap->ga_len)
2697 {
2698 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2699 ccnt = getc(fd); /* <salfromlen> */
2700 if (ccnt < 0)
2701 return SP_TRUNCERROR;
2702 if ((p = alloc(ccnt + 2)) == NULL)
2703 return SP_ERROR;
2704 smp->sm_lead = p;
2705
2706 /* Read up to the first special char into sm_lead. */
2707 for (i = 0; i < ccnt; ++i)
2708 {
2709 c = getc(fd); /* <salfrom> */
2710 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
2711 break;
2712 *p++ = c;
2713 }
2714 smp->sm_leadlen = p - smp->sm_lead;
2715 *p++ = NUL;
2716
2717 /* Put (abc) chars in sm_oneof, if any. */
2718 if (c == '(')
2719 {
2720 smp->sm_oneof = p;
2721 for (++i; i < ccnt; ++i)
2722 {
2723 c = getc(fd); /* <salfrom> */
2724 if (c == ')')
2725 break;
2726 *p++ = c;
2727 }
2728 *p++ = NUL;
2729 if (++i < ccnt)
2730 c = getc(fd);
2731 }
2732 else
2733 smp->sm_oneof = NULL;
2734
2735 /* Any following chars go in sm_rules. */
2736 smp->sm_rules = p;
2737 if (i < ccnt)
2738 /* store the char we got while checking for end of sm_lead */
2739 *p++ = c;
2740 for (++i; i < ccnt; ++i)
2741 *p++ = getc(fd); /* <salfrom> */
2742 *p++ = NUL;
2743
2744 /* <saltolen> <salto> */
2745 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
2746 if (ccnt < 0)
2747 {
2748 vim_free(smp->sm_lead);
2749 return ccnt;
2750 }
2751
2752#ifdef FEAT_MBYTE
2753 if (has_mbyte)
2754 {
2755 /* convert the multi-byte strings to wide char strings */
2756 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2757 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2758 if (smp->sm_oneof == NULL)
2759 smp->sm_oneof_w = NULL;
2760 else
2761 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
2762 if (smp->sm_to == NULL)
2763 smp->sm_to_w = NULL;
2764 else
2765 smp->sm_to_w = mb_str2wide(smp->sm_to);
2766 if (smp->sm_lead_w == NULL
2767 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
2768 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
2769 {
2770 vim_free(smp->sm_lead);
2771 vim_free(smp->sm_to);
2772 vim_free(smp->sm_lead_w);
2773 vim_free(smp->sm_oneof_w);
2774 vim_free(smp->sm_to_w);
2775 return SP_ERROR;
2776 }
2777 }
2778#endif
2779 }
2780
2781 /* Fill the first-index table. */
2782 set_sal_first(slang);
2783
2784 return 0;
2785}
2786
2787/*
2788 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
2789 * Return SP_*ERROR flags.
2790 */
2791 static int
2792read_sofo_section(fd, slang)
2793 FILE *fd;
2794 slang_T *slang;
2795{
2796 int cnt;
2797 char_u *from, *to;
2798 int res;
2799
2800 slang->sl_sofo = TRUE;
2801
2802 /* <sofofromlen> <sofofrom> */
2803 from = read_cnt_string(fd, 2, &cnt);
2804 if (cnt < 0)
2805 return cnt;
2806
2807 /* <sofotolen> <sofoto> */
2808 to = read_cnt_string(fd, 2, &cnt);
2809 if (cnt < 0)
2810 {
2811 vim_free(from);
2812 return cnt;
2813 }
2814
2815 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
2816 if (from != NULL && to != NULL)
2817 res = set_sofo(slang, from, to);
2818 else if (from != NULL || to != NULL)
2819 res = SP_FORMERROR; /* only one of two strings is an error */
2820 else
2821 res = 0;
2822
2823 vim_free(from);
2824 vim_free(to);
2825 return res;
2826}
2827
2828/*
2829 * Read the compound section from the .spl file:
2830 * <compmax> <compminlen> <compsylmax> <compflags>
2831 * Returns SP_*ERROR flags.
2832 */
2833 static int
2834read_compound(fd, slang, len)
2835 FILE *fd;
2836 slang_T *slang;
2837 int len;
2838{
2839 int todo = len;
2840 int c;
2841 int atstart;
2842 char_u *pat;
2843 char_u *pp;
2844 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002845 char_u *ap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002846
2847 if (todo < 2)
2848 return SP_FORMERROR; /* need at least two bytes */
2849
2850 --todo;
2851 c = getc(fd); /* <compmax> */
2852 if (c < 2)
2853 c = MAXWLEN;
2854 slang->sl_compmax = c;
2855
2856 --todo;
2857 c = getc(fd); /* <compminlen> */
2858 if (c < 1)
2859 c = 3;
2860 slang->sl_compminlen = c;
2861
2862 --todo;
2863 c = getc(fd); /* <compsylmax> */
2864 if (c < 1)
2865 c = MAXWLEN;
2866 slang->sl_compsylmax = c;
2867
2868 /* Turn the COMPOUNDFLAGS items into a regexp pattern:
2869 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
2870 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes. */
2871 pat = alloc((unsigned)todo * 2 + 7);
2872 if (pat == NULL)
2873 return SP_ERROR;
2874
Bram Moolenaard12a1322005-08-21 22:08:24 +00002875 /* We also need a list of all flags that can appear at the start and one
2876 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002877 cp = alloc(todo + 1);
2878 if (cp == NULL)
2879 {
2880 vim_free(pat);
2881 return SP_ERROR;
2882 }
2883 slang->sl_compstartflags = cp;
2884 *cp = NUL;
2885
Bram Moolenaard12a1322005-08-21 22:08:24 +00002886 ap = alloc(todo + 1);
2887 if (ap == NULL)
2888 {
2889 vim_free(pat);
2890 return SP_ERROR;
2891 }
2892 slang->sl_compallflags = ap;
2893 *ap = NUL;
2894
Bram Moolenaar5195e452005-08-19 20:32:47 +00002895 pp = pat;
2896 *pp++ = '^';
2897 *pp++ = '\\';
2898 *pp++ = '(';
2899
2900 atstart = 1;
2901 while (todo-- > 0)
2902 {
2903 c = getc(fd); /* <compflags> */
Bram Moolenaard12a1322005-08-21 22:08:24 +00002904
2905 /* Add all flags to "sl_compallflags". */
2906 if (vim_strchr((char_u *)"+*[]/", c) == NULL
2907 && vim_strchr(slang->sl_compallflags, c) == NULL)
2908 {
2909 *ap++ = c;
2910 *ap = NUL;
2911 }
2912
Bram Moolenaar5195e452005-08-19 20:32:47 +00002913 if (atstart != 0)
2914 {
2915 /* At start of item: copy flags to "sl_compstartflags". For a
2916 * [abc] item set "atstart" to 2 and copy up to the ']'. */
2917 if (c == '[')
2918 atstart = 2;
2919 else if (c == ']')
2920 atstart = 0;
2921 else
2922 {
2923 if (vim_strchr(slang->sl_compstartflags, c) == NULL)
2924 {
2925 *cp++ = c;
2926 *cp = NUL;
2927 }
2928 if (atstart == 1)
2929 atstart = 0;
2930 }
2931 }
2932 if (c == '/') /* slash separates two items */
2933 {
2934 *pp++ = '\\';
2935 *pp++ = '|';
2936 atstart = 1;
2937 }
2938 else /* normal char, "[abc]" and '*' are copied as-is */
2939 {
2940 if (c == '+')
2941 *pp++ = '\\'; /* "a+" becomes "a\+" */
2942 *pp++ = c;
2943 }
2944 }
2945
2946 *pp++ = '\\';
2947 *pp++ = ')';
2948 *pp++ = '$';
2949 *pp = NUL;
2950
2951 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
2952 vim_free(pat);
2953 if (slang->sl_compprog == NULL)
2954 return SP_FORMERROR;
2955
2956 return 0;
2957}
2958
2959#define SY_MAXLEN 30
2960typedef struct syl_item_S
2961{
2962 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
2963 int sy_len;
2964} syl_item_T;
2965
2966/*
2967 * Truncate "slang->sl_syllable" at the first slash and put the following items
2968 * in "slang->sl_syl_items".
2969 */
2970 static int
2971init_syl_tab(slang)
2972 slang_T *slang;
2973{
2974 char_u *p;
2975 char_u *s;
2976 int l;
2977 syl_item_T *syl;
2978
2979 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
2980 p = vim_strchr(slang->sl_syllable, '/');
2981 while (p != NULL)
2982 {
2983 *p++ = NUL;
2984 if (p == NUL)
2985 break;
2986 s = p;
2987 p = vim_strchr(p, '/');
2988 if (p == NULL)
2989 l = STRLEN(s);
2990 else
2991 l = p - s;
2992 if (l >= SY_MAXLEN)
2993 return SP_FORMERROR;
2994 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
2995 return SP_ERROR;
2996 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
2997 + slang->sl_syl_items.ga_len++;
2998 vim_strncpy(syl->sy_chars, s, l);
2999 syl->sy_len = l;
3000 }
3001 return OK;
3002}
3003
3004/*
3005 * Count the number of syllables in "word".
3006 * When "word" contains spaces the syllables after the last space are counted.
3007 * Returns zero if syllables are not defines.
3008 */
3009 static int
3010count_syllables(slang, word)
3011 slang_T *slang;
3012 char_u *word;
3013{
3014 int cnt = 0;
3015 int skip = FALSE;
3016 char_u *p;
3017 int len;
3018 int i;
3019 syl_item_T *syl;
3020 int c;
3021
3022 if (slang->sl_syllable == NULL)
3023 return 0;
3024
3025 for (p = word; *p != NUL; p += len)
3026 {
3027 /* When running into a space reset counter. */
3028 if (*p == ' ')
3029 {
3030 len = 1;
3031 cnt = 0;
3032 continue;
3033 }
3034
3035 /* Find longest match of syllable items. */
3036 len = 0;
3037 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3038 {
3039 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3040 if (syl->sy_len > len
3041 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3042 len = syl->sy_len;
3043 }
3044 if (len != 0) /* found a match, count syllable */
3045 {
3046 ++cnt;
3047 skip = FALSE;
3048 }
3049 else
3050 {
3051 /* No recognized syllable item, at least a syllable char then? */
3052#ifdef FEAT_MBYTE
3053 c = mb_ptr2char(p);
3054 len = (*mb_ptr2len)(p);
3055#else
3056 c = *p;
3057 len = 1;
3058#endif
3059 if (vim_strchr(slang->sl_syllable, c) == NULL)
3060 skip = FALSE; /* No, search for next syllable */
3061 else if (!skip)
3062 {
3063 ++cnt; /* Yes, count it */
3064 skip = TRUE; /* don't count following syllable chars */
3065 }
3066 }
3067 }
3068 return cnt;
3069}
3070
3071/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003072 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003073 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003074 */
3075 static int
3076set_sofo(lp, from, to)
3077 slang_T *lp;
3078 char_u *from;
3079 char_u *to;
3080{
3081 int i;
3082
3083#ifdef FEAT_MBYTE
3084 garray_T *gap;
3085 char_u *s;
3086 char_u *p;
3087 int c;
3088 int *inp;
3089
3090 if (has_mbyte)
3091 {
3092 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3093 * characters. The index is the low byte of the character.
3094 * The list contains from-to pairs with a terminating NUL.
3095 * sl_sal_first[] is used for latin1 "from" characters. */
3096 gap = &lp->sl_sal;
3097 ga_init2(gap, sizeof(int *), 1);
3098 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003099 return SP_ERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003100 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3101 gap->ga_len = 256;
3102
3103 /* First count the number of items for each list. Temporarily use
3104 * sl_sal_first[] for this. */
3105 for (p = from, s = to; *p != NUL && *s != NUL; )
3106 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003107 c = mb_cptr2char_adv(&p);
3108 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003109 if (c >= 256)
3110 ++lp->sl_sal_first[c & 0xff];
3111 }
3112 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003113 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003114
3115 /* Allocate the lists. */
3116 for (i = 0; i < 256; ++i)
3117 if (lp->sl_sal_first[i] > 0)
3118 {
3119 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3120 if (p == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003121 return SP_ERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003122 ((int **)gap->ga_data)[i] = (int *)p;
3123 *(int *)p = 0;
3124 }
3125
3126 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3127 * list. */
3128 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3129 for (p = from, s = to; *p != NUL && *s != NUL; )
3130 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003131 c = mb_cptr2char_adv(&p);
3132 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003133 if (c >= 256)
3134 {
3135 /* Append the from-to chars at the end of the list with
3136 * the low byte. */
3137 inp = ((int **)gap->ga_data)[c & 0xff];
3138 while (*inp != 0)
3139 ++inp;
3140 *inp++ = c; /* from char */
3141 *inp++ = i; /* to char */
3142 *inp++ = NUL; /* NUL at the end */
3143 }
3144 else
3145 /* mapping byte to char is done in sl_sal_first[] */
3146 lp->sl_sal_first[c] = i;
3147 }
3148 }
3149 else
3150#endif
3151 {
3152 /* mapping bytes to bytes is done in sl_sal_first[] */
3153 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003154 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003155
3156 for (i = 0; to[i] != NUL; ++i)
3157 lp->sl_sal_first[from[i]] = to[i];
3158 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3159 }
3160
Bram Moolenaar5195e452005-08-19 20:32:47 +00003161 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003162}
3163
3164/*
3165 * Fill the first-index table for "lp".
3166 */
3167 static void
3168set_sal_first(lp)
3169 slang_T *lp;
3170{
3171 salfirst_T *sfirst;
3172 int i;
3173 salitem_T *smp;
3174 int c;
3175 garray_T *gap = &lp->sl_sal;
3176
3177 sfirst = lp->sl_sal_first;
3178 for (i = 0; i < 256; ++i)
3179 sfirst[i] = -1;
3180 smp = (salitem_T *)gap->ga_data;
3181 for (i = 0; i < gap->ga_len; ++i)
3182 {
3183#ifdef FEAT_MBYTE
3184 if (has_mbyte)
3185 /* Use the lowest byte of the first character. For latin1 it's
3186 * the character, for other encodings it should differ for most
3187 * characters. */
3188 c = *smp[i].sm_lead_w & 0xff;
3189 else
3190#endif
3191 c = *smp[i].sm_lead;
3192 if (sfirst[c] == -1)
3193 {
3194 sfirst[c] = i;
3195#ifdef FEAT_MBYTE
3196 if (has_mbyte)
3197 {
3198 int n;
3199
3200 /* Make sure all entries with this byte are following each
3201 * other. Move the ones that are in the wrong position. Do
3202 * keep the same ordering! */
3203 while (i + 1 < gap->ga_len
3204 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3205 /* Skip over entry with same index byte. */
3206 ++i;
3207
3208 for (n = 1; i + n < gap->ga_len; ++n)
3209 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3210 {
3211 salitem_T tsal;
3212
3213 /* Move entry with same index byte after the entries
3214 * we already found. */
3215 ++i;
3216 --n;
3217 tsal = smp[i + n];
3218 mch_memmove(smp + i + 1, smp + i,
3219 sizeof(salitem_T) * n);
3220 smp[i] = tsal;
3221 }
3222 }
3223#endif
3224 }
3225 }
3226}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003227
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003228#ifdef FEAT_MBYTE
3229/*
3230 * Turn a multi-byte string into a wide character string.
3231 * Return it in allocated memory (NULL for out-of-memory)
3232 */
3233 static int *
3234mb_str2wide(s)
3235 char_u *s;
3236{
3237 int *res;
3238 char_u *p;
3239 int i = 0;
3240
3241 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3242 if (res != NULL)
3243 {
3244 for (p = s; *p != NUL; )
3245 res[i++] = mb_ptr2char_adv(&p);
3246 res[i] = NUL;
3247 }
3248 return res;
3249}
3250#endif
3251
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003252/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003253 * Read one row of siblings from the spell file and store it in the byte array
3254 * "byts" and index array "idxs". Recursively read the children.
3255 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00003256 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00003257 *
3258 * Returns the index follosing the siblings.
3259 * Returns -1 if the file is shorter than expected.
3260 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003261 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003262 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003263read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003264 FILE *fd;
3265 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003266 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003267 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003268 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003269 int prefixtree; /* TRUE for reading PREFIXTREE */
3270 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003271{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003272 int len;
3273 int i;
3274 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003275 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003276 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003277 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003278#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003279
Bram Moolenaar51485f02005-06-04 21:55:20 +00003280 len = getc(fd); /* <siblingcount> */
3281 if (len <= 0)
3282 return -1;
3283
3284 if (startidx + len >= maxidx)
3285 return -2;
3286 byts[idx++] = len;
3287
3288 /* Read the byte values, flag/region bytes and shared indexes. */
3289 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003290 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003291 c = getc(fd); /* <byte> */
3292 if (c < 0)
3293 return -1;
3294 if (c <= BY_SPECIAL)
3295 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003296 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003297 {
3298 /* No flags, all regions. */
3299 idxs[idx] = 0;
3300 c = 0;
3301 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003302 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003303 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003304 if (prefixtree)
3305 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003306 /* Read the optional pflags byte, the prefix ID and the
3307 * condition nr. In idxs[] store the prefix ID in the low
3308 * byte, the condition index shifted up 8 bits, the flags
3309 * shifted up 24 bits. */
3310 if (c == BY_FLAGS)
3311 c = getc(fd) << 24; /* <pflags> */
3312 else
3313 c = 0;
3314
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003315 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003316
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003317 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
3318 if (n >= maxprefcondnr)
3319 return -2;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003320 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003321 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003322 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003323 {
3324 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003325 * idxs[] the flags go in the low two bytes, region above
3326 * that and prefix ID above the region. */
3327 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003328 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003329 if (c2 == BY_FLAGS2)
3330 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003331 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003332 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003333 if (c & WF_AFX)
3334 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003335 }
3336
Bram Moolenaar51485f02005-06-04 21:55:20 +00003337 idxs[idx] = c;
3338 c = 0;
3339 }
3340 else /* c == BY_INDEX */
3341 {
3342 /* <nodeidx> */
3343 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
3344 if (n < 0 || n >= maxidx)
3345 return -2;
3346 idxs[idx] = n + SHARED_MASK;
3347 c = getc(fd); /* <xbyte> */
3348 }
3349 }
3350 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003351 }
3352
Bram Moolenaar51485f02005-06-04 21:55:20 +00003353 /* Recursively read the children for non-shared siblings.
3354 * Skip the end-of-word ones (zero byte value) and the shared ones (and
3355 * remove SHARED_MASK) */
3356 for (i = 1; i <= len; ++i)
3357 if (byts[startidx + i] != 0)
3358 {
3359 if (idxs[startidx + i] & SHARED_MASK)
3360 idxs[startidx + i] &= ~SHARED_MASK;
3361 else
3362 {
3363 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003364 idx = read_tree(fd, byts, idxs, maxidx, idx,
3365 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003366 if (idx < 0)
3367 break;
3368 }
3369 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003370
Bram Moolenaar51485f02005-06-04 21:55:20 +00003371 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003372}
3373
3374/*
3375 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003376 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003377 */
3378 char_u *
3379did_set_spelllang(buf)
3380 buf_T *buf;
3381{
3382 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003383 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003384 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00003385 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003386 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003387 int region_mask;
3388 slang_T *lp;
3389 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003390 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003391 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003392 int len;
3393 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003394 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003395 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003396 char_u *use_region = NULL;
3397 int dont_use_region = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003398
3399 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003400 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003401
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003402 /* loop over comma separated language names. */
3403 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003404 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003405 /* Get one language name. */
3406 copy_option_part(&splp, lang, MAXWLEN, ",");
3407
Bram Moolenaar5482f332005-04-17 20:18:43 +00003408 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003409 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003410
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003411 /* If the name ends in ".spl" use it as the name of the spell file.
3412 * If there is a region name let "region" point to it and remove it
3413 * from the name. */
3414 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
3415 {
3416 filename = TRUE;
3417
Bram Moolenaarb6356332005-07-18 21:40:44 +00003418 /* Locate a region and remove it from the file name. */
3419 p = vim_strchr(gettail(lang), '_');
3420 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
3421 && !ASCII_ISALPHA(p[3]))
3422 {
3423 vim_strncpy(region_cp, p + 1, 2);
3424 mch_memmove(p, p + 3, len - (p - lang) - 2);
3425 len -= 3;
3426 region = region_cp;
3427 }
3428 else
3429 dont_use_region = TRUE;
3430
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003431 /* Check if we loaded this language before. */
3432 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3433 if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME)
3434 break;
3435 }
3436 else
3437 {
3438 filename = FALSE;
3439 if (len > 3 && lang[len - 3] == '_')
3440 {
3441 region = lang + len - 2;
3442 len -= 3;
3443 lang[len] = NUL;
3444 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003445 else
3446 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003447
3448 /* Check if we loaded this language before. */
3449 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3450 if (STRICMP(lang, lp->sl_name) == 0)
3451 break;
3452 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003453
Bram Moolenaarb6356332005-07-18 21:40:44 +00003454 if (region != NULL)
3455 {
3456 /* If the region differs from what was used before then don't
3457 * use it for 'spellfile'. */
3458 if (use_region != NULL && STRCMP(region, use_region) != 0)
3459 dont_use_region = TRUE;
3460 use_region = region;
3461 }
3462
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003463 /* If not found try loading the language now. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003464 if (lp == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003465 {
3466 if (filename)
3467 (void)spell_load_file(lang, lang, NULL, FALSE);
3468 else
3469 spell_load_lang(lang);
3470 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003471
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003472 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003473 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003474 */
3475 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003476 if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME
3477 : STRICMP(lang, lp->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003478 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003479 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003480 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003481 {
3482 /* find region in sl_regions */
3483 c = find_region(lp->sl_regions, region);
3484 if (c == REGION_ALL)
3485 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003486 if (lp->sl_add)
3487 {
3488 if (*lp->sl_regions != NUL)
3489 /* This addition file is for other regions. */
3490 region_mask = 0;
3491 }
3492 else
3493 /* This is probably an error. Give a warning and
3494 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003495 smsg((char_u *)
3496 _("Warning: region %s not supported"),
3497 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003498 }
3499 else
3500 region_mask = 1 << c;
3501 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003502
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003503 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003504 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003505 if (ga_grow(&ga, 1) == FAIL)
3506 {
3507 ga_clear(&ga);
3508 return e_outofmem;
3509 }
3510 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
3511 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
3512 ++ga.ga_len;
3513 use_midword(lp, buf);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003514 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003515 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003516 }
3517
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003518 /* round 0: load int_wordlist, if possible.
3519 * round 1: load first name in 'spellfile'.
3520 * round 2: load second name in 'spellfile.
3521 * etc. */
3522 spf = curbuf->b_p_spf;
3523 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003524 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003525 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003526 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003527 /* Internal wordlist, if there is one. */
3528 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003529 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003530 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003531 }
3532 else
3533 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003534 /* One entry in 'spellfile'. */
3535 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
3536 STRCAT(spf_name, ".spl");
3537
3538 /* If it was already found above then skip it. */
3539 for (c = 0; c < ga.ga_len; ++c)
3540 if (fullpathcmp(spf_name,
3541 LANGP_ENTRY(ga, c)->lp_slang->sl_fname,
3542 FALSE) == FPC_SAME)
3543 break;
3544 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003545 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003546 }
3547
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003548 /* Check if it was loaded already. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003549 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3550 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
3551 break;
3552 if (lp == NULL)
3553 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003554 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003555 * region name, the region is ignored otherwise. for int_wordlist
3556 * use an arbitrary name. */
3557 if (round == 0)
3558 STRCPY(lang, "internal wordlist");
3559 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00003560 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003561 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003562 p = vim_strchr(lang, '.');
3563 if (p != NULL)
3564 *p = NUL; /* truncate at ".encoding.add" */
3565 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003566 lp = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003567 }
3568 if (lp != NULL && ga_grow(&ga, 1) == OK)
3569 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003570 region_mask = REGION_ALL;
3571 if (use_region != NULL && !dont_use_region)
3572 {
3573 /* find region in sl_regions */
3574 c = find_region(lp->sl_regions, use_region);
3575 if (c != REGION_ALL)
3576 region_mask = 1 << c;
3577 else if (*lp->sl_regions != NUL)
3578 /* This spell file is for other regions. */
3579 region_mask = 0;
3580 }
3581
3582 if (region_mask != 0)
3583 {
3584 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
3585 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
3586 ++ga.ga_len;
3587 use_midword(lp, buf);
3588 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003589 }
3590 }
3591
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003592 /* Add a NULL entry to mark the end of the list. */
3593 if (ga_grow(&ga, 1) == FAIL)
3594 {
3595 ga_clear(&ga);
3596 return e_outofmem;
3597 }
3598 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
3599 ++ga.ga_len;
3600
3601 /* Everything is fine, store the new b_langp value. */
3602 ga_clear(&buf->b_langp);
3603 buf->b_langp = ga;
3604
3605 return NULL;
3606}
3607
3608/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003609 * Clear the midword characters for buffer "buf".
3610 */
3611 static void
3612clear_midword(buf)
3613 buf_T *buf;
3614{
3615 vim_memset(buf->b_spell_ismw, 0, 256);
3616#ifdef FEAT_MBYTE
3617 vim_free(buf->b_spell_ismw_mb);
3618 buf->b_spell_ismw_mb = NULL;
3619#endif
3620}
3621
3622/*
3623 * Use the "sl_midword" field of language "lp" for buffer "buf".
3624 * They add up to any currently used midword characters.
3625 */
3626 static void
3627use_midword(lp, buf)
3628 slang_T *lp;
3629 buf_T *buf;
3630{
3631 char_u *p;
3632
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003633 if (lp->sl_midword == NULL) /* there aren't any */
3634 return;
3635
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003636 for (p = lp->sl_midword; *p != NUL; )
3637#ifdef FEAT_MBYTE
3638 if (has_mbyte)
3639 {
3640 int c, l, n;
3641 char_u *bp;
3642
3643 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003644 l = (*mb_ptr2len)(p);
3645 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003646 buf->b_spell_ismw[c] = TRUE;
3647 else if (buf->b_spell_ismw_mb == NULL)
3648 /* First multi-byte char in "b_spell_ismw_mb". */
3649 buf->b_spell_ismw_mb = vim_strnsave(p, l);
3650 else
3651 {
3652 /* Append multi-byte chars to "b_spell_ismw_mb". */
3653 n = STRLEN(buf->b_spell_ismw_mb);
3654 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
3655 if (bp != NULL)
3656 {
3657 vim_free(buf->b_spell_ismw_mb);
3658 buf->b_spell_ismw_mb = bp;
3659 vim_strncpy(bp + n, p, l);
3660 }
3661 }
3662 p += l;
3663 }
3664 else
3665#endif
3666 buf->b_spell_ismw[*p++] = TRUE;
3667}
3668
3669/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003670 * Find the region "region[2]" in "rp" (points to "sl_regions").
3671 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003672 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003673 */
3674 static int
3675find_region(rp, region)
3676 char_u *rp;
3677 char_u *region;
3678{
3679 int i;
3680
3681 for (i = 0; ; i += 2)
3682 {
3683 if (rp[i] == NUL)
3684 return REGION_ALL;
3685 if (rp[i] == region[0] && rp[i + 1] == region[1])
3686 break;
3687 }
3688 return i / 2;
3689}
3690
3691/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003692 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003693 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00003694 * Word WF_ONECAP
3695 * W WORD WF_ALLCAP
3696 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003697 */
3698 static int
3699captype(word, end)
3700 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003701 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003702{
3703 char_u *p;
3704 int c;
3705 int firstcap;
3706 int allcap;
3707 int past_second = FALSE; /* past second word char */
3708
3709 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003710 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003711 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003712 return 0; /* only non-word characters, illegal word */
3713#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00003714 if (has_mbyte)
3715 c = mb_ptr2char_adv(&p);
3716 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003717#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00003718 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003719 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003720
3721 /*
3722 * Need to check all letters to find a word with mixed upper/lower.
3723 * But a word with an upper char only at start is a ONECAP.
3724 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003725 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003726 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003727 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003728 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003729 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003730 {
3731 /* UUl -> KEEPCAP */
3732 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003733 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003734 allcap = FALSE;
3735 }
3736 else if (!allcap)
3737 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003738 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003739 past_second = TRUE;
3740 }
3741
3742 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003743 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003744 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003745 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003746 return 0;
3747}
3748
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003749/*
3750 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
3751 * capital. So that make_case_word() can turn WOrd into Word.
3752 * Add ALLCAP for "WOrD".
3753 */
3754 static int
3755badword_captype(word, end)
3756 char_u *word;
3757 char_u *end;
3758{
3759 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003760 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003761 int l, u;
3762 int first;
3763 char_u *p;
3764
3765 if (flags & WF_KEEPCAP)
3766 {
3767 /* Count the number of UPPER and lower case letters. */
3768 l = u = 0;
3769 first = FALSE;
3770 for (p = word; p < end; mb_ptr_adv(p))
3771 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003772 c = PTR2CHAR(p);
3773 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003774 {
3775 ++u;
3776 if (p == word)
3777 first = TRUE;
3778 }
3779 else
3780 ++l;
3781 }
3782
3783 /* If there are more UPPER than lower case letters suggest an
3784 * ALLCAP word. Otherwise, if the first letter is UPPER then
3785 * suggest ONECAP. Exception: "ALl" most likely should be "All",
3786 * require three upper case letters. */
3787 if (u > l && u > 2)
3788 flags |= WF_ALLCAP;
3789 else if (first)
3790 flags |= WF_ONECAP;
3791 }
3792 return flags;
3793}
3794
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003795# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
3796/*
3797 * Free all languages.
3798 */
3799 void
3800spell_free_all()
3801{
3802 slang_T *lp;
3803 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003804 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003805
3806 /* Go through all buffers and handle 'spelllang'. */
3807 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3808 ga_clear(&buf->b_langp);
3809
3810 while (first_lang != NULL)
3811 {
3812 lp = first_lang;
3813 first_lang = lp->sl_next;
3814 slang_free(lp);
3815 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003816
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003817 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003818 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003819 /* Delete the internal wordlist and its .spl file */
3820 mch_remove(int_wordlist);
3821 int_wordlist_spl(fname);
3822 mch_remove(fname);
3823 vim_free(int_wordlist);
3824 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003825 }
3826
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003827 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003828}
3829# endif
3830
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003831# if defined(FEAT_MBYTE) || defined(PROTO)
3832/*
3833 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003834 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003835 */
3836 void
3837spell_reload()
3838{
3839 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003840 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003841
Bram Moolenaarea408852005-06-25 22:49:46 +00003842 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003843 init_spell_chartab();
3844
3845 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003846 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003847
3848 /* Go through all buffers and handle 'spelllang'. */
3849 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3850 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003851 /* Only load the wordlists when 'spelllang' is set and there is a
3852 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003853 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00003854 {
3855 FOR_ALL_WINDOWS(wp)
3856 if (wp->w_buffer == buf && wp->w_p_spell)
3857 {
3858 (void)did_set_spelllang(buf);
3859# ifdef FEAT_WINDOWS
3860 break;
3861# endif
3862 }
3863 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003864 }
3865}
3866# endif
3867
Bram Moolenaarb765d632005-06-07 21:00:02 +00003868/*
3869 * Reload the spell file "fname" if it's loaded.
3870 */
3871 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003872spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003873 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003874 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003875{
3876 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003877 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003878
Bram Moolenaarb765d632005-06-07 21:00:02 +00003879 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3880 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
3881 {
3882 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003883 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003884 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003885 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003886 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003887
3888 /* When "zg" was used and the file wasn't loaded yet, should redo
3889 * 'spelllang' to get it loaded. */
3890 if (added_word && !didit)
3891 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003892}
3893
3894
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003895/*
3896 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003897 */
3898
Bram Moolenaar51485f02005-06-04 21:55:20 +00003899#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003900 and .dic file. */
3901/*
3902 * Main structure to store the contents of a ".aff" file.
3903 */
3904typedef struct afffile_S
3905{
3906 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003907 int af_slash; /* character used in word for slash */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003908 int af_rar; /* RAR ID for rare word */
3909 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003910 int af_bad; /* BAD ID for banned word */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003911 int af_needaffix; /* NEEDAFFIX ID */
3912 char_u *af_compflags; /* COMPOUNDFLAG and COMPOUNDFLAGS concat'ed */
3913 int af_compmax; /* COMPOUNDMAX */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003914 int af_compminlen; /* COMPOUNDMIN */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003915 int af_compsylmax; /* COMPOUNDSYLMAX */
3916 char_u *af_syllable; /* SYLLABLE */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003917 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003918 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
3919 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003920} afffile_T;
3921
3922typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003923/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
3924struct affentry_S
3925{
3926 affentry_T *ae_next; /* next affix with same name/number */
3927 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
3928 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003929 char_u *ae_cond; /* condition (NULL for ".") */
3930 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003931 char_u ae_rare; /* rare affix */
3932 char_u ae_nocomp; /* word with affix not compoundable */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003933};
3934
Bram Moolenaar53805d12005-08-01 07:08:33 +00003935#define AH_KEY_LEN 10
3936
Bram Moolenaar51485f02005-06-04 21:55:20 +00003937/* Affix header from ".aff" file. Used for af_pref and af_suff. */
3938typedef struct affheader_S
3939{
Bram Moolenaar53805d12005-08-01 07:08:33 +00003940 /* key for hashtable == name of affix entry */
3941#ifdef FEAT_MBYTE
3942 char_u ah_key[AH_KEY_LEN]; /* multi-byte char plus NUL */
3943#else
3944 char_u ah_key[2]; /* one byte char plus NUL */
3945#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003946 int ah_newID; /* prefix ID after renumbering */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003947 int ah_combine; /* suffix may combine with prefix */
3948 affentry_T *ah_first; /* first affix entry */
3949} affheader_T;
3950
3951#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
3952
3953/*
3954 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003955 * the need to keep track of each allocated thing, everything is freed all at
3956 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00003957 */
3958#define SBLOCKSIZE 16000 /* size of sb_data */
3959typedef struct sblock_S sblock_T;
3960struct sblock_S
3961{
3962 sblock_T *sb_next; /* next block in list */
3963 int sb_used; /* nr of bytes already in use */
3964 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003965};
3966
3967/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003968 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003969 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003970typedef struct wordnode_S wordnode_T;
3971struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003972{
Bram Moolenaar0c405862005-06-22 22:26:26 +00003973 union /* shared to save space */
3974 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003975 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003976 int index; /* index in written nodes (valid after first
3977 round) */
3978 } wn_u1;
3979 union /* shared to save space */
3980 {
3981 wordnode_T *next; /* next node with same hash key */
3982 wordnode_T *wnode; /* parent node that will write this node */
3983 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003984 wordnode_T *wn_child; /* child (next byte in word) */
3985 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
3986 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003987 int wn_refs; /* Nr. of references to this node. Only
3988 relevant for first node in a list of
3989 siblings, in following siblings it is
3990 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003991 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003992 char_u wn_affixID; /* when "wn_byte" is NUL: supported/required
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003993 prefix ID or 0 */
3994 short_u wn_flags; /* when "wn_byte" is NUL: WF_ flags */
3995 short wn_region; /* when "wn_byte" is NUL: region mask; for
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003996 PREFIXTREE it's the prefcondnr */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003997#ifdef SPELL_PRINTTREE
3998 int wn_nr; /* sequence nr for printing */
3999#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004000};
4001
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004002#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4003
Bram Moolenaar51485f02005-06-04 21:55:20 +00004004#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004005
Bram Moolenaar51485f02005-06-04 21:55:20 +00004006/*
4007 * Info used while reading the spell files.
4008 */
4009typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004010{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004011 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004012 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004013
Bram Moolenaar51485f02005-06-04 21:55:20 +00004014 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004015 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004016
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004017 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004018
Bram Moolenaar51485f02005-06-04 21:55:20 +00004019 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004020 long si_blocks_cnt; /* memory blocks allocated */
4021 long si_compress_cnt; /* words to add before lowering
4022 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004023 wordnode_T *si_first_free; /* List of nodes that have been freed during
4024 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004025 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004026#ifdef SPELL_PRINTTREE
4027 int si_wordnode_nr; /* sequence nr for nodes */
4028#endif
4029
4030
Bram Moolenaar51485f02005-06-04 21:55:20 +00004031 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004032 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004033 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004034 int si_region; /* region mask */
4035 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004036 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004037 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004038 int si_msg_count; /* number of words added since last message */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004039 int si_region_count; /* number of regions supported (1 when there
4040 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004041 char_u si_region_name[16]; /* region names; used only if
4042 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004043
4044 garray_T si_rep; /* list of fromto_T entries from REP lines */
4045 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004046 char_u *si_sofofr; /* SOFOFROM text */
4047 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004048 int si_followup; /* soundsalike: ? */
4049 int si_collapse; /* soundsalike: ? */
4050 int si_rem_accents; /* soundsalike: remove accents */
4051 garray_T si_map; /* MAP info concatenated */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004052 char_u *si_midword; /* MIDWORD chars, alloc'ed string or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004053 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004054 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004055 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004056 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004057 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004058 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004059 garray_T si_prefcond; /* table with conditions for postponed
4060 * prefixes, each stored as a string */
4061 int si_newID; /* current value for ah_newID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004062} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004063
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004064static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004065static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004066static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4067static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00004068static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004069static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004070static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004071static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
4072static void get_compflags __ARGS((spellinfo_T *spin, char_u *afflist, char_u *store_afflist));
4073static 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 +00004074static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
4075static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
4076static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004077static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004078static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004079static 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 +00004080static 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 +00004081static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
4082static void deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
4083static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
4084static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
4085static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004086static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004087static void write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00004088static void clear_node __ARGS((wordnode_T *node));
4089static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004090static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004091static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004092
Bram Moolenaar53805d12005-08-01 07:08:33 +00004093/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
4094 * but it must be negative to indicate the prefix tree to tree_add_word().
4095 * Use a negative number with the lower 8 bits zero. */
4096#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004097
Bram Moolenaar5195e452005-08-19 20:32:47 +00004098/*
4099 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
4100 */
4101static long compress_start = 30000; /* memory / SBLOCKSIZE */
4102static long compress_inc = 100; /* memory / SBLOCKSIZE */
4103static long compress_added = 500000; /* word count */
4104
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004105#ifdef SPELL_PRINTTREE
4106/*
4107 * For debugging the tree code: print the current tree in a (more or less)
4108 * readable format, so that we can see what happens when adding a word and/or
4109 * compressing the tree.
4110 * Based on code from Olaf Seibert.
4111 */
4112#define PRINTLINESIZE 1000
4113#define PRINTWIDTH 6
4114
4115#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4116 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4117
4118static char line1[PRINTLINESIZE];
4119static char line2[PRINTLINESIZE];
4120static char line3[PRINTLINESIZE];
4121
4122 static void
4123spell_clear_flags(wordnode_T *node)
4124{
4125 wordnode_T *np;
4126
4127 for (np = node; np != NULL; np = np->wn_sibling)
4128 {
4129 np->wn_u1.index = FALSE;
4130 spell_clear_flags(np->wn_child);
4131 }
4132}
4133
4134 static void
4135spell_print_node(wordnode_T *node, int depth)
4136{
4137 if (node->wn_u1.index)
4138 {
4139 /* Done this node before, print the reference. */
4140 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
4141 PRINTSOME(line2, depth, " ", 0, 0);
4142 PRINTSOME(line3, depth, " ", 0, 0);
4143 msg(line1);
4144 msg(line2);
4145 msg(line3);
4146 }
4147 else
4148 {
4149 node->wn_u1.index = TRUE;
4150
4151 if (node->wn_byte != NUL)
4152 {
4153 if (node->wn_child != NULL)
4154 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
4155 else
4156 /* Cannot happen? */
4157 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
4158 }
4159 else
4160 PRINTSOME(line1, depth, " $ ", 0, 0);
4161
4162 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
4163
4164 if (node->wn_sibling != NULL)
4165 PRINTSOME(line3, depth, " | ", 0, 0);
4166 else
4167 PRINTSOME(line3, depth, " ", 0, 0);
4168
4169 if (node->wn_byte == NUL)
4170 {
4171 msg(line1);
4172 msg(line2);
4173 msg(line3);
4174 }
4175
4176 /* do the children */
4177 if (node->wn_byte != NUL && node->wn_child != NULL)
4178 spell_print_node(node->wn_child, depth + 1);
4179
4180 /* do the siblings */
4181 if (node->wn_sibling != NULL)
4182 {
4183 /* get rid of all parent details except | */
4184 STRCPY(line1, line3);
4185 STRCPY(line2, line3);
4186 spell_print_node(node->wn_sibling, depth);
4187 }
4188 }
4189}
4190
4191 static void
4192spell_print_tree(wordnode_T *root)
4193{
4194 if (root != NULL)
4195 {
4196 /* Clear the "wn_u1.index" fields, used to remember what has been
4197 * done. */
4198 spell_clear_flags(root);
4199
4200 /* Recursively print the tree. */
4201 spell_print_node(root, 0);
4202 }
4203}
4204#endif /* SPELL_PRINTTREE */
4205
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004206/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004207 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00004208 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004209 */
4210 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004211spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004212 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004213 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004214{
4215 FILE *fd;
4216 afffile_T *aff;
4217 char_u rline[MAXLINELEN];
4218 char_u *line;
4219 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004220#define MAXITEMCNT 7
4221 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004222 int itemcnt;
4223 char_u *p;
4224 int lnum = 0;
4225 affheader_T *cur_aff = NULL;
4226 int aff_todo = 0;
4227 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004228 char_u *low = NULL;
4229 char_u *fol = NULL;
4230 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004231 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004232 int do_rep;
4233 int do_sal;
4234 int do_map;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004235 int do_midword;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004236 int do_sofo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004237 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004238 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004239 int l;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004240
Bram Moolenaar51485f02005-06-04 21:55:20 +00004241 /*
4242 * Open the file.
4243 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004244 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004245 if (fd == NULL)
4246 {
4247 EMSG2(_(e_notopen), fname);
4248 return NULL;
4249 }
4250
Bram Moolenaarb765d632005-06-07 21:00:02 +00004251 if (spin->si_verbose || p_verbose > 2)
4252 {
4253 if (!spin->si_verbose)
4254 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004255 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004256 out_flush();
4257 if (!spin->si_verbose)
4258 verbose_leave();
4259 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004260
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004261 /* Only do REP lines when not done in another .aff file already. */
4262 do_rep = spin->si_rep.ga_len == 0;
4263
4264 /* Only do SAL lines when not done in another .aff file already. */
4265 do_sal = spin->si_sal.ga_len == 0;
4266
4267 /* Only do MAP lines when not done in another .aff file already. */
4268 do_map = spin->si_map.ga_len == 0;
4269
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004270 /* Only do MIDWORD line when not done in another .aff file already */
4271 do_midword = spin->si_midword == NULL;
4272
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004273 /* Only do SOFOFROM and SOFOTO when not done in another .aff file already */
4274 do_sofo = spin->si_sofofr == NULL;
4275
Bram Moolenaar51485f02005-06-04 21:55:20 +00004276 /*
4277 * Allocate and init the afffile_T structure.
4278 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004279 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004280 if (aff == NULL)
4281 return NULL;
4282 hash_init(&aff->af_pref);
4283 hash_init(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004284
4285 /*
4286 * Read all the lines in the file one by one.
4287 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004288 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004289 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004290 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004291 ++lnum;
4292
4293 /* Skip comment lines. */
4294 if (*rline == '#')
4295 continue;
4296
4297 /* Convert from "SET" to 'encoding' when needed. */
4298 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004299#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004300 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004301 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004302 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004303 if (pc == NULL)
4304 {
4305 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4306 fname, lnum, rline);
4307 continue;
4308 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004309 line = pc;
4310 }
4311 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004312#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004313 {
4314 pc = NULL;
4315 line = rline;
4316 }
4317
4318 /* Split the line up in white separated items. Put a NUL after each
4319 * item. */
4320 itemcnt = 0;
4321 for (p = line; ; )
4322 {
4323 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
4324 ++p;
4325 if (*p == NUL)
4326 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004327 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004328 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004329 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004330 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004331 ++p;
4332 if (*p == NUL)
4333 break;
4334 *p++ = NUL;
4335 }
4336
4337 /* Handle non-empty lines. */
4338 if (itemcnt > 0)
4339 {
4340 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
4341 && aff->af_enc == NULL)
4342 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004343#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004344 /* Setup for conversion from "ENC" to 'encoding'. */
4345 aff->af_enc = enc_canonize(items[1]);
4346 if (aff->af_enc != NULL && !spin->si_ascii
4347 && convert_setup(&spin->si_conv, aff->af_enc,
4348 p_enc) == FAIL)
4349 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
4350 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004351 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004352#else
4353 smsg((char_u *)_("Conversion in %s not supported"), fname);
4354#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004355 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004356 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2)
4357 {
4358 if (do_midword)
4359 spin->si_midword = vim_strsave(items[1]);
4360 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00004361 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
4362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004363 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004364 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004365 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004366 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004367 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004368 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004369 else if (STRCMP(items[0], "SLASH") == 0 && itemcnt == 2
4370 && aff->af_slash == 0)
4371 {
4372 aff->af_slash = items[1][0];
4373 if (items[1][1] != NUL)
4374 smsg((char_u *)_("Character used for SLASH must be ASCII; in %s line %d: %s"),
4375 fname, lnum, items[1]);
4376 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004377 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
4378 && aff->af_rar == 0)
4379 {
4380 aff->af_rar = items[1][0];
4381 if (items[1][1] != NUL)
4382 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
4383 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004384 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
4385 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004386 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004387 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004388 if (items[1][1] != NUL)
4389 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
4390 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00004391 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
4392 && aff->af_bad == 0)
4393 {
4394 aff->af_bad = items[1][0];
4395 if (items[1][1] != NUL)
4396 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
4397 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004398 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
4399 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004400 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004401 aff->af_needaffix = items[1][0];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004402 if (items[1][1] != NUL)
4403 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
4404 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004405 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
4406 && aff->af_compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004407 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004408 p = getroom(spin, 3, FALSE);
4409 if (p != NULL)
4410 {
4411 /* Turn single flag "c" into COMPOUNDFLAGS compatible
4412 * string "c+". */
4413 p[0] = items[1][0];
4414 p[1] = '+';
4415 p[2] = NUL;
4416 aff->af_compflags = p;
4417 }
4418 if (items[1][1] != NUL)
4419 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
4420 }
4421 else if (STRCMP(items[0], "COMPOUNDFLAGS") == 0 && itemcnt == 2)
4422 {
4423 /* Concatenate this string to previously defined ones, using a
4424 * slash to separate them. */
4425 l = STRLEN(items[1]) + 1;
4426 if (aff->af_compflags != NULL)
4427 l += STRLEN(aff->af_compflags) + 1;
4428 p = getroom(spin, l, FALSE);
4429 if (p != NULL)
4430 {
4431 if (aff->af_compflags != NULL)
4432 {
4433 STRCPY(p, aff->af_compflags);
4434 STRCAT(p, "/");
4435 }
4436 STRCAT(p, items[1]);
4437 aff->af_compflags = p;
4438 }
4439 }
4440 else if (STRCMP(items[0], "COMPOUNDMAX") == 0 && itemcnt == 2
4441 && aff->af_compmax == 0)
4442 {
4443 aff->af_compmax = atoi((char *)items[1]);
4444 if (aff->af_compmax == 0)
4445 smsg((char_u *)_("Wrong COMPOUNDMAX value in %s line %d: %s"),
4446 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004447 }
4448 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
4449 && aff->af_compminlen == 0)
4450 {
4451 aff->af_compminlen = atoi((char *)items[1]);
4452 if (aff->af_compminlen == 0)
4453 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
4454 fname, lnum, items[1]);
4455 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004456 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
4457 && aff->af_compsylmax == 0)
4458 {
4459 aff->af_compsylmax = atoi((char *)items[1]);
4460 if (aff->af_compsylmax == 0)
4461 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
4462 fname, lnum, items[1]);
4463 }
4464 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
4465 && aff->af_syllable == NULL)
4466 {
4467 aff->af_syllable = getroom_save(spin, items[1]);
4468 }
Bram Moolenaar78622822005-08-23 21:00:13 +00004469 else if (STRCMP(items[0], "NOBREAK") == 0 && itemcnt == 1)
4470 {
4471 spin->si_nobreak = TRUE;
4472 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004473 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
4474 {
4475 aff->af_pfxpostpone = TRUE;
4476 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004477 else if ((STRCMP(items[0], "PFX") == 0
4478 || STRCMP(items[0], "SFX") == 0)
4479 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00004480 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004481 {
Bram Moolenaar8db73182005-06-17 21:51:16 +00004482 /* Myspell allows extra text after the item, but that might
4483 * mean mistakes go unnoticed. Require a comment-starter. */
4484 if (itemcnt > 4 && *items[4] != '#')
4485 smsg((char_u *)_("Trailing text in %s line %d: %s"),
4486 fname, lnum, items[4]);
4487
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004488 /* New affix letter. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004489 cur_aff = (affheader_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004490 sizeof(affheader_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004491 if (cur_aff == NULL)
4492 break;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004493#ifdef FEAT_MBYTE
4494 if (has_mbyte)
4495 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004496 l = (*mb_ptr2len)(items[1]);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004497 if (l >= AH_KEY_LEN)
4498 l = 1; /* too long, must be an overlong sequence */
4499 else
4500 mch_memmove(cur_aff->ah_key, items[1], l);
4501 }
4502 else
4503#endif
4504 {
4505 *cur_aff->ah_key = *items[1];
4506 l = 1;
4507 }
4508 cur_aff->ah_key[l] = NUL;
4509 if (items[1][l] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004510 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004511 if (*items[2] == 'Y')
4512 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004513 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004514 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
4515 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004516
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004517 if (*items[0] == 'P')
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004518 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004519 tp = &aff->af_pref;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004520 /* Use a new number in the .spl file later, to be able to
4521 * handle multiple .aff files. */
4522 if (aff->af_pfxpostpone)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004523 cur_aff->ah_newID = ++spin->si_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004524 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004525 else
4526 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004527 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004528 hi = hash_find(tp, cur_aff->ah_key);
4529 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar51485f02005-06-04 21:55:20 +00004530 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004531 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
4532 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004533 aff_todo = 0;
4534 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004535 else
4536 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004537 }
4538 else if ((STRCMP(items[0], "PFX") == 0
4539 || STRCMP(items[0], "SFX") == 0)
4540 && aff_todo > 0
4541 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00004542 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004543 {
4544 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004545 int rare = FALSE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004546 int nocomp = FALSE;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004547 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004548 int lasti = 5;
4549
Bram Moolenaar5195e452005-08-19 20:32:47 +00004550 /* Check for "rare" and "nocomp" after the other info. */
4551 while (itemcnt > lasti)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004552 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004553 if (!rare && STRICMP(items[lasti], "rare") == 0)
4554 {
4555 rare = TRUE;
4556 ++lasti;
4557 }
4558 else if (!nocomp && STRICMP(items[lasti], "nocomp") == 0)
4559 {
4560 nocomp = TRUE;
4561 ++lasti;
4562 }
4563 else
4564 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004565 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004566
Bram Moolenaar8db73182005-06-17 21:51:16 +00004567 /* Myspell allows extra text after the item, but that might
4568 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004569 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004570 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00004571
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004572 /* New item for an affix letter. */
4573 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004574 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004575 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004576 if (aff_entry == NULL)
4577 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004578 aff_entry->ae_rare = rare;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004579 aff_entry->ae_nocomp = nocomp;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004580
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004581 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004582 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004583 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004584 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004585
Bram Moolenaar51485f02005-06-04 21:55:20 +00004586 /* Don't use an affix entry with non-ASCII characters when
4587 * "spin->si_ascii" is TRUE. */
4588 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004589 || has_non_ascii(aff_entry->ae_add)))
4590 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00004591 aff_entry->ae_next = cur_aff->ah_first;
4592 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004593
4594 if (STRCMP(items[4], ".") != 0)
4595 {
4596 char_u buf[MAXLINELEN];
4597
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004598 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004599 if (*items[0] == 'P')
4600 sprintf((char *)buf, "^%s", items[4]);
4601 else
4602 sprintf((char *)buf, "%s$", items[4]);
4603 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004604 RE_MAGIC + RE_STRING + RE_STRICT);
4605 if (aff_entry->ae_prog == NULL)
4606 smsg((char_u *)_("Broken condition in %s line %d: %s"),
4607 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004608 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004609
4610 /* For postponed prefixes we need an entry in si_prefcond
4611 * for the condition. Use an existing one if possible. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004612 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004613 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004614 /* When the chop string is one lower-case letter and
4615 * the add string ends in the upper-case letter we set
4616 * the "upper" flag, clear "ae_chop" and remove the
4617 * letters from "ae_add". The condition must either
4618 * be empty or start with the same letter. */
4619 if (aff_entry->ae_chop != NULL
4620 && aff_entry->ae_add != NULL
4621#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004622 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00004623 aff_entry->ae_chop)] == NUL
4624#else
4625 && aff_entry->ae_chop[1] == NUL
4626#endif
4627 )
4628 {
4629 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004630
Bram Moolenaar53805d12005-08-01 07:08:33 +00004631 c = PTR2CHAR(aff_entry->ae_chop);
4632 c_up = SPELL_TOUPPER(c);
4633 if (c_up != c
4634 && (aff_entry->ae_cond == NULL
4635 || PTR2CHAR(aff_entry->ae_cond) == c))
4636 {
4637 p = aff_entry->ae_add
4638 + STRLEN(aff_entry->ae_add);
4639 mb_ptr_back(aff_entry->ae_add, p);
4640 if (PTR2CHAR(p) == c_up)
4641 {
4642 upper = TRUE;
4643 aff_entry->ae_chop = NULL;
4644 *p = NUL;
4645
4646 /* The condition is matched with the
4647 * actual word, thus must check for the
4648 * upper-case letter. */
4649 if (aff_entry->ae_cond != NULL)
4650 {
4651 char_u buf[MAXLINELEN];
4652#ifdef FEAT_MBYTE
4653 if (has_mbyte)
4654 {
4655 onecap_copy(items[4], buf, TRUE);
4656 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004657 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004658 }
4659 else
4660#endif
4661 *aff_entry->ae_cond = c_up;
4662 if (aff_entry->ae_cond != NULL)
4663 {
4664 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004665 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004666 vim_free(aff_entry->ae_prog);
4667 aff_entry->ae_prog = vim_regcomp(
4668 buf, RE_MAGIC + RE_STRING);
4669 }
4670 }
4671 }
4672 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004673 }
4674
Bram Moolenaar53805d12005-08-01 07:08:33 +00004675 if (aff_entry->ae_chop == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004676 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004677 int idx;
4678 char_u **pp;
4679 int n;
4680
4681 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
4682 --idx)
4683 {
4684 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
4685 if (str_equal(p, aff_entry->ae_cond))
4686 break;
4687 }
4688 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
4689 {
4690 /* Not found, add a new condition. */
4691 idx = spin->si_prefcond.ga_len++;
4692 pp = ((char_u **)spin->si_prefcond.ga_data)
4693 + idx;
4694 if (aff_entry->ae_cond == NULL)
4695 *pp = NULL;
4696 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004697 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004698 aff_entry->ae_cond);
4699 }
4700
4701 /* Add the prefix to the prefix tree. */
4702 if (aff_entry->ae_add == NULL)
4703 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004704 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00004705 p = aff_entry->ae_add;
4706 /* PFX_FLAGS is a negative number, so that
4707 * tree_add_word() knows this is the prefix tree. */
4708 n = PFX_FLAGS;
4709 if (rare)
4710 n |= WFP_RARE;
4711 if (!cur_aff->ah_combine)
4712 n |= WFP_NC;
4713 if (upper)
4714 n |= WFP_UP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004715 tree_add_word(spin, p, spin->si_prefroot, n,
4716 idx, cur_aff->ah_newID);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004717 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004718 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00004719 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004720 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004721 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
4722 {
4723 if (fol != NULL)
4724 smsg((char_u *)_("Duplicate FOL in %s line %d"),
4725 fname, lnum);
4726 else
4727 fol = vim_strsave(items[1]);
4728 }
4729 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
4730 {
4731 if (low != NULL)
4732 smsg((char_u *)_("Duplicate LOW in %s line %d"),
4733 fname, lnum);
4734 else
4735 low = vim_strsave(items[1]);
4736 }
4737 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
4738 {
4739 if (upp != NULL)
4740 smsg((char_u *)_("Duplicate UPP in %s line %d"),
4741 fname, lnum);
4742 else
4743 upp = vim_strsave(items[1]);
4744 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004745 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004746 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004747 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004748 if (!isdigit(*items[1]))
4749 smsg((char_u *)_("Expected REP count in %s line %d"),
4750 fname, lnum);
4751 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004752 else if (STRCMP(items[0], "REP") == 0 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004753 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004754 /* REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004755 /* Myspell ignores extra arguments, we require it starts with
4756 * # to detect mistakes. */
4757 if (itemcnt > 3 && items[3][0] != '#')
4758 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004759 if (do_rep)
4760 add_fromto(spin, &spin->si_rep, items[1], items[2]);
4761 }
4762 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
4763 {
4764 /* MAP item or count */
4765 if (!found_map)
4766 {
4767 /* First line contains the count. */
4768 found_map = TRUE;
4769 if (!isdigit(*items[1]))
4770 smsg((char_u *)_("Expected MAP count in %s line %d"),
4771 fname, lnum);
4772 }
4773 else if (do_map)
4774 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004775 int c;
4776
4777 /* Check that every character appears only once. */
4778 for (p = items[1]; *p != NUL; )
4779 {
4780#ifdef FEAT_MBYTE
4781 c = mb_ptr2char_adv(&p);
4782#else
4783 c = *p++;
4784#endif
4785 if ((spin->si_map.ga_len > 0
4786 && vim_strchr(spin->si_map.ga_data, c)
4787 != NULL)
4788 || vim_strchr(p, c) != NULL)
4789 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
4790 fname, lnum);
4791 }
4792
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004793 /* We simply concatenate all the MAP strings, separated by
4794 * slashes. */
4795 ga_concat(&spin->si_map, items[1]);
4796 ga_append(&spin->si_map, '/');
4797 }
4798 }
4799 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
4800 {
4801 if (do_sal)
4802 {
4803 /* SAL item (sounds-a-like)
4804 * Either one of the known keys or a from-to pair. */
4805 if (STRCMP(items[1], "followup") == 0)
4806 spin->si_followup = sal_to_bool(items[2]);
4807 else if (STRCMP(items[1], "collapse_result") == 0)
4808 spin->si_collapse = sal_to_bool(items[2]);
4809 else if (STRCMP(items[1], "remove_accents") == 0)
4810 spin->si_rem_accents = sal_to_bool(items[2]);
4811 else
4812 /* when "to" is "_" it means empty */
4813 add_fromto(spin, &spin->si_sal, items[1],
4814 STRCMP(items[2], "_") == 0 ? (char_u *)""
4815 : items[2]);
4816 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004817 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004818 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
4819 && (!do_sofo || spin->si_sofofr == NULL))
4820 {
4821 if (do_sofo)
4822 spin->si_sofofr = vim_strsave(items[1]);
4823 }
4824 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
4825 && (!do_sofo || spin->si_sofoto == NULL))
4826 {
4827 if (do_sofo)
4828 spin->si_sofoto = vim_strsave(items[1]);
4829 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004830 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004831 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004832 fname, lnum, items[0]);
4833 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004834 }
4835
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004836 if (do_sofo && (spin->si_sofofr == NULL) != (spin->si_sofoto == NULL))
4837 smsg((char_u *)_("Missing SOFO%s line in %s"),
4838 spin->si_sofofr == NULL ? "FROM" : "TO", fname);
4839 if (spin->si_sofofr != NULL && spin->si_sal.ga_len > 0)
4840 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
4841
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004842 if (fol != NULL || low != NULL || upp != NULL)
4843 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004844 if (spin->si_clear_chartab)
4845 {
4846 /* Clear the char type tables, don't want to use any of the
4847 * currently used spell properties. */
4848 init_spell_chartab();
4849 spin->si_clear_chartab = FALSE;
4850 }
4851
Bram Moolenaar3982c542005-06-08 21:56:31 +00004852 /*
4853 * Don't write a word table for an ASCII file, so that we don't check
4854 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004855 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00004856 * mb_get_class(), the list of chars in the file will be incomplete.
4857 */
4858 if (!spin->si_ascii
4859#ifdef FEAT_MBYTE
4860 && !enc_utf8
4861#endif
4862 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004863 {
4864 if (fol == NULL || low == NULL || upp == NULL)
4865 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
4866 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00004867 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004868 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004869
4870 vim_free(fol);
4871 vim_free(low);
4872 vim_free(upp);
4873 }
4874
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004875 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004876 if (aff->af_compmax != 0)
4877 {
4878 if (spin->si_compmax != 0 && spin->si_compmax != aff->af_compmax)
4879 smsg((char_u *)_("COMPOUNDMAX value differs from what is used in another .aff file"));
4880 else
4881 spin->si_compmax = aff->af_compmax;
4882 }
4883
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004884 if (aff->af_compminlen != 0)
4885 {
4886 if (spin->si_compminlen != 0
4887 && spin->si_compminlen != aff->af_compminlen)
4888 smsg((char_u *)_("COMPOUNDMIN value differs from what is used in another .aff file"));
4889 else
4890 spin->si_compminlen = aff->af_compminlen;
4891 }
4892
Bram Moolenaar5195e452005-08-19 20:32:47 +00004893 if (aff->af_compsylmax != 0)
4894 {
4895 if (aff->af_syllable == NULL)
4896 smsg((char_u *)_("COMPOUNDSYLMAX without SYLLABLE"));
4897
4898 if (spin->si_compsylmax != 0
4899 && spin->si_compsylmax != aff->af_compsylmax)
4900 smsg((char_u *)_("COMPOUNDSYLMAX value differs from what is used in another .aff file"));
4901 else
4902 spin->si_compsylmax = aff->af_compsylmax;
4903 }
4904
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004905 if (aff->af_compflags != NULL)
4906 {
4907 if (spin->si_compflags != NULL
4908 && STRCMP(spin->si_compflags, aff->af_compflags) != 0)
4909 smsg((char_u *)_("COMPOUNDFLAG(S) value differs from what is used in another .aff file"));
4910 else
4911 spin->si_compflags = aff->af_compflags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004912 }
4913
Bram Moolenaar5195e452005-08-19 20:32:47 +00004914 if (aff->af_syllable != NULL)
4915 {
4916 if (spin->si_syllable != NULL
4917 && STRCMP(spin->si_syllable, aff->af_syllable) != 0)
4918 smsg((char_u *)_("SYLLABLE value differs from what is used in another .aff file"));
4919 else
4920 spin->si_syllable = aff->af_syllable;
4921 }
4922
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004923 vim_free(pc);
4924 fclose(fd);
4925 return aff;
4926}
4927
4928/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004929 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
4930 * NULL as equal.
4931 */
4932 static int
4933str_equal(s1, s2)
4934 char_u *s1;
4935 char_u *s2;
4936{
4937 if (s1 == NULL || s2 == NULL)
4938 return s1 == s2;
4939 return STRCMP(s1, s2) == 0;
4940}
4941
4942/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004943 * Add a from-to item to "gap". Used for REP and SAL items.
4944 * They are stored case-folded.
4945 */
4946 static void
4947add_fromto(spin, gap, from, to)
4948 spellinfo_T *spin;
4949 garray_T *gap;
4950 char_u *from;
4951 char_u *to;
4952{
4953 fromto_T *ftp;
4954 char_u word[MAXWLEN];
4955
4956 if (ga_grow(gap, 1) == OK)
4957 {
4958 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
4959 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004960 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004961 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004962 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004963 ++gap->ga_len;
4964 }
4965}
4966
4967/*
4968 * Convert a boolean argument in a SAL line to TRUE or FALSE;
4969 */
4970 static int
4971sal_to_bool(s)
4972 char_u *s;
4973{
4974 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
4975}
4976
4977/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00004978 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
4979 * When "s" is NULL FALSE is returned.
4980 */
4981 static int
4982has_non_ascii(s)
4983 char_u *s;
4984{
4985 char_u *p;
4986
4987 if (s != NULL)
4988 for (p = s; *p != NUL; ++p)
4989 if (*p >= 128)
4990 return TRUE;
4991 return FALSE;
4992}
4993
4994/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004995 * Free the structure filled by spell_read_aff().
4996 */
4997 static void
4998spell_free_aff(aff)
4999 afffile_T *aff;
5000{
5001 hashtab_T *ht;
5002 hashitem_T *hi;
5003 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005004 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005005 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005006
5007 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005008
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005009 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005010 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
5011 {
5012 todo = ht->ht_used;
5013 for (hi = ht->ht_array; todo > 0; ++hi)
5014 {
5015 if (!HASHITEM_EMPTY(hi))
5016 {
5017 --todo;
5018 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005019 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
5020 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005021 }
5022 }
5023 if (ht == &aff->af_suff)
5024 break;
5025 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005026
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005027 hash_clear(&aff->af_pref);
5028 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005029}
5030
5031/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005032 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005033 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005034 */
5035 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005036spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005037 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005038 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005039 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005040{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005041 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005042 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005043 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005044 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005045 char_u store_afflist[MAXWLEN];
5046 int pfxlen;
5047 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005048 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005049 char_u *pc;
5050 char_u *w;
5051 int l;
5052 hash_T hash;
5053 hashitem_T *hi;
5054 FILE *fd;
5055 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005056 int non_ascii = 0;
5057 int retval = OK;
5058 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005059 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005060 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005061
Bram Moolenaar51485f02005-06-04 21:55:20 +00005062 /*
5063 * Open the file.
5064 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005065 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005066 if (fd == NULL)
5067 {
5068 EMSG2(_(e_notopen), fname);
5069 return FAIL;
5070 }
5071
Bram Moolenaar51485f02005-06-04 21:55:20 +00005072 /* The hashtable is only used to detect duplicated words. */
5073 hash_init(&ht);
5074
Bram Moolenaarb765d632005-06-07 21:00:02 +00005075 if (spin->si_verbose || p_verbose > 2)
5076 {
5077 if (!spin->si_verbose)
5078 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005079 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005080 out_flush();
5081 if (!spin->si_verbose)
5082 verbose_leave();
5083 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005084
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005085 /* start with a message for the first line */
5086 spin->si_msg_count = 999999;
5087
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005088 /* Read and ignore the first line: word count. */
5089 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005090 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005091 EMSG2(_("E760: No word count in %s"), fname);
5092
5093 /*
5094 * Read all the lines in the file one by one.
5095 * The words are converted to 'encoding' here, before being added to
5096 * the hashtable.
5097 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005098 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005099 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005100 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005101 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005102 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005103 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005104
Bram Moolenaar51485f02005-06-04 21:55:20 +00005105 /* Remove CR, LF and white space from the end. White space halfway
5106 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005107 l = STRLEN(line);
5108 while (l > 0 && line[l - 1] <= ' ')
5109 --l;
5110 if (l == 0)
5111 continue; /* empty line */
5112 line[l] = NUL;
5113
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005114 /* Find the optional affix names. Replace the SLASH character by a
5115 * slash. */
5116 afflist = NULL;
5117 for (p = line; *p != NUL; mb_ptr_adv(p))
5118 {
5119 if (*p == affile->af_slash)
5120 *p = '/';
5121 else if (*p == '/')
5122 {
5123 *p = NUL;
5124 afflist = p + 1;
5125 break;
5126 }
5127 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005128
5129 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
5130 if (spin->si_ascii && has_non_ascii(line))
5131 {
5132 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005133 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005134 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005135
Bram Moolenaarb765d632005-06-07 21:00:02 +00005136#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005137 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005138 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005139 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005140 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005141 if (pc == NULL)
5142 {
5143 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5144 fname, lnum, line);
5145 continue;
5146 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005147 w = pc;
5148 }
5149 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005150#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005151 {
5152 pc = NULL;
5153 w = line;
5154 }
5155
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005156 /* This takes time, print a message every 10000 words. */
5157 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005158 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005159 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005160 vim_snprintf((char *)message, sizeof(message),
5161 _("line %6d, word %6d - %s"),
5162 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
5163 msg_start();
5164 msg_puts_long_attr(message, 0);
5165 msg_clr_eos();
5166 msg_didout = FALSE;
5167 msg_col = 0;
5168 out_flush();
5169 }
5170
Bram Moolenaar51485f02005-06-04 21:55:20 +00005171 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005172 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005173 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005174 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005175 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005176 if (retval == FAIL)
5177 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005178
Bram Moolenaar51485f02005-06-04 21:55:20 +00005179 hash = hash_hash(dw);
5180 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005181 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005182 {
5183 if (p_verbose > 0)
5184 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005185 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005186 else if (duplicate == 0)
5187 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
5188 fname, lnum, dw);
5189 ++duplicate;
5190 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005191 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00005192 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005193
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005194 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005195 store_afflist[0] = NUL;
5196 pfxlen = 0;
5197 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005198 if (afflist != NULL)
5199 {
5200 /* Check for affix name that stands for keep-case word and stands
5201 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005202 if (affile->af_kep != NUL
5203 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005204 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005205 if (affile->af_rar != NUL
5206 && vim_strchr(afflist, affile->af_rar) != NULL)
5207 flags |= WF_RARE;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005208 if (affile->af_bad != NUL
5209 && vim_strchr(afflist, affile->af_bad) != NULL)
5210 flags |= WF_BANNED;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005211 if (affile->af_needaffix != NUL
5212 && vim_strchr(afflist, affile->af_needaffix) != NULL)
5213 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005214
5215 if (affile->af_pfxpostpone)
5216 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005217 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005218
Bram Moolenaar5195e452005-08-19 20:32:47 +00005219 if (spin->si_compflags != NULL)
5220 /* Need to store the list of compound flags with the word.
5221 * Concatenate them to the list of prefix IDs. */
5222 get_compflags(spin, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005223 }
5224
Bram Moolenaar51485f02005-06-04 21:55:20 +00005225 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005226 if (store_word(spin, dw, flags, spin->si_region,
5227 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005228 retval = FAIL;
5229
5230 if (afflist != NULL)
5231 {
5232 /* Find all matching suffixes and add the resulting words.
5233 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005234 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005235 &affile->af_suff, &affile->af_pref,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005236 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005237 retval = FAIL;
5238
5239 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005240 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005241 &affile->af_pref, NULL,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005242 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005243 retval = FAIL;
5244 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005245 }
5246
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005247 if (duplicate > 0)
5248 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005249 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005250 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
5251 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005252 hash_clear(&ht);
5253
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005254 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005255 return retval;
5256}
5257
5258/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005259 * Get the list of prefix IDs from the affix list "afflist".
5260 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00005261 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
5262 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005263 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005264 static int
5265get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005266 afffile_T *affile;
5267 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005268 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005269{
5270 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005271 int cnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005272 char_u key[2];
5273 hashitem_T *hi;
5274
5275 key[1] = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005276 for (p = afflist; *p != NUL; ++p)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005277 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00005278 key[0] = *p;
5279 hi = hash_find(&affile->af_pref, key);
5280 if (!HASHITEM_EMPTY(hi))
5281 /* This is a prefix ID, use the new number. */
5282 store_afflist[cnt++] = HI2AH(hi)->ah_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005283 }
5284
Bram Moolenaar5195e452005-08-19 20:32:47 +00005285 store_afflist[cnt] = NUL;
5286 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005287}
5288
5289/*
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005290 * Get the list of affix IDs from the affix list "afflist" that are used for
5291 * compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00005292 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005293 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005294 static void
5295get_compflags(spin, afflist, store_afflist)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005296 spellinfo_T *spin;
5297 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005298 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005299{
5300 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005301 int cnt = 0;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005302
Bram Moolenaar5195e452005-08-19 20:32:47 +00005303 for (p = afflist; *p != NUL; ++p)
5304 /* A flag is a compound flag if it appears in "si_compflags" and
5305 * it's not a special character. */
5306 if (vim_strchr(spin->si_compflags, *p) != NULL
5307 && vim_strchr((char_u *)"+*[]/", *p) == NULL)
5308 store_afflist[cnt++] = *p;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005309
Bram Moolenaar5195e452005-08-19 20:32:47 +00005310 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005311}
5312
5313/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005314 * Apply affixes to a word and store the resulting words.
5315 * "ht" is the hashtable with affentry_T that need to be applied, either
5316 * prefixes or suffixes.
5317 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
5318 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005319 *
5320 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005321 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005322 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00005323store_aff_word(spin, word, afflist, affile, ht, xht, comb, flags,
5324 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005325 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005326 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005327 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005328 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005329 hashtab_T *ht;
5330 hashtab_T *xht;
5331 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005332 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005333 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005334 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
5335 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005336{
5337 int todo;
5338 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005339 affheader_T *ah;
5340 affentry_T *ae;
5341 regmatch_T regmatch;
5342 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005343 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005344 int i;
5345 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005346 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005347 char_u *use_pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005348 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +00005349 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005350 size_t wordlen = STRLEN(word);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005351
Bram Moolenaar51485f02005-06-04 21:55:20 +00005352 todo = ht->ht_used;
5353 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005354 {
5355 if (!HASHITEM_EMPTY(hi))
5356 {
5357 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005358 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00005359
Bram Moolenaar51485f02005-06-04 21:55:20 +00005360 /* Check that the affix combines, if required, and that the word
5361 * supports this affix. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005362 c = PTR2CHAR(ah->ah_key);
5363 if ((!comb || ah->ah_combine) && vim_strchr(afflist, c) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005364 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005365 /* Loop over all affix entries with this name. */
5366 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005367 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005368 /* Check the condition. It's not logical to match case
5369 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005370 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005371 * Another requirement from Myspell is that the chop
5372 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005373 * For prefixes, when "PFXPOSTPONE" was used, only do
5374 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005375 regmatch.regprog = ae->ae_prog;
5376 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005377 if ((xht != NULL || !affile->af_pfxpostpone
5378 || ae->ae_chop != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005379 && (ae->ae_chop == NULL
5380 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005381 && (ae->ae_prog == NULL
5382 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005383 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005384 /* Match. Remove the chop and add the affix. */
5385 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005386 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005387 /* prefix: chop/add at the start of the word */
5388 if (ae->ae_add == NULL)
5389 *newword = NUL;
5390 else
5391 STRCPY(newword, ae->ae_add);
5392 p = word;
5393 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005394 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005395 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005396#ifdef FEAT_MBYTE
5397 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005398 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005399 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005400 for ( ; i > 0; --i)
5401 mb_ptr_adv(p);
5402 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005403 else
5404#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005405 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005406 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005407 STRCAT(newword, p);
5408 }
5409 else
5410 {
5411 /* suffix: chop/add at the end of the word */
5412 STRCPY(newword, word);
5413 if (ae->ae_chop != NULL)
5414 {
5415 /* Remove chop string. */
5416 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005417 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005418 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005419 mb_ptr_back(newword, p);
5420 *p = NUL;
5421 }
5422 if (ae->ae_add != NULL)
5423 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005424 }
5425
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005426 /* Obey the "rare" flag of the affix. */
5427 if (ae->ae_rare)
5428 use_flags = flags | WF_RARE;
5429 else
5430 use_flags = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005431
5432 /* Obey the "nocomp" flag of the affix: don't use the
5433 * compound flags. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005434 use_pfxlist = pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005435 if (ae->ae_nocomp && pfxlist != NULL)
5436 {
5437 vim_strncpy(pfx_pfxlist, pfxlist, pfxlen);
5438 use_pfxlist = pfx_pfxlist;
5439 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005440
5441 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00005442 if (spin->si_prefroot != NULL
5443 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005444 {
5445 /* ... add a flag to indicate an affix was used. */
5446 use_flags |= WF_HAS_AFF;
5447
5448 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00005449 * affixes is not allowed. But do use the
5450 * compound flags after them. */
5451 if ((!ah->ah_combine || comb) && pfxlist != NULL)
5452 use_pfxlist += pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005453 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005454
Bram Moolenaar51485f02005-06-04 21:55:20 +00005455 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005456 if (store_word(spin, newword, use_flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005457 spin->si_region, use_pfxlist, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005458 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005459
Bram Moolenaar51485f02005-06-04 21:55:20 +00005460 /* When added a suffix and combining is allowed also
5461 * try adding prefixes additionally. */
5462 if (xht != NULL && ah->ah_combine)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005463 if (store_aff_word(spin, newword, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005464 xht, NULL, TRUE,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005465 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005466 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005467 }
5468 }
5469 }
5470 }
5471 }
5472
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005473 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005474}
5475
5476/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005477 * Read a file with a list of words.
5478 */
5479 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005480spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005481 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005482 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005483{
5484 FILE *fd;
5485 long lnum = 0;
5486 char_u rline[MAXLINELEN];
5487 char_u *line;
5488 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005489 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005490 int l;
5491 int retval = OK;
5492 int did_word = FALSE;
5493 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005494 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00005495 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005496
5497 /*
5498 * Open the file.
5499 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005500 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00005501 if (fd == NULL)
5502 {
5503 EMSG2(_(e_notopen), fname);
5504 return FAIL;
5505 }
5506
Bram Moolenaarb765d632005-06-07 21:00:02 +00005507 if (spin->si_verbose || p_verbose > 2)
5508 {
5509 if (!spin->si_verbose)
5510 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005511 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005512 out_flush();
5513 if (!spin->si_verbose)
5514 verbose_leave();
5515 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005516
5517 /*
5518 * Read all the lines in the file one by one.
5519 */
5520 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
5521 {
5522 line_breakcheck();
5523 ++lnum;
5524
5525 /* Skip comment lines. */
5526 if (*rline == '#')
5527 continue;
5528
5529 /* Remove CR, LF and white space from the end. */
5530 l = STRLEN(rline);
5531 while (l > 0 && rline[l - 1] <= ' ')
5532 --l;
5533 if (l == 0)
5534 continue; /* empty or blank line */
5535 rline[l] = NUL;
5536
5537 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
5538 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005539#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005540 if (spin->si_conv.vc_type != CONV_NONE)
5541 {
5542 pc = string_convert(&spin->si_conv, rline, NULL);
5543 if (pc == NULL)
5544 {
5545 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5546 fname, lnum, rline);
5547 continue;
5548 }
5549 line = pc;
5550 }
5551 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005552#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00005553 {
5554 pc = NULL;
5555 line = rline;
5556 }
5557
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005558 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00005559 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005560 ++line;
5561 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005562 {
5563 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00005564 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
5565 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005566 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00005567 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
5568 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005569 else
5570 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005571#ifdef FEAT_MBYTE
5572 char_u *enc;
5573
Bram Moolenaar51485f02005-06-04 21:55:20 +00005574 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00005575 line += 10;
5576 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005577 if (enc != NULL && !spin->si_ascii
5578 && convert_setup(&spin->si_conv, enc,
5579 p_enc) == FAIL)
5580 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00005581 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005582 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005583 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005584#else
5585 smsg((char_u *)_("Conversion in %s not supported"), fname);
5586#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00005587 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005588 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005589 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005590
Bram Moolenaar3982c542005-06-08 21:56:31 +00005591 if (STRNCMP(line, "regions=", 8) == 0)
5592 {
5593 if (spin->si_region_count > 1)
5594 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
5595 fname, lnum, line);
5596 else
5597 {
5598 line += 8;
5599 if (STRLEN(line) > 16)
5600 smsg((char_u *)_("Too many regions in %s line %d: %s"),
5601 fname, lnum, line);
5602 else
5603 {
5604 spin->si_region_count = STRLEN(line) / 2;
5605 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005606
5607 /* Adjust the mask for a word valid in all regions. */
5608 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00005609 }
5610 }
5611 continue;
5612 }
5613
Bram Moolenaar7887d882005-07-01 22:33:52 +00005614 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
5615 fname, lnum, line - 1);
5616 continue;
5617 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005618
Bram Moolenaar7887d882005-07-01 22:33:52 +00005619 flags = 0;
5620 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005621
Bram Moolenaar7887d882005-07-01 22:33:52 +00005622 /* Check for flags and region after a slash. */
5623 p = vim_strchr(line, '/');
5624 if (p != NULL)
5625 {
5626 *p++ = NUL;
5627 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00005628 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005629 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005630 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005631 else if (*p == '!') /* Bad, bad, wicked word. */
5632 flags |= WF_BANNED;
5633 else if (*p == '?') /* Rare word. */
5634 flags |= WF_RARE;
5635 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00005636 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005637 if ((flags & WF_REGION) == 0) /* first one */
5638 regionmask = 0;
5639 flags |= WF_REGION;
5640
5641 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00005642 if (l > spin->si_region_count)
5643 {
5644 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00005645 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00005646 break;
5647 }
5648 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00005649 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005650 else
5651 {
5652 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
5653 fname, lnum, p);
5654 break;
5655 }
5656 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005657 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005658 }
5659
5660 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
5661 if (spin->si_ascii && has_non_ascii(line))
5662 {
5663 ++non_ascii;
5664 continue;
5665 }
5666
5667 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005668 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005669 {
5670 retval = FAIL;
5671 break;
5672 }
5673 did_word = TRUE;
5674 }
5675
5676 vim_free(pc);
5677 fclose(fd);
5678
Bram Moolenaarb765d632005-06-07 21:00:02 +00005679 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
5680 {
5681 if (p_verbose > 2)
5682 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00005683 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
5684 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005685 if (p_verbose > 2)
5686 verbose_leave();
5687 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005688 return retval;
5689}
5690
5691/*
5692 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005693 * This avoids calling free() for every little struct we use (and keeping
5694 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00005695 * The memory is cleared to all zeros.
5696 * Returns NULL when out of memory.
5697 */
5698 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005699getroom(spin, len, align)
5700 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005701 size_t len; /* length needed */
5702 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005703{
5704 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005705 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005706
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005707 if (align && bl != NULL)
5708 /* Round size up for alignment. On some systems structures need to be
5709 * aligned to the size of a pointer (e.g., SPARC). */
5710 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
5711 & ~(sizeof(char *) - 1);
5712
Bram Moolenaar51485f02005-06-04 21:55:20 +00005713 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
5714 {
5715 /* Allocate a block of memory. This is not freed until much later. */
5716 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
5717 if (bl == NULL)
5718 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005719 bl->sb_next = spin->si_blocks;
5720 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005721 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005722 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005723 }
5724
5725 p = bl->sb_data + bl->sb_used;
5726 bl->sb_used += len;
5727
5728 return p;
5729}
5730
5731/*
5732 * Make a copy of a string into memory allocated with getroom().
5733 */
5734 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005735getroom_save(spin, s)
5736 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005737 char_u *s;
5738{
5739 char_u *sc;
5740
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005741 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005742 if (sc != NULL)
5743 STRCPY(sc, s);
5744 return sc;
5745}
5746
5747
5748/*
5749 * Free the list of allocated sblock_T.
5750 */
5751 static void
5752free_blocks(bl)
5753 sblock_T *bl;
5754{
5755 sblock_T *next;
5756
5757 while (bl != NULL)
5758 {
5759 next = bl->sb_next;
5760 vim_free(bl);
5761 bl = next;
5762 }
5763}
5764
5765/*
5766 * Allocate the root of a word tree.
5767 */
5768 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005769wordtree_alloc(spin)
5770 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005771{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005772 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005773}
5774
5775/*
5776 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005777 * Always store it in the case-folded tree. For a keep-case word this is
5778 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
5779 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00005780 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005781 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
5782 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00005783 */
5784 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00005785store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005786 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005787 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005788 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00005789 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005790 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005791 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005792{
5793 int len = STRLEN(word);
5794 int ct = captype(word, word + len);
5795 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005796 int res = OK;
5797 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005798
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005799 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005800 for (p = pfxlist; res == OK; ++p)
5801 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00005802 if (!need_affix || (p != NULL && *p != NUL))
5803 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005804 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005805 if (p == NULL || *p == NUL)
5806 break;
5807 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00005808 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005809
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005810 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00005811 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005812 for (p = pfxlist; res == OK; ++p)
5813 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00005814 if (!need_affix || (p != NULL && *p != NUL))
5815 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005816 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005817 if (p == NULL || *p == NUL)
5818 break;
5819 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00005820 ++spin->si_keepwcount;
5821 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005822 return res;
5823}
5824
5825/*
5826 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005827 * When "flags" < 0 we are adding to the prefix tree where flags is used for
5828 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005829 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005830 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005831 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005832tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005833 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005834 char_u *word;
5835 wordnode_T *root;
5836 int flags;
5837 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005838 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005839{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005840 wordnode_T *node = root;
5841 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005842 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005843 wordnode_T **prev = NULL;
5844 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005845
Bram Moolenaar51485f02005-06-04 21:55:20 +00005846 /* Add each byte of the word to the tree, including the NUL at the end. */
5847 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005848 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005849 /* When there is more than one reference to this node we need to make
5850 * a copy, so that we can modify it. Copy the whole list of siblings
5851 * (we don't optimize for a partly shared list of siblings). */
5852 if (node != NULL && node->wn_refs > 1)
5853 {
5854 --node->wn_refs;
5855 copyprev = prev;
5856 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
5857 {
5858 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005859 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005860 if (np == NULL)
5861 return FAIL;
5862 np->wn_child = copyp->wn_child;
5863 if (np->wn_child != NULL)
5864 ++np->wn_child->wn_refs; /* child gets extra ref */
5865 np->wn_byte = copyp->wn_byte;
5866 if (np->wn_byte == NUL)
5867 {
5868 np->wn_flags = copyp->wn_flags;
5869 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005870 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005871 }
5872
5873 /* Link the new node in the list, there will be one ref. */
5874 np->wn_refs = 1;
5875 *copyprev = np;
5876 copyprev = &np->wn_sibling;
5877
5878 /* Let "node" point to the head of the copied list. */
5879 if (copyp == node)
5880 node = np;
5881 }
5882 }
5883
Bram Moolenaar51485f02005-06-04 21:55:20 +00005884 /* Look for the sibling that has the same character. They are sorted
5885 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005886 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005887 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005888 while (node != NULL
5889 && (node->wn_byte < word[i]
5890 || (node->wn_byte == NUL
5891 && (flags < 0
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005892 ? node->wn_affixID < affixID
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005893 : node->wn_flags < (flags & WN_MASK)
5894 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005895 && node->wn_affixID < affixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005896 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005897 prev = &node->wn_sibling;
5898 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005899 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005900 if (node == NULL
5901 || node->wn_byte != word[i]
5902 || (word[i] == NUL
5903 && (flags < 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005904 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005905 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005906 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005907 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005908 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005909 if (np == NULL)
5910 return FAIL;
5911 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005912
5913 /* If "node" is NULL this is a new child or the end of the sibling
5914 * list: ref count is one. Otherwise use ref count of sibling and
5915 * make ref count of sibling one (matters when inserting in front
5916 * of the list of siblings). */
5917 if (node == NULL)
5918 np->wn_refs = 1;
5919 else
5920 {
5921 np->wn_refs = node->wn_refs;
5922 node->wn_refs = 1;
5923 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005924 *prev = np;
5925 np->wn_sibling = node;
5926 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005927 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005928
Bram Moolenaar51485f02005-06-04 21:55:20 +00005929 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005930 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005931 node->wn_flags = flags;
5932 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005933 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005934 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00005935 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005936 prev = &node->wn_child;
5937 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005938 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005939#ifdef SPELL_PRINTTREE
5940 smsg("Added \"%s\"", word);
5941 spell_print_tree(root->wn_sibling);
5942#endif
5943
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005944 /* count nr of words added since last message */
5945 ++spin->si_msg_count;
5946
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005947 if (spin->si_compress_cnt > 1)
5948 {
5949 if (--spin->si_compress_cnt == 1)
5950 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005951 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005952 }
5953
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005954 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005955 * When we have allocated lots of memory we need to compress the word tree
5956 * to free up some room. But compression is slow, and we might actually
5957 * need that room, thus only compress in the following situations:
5958 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00005959 * "compress_start" blocks.
5960 * 2. When compressed before and used "compress_inc" blocks before
5961 * adding "compress_added" words (si_compress_cnt > 1).
5962 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005963 * (si_compress_cnt == 1) and the number of free nodes drops below the
5964 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005965 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005966#ifndef SPELL_PRINTTREE
5967 if (spin->si_compress_cnt == 1
5968 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00005969 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005970#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005971 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005972 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00005973 * when the freed up room has been used and another "compress_inc"
5974 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005975 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005976 spin->si_blocks_cnt -= compress_inc;
5977 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005978
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005979 if (spin->si_verbose)
5980 {
5981 msg_start();
5982 msg_puts((char_u *)_(msg_compressing));
5983 msg_clr_eos();
5984 msg_didout = FALSE;
5985 msg_col = 0;
5986 out_flush();
5987 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005988
5989 /* Compress both trees. Either they both have many nodes, which makes
5990 * compression useful, or one of them is small, which means
5991 * compression goes fast. */
5992 wordtree_compress(spin, spin->si_foldroot);
5993 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005994 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005995
5996 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005997}
5998
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005999/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00006000 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
6001 * Sets "sps_flags".
6002 */
6003 int
6004spell_check_msm()
6005{
6006 char_u *p = p_msm;
6007 long start = 0;
6008 long inc = 0;
6009 long added = 0;
6010
6011 if (!VIM_ISDIGIT(*p))
6012 return FAIL;
6013 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
6014 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
6015 if (*p != ',')
6016 return FAIL;
6017 ++p;
6018 if (!VIM_ISDIGIT(*p))
6019 return FAIL;
6020 inc = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
6021 if (*p != ',')
6022 return FAIL;
6023 ++p;
6024 if (!VIM_ISDIGIT(*p))
6025 return FAIL;
6026 added = getdigits(&p) * 1024;
6027 if (*p != NUL)
6028 return FAIL;
6029
6030 if (start == 0 || inc == 0 || added == 0 || inc > start)
6031 return FAIL;
6032
6033 compress_start = start;
6034 compress_inc = inc;
6035 compress_added = added;
6036 return OK;
6037}
6038
6039
6040/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006041 * Get a wordnode_T, either from the list of previously freed nodes or
6042 * allocate a new one.
6043 */
6044 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006045get_wordnode(spin)
6046 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006047{
6048 wordnode_T *n;
6049
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006050 if (spin->si_first_free == NULL)
6051 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006052 else
6053 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006054 n = spin->si_first_free;
6055 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006056 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006057 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006058 }
6059#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006060 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006061#endif
6062 return n;
6063}
6064
6065/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006066 * Decrement the reference count on a node (which is the head of a list of
6067 * siblings). If the reference count becomes zero free the node and its
6068 * siblings.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006069 */
6070 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006071deref_wordnode(spin, node)
6072 spellinfo_T *spin;
6073 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006074{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006075 wordnode_T *np;
6076
6077 if (--node->wn_refs == 0)
6078 for (np = node; np != NULL; np = np->wn_sibling)
6079 {
6080 if (np->wn_child != NULL)
6081 deref_wordnode(spin, np->wn_child);
6082 free_wordnode(spin, np);
6083 }
6084}
6085
6086/*
6087 * Free a wordnode_T for re-use later.
6088 * Only the "wn_child" field becomes invalid.
6089 */
6090 static void
6091free_wordnode(spin, n)
6092 spellinfo_T *spin;
6093 wordnode_T *n;
6094{
6095 n->wn_child = spin->si_first_free;
6096 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006097 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006098}
6099
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006100/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006101 * Compress a tree: find tails that are identical and can be shared.
6102 */
6103 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006104wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006105 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006106 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006107{
6108 hashtab_T ht;
6109 int n;
6110 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006111 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006112
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006113 /* Skip the root itself, it's not actually used. The first sibling is the
6114 * start of the tree. */
6115 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006116 {
6117 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006118 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006119
6120#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00006121 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006122#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00006123 {
6124 if (!spin->si_verbose)
6125 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006126 if (tot > 1000000)
6127 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006128 else if (tot == 0)
6129 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006130 else
6131 perc = (tot - n) * 100 / tot;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006132 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006133 n, tot, perc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006134 if (p_verbose > 2)
6135 verbose_leave();
6136 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006137#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006138 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006139#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006140 hash_clear(&ht);
6141 }
6142}
6143
6144/*
6145 * Compress a node, its siblings and its children, depth first.
6146 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006147 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006148 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006149node_compress(spin, node, ht, tot)
6150 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006151 wordnode_T *node;
6152 hashtab_T *ht;
6153 int *tot; /* total count of nodes before compressing,
6154 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006155{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006156 wordnode_T *np;
6157 wordnode_T *tp;
6158 wordnode_T *child;
6159 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006160 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006161 int len = 0;
6162 unsigned nr, n;
6163 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006164
Bram Moolenaar51485f02005-06-04 21:55:20 +00006165 /*
6166 * Go through the list of siblings. Compress each child and then try
6167 * finding an identical child to replace it.
6168 * Note that with "child" we mean not just the node that is pointed to,
6169 * but the whole list of siblings, of which the node is the first.
6170 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006171 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006172 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006173 ++len;
6174 if ((child = np->wn_child) != NULL)
6175 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006176 /* Compress the child. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006177 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006178
6179 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006180 hash = hash_hash(child->wn_u1.hashkey);
6181 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006182 tp = NULL;
6183 if (!HASHITEM_EMPTY(hi))
6184 {
6185 /* There are children with an identical hash value. Now check
6186 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006187 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006188 if (node_equal(child, tp))
6189 {
6190 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006191 * current one. This means the current child and all
6192 * its siblings is unlinked from the tree. */
6193 ++tp->wn_refs;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006194 deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006195 np->wn_child = tp;
6196 ++compressed;
6197 break;
6198 }
6199 if (tp == NULL)
6200 {
6201 /* No other child with this hash value equals the child of
6202 * the node, add it to the linked list after the first
6203 * item. */
6204 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006205 child->wn_u2.next = tp->wn_u2.next;
6206 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006207 }
6208 }
6209 else
6210 /* No other child has this hash value, add it to the
6211 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006212 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006213 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006214 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006215 *tot += len;
6216
6217 /*
6218 * Make a hash key for the node and its siblings, so that we can quickly
6219 * find a lookalike node. This must be done after compressing the sibling
6220 * list, otherwise the hash key would become invalid by the compression.
6221 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006222 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006223 nr = 0;
6224 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006225 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006226 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006227 /* end node: use wn_flags, wn_region and wn_affixID */
6228 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006229 else
6230 /* byte node: use the byte value and the child pointer */
6231 n = np->wn_byte + ((long_u)np->wn_child << 8);
6232 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006233 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006234
6235 /* Avoid NUL bytes, it terminates the hash key. */
6236 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006237 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006238 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006239 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006240 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006241 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006242 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006243 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
6244 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006245
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006246 /* Check for CTRL-C pressed now and then. */
6247 fast_breakcheck();
6248
Bram Moolenaar51485f02005-06-04 21:55:20 +00006249 return compressed;
6250}
6251
6252/*
6253 * Return TRUE when two nodes have identical siblings and children.
6254 */
6255 static int
6256node_equal(n1, n2)
6257 wordnode_T *n1;
6258 wordnode_T *n2;
6259{
6260 wordnode_T *p1;
6261 wordnode_T *p2;
6262
6263 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
6264 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
6265 if (p1->wn_byte != p2->wn_byte
6266 || (p1->wn_byte == NUL
6267 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006268 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006269 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006270 : (p1->wn_child != p2->wn_child)))
6271 break;
6272
6273 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006274}
6275
6276/*
6277 * Write a number to file "fd", MSB first, in "len" bytes.
6278 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006279 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006280put_bytes(fd, nr, len)
6281 FILE *fd;
6282 long_u nr;
6283 int len;
6284{
6285 int i;
6286
6287 for (i = len - 1; i >= 0; --i)
6288 putc((int)(nr >> (i * 8)), fd);
6289}
6290
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006291static int
6292#ifdef __BORLANDC__
6293_RTLENTRYF
6294#endif
6295rep_compare __ARGS((const void *s1, const void *s2));
6296
6297/*
6298 * Function given to qsort() to sort the REP items on "from" string.
6299 */
6300 static int
6301#ifdef __BORLANDC__
6302_RTLENTRYF
6303#endif
6304rep_compare(s1, s2)
6305 const void *s1;
6306 const void *s2;
6307{
6308 fromto_T *p1 = (fromto_T *)s1;
6309 fromto_T *p2 = (fromto_T *)s2;
6310
6311 return STRCMP(p1->ft_from, p2->ft_from);
6312}
6313
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006314/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00006315 * Write the Vim .spl file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006316 */
6317 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006318write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006319 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006320 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006321{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006322 FILE *fd;
6323 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006324 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006325 wordnode_T *tree;
6326 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006327 int i;
6328 int l;
6329 garray_T *gap;
6330 fromto_T *ftp;
6331 char_u *p;
6332 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006333
Bram Moolenaarb765d632005-06-07 21:00:02 +00006334 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006335 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006336 {
6337 EMSG2(_(e_notopen), fname);
6338 return;
6339 }
6340
Bram Moolenaar5195e452005-08-19 20:32:47 +00006341 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006342 /* <fileID> */
6343 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
6344 EMSG(_(e_write));
Bram Moolenaar5195e452005-08-19 20:32:47 +00006345 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006346
Bram Moolenaar5195e452005-08-19 20:32:47 +00006347 /*
6348 * <SECTIONS>: <section> ... <sectionend>
6349 */
6350
6351 /* SN_REGION: <regionname> ...
6352 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006353 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006354 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006355 putc(SN_REGION, fd); /* <sectionID> */
6356 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6357 l = spin->si_region_count * 2;
6358 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6359 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
6360 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006361 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006362 }
6363 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006364 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006365
Bram Moolenaar5195e452005-08-19 20:32:47 +00006366 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
6367 *
6368 * The table with character flags and the table for case folding.
6369 * This makes sure the same characters are recognized as word characters
6370 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006371 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006372 * 'encoding'.
6373 * Also skip this for an .add.spl file, the main spell file must contain
6374 * the table (avoids that it conflicts). File is shorter too.
6375 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006376 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006377 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006378 char_u folchars[128 * 8];
6379 int flags;
6380
Bram Moolenaard12a1322005-08-21 22:08:24 +00006381 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006382 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6383
6384 /* Form the <folchars> string first, we need to know its length. */
6385 l = 0;
6386 for (i = 128; i < 256; ++i)
6387 {
6388#ifdef FEAT_MBYTE
6389 if (has_mbyte)
6390 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
6391 else
6392#endif
6393 folchars[l++] = spelltab.st_fold[i];
6394 }
6395 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
6396
6397 fputc(128, fd); /* <charflagslen> */
6398 for (i = 128; i < 256; ++i)
6399 {
6400 flags = 0;
6401 if (spelltab.st_isw[i])
6402 flags |= CF_WORD;
6403 if (spelltab.st_isu[i])
6404 flags |= CF_UPPER;
6405 fputc(flags, fd); /* <charflags> */
6406 }
6407
6408 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
6409 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006410 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006411
Bram Moolenaar5195e452005-08-19 20:32:47 +00006412 /* SN_MIDWORD: <midword> */
6413 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006414 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006415 putc(SN_MIDWORD, fd); /* <sectionID> */
6416 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6417
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006418 i = STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006419 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006420 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
6421 }
6422
Bram Moolenaar5195e452005-08-19 20:32:47 +00006423 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
6424 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006425 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006426 putc(SN_PREFCOND, fd); /* <sectionID> */
6427 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6428
6429 l = write_spell_prefcond(NULL, &spin->si_prefcond);
6430 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6431
6432 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006433 }
6434
Bram Moolenaar5195e452005-08-19 20:32:47 +00006435 /* SN_REP: <repcount> <rep> ...
6436 * SN_SAL: <salflags> <salcount> <sal> ... */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006437
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006438 /* Sort the REP items. */
6439 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
6440 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006441
Bram Moolenaar5195e452005-08-19 20:32:47 +00006442 /* round 1: SN_REP section
6443 * round 2: SN_SAL section (unless SN_SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006444 for (round = 1; round <= 2; ++round)
6445 {
6446 if (round == 1)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006447 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006448 gap = &spin->si_rep;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006449 putc(SN_REP, fd); /* <sectionID> */
6450 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006451 else
6452 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006453 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
6454 /* using SN_SOFO section instead of SN_SAL */
6455 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006456 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006457 putc(SN_SAL, fd); /* <sectionID> */
6458 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006459
Bram Moolenaar5195e452005-08-19 20:32:47 +00006460 /* This is for making suggestions, section is not required. */
6461 putc(0, fd); /* <sectionflags> */
6462
6463 /* Compute the length of what follows. */
6464 l = 2; /* count <repcount> or <salcount> */
6465 for (i = 0; i < gap->ga_len; ++i)
6466 {
6467 ftp = &((fromto_T *)gap->ga_data)[i];
6468 l += 1 + STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
6469 l += 1 + STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
6470 }
6471 if (round == 2)
6472 ++l; /* count <salflags> */
6473 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6474
6475 if (round == 2)
6476 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006477 i = 0;
6478 if (spin->si_followup)
6479 i |= SAL_F0LLOWUP;
6480 if (spin->si_collapse)
6481 i |= SAL_COLLAPSE;
6482 if (spin->si_rem_accents)
6483 i |= SAL_REM_ACCENTS;
6484 putc(i, fd); /* <salflags> */
6485 }
6486
6487 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
6488 for (i = 0; i < gap->ga_len; ++i)
6489 {
6490 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
6491 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
6492 ftp = &((fromto_T *)gap->ga_data)[i];
6493 for (rr = 1; rr <= 2; ++rr)
6494 {
6495 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
6496 l = STRLEN(p);
6497 putc(l, fd);
6498 fwrite(p, l, (size_t)1, fd);
6499 }
6500 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00006501
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006502 }
6503
Bram Moolenaar5195e452005-08-19 20:32:47 +00006504 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
6505 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006506 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
6507 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006508 putc(SN_SOFO, fd); /* <sectionID> */
6509 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006510
6511 l = STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006512 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
6513 /* <sectionlen> */
6514
6515 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
6516 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006517
6518 l = STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006519 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
6520 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006521 }
6522
Bram Moolenaar5195e452005-08-19 20:32:47 +00006523 /* SN_MAP: <mapstr>
6524 * This is for making suggestions, section is not required. */
6525 if (spin->si_map.ga_len > 0)
6526 {
6527 putc(SN_MAP, fd); /* <sectionID> */
6528 putc(0, fd); /* <sectionflags> */
6529 l = spin->si_map.ga_len;
6530 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6531 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
6532 /* <mapstr> */
6533 }
6534
6535 /* SN_COMPOUND: compound info.
6536 * We don't mark it required, when not supported all compound words will
6537 * be bad words. */
6538 if (spin->si_compflags != NULL)
6539 {
6540 putc(SN_COMPOUND, fd); /* <sectionID> */
6541 putc(0, fd); /* <sectionflags> */
6542
6543 l = STRLEN(spin->si_compflags);
6544 put_bytes(fd, (long_u)(l + 3), 4); /* <sectionlen> */
6545 putc(spin->si_compmax, fd); /* <compmax> */
6546 putc(spin->si_compminlen, fd); /* <compminlen> */
6547 putc(spin->si_compsylmax, fd); /* <compsylmax> */
6548 /* <compflags> */
6549 fwrite(spin->si_compflags, (size_t)l, (size_t)1, fd);
6550 }
6551
Bram Moolenaar78622822005-08-23 21:00:13 +00006552 /* SN_NOBREAK: NOBREAK flag */
6553 if (spin->si_nobreak)
6554 {
6555 putc(SN_NOBREAK, fd); /* <sectionID> */
6556 putc(0, fd); /* <sectionflags> */
6557
6558 /* It's empty, the precense of the section flags the feature. */
6559 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
6560 }
6561
Bram Moolenaar5195e452005-08-19 20:32:47 +00006562 /* SN_SYLLABLE: syllable info.
6563 * We don't mark it required, when not supported syllables will not be
6564 * counted. */
6565 if (spin->si_syllable != NULL)
6566 {
6567 putc(SN_SYLLABLE, fd); /* <sectionID> */
6568 putc(0, fd); /* <sectionflags> */
6569
6570 l = STRLEN(spin->si_syllable);
6571 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6572 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
6573 }
6574
6575 /* end of <SECTIONS> */
6576 putc(SN_END, fd); /* <sectionend> */
6577
Bram Moolenaar50cde822005-06-05 21:54:54 +00006578
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006579 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006580 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006581 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006582 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006583 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006584 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006585 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006586 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006587 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006588 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006589 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006590 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006591
Bram Moolenaar0c405862005-06-22 22:26:26 +00006592 /* Clear the index and wnode fields in the tree. */
6593 clear_node(tree);
6594
Bram Moolenaar51485f02005-06-04 21:55:20 +00006595 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00006596 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00006597 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006598 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006599
Bram Moolenaar51485f02005-06-04 21:55:20 +00006600 /* number of nodes in 4 bytes */
6601 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00006602 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006603
Bram Moolenaar51485f02005-06-04 21:55:20 +00006604 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006605 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006606 }
6607
Bram Moolenaar51485f02005-06-04 21:55:20 +00006608 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006609}
6610
6611/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006612 * Clear the index and wnode fields of "node", it siblings and its
6613 * children. This is needed because they are a union with other items to save
6614 * space.
6615 */
6616 static void
6617clear_node(node)
6618 wordnode_T *node;
6619{
6620 wordnode_T *np;
6621
6622 if (node != NULL)
6623 for (np = node; np != NULL; np = np->wn_sibling)
6624 {
6625 np->wn_u1.index = 0;
6626 np->wn_u2.wnode = NULL;
6627
6628 if (np->wn_byte != NUL)
6629 clear_node(np->wn_child);
6630 }
6631}
6632
6633
6634/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006635 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006636 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00006637 * This first writes the list of possible bytes (siblings). Then for each
6638 * byte recursively write the children.
6639 *
6640 * NOTE: The code here must match the code in read_tree(), since assumptions
6641 * are made about the indexes (so that we don't have to write them in the
6642 * file).
6643 *
6644 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006645 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006646 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00006647put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006648 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006649 wordnode_T *node;
6650 int index;
6651 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006652 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006653{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006654 int newindex = index;
6655 int siblingcount = 0;
6656 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006657 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006658
Bram Moolenaar51485f02005-06-04 21:55:20 +00006659 /* If "node" is zero the tree is empty. */
6660 if (node == NULL)
6661 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006662
Bram Moolenaar51485f02005-06-04 21:55:20 +00006663 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006664 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006665
6666 /* Count the number of siblings. */
6667 for (np = node; np != NULL; np = np->wn_sibling)
6668 ++siblingcount;
6669
6670 /* Write the sibling count. */
6671 if (fd != NULL)
6672 putc(siblingcount, fd); /* <siblingcount> */
6673
6674 /* Write each sibling byte and optionally extra info. */
6675 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006676 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006677 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006678 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006679 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006680 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006681 /* For a NUL byte (end of word) write the flags etc. */
6682 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006683 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006684 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006685 * associated condition nr (stored in wn_region). The
6686 * byte value is misused to store the "rare" and "not
6687 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00006688 if (np->wn_flags == (short_u)PFX_FLAGS)
6689 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006690 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00006691 {
6692 putc(BY_FLAGS, fd); /* <byte> */
6693 putc(np->wn_flags, fd); /* <pflags> */
6694 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006695 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006696 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006697 }
6698 else
6699 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006700 /* For word trees we write the flag/region items. */
6701 flags = np->wn_flags;
6702 if (regionmask != 0 && np->wn_region != regionmask)
6703 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006704 if (np->wn_affixID != 0)
6705 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006706 if (flags == 0)
6707 {
6708 /* word without flags or region */
6709 putc(BY_NOFLAGS, fd); /* <byte> */
6710 }
6711 else
6712 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006713 if (np->wn_flags >= 0x100)
6714 {
6715 putc(BY_FLAGS2, fd); /* <byte> */
6716 putc(flags, fd); /* <flags> */
6717 putc((unsigned)flags >> 8, fd); /* <flags2> */
6718 }
6719 else
6720 {
6721 putc(BY_FLAGS, fd); /* <byte> */
6722 putc(flags, fd); /* <flags> */
6723 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006724 if (flags & WF_REGION)
6725 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006726 if (flags & WF_AFX)
6727 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006728 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006729 }
6730 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006731 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006732 else
6733 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006734 if (np->wn_child->wn_u1.index != 0
6735 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006736 {
6737 /* The child is written elsewhere, write the reference. */
6738 if (fd != NULL)
6739 {
6740 putc(BY_INDEX, fd); /* <byte> */
6741 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006742 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006743 }
6744 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00006745 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006746 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006747 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006748
Bram Moolenaar51485f02005-06-04 21:55:20 +00006749 if (fd != NULL)
6750 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
6751 {
6752 EMSG(_(e_write));
6753 return 0;
6754 }
6755 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006756 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006757
6758 /* Space used in the array when reading: one for each sibling and one for
6759 * the count. */
6760 newindex += siblingcount + 1;
6761
6762 /* Recursively dump the children of each sibling. */
6763 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00006764 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
6765 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006766 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006767
6768 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006769}
6770
6771
6772/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00006773 * ":mkspell [-ascii] outfile infile ..."
6774 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006775 */
6776 void
6777ex_mkspell(eap)
6778 exarg_T *eap;
6779{
6780 int fcount;
6781 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006782 char_u *arg = eap->arg;
6783 int ascii = FALSE;
6784
6785 if (STRNCMP(arg, "-ascii", 6) == 0)
6786 {
6787 ascii = TRUE;
6788 arg = skipwhite(arg + 6);
6789 }
6790
6791 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
6792 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
6793 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006794 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006795 FreeWild(fcount, fnames);
6796 }
6797}
6798
6799/*
6800 * Create a Vim spell file from one or more word lists.
6801 * "fnames[0]" is the output file name.
6802 * "fnames[fcount - 1]" is the last input file name.
6803 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
6804 * and ".spl" is appended to make the output file name.
6805 */
6806 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006807mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006808 int fcount;
6809 char_u **fnames;
6810 int ascii; /* -ascii argument given */
6811 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006812 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006813{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006814 char_u fname[MAXPATHL];
6815 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00006816 char_u **innames;
6817 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006818 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006819 int i;
6820 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006821 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006822 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006823 spellinfo_T spin;
6824
6825 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006826 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006827 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006828 spin.si_followup = TRUE;
6829 spin.si_rem_accents = TRUE;
6830 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
6831 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
6832 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006833 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006834
Bram Moolenaarb765d632005-06-07 21:00:02 +00006835 /* default: fnames[0] is output file, following are input files */
6836 innames = &fnames[1];
6837 incount = fcount - 1;
6838
6839 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00006840 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006841 len = STRLEN(fnames[0]);
6842 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
6843 {
6844 /* For ":mkspell path/en.latin1.add" output file is
6845 * "path/en.latin1.add.spl". */
6846 innames = &fnames[0];
6847 incount = 1;
6848 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
6849 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006850 else if (fcount == 1)
6851 {
6852 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
6853 innames = &fnames[0];
6854 incount = 1;
6855 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
6856 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
6857 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006858 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
6859 {
6860 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006861 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006862 }
6863 else
6864 /* Name should be language, make the file name from it. */
6865 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
6866 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
6867
6868 /* Check for .ascii.spl. */
6869 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
6870 spin.si_ascii = TRUE;
6871
6872 /* Check for .add.spl. */
6873 if (strstr((char *)gettail(wfname), ".add.") != NULL)
6874 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00006875 }
6876
Bram Moolenaarb765d632005-06-07 21:00:02 +00006877 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006878 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006879 else if (vim_strchr(gettail(wfname), '_') != NULL)
6880 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00006881 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006882 EMSG(_("E754: Only up to 8 regions supported"));
6883 else
6884 {
6885 /* Check for overwriting before doing things that may take a lot of
6886 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006887 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006888 {
6889 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00006890 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006891 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006892 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006893 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006894 EMSG2(_(e_isadir2), wfname);
6895 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006896 }
6897
6898 /*
6899 * Init the aff and dic pointers.
6900 * Get the region names if there are more than 2 arguments.
6901 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006902 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006903 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006904 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006905
Bram Moolenaar3982c542005-06-08 21:56:31 +00006906 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006907 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006908 len = STRLEN(innames[i]);
6909 if (STRLEN(gettail(innames[i])) < 5
6910 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006911 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006912 EMSG2(_("E755: Invalid region in %s"), innames[i]);
6913 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006914 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00006915 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
6916 spin.si_region_name[i * 2 + 1] =
6917 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006918 }
6919 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00006920 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006921
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006922 spin.si_foldroot = wordtree_alloc(&spin);
6923 spin.si_keeproot = wordtree_alloc(&spin);
6924 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006925 if (spin.si_foldroot == NULL
6926 || spin.si_keeproot == NULL
6927 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006928 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006929 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006930 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006931 }
6932
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006933 /* When not producing a .add.spl file clear the character table when
6934 * we encounter one in the .aff file. This means we dump the current
6935 * one in the .spl file if the .aff file doesn't define one. That's
6936 * better than guessing the contents, the table will match a
6937 * previously loaded spell file. */
6938 if (!spin.si_add)
6939 spin.si_clear_chartab = TRUE;
6940
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006941 /*
6942 * Read all the .aff and .dic files.
6943 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006944 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006945 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006946 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006947 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006948 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006949 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006950
Bram Moolenaarb765d632005-06-07 21:00:02 +00006951 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006952 if (mch_stat((char *)fname, &st) >= 0)
6953 {
6954 /* Read the .aff file. Will init "spin->si_conv" based on the
6955 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006956 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006957 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006958 error = TRUE;
6959 else
6960 {
6961 /* Read the .dic file and store the words in the trees. */
6962 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00006963 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006964 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006965 error = TRUE;
6966 }
6967 }
6968 else
6969 {
6970 /* No .aff file, try reading the file as a word list. Store
6971 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006972 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006973 error = TRUE;
6974 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006975
Bram Moolenaarb765d632005-06-07 21:00:02 +00006976#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006977 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006978 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006979#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006980 }
6981
Bram Moolenaar78622822005-08-23 21:00:13 +00006982 if (spin.si_compflags != NULL && spin.si_nobreak)
6983 MSG(_("Warning: both compounding and NOBREAK specified"));
6984
Bram Moolenaar51485f02005-06-04 21:55:20 +00006985 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006986 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006987 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006988 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006989 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006990 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006991 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006992 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006993 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006994 MSG(_(msg_compressing));
Bram Moolenaarb765d632005-06-07 21:00:02 +00006995 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006996 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006997 verbose_leave();
6998 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006999 wordtree_compress(&spin, spin.si_foldroot);
7000 wordtree_compress(&spin, spin.si_keeproot);
7001 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007002 }
7003
Bram Moolenaar51485f02005-06-04 21:55:20 +00007004 if (!error)
7005 {
7006 /*
7007 * Write the info in the spell file.
7008 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007009 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007010 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007011 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007012 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007013 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007014 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007015 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007016 verbose_leave();
7017 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00007018
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007019 write_vim_spell(&spin, wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007020
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007021 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007022 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007023 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007024 verbose_enter();
7025 MSG(_("Done!"));
7026 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00007027 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007028 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007029 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007030 verbose_leave();
7031 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007032
Bram Moolenaarb765d632005-06-07 21:00:02 +00007033 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007034 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007035 }
7036
7037 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007038 ga_clear(&spin.si_rep);
7039 ga_clear(&spin.si_sal);
7040 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007041 ga_clear(&spin.si_prefcond);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007042 vim_free(spin.si_midword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007043 vim_free(spin.si_sofofr);
7044 vim_free(spin.si_sofoto);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007045
7046 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007047 for (i = 0; i < incount; ++i)
7048 if (afile[i] != NULL)
7049 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007050
7051 /* Free all the bits and pieces at once. */
7052 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007053 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007054}
7055
Bram Moolenaarb765d632005-06-07 21:00:02 +00007056
7057/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007058 * ":[count]spellgood {word}"
7059 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00007060 */
7061 void
7062ex_spell(eap)
7063 exarg_T *eap;
7064{
Bram Moolenaar7887d882005-07-01 22:33:52 +00007065 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007066 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007067}
7068
7069/*
7070 * Add "word[len]" to 'spellfile' as a good or bad word.
7071 */
7072 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007073spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007074 char_u *word;
7075 int len;
7076 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007077 int index; /* "zG" and "zW": zero, otherwise index in
7078 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007079{
7080 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007081 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007082 int new_spf = FALSE;
7083 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007084 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007085 char_u fnamebuf[MAXPATHL];
7086 char_u line[MAXWLEN * 2];
7087 long fpos, fpos_next = 0;
7088 int i;
7089 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007090
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007091 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007092 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007093 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00007094 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007095 int_wordlist = vim_tempname('s');
7096 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00007097 return;
7098 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007099 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007100 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007101 else
7102 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007103 /* If 'spellfile' isn't set figure out a good default value. */
7104 if (*curbuf->b_p_spf == NUL)
7105 {
7106 init_spellfile();
7107 new_spf = TRUE;
7108 }
7109
7110 if (*curbuf->b_p_spf == NUL)
7111 {
7112 EMSG(_("E764: 'spellfile' is not set"));
7113 return;
7114 }
7115
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007116 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
7117 {
7118 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
7119 if (i == index)
7120 break;
7121 if (*spf == NUL)
7122 {
7123 EMSGN(_("E765: 'spellfile' does not have %ld enties"), index);
7124 return;
7125 }
7126 }
7127
Bram Moolenaarb765d632005-06-07 21:00:02 +00007128 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007129 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007130 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
7131 buf = NULL;
7132 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00007133 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007134 EMSG(_(e_bufloaded));
7135 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007136 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007137
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007138 fname = fnamebuf;
7139 }
7140
7141 if (bad)
7142 {
7143 /* When the word also appears as good word we need to remove that one,
7144 * since its flags sort before the one with WF_BANNED. */
7145 fd = mch_fopen((char *)fname, "r");
7146 if (fd != NULL)
7147 {
7148 while (!vim_fgets(line, MAXWLEN * 2, fd))
7149 {
7150 fpos = fpos_next;
7151 fpos_next = ftell(fd);
7152 if (STRNCMP(word, line, len) == 0
7153 && (line[len] == '/' || line[len] < ' '))
7154 {
7155 /* Found duplicate word. Remove it by writing a '#' at
7156 * the start of the line. Mixing reading and writing
7157 * doesn't work for all systems, close the file first. */
7158 fclose(fd);
7159 fd = mch_fopen((char *)fname, "r+");
7160 if (fd == NULL)
7161 break;
7162 if (fseek(fd, fpos, SEEK_SET) == 0)
7163 fputc('#', fd);
7164 fseek(fd, fpos_next, SEEK_SET);
7165 }
7166 }
7167 fclose(fd);
7168 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007169 }
7170
7171 fd = mch_fopen((char *)fname, "a");
7172 if (fd == NULL && new_spf)
7173 {
7174 /* We just initialized the 'spellfile' option and can't open the file.
7175 * We may need to create the "spell" directory first. We already
7176 * checked the runtime directory is writable in init_spellfile(). */
7177 STRCPY(NameBuff, fname);
7178 *gettail_sep(NameBuff) = NUL;
7179 if (mch_stat((char *)NameBuff, &st) < 0)
7180 {
7181 /* The directory doesn't exist. Try creating it and opening the
7182 * file again. */
7183 vim_mkdir(NameBuff, 0755);
7184 fd = mch_fopen((char *)fname, "a");
7185 }
7186 }
7187
7188 if (fd == NULL)
7189 EMSG2(_(e_notopen), fname);
7190 else
7191 {
7192 if (bad)
7193 fprintf(fd, "%.*s/!\n", len, word);
7194 else
7195 fprintf(fd, "%.*s\n", len, word);
7196 fclose(fd);
7197
7198 /* Update the .add.spl file. */
7199 mkspell(1, &fname, FALSE, TRUE, TRUE);
7200
7201 /* If the .add file is edited somewhere, reload it. */
7202 if (buf != NULL)
7203 buf_reload(buf);
7204
7205 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007206 }
7207}
7208
7209/*
7210 * Initialize 'spellfile' for the current buffer.
7211 */
7212 static void
7213init_spellfile()
7214{
7215 char_u buf[MAXPATHL];
7216 int l;
7217 slang_T *sl;
7218 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007219 char_u *lend;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007220
7221 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
7222 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007223 /* Find the end of the language name. Exclude the region. */
7224 for (lend = curbuf->b_p_spl; *lend != NUL
7225 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
7226 ;
7227
7228 /* Loop over all entries in 'runtimepath'. Use the first one where we
7229 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007230 rtp = p_rtp;
7231 while (*rtp != NUL)
7232 {
7233 /* Copy the path from 'runtimepath' to buf[]. */
7234 copy_option_part(&rtp, buf, MAXPATHL, ",");
7235 if (filewritable(buf) == 2)
7236 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00007237 /* Use the first language name from 'spelllang' and the
7238 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007239 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
7240 l = STRLEN(buf);
7241 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00007242 "/spell/%.*s.%s.add",
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007243 (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00007244 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
7245 ? (char_u *)"ascii" : spell_enc());
7246 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
7247 break;
7248 }
7249 }
7250 }
7251}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007252
Bram Moolenaar51485f02005-06-04 21:55:20 +00007253
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007254/*
7255 * Init the chartab used for spelling for ASCII.
7256 * EBCDIC is not supported!
7257 */
7258 static void
7259clear_spell_chartab(sp)
7260 spelltab_T *sp;
7261{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007262 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007263
7264 /* Init everything to FALSE. */
7265 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
7266 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
7267 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007268 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007269 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007270 sp->st_upper[i] = i;
7271 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007272
7273 /* We include digits. A word shouldn't start with a digit, but handling
7274 * that is done separately. */
7275 for (i = '0'; i <= '9'; ++i)
7276 sp->st_isw[i] = TRUE;
7277 for (i = 'A'; i <= 'Z'; ++i)
7278 {
7279 sp->st_isw[i] = TRUE;
7280 sp->st_isu[i] = TRUE;
7281 sp->st_fold[i] = i + 0x20;
7282 }
7283 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007284 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007285 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007286 sp->st_upper[i] = i - 0x20;
7287 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007288}
7289
7290/*
7291 * Init the chartab used for spelling. Only depends on 'encoding'.
7292 * Called once while starting up and when 'encoding' changes.
7293 * The default is to use isalpha(), but the spell file should define the word
7294 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007295 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007296 */
7297 void
7298init_spell_chartab()
7299{
7300 int i;
7301
7302 did_set_spelltab = FALSE;
7303 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007304#ifdef FEAT_MBYTE
7305 if (enc_dbcs)
7306 {
7307 /* DBCS: assume double-wide characters are word characters. */
7308 for (i = 128; i <= 255; ++i)
7309 if (MB_BYTE2LEN(i) == 2)
7310 spelltab.st_isw[i] = TRUE;
7311 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007312 else if (enc_utf8)
7313 {
7314 for (i = 128; i < 256; ++i)
7315 {
7316 spelltab.st_isu[i] = utf_isupper(i);
7317 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
7318 spelltab.st_fold[i] = utf_fold(i);
7319 spelltab.st_upper[i] = utf_toupper(i);
7320 }
7321 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007322 else
7323#endif
7324 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007325 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007326 for (i = 128; i < 256; ++i)
7327 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007328 if (MB_ISUPPER(i))
7329 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007330 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007331 spelltab.st_isu[i] = TRUE;
7332 spelltab.st_fold[i] = MB_TOLOWER(i);
7333 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007334 else if (MB_ISLOWER(i))
7335 {
7336 spelltab.st_isw[i] = TRUE;
7337 spelltab.st_upper[i] = MB_TOUPPER(i);
7338 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007339 }
7340 }
7341}
7342
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007343static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
7344static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
7345
7346/*
7347 * Set the spell character tables from strings in the affix file.
7348 */
7349 static int
7350set_spell_chartab(fol, low, upp)
7351 char_u *fol;
7352 char_u *low;
7353 char_u *upp;
7354{
7355 /* We build the new tables here first, so that we can compare with the
7356 * previous one. */
7357 spelltab_T new_st;
7358 char_u *pf = fol, *pl = low, *pu = upp;
7359 int f, l, u;
7360
7361 clear_spell_chartab(&new_st);
7362
7363 while (*pf != NUL)
7364 {
7365 if (*pl == NUL || *pu == NUL)
7366 {
7367 EMSG(_(e_affform));
7368 return FAIL;
7369 }
7370#ifdef FEAT_MBYTE
7371 f = mb_ptr2char_adv(&pf);
7372 l = mb_ptr2char_adv(&pl);
7373 u = mb_ptr2char_adv(&pu);
7374#else
7375 f = *pf++;
7376 l = *pl++;
7377 u = *pu++;
7378#endif
7379 /* Every character that appears is a word character. */
7380 if (f < 256)
7381 new_st.st_isw[f] = TRUE;
7382 if (l < 256)
7383 new_st.st_isw[l] = TRUE;
7384 if (u < 256)
7385 new_st.st_isw[u] = TRUE;
7386
7387 /* if "LOW" and "FOL" are not the same the "LOW" char needs
7388 * case-folding */
7389 if (l < 256 && l != f)
7390 {
7391 if (f >= 256)
7392 {
7393 EMSG(_(e_affrange));
7394 return FAIL;
7395 }
7396 new_st.st_fold[l] = f;
7397 }
7398
7399 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007400 * case-folding, it's upper case and the "UPP" is the upper case of
7401 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007402 if (u < 256 && u != f)
7403 {
7404 if (f >= 256)
7405 {
7406 EMSG(_(e_affrange));
7407 return FAIL;
7408 }
7409 new_st.st_fold[u] = f;
7410 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007411 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007412 }
7413 }
7414
7415 if (*pl != NUL || *pu != NUL)
7416 {
7417 EMSG(_(e_affform));
7418 return FAIL;
7419 }
7420
7421 return set_spell_finish(&new_st);
7422}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007423
7424/*
7425 * Set the spell character tables from strings in the .spl file.
7426 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007427 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007428set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007429 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007430 int cnt; /* length of "flags" */
7431 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007432{
7433 /* We build the new tables here first, so that we can compare with the
7434 * previous one. */
7435 spelltab_T new_st;
7436 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007437 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007438 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007439
7440 clear_spell_chartab(&new_st);
7441
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007442 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007443 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007444 if (i < cnt)
7445 {
7446 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
7447 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
7448 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007449
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007450 if (*p != NUL)
7451 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007452#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007453 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007454#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007455 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007456#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007457 new_st.st_fold[i + 128] = c;
7458 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
7459 new_st.st_upper[c] = i + 128;
7460 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007461 }
7462
Bram Moolenaar5195e452005-08-19 20:32:47 +00007463 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007464}
7465
7466 static int
7467set_spell_finish(new_st)
7468 spelltab_T *new_st;
7469{
7470 int i;
7471
7472 if (did_set_spelltab)
7473 {
7474 /* check that it's the same table */
7475 for (i = 0; i < 256; ++i)
7476 {
7477 if (spelltab.st_isw[i] != new_st->st_isw[i]
7478 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007479 || spelltab.st_fold[i] != new_st->st_fold[i]
7480 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007481 {
7482 EMSG(_("E763: Word characters differ between spell files"));
7483 return FAIL;
7484 }
7485 }
7486 }
7487 else
7488 {
7489 /* copy the new spelltab into the one being used */
7490 spelltab = *new_st;
7491 did_set_spelltab = TRUE;
7492 }
7493
7494 return OK;
7495}
7496
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007497/*
Bram Moolenaarea408852005-06-25 22:49:46 +00007498 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007499 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00007500 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007501 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00007502 */
7503 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007504spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00007505 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007506 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00007507{
Bram Moolenaarea408852005-06-25 22:49:46 +00007508#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007509 char_u *s;
7510 int l;
7511 int c;
7512
7513 if (has_mbyte)
7514 {
7515 l = MB_BYTE2LEN(*p);
7516 s = p;
7517 if (l == 1)
7518 {
7519 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007520 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007521 {
7522 s = p + 1; /* skip a mid-word character */
7523 l = MB_BYTE2LEN(*s);
7524 }
7525 }
7526 else
7527 {
7528 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007529 if (c < 256 ? buf->b_spell_ismw[c]
7530 : (buf->b_spell_ismw_mb != NULL
7531 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007532 {
7533 s = p + l;
7534 l = MB_BYTE2LEN(*s);
7535 }
7536 }
7537
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007538 c = mb_ptr2char(s);
7539 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007540 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007541 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007542 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007543#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007544
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007545 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
7546}
7547
7548/*
7549 * Return TRUE if "p" points to a word character.
7550 * Unlike spell_iswordp() this doesn't check for "midword" characters.
7551 */
7552 static int
7553spell_iswordp_nmw(p)
7554 char_u *p;
7555{
7556#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007557 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007558
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007559 if (has_mbyte)
7560 {
7561 c = mb_ptr2char(p);
7562 if (c > 255)
7563 return mb_get_class(p) >= 2;
7564 return spelltab.st_isw[c];
7565 }
7566#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007567 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00007568}
7569
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007570#ifdef FEAT_MBYTE
7571/*
7572 * Return TRUE if "p" points to a word character.
7573 * Wide version of spell_iswordp().
7574 */
7575 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007576spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007577 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007578 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007579{
7580 int *s;
7581
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007582 if (*p < 256 ? buf->b_spell_ismw[*p]
7583 : (buf->b_spell_ismw_mb != NULL
7584 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007585 s = p + 1;
7586 else
7587 s = p;
7588
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007589 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007590 {
7591 if (enc_utf8)
7592 return utf_class(*s) >= 2;
7593 if (enc_dbcs)
7594 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
7595 return 0;
7596 }
7597 return spelltab.st_isw[*s];
7598}
7599#endif
7600
Bram Moolenaarea408852005-06-25 22:49:46 +00007601/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007602 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00007603 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007604 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007605 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007606write_spell_prefcond(fd, gap)
7607 FILE *fd;
7608 garray_T *gap;
7609{
7610 int i;
7611 char_u *p;
7612 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007613 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007614
Bram Moolenaar5195e452005-08-19 20:32:47 +00007615 if (fd != NULL)
7616 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
7617
7618 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007619
7620 for (i = 0; i < gap->ga_len; ++i)
7621 {
7622 /* <prefcond> : <condlen> <condstr> */
7623 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00007624 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007625 {
7626 len = STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007627 if (fd != NULL)
7628 {
7629 fputc(len, fd);
7630 fwrite(p, (size_t)len, (size_t)1, fd);
7631 }
7632 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007633 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007634 else if (fd != NULL)
7635 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007636 }
7637
Bram Moolenaar5195e452005-08-19 20:32:47 +00007638 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007639}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007640
7641/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007642 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
7643 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007644 * When using a multi-byte 'encoding' the length may change!
7645 * Returns FAIL when something wrong.
7646 */
7647 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007648spell_casefold(str, len, buf, buflen)
7649 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007650 int len;
7651 char_u *buf;
7652 int buflen;
7653{
7654 int i;
7655
7656 if (len >= buflen)
7657 {
7658 buf[0] = NUL;
7659 return FAIL; /* result will not fit */
7660 }
7661
7662#ifdef FEAT_MBYTE
7663 if (has_mbyte)
7664 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007665 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007666 char_u *p;
7667 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007668
7669 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007670 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007671 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007672 if (outi + MB_MAXBYTES > buflen)
7673 {
7674 buf[outi] = NUL;
7675 return FAIL;
7676 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007677 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007678 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007679 }
7680 buf[outi] = NUL;
7681 }
7682 else
7683#endif
7684 {
7685 /* Be quick for non-multibyte encodings. */
7686 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007687 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007688 buf[i] = NUL;
7689 }
7690
7691 return OK;
7692}
7693
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007694#define SPS_BEST 1
7695#define SPS_FAST 2
7696#define SPS_DOUBLE 4
7697
7698static int sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007699static int sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007700
7701/*
7702 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00007703 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007704 */
7705 int
7706spell_check_sps()
7707{
7708 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007709 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007710 char_u buf[MAXPATHL];
7711 int f;
7712
7713 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007714 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007715
7716 for (p = p_sps; *p != NUL; )
7717 {
7718 copy_option_part(&p, buf, MAXPATHL, ",");
7719
7720 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007721 if (VIM_ISDIGIT(*buf))
7722 {
7723 s = buf;
7724 sps_limit = getdigits(&s);
7725 if (*s != NUL && !VIM_ISDIGIT(*s))
7726 f = -1;
7727 }
7728 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007729 f = SPS_BEST;
7730 else if (STRCMP(buf, "fast") == 0)
7731 f = SPS_FAST;
7732 else if (STRCMP(buf, "double") == 0)
7733 f = SPS_DOUBLE;
7734 else if (STRNCMP(buf, "expr:", 5) != 0
7735 && STRNCMP(buf, "file:", 5) != 0)
7736 f = -1;
7737
7738 if (f == -1 || (sps_flags != 0 && f != 0))
7739 {
7740 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007741 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007742 return FAIL;
7743 }
7744 if (f != 0)
7745 sps_flags = f;
7746 }
7747
7748 if (sps_flags == 0)
7749 sps_flags = SPS_BEST;
7750
7751 return OK;
7752}
7753
7754/* Remember what "z?" replaced. */
7755static char_u *repl_from = NULL;
7756static char_u *repl_to = NULL;
7757
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007758/*
7759 * "z?": Find badly spelled word under or after the cursor.
7760 * Give suggestions for the properly spelled word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00007761 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007762 */
7763 void
Bram Moolenaard12a1322005-08-21 22:08:24 +00007764spell_suggest(count)
7765 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007766{
7767 char_u *line;
7768 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007769 char_u wcopy[MAXWLEN + 2];
7770 char_u *p;
7771 int i;
7772 int c;
7773 suginfo_T sug;
7774 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007775 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007776 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007777 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00007778 int selected = count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007779
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007780 /* Find the start of the badly spelled word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007781 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL
7782 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007783 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00007784 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
7785 return;
7786
7787 /* No bad word or it starts after the cursor: use the word under the
7788 * cursor. */
7789 curwin->w_cursor = prev_cursor;
7790 line = ml_get_curline();
7791 p = line + curwin->w_cursor.col;
7792 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007793 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00007794 mb_ptr_back(line, p);
7795 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007796 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00007797 mb_ptr_adv(p);
7798
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007799 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007800 {
7801 beep_flush();
7802 return;
7803 }
7804 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007805 }
7806
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007807 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007808
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007809 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007810 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007811
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007812 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007813
Bram Moolenaar5195e452005-08-19 20:32:47 +00007814 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
7815 * 'spellsuggest', whatever is smaller. */
7816 if (sps_limit > (int)Rows - 2)
7817 limit = (int)Rows - 2;
7818 else
7819 limit = sps_limit;
7820 spell_find_suggest(line + curwin->w_cursor.col, &sug, limit,
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00007821 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007822
7823 if (sug.su_ga.ga_len == 0)
7824 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00007825 else if (count > 0)
7826 {
7827 if (count > sug.su_ga.ga_len)
7828 smsg((char_u *)_("Sorry, only %ld suggestions"),
7829 (long)sug.su_ga.ga_len);
7830 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007831 else
7832 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007833 vim_free(repl_from);
7834 repl_from = NULL;
7835 vim_free(repl_to);
7836 repl_to = NULL;
7837
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007838#ifdef FEAT_RIGHTLEFT
7839 /* When 'rightleft' is set the list is drawn right-left. */
7840 cmdmsg_rl = curwin->w_p_rl;
7841 if (cmdmsg_rl)
7842 msg_col = Columns - 1;
7843#endif
7844
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007845 /* List the suggestions. */
7846 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007847 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007848 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
7849 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007850#ifdef FEAT_RIGHTLEFT
7851 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
7852 {
7853 /* And now the rabbit from the high hat: Avoid showing the
7854 * untranslated message rightleft. */
7855 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
7856 sug.su_badlen, sug.su_badptr);
7857 }
7858#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007859 msg_puts(IObuff);
7860 msg_clr_eos();
7861 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00007862
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007863 msg_scroll = TRUE;
7864 for (i = 0; i < sug.su_ga.ga_len; ++i)
7865 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007866 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007867
7868 /* The suggested word may replace only part of the bad word, add
7869 * the not replaced part. */
7870 STRCPY(wcopy, stp->st_word);
7871 if (sug.su_badlen > stp->st_orglen)
7872 vim_strncpy(wcopy + STRLEN(wcopy),
7873 sug.su_badptr + stp->st_orglen,
7874 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007875 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
7876#ifdef FEAT_RIGHTLEFT
7877 if (cmdmsg_rl)
7878 rl_mirror(IObuff);
7879#endif
7880 msg_puts(IObuff);
7881
7882 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007883 msg_puts(IObuff);
7884
7885 /* The word may replace more than "su_badlen". */
7886 if (sug.su_badlen < stp->st_orglen)
7887 {
7888 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
7889 stp->st_orglen, sug.su_badptr);
7890 msg_puts(IObuff);
7891 }
7892
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007893 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007894 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00007895 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007896 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007897 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007898 stp->st_salscore ? "s " : "",
7899 stp->st_score, stp->st_altscore);
7900 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007901 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00007902 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007903#ifdef FEAT_RIGHTLEFT
7904 if (cmdmsg_rl)
7905 /* Mirror the numbers, but keep the leading space. */
7906 rl_mirror(IObuff + 1);
7907#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00007908 msg_advance(30);
7909 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007910 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007911 msg_putchar('\n');
7912 }
7913
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007914#ifdef FEAT_RIGHTLEFT
7915 cmdmsg_rl = FALSE;
7916 msg_col = 0;
7917#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007918 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00007919 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007920 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00007921 selected -= lines_left;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007922 }
7923
Bram Moolenaard12a1322005-08-21 22:08:24 +00007924 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
7925 {
7926 /* Save the from and to text for :spellrepall. */
7927 stp = &SUG(sug.su_ga, selected - 1);
7928 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
7929 repl_to = vim_strsave(stp->st_word);
7930
7931 /* Replace the word. */
7932 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
7933 if (p != NULL)
7934 {
7935 c = sug.su_badptr - line;
7936 mch_memmove(p, line, c);
7937 STRCPY(p + c, stp->st_word);
7938 STRCAT(p, sug.su_badptr + stp->st_orglen);
7939 ml_replace(curwin->w_cursor.lnum, p, FALSE);
7940 curwin->w_cursor.col = c;
7941 changed_bytes(curwin->w_cursor.lnum, c);
7942
7943 /* For redo we use a change-word command. */
7944 ResetRedobuff();
7945 AppendToRedobuff((char_u *)"ciw");
7946 AppendToRedobuff(stp->st_word);
7947 AppendCharToRedobuff(ESC);
7948 }
7949 }
7950 else
7951 curwin->w_cursor = prev_cursor;
7952
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007953 spell_find_cleanup(&sug);
7954}
7955
7956/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00007957 * Check if the word at line "lnum" column "col" is required to start with a
7958 * capital. This uses 'spellcapcheck' of the current buffer.
7959 */
7960 static int
7961check_need_cap(lnum, col)
7962 linenr_T lnum;
7963 colnr_T col;
7964{
7965 int need_cap = FALSE;
7966 char_u *line;
7967 char_u *line_copy = NULL;
7968 char_u *p;
7969 colnr_T endcol;
7970 regmatch_T regmatch;
7971
7972 if (curbuf->b_cap_prog == NULL)
7973 return FALSE;
7974
7975 line = ml_get_curline();
7976 endcol = 0;
7977 if ((int)(skipwhite(line) - line) >= (int)col)
7978 {
7979 /* At start of line, check if previous line is empty or sentence
7980 * ends there. */
7981 if (lnum == 1)
7982 need_cap = TRUE;
7983 else
7984 {
7985 line = ml_get(lnum - 1);
7986 if (*skipwhite(line) == NUL)
7987 need_cap = TRUE;
7988 else
7989 {
7990 /* Append a space in place of the line break. */
7991 line_copy = concat_str(line, (char_u *)" ");
7992 line = line_copy;
7993 endcol = STRLEN(line);
7994 }
7995 }
7996 }
7997 else
7998 endcol = col;
7999
8000 if (endcol > 0)
8001 {
8002 /* Check if sentence ends before the bad word. */
8003 regmatch.regprog = curbuf->b_cap_prog;
8004 regmatch.rm_ic = FALSE;
8005 p = line + endcol;
8006 for (;;)
8007 {
8008 mb_ptr_back(line, p);
8009 if (p == line || spell_iswordp_nmw(p))
8010 break;
8011 if (vim_regexec(&regmatch, p, 0)
8012 && regmatch.endp[0] == line + endcol)
8013 {
8014 need_cap = TRUE;
8015 break;
8016 }
8017 }
8018 }
8019
8020 vim_free(line_copy);
8021
8022 return need_cap;
8023}
8024
8025
8026/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008027 * ":spellrepall"
8028 */
8029/*ARGSUSED*/
8030 void
8031ex_spellrepall(eap)
8032 exarg_T *eap;
8033{
8034 pos_T pos = curwin->w_cursor;
8035 char_u *frompat;
8036 int addlen;
8037 char_u *line;
8038 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008039 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008040 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008041
8042 if (repl_from == NULL || repl_to == NULL)
8043 {
8044 EMSG(_("E752: No previous spell replacement"));
8045 return;
8046 }
8047 addlen = STRLEN(repl_to) - STRLEN(repl_from);
8048
8049 frompat = alloc(STRLEN(repl_from) + 7);
8050 if (frompat == NULL)
8051 return;
8052 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
8053 p_ws = FALSE;
8054
Bram Moolenaar5195e452005-08-19 20:32:47 +00008055 sub_nsubs = 0;
8056 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008057 curwin->w_cursor.lnum = 0;
8058 while (!got_int)
8059 {
8060 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
8061 || u_save_cursor() == FAIL)
8062 break;
8063
8064 /* Only replace when the right word isn't there yet. This happens
8065 * when changing "etc" to "etc.". */
8066 line = ml_get_curline();
8067 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
8068 repl_to, STRLEN(repl_to)) != 0)
8069 {
8070 p = alloc(STRLEN(line) + addlen + 1);
8071 if (p == NULL)
8072 break;
8073 mch_memmove(p, line, curwin->w_cursor.col);
8074 STRCPY(p + curwin->w_cursor.col, repl_to);
8075 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
8076 ml_replace(curwin->w_cursor.lnum, p, FALSE);
8077 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008078
8079 if (curwin->w_cursor.lnum != prev_lnum)
8080 {
8081 ++sub_nlines;
8082 prev_lnum = curwin->w_cursor.lnum;
8083 }
8084 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008085 }
8086 curwin->w_cursor.col += STRLEN(repl_to);
8087 }
8088
8089 p_ws = save_ws;
8090 curwin->w_cursor = pos;
8091 vim_free(frompat);
8092
Bram Moolenaar5195e452005-08-19 20:32:47 +00008093 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008094 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008095 else
8096 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008097}
8098
8099/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008100 * Find spell suggestions for "word". Return them in the growarray "*gap" as
8101 * a list of allocated strings.
8102 */
8103 void
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008104spell_suggest_list(gap, word, maxcount, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008105 garray_T *gap;
8106 char_u *word;
8107 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008108 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008109{
8110 suginfo_T sug;
8111 int i;
8112 suggest_T *stp;
8113 char_u *wcopy;
8114
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008115 spell_find_suggest(word, &sug, maxcount, FALSE, need_cap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008116
8117 /* Make room in "gap". */
8118 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
8119 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
8120 return;
8121
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008122 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008123 {
8124 stp = &SUG(sug.su_ga, i);
8125
8126 /* The suggested word may replace only part of "word", add the not
8127 * replaced part. */
8128 wcopy = alloc(STRLEN(stp->st_word)
8129 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
8130 if (wcopy == NULL)
8131 break;
8132 STRCPY(wcopy, stp->st_word);
8133 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
8134 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
8135 }
8136
8137 spell_find_cleanup(&sug);
8138}
8139
8140/*
8141 * Find spell suggestions for the word at the start of "badptr".
8142 * Return the suggestions in "su->su_ga".
8143 * The maximum number of suggestions is "maxcount".
8144 * Note: does use info for the current window.
8145 * This is based on the mechanisms of Aspell, but completely reimplemented.
8146 */
8147 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008148spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008149 char_u *badptr;
8150 suginfo_T *su;
8151 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00008152 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008153 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008154{
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008155 int attr = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008156 char_u buf[MAXPATHL];
8157 char_u *p;
8158 int do_combine = FALSE;
8159 char_u *sps_copy;
8160#ifdef FEAT_EVAL
8161 static int expr_busy = FALSE;
8162#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008163 int c;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008164
8165 /*
8166 * Set the info in "*su".
8167 */
8168 vim_memset(su, 0, sizeof(suginfo_T));
8169 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
8170 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008171 if (*badptr == NUL)
8172 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008173 hash_init(&su->su_banned);
8174
8175 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008176 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008177 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008178 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008179
8180 if (su->su_badlen >= MAXWLEN)
8181 su->su_badlen = MAXWLEN - 1; /* just in case */
8182 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
8183 (void)spell_casefold(su->su_badptr, su->su_badlen,
8184 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008185 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008186 su->su_badflags = badword_captype(su->su_badptr,
8187 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008188 if (need_cap)
8189 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008190
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008191 /* If the word is not capitalised and spell_check() doesn't consider the
8192 * word to be bad then it might need to be capitalised. Add a suggestion
8193 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008194 c = PTR2CHAR(su->su_badptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008195 if (!SPELL_ISUPPER(c) && attr == 0)
8196 {
8197 make_case_word(su->su_badword, buf, WF_ONECAP);
8198 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
8199 0, TRUE);
8200 }
8201
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008202 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00008203 if (banbadword)
8204 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008205
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008206 /* Make a copy of 'spellsuggest', because the expression may change it. */
8207 sps_copy = vim_strsave(p_sps);
8208 if (sps_copy == NULL)
8209 return;
8210
8211 /* Loop over the items in 'spellsuggest'. */
8212 for (p = sps_copy; *p != NUL; )
8213 {
8214 copy_option_part(&p, buf, MAXPATHL, ",");
8215
8216 if (STRNCMP(buf, "expr:", 5) == 0)
8217 {
8218#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008219 /* Evaluate an expression. Skip this when called recursively,
8220 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008221 if (!expr_busy)
8222 {
8223 expr_busy = TRUE;
8224 spell_suggest_expr(su, buf + 5);
8225 expr_busy = FALSE;
8226 }
8227#endif
8228 }
8229 else if (STRNCMP(buf, "file:", 5) == 0)
8230 /* Use list of suggestions in a file. */
8231 spell_suggest_file(su, buf + 5);
8232 else
8233 {
8234 /* Use internal method. */
8235 spell_suggest_intern(su);
8236 if (sps_flags & SPS_DOUBLE)
8237 do_combine = TRUE;
8238 }
8239 }
8240
8241 vim_free(sps_copy);
8242
8243 if (do_combine)
8244 /* Combine the two list of suggestions. This must be done last,
8245 * because sorting changes the order again. */
8246 score_combine(su);
8247}
8248
8249#ifdef FEAT_EVAL
8250/*
8251 * Find suggestions by evaluating expression "expr".
8252 */
8253 static void
8254spell_suggest_expr(su, expr)
8255 suginfo_T *su;
8256 char_u *expr;
8257{
8258 list_T *list;
8259 listitem_T *li;
8260 int score;
8261 char_u *p;
8262
8263 /* The work is split up in a few parts to avoid having to export
8264 * suginfo_T.
8265 * First evaluate the expression and get the resulting list. */
8266 list = eval_spell_expr(su->su_badword, expr);
8267 if (list != NULL)
8268 {
8269 /* Loop over the items in the list. */
8270 for (li = list->lv_first; li != NULL; li = li->li_next)
8271 if (li->li_tv.v_type == VAR_LIST)
8272 {
8273 /* Get the word and the score from the items. */
8274 score = get_spellword(li->li_tv.vval.v_list, &p);
8275 if (score >= 0)
8276 add_suggestion(su, &su->su_ga, p,
8277 su->su_badlen, score, 0, TRUE);
8278 }
8279 list_unref(list);
8280 }
8281
8282 /* Sort the suggestions and truncate at "maxcount". */
8283 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8284}
8285#endif
8286
8287/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008288 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008289 */
8290 static void
8291spell_suggest_file(su, fname)
8292 suginfo_T *su;
8293 char_u *fname;
8294{
8295 FILE *fd;
8296 char_u line[MAXWLEN * 2];
8297 char_u *p;
8298 int len;
8299 char_u cword[MAXWLEN];
8300
8301 /* Open the file. */
8302 fd = mch_fopen((char *)fname, "r");
8303 if (fd == NULL)
8304 {
8305 EMSG2(_(e_notopen), fname);
8306 return;
8307 }
8308
8309 /* Read it line by line. */
8310 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
8311 {
8312 line_breakcheck();
8313
8314 p = vim_strchr(line, '/');
8315 if (p == NULL)
8316 continue; /* No Tab found, just skip the line. */
8317 *p++ = NUL;
8318 if (STRICMP(su->su_badword, line) == 0)
8319 {
8320 /* Match! Isolate the good word, until CR or NL. */
8321 for (len = 0; p[len] >= ' '; ++len)
8322 ;
8323 p[len] = NUL;
8324
8325 /* If the suggestion doesn't have specific case duplicate the case
8326 * of the bad word. */
8327 if (captype(p, NULL) == 0)
8328 {
8329 make_case_word(p, cword, su->su_badflags);
8330 p = cword;
8331 }
8332
8333 add_suggestion(su, &su->su_ga, p, su->su_badlen,
8334 SCORE_FILE, 0, TRUE);
8335 }
8336 }
8337
8338 fclose(fd);
8339
8340 /* Sort the suggestions and truncate at "maxcount". */
8341 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8342}
8343
8344/*
8345 * Find suggestions for the internal method indicated by "sps_flags".
8346 */
8347 static void
8348spell_suggest_intern(su)
8349 suginfo_T *su;
8350{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008351 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008352 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008353 *
8354 * Set a maximum score to limit the combination of operations that is
8355 * tried.
8356 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008357 suggest_try_special(su);
8358
8359 /*
8360 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
8361 * from the .aff file and inserting a space (split the word).
8362 */
8363 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008364
8365 /* For the resulting top-scorers compute the sound-a-like score. */
8366 if (sps_flags & SPS_DOUBLE)
8367 score_comp_sal(su);
8368
8369 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008370 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008371 *
8372 * Only do this when we don't have a lot of suggestions yet, because it's
8373 * very slow and often doesn't find new suggestions.
8374 */
8375 if ((sps_flags & SPS_DOUBLE)
8376 || (!(sps_flags & SPS_FAST)
8377 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
8378 {
8379 /* Allow a higher score now. */
8380 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008381 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008382 }
8383
8384 /* When CTRL-C was hit while searching do show the results. */
8385 ui_breakcheck();
8386 if (got_int)
8387 {
8388 (void)vgetc();
8389 got_int = FALSE;
8390 }
8391
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008392 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008393 {
8394 if (sps_flags & SPS_BEST)
8395 /* Adjust the word score for how it sounds like. */
8396 rescore_suggestions(su);
8397
8398 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008399 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008400 }
8401}
8402
8403/*
8404 * Free the info put in "*su" by spell_find_suggest().
8405 */
8406 static void
8407spell_find_cleanup(su)
8408 suginfo_T *su;
8409{
8410 int i;
8411
8412 /* Free the suggestions. */
8413 for (i = 0; i < su->su_ga.ga_len; ++i)
8414 vim_free(SUG(su->su_ga, i).st_word);
8415 ga_clear(&su->su_ga);
8416 for (i = 0; i < su->su_sga.ga_len; ++i)
8417 vim_free(SUG(su->su_sga, i).st_word);
8418 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008419
8420 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008421 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008422}
8423
8424/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008425 * Make a copy of "word", with the first letter upper or lower cased, to
8426 * "wcopy[MAXWLEN]". "word" must not be empty.
8427 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008428 */
8429 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008430onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008431 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008432 char_u *wcopy;
8433 int upper; /* TRUE: first letter made upper case */
8434{
8435 char_u *p;
8436 int c;
8437 int l;
8438
8439 p = word;
8440#ifdef FEAT_MBYTE
8441 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008442 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008443 else
8444#endif
8445 c = *p++;
8446 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008447 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008448 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008449 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008450#ifdef FEAT_MBYTE
8451 if (has_mbyte)
8452 l = mb_char2bytes(c, wcopy);
8453 else
8454#endif
8455 {
8456 l = 1;
8457 wcopy[0] = c;
8458 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008459 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008460}
8461
8462/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008463 * Make a copy of "word" with all the letters upper cased into
8464 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008465 */
8466 static void
8467allcap_copy(word, wcopy)
8468 char_u *word;
8469 char_u *wcopy;
8470{
8471 char_u *s;
8472 char_u *d;
8473 int c;
8474
8475 d = wcopy;
8476 for (s = word; *s != NUL; )
8477 {
8478#ifdef FEAT_MBYTE
8479 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008480 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008481 else
8482#endif
8483 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00008484
8485#ifdef FEAT_MBYTE
8486 /* We only change ß to SS when we are certain latin1 is used. It
8487 * would cause weird errors in other 8-bit encodings. */
8488 if (enc_latin1like && c == 0xdf)
8489 {
8490 c = 'S';
8491 if (d - wcopy >= MAXWLEN - 1)
8492 break;
8493 *d++ = c;
8494 }
8495 else
8496#endif
8497 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008498
8499#ifdef FEAT_MBYTE
8500 if (has_mbyte)
8501 {
8502 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
8503 break;
8504 d += mb_char2bytes(c, d);
8505 }
8506 else
8507#endif
8508 {
8509 if (d - wcopy >= MAXWLEN - 1)
8510 break;
8511 *d++ = c;
8512 }
8513 }
8514 *d = NUL;
8515}
8516
8517/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008518 * Try finding suggestions by recognizing specific situations.
8519 */
8520 static void
8521suggest_try_special(su)
8522 suginfo_T *su;
8523{
8524 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008525 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008526 int c;
8527 char_u word[MAXWLEN];
8528
8529 /*
8530 * Recognize a word that is repeated: "the the".
8531 */
8532 p = skiptowhite(su->su_fbadword);
8533 len = p - su->su_fbadword;
8534 p = skipwhite(p);
8535 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
8536 {
8537 /* Include badflags: if the badword is onecap or allcap
8538 * use that for the goodword too: "The the" -> "The". */
8539 c = su->su_fbadword[len];
8540 su->su_fbadword[len] = NUL;
8541 make_case_word(su->su_fbadword, word, su->su_badflags);
8542 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008543 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008544 }
8545}
8546
8547/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008548 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00008549 *
8550 * This uses a state machine. At each node in the tree we try various
8551 * operations. When trying if an operation work "depth" is increased and the
8552 * stack[] is used to store info. This allows combinations, thus insert one
8553 * character, replace one and delete another. The number of changes is
8554 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaard12a1322005-08-21 22:08:24 +00008555 *
8556 * After implementing this I noticed an article by Kemal Oflazer that
8557 * describes something similar: "Error-tolerant Finite State Recognition with
8558 * Applications to Morphological Analysis and Spelling Correction" (1996).
8559 * The implementation in the article is simplified and requires a stack of
8560 * unknown depth. The implementation here only needs a stack depth of the
8561 * length of the word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008562 */
8563 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00008564suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008565 suginfo_T *su;
8566{
8567 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
8568 char_u tword[MAXWLEN]; /* good word collected so far */
8569 trystate_T stack[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00008570 char_u preword[MAXWLEN * 3]; /* word found with proper case;
8571 * concatanation of prefix compound
8572 * words and split word. NUL terminated
8573 * when going deeper but not when coming
8574 * back. */
8575 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008576 trystate_T *sp;
8577 int newscore;
8578 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008579 char_u *byts, *fbyts, *pbyts;
8580 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008581 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008582 int c, c2, c3;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008583 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008584 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008585 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008586 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008587 int len;
8588 char_u *p;
8589 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00008590 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008591 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008592 slang_T *slang;
8593 int fword_ends;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008594
8595 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00008596 * to find matches (esp. REP items). Append some more text, changing
8597 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008598 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008599 n = STRLEN(fword);
8600 p = su->su_badptr + su->su_badlen;
8601 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008602
8603 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8604 lp->lp_slang != NULL; ++lp)
8605 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008606 slang = lp->lp_slang;
8607
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008608 /*
8609 * Go through the whole case-fold tree, try changes at each node.
8610 * "tword[]" contains the word collected from nodes in the tree.
8611 * "fword[]" the word we are trying to match with (initially the bad
8612 * word).
8613 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008614 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008615 sp = &stack[0];
Bram Moolenaar5195e452005-08-19 20:32:47 +00008616 vim_memset(sp, 0, sizeof(trystate_T));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008617 sp->ts_curi = 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008618
Bram Moolenaarea424162005-06-16 21:51:00 +00008619 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008620 * When there are postponed prefixes we need to use these first. At
8621 * the end of the prefix we continue in the case-fold tree.
8622 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008623 fbyts = slang->sl_fbyts;
8624 fidxs = slang->sl_fidxs;
8625 pbyts = slang->sl_pbyts;
8626 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008627 if (pbyts != NULL)
8628 {
8629 byts = pbyts;
8630 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008631 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008632 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
8633 }
8634 else
8635 {
8636 byts = fbyts;
8637 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008638 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +00008639 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008640 }
8641
8642 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00008643 * Loop to find all suggestions. At each round we either:
8644 * - For the current state try one operation, advance "ts_curi",
8645 * increase "depth".
8646 * - When a state is done go to the next, set "ts_state".
8647 * - When all states are tried decrease "depth".
8648 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008649 while (depth >= 0 && !got_int)
8650 {
8651 sp = &stack[depth];
8652 switch (sp->ts_state)
8653 {
8654 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008655 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008656 /*
8657 * Start of node: Deal with NUL bytes, which means
8658 * tword[] may end here.
8659 */
8660 arridx = sp->ts_arridx; /* current node in the tree */
8661 len = byts[arridx]; /* bytes in this node */
8662 arridx += sp->ts_curi; /* index of current byte */
8663
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008664 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008665 {
8666 /* Skip over the NUL bytes, we use them later. */
8667 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
8668 ;
8669 sp->ts_curi += n;
8670
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008671 /* Always past NUL bytes now. */
8672 n = (int)sp->ts_state;
8673 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00008674 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008675
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008676 /* At end of a prefix or at start of prefixtree: check for
8677 * following word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008678 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008679 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00008680 /* Set su->su_badflags to the caps type at this
8681 * position. Use the caps type until here for the
8682 * prefix itself. */
8683#ifdef FEAT_MBYTE
8684 if (has_mbyte)
8685 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
8686 else
8687#endif
8688 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008689 flags = badword_captype(su->su_badptr,
8690 su->su_badptr + n);
8691 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00008692 su->su_badptr + su->su_badlen);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008693 ++depth;
8694 stack[depth] = stack[depth - 1];
8695 sp = &stack[depth];
8696 sp->ts_prefixdepth = depth - 1;
8697 byts = fbyts;
8698 idxs = fidxs;
8699 sp->ts_state = STATE_START;
8700 sp->ts_curi = 1; /* start just after length byte */
8701 sp->ts_arridx = 0;
8702
Bram Moolenaar53805d12005-08-01 07:08:33 +00008703 /* Move the prefix to preword[] with the right case
8704 * and make find_keepcap_word() works. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00008705 tword[sp->ts_twordlen] = NUL;
8706 make_case_word(tword + sp->ts_splitoff,
8707 preword + sp->ts_prewordlen,
8708 flags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008709 sp->ts_prewordlen = STRLEN(preword);
Bram Moolenaard12a1322005-08-21 22:08:24 +00008710 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008711 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008712 break;
8713 }
8714
Bram Moolenaar0c405862005-06-22 22:26:26 +00008715 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008716 {
8717 /* Past bytes in node and/or past NUL bytes. */
8718 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00008719 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008720 break;
8721 }
8722
8723 /*
8724 * End of word in tree.
8725 */
8726 ++sp->ts_curi; /* eat one NUL byte */
8727
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008728 flags = (int)idxs[arridx];
Bram Moolenaar5195e452005-08-19 20:32:47 +00008729 fword_ends = (fword[sp->ts_fidx] == NUL
8730 || !spell_iswordp(fword + sp->ts_fidx, curbuf));
8731 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008732
Bram Moolenaard12a1322005-08-21 22:08:24 +00008733 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
8734 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008735 {
8736 /* There was a prefix before the word. Check that the
8737 * prefix can be used with this word. */
8738 /* Count the length of the NULs in the prefix. If there
8739 * are none this must be the first try without a prefix.
8740 */
8741 n = stack[sp->ts_prefixdepth].ts_arridx;
8742 len = pbyts[n++];
8743 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
8744 ;
8745 if (c > 0)
8746 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008747 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00008748 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008749 if (c == 0)
8750 break;
8751
8752 /* Use the WF_RARE flag for a rare prefix. */
8753 if (c & WF_RAREPFX)
8754 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00008755
8756 /* Tricky: when checking for both prefix and
8757 * compounding we run into the prefix flag first.
8758 * Remember that it's OK, so that we accept the prefix
8759 * when arriving at a compound flag. */
8760 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008761 }
8762 }
8763
Bram Moolenaard12a1322005-08-21 22:08:24 +00008764 if (sp->ts_complen > sp->ts_compsplit)
8765 {
Bram Moolenaar78622822005-08-23 21:00:13 +00008766 if (slang->sl_nobreak)
8767 {
8768 /* There was a word before this word. When there was
8769 * no change in this word (it was correct) add the
8770 * first word as a suggestion. If this word was
8771 * corrected too, we need to check if a correct word
8772 * follows. */
8773 if (sp->ts_fidx - sp->ts_splitfidx
8774 == sp->ts_twordlen - sp->ts_splitoff
8775 && STRNCMP(fword + sp->ts_splitfidx,
8776 tword + sp->ts_splitoff,
8777 sp->ts_fidx - sp->ts_splitfidx) == 0)
8778 {
8779 preword[sp->ts_prewordlen] = NUL;
8780 add_suggestion(su, &su->su_ga, preword,
8781 sp->ts_splitfidx - repextra,
8782 sp->ts_score, 0, FALSE);
8783 break;
8784 }
8785 }
8786 else
8787 {
8788 /* There was a compound word before this word. If
8789 * this word does not support compounding then give up
8790 * (splitting is tried for the word without compound
8791 * flag). */
8792 if (((unsigned)flags >> 24) == 0
8793 || sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaard12a1322005-08-21 22:08:24 +00008794 < slang->sl_compminlen)
Bram Moolenaar78622822005-08-23 21:00:13 +00008795 break;
8796 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
8797 compflags[sp->ts_complen + 1] = NUL;
8798 vim_strncpy(preword + sp->ts_prewordlen,
8799 tword + sp->ts_splitoff,
8800 sp->ts_twordlen - sp->ts_splitoff);
8801 p = preword;
8802 while (*skiptowhite(p) != NUL)
8803 p = skipwhite(skiptowhite(p));
8804 if (fword_ends && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00008805 compflags + sp->ts_compsplit))
Bram Moolenaar78622822005-08-23 21:00:13 +00008806 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00008807
Bram Moolenaar78622822005-08-23 21:00:13 +00008808 /* Get pointer to last char of previous word. */
8809 p = preword + sp->ts_prewordlen;
8810 mb_ptr_back(preword, p);
8811 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00008812 }
Bram Moolenaare52325c2005-08-22 22:54:29 +00008813 else
8814 p = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00008815
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008816 /*
8817 * Form the word with proper case in preword.
8818 * If there is a word from a previous split, append.
8819 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008820 if (flags & WF_KEEPCAP)
8821 /* Must find the word in the keep-case tree. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008822 find_keepcap_word(slang, tword + sp->ts_splitoff,
8823 preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008824 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00008825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008826 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00008827 * use that for the goodword too. But if the badword is
8828 * allcap and it's only one char long use onecap. */
8829 c = su->su_badflags;
8830 if ((c & WF_ALLCAP)
8831#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008832 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008833#else
8834 && su->su_badlen == 1
8835#endif
8836 )
8837 c = WF_ONECAP;
Bram Moolenaare52325c2005-08-22 22:54:29 +00008838 c |= flags;
8839
8840 /* When appending a compound word after a word character
8841 * don't use Onecap. */
8842 if (p != NULL && spell_iswordp_nmw(p))
8843 c &= ~WF_ONECAP;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008844 make_case_word(tword + sp->ts_splitoff,
Bram Moolenaare52325c2005-08-22 22:54:29 +00008845 preword + sp->ts_prewordlen, c);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008846 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008847
8848 /* Don't use a banned word. It may appear again as a good
8849 * word, thus remember it. */
8850 if (flags & WF_BANNED)
8851 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008852 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008853 break;
8854 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008855 if (was_banned(su, preword + sp->ts_prewordlen)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008856 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008857 break;
8858
8859 newscore = 0;
8860 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008861 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008862 newscore += SCORE_REGION;
8863 if (flags & WF_RARE)
8864 newscore += SCORE_RARE;
8865
Bram Moolenaar0c405862005-06-22 22:26:26 +00008866 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00008867 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008868 newscore += SCORE_ICASE;
8869
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008870 if (fword_ends && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008871 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008872 /* The badword also ends: add suggestions. Give a penalty
8873 * when changing non-word char to word char, e.g., "thes,"
8874 * -> "these". */
8875 p = fword + sp->ts_fidx;
8876#ifdef FEAT_MBYTE
8877 if (has_mbyte)
8878 mb_ptr_back(fword, p);
8879 else
8880#endif
8881 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008882 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008883 {
8884 p = preword + STRLEN(preword);
8885#ifdef FEAT_MBYTE
8886 if (has_mbyte)
8887 mb_ptr_back(preword, p);
8888 else
8889#endif
8890 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008891 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008892 newscore += SCORE_NONWORD;
8893 }
8894
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008895 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008896 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008897 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008898 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008899 else if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +00008900#ifdef FEAT_MBYTE
8901 /* Don't split halfway a character. */
8902 && (!has_mbyte || sp->ts_tcharlen == 0)
8903#endif
8904 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008905 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008906 int try_compound;
8907
8908 /* Get here in two situations:
8909 * 1. The word in the tree ends but the badword continues:
8910 * If the word allows compounding try that. Otherwise
8911 * try a split by inserting a space. For both check
8912 * that a valid words starts at fword[sp->ts_fidx].
Bram Moolenaar78622822005-08-23 21:00:13 +00008913 * For NOBREAK do like compounding to be able to check
8914 * if the next word is valid.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008915 * 2. The badword does end, but it was due to a change
8916 * (e.g., a swap). No need to split, but do check that
8917 * the following word is valid.
8918 */
Bram Moolenaard12a1322005-08-21 22:08:24 +00008919 try_compound = FALSE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008920 if (!fword_ends
Bram Moolenaar5195e452005-08-19 20:32:47 +00008921 && ((unsigned)flags >> 24) != 0
8922 && sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008923 >= slang->sl_compminlen
Bram Moolenaare52325c2005-08-22 22:54:29 +00008924 && (slang->sl_compsylmax < MAXWLEN
8925 || sp->ts_complen + 1 - sp->ts_compsplit
8926 < slang->sl_compmax)
Bram Moolenaard12a1322005-08-21 22:08:24 +00008927 && (vim_strchr(sp->ts_complen == sp->ts_compsplit
8928 ? slang->sl_compstartflags
8929 : slang->sl_compallflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00008930 ((unsigned)flags >> 24)) != NULL))
8931 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008932 try_compound = TRUE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008933 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
8934 compflags[sp->ts_complen + 1] = NUL;
8935 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00008936
Bram Moolenaar78622822005-08-23 21:00:13 +00008937 /* For NOBREAK we never try splitting, it won't make any
8938 * word valid. */
8939 if (slang->sl_nobreak)
8940 try_compound = TRUE;
8941
Bram Moolenaard12a1322005-08-21 22:08:24 +00008942 /* If we could add a compound word, and it's also possible
8943 * to split at this point, do the split first and set
8944 * TSF_DIDSPLIT to avoid doing it again. */
Bram Moolenaar78622822005-08-23 21:00:13 +00008945 else if (!fword_ends
Bram Moolenaard12a1322005-08-21 22:08:24 +00008946 && try_compound
8947 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008948 {
8949 try_compound = FALSE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00008950 sp->ts_flags |= TSF_DIDSPLIT;
8951 --sp->ts_curi; /* do the same NUL again */
8952 compflags[sp->ts_complen] = NUL;
8953 }
8954 else
8955 sp->ts_flags &= ~TSF_DIDSPLIT;
8956
8957 if (!try_compound && !fword_ends)
8958 {
8959 /* If we're going to split need to check that the
8960 * words so far are valid for compounding. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00008961 p = preword;
8962 while (*skiptowhite(p) != NUL)
8963 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +00008964 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +00008965 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00008966 compflags + sp->ts_compsplit))
8967 break;
8968 newscore += SCORE_SPLIT;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008969 }
8970
8971 if (try_deeper(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008972 {
8973 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008974 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008975 sp->ts_state = STATE_SPLITUNDO;
8976
8977 ++depth;
8978 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008979
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008980 /* Append a space to preword when splitting. */
8981 if (!try_compound && !fword_ends)
8982 STRCAT(preword, " ");
Bram Moolenaar5195e452005-08-19 20:32:47 +00008983 sp->ts_prewordlen = STRLEN(preword);
8984 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +00008985 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008986
8987 /* If the badword has a non-word character at this
8988 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008989 * non-word character with a space. Always skip a
8990 * character when the word ends. */
8991 if ((!try_compound
8992 && !spell_iswordp_nmw(fword + sp->ts_fidx))
8993 || fword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008994 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008995 int l;
8996
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008997#ifdef FEAT_MBYTE
8998 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008999 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009000 else
9001#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009002 l = 1;
9003 if (fword_ends)
9004 {
9005 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009006 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009007 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009008 sp->ts_prewordlen += l;
9009 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009010 }
9011 else
9012 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
9013 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009014 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00009015
Bram Moolenaard12a1322005-08-21 22:08:24 +00009016 /* When compounding include compound flag in
9017 * compflags[] (already set above). When splitting we
9018 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009019 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +00009020 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009021 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00009022 sp->ts_compsplit = sp->ts_complen;
9023 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009024
Bram Moolenaar53805d12005-08-01 07:08:33 +00009025 /* set su->su_badflags to the caps type at this
9026 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009027#ifdef FEAT_MBYTE
9028 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00009029 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009030 else
9031#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00009032 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009033 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00009034 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009035
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009036 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009037 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009038
9039 /* If there are postponed prefixes, try these too. */
9040 if (pbyts != NULL)
9041 {
9042 byts = pbyts;
9043 idxs = pidxs;
9044 sp->ts_prefixdepth = PFD_PREFIXTREE;
9045 sp->ts_state = STATE_NOPREFIX;
9046 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009047 }
9048 }
9049 break;
9050
9051 case STATE_SPLITUNDO:
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009052 /* Undo the changes done for word split or compound word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009053 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009054
9055 /* Continue looking for NUL bytes. */
9056 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009057
9058 /* In case we went into the prefix tree. */
9059 byts = fbyts;
9060 idxs = fidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009061 break;
9062
9063 case STATE_ENDNUL:
9064 /* Past the NUL bytes in the node. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00009065 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009066 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009067 {
9068 /* The badword ends, can't use the bytes in this node. */
9069 sp->ts_state = STATE_DEL;
9070 break;
9071 }
9072 sp->ts_state = STATE_PLAIN;
9073 /*FALLTHROUGH*/
9074
9075 case STATE_PLAIN:
9076 /*
9077 * Go over all possible bytes at this node, add each to
9078 * tword[] and use child node. "ts_curi" is the index.
9079 */
9080 arridx = sp->ts_arridx;
9081 if (sp->ts_curi > byts[arridx])
9082 {
9083 /* Done all bytes at this node, do next state. When still
9084 * at already changed bytes skip the other tricks. */
9085 if (sp->ts_fidx >= sp->ts_fidxtry)
9086 sp->ts_state = STATE_DEL;
9087 else
9088 sp->ts_state = STATE_FINAL;
9089 }
9090 else
9091 {
9092 arridx += sp->ts_curi++;
9093 c = byts[arridx];
9094
9095 /* Normal byte, go one level deeper. If it's not equal to
9096 * the byte in the bad word adjust the score. But don't
9097 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00009098 if (c == fword[sp->ts_fidx]
9099#ifdef FEAT_MBYTE
9100 || (sp->ts_tcharlen > 0
9101 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009102#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00009103 )
9104 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009105 else
9106 newscore = SCORE_SUBST;
9107 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
9108 && try_deeper(su, stack, depth, newscore))
9109 {
9110 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009111 sp = &stack[depth];
9112 ++sp->ts_fidx;
9113 tword[sp->ts_twordlen++] = c;
9114 sp->ts_arridx = idxs[arridx];
9115#ifdef FEAT_MBYTE
9116 if (newscore == SCORE_SUBST)
9117 sp->ts_isdiff = DIFF_YES;
9118 if (has_mbyte)
9119 {
9120 /* Multi-byte characters are a bit complicated to
9121 * handle: They differ when any of the bytes
9122 * differ and then their length may also differ. */
9123 if (sp->ts_tcharlen == 0)
9124 {
9125 /* First byte. */
9126 sp->ts_tcharidx = 0;
9127 sp->ts_tcharlen = MB_BYTE2LEN(c);
9128 sp->ts_fcharstart = sp->ts_fidx - 1;
9129 sp->ts_isdiff = (newscore != 0)
9130 ? DIFF_YES : DIFF_NONE;
9131 }
9132 else if (sp->ts_isdiff == DIFF_INSERT)
9133 /* When inserting trail bytes don't advance in
9134 * the bad word. */
9135 --sp->ts_fidx;
9136 if (++sp->ts_tcharidx == sp->ts_tcharlen)
9137 {
9138 /* Last byte of character. */
9139 if (sp->ts_isdiff == DIFF_YES)
9140 {
9141 /* Correct ts_fidx for the byte length of
9142 * the character (we didn't check that
9143 * before). */
9144 sp->ts_fidx = sp->ts_fcharstart
9145 + MB_BYTE2LEN(
9146 fword[sp->ts_fcharstart]);
9147
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00009148 /* For changing a composing character
9149 * adjust the score from SCORE_SUBST to
9150 * SCORE_SUBCOMP. */
9151 if (enc_utf8
9152 && utf_iscomposing(
9153 mb_ptr2char(tword
9154 + sp->ts_twordlen
9155 - sp->ts_tcharlen))
9156 && utf_iscomposing(
9157 mb_ptr2char(fword
9158 + sp->ts_fcharstart)))
9159 sp->ts_score -=
9160 SCORE_SUBST - SCORE_SUBCOMP;
9161
Bram Moolenaarea424162005-06-16 21:51:00 +00009162 /* For a similar character adjust score
9163 * from SCORE_SUBST to SCORE_SIMILAR. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009164 else if (slang->sl_has_map
9165 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00009166 mb_ptr2char(tword
9167 + sp->ts_twordlen
9168 - sp->ts_tcharlen),
9169 mb_ptr2char(fword
9170 + sp->ts_fcharstart)))
9171 sp->ts_score -=
9172 SCORE_SUBST - SCORE_SIMILAR;
9173 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009174 else if (sp->ts_isdiff == DIFF_INSERT
9175 && sp->ts_twordlen > sp->ts_tcharlen)
9176 {
Bram Moolenaarea408852005-06-25 22:49:46 +00009177 p = tword + sp->ts_twordlen
9178 - sp->ts_tcharlen;
9179 c = mb_ptr2char(p);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009180 if (enc_utf8 && utf_iscomposing(c))
9181 {
9182 /* Inserting a composing char doesn't
9183 * count that much. */
Bram Moolenaarea408852005-06-25 22:49:46 +00009184 sp->ts_score -= SCORE_INS
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009185 - SCORE_INSCOMP;
9186 }
9187 else
9188 {
9189 /* If the previous character was the
9190 * same, thus doubling a character,
9191 * give a bonus to the score. */
9192 mb_ptr_back(tword, p);
9193 if (c == mb_ptr2char(p))
9194 sp->ts_score -= SCORE_INS
Bram Moolenaarea408852005-06-25 22:49:46 +00009195 - SCORE_INSDUP;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009196 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009197 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009198
9199 /* Starting a new char, reset the length. */
9200 sp->ts_tcharlen = 0;
9201 }
9202 }
9203 else
9204#endif
9205 {
9206 /* If we found a similar char adjust the score.
9207 * We do this after calling try_deeper() because
9208 * it's slow. */
9209 if (newscore != 0
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009210 && slang->sl_has_map
9211 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00009212 c, fword[sp->ts_fidx - 1]))
9213 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
9214 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009215 }
9216 }
9217 break;
9218
9219 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00009220#ifdef FEAT_MBYTE
9221 /* When past the first byte of a multi-byte char don't try
9222 * delete/insert/swap a character. */
9223 if (has_mbyte && sp->ts_tcharlen > 0)
9224 {
9225 sp->ts_state = STATE_FINAL;
9226 break;
9227 }
9228#endif
9229 /*
9230 * Try skipping one character in the bad word (delete it).
9231 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009232 sp->ts_state = STATE_INS;
9233 sp->ts_curi = 1;
9234 if (fword[sp->ts_fidx] != NUL
9235 && try_deeper(su, stack, depth, SCORE_DEL))
9236 {
9237 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00009238
9239 /* Advance over the character in fword[]. Give a bonus to
9240 * the score if the same character is following "nn" ->
9241 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00009242#ifdef FEAT_MBYTE
9243 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00009244 {
9245 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00009246 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00009247 if (enc_utf8 && utf_iscomposing(c))
9248 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
9249 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
Bram Moolenaarea408852005-06-25 22:49:46 +00009250 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
9251 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009252 else
9253#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00009254 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009255 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00009256 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
9257 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
9258 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009259 break;
9260 }
9261 /*FALLTHROUGH*/
9262
9263 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00009264 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009265 * node. */
9266 n = sp->ts_arridx;
9267 if (sp->ts_curi > byts[n])
9268 {
9269 /* Done all bytes at this node, do next state. */
9270 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009271 }
9272 else
9273 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009274 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009275 n += sp->ts_curi++;
9276 c = byts[n];
9277 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
9278 {
9279 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009280 sp = &stack[depth];
9281 tword[sp->ts_twordlen++] = c;
9282 sp->ts_arridx = idxs[n];
9283#ifdef FEAT_MBYTE
9284 if (has_mbyte)
9285 {
9286 fl = MB_BYTE2LEN(c);
9287 if (fl > 1)
9288 {
9289 /* There are following bytes for the same
9290 * character. We must find all bytes before
9291 * trying delete/insert/swap/etc. */
9292 sp->ts_tcharlen = fl;
9293 sp->ts_tcharidx = 1;
9294 sp->ts_isdiff = DIFF_INSERT;
9295 }
9296 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009297 else
9298 fl = 1;
9299 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00009300#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00009301 {
9302 /* If the previous character was the same, thus
9303 * doubling a character, give a bonus to the
9304 * score. */
9305 if (sp->ts_twordlen >= 2
9306 && tword[sp->ts_twordlen - 2] == c)
9307 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
9308 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009309 }
9310 }
9311 break;
9312
9313 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00009314 /*
9315 * Swap two bytes in the bad word: "12" -> "21".
9316 * We change "fword" here, it's changed back afterwards.
9317 */
9318 p = fword + sp->ts_fidx;
9319 c = *p;
9320 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009321 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009322 /* End of word, can't swap or replace. */
9323 sp->ts_state = STATE_FINAL;
9324 break;
9325 }
9326#ifdef FEAT_MBYTE
9327 if (has_mbyte)
9328 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009329 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009330 c = mb_ptr2char(p);
9331 c2 = mb_ptr2char(p + n);
9332 }
9333 else
9334#endif
9335 c2 = p[1];
9336 if (c == c2)
9337 {
9338 /* Characters are identical, swap won't do anything. */
9339 sp->ts_state = STATE_SWAP3;
9340 break;
9341 }
9342 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
9343 {
9344 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009345 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009346#ifdef FEAT_MBYTE
9347 if (has_mbyte)
9348 {
9349 fl = mb_char2len(c2);
9350 mch_memmove(p, p + n, fl);
9351 mb_char2bytes(c, p + fl);
9352 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
9353 }
9354 else
9355#endif
9356 {
9357 p[0] = c2;
9358 p[1] = c;
9359 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
9360 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009361 }
9362 else
9363 /* If this swap doesn't work then SWAP3 won't either. */
9364 sp->ts_state = STATE_REP_INI;
9365 break;
9366
Bram Moolenaarea424162005-06-16 21:51:00 +00009367 case STATE_UNSWAP:
9368 /* Undo the STATE_SWAP swap: "21" -> "12". */
9369 p = fword + sp->ts_fidx;
9370#ifdef FEAT_MBYTE
9371 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009372 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009373 n = MB_BYTE2LEN(*p);
9374 c = mb_ptr2char(p + n);
9375 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
9376 mb_char2bytes(c, p);
9377 }
9378 else
9379#endif
9380 {
9381 c = *p;
9382 *p = p[1];
9383 p[1] = c;
9384 }
9385 /*FALLTHROUGH*/
9386
9387 case STATE_SWAP3:
9388 /* Swap two bytes, skipping one: "123" -> "321". We change
9389 * "fword" here, it's changed back afterwards. */
9390 p = fword + sp->ts_fidx;
9391#ifdef FEAT_MBYTE
9392 if (has_mbyte)
9393 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009394 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009395 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009396 fl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00009397 c2 = mb_ptr2char(p + n);
9398 c3 = mb_ptr2char(p + n + fl);
9399 }
9400 else
9401#endif
9402 {
9403 c = *p;
9404 c2 = p[1];
9405 c3 = p[2];
9406 }
9407
9408 /* When characters are identical: "121" then SWAP3 result is
9409 * identical, ROT3L result is same as SWAP: "211", ROT3L
9410 * result is same as SWAP on next char: "112". Thus skip all
9411 * swapping. Also skip when c3 is NUL. */
9412 if (c == c3 || c3 == NUL)
9413 {
9414 sp->ts_state = STATE_REP_INI;
9415 break;
9416 }
9417 if (try_deeper(su, stack, depth, SCORE_SWAP3))
9418 {
9419 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009420 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009421#ifdef FEAT_MBYTE
9422 if (has_mbyte)
9423 {
9424 tl = mb_char2len(c3);
9425 mch_memmove(p, p + n + fl, tl);
9426 mb_char2bytes(c2, p + tl);
9427 mb_char2bytes(c, p + fl + tl);
9428 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
9429 }
9430 else
9431#endif
9432 {
9433 p[0] = p[2];
9434 p[2] = c;
9435 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
9436 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009437 }
9438 else
9439 sp->ts_state = STATE_REP_INI;
9440 break;
9441
Bram Moolenaarea424162005-06-16 21:51:00 +00009442 case STATE_UNSWAP3:
9443 /* Undo STATE_SWAP3: "321" -> "123" */
9444 p = fword + sp->ts_fidx;
9445#ifdef FEAT_MBYTE
9446 if (has_mbyte)
9447 {
9448 n = MB_BYTE2LEN(*p);
9449 c2 = mb_ptr2char(p + n);
9450 fl = MB_BYTE2LEN(p[n]);
9451 c = mb_ptr2char(p + n + fl);
9452 tl = MB_BYTE2LEN(p[n + fl]);
9453 mch_memmove(p + fl + tl, p, n);
9454 mb_char2bytes(c, p);
9455 mb_char2bytes(c2, p + tl);
9456 }
9457 else
9458#endif
9459 {
9460 c = *p;
9461 *p = p[2];
9462 p[2] = c;
9463 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009464
Bram Moolenaarea424162005-06-16 21:51:00 +00009465 /* Rotate three characters left: "123" -> "231". We change
9466 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009467 if (try_deeper(su, stack, depth, SCORE_SWAP3))
9468 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009469 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009470 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009471 p = fword + sp->ts_fidx;
9472#ifdef FEAT_MBYTE
9473 if (has_mbyte)
9474 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009475 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009476 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009477 fl = mb_cptr2len(p + n);
9478 fl += mb_cptr2len(p + n + fl);
Bram Moolenaarea424162005-06-16 21:51:00 +00009479 mch_memmove(p, p + n, fl);
9480 mb_char2bytes(c, p + fl);
9481 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
9482 }
9483 else
9484#endif
9485 {
9486 c = *p;
9487 *p = p[1];
9488 p[1] = p[2];
9489 p[2] = c;
9490 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
9491 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009492 }
9493 else
9494 sp->ts_state = STATE_REP_INI;
9495 break;
9496
Bram Moolenaarea424162005-06-16 21:51:00 +00009497 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00009498 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00009499 p = fword + sp->ts_fidx;
9500#ifdef FEAT_MBYTE
9501 if (has_mbyte)
9502 {
9503 n = MB_BYTE2LEN(*p);
9504 n += MB_BYTE2LEN(p[n]);
9505 c = mb_ptr2char(p + n);
9506 tl = MB_BYTE2LEN(p[n]);
9507 mch_memmove(p + tl, p, n);
9508 mb_char2bytes(c, p);
9509 }
9510 else
9511#endif
9512 {
9513 c = p[2];
9514 p[2] = p[1];
9515 p[1] = *p;
9516 *p = c;
9517 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009518
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009519 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00009520 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009521 if (try_deeper(su, stack, depth, SCORE_SWAP3))
9522 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009523 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009524 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009525 p = fword + sp->ts_fidx;
9526#ifdef FEAT_MBYTE
9527 if (has_mbyte)
9528 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009529 n = mb_cptr2len(p);
9530 n += mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00009531 c = mb_ptr2char(p + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009532 tl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00009533 mch_memmove(p + tl, p, n);
9534 mb_char2bytes(c, p);
9535 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
9536 }
9537 else
9538#endif
9539 {
9540 c = p[2];
9541 p[2] = p[1];
9542 p[1] = *p;
9543 *p = c;
9544 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
9545 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009546 }
9547 else
9548 sp->ts_state = STATE_REP_INI;
9549 break;
9550
Bram Moolenaarea424162005-06-16 21:51:00 +00009551 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00009552 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00009553 p = fword + sp->ts_fidx;
9554#ifdef FEAT_MBYTE
9555 if (has_mbyte)
9556 {
9557 c = mb_ptr2char(p);
9558 tl = MB_BYTE2LEN(*p);
9559 n = MB_BYTE2LEN(p[tl]);
9560 n += MB_BYTE2LEN(p[tl + n]);
9561 mch_memmove(p, p + tl, n);
9562 mb_char2bytes(c, p + n);
9563 }
9564 else
9565#endif
9566 {
9567 c = *p;
9568 *p = p[1];
9569 p[1] = p[2];
9570 p[2] = c;
9571 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009572 /*FALLTHROUGH*/
9573
9574 case STATE_REP_INI:
9575 /* Check if matching with REP items from the .aff file would
9576 * work. Quickly skip if there are no REP items or the score
9577 * is going to be too high anyway. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009578 gap = &slang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009579 if (gap->ga_len == 0
9580 || sp->ts_score + SCORE_REP >= su->su_maxscore)
9581 {
9582 sp->ts_state = STATE_FINAL;
9583 break;
9584 }
9585
9586 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00009587 * may match. If the index is -1 there is none. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009588 sp->ts_curi = slang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009589 if (sp->ts_curi < 0)
9590 {
9591 sp->ts_state = STATE_FINAL;
9592 break;
9593 }
9594
9595 sp->ts_state = STATE_REP;
9596 /*FALLTHROUGH*/
9597
9598 case STATE_REP:
9599 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00009600 * match replace the characters and check if the resulting
9601 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009602 p = fword + sp->ts_fidx;
9603
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009604 gap = &slang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009605 while (sp->ts_curi < gap->ga_len)
9606 {
9607 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
9608 if (*ftp->ft_from != *p)
9609 {
9610 /* past possible matching entries */
9611 sp->ts_curi = gap->ga_len;
9612 break;
9613 }
9614 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
9615 && try_deeper(su, stack, depth, SCORE_REP))
9616 {
9617 /* Need to undo this afterwards. */
9618 sp->ts_state = STATE_REP_UNDO;
9619
9620 /* Change the "from" to the "to" string. */
9621 ++depth;
9622 fl = STRLEN(ftp->ft_from);
9623 tl = STRLEN(ftp->ft_to);
9624 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00009625 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009626 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009627 repextra += tl - fl;
9628 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009629 mch_memmove(p, ftp->ft_to, tl);
9630 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00009631#ifdef FEAT_MBYTE
9632 stack[depth].ts_tcharlen = 0;
9633#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009634 break;
9635 }
9636 }
9637
9638 if (sp->ts_curi >= gap->ga_len)
9639 /* No (more) matches. */
9640 sp->ts_state = STATE_FINAL;
9641
9642 break;
9643
9644 case STATE_REP_UNDO:
9645 /* Undo a REP replacement and continue with the next one. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009646 ftp = (fromto_T *)slang->sl_rep.ga_data + sp->ts_curi - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009647 fl = STRLEN(ftp->ft_from);
9648 tl = STRLEN(ftp->ft_to);
9649 p = fword + sp->ts_fidx;
9650 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00009651 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009652 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009653 repextra -= tl - fl;
9654 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009655 mch_memmove(p, ftp->ft_from, fl);
9656 sp->ts_state = STATE_REP;
9657 break;
9658
9659 default:
9660 /* Did all possible states at this level, go up one level. */
9661 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009662
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009663 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009664 {
9665 /* Continue in or go back to the prefix tree. */
9666 byts = pbyts;
9667 idxs = pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009668 }
9669
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009670 /* Don't check for CTRL-C too often, it takes time. */
9671 line_breakcheck();
9672 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009673 }
9674 }
9675}
9676
9677/*
9678 * Try going one level deeper in the tree.
9679 */
9680 static int
9681try_deeper(su, stack, depth, score_add)
9682 suginfo_T *su;
9683 trystate_T *stack;
9684 int depth;
9685 int score_add;
9686{
9687 int newscore;
9688
9689 /* Refuse to go deeper if the scrore is getting too big. */
9690 newscore = stack[depth].ts_score + score_add;
9691 if (newscore >= su->su_maxscore)
9692 return FALSE;
9693
Bram Moolenaarea424162005-06-16 21:51:00 +00009694 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009695 stack[depth + 1].ts_state = STATE_START;
9696 stack[depth + 1].ts_score = newscore;
9697 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +00009698 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009699 return TRUE;
9700}
9701
Bram Moolenaar53805d12005-08-01 07:08:33 +00009702#ifdef FEAT_MBYTE
9703/*
9704 * Case-folding may change the number of bytes: Count nr of chars in
9705 * fword[flen] and return the byte length of that many chars in "word".
9706 */
9707 static int
9708nofold_len(fword, flen, word)
9709 char_u *fword;
9710 int flen;
9711 char_u *word;
9712{
9713 char_u *p;
9714 int i = 0;
9715
9716 for (p = fword; p < fword + flen; mb_ptr_adv(p))
9717 ++i;
9718 for (p = word; i > 0; mb_ptr_adv(p))
9719 --i;
9720 return (int)(p - word);
9721}
9722#endif
9723
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009724/*
9725 * "fword" is a good word with case folded. Find the matching keep-case
9726 * words and put it in "kword".
9727 * Theoretically there could be several keep-case words that result in the
9728 * same case-folded word, but we only find one...
9729 */
9730 static void
9731find_keepcap_word(slang, fword, kword)
9732 slang_T *slang;
9733 char_u *fword;
9734 char_u *kword;
9735{
9736 char_u uword[MAXWLEN]; /* "fword" in upper-case */
9737 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009738 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009739
9740 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009741 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009742 int round[MAXWLEN];
9743 int fwordidx[MAXWLEN];
9744 int uwordidx[MAXWLEN];
9745 int kwordlen[MAXWLEN];
9746
9747 int flen, ulen;
9748 int l;
9749 int len;
9750 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009751 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009752 char_u *p;
9753 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009754 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009755
9756 if (byts == NULL)
9757 {
9758 /* array is empty: "cannot happen" */
9759 *kword = NUL;
9760 return;
9761 }
9762
9763 /* Make an all-cap version of "fword". */
9764 allcap_copy(fword, uword);
9765
9766 /*
9767 * Each character needs to be tried both case-folded and upper-case.
9768 * All this gets very complicated if we keep in mind that changing case
9769 * may change the byte length of a multi-byte character...
9770 */
9771 depth = 0;
9772 arridx[0] = 0;
9773 round[0] = 0;
9774 fwordidx[0] = 0;
9775 uwordidx[0] = 0;
9776 kwordlen[0] = 0;
9777 while (depth >= 0)
9778 {
9779 if (fword[fwordidx[depth]] == NUL)
9780 {
9781 /* We are at the end of "fword". If the tree allows a word to end
9782 * here we have found a match. */
9783 if (byts[arridx[depth] + 1] == 0)
9784 {
9785 kword[kwordlen[depth]] = NUL;
9786 return;
9787 }
9788
9789 /* kword is getting too long, continue one level up */
9790 --depth;
9791 }
9792 else if (++round[depth] > 2)
9793 {
9794 /* tried both fold-case and upper-case character, continue one
9795 * level up */
9796 --depth;
9797 }
9798 else
9799 {
9800 /*
9801 * round[depth] == 1: Try using the folded-case character.
9802 * round[depth] == 2: Try using the upper-case character.
9803 */
9804#ifdef FEAT_MBYTE
9805 if (has_mbyte)
9806 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009807 flen = mb_cptr2len(fword + fwordidx[depth]);
9808 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009809 }
9810 else
9811#endif
9812 ulen = flen = 1;
9813 if (round[depth] == 1)
9814 {
9815 p = fword + fwordidx[depth];
9816 l = flen;
9817 }
9818 else
9819 {
9820 p = uword + uwordidx[depth];
9821 l = ulen;
9822 }
9823
9824 for (tryidx = arridx[depth]; l > 0; --l)
9825 {
9826 /* Perform a binary search in the list of accepted bytes. */
9827 len = byts[tryidx++];
9828 c = *p++;
9829 lo = tryidx;
9830 hi = tryidx + len - 1;
9831 while (lo < hi)
9832 {
9833 m = (lo + hi) / 2;
9834 if (byts[m] > c)
9835 hi = m - 1;
9836 else if (byts[m] < c)
9837 lo = m + 1;
9838 else
9839 {
9840 lo = hi = m;
9841 break;
9842 }
9843 }
9844
9845 /* Stop if there is no matching byte. */
9846 if (hi < lo || byts[lo] != c)
9847 break;
9848
9849 /* Continue at the child (if there is one). */
9850 tryidx = idxs[lo];
9851 }
9852
9853 if (l == 0)
9854 {
9855 /*
9856 * Found the matching char. Copy it to "kword" and go a
9857 * level deeper.
9858 */
9859 if (round[depth] == 1)
9860 {
9861 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
9862 flen);
9863 kwordlen[depth + 1] = kwordlen[depth] + flen;
9864 }
9865 else
9866 {
9867 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
9868 ulen);
9869 kwordlen[depth + 1] = kwordlen[depth] + ulen;
9870 }
9871 fwordidx[depth + 1] = fwordidx[depth] + flen;
9872 uwordidx[depth + 1] = uwordidx[depth] + ulen;
9873
9874 ++depth;
9875 arridx[depth] = tryidx;
9876 round[depth] = 0;
9877 }
9878 }
9879 }
9880
9881 /* Didn't find it: "cannot happen". */
9882 *kword = NUL;
9883}
9884
9885/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009886 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
9887 * su->su_sga.
9888 */
9889 static void
9890score_comp_sal(su)
9891 suginfo_T *su;
9892{
9893 langp_T *lp;
9894 char_u badsound[MAXWLEN];
9895 int i;
9896 suggest_T *stp;
9897 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009898 int score;
9899
9900 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
9901 return;
9902
9903 /* Use the sound-folding of the first language that supports it. */
9904 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
9905 lp->lp_slang != NULL; ++lp)
9906 if (lp->lp_slang->sl_sal.ga_len > 0)
9907 {
9908 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009909 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009910
9911 for (i = 0; i < su->su_ga.ga_len; ++i)
9912 {
9913 stp = &SUG(su->su_ga, i);
9914
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009915 /* Case-fold the suggested word, sound-fold it and compute the
9916 * sound-a-like score. */
9917 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009918 if (score < SCORE_MAXMAX)
9919 {
9920 /* Add the suggestion. */
9921 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
9922 sstp->st_word = vim_strsave(stp->st_word);
9923 if (sstp->st_word != NULL)
9924 {
9925 sstp->st_score = score;
9926 sstp->st_altscore = 0;
9927 sstp->st_orglen = stp->st_orglen;
9928 ++su->su_sga.ga_len;
9929 }
9930 }
9931 }
9932 break;
9933 }
9934}
9935
9936/*
9937 * Combine the list of suggestions in su->su_ga and su->su_sga.
9938 * They are intwined.
9939 */
9940 static void
9941score_combine(su)
9942 suginfo_T *su;
9943{
9944 int i;
9945 int j;
9946 garray_T ga;
9947 garray_T *gap;
9948 langp_T *lp;
9949 suggest_T *stp;
9950 char_u *p;
9951 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009952 int round;
9953
9954 /* Add the alternate score to su_ga. */
9955 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
9956 lp->lp_slang != NULL; ++lp)
9957 {
9958 if (lp->lp_slang->sl_sal.ga_len > 0)
9959 {
9960 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009961 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009962
9963 for (i = 0; i < su->su_ga.ga_len; ++i)
9964 {
9965 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009966 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
9967 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009968 if (stp->st_altscore == SCORE_MAXMAX)
9969 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
9970 else
9971 stp->st_score = (stp->st_score * 3
9972 + stp->st_altscore) / 4;
9973 stp->st_salscore = FALSE;
9974 }
9975 break;
9976 }
9977 }
9978
9979 /* Add the alternate score to su_sga. */
9980 for (i = 0; i < su->su_sga.ga_len; ++i)
9981 {
9982 stp = &SUG(su->su_sga, i);
9983 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
9984 if (stp->st_score == SCORE_MAXMAX)
9985 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
9986 else
9987 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
9988 stp->st_salscore = TRUE;
9989 }
9990
9991 /* Sort the suggestions and truncate at "maxcount" for both lists. */
9992 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
9993 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
9994
9995 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
9996 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
9997 return;
9998
9999 stp = &SUG(ga, 0);
10000 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
10001 {
10002 /* round 1: get a suggestion from su_ga
10003 * round 2: get a suggestion from su_sga */
10004 for (round = 1; round <= 2; ++round)
10005 {
10006 gap = round == 1 ? &su->su_ga : &su->su_sga;
10007 if (i < gap->ga_len)
10008 {
10009 /* Don't add a word if it's already there. */
10010 p = SUG(*gap, i).st_word;
10011 for (j = 0; j < ga.ga_len; ++j)
10012 if (STRCMP(stp[j].st_word, p) == 0)
10013 break;
10014 if (j == ga.ga_len)
10015 stp[ga.ga_len++] = SUG(*gap, i);
10016 else
10017 vim_free(p);
10018 }
10019 }
10020 }
10021
10022 ga_clear(&su->su_ga);
10023 ga_clear(&su->su_sga);
10024
10025 /* Truncate the list to the number of suggestions that will be displayed. */
10026 if (ga.ga_len > su->su_maxcount)
10027 {
10028 for (i = su->su_maxcount; i < ga.ga_len; ++i)
10029 vim_free(stp[i].st_word);
10030 ga.ga_len = su->su_maxcount;
10031 }
10032
10033 su->su_ga = ga;
10034}
10035
10036/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010037 * For the goodword in "stp" compute the soundalike score compared to the
10038 * badword.
10039 */
10040 static int
10041stp_sal_score(stp, su, slang, badsound)
10042 suggest_T *stp;
10043 suginfo_T *su;
10044 slang_T *slang;
10045 char_u *badsound; /* sound-folded badword */
10046{
10047 char_u *p;
10048 char_u badsound2[MAXWLEN];
10049 char_u fword[MAXWLEN];
10050 char_u goodsound[MAXWLEN];
10051
10052 if (stp->st_orglen <= su->su_badlen)
10053 p = badsound;
10054 else
10055 {
10056 /* soundfold the bad word with more characters following */
10057 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
10058
10059 /* When joining two words the sound often changes a lot. E.g., "t he"
10060 * sounds like "t h" while "the" sounds like "@". Avoid that by
10061 * removing the space. Don't do it when the good word also contains a
10062 * space. */
10063 if (vim_iswhite(su->su_badptr[su->su_badlen])
10064 && *skiptowhite(stp->st_word) == NUL)
10065 for (p = fword; *(p = skiptowhite(p)) != NUL; )
10066 mch_memmove(p, p + 1, STRLEN(p));
10067
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010068 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010069 p = badsound2;
10070 }
10071
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010072 /* Sound-fold the word and compute the score for the difference. */
10073 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010074
10075 return soundalike_score(goodsound, p);
10076}
10077
10078/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010079 * Find suggestions by comparing the word in a sound-a-like form.
10080 */
10081 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000010082suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010083 suginfo_T *su;
10084{
10085 char_u salword[MAXWLEN];
10086 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010087 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010088 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010089 int curi[MAXWLEN];
10090 langp_T *lp;
10091 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010092 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010093 int depth;
10094 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010095 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010096 int round;
10097 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010098 int sound_score;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010099 int local_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010100
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010101 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010102 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
10103 lp->lp_slang != NULL; ++lp)
10104 {
10105 if (lp->lp_slang->sl_sal.ga_len > 0)
10106 {
10107 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010108 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010109
10110 /*
10111 * Go through the whole tree, soundfold each word and compare.
10112 * round 1: use the case-folded tree.
10113 * round 2: use the keep-case tree.
10114 */
10115 for (round = 1; round <= 2; ++round)
10116 {
10117 if (round == 1)
10118 {
10119 byts = lp->lp_slang->sl_fbyts;
10120 idxs = lp->lp_slang->sl_fidxs;
10121 }
10122 else
10123 {
10124 byts = lp->lp_slang->sl_kbyts;
10125 idxs = lp->lp_slang->sl_kidxs;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010126 if (byts == NULL) /* no keep-case words */
10127 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010128 }
10129
10130 depth = 0;
10131 arridx[0] = 0;
10132 curi[0] = 1;
10133 while (depth >= 0 && !got_int)
10134 {
10135 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010136 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010137 /* Done all bytes at this node, go up one level. */
10138 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010139 line_breakcheck();
10140 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010141 else
10142 {
10143 /* Do one more byte at this node. */
10144 n = arridx[depth] + curi[depth];
10145 ++curi[depth];
10146 c = byts[n];
10147 if (c == 0)
10148 {
10149 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010150 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010151 if (round == 2 || (flags & WF_KEEPCAP) == 0)
10152 {
10153 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010154 /* Sound-fold. Only in keep-case tree need to
10155 * case-fold the word. */
10156 spell_soundfold(lp->lp_slang, tword,
10157 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010158
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010159 /* Compute the edit distance between the
10160 * sound-a-like words. */
10161 sound_score = soundalike_score(salword,
10162 tsalword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010163
10164 /* Add a penalty for words in another region. */
10165 if ((flags & WF_REGION) && (((unsigned)flags
10166 >> 16) & lp->lp_region) == 0)
10167 local_score = SCORE_REGION;
10168 else
10169 local_score = 0;
10170 sound_score += local_score;
10171
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010172 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010173 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010174 char_u cword[MAXWLEN];
10175 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010176 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010177
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010178 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010179 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010180 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010181 /* Need to fix case according to
10182 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010183 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010184 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010185 }
10186 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010187 p = tword;
10188
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010189 if (sps_flags & SPS_DOUBLE)
10190 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000010191 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010192 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010193 else
10194 {
10195 /* Compute the score. */
10196 score = spell_edit_score(
Bram Moolenaar5195e452005-08-19 20:32:47 +000010197 su->su_badword, p)
10198 + local_score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010199 if (sps_flags & SPS_BEST)
10200 /* give a bonus for the good word
10201 * sounding the same as the bad
10202 * word */
10203 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000010204 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010205 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010206 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010207 else
10208 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000010209 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010210 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010211 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010212 }
10213 }
10214
10215 /* Skip over other NUL bytes. */
10216 while (byts[n + 1] == 0)
10217 {
10218 ++n;
10219 ++curi[depth];
10220 }
10221 }
10222 else
10223 {
10224 /* Normal char, go one level deeper. */
10225 tword[depth++] = c;
10226 arridx[depth] = idxs[n];
10227 curi[depth] = 1;
10228 }
10229 }
10230 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010231 }
10232 }
10233 }
10234}
10235
10236/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010237 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010238 */
10239 static void
10240make_case_word(fword, cword, flags)
10241 char_u *fword;
10242 char_u *cword;
10243 int flags;
10244{
10245 if (flags & WF_ALLCAP)
10246 /* Make it all upper-case */
10247 allcap_copy(fword, cword);
10248 else if (flags & WF_ONECAP)
10249 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010250 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010251 else
10252 /* Use goodword as-is. */
10253 STRCPY(cword, fword);
10254}
10255
Bram Moolenaarea424162005-06-16 21:51:00 +000010256/*
10257 * Use map string "map" for languages "lp".
10258 */
10259 static void
10260set_map_str(lp, map)
10261 slang_T *lp;
10262 char_u *map;
10263{
10264 char_u *p;
10265 int headc = 0;
10266 int c;
10267 int i;
10268
10269 if (*map == NUL)
10270 {
10271 lp->sl_has_map = FALSE;
10272 return;
10273 }
10274 lp->sl_has_map = TRUE;
10275
10276 /* Init the array and hash table empty. */
10277 for (i = 0; i < 256; ++i)
10278 lp->sl_map_array[i] = 0;
10279#ifdef FEAT_MBYTE
10280 hash_init(&lp->sl_map_hash);
10281#endif
10282
10283 /*
10284 * The similar characters are stored separated with slashes:
10285 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
10286 * before the same slash. For characters above 255 sl_map_hash is used.
10287 */
10288 for (p = map; *p != NUL; )
10289 {
10290#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010291 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000010292#else
10293 c = *p++;
10294#endif
10295 if (c == '/')
10296 headc = 0;
10297 else
10298 {
10299 if (headc == 0)
10300 headc = c;
10301
10302#ifdef FEAT_MBYTE
10303 /* Characters above 255 don't fit in sl_map_array[], put them in
10304 * the hash table. Each entry is the char, a NUL the headchar and
10305 * a NUL. */
10306 if (c >= 256)
10307 {
10308 int cl = mb_char2len(c);
10309 int headcl = mb_char2len(headc);
10310 char_u *b;
10311 hash_T hash;
10312 hashitem_T *hi;
10313
10314 b = alloc((unsigned)(cl + headcl + 2));
10315 if (b == NULL)
10316 return;
10317 mb_char2bytes(c, b);
10318 b[cl] = NUL;
10319 mb_char2bytes(headc, b + cl + 1);
10320 b[cl + 1 + headcl] = NUL;
10321 hash = hash_hash(b);
10322 hi = hash_lookup(&lp->sl_map_hash, b, hash);
10323 if (HASHITEM_EMPTY(hi))
10324 hash_add_item(&lp->sl_map_hash, hi, b, hash);
10325 else
10326 {
10327 /* This should have been checked when generating the .spl
10328 * file. */
10329 EMSG(_("E999: duplicate char in MAP entry"));
10330 vim_free(b);
10331 }
10332 }
10333 else
10334#endif
10335 lp->sl_map_array[c] = headc;
10336 }
10337 }
10338}
10339
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010340/*
10341 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
10342 * lines in the .aff file.
10343 */
10344 static int
10345similar_chars(slang, c1, c2)
10346 slang_T *slang;
10347 int c1;
10348 int c2;
10349{
Bram Moolenaarea424162005-06-16 21:51:00 +000010350 int m1, m2;
10351#ifdef FEAT_MBYTE
10352 char_u buf[MB_MAXBYTES];
10353 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010354
Bram Moolenaarea424162005-06-16 21:51:00 +000010355 if (c1 >= 256)
10356 {
10357 buf[mb_char2bytes(c1, buf)] = 0;
10358 hi = hash_find(&slang->sl_map_hash, buf);
10359 if (HASHITEM_EMPTY(hi))
10360 m1 = 0;
10361 else
10362 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
10363 }
10364 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010365#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000010366 m1 = slang->sl_map_array[c1];
10367 if (m1 == 0)
10368 return FALSE;
10369
10370
10371#ifdef FEAT_MBYTE
10372 if (c2 >= 256)
10373 {
10374 buf[mb_char2bytes(c2, buf)] = 0;
10375 hi = hash_find(&slang->sl_map_hash, buf);
10376 if (HASHITEM_EMPTY(hi))
10377 m2 = 0;
10378 else
10379 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
10380 }
10381 else
10382#endif
10383 m2 = slang->sl_map_array[c2];
10384
10385 return m1 == m2;
10386}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010387
10388/*
10389 * Add a suggestion to the list of suggestions.
10390 * Do not add a duplicate suggestion or suggestions with a bad score.
10391 * When "use_score" is not zero it's used, otherwise the score is computed
10392 * with spell_edit_score().
10393 */
10394 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010395add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010396 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010397 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010398 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010399 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010400 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010401 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010402 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010403{
10404 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010405 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010406 char_u *p = NULL;
10407 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010408
10409 /* Check that the word wasn't banned. */
10410 if (was_banned(su, goodword))
10411 return;
10412
Bram Moolenaar0c405862005-06-22 22:26:26 +000010413 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
10414 * Remove the common part from "goodword". */
10415 i = badlen - su->su_badlen;
10416 if (i > 0)
10417 {
10418 /* This assumes there was no case folding or it didn't change the
10419 * length... */
10420 p = goodword + STRLEN(goodword) - i;
10421 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
10422 {
10423 badlen = su->su_badlen;
10424 c = *p;
10425 *p = NUL;
10426 }
10427 else
10428 p = NULL;
10429 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010430 else if (i < 0)
10431 {
10432 /* When replacing part of the word check that we actually change
10433 * something. For "the the" a suggestion can be replacing the first
10434 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010435 if (badlen == (int)STRLEN(goodword)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010436 && STRNCMP(su->su_badword, goodword, badlen) == 0)
10437 return;
10438 }
10439
Bram Moolenaar0c405862005-06-22 22:26:26 +000010440
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010441 if (score <= su->su_maxscore)
10442 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010443 /* Check if the word is already there. Also check the length that is
10444 * being replaced "thes," -> "these" is a different suggestion from
10445 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010446 stp = &SUG(*gap, 0);
10447 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010448 if (STRCMP(stp[i].st_word, goodword) == 0
10449 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010450 {
10451 /* Found it. Remember the lowest score. */
10452 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010453 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010454 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010455 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010456 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010457 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010458 break;
10459 }
10460
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010461 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010462 {
10463 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010464 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010465 stp->st_word = vim_strsave(goodword);
10466 if (stp->st_word != NULL)
10467 {
10468 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010469 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010470 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010471 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010472 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010473
10474 /* If we have too many suggestions now, sort the list and keep
10475 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010476 if (gap->ga_len > SUG_MAX_COUNT(su))
10477 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
10478 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010479 }
10480 }
10481 }
Bram Moolenaar0c405862005-06-22 22:26:26 +000010482
10483 if (p != NULL)
10484 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010485}
10486
10487/*
10488 * Add a word to be banned.
10489 */
10490 static void
10491add_banned(su, word)
10492 suginfo_T *su;
10493 char_u *word;
10494{
10495 char_u *s = vim_strsave(word);
10496 hash_T hash;
10497 hashitem_T *hi;
10498
10499 if (s != NULL)
10500 {
10501 hash = hash_hash(s);
10502 hi = hash_lookup(&su->su_banned, s, hash);
10503 if (HASHITEM_EMPTY(hi))
10504 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010505 else
10506 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010507 }
10508}
10509
10510/*
10511 * Return TRUE if a word appears in the list of banned words.
10512 */
10513 static int
10514was_banned(su, word)
10515 suginfo_T *su;
10516 char_u *word;
10517{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010518 hashitem_T *hi = hash_find(&su->su_banned, word);
10519
10520 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010521}
10522
10523/*
10524 * Free the banned words in "su".
10525 */
10526 static void
10527free_banned(su)
10528 suginfo_T *su;
10529{
10530 int todo;
10531 hashitem_T *hi;
10532
10533 todo = su->su_banned.ht_used;
10534 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
10535 {
10536 if (!HASHITEM_EMPTY(hi))
10537 {
10538 vim_free(hi->hi_key);
10539 --todo;
10540 }
10541 }
10542 hash_clear(&su->su_banned);
10543}
10544
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010545/*
10546 * Recompute the score if sound-folding is possible. This is slow,
10547 * thus only done for the final results.
10548 */
10549 static void
10550rescore_suggestions(su)
10551 suginfo_T *su;
10552{
10553 langp_T *lp;
10554 suggest_T *stp;
10555 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010556 int i;
10557
10558 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
10559 lp->lp_slang != NULL; ++lp)
10560 {
10561 if (lp->lp_slang->sl_sal.ga_len > 0)
10562 {
10563 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010564 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010565
10566 for (i = 0; i < su->su_ga.ga_len; ++i)
10567 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010568 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010569 if (!stp->st_had_bonus)
10570 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010571 stp->st_altscore = stp_sal_score(stp, su,
10572 lp->lp_slang, sal_badword);
10573 if (stp->st_altscore == SCORE_MAXMAX)
10574 stp->st_altscore = SCORE_BIG;
10575 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010576 }
10577 }
10578 break;
10579 }
10580 }
10581}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010582
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010583static int
10584#ifdef __BORLANDC__
10585_RTLENTRYF
10586#endif
10587sug_compare __ARGS((const void *s1, const void *s2));
10588
10589/*
10590 * Function given to qsort() to sort the suggestions on st_score.
10591 */
10592 static int
10593#ifdef __BORLANDC__
10594_RTLENTRYF
10595#endif
10596sug_compare(s1, s2)
10597 const void *s1;
10598 const void *s2;
10599{
10600 suggest_T *p1 = (suggest_T *)s1;
10601 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010602 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010603
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010604 if (n == 0)
10605 return p1->st_altscore - p2->st_altscore;
10606 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010607}
10608
10609/*
10610 * Cleanup the suggestions:
10611 * - Sort on score.
10612 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010613 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010614 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010615 static int
10616cleanup_suggestions(gap, maxscore, keep)
10617 garray_T *gap;
10618 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010619 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010620{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010621 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010622 int i;
10623
10624 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010625 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010626
10627 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010628 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010629 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010630 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010631 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010632 gap->ga_len = keep;
10633 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010634 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010635 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010636}
10637
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010638#if defined(FEAT_EVAL) || defined(PROTO)
10639/*
10640 * Soundfold a string, for soundfold().
10641 * Result is in allocated memory, NULL for an error.
10642 */
10643 char_u *
10644eval_soundfold(word)
10645 char_u *word;
10646{
10647 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010648 char_u sound[MAXWLEN];
10649
10650 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
10651 /* Use the sound-folding of the first language that supports it. */
10652 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
10653 lp->lp_slang != NULL; ++lp)
10654 if (lp->lp_slang->sl_sal.ga_len > 0)
10655 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010656 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010657 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010658 return vim_strsave(sound);
10659 }
10660
10661 /* No language with sound folding, return word as-is. */
10662 return vim_strsave(word);
10663}
10664#endif
10665
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010666/*
10667 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000010668 *
10669 * There are many ways to turn a word into a sound-a-like representation. The
10670 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
10671 * swedish name matching - survey and test of different algorithms" by Klas
10672 * Erikson.
10673 *
10674 * We support two methods:
10675 * 1. SOFOFROM/SOFOTO do a simple character mapping.
10676 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010677 */
10678 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010679spell_soundfold(slang, inword, folded, res)
10680 slang_T *slang;
10681 char_u *inword;
10682 int folded; /* "inword" is already case-folded */
10683 char_u *res;
10684{
10685 char_u fword[MAXWLEN];
10686 char_u *word;
10687
10688 if (slang->sl_sofo)
10689 /* SOFOFROM and SOFOTO used */
10690 spell_soundfold_sofo(slang, inword, res);
10691 else
10692 {
10693 /* SAL items used. Requires the word to be case-folded. */
10694 if (folded)
10695 word = inword;
10696 else
10697 {
10698 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
10699 word = fword;
10700 }
10701
10702#ifdef FEAT_MBYTE
10703 if (has_mbyte)
10704 spell_soundfold_wsal(slang, word, res);
10705 else
10706#endif
10707 spell_soundfold_sal(slang, word, res);
10708 }
10709}
10710
10711/*
10712 * Perform sound folding of "inword" into "res" according to SOFOFROM and
10713 * SOFOTO lines.
10714 */
10715 static void
10716spell_soundfold_sofo(slang, inword, res)
10717 slang_T *slang;
10718 char_u *inword;
10719 char_u *res;
10720{
10721 char_u *s;
10722 int ri = 0;
10723 int c;
10724
10725#ifdef FEAT_MBYTE
10726 if (has_mbyte)
10727 {
10728 int prevc = 0;
10729 int *ip;
10730
10731 /* The sl_sal_first[] table contains the translation for chars up to
10732 * 255, sl_sal the rest. */
10733 for (s = inword; *s != NUL; )
10734 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010735 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010736 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
10737 c = ' ';
10738 else if (c < 256)
10739 c = slang->sl_sal_first[c];
10740 else
10741 {
10742 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
10743 if (ip == NULL) /* empty list, can't match */
10744 c = NUL;
10745 else
10746 for (;;) /* find "c" in the list */
10747 {
10748 if (*ip == 0) /* not found */
10749 {
10750 c = NUL;
10751 break;
10752 }
10753 if (*ip == c) /* match! */
10754 {
10755 c = ip[1];
10756 break;
10757 }
10758 ip += 2;
10759 }
10760 }
10761
10762 if (c != NUL && c != prevc)
10763 {
10764 ri += mb_char2bytes(c, res + ri);
10765 if (ri + MB_MAXBYTES > MAXWLEN)
10766 break;
10767 prevc = c;
10768 }
10769 }
10770 }
10771 else
10772#endif
10773 {
10774 /* The sl_sal_first[] table contains the translation. */
10775 for (s = inword; (c = *s) != NUL; ++s)
10776 {
10777 if (vim_iswhite(c))
10778 c = ' ';
10779 else
10780 c = slang->sl_sal_first[c];
10781 if (c != NUL && (ri == 0 || res[ri - 1] != c))
10782 res[ri++] = c;
10783 }
10784 }
10785
10786 res[ri] = NUL;
10787}
10788
10789 static void
10790spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010791 slang_T *slang;
10792 char_u *inword;
10793 char_u *res;
10794{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010795 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010796 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010797 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010798 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010799 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010800 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010801 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010802 int n, k = 0;
10803 int z0;
10804 int k0;
10805 int n0;
10806 int c;
10807 int pri;
10808 int p0 = -333;
10809 int c0;
10810
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010811 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010812 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010813 if (slang->sl_rem_accents)
10814 {
10815 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010816 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010817 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010818 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010819 {
10820 *t++ = ' ';
10821 s = skipwhite(s);
10822 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010823 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010824 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010825 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010826 *t++ = *s;
10827 ++s;
10828 }
10829 }
10830 *t = NUL;
10831 }
10832 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010833 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010834
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010835 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010836
10837 /*
10838 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010839 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010840 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010841 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010842 while ((c = word[i]) != NUL)
10843 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010844 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010845 n = slang->sl_sal_first[c];
10846 z0 = 0;
10847
10848 if (n >= 0)
10849 {
10850 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010851 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010852 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010853 /* Quickly skip entries that don't match the word. Most
10854 * entries are less then three chars, optimize for that. */
10855 k = smp[n].sm_leadlen;
10856 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010857 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010858 if (word[i + 1] != s[1])
10859 continue;
10860 if (k > 2)
10861 {
10862 for (j = 2; j < k; ++j)
10863 if (word[i + j] != s[j])
10864 break;
10865 if (j < k)
10866 continue;
10867 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010868 }
10869
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010870 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010871 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010872 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010873 while (*pf != NUL && *pf != word[i + k])
10874 ++pf;
10875 if (*pf == NUL)
10876 continue;
10877 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010878 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010879 s = smp[n].sm_rules;
10880 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010881
10882 p0 = *s;
10883 k0 = k;
10884 while (*s == '-' && k > 1)
10885 {
10886 k--;
10887 s++;
10888 }
10889 if (*s == '<')
10890 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010891 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010892 {
10893 /* determine priority */
10894 pri = *s - '0';
10895 s++;
10896 }
10897 if (*s == '^' && *(s + 1) == '^')
10898 s++;
10899
10900 if (*s == NUL
10901 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010902 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010903 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010904 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010905 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010906 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010907 && spell_iswordp(word + i - 1, curbuf)
10908 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010909 {
10910 /* search for followup rules, if: */
10911 /* followup and k > 1 and NO '-' in searchstring */
10912 c0 = word[i + k - 1];
10913 n0 = slang->sl_sal_first[c0];
10914
10915 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010916 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010917 {
10918 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010919 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010920 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010921 /* Quickly skip entries that don't match the word.
10922 * */
10923 k0 = smp[n0].sm_leadlen;
10924 if (k0 > 1)
10925 {
10926 if (word[i + k] != s[1])
10927 continue;
10928 if (k0 > 2)
10929 {
10930 pf = word + i + k + 1;
10931 for (j = 2; j < k0; ++j)
10932 if (*pf++ != s[j])
10933 break;
10934 if (j < k0)
10935 continue;
10936 }
10937 }
10938 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010939
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010940 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010941 {
10942 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010943 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010944 while (*pf != NUL && *pf != word[i + k0])
10945 ++pf;
10946 if (*pf == NUL)
10947 continue;
10948 ++k0;
10949 }
10950
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010951 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010952 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010953 while (*s == '-')
10954 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010955 /* "k0" gets NOT reduced because
10956 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010957 s++;
10958 }
10959 if (*s == '<')
10960 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010961 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010962 {
10963 p0 = *s - '0';
10964 s++;
10965 }
10966
10967 if (*s == NUL
10968 /* *s == '^' cuts */
10969 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010970 && !spell_iswordp(word + i + k0,
10971 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010972 {
10973 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010974 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010975 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010976
10977 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010978 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010979 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010980 /* rule fits; stop search */
10981 break;
10982 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010983 }
10984
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010985 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010986 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010987 }
10988
10989 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010990 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010991 if (s == NULL)
10992 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010993 pf = smp[n].sm_rules;
10994 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010995 if (p0 == 1 && z == 0)
10996 {
10997 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010998 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
10999 || res[reslen - 1] == *s))
11000 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011001 z0 = 1;
11002 z = 1;
11003 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011004 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011005 {
11006 word[i + k0] = *s;
11007 k0++;
11008 s++;
11009 }
11010 if (k > k0)
11011 mch_memmove(word + i + k0, word + i + k,
11012 STRLEN(word + i + k) + 1);
11013
11014 /* new "actual letter" */
11015 c = word[i];
11016 }
11017 else
11018 {
11019 /* no '<' rule used */
11020 i += k - 1;
11021 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011022 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011023 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011024 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011025 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011026 s++;
11027 }
11028 /* new "actual letter" */
11029 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011030 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011031 {
11032 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011033 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011034 mch_memmove(word, word + i + 1,
11035 STRLEN(word + i + 1) + 1);
11036 i = 0;
11037 z0 = 1;
11038 }
11039 }
11040 break;
11041 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011042 }
11043 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011044 else if (vim_iswhite(c))
11045 {
11046 c = ' ';
11047 k = 1;
11048 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011049
11050 if (z0 == 0)
11051 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011052 if (k && !p0 && reslen < MAXWLEN && c != NUL
11053 && (!slang->sl_collapse || reslen == 0
11054 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011055 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011056 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011057
11058 i++;
11059 z = 0;
11060 k = 0;
11061 }
11062 }
11063
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011064 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011065}
11066
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011067#ifdef FEAT_MBYTE
11068/*
11069 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
11070 * Multi-byte version of spell_soundfold().
11071 */
11072 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011073spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011074 slang_T *slang;
11075 char_u *inword;
11076 char_u *res;
11077{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011078 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011079 int word[MAXWLEN];
11080 int wres[MAXWLEN];
11081 int l;
11082 char_u *s;
11083 int *ws;
11084 char_u *t;
11085 int *pf;
11086 int i, j, z;
11087 int reslen;
11088 int n, k = 0;
11089 int z0;
11090 int k0;
11091 int n0;
11092 int c;
11093 int pri;
11094 int p0 = -333;
11095 int c0;
11096 int did_white = FALSE;
11097
11098 /*
11099 * Convert the multi-byte string to a wide-character string.
11100 * Remove accents, if wanted. We actually remove all non-word characters.
11101 * But keep white space.
11102 */
11103 n = 0;
11104 for (s = inword; *s != NUL; )
11105 {
11106 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011107 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011108 if (slang->sl_rem_accents)
11109 {
11110 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
11111 {
11112 if (did_white)
11113 continue;
11114 c = ' ';
11115 did_white = TRUE;
11116 }
11117 else
11118 {
11119 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011120 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011121 continue;
11122 }
11123 }
11124 word[n++] = c;
11125 }
11126 word[n] = NUL;
11127
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011128 /*
11129 * This comes from Aspell phonet.cpp.
11130 * Converted from C++ to C. Added support for multi-byte chars.
11131 * Changed to keep spaces.
11132 */
11133 i = reslen = z = 0;
11134 while ((c = word[i]) != NUL)
11135 {
11136 /* Start with the first rule that has the character in the word. */
11137 n = slang->sl_sal_first[c & 0xff];
11138 z0 = 0;
11139
11140 if (n >= 0)
11141 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011142 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011143 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
11144 {
11145 /* Quickly skip entries that don't match the word. Most
11146 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011147 if (c != ws[0])
11148 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011149 k = smp[n].sm_leadlen;
11150 if (k > 1)
11151 {
11152 if (word[i + 1] != ws[1])
11153 continue;
11154 if (k > 2)
11155 {
11156 for (j = 2; j < k; ++j)
11157 if (word[i + j] != ws[j])
11158 break;
11159 if (j < k)
11160 continue;
11161 }
11162 }
11163
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011164 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011165 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011166 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011167 while (*pf != NUL && *pf != word[i + k])
11168 ++pf;
11169 if (*pf == NUL)
11170 continue;
11171 ++k;
11172 }
11173 s = smp[n].sm_rules;
11174 pri = 5; /* default priority */
11175
11176 p0 = *s;
11177 k0 = k;
11178 while (*s == '-' && k > 1)
11179 {
11180 k--;
11181 s++;
11182 }
11183 if (*s == '<')
11184 s++;
11185 if (VIM_ISDIGIT(*s))
11186 {
11187 /* determine priority */
11188 pri = *s - '0';
11189 s++;
11190 }
11191 if (*s == '^' && *(s + 1) == '^')
11192 s++;
11193
11194 if (*s == NUL
11195 || (*s == '^'
11196 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011197 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011198 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011199 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011200 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011201 && spell_iswordp_w(word + i - 1, curbuf)
11202 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011203 {
11204 /* search for followup rules, if: */
11205 /* followup and k > 1 and NO '-' in searchstring */
11206 c0 = word[i + k - 1];
11207 n0 = slang->sl_sal_first[c0 & 0xff];
11208
11209 if (slang->sl_followup && k > 1 && n0 >= 0
11210 && p0 != '-' && word[i + k] != NUL)
11211 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011212 /* Test follow-up rule for "word[i + k]"; loop over
11213 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011214 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
11215 == (c0 & 0xff); ++n0)
11216 {
11217 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011218 */
11219 if (c0 != ws[0])
11220 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011221 k0 = smp[n0].sm_leadlen;
11222 if (k0 > 1)
11223 {
11224 if (word[i + k] != ws[1])
11225 continue;
11226 if (k0 > 2)
11227 {
11228 pf = word + i + k + 1;
11229 for (j = 2; j < k0; ++j)
11230 if (*pf++ != ws[j])
11231 break;
11232 if (j < k0)
11233 continue;
11234 }
11235 }
11236 k0 += k - 1;
11237
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011238 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011239 {
11240 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011241 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011242 while (*pf != NUL && *pf != word[i + k0])
11243 ++pf;
11244 if (*pf == NUL)
11245 continue;
11246 ++k0;
11247 }
11248
11249 p0 = 5;
11250 s = smp[n0].sm_rules;
11251 while (*s == '-')
11252 {
11253 /* "k0" gets NOT reduced because
11254 * "if (k0 == k)" */
11255 s++;
11256 }
11257 if (*s == '<')
11258 s++;
11259 if (VIM_ISDIGIT(*s))
11260 {
11261 p0 = *s - '0';
11262 s++;
11263 }
11264
11265 if (*s == NUL
11266 /* *s == '^' cuts */
11267 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011268 && !spell_iswordp_w(word + i + k0,
11269 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011270 {
11271 if (k0 == k)
11272 /* this is just a piece of the string */
11273 continue;
11274
11275 if (p0 < pri)
11276 /* priority too low */
11277 continue;
11278 /* rule fits; stop search */
11279 break;
11280 }
11281 }
11282
11283 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
11284 == (c0 & 0xff))
11285 continue;
11286 }
11287
11288 /* replace string */
11289 ws = smp[n].sm_to_w;
11290 s = smp[n].sm_rules;
11291 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
11292 if (p0 == 1 && z == 0)
11293 {
11294 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011295 if (reslen > 0 && ws != NULL && *ws != NUL
11296 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011297 || wres[reslen - 1] == *ws))
11298 reslen--;
11299 z0 = 1;
11300 z = 1;
11301 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011302 if (ws != NULL)
11303 while (*ws != NUL && word[i + k0] != NUL)
11304 {
11305 word[i + k0] = *ws;
11306 k0++;
11307 ws++;
11308 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011309 if (k > k0)
11310 mch_memmove(word + i + k0, word + i + k,
11311 sizeof(int) * (STRLEN(word + i + k) + 1));
11312
11313 /* new "actual letter" */
11314 c = word[i];
11315 }
11316 else
11317 {
11318 /* no '<' rule used */
11319 i += k - 1;
11320 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011321 if (ws != NULL)
11322 while (*ws != NUL && ws[1] != NUL
11323 && reslen < MAXWLEN)
11324 {
11325 if (reslen == 0 || wres[reslen - 1] != *ws)
11326 wres[reslen++] = *ws;
11327 ws++;
11328 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011329 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011330 if (ws == NULL)
11331 c = NUL;
11332 else
11333 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011334 if (strstr((char *)s, "^^") != NULL)
11335 {
11336 if (c != NUL)
11337 wres[reslen++] = c;
11338 mch_memmove(word, word + i + 1,
11339 sizeof(int) * (STRLEN(word + i + 1) + 1));
11340 i = 0;
11341 z0 = 1;
11342 }
11343 }
11344 break;
11345 }
11346 }
11347 }
11348 else if (vim_iswhite(c))
11349 {
11350 c = ' ';
11351 k = 1;
11352 }
11353
11354 if (z0 == 0)
11355 {
11356 if (k && !p0 && reslen < MAXWLEN && c != NUL
11357 && (!slang->sl_collapse || reslen == 0
11358 || wres[reslen - 1] != c))
11359 /* condense only double letters */
11360 wres[reslen++] = c;
11361
11362 i++;
11363 z = 0;
11364 k = 0;
11365 }
11366 }
11367
11368 /* Convert wide characters in "wres" to a multi-byte string in "res". */
11369 l = 0;
11370 for (n = 0; n < reslen; ++n)
11371 {
11372 l += mb_char2bytes(wres[n], res + l);
11373 if (l + MB_MAXBYTES > MAXWLEN)
11374 break;
11375 }
11376 res[l] = NUL;
11377}
11378#endif
11379
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011380/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011381 * Compute a score for two sound-a-like words.
11382 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
11383 * Instead of a generic loop we write out the code. That keeps it fast by
11384 * avoiding checks that will not be possible.
11385 */
11386 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011387soundalike_score(goodstart, badstart)
11388 char_u *goodstart; /* sound-folded good word */
11389 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011390{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011391 char_u *goodsound = goodstart;
11392 char_u *badsound = badstart;
11393 int goodlen;
11394 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011395 int n;
11396 char_u *pl, *ps;
11397 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011398 int score = 0;
11399
11400 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
11401 * counted so much, vowels halfway the word aren't counted at all. */
11402 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
11403 {
11404 score = SCORE_DEL / 2;
11405 if (*badsound == '*')
11406 ++badsound;
11407 else
11408 ++goodsound;
11409 }
11410
11411 goodlen = STRLEN(goodsound);
11412 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011413
11414 /* Return quickly if the lenghts are too different to be fixed by two
11415 * changes. */
11416 n = goodlen - badlen;
11417 if (n < -2 || n > 2)
11418 return SCORE_MAXMAX;
11419
11420 if (n > 0)
11421 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011422 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011423 ps = badsound;
11424 }
11425 else
11426 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011427 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011428 ps = goodsound;
11429 }
11430
11431 /* Skip over the identical part. */
11432 while (*pl == *ps && *pl != NUL)
11433 {
11434 ++pl;
11435 ++ps;
11436 }
11437
11438 switch (n)
11439 {
11440 case -2:
11441 case 2:
11442 /*
11443 * Must delete two characters from "pl".
11444 */
11445 ++pl; /* first delete */
11446 while (*pl == *ps)
11447 {
11448 ++pl;
11449 ++ps;
11450 }
11451 /* strings must be equal after second delete */
11452 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011453 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011454
11455 /* Failed to compare. */
11456 break;
11457
11458 case -1:
11459 case 1:
11460 /*
11461 * Minimal one delete from "pl" required.
11462 */
11463
11464 /* 1: delete */
11465 pl2 = pl + 1;
11466 ps2 = ps;
11467 while (*pl2 == *ps2)
11468 {
11469 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011470 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011471 ++pl2;
11472 ++ps2;
11473 }
11474
11475 /* 2: delete then swap, then rest must be equal */
11476 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
11477 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011478 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011479
11480 /* 3: delete then substitute, then the rest must be equal */
11481 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011482 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011483
11484 /* 4: first swap then delete */
11485 if (pl[0] == ps[1] && pl[1] == ps[0])
11486 {
11487 pl2 = pl + 2; /* swap, skip two chars */
11488 ps2 = ps + 2;
11489 while (*pl2 == *ps2)
11490 {
11491 ++pl2;
11492 ++ps2;
11493 }
11494 /* delete a char and then strings must be equal */
11495 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011496 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011497 }
11498
11499 /* 5: first substitute then delete */
11500 pl2 = pl + 1; /* substitute, skip one char */
11501 ps2 = ps + 1;
11502 while (*pl2 == *ps2)
11503 {
11504 ++pl2;
11505 ++ps2;
11506 }
11507 /* delete a char and then strings must be equal */
11508 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011509 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011510
11511 /* Failed to compare. */
11512 break;
11513
11514 case 0:
11515 /*
11516 * Lenghts are equal, thus changes must result in same length: An
11517 * insert is only possible in combination with a delete.
11518 * 1: check if for identical strings
11519 */
11520 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011521 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011522
11523 /* 2: swap */
11524 if (pl[0] == ps[1] && pl[1] == ps[0])
11525 {
11526 pl2 = pl + 2; /* swap, skip two chars */
11527 ps2 = ps + 2;
11528 while (*pl2 == *ps2)
11529 {
11530 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011531 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011532 ++pl2;
11533 ++ps2;
11534 }
11535 /* 3: swap and swap again */
11536 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
11537 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011538 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011539
11540 /* 4: swap and substitute */
11541 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011542 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011543 }
11544
11545 /* 5: substitute */
11546 pl2 = pl + 1;
11547 ps2 = ps + 1;
11548 while (*pl2 == *ps2)
11549 {
11550 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011551 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011552 ++pl2;
11553 ++ps2;
11554 }
11555
11556 /* 6: substitute and swap */
11557 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
11558 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011559 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011560
11561 /* 7: substitute and substitute */
11562 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011563 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011564
11565 /* 8: insert then delete */
11566 pl2 = pl;
11567 ps2 = ps + 1;
11568 while (*pl2 == *ps2)
11569 {
11570 ++pl2;
11571 ++ps2;
11572 }
11573 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011574 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011575
11576 /* 9: delete then insert */
11577 pl2 = pl + 1;
11578 ps2 = ps;
11579 while (*pl2 == *ps2)
11580 {
11581 ++pl2;
11582 ++ps2;
11583 }
11584 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011585 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011586
11587 /* Failed to compare. */
11588 break;
11589 }
11590
11591 return SCORE_MAXMAX;
11592}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011593
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011594/*
11595 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011596 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011597 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000011598 * The algorithm is described by Du and Chang, 1992.
11599 * The implementation of the algorithm comes from Aspell editdist.cpp,
11600 * edit_distance(). It has been converted from C++ to C and modified to
11601 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011602 */
11603 static int
11604spell_edit_score(badword, goodword)
11605 char_u *badword;
11606 char_u *goodword;
11607{
11608 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011609 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011610 int j, i;
11611 int t;
11612 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011613 int pbc, pgc;
11614#ifdef FEAT_MBYTE
11615 char_u *p;
11616 int wbadword[MAXWLEN];
11617 int wgoodword[MAXWLEN];
11618
11619 if (has_mbyte)
11620 {
11621 /* Get the characters from the multi-byte strings and put them in an
11622 * int array for easy access. */
11623 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011624 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000011625 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011626 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011627 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000011628 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011629 }
11630 else
11631#endif
11632 {
11633 badlen = STRLEN(badword) + 1;
11634 goodlen = STRLEN(goodword) + 1;
11635 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011636
11637 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
11638#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011639 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
11640 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011641 if (cnt == NULL)
11642 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011643
11644 CNT(0, 0) = 0;
11645 for (j = 1; j <= goodlen; ++j)
11646 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
11647
11648 for (i = 1; i <= badlen; ++i)
11649 {
11650 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
11651 for (j = 1; j <= goodlen; ++j)
11652 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011653#ifdef FEAT_MBYTE
11654 if (has_mbyte)
11655 {
11656 bc = wbadword[i - 1];
11657 gc = wgoodword[j - 1];
11658 }
11659 else
11660#endif
11661 {
11662 bc = badword[i - 1];
11663 gc = goodword[j - 1];
11664 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011665 if (bc == gc)
11666 CNT(i, j) = CNT(i - 1, j - 1);
11667 else
11668 {
11669 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011670 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011671 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
11672 else
11673 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
11674
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011675 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011676 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011677#ifdef FEAT_MBYTE
11678 if (has_mbyte)
11679 {
11680 pbc = wbadword[i - 2];
11681 pgc = wgoodword[j - 2];
11682 }
11683 else
11684#endif
11685 {
11686 pbc = badword[i - 2];
11687 pgc = goodword[j - 2];
11688 }
11689 if (bc == pgc && pbc == gc)
11690 {
11691 t = SCORE_SWAP + CNT(i - 2, j - 2);
11692 if (t < CNT(i, j))
11693 CNT(i, j) = t;
11694 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011695 }
11696 t = SCORE_DEL + CNT(i - 1, j);
11697 if (t < CNT(i, j))
11698 CNT(i, j) = t;
11699 t = SCORE_INS + CNT(i, j - 1);
11700 if (t < CNT(i, j))
11701 CNT(i, j) = t;
11702 }
11703 }
11704 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011705
11706 i = CNT(badlen - 1, goodlen - 1);
11707 vim_free(cnt);
11708 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011709}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000011710
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011711/*
11712 * ":spelldump"
11713 */
11714/*ARGSUSED*/
11715 void
11716ex_spelldump(eap)
11717 exarg_T *eap;
11718{
11719 buf_T *buf = curbuf;
11720 langp_T *lp;
11721 slang_T *slang;
11722 idx_T arridx[MAXWLEN];
11723 int curi[MAXWLEN];
11724 char_u word[MAXWLEN];
11725 int c;
11726 char_u *byts;
11727 idx_T *idxs;
11728 linenr_T lnum = 0;
11729 int round;
11730 int depth;
11731 int n;
11732 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000011733 char_u *region_names = NULL; /* region names being used */
11734 int do_region = TRUE; /* dump region names and numbers */
11735 char_u *p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011736
11737 if (no_spell_checking())
11738 return;
11739
11740 /* Create a new empty buffer by splitting the window. */
11741 do_cmdline_cmd((char_u *)"new");
11742 if (!bufempty() || !buf_valid(buf))
11743 return;
11744
Bram Moolenaar7887d882005-07-01 22:33:52 +000011745 /* Find out if we can support regions: All languages must support the same
11746 * regions or none at all. */
11747 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
11748 {
11749 p = lp->lp_slang->sl_regions;
11750 if (p[0] != 0)
11751 {
11752 if (region_names == NULL) /* first language with regions */
11753 region_names = p;
11754 else if (STRCMP(region_names, p) != 0)
11755 {
11756 do_region = FALSE; /* region names are different */
11757 break;
11758 }
11759 }
11760 }
11761
11762 if (do_region && region_names != NULL)
11763 {
11764 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
11765 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
11766 }
11767 else
11768 do_region = FALSE;
11769
11770 /*
11771 * Loop over all files loaded for the entries in 'spelllang'.
11772 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011773 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
11774 {
11775 slang = lp->lp_slang;
11776
11777 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
11778 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
11779
11780 /* round 1: case-folded tree
11781 * round 2: keep-case tree */
11782 for (round = 1; round <= 2; ++round)
11783 {
11784 if (round == 1)
11785 {
11786 byts = slang->sl_fbyts;
11787 idxs = slang->sl_fidxs;
11788 }
11789 else
11790 {
11791 byts = slang->sl_kbyts;
11792 idxs = slang->sl_kidxs;
11793 }
11794 if (byts == NULL)
11795 continue; /* array is empty */
11796
11797 depth = 0;
11798 arridx[0] = 0;
11799 curi[0] = 1;
11800 while (depth >= 0 && !got_int)
11801 {
11802 if (curi[depth] > byts[arridx[depth]])
11803 {
11804 /* Done all bytes at this node, go up one level. */
11805 --depth;
11806 line_breakcheck();
11807 }
11808 else
11809 {
11810 /* Do one more byte at this node. */
11811 n = arridx[depth] + curi[depth];
11812 ++curi[depth];
11813 c = byts[n];
11814 if (c == 0)
11815 {
11816 /* End of word, deal with the word.
11817 * Don't use keep-case words in the fold-case tree,
11818 * they will appear in the keep-case tree.
11819 * Only use the word when the region matches. */
11820 flags = (int)idxs[n];
11821 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +000011822 && (do_region
11823 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011824 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011825 & lp->lp_region) != 0))
11826 {
11827 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000011828 if (!do_region)
11829 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000011830
11831 /* Dump the basic word if there is no prefix or
11832 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011833 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000011834 if (c == 0 || curi[depth] == 2)
11835 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011836
11837 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000011838 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011839 lnum = apply_prefixes(slang, word, round,
11840 flags, lnum);
11841 }
11842 }
11843 else
11844 {
11845 /* Normal char, go one level deeper. */
11846 word[depth++] = c;
11847 arridx[depth] = idxs[n];
11848 curi[depth] = 1;
11849 }
11850 }
11851 }
11852 }
11853 }
11854
11855 /* Delete the empty line that we started with. */
11856 if (curbuf->b_ml.ml_line_count > 1)
11857 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
11858
11859 redraw_later(NOT_VALID);
11860}
11861
11862/*
11863 * Dump one word: apply case modifications and append a line to the buffer.
11864 */
11865 static void
11866dump_word(word, round, flags, lnum)
11867 char_u *word;
11868 int round;
11869 int flags;
11870 linenr_T lnum;
11871{
11872 int keepcap = FALSE;
11873 char_u *p;
11874 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000011875 char_u badword[MAXWLEN + 10];
11876 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011877
11878 if (round == 1 && (flags & WF_CAPMASK) != 0)
11879 {
11880 /* Need to fix case according to "flags". */
11881 make_case_word(word, cword, flags);
11882 p = cword;
11883 }
11884 else
11885 {
11886 p = word;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011887 if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0
11888 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011889 keepcap = TRUE;
11890 }
11891
Bram Moolenaar7887d882005-07-01 22:33:52 +000011892 /* Add flags and regions after a slash. */
11893 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011894 {
Bram Moolenaar7887d882005-07-01 22:33:52 +000011895 STRCPY(badword, p);
11896 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011897 if (keepcap)
11898 STRCAT(badword, "=");
11899 if (flags & WF_BANNED)
11900 STRCAT(badword, "!");
11901 else if (flags & WF_RARE)
11902 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +000011903 if (flags & WF_REGION)
11904 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011905 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +000011906 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011907 p = badword;
11908 }
11909
11910 ml_append(lnum, p, (colnr_T)0, FALSE);
11911}
11912
11913/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011914 * For ":spelldump": Find matching prefixes for "word". Prepend each to
11915 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011916 * Return the updated line number.
11917 */
11918 static linenr_T
11919apply_prefixes(slang, word, round, flags, startlnum)
11920 slang_T *slang;
11921 char_u *word; /* case-folded word */
11922 int round;
11923 int flags; /* flags with prefix ID */
11924 linenr_T startlnum;
11925{
11926 idx_T arridx[MAXWLEN];
11927 int curi[MAXWLEN];
11928 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000011929 char_u word_up[MAXWLEN];
11930 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011931 int c;
11932 char_u *byts;
11933 idx_T *idxs;
11934 linenr_T lnum = startlnum;
11935 int depth;
11936 int n;
11937 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011938 int i;
11939
Bram Moolenaar53805d12005-08-01 07:08:33 +000011940 /* if the word starts with a lower-case letter make the word with an
11941 * upper-case letter in word_up[]. */
11942 c = PTR2CHAR(word);
11943 if (SPELL_TOUPPER(c) != c)
11944 {
11945 onecap_copy(word, word_up, TRUE);
11946 has_word_up = TRUE;
11947 }
11948
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011949 byts = slang->sl_pbyts;
11950 idxs = slang->sl_pidxs;
11951 if (byts != NULL) /* array not is empty */
11952 {
11953 /*
11954 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011955 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011956 */
11957 depth = 0;
11958 arridx[0] = 0;
11959 curi[0] = 1;
11960 while (depth >= 0 && !got_int)
11961 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011962 n = arridx[depth];
11963 len = byts[n];
11964 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011965 {
11966 /* Done all bytes at this node, go up one level. */
11967 --depth;
11968 line_breakcheck();
11969 }
11970 else
11971 {
11972 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011973 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011974 ++curi[depth];
11975 c = byts[n];
11976 if (c == 0)
11977 {
11978 /* End of prefix, find out how many IDs there are. */
11979 for (i = 1; i < len; ++i)
11980 if (byts[n + i] != 0)
11981 break;
11982 curi[depth] += i - 1;
11983
Bram Moolenaar53805d12005-08-01 07:08:33 +000011984 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
11985 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011986 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011987 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011988 dump_word(prefix, round,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011989 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011990 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011991 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011992
11993 /* Check for prefix that matches the word when the
11994 * first letter is upper-case, but only if the prefix has
11995 * a condition. */
11996 if (has_word_up)
11997 {
11998 c = valid_word_prefix(i, n, flags, word_up, slang,
11999 TRUE);
12000 if (c != 0)
12001 {
12002 vim_strncpy(prefix + depth, word_up,
12003 MAXWLEN - depth - 1);
12004 dump_word(prefix, round,
12005 (c & WF_RAREPFX) ? (flags | WF_RARE)
12006 : flags, lnum++);
12007 }
12008 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012009 }
12010 else
12011 {
12012 /* Normal char, go one level deeper. */
12013 prefix[depth++] = c;
12014 arridx[depth] = idxs[n];
12015 curi[depth] = 1;
12016 }
12017 }
12018 }
12019 }
12020
12021 return lnum;
12022}
12023
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012024#if defined(FEAT_INS_EXPAND) || defined(PROTO)
12025static int spell_expand_need_cap;
12026
12027/*
12028 * Find start of the word in front of the cursor. We don't check if it is
12029 * badly spelled, with completion we can only change the word in front of the
12030 * cursor.
12031 * Used for Insert mode completion CTRL-X ?.
12032 * Returns the column number of the word.
12033 */
12034 int
12035spell_word_start(startcol)
12036 int startcol;
12037{
12038 char_u *line;
12039 char_u *p;
12040 int col = 0;
12041
12042 if (no_spell_checking())
12043 return startcol;
12044
12045 /* Find a word character before "startcol". */
12046 line = ml_get_curline();
12047 for (p = line + startcol; p > line; )
12048 {
12049 mb_ptr_back(line, p);
12050 if (spell_iswordp_nmw(p))
12051 break;
12052 }
12053
12054 /* Go back to start of the word. */
12055 while (p > line)
12056 {
12057 col = p - line;
12058 mb_ptr_back(line, p);
12059 if (!spell_iswordp(p, curbuf))
12060 break;
12061 col = 0;
12062 }
12063
12064 /* Need to check for 'spellcapcheck' now, the word is removed before
12065 * expand_spelling() is called. Therefore the ugly global variable. */
12066 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
12067
12068 return col;
12069}
12070
12071/*
12072 * Get list of spelling suggestions.
12073 * Used for Insert mode completion CTRL-X ?.
12074 * Returns the number of matches. The matches are in "matchp[]", array of
12075 * allocated strings.
12076 */
12077/*ARGSUSED*/
12078 int
12079expand_spelling(lnum, col, pat, matchp)
12080 linenr_T lnum;
12081 int col;
12082 char_u *pat;
12083 char_u ***matchp;
12084{
12085 garray_T ga;
12086
12087 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap);
12088 *matchp = ga.ga_data;
12089 return ga.ga_len;
12090}
12091#endif
12092
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000012093#endif /* FEAT_SYN_HL */