blob: ee3bba2e4fe47404524f57027cb2c3597879466a [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 */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000340#define SP_OTHERERROR -3 /* other error while reading spell file */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000341
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));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000700static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000701static int init_syl_tab __ARGS((slang_T *slang));
702static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000703static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
704static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000705#ifdef FEAT_MBYTE
706static int *mb_str2wide __ARGS((char_u *s));
707#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000708static 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 +0000709static void clear_midword __ARGS((buf_T *buf));
710static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000711static int find_region __ARGS((char_u *rp, char_u *region));
712static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000713static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000714static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000715static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000716static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000717static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000718static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +0000719static 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 +0000720#ifdef FEAT_EVAL
721static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
722#endif
723static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
724static void spell_suggest_intern __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000725static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000726static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000727static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000728static void suggest_try_special __ARGS((suginfo_T *su));
729static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000730static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000731#ifdef FEAT_MBYTE
732static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
733#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000734static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000735static void score_comp_sal __ARGS((suginfo_T *su));
736static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000737static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000738static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000739static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000740static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000741static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000742static 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 +0000743static void add_banned __ARGS((suginfo_T *su, char_u *word));
744static int was_banned __ARGS((suginfo_T *su, char_u *word));
745static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000746static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000747static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000748static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
749static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
750static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000751#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000752static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000753#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000754static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000755static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000756static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
757static 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 +0000758
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000759/*
760 * Use our own character-case definitions, because the current locale may
761 * differ from what the .spl file uses.
762 * These must not be called with negative number!
763 */
764#ifndef FEAT_MBYTE
765/* Non-multi-byte implementation. */
766# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
767# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
768# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
769#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000770# if defined(HAVE_WCHAR_H)
771# include <wchar.h> /* for towupper() and towlower() */
772# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000773/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
774 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
775 * the "w" library function for characters above 255 if available. */
776# ifdef HAVE_TOWLOWER
777# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
778 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
779# else
780# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
781 : (c) < 256 ? spelltab.st_fold[c] : (c))
782# endif
783
784# ifdef HAVE_TOWUPPER
785# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
786 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
787# else
788# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
789 : (c) < 256 ? spelltab.st_upper[c] : (c))
790# endif
791
792# ifdef HAVE_ISWUPPER
793# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
794 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
795# else
796# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000797 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000798# endif
799#endif
800
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000801
802static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000803static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000804static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000805static char *e_affname = N_("Affix name too long in %s line %d: %s");
806static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
807static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000808static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000809
810/*
811 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000812 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000813 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
814 * or when it's OK it remains unchanged.
815 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000816 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000817 * "capcol" is used to check for a Capitalised word after the end of a
818 * sentence. If it's zero then perform the check. Return the column where to
819 * check next, or -1 when no sentence end was found. If it's NULL then don't
820 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000821 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000822 * Returns the length of the word in bytes, also when it's OK, so that the
823 * caller can skip over the word.
824 */
825 int
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000826spell_check(wp, ptr, attrp, capcol)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000827 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000828 char_u *ptr;
829 int *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000830 int *capcol; /* column to check for Capital */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000831{
832 matchinf_T mi; /* Most things are put in "mi" so that it can
833 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000834 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000835 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000836 int wrongcaplen = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000837
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000838 /* A word never starts at a space or a control character. Return quickly
839 * then, skipping over the character. */
840 if (*ptr <= ' ')
841 return 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000842 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000843
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000844 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000845 * 0X99FF. But when a word character follows do check spelling to find
846 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000847 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000848 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000849 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
850 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000851 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000852 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000853 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000854 nrlen = mi.mi_end - ptr;
855 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000856 if (!spell_iswordp(mi.mi_end, wp->w_buffer))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000857 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000858
859 /* Try including the digits in the word. */
860 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000861 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000862 else
863 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000864
Bram Moolenaar0c405862005-06-22 22:26:26 +0000865 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000866 mi.mi_word = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000867 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000868 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000869 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000870 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000871 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000872 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000873
874 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
875 {
876 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000877 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000878 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000879 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000880 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000881 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000882 if (capcol != NULL)
883 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000884
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000885 /* We always use the characters up to the next non-word character,
886 * also for bad words. */
887 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000888
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000889 /* Check caps type later. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000890 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000891
Bram Moolenaar5195e452005-08-19 20:32:47 +0000892 /* case-fold the word with one non-word character, so that we can check
893 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000894 if (*mi.mi_fend != NUL)
895 mb_ptr_adv(mi.mi_fend);
896
897 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
898 MAXWLEN + 1);
899 mi.mi_fwordlen = STRLEN(mi.mi_fword);
900
901 /* The word is bad unless we recognize it. */
902 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +0000903 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000904
905 /*
906 * Loop over the languages specified in 'spelllang'.
907 * We check them all, because a matching word may be longer than an
908 * already found matching word.
909 */
910 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
911 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
912 {
913 /* Check for a matching word in case-folded words. */
914 find_word(&mi, FIND_FOLDWORD);
915
916 /* Check for a matching word in keep-case words. */
917 find_word(&mi, FIND_KEEPWORD);
918
919 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000920 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +0000921
922 /* For a NOBREAK language, may want to use a word without a following
923 * word as a backup. */
924 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
925 && mi.mi_result2 != SP_BAD)
926 {
927 mi.mi_result = mi.mi_result2;
928 mi.mi_end = mi.mi_end2;
929 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000930 }
931
932 if (mi.mi_result != SP_OK)
933 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000934 /* If we found a number skip over it. Allows for "42nd". Do flag
935 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000936 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000937 {
938 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
939 return nrlen;
940 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000941
942 /* When we are at a non-word character there is no error, just
943 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000944 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000945 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000946 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
947 {
948 regmatch_T regmatch;
949
950 /* Check for end of sentence. */
951 regmatch.regprog = wp->w_buffer->b_cap_prog;
952 regmatch.rm_ic = FALSE;
953 if (vim_regexec(&regmatch, ptr, 0))
954 *capcol = (int)(regmatch.endp[0] - ptr);
955 }
956
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000957#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000958 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000959 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000960#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000961 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000962 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000963 else if (mi.mi_end == ptr)
964 /* Always include at least one character. Required for when there
965 * is a mixup in "midword". */
966 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +0000967 else if (mi.mi_result == SP_BAD
968 && LANGP_ENTRY(wp->w_buffer->b_langp, 0)->lp_slang->sl_nobreak)
969 {
970 char_u *p, *fp;
971 int save_result = mi.mi_result;
972
973 /* First language in 'spelllang' is NOBREAK. Find first position
974 * at which any word would be valid. */
975 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
976 p = mi.mi_word;
977 fp = mi.mi_fword;
978 for (;;)
979 {
980 mb_ptr_adv(p);
981 mb_ptr_adv(fp);
982 if (p >= mi.mi_end)
983 break;
984 mi.mi_compoff = fp - mi.mi_fword;
985 find_word(&mi, FIND_COMPOUND);
986 if (mi.mi_result != SP_BAD)
987 {
988 mi.mi_end = p;
989 break;
990 }
991 }
992 mi.mi_result = save_result;
993 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000994
995 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
996 *attrp = highlight_attr[HLF_SPB];
997 else if (mi.mi_result == SP_RARE)
998 *attrp = highlight_attr[HLF_SPR];
999 else
1000 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001001 }
1002
Bram Moolenaar5195e452005-08-19 20:32:47 +00001003 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1004 {
1005 /* Report SpellCap only when the word isn't badly spelled. */
1006 *attrp = highlight_attr[HLF_SPC];
1007 return wrongcaplen;
1008 }
1009
Bram Moolenaar51485f02005-06-04 21:55:20 +00001010 return (int)(mi.mi_end - ptr);
1011}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001012
Bram Moolenaar51485f02005-06-04 21:55:20 +00001013/*
1014 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001015 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1016 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1017 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1018 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001019 *
1020 * For a match mip->mi_result is updated.
1021 */
1022 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001023find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001024 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001025 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001026{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001027 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001028 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001029 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001030 int endidxcnt = 0;
1031 int len;
1032 int wlen = 0;
1033 int flen;
1034 int c;
1035 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001036 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001037#ifdef FEAT_MBYTE
1038 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001039#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001040 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001041 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001042 slang_T *slang = mip->mi_lp->lp_slang;
1043 unsigned flags;
1044 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001045 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001046 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001047 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001048 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001049
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001050 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001051 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001052 /* Check for word with matching case in keep-case tree. */
1053 ptr = mip->mi_word;
1054 flen = 9999; /* no case folding, always enough bytes */
1055 byts = slang->sl_kbyts;
1056 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001057
1058 if (mode == FIND_KEEPCOMPOUND)
1059 /* Skip over the previously found word(s). */
1060 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001061 }
1062 else
1063 {
1064 /* Check for case-folded in case-folded tree. */
1065 ptr = mip->mi_fword;
1066 flen = mip->mi_fwordlen; /* available case-folded bytes */
1067 byts = slang->sl_fbyts;
1068 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001069
1070 if (mode == FIND_PREFIX)
1071 {
1072 /* Skip over the prefix. */
1073 wlen = mip->mi_prefixlen;
1074 flen -= mip->mi_prefixlen;
1075 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001076 else if (mode == FIND_COMPOUND)
1077 {
1078 /* Skip over the previously found word(s). */
1079 wlen = mip->mi_compoff;
1080 flen -= mip->mi_compoff;
1081 }
1082
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001083 }
1084
Bram Moolenaar51485f02005-06-04 21:55:20 +00001085 if (byts == NULL)
1086 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001087
Bram Moolenaar51485f02005-06-04 21:55:20 +00001088 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001089 * Repeat advancing in the tree until:
1090 * - there is a byte that doesn't match,
1091 * - we reach the end of the tree,
1092 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001093 */
1094 for (;;)
1095 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001096 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001097 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001098
1099 len = byts[arridx++];
1100
1101 /* If the first possible byte is a zero the word could end here.
1102 * Remember this index, we first check for the longest word. */
1103 if (byts[arridx] == 0)
1104 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001105 if (endidxcnt == MAXWLEN)
1106 {
1107 /* Must be a corrupted spell file. */
1108 EMSG(_(e_format));
1109 return;
1110 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001111 endlen[endidxcnt] = wlen;
1112 endidx[endidxcnt++] = arridx++;
1113 --len;
1114
1115 /* Skip over the zeros, there can be several flag/region
1116 * combinations. */
1117 while (len > 0 && byts[arridx] == 0)
1118 {
1119 ++arridx;
1120 --len;
1121 }
1122 if (len == 0)
1123 break; /* no children, word must end here */
1124 }
1125
1126 /* Stop looking at end of the line. */
1127 if (ptr[wlen] == NUL)
1128 break;
1129
1130 /* Perform a binary search in the list of accepted bytes. */
1131 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001132 if (c == TAB) /* <Tab> is handled like <Space> */
1133 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001134 lo = arridx;
1135 hi = arridx + len - 1;
1136 while (lo < hi)
1137 {
1138 m = (lo + hi) / 2;
1139 if (byts[m] > c)
1140 hi = m - 1;
1141 else if (byts[m] < c)
1142 lo = m + 1;
1143 else
1144 {
1145 lo = hi = m;
1146 break;
1147 }
1148 }
1149
1150 /* Stop if there is no matching byte. */
1151 if (hi < lo || byts[lo] != c)
1152 break;
1153
1154 /* Continue at the child (if there is one). */
1155 arridx = idxs[lo];
1156 ++wlen;
1157 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001158
1159 /* One space in the good word may stand for several spaces in the
1160 * checked word. */
1161 if (c == ' ')
1162 {
1163 for (;;)
1164 {
1165 if (flen <= 0 && *mip->mi_fend != NUL)
1166 flen = fold_more(mip);
1167 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1168 break;
1169 ++wlen;
1170 --flen;
1171 }
1172 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001173 }
1174
1175 /*
1176 * Verify that one of the possible endings is valid. Try the longest
1177 * first.
1178 */
1179 while (endidxcnt > 0)
1180 {
1181 --endidxcnt;
1182 arridx = endidx[endidxcnt];
1183 wlen = endlen[endidxcnt];
1184
1185#ifdef FEAT_MBYTE
1186 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1187 continue; /* not at first byte of character */
1188#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001189 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001190 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001191 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001192 continue; /* next char is a word character */
1193 word_ends = FALSE;
1194 }
1195 else
1196 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001197 /* The prefix flag is before compound flags. Once a valid prefix flag
1198 * has been found we try compound flags. */
1199 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001200
1201#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001202 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001203 {
1204 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001205 * when folding case. This can be slow, take a shortcut when the
1206 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001207 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001208 if (STRNCMP(ptr, p, wlen) != 0)
1209 {
1210 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1211 mb_ptr_adv(p);
1212 wlen = p - mip->mi_word;
1213 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001214 }
1215#endif
1216
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001217 /* Check flags and region. For FIND_PREFIX check the condition and
1218 * prefix ID.
1219 * Repeat this if there are more flags/region alternatives until there
1220 * is a match. */
1221 res = SP_BAD;
1222 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1223 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001224 {
1225 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001226
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001227 /* For the fold-case tree check that the case of the checked word
1228 * matches with what the word in the tree requires.
1229 * For keep-case tree the case is always right. For prefixes we
1230 * don't bother to check. */
1231 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001232 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001233 if (mip->mi_cend != mip->mi_word + wlen)
1234 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001235 /* mi_capflags was set for a different word length, need
1236 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001237 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001238 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001239 }
1240
Bram Moolenaar0c405862005-06-22 22:26:26 +00001241 if (mip->mi_capflags == WF_KEEPCAP
1242 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001243 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001244 }
1245
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001246 /* When mode is FIND_PREFIX the word must support the prefix:
1247 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001248 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001249 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001250 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001251 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001252 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001253 mip->mi_word + mip->mi_cprefixlen, slang,
1254 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001255 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001256 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001257
1258 /* Use the WF_RARE flag for a rare prefix. */
1259 if (c & WF_RAREPFX)
1260 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001261 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001262 }
1263
Bram Moolenaar78622822005-08-23 21:00:13 +00001264 if (slang->sl_nobreak)
1265 {
1266 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1267 && (flags & WF_BANNED) == 0)
1268 {
1269 /* NOBREAK: found a valid following word. That's all we
1270 * need to know, so return. */
1271 mip->mi_result = SP_OK;
1272 break;
1273 }
1274 }
1275
1276 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1277 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001278 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00001279 /* If there is no flag or the word is shorter than
1280 * COMPOUNDMIN reject it quickly.
1281 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001282 * that's too short... Myspell compatibility requires this
1283 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001284 if (((unsigned)flags >> 24) == 0
1285 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001286 continue;
1287
Bram Moolenaare52325c2005-08-22 22:54:29 +00001288 /* Limit the number of compound words to COMPOUNDMAX if no
1289 * maximum for syllables is specified. */
1290 if (!word_ends && mip->mi_complen + 2 > slang->sl_compmax
1291 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001292 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001293
Bram Moolenaard12a1322005-08-21 22:08:24 +00001294 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001295 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001296 ? slang->sl_compstartflags
1297 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001298 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001299 continue;
1300
Bram Moolenaare52325c2005-08-22 22:54:29 +00001301 if (mode == FIND_COMPOUND)
1302 {
1303 int capflags;
1304
1305 /* Need to check the caps type of the appended compound
1306 * word. */
1307#ifdef FEAT_MBYTE
1308 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1309 mip->mi_compoff) != 0)
1310 {
1311 /* case folding may have changed the length */
1312 p = mip->mi_word;
1313 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1314 mb_ptr_adv(p);
1315 }
1316 else
1317#endif
1318 p = mip->mi_word + mip->mi_compoff;
1319 capflags = captype(p, mip->mi_word + wlen);
1320 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1321 && (flags & WF_FIXCAP) != 0))
1322 continue;
1323
1324 if (capflags != WF_ALLCAP)
1325 {
1326 /* When the character before the word is a word
1327 * character we do not accept a Onecap word. We do
1328 * accept a no-caps word, even when the dictionary
1329 * word specifies ONECAP. */
1330 mb_ptr_back(mip->mi_word, p);
1331 if (spell_iswordp_nmw(p)
1332 ? capflags == WF_ONECAP
1333 : (flags & WF_ONECAP) != 0
1334 && capflags != WF_ONECAP)
1335 continue;
1336 }
1337 }
1338
Bram Moolenaar5195e452005-08-19 20:32:47 +00001339 /* If the word ends the sequence of compound flags of the
1340 * words must match with one of the COMPOUNDFLAGS items and
1341 * the number of syllables must not be too large. */
1342 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1343 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1344 if (word_ends)
1345 {
1346 char_u fword[MAXWLEN];
1347
1348 if (slang->sl_compsylmax < MAXWLEN)
1349 {
1350 /* "fword" is only needed for checking syllables. */
1351 if (ptr == mip->mi_word)
1352 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1353 else
1354 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1355 }
1356 if (!can_compound(slang, fword, mip->mi_compflags))
1357 continue;
1358 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001359 }
1360
Bram Moolenaar78622822005-08-23 21:00:13 +00001361 nobreak_result = SP_OK;
1362
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001363 if (!word_ends)
1364 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001365 int save_result = mip->mi_result;
1366 char_u *save_end = mip->mi_end;
1367
1368 /* Check that a valid word follows. If there is one and we
1369 * are compounding, it will set "mi_result", thus we are
1370 * always finished here. For NOBREAK we only check that a
1371 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001372 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001373 if (slang->sl_nobreak)
1374 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001375
1376 /* Find following word in case-folded tree. */
1377 mip->mi_compoff = endlen[endidxcnt];
1378#ifdef FEAT_MBYTE
1379 if (has_mbyte && mode == FIND_KEEPWORD)
1380 {
1381 /* Compute byte length in case-folded word from "wlen":
1382 * byte length in keep-case word. Length may change when
1383 * folding case. This can be slow, take a shortcut when
1384 * the case-folded word is equal to the keep-case word. */
1385 p = mip->mi_fword;
1386 if (STRNCMP(ptr, p, wlen) != 0)
1387 {
1388 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1389 mb_ptr_adv(p);
1390 mip->mi_compoff = p - mip->mi_fword;
1391 }
1392 }
1393#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001394 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001395 ++mip->mi_complen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001396 find_word(mip, FIND_COMPOUND);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001397
Bram Moolenaar78622822005-08-23 21:00:13 +00001398 /* When NOBREAK any word that matches is OK. Otherwise we
1399 * need to find the longest match, thus try with keep-case and
1400 * prefix too. */
1401 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1402 {
1403 /* Find following word in keep-case tree. */
1404 mip->mi_compoff = wlen;
1405 find_word(mip, FIND_KEEPCOMPOUND);
Bram Moolenaard12a1322005-08-21 22:08:24 +00001406
Bram Moolenaar78622822005-08-23 21:00:13 +00001407 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1408 {
1409 /* Check for following word with prefix. */
1410 mip->mi_compoff = c;
1411 find_prefix(mip, FIND_COMPOUND);
1412 }
1413 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001414 --mip->mi_complen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001415
Bram Moolenaar78622822005-08-23 21:00:13 +00001416 if (slang->sl_nobreak)
1417 {
1418 nobreak_result = mip->mi_result;
1419 mip->mi_result = save_result;
1420 mip->mi_end = save_end;
1421 }
1422 else
1423 {
1424 if (mip->mi_result == SP_OK)
1425 break;
1426 continue;
1427 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001428 }
1429
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001430 if (flags & WF_BANNED)
1431 res = SP_BANNED;
1432 else if (flags & WF_REGION)
1433 {
1434 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001435 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001436 res = SP_OK;
1437 else
1438 res = SP_LOCAL;
1439 }
1440 else if (flags & WF_RARE)
1441 res = SP_RARE;
1442 else
1443 res = SP_OK;
1444
Bram Moolenaar78622822005-08-23 21:00:13 +00001445 /* Always use the longest match and the best result. For NOBREAK
1446 * we separately keep the longest match without a following good
1447 * word as a fall-back. */
1448 if (nobreak_result == SP_BAD)
1449 {
1450 if (mip->mi_result2 > res)
1451 {
1452 mip->mi_result2 = res;
1453 mip->mi_end2 = mip->mi_word + wlen;
1454 }
1455 else if (mip->mi_result2 == res
1456 && mip->mi_end2 < mip->mi_word + wlen)
1457 mip->mi_end2 = mip->mi_word + wlen;
1458 }
1459 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001460 {
1461 mip->mi_result = res;
1462 mip->mi_end = mip->mi_word + wlen;
1463 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001464 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001465 mip->mi_end = mip->mi_word + wlen;
1466
Bram Moolenaar78622822005-08-23 21:00:13 +00001467 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001468 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001469 }
1470
Bram Moolenaar78622822005-08-23 21:00:13 +00001471 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001472 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001473 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001474}
1475
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001476/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00001477 * Return TRUE if "flags" is a valid sequence of compound flags and
1478 * "word[len]" does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001479 */
1480 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001481can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001482 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001483 char_u *word;
1484 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001485{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001486 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001487#ifdef FEAT_MBYTE
1488 char_u uflags[MAXWLEN * 2];
1489 int i;
1490#endif
1491 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001492
1493 if (slang->sl_compprog == NULL)
1494 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001495#ifdef FEAT_MBYTE
1496 if (enc_utf8)
1497 {
1498 /* Need to convert the single byte flags to utf8 characters. */
1499 p = uflags;
1500 for (i = 0; flags[i] != NUL; ++i)
1501 p += mb_char2bytes(flags[i], p);
1502 *p = NUL;
1503 p = uflags;
1504 }
1505 else
1506#endif
1507 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001508 regmatch.regprog = slang->sl_compprog;
1509 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001510 if (!vim_regexec(&regmatch, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001511 return FALSE;
1512
Bram Moolenaare52325c2005-08-22 22:54:29 +00001513 /* Count the number of syllables. This may be slow, do it last. If there
1514 * are too many syllables AND the number of compound words is above
1515 * COMPOUNDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001516 if (slang->sl_compsylmax < MAXWLEN
1517 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001518 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001519 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001520}
1521
1522/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001523 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1524 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001525 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001526 */
1527 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001528valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001529 int totprefcnt; /* nr of prefix IDs */
1530 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001531 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001532 char_u *word;
1533 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001534 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001535{
1536 int prefcnt;
1537 int pidx;
1538 regprog_T *rp;
1539 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001540 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001541
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001542 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001543 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1544 {
1545 pidx = slang->sl_pidxs[arridx + prefcnt];
1546
1547 /* Check the prefix ID. */
1548 if (prefid != (pidx & 0xff))
1549 continue;
1550
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001551 /* Check if the prefix doesn't combine and the word already has a
1552 * suffix. */
1553 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1554 continue;
1555
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001556 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001557 * stored in the two bytes above the prefix ID byte. */
1558 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001559 if (rp != NULL)
1560 {
1561 regmatch.regprog = rp;
1562 regmatch.rm_ic = FALSE;
1563 if (!vim_regexec(&regmatch, word, 0))
1564 continue;
1565 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001566 else if (cond_req)
1567 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001568
Bram Moolenaar53805d12005-08-01 07:08:33 +00001569 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001570 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001571 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001572 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001573}
1574
1575/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001576 * Check if the word at "mip->mi_word" has a matching prefix.
1577 * If it does, then check the following word.
1578 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001579 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1580 * prefix in a compound word.
1581 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001582 * For a match mip->mi_result is updated.
1583 */
1584 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001585find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001586 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001587 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001588{
1589 idx_T arridx = 0;
1590 int len;
1591 int wlen = 0;
1592 int flen;
1593 int c;
1594 char_u *ptr;
1595 idx_T lo, hi, m;
1596 slang_T *slang = mip->mi_lp->lp_slang;
1597 char_u *byts;
1598 idx_T *idxs;
1599
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001600 byts = slang->sl_pbyts;
1601 if (byts == NULL)
1602 return; /* array is empty */
1603
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001604 /* We use the case-folded word here, since prefixes are always
1605 * case-folded. */
1606 ptr = mip->mi_fword;
1607 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001608 if (mode == FIND_COMPOUND)
1609 {
1610 /* Skip over the previously found word(s). */
1611 ptr += mip->mi_compoff;
1612 flen -= mip->mi_compoff;
1613 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001614 idxs = slang->sl_pidxs;
1615
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001616 /*
1617 * Repeat advancing in the tree until:
1618 * - there is a byte that doesn't match,
1619 * - we reach the end of the tree,
1620 * - or we reach the end of the line.
1621 */
1622 for (;;)
1623 {
1624 if (flen == 0 && *mip->mi_fend != NUL)
1625 flen = fold_more(mip);
1626
1627 len = byts[arridx++];
1628
1629 /* If the first possible byte is a zero the prefix could end here.
1630 * Check if the following word matches and supports the prefix. */
1631 if (byts[arridx] == 0)
1632 {
1633 /* There can be several prefixes with different conditions. We
1634 * try them all, since we don't know which one will give the
1635 * longest match. The word is the same each time, pass the list
1636 * of possible prefixes to find_word(). */
1637 mip->mi_prefarridx = arridx;
1638 mip->mi_prefcnt = len;
1639 while (len > 0 && byts[arridx] == 0)
1640 {
1641 ++arridx;
1642 --len;
1643 }
1644 mip->mi_prefcnt -= len;
1645
1646 /* Find the word that comes after the prefix. */
1647 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001648 if (mode == FIND_COMPOUND)
1649 /* Skip over the previously found word(s). */
1650 mip->mi_prefixlen += mip->mi_compoff;
1651
Bram Moolenaar53805d12005-08-01 07:08:33 +00001652#ifdef FEAT_MBYTE
1653 if (has_mbyte)
1654 {
1655 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001656 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1657 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001658 }
1659 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001660 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001661#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001662 find_word(mip, FIND_PREFIX);
1663
1664
1665 if (len == 0)
1666 break; /* no children, word must end here */
1667 }
1668
1669 /* Stop looking at end of the line. */
1670 if (ptr[wlen] == NUL)
1671 break;
1672
1673 /* Perform a binary search in the list of accepted bytes. */
1674 c = ptr[wlen];
1675 lo = arridx;
1676 hi = arridx + len - 1;
1677 while (lo < hi)
1678 {
1679 m = (lo + hi) / 2;
1680 if (byts[m] > c)
1681 hi = m - 1;
1682 else if (byts[m] < c)
1683 lo = m + 1;
1684 else
1685 {
1686 lo = hi = m;
1687 break;
1688 }
1689 }
1690
1691 /* Stop if there is no matching byte. */
1692 if (hi < lo || byts[lo] != c)
1693 break;
1694
1695 /* Continue at the child (if there is one). */
1696 arridx = idxs[lo];
1697 ++wlen;
1698 --flen;
1699 }
1700}
1701
1702/*
1703 * Need to fold at least one more character. Do until next non-word character
1704 * for efficiency.
1705 * Return the length of the folded chars in bytes.
1706 */
1707 static int
1708fold_more(mip)
1709 matchinf_T *mip;
1710{
1711 int flen;
1712 char_u *p;
1713
1714 p = mip->mi_fend;
1715 do
1716 {
1717 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001718 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001719
1720 /* Include the non-word character so that we can check for the
1721 * word end. */
1722 if (*mip->mi_fend != NUL)
1723 mb_ptr_adv(mip->mi_fend);
1724
1725 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1726 mip->mi_fword + mip->mi_fwordlen,
1727 MAXWLEN - mip->mi_fwordlen);
1728 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1729 mip->mi_fwordlen += flen;
1730 return flen;
1731}
1732
1733/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001734 * Check case flags for a word. Return TRUE if the word has the requested
1735 * case.
1736 */
1737 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001738spell_valid_case(wordflags, treeflags)
1739 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001740 int treeflags; /* flags for the word in the spell tree */
1741{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001742 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001743 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001744 && ((treeflags & WF_ONECAP) == 0
1745 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001746}
1747
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001748/*
1749 * Return TRUE if spell checking is not enabled.
1750 */
1751 static int
1752no_spell_checking()
1753{
1754 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
1755 {
1756 EMSG(_("E756: Spell checking is not enabled"));
1757 return TRUE;
1758 }
1759 return FALSE;
1760}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001761
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001762/*
1763 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001764 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001765 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1766 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001767 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001768 */
1769 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001770spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001771 int dir; /* FORWARD or BACKWARD */
1772 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001773 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001774{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001775 linenr_T lnum;
1776 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001777 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001778 char_u *line;
1779 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001780 char_u *endp;
1781 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001782 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001783 int has_syntax = syntax_present(curbuf);
1784 int col;
1785 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001786 char_u *buf = NULL;
1787 int buflen = 0;
1788 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001789 int capcol = -1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001790
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001791 if (no_spell_checking())
Bram Moolenaar6de68532005-08-24 22:08:48 +00001792 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001793
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001794 /*
1795 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001796 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001797 *
1798 * When searching backwards, we continue in the line to find the last
1799 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001800 *
1801 * We concatenate the start of the next line, so that wrapped words work
1802 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1803 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001804 */
1805 lnum = curwin->w_cursor.lnum;
1806 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001807
1808 while (!got_int)
1809 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001810 line = ml_get(lnum);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001811
Bram Moolenaar0c405862005-06-22 22:26:26 +00001812 len = STRLEN(line);
1813 if (buflen < len + MAXWLEN + 2)
1814 {
1815 vim_free(buf);
1816 buflen = len + MAXWLEN + 2;
1817 buf = alloc(buflen);
1818 if (buf == NULL)
1819 break;
1820 }
1821
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001822 /* In first line check first word for Capital. */
1823 if (lnum == 1)
1824 capcol = 0;
1825
1826 /* For checking first word with a capital skip white space. */
1827 if (capcol == 0)
1828 capcol = skipwhite(line) - line;
1829
Bram Moolenaar0c405862005-06-22 22:26:26 +00001830 /* Copy the line into "buf" and append the start of the next line if
1831 * possible. */
1832 STRCPY(buf, line);
1833 if (lnum < curbuf->b_ml.ml_line_count)
1834 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1835
1836 p = buf + skip;
1837 endp = buf + len;
1838 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001839 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001840 /* When searching backward don't search after the cursor. */
1841 if (dir == BACKWARD
1842 && lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001843 && (colnr_T)(p - buf) >= curwin->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001844 break;
1845
1846 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001847 attr = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001848 len = spell_check(curwin, p, &attr, &capcol);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001849
1850 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001851 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001852 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001853 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001854 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001855 /* When searching forward only accept a bad word after
1856 * the cursor. */
1857 if (dir == BACKWARD
1858 || lnum > curwin->w_cursor.lnum
1859 || (lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001860 && (colnr_T)(curline ? p - buf + len
1861 : p - buf)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001862 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001863 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001864 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001865 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001866 col = p - buf;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001867 (void)syn_get_id(lnum, (colnr_T)col,
1868 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001869 }
1870 else
1871 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001872
Bram Moolenaar51485f02005-06-04 21:55:20 +00001873 if (can_spell)
1874 {
1875 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001876 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001877#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001878 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001879#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001880 if (dir == FORWARD)
1881 {
1882 /* No need to search further. */
1883 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001884 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001885 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001886 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001887 else if (curline)
1888 /* Insert mode completion: put cursor after
1889 * the bad word. */
1890 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001891 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001892 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001893 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001894 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001895 }
1896
Bram Moolenaar51485f02005-06-04 21:55:20 +00001897 /* advance to character after the word */
1898 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001899 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001900 }
1901
Bram Moolenaar5195e452005-08-19 20:32:47 +00001902 if (dir == BACKWARD && found_pos.lnum != 0)
1903 {
1904 /* Use the last match in the line. */
1905 curwin->w_cursor = found_pos;
1906 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001907 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001908 }
1909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001910 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001911 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001912
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001913 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001914 if (dir == BACKWARD)
1915 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001916 if (lnum == 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001917 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001918 --lnum;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001919 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001920 }
1921 else
1922 {
1923 if (lnum == curbuf->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001924 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001925 ++lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001926
1927 /* Skip the characters at the start of the next line that were
1928 * included in a match crossing line boundaries. */
1929 if (attr == 0)
1930 skip = p - endp;
1931 else
1932 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001933
1934 /* Capscol skips over the inserted space. */
1935 --capcol;
1936
1937 /* But after empty line check first word in next line */
1938 if (*skipwhite(line) == NUL)
1939 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001940 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001941
1942 line_breakcheck();
1943 }
1944
Bram Moolenaar0c405862005-06-22 22:26:26 +00001945 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001946 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001947}
1948
1949/*
1950 * For spell checking: concatenate the start of the following line "line" into
1951 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
1952 */
1953 void
1954spell_cat_line(buf, line, maxlen)
1955 char_u *buf;
1956 char_u *line;
1957 int maxlen;
1958{
1959 char_u *p;
1960 int n;
1961
1962 p = skipwhite(line);
1963 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1964 p = skipwhite(p + 1);
1965
1966 if (*p != NUL)
1967 {
1968 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001969 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001970 n = p - line;
1971 if (n >= maxlen)
1972 n = maxlen - 1;
1973 vim_memset(buf + 1, ' ', n);
1974 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001975}
1976
1977/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001978 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001979 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001980 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001981 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001982spell_load_lang(lang)
1983 char_u *lang;
1984{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001985 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001986 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001987 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001988
Bram Moolenaarb765d632005-06-07 21:00:02 +00001989 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001990 * It's truncated when an error is detected. */
1991 STRCPY(langcp, lang);
1992
Bram Moolenaarb765d632005-06-07 21:00:02 +00001993 /*
1994 * Find the first spell file for "lang" in 'runtimepath' and load it.
1995 */
1996 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1997 "spell/%s.%s.spl", lang, spell_enc());
1998 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001999
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002000 if (r == FAIL && *langcp != NUL)
2001 {
2002 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002003 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002004 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002005 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002006 }
2007
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002008 if (r == FAIL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002009 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2010 lang, spell_enc(), lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002011 else if (*langcp != NUL)
2012 {
2013 /* Load all the additions. */
2014 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
2015 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
2016 }
2017}
2018
2019/*
2020 * Return the encoding used for spell checking: Use 'encoding', except that we
2021 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2022 */
2023 static char_u *
2024spell_enc()
2025{
2026
2027#ifdef FEAT_MBYTE
2028 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2029 return p_enc;
2030#endif
2031 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002032}
2033
2034/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002035 * Get the name of the .spl file for the internal wordlist into
2036 * "fname[MAXPATHL]".
2037 */
2038 static void
2039int_wordlist_spl(fname)
2040 char_u *fname;
2041{
2042 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2043 int_wordlist, spell_enc());
2044}
2045
2046/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002047 * Allocate a new slang_T.
2048 * Caller must fill "sl_next".
2049 */
2050 static slang_T *
2051slang_alloc(lang)
2052 char_u *lang;
2053{
2054 slang_T *lp;
2055
Bram Moolenaar51485f02005-06-04 21:55:20 +00002056 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002057 if (lp != NULL)
2058 {
2059 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002060 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002061 lp->sl_compmax = MAXWLEN;
2062 lp->sl_compminlen = MAXWLEN;
2063 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002064 }
2065 return lp;
2066}
2067
2068/*
2069 * Free the contents of an slang_T and the structure itself.
2070 */
2071 static void
2072slang_free(lp)
2073 slang_T *lp;
2074{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002075 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002076 vim_free(lp->sl_fname);
2077 slang_clear(lp);
2078 vim_free(lp);
2079}
2080
2081/*
2082 * Clear an slang_T so that the file can be reloaded.
2083 */
2084 static void
2085slang_clear(lp)
2086 slang_T *lp;
2087{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002088 garray_T *gap;
2089 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002090 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002091 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002092
Bram Moolenaar51485f02005-06-04 21:55:20 +00002093 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002094 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002095 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002096 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002097 vim_free(lp->sl_pbyts);
2098 lp->sl_pbyts = NULL;
2099
Bram Moolenaar51485f02005-06-04 21:55:20 +00002100 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002101 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002102 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002103 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002104 vim_free(lp->sl_pidxs);
2105 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002106
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002107 gap = &lp->sl_rep;
2108 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002109 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002110 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2111 vim_free(ftp->ft_from);
2112 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002113 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002114 ga_clear(gap);
2115
2116 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002117 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002118 {
2119 /* "ga_len" is set to 1 without adding an item for latin1 */
2120 if (gap->ga_data != NULL)
2121 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2122 for (i = 0; i < gap->ga_len; ++i)
2123 vim_free(((int **)gap->ga_data)[i]);
2124 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002125 else
2126 /* SAL items: free salitem_T items */
2127 while (gap->ga_len > 0)
2128 {
2129 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2130 vim_free(smp->sm_lead);
2131 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2132 vim_free(smp->sm_to);
2133#ifdef FEAT_MBYTE
2134 vim_free(smp->sm_lead_w);
2135 vim_free(smp->sm_oneof_w);
2136 vim_free(smp->sm_to_w);
2137#endif
2138 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002139 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002140
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002141 for (i = 0; i < lp->sl_prefixcnt; ++i)
2142 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002143 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002144 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002145 lp->sl_prefprog = NULL;
2146
2147 vim_free(lp->sl_midword);
2148 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002149
Bram Moolenaar5195e452005-08-19 20:32:47 +00002150 vim_free(lp->sl_compprog);
2151 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002152 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002153 lp->sl_compprog = NULL;
2154 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002155 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002156
2157 vim_free(lp->sl_syllable);
2158 lp->sl_syllable = NULL;
2159 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002160
Bram Moolenaarea424162005-06-16 21:51:00 +00002161#ifdef FEAT_MBYTE
2162 {
2163 int todo = lp->sl_map_hash.ht_used;
2164 hashitem_T *hi;
2165
2166 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
2167 if (!HASHITEM_EMPTY(hi))
2168 {
2169 --todo;
2170 vim_free(hi->hi_key);
2171 }
2172 }
2173 hash_clear(&lp->sl_map_hash);
2174#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002175
2176 lp->sl_compmax = MAXWLEN;
2177 lp->sl_compminlen = MAXWLEN;
2178 lp->sl_compsylmax = MAXWLEN;
2179 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002180}
2181
2182/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002183 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002184 * Invoked through do_in_runtimepath().
2185 */
2186 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002187spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002188 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002189 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002190{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002191 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002192}
2193
2194/*
2195 * Load one spell file and store the info into a slang_T.
2196 *
2197 * This is invoked in two ways:
2198 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2199 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2200 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2201 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002202 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002203 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002204 static slang_T *
2205spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002206 char_u *fname;
2207 char_u *lang;
2208 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002209 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002210{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002211 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002212 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002213 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002214 char_u *bp;
2215 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002216 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002217 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002218 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002219 int round;
2220 char_u *save_sourcing_name = sourcing_name;
2221 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002222 slang_T *lp = NULL;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002223 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002224 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002225 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002226
Bram Moolenaarb765d632005-06-07 21:00:02 +00002227 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002228 if (fd == NULL)
2229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002230 if (!silent)
2231 EMSG2(_(e_notopen), fname);
2232 else if (p_verbose > 2)
2233 {
2234 verbose_enter();
2235 smsg((char_u *)e_notopen, fname);
2236 verbose_leave();
2237 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002238 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002239 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002240 if (p_verbose > 2)
2241 {
2242 verbose_enter();
2243 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2244 verbose_leave();
2245 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002246
Bram Moolenaarb765d632005-06-07 21:00:02 +00002247 if (old_lp == NULL)
2248 {
2249 lp = slang_alloc(lang);
2250 if (lp == NULL)
2251 goto endFAIL;
2252
2253 /* Remember the file name, used to reload the file when it's updated. */
2254 lp->sl_fname = vim_strsave(fname);
2255 if (lp->sl_fname == NULL)
2256 goto endFAIL;
2257
2258 /* Check for .add.spl. */
2259 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2260 }
2261 else
2262 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002263
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002264 /* Set sourcing_name, so that error messages mention the file name. */
2265 sourcing_name = fname;
2266 sourcing_lnum = 0;
2267
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002268 /* <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002269 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002270 for (i = 0; i < VIMSPELLMAGICL; ++i)
2271 buf[i] = getc(fd); /* <fileID> */
2272 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2273 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002274 EMSG(_("E757: This does not look like a spell file"));
2275 goto endFAIL;
2276 }
2277 c = getc(fd); /* <versionnr> */
2278 if (c < VIMSPELLVERSION)
2279 {
2280 EMSG(_("E771: Old spell file, needs to be updated"));
2281 goto endFAIL;
2282 }
2283 else if (c > VIMSPELLVERSION)
2284 {
2285 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002286 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002287 }
2288
Bram Moolenaar5195e452005-08-19 20:32:47 +00002289
2290 /*
2291 * <SECTIONS>: <section> ... <sectionend>
2292 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2293 */
2294 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002295 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002296 n = getc(fd); /* <sectionID> or <sectionend> */
2297 if (n == SN_END)
2298 break;
2299 c = getc(fd); /* <sectionflags> */
2300 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2301 /* <sectionlen> */
2302 if (len < 0)
2303 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002304
Bram Moolenaar5195e452005-08-19 20:32:47 +00002305 res = 0;
2306 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002307 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002308 case SN_REGION:
2309 res = read_region_section(fd, lp, len);
2310 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002311
Bram Moolenaar5195e452005-08-19 20:32:47 +00002312 case SN_CHARFLAGS:
2313 res = read_charflags_section(fd);
2314 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002315
Bram Moolenaar5195e452005-08-19 20:32:47 +00002316 case SN_MIDWORD:
2317 lp->sl_midword = read_string(fd, len); /* <midword> */
2318 if (lp->sl_midword == NULL)
2319 goto endFAIL;
2320 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002321
Bram Moolenaar5195e452005-08-19 20:32:47 +00002322 case SN_PREFCOND:
2323 res = read_prefcond_section(fd, lp);
2324 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002325
Bram Moolenaar5195e452005-08-19 20:32:47 +00002326 case SN_REP:
2327 res = read_rep_section(fd, lp);
2328 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002329
Bram Moolenaar5195e452005-08-19 20:32:47 +00002330 case SN_SAL:
2331 res = read_sal_section(fd, lp);
2332 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002333
Bram Moolenaar5195e452005-08-19 20:32:47 +00002334 case SN_SOFO:
2335 res = read_sofo_section(fd, lp);
2336 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002337
Bram Moolenaar5195e452005-08-19 20:32:47 +00002338 case SN_MAP:
2339 p = read_string(fd, len); /* <mapstr> */
2340 if (p == NULL)
2341 goto endFAIL;
2342 set_map_str(lp, p);
2343 vim_free(p);
2344 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002345
Bram Moolenaar5195e452005-08-19 20:32:47 +00002346 case SN_COMPOUND:
2347 res = read_compound(fd, lp, len);
2348 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002349
Bram Moolenaar78622822005-08-23 21:00:13 +00002350 case SN_NOBREAK:
2351 lp->sl_nobreak = TRUE;
2352 break;
2353
Bram Moolenaar5195e452005-08-19 20:32:47 +00002354 case SN_SYLLABLE:
2355 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2356 if (lp->sl_syllable == NULL)
2357 goto endFAIL;
2358 if (init_syl_tab(lp) == FAIL)
2359 goto endFAIL;
2360 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002361
Bram Moolenaar5195e452005-08-19 20:32:47 +00002362 default:
2363 /* Unsupported section. When it's required give an error
2364 * message. When it's not required skip the contents. */
2365 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002366 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002367 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002368 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002369 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002370 while (--len >= 0)
2371 if (getc(fd) < 0)
2372 goto truncerr;
2373 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002374 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002375 if (res == SP_FORMERROR)
2376 {
2377formerr:
2378 EMSG(_(e_format));
2379 goto endFAIL;
2380 }
2381 if (res == SP_TRUNCERROR)
2382 {
2383truncerr:
2384 EMSG(_(e_spell_trunc));
2385 goto endFAIL;
2386 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002387 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002388 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002389 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002390
Bram Moolenaar51485f02005-06-04 21:55:20 +00002391 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002392 * round 2: <KWORDTREE>
2393 * round 3: <PREFIXTREE> */
2394 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002395 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002396 /* The tree size was computed when writing the file, so that we can
2397 * allocate it as one long block. <nodecount> */
2398 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2399 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002400 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002401 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002402 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002403 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002404 bp = lalloc((long_u)len, TRUE);
2405 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002406 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002407 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002408 lp->sl_fbyts = bp;
2409 else if (round == 2)
2410 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002411 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002412 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002413
2414 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002415 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2416 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002417 goto endFAIL;
2418 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002419 lp->sl_fidxs = ip;
2420 else if (round == 2)
2421 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002422 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002423 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002424
2425 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002426 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002427 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002428 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002429 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002430 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002431 }
2432 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002433
Bram Moolenaarb765d632005-06-07 21:00:02 +00002434 /* For a new file link it in the list of spell files. */
2435 if (old_lp == NULL)
2436 {
2437 lp->sl_next = first_lang;
2438 first_lang = lp;
2439 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002440
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002441 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002442
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002443endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002444 if (lang != NULL)
2445 /* truncating the name signals the error to spell_load_lang() */
2446 *lang = NUL;
2447 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002448 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002449 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002450 lp = NULL;
2451 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002452
2453endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002454 if (fd != NULL)
2455 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002456 sourcing_name = save_sourcing_name;
2457 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002458
2459 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002460}
2461
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002462/*
2463 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002464 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002465 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002466 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2467 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002468 */
2469 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002470read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002471 FILE *fd;
2472 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002473 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002474{
2475 int cnt = 0;
2476 int i;
2477 char_u *str;
2478
2479 /* read the length bytes, MSB first */
2480 for (i = 0; i < cnt_bytes; ++i)
2481 cnt = (cnt << 8) + getc(fd);
2482 if (cnt < 0)
2483 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002484 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002485 return NULL;
2486 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002487 *cntp = cnt;
2488 if (cnt == 0)
2489 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002490
Bram Moolenaar5195e452005-08-19 20:32:47 +00002491 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002492 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002493 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002494 return str;
2495}
2496
Bram Moolenaar7887d882005-07-01 22:33:52 +00002497/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002498 * Read a string of length "cnt" from "fd" into allocated memory.
2499 * Returns NULL when out of memory.
2500 */
2501 static char_u *
2502read_string(fd, cnt)
2503 FILE *fd;
2504 int cnt;
2505{
2506 char_u *str;
2507 int i;
2508
2509 /* allocate memory */
2510 str = alloc((unsigned)cnt + 1);
2511 if (str != NULL)
2512 {
2513 /* Read the string. Doesn't check for truncated file. */
2514 for (i = 0; i < cnt; ++i)
2515 str[i] = getc(fd);
2516 str[i] = NUL;
2517 }
2518 return str;
2519}
2520
2521/*
2522 * Read SN_REGION: <regionname> ...
2523 * Return SP_*ERROR flags.
2524 */
2525 static int
2526read_region_section(fd, lp, len)
2527 FILE *fd;
2528 slang_T *lp;
2529 int len;
2530{
2531 int i;
2532
2533 if (len > 16)
2534 return SP_FORMERROR;
2535 for (i = 0; i < len; ++i)
2536 lp->sl_regions[i] = getc(fd); /* <regionname> */
2537 lp->sl_regions[len] = NUL;
2538 return 0;
2539}
2540
2541/*
2542 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2543 * <folcharslen> <folchars>
2544 * Return SP_*ERROR flags.
2545 */
2546 static int
2547read_charflags_section(fd)
2548 FILE *fd;
2549{
2550 char_u *flags;
2551 char_u *fol;
2552 int flagslen, follen;
2553
2554 /* <charflagslen> <charflags> */
2555 flags = read_cnt_string(fd, 1, &flagslen);
2556 if (flagslen < 0)
2557 return flagslen;
2558
2559 /* <folcharslen> <folchars> */
2560 fol = read_cnt_string(fd, 2, &follen);
2561 if (follen < 0)
2562 {
2563 vim_free(flags);
2564 return follen;
2565 }
2566
2567 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
2568 if (flags != NULL && fol != NULL)
2569 set_spell_charflags(flags, flagslen, fol);
2570
2571 vim_free(flags);
2572 vim_free(fol);
2573
2574 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
2575 if ((flags == NULL) != (fol == NULL))
2576 return SP_FORMERROR;
2577 return 0;
2578}
2579
2580/*
2581 * Read SN_PREFCOND section.
2582 * Return SP_*ERROR flags.
2583 */
2584 static int
2585read_prefcond_section(fd, lp)
2586 FILE *fd;
2587 slang_T *lp;
2588{
2589 int cnt;
2590 int i;
2591 int n;
2592 char_u *p;
2593 char_u buf[MAXWLEN + 1];
2594
2595 /* <prefcondcnt> <prefcond> ... */
2596 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
2597 if (cnt <= 0)
2598 return SP_FORMERROR;
2599
2600 lp->sl_prefprog = (regprog_T **)alloc_clear(
2601 (unsigned)sizeof(regprog_T *) * cnt);
2602 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002603 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002604 lp->sl_prefixcnt = cnt;
2605
2606 for (i = 0; i < cnt; ++i)
2607 {
2608 /* <prefcond> : <condlen> <condstr> */
2609 n = getc(fd); /* <condlen> */
2610 if (n < 0 || n >= MAXWLEN)
2611 return SP_FORMERROR;
2612
2613 /* When <condlen> is zero we have an empty condition. Otherwise
2614 * compile the regexp program used to check for the condition. */
2615 if (n > 0)
2616 {
2617 buf[0] = '^'; /* always match at one position only */
2618 p = buf + 1;
2619 while (n-- > 0)
2620 *p++ = getc(fd); /* <condstr> */
2621 *p = NUL;
2622 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
2623 }
2624 }
2625 return 0;
2626}
2627
2628/*
2629 * Read REP items section from "fd": <repcount> <rep> ...
2630 * Return SP_*ERROR flags.
2631 */
2632 static int
2633read_rep_section(fd, slang)
2634 FILE *fd;
2635 slang_T *slang;
2636{
2637 int cnt;
2638 garray_T *gap;
2639 fromto_T *ftp;
2640 short *first;
2641 int i;
2642
2643 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
2644 if (cnt < 0)
2645 return SP_TRUNCERROR;
2646
2647 gap = &slang->sl_rep;
2648 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002649 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002650
2651 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
2652 for (; gap->ga_len < cnt; ++gap->ga_len)
2653 {
2654 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
2655 ftp->ft_from = read_cnt_string(fd, 1, &i);
2656 if (i < 0)
2657 return i;
2658 if (i == 0)
2659 return SP_FORMERROR;
2660 ftp->ft_to = read_cnt_string(fd, 1, &i);
2661 if (i <= 0)
2662 {
2663 vim_free(ftp->ft_from);
2664 if (i < 0)
2665 return i;
2666 return SP_FORMERROR;
2667 }
2668 }
2669
2670 /* Fill the first-index table. */
2671 first = slang->sl_rep_first;
2672 for (i = 0; i < 256; ++i)
2673 first[i] = -1;
2674 for (i = 0; i < gap->ga_len; ++i)
2675 {
2676 ftp = &((fromto_T *)gap->ga_data)[i];
2677 if (first[*ftp->ft_from] == -1)
2678 first[*ftp->ft_from] = i;
2679 }
2680 return 0;
2681}
2682
2683/*
2684 * Read SN_SAL section: <salflags> <salcount> <sal> ...
2685 * Return SP_*ERROR flags.
2686 */
2687 static int
2688read_sal_section(fd, slang)
2689 FILE *fd;
2690 slang_T *slang;
2691{
2692 int i;
2693 int cnt;
2694 garray_T *gap;
2695 salitem_T *smp;
2696 int ccnt;
2697 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002698 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002699
2700 slang->sl_sofo = FALSE;
2701
2702 i = getc(fd); /* <salflags> */
2703 if (i & SAL_F0LLOWUP)
2704 slang->sl_followup = TRUE;
2705 if (i & SAL_COLLAPSE)
2706 slang->sl_collapse = TRUE;
2707 if (i & SAL_REM_ACCENTS)
2708 slang->sl_rem_accents = TRUE;
2709
2710 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
2711 if (cnt < 0)
2712 return SP_TRUNCERROR;
2713
2714 gap = &slang->sl_sal;
2715 ga_init2(gap, sizeof(salitem_T), 10);
2716 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002717 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002718
2719 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
2720 for (; gap->ga_len < cnt; ++gap->ga_len)
2721 {
2722 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2723 ccnt = getc(fd); /* <salfromlen> */
2724 if (ccnt < 0)
2725 return SP_TRUNCERROR;
2726 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002727 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002728 smp->sm_lead = p;
2729
2730 /* Read up to the first special char into sm_lead. */
2731 for (i = 0; i < ccnt; ++i)
2732 {
2733 c = getc(fd); /* <salfrom> */
2734 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
2735 break;
2736 *p++ = c;
2737 }
2738 smp->sm_leadlen = p - smp->sm_lead;
2739 *p++ = NUL;
2740
2741 /* Put (abc) chars in sm_oneof, if any. */
2742 if (c == '(')
2743 {
2744 smp->sm_oneof = p;
2745 for (++i; i < ccnt; ++i)
2746 {
2747 c = getc(fd); /* <salfrom> */
2748 if (c == ')')
2749 break;
2750 *p++ = c;
2751 }
2752 *p++ = NUL;
2753 if (++i < ccnt)
2754 c = getc(fd);
2755 }
2756 else
2757 smp->sm_oneof = NULL;
2758
2759 /* Any following chars go in sm_rules. */
2760 smp->sm_rules = p;
2761 if (i < ccnt)
2762 /* store the char we got while checking for end of sm_lead */
2763 *p++ = c;
2764 for (++i; i < ccnt; ++i)
2765 *p++ = getc(fd); /* <salfrom> */
2766 *p++ = NUL;
2767
2768 /* <saltolen> <salto> */
2769 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
2770 if (ccnt < 0)
2771 {
2772 vim_free(smp->sm_lead);
2773 return ccnt;
2774 }
2775
2776#ifdef FEAT_MBYTE
2777 if (has_mbyte)
2778 {
2779 /* convert the multi-byte strings to wide char strings */
2780 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2781 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2782 if (smp->sm_oneof == NULL)
2783 smp->sm_oneof_w = NULL;
2784 else
2785 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
2786 if (smp->sm_to == NULL)
2787 smp->sm_to_w = NULL;
2788 else
2789 smp->sm_to_w = mb_str2wide(smp->sm_to);
2790 if (smp->sm_lead_w == NULL
2791 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
2792 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
2793 {
2794 vim_free(smp->sm_lead);
2795 vim_free(smp->sm_to);
2796 vim_free(smp->sm_lead_w);
2797 vim_free(smp->sm_oneof_w);
2798 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002799 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002800 }
2801 }
2802#endif
2803 }
2804
2805 /* Fill the first-index table. */
2806 set_sal_first(slang);
2807
2808 return 0;
2809}
2810
2811/*
2812 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
2813 * Return SP_*ERROR flags.
2814 */
2815 static int
2816read_sofo_section(fd, slang)
2817 FILE *fd;
2818 slang_T *slang;
2819{
2820 int cnt;
2821 char_u *from, *to;
2822 int res;
2823
2824 slang->sl_sofo = TRUE;
2825
2826 /* <sofofromlen> <sofofrom> */
2827 from = read_cnt_string(fd, 2, &cnt);
2828 if (cnt < 0)
2829 return cnt;
2830
2831 /* <sofotolen> <sofoto> */
2832 to = read_cnt_string(fd, 2, &cnt);
2833 if (cnt < 0)
2834 {
2835 vim_free(from);
2836 return cnt;
2837 }
2838
2839 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
2840 if (from != NULL && to != NULL)
2841 res = set_sofo(slang, from, to);
2842 else if (from != NULL || to != NULL)
2843 res = SP_FORMERROR; /* only one of two strings is an error */
2844 else
2845 res = 0;
2846
2847 vim_free(from);
2848 vim_free(to);
2849 return res;
2850}
2851
2852/*
2853 * Read the compound section from the .spl file:
2854 * <compmax> <compminlen> <compsylmax> <compflags>
2855 * Returns SP_*ERROR flags.
2856 */
2857 static int
2858read_compound(fd, slang, len)
2859 FILE *fd;
2860 slang_T *slang;
2861 int len;
2862{
2863 int todo = len;
2864 int c;
2865 int atstart;
2866 char_u *pat;
2867 char_u *pp;
2868 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002869 char_u *ap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002870
2871 if (todo < 2)
2872 return SP_FORMERROR; /* need at least two bytes */
2873
2874 --todo;
2875 c = getc(fd); /* <compmax> */
2876 if (c < 2)
2877 c = MAXWLEN;
2878 slang->sl_compmax = c;
2879
2880 --todo;
2881 c = getc(fd); /* <compminlen> */
2882 if (c < 1)
2883 c = 3;
2884 slang->sl_compminlen = c;
2885
2886 --todo;
2887 c = getc(fd); /* <compsylmax> */
2888 if (c < 1)
2889 c = MAXWLEN;
2890 slang->sl_compsylmax = c;
2891
2892 /* Turn the COMPOUNDFLAGS items into a regexp pattern:
2893 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00002894 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
2895 * Conversion to utf-8 may double the size. */
2896 c = todo * 2 + 7;
2897#ifdef FEAT_MBYTE
2898 if (enc_utf8)
2899 c += todo * 2;
2900#endif
2901 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002902 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002903 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002904
Bram Moolenaard12a1322005-08-21 22:08:24 +00002905 /* We also need a list of all flags that can appear at the start and one
2906 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002907 cp = alloc(todo + 1);
2908 if (cp == NULL)
2909 {
2910 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002911 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002912 }
2913 slang->sl_compstartflags = cp;
2914 *cp = NUL;
2915
Bram Moolenaard12a1322005-08-21 22:08:24 +00002916 ap = alloc(todo + 1);
2917 if (ap == NULL)
2918 {
2919 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002920 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002921 }
2922 slang->sl_compallflags = ap;
2923 *ap = NUL;
2924
Bram Moolenaar5195e452005-08-19 20:32:47 +00002925 pp = pat;
2926 *pp++ = '^';
2927 *pp++ = '\\';
2928 *pp++ = '(';
2929
2930 atstart = 1;
2931 while (todo-- > 0)
2932 {
2933 c = getc(fd); /* <compflags> */
Bram Moolenaard12a1322005-08-21 22:08:24 +00002934
2935 /* Add all flags to "sl_compallflags". */
2936 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00002937 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00002938 {
2939 *ap++ = c;
2940 *ap = NUL;
2941 }
2942
Bram Moolenaar5195e452005-08-19 20:32:47 +00002943 if (atstart != 0)
2944 {
2945 /* At start of item: copy flags to "sl_compstartflags". For a
2946 * [abc] item set "atstart" to 2 and copy up to the ']'. */
2947 if (c == '[')
2948 atstart = 2;
2949 else if (c == ']')
2950 atstart = 0;
2951 else
2952 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00002953 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00002954 {
2955 *cp++ = c;
2956 *cp = NUL;
2957 }
2958 if (atstart == 1)
2959 atstart = 0;
2960 }
2961 }
2962 if (c == '/') /* slash separates two items */
2963 {
2964 *pp++ = '\\';
2965 *pp++ = '|';
2966 atstart = 1;
2967 }
2968 else /* normal char, "[abc]" and '*' are copied as-is */
2969 {
2970 if (c == '+')
2971 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00002972#ifdef FEAT_MBYTE
2973 if (enc_utf8)
2974 pp += mb_char2bytes(c, pp);
2975 else
2976#endif
2977 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002978 }
2979 }
2980
2981 *pp++ = '\\';
2982 *pp++ = ')';
2983 *pp++ = '$';
2984 *pp = NUL;
2985
2986 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
2987 vim_free(pat);
2988 if (slang->sl_compprog == NULL)
2989 return SP_FORMERROR;
2990
2991 return 0;
2992}
2993
Bram Moolenaar6de68532005-08-24 22:08:48 +00002994/*
2995 * Return TRUE if "byte" appears in "str".
2996 * Like strchr() but independent of locale.
2997 */
2998 static int
2999byte_in_str(str, byte)
3000 char_u *str;
3001 int byte;
3002{
3003 char_u *p;
3004
3005 for (p = str; *p != NUL; ++p)
3006 if (*p == byte)
3007 return TRUE;
3008 return FALSE;
3009}
3010
Bram Moolenaar5195e452005-08-19 20:32:47 +00003011#define SY_MAXLEN 30
3012typedef struct syl_item_S
3013{
3014 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3015 int sy_len;
3016} syl_item_T;
3017
3018/*
3019 * Truncate "slang->sl_syllable" at the first slash and put the following items
3020 * in "slang->sl_syl_items".
3021 */
3022 static int
3023init_syl_tab(slang)
3024 slang_T *slang;
3025{
3026 char_u *p;
3027 char_u *s;
3028 int l;
3029 syl_item_T *syl;
3030
3031 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3032 p = vim_strchr(slang->sl_syllable, '/');
3033 while (p != NULL)
3034 {
3035 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003036 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003037 break;
3038 s = p;
3039 p = vim_strchr(p, '/');
3040 if (p == NULL)
3041 l = STRLEN(s);
3042 else
3043 l = p - s;
3044 if (l >= SY_MAXLEN)
3045 return SP_FORMERROR;
3046 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003047 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003048 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3049 + slang->sl_syl_items.ga_len++;
3050 vim_strncpy(syl->sy_chars, s, l);
3051 syl->sy_len = l;
3052 }
3053 return OK;
3054}
3055
3056/*
3057 * Count the number of syllables in "word".
3058 * When "word" contains spaces the syllables after the last space are counted.
3059 * Returns zero if syllables are not defines.
3060 */
3061 static int
3062count_syllables(slang, word)
3063 slang_T *slang;
3064 char_u *word;
3065{
3066 int cnt = 0;
3067 int skip = FALSE;
3068 char_u *p;
3069 int len;
3070 int i;
3071 syl_item_T *syl;
3072 int c;
3073
3074 if (slang->sl_syllable == NULL)
3075 return 0;
3076
3077 for (p = word; *p != NUL; p += len)
3078 {
3079 /* When running into a space reset counter. */
3080 if (*p == ' ')
3081 {
3082 len = 1;
3083 cnt = 0;
3084 continue;
3085 }
3086
3087 /* Find longest match of syllable items. */
3088 len = 0;
3089 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3090 {
3091 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3092 if (syl->sy_len > len
3093 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3094 len = syl->sy_len;
3095 }
3096 if (len != 0) /* found a match, count syllable */
3097 {
3098 ++cnt;
3099 skip = FALSE;
3100 }
3101 else
3102 {
3103 /* No recognized syllable item, at least a syllable char then? */
3104#ifdef FEAT_MBYTE
3105 c = mb_ptr2char(p);
3106 len = (*mb_ptr2len)(p);
3107#else
3108 c = *p;
3109 len = 1;
3110#endif
3111 if (vim_strchr(slang->sl_syllable, c) == NULL)
3112 skip = FALSE; /* No, search for next syllable */
3113 else if (!skip)
3114 {
3115 ++cnt; /* Yes, count it */
3116 skip = TRUE; /* don't count following syllable chars */
3117 }
3118 }
3119 }
3120 return cnt;
3121}
3122
3123/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003124 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003125 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003126 */
3127 static int
3128set_sofo(lp, from, to)
3129 slang_T *lp;
3130 char_u *from;
3131 char_u *to;
3132{
3133 int i;
3134
3135#ifdef FEAT_MBYTE
3136 garray_T *gap;
3137 char_u *s;
3138 char_u *p;
3139 int c;
3140 int *inp;
3141
3142 if (has_mbyte)
3143 {
3144 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3145 * characters. The index is the low byte of the character.
3146 * The list contains from-to pairs with a terminating NUL.
3147 * sl_sal_first[] is used for latin1 "from" characters. */
3148 gap = &lp->sl_sal;
3149 ga_init2(gap, sizeof(int *), 1);
3150 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003151 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003152 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3153 gap->ga_len = 256;
3154
3155 /* First count the number of items for each list. Temporarily use
3156 * sl_sal_first[] for this. */
3157 for (p = from, s = to; *p != NUL && *s != NUL; )
3158 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003159 c = mb_cptr2char_adv(&p);
3160 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003161 if (c >= 256)
3162 ++lp->sl_sal_first[c & 0xff];
3163 }
3164 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003165 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003166
3167 /* Allocate the lists. */
3168 for (i = 0; i < 256; ++i)
3169 if (lp->sl_sal_first[i] > 0)
3170 {
3171 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3172 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003173 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003174 ((int **)gap->ga_data)[i] = (int *)p;
3175 *(int *)p = 0;
3176 }
3177
3178 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3179 * list. */
3180 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3181 for (p = from, s = to; *p != NUL && *s != NUL; )
3182 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003183 c = mb_cptr2char_adv(&p);
3184 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003185 if (c >= 256)
3186 {
3187 /* Append the from-to chars at the end of the list with
3188 * the low byte. */
3189 inp = ((int **)gap->ga_data)[c & 0xff];
3190 while (*inp != 0)
3191 ++inp;
3192 *inp++ = c; /* from char */
3193 *inp++ = i; /* to char */
3194 *inp++ = NUL; /* NUL at the end */
3195 }
3196 else
3197 /* mapping byte to char is done in sl_sal_first[] */
3198 lp->sl_sal_first[c] = i;
3199 }
3200 }
3201 else
3202#endif
3203 {
3204 /* mapping bytes to bytes is done in sl_sal_first[] */
3205 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003206 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003207
3208 for (i = 0; to[i] != NUL; ++i)
3209 lp->sl_sal_first[from[i]] = to[i];
3210 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3211 }
3212
Bram Moolenaar5195e452005-08-19 20:32:47 +00003213 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003214}
3215
3216/*
3217 * Fill the first-index table for "lp".
3218 */
3219 static void
3220set_sal_first(lp)
3221 slang_T *lp;
3222{
3223 salfirst_T *sfirst;
3224 int i;
3225 salitem_T *smp;
3226 int c;
3227 garray_T *gap = &lp->sl_sal;
3228
3229 sfirst = lp->sl_sal_first;
3230 for (i = 0; i < 256; ++i)
3231 sfirst[i] = -1;
3232 smp = (salitem_T *)gap->ga_data;
3233 for (i = 0; i < gap->ga_len; ++i)
3234 {
3235#ifdef FEAT_MBYTE
3236 if (has_mbyte)
3237 /* Use the lowest byte of the first character. For latin1 it's
3238 * the character, for other encodings it should differ for most
3239 * characters. */
3240 c = *smp[i].sm_lead_w & 0xff;
3241 else
3242#endif
3243 c = *smp[i].sm_lead;
3244 if (sfirst[c] == -1)
3245 {
3246 sfirst[c] = i;
3247#ifdef FEAT_MBYTE
3248 if (has_mbyte)
3249 {
3250 int n;
3251
3252 /* Make sure all entries with this byte are following each
3253 * other. Move the ones that are in the wrong position. Do
3254 * keep the same ordering! */
3255 while (i + 1 < gap->ga_len
3256 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3257 /* Skip over entry with same index byte. */
3258 ++i;
3259
3260 for (n = 1; i + n < gap->ga_len; ++n)
3261 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3262 {
3263 salitem_T tsal;
3264
3265 /* Move entry with same index byte after the entries
3266 * we already found. */
3267 ++i;
3268 --n;
3269 tsal = smp[i + n];
3270 mch_memmove(smp + i + 1, smp + i,
3271 sizeof(salitem_T) * n);
3272 smp[i] = tsal;
3273 }
3274 }
3275#endif
3276 }
3277 }
3278}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003279
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003280#ifdef FEAT_MBYTE
3281/*
3282 * Turn a multi-byte string into a wide character string.
3283 * Return it in allocated memory (NULL for out-of-memory)
3284 */
3285 static int *
3286mb_str2wide(s)
3287 char_u *s;
3288{
3289 int *res;
3290 char_u *p;
3291 int i = 0;
3292
3293 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3294 if (res != NULL)
3295 {
3296 for (p = s; *p != NUL; )
3297 res[i++] = mb_ptr2char_adv(&p);
3298 res[i] = NUL;
3299 }
3300 return res;
3301}
3302#endif
3303
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003304/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003305 * Read one row of siblings from the spell file and store it in the byte array
3306 * "byts" and index array "idxs". Recursively read the children.
3307 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00003308 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00003309 *
3310 * Returns the index follosing the siblings.
3311 * Returns -1 if the file is shorter than expected.
3312 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003313 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003314 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003315read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003316 FILE *fd;
3317 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003318 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003319 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003320 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003321 int prefixtree; /* TRUE for reading PREFIXTREE */
3322 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003323{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003324 int len;
3325 int i;
3326 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003327 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003328 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003329 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003330#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003331
Bram Moolenaar51485f02005-06-04 21:55:20 +00003332 len = getc(fd); /* <siblingcount> */
3333 if (len <= 0)
3334 return -1;
3335
3336 if (startidx + len >= maxidx)
3337 return -2;
3338 byts[idx++] = len;
3339
3340 /* Read the byte values, flag/region bytes and shared indexes. */
3341 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003342 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003343 c = getc(fd); /* <byte> */
3344 if (c < 0)
3345 return -1;
3346 if (c <= BY_SPECIAL)
3347 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003348 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003349 {
3350 /* No flags, all regions. */
3351 idxs[idx] = 0;
3352 c = 0;
3353 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003354 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003355 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003356 if (prefixtree)
3357 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003358 /* Read the optional pflags byte, the prefix ID and the
3359 * condition nr. In idxs[] store the prefix ID in the low
3360 * byte, the condition index shifted up 8 bits, the flags
3361 * shifted up 24 bits. */
3362 if (c == BY_FLAGS)
3363 c = getc(fd) << 24; /* <pflags> */
3364 else
3365 c = 0;
3366
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003367 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003368
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003369 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
3370 if (n >= maxprefcondnr)
3371 return -2;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003372 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003373 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003374 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003375 {
3376 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003377 * idxs[] the flags go in the low two bytes, region above
3378 * that and prefix ID above the region. */
3379 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003380 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003381 if (c2 == BY_FLAGS2)
3382 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003383 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003384 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003385 if (c & WF_AFX)
3386 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003387 }
3388
Bram Moolenaar51485f02005-06-04 21:55:20 +00003389 idxs[idx] = c;
3390 c = 0;
3391 }
3392 else /* c == BY_INDEX */
3393 {
3394 /* <nodeidx> */
3395 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
3396 if (n < 0 || n >= maxidx)
3397 return -2;
3398 idxs[idx] = n + SHARED_MASK;
3399 c = getc(fd); /* <xbyte> */
3400 }
3401 }
3402 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003403 }
3404
Bram Moolenaar51485f02005-06-04 21:55:20 +00003405 /* Recursively read the children for non-shared siblings.
3406 * Skip the end-of-word ones (zero byte value) and the shared ones (and
3407 * remove SHARED_MASK) */
3408 for (i = 1; i <= len; ++i)
3409 if (byts[startidx + i] != 0)
3410 {
3411 if (idxs[startidx + i] & SHARED_MASK)
3412 idxs[startidx + i] &= ~SHARED_MASK;
3413 else
3414 {
3415 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003416 idx = read_tree(fd, byts, idxs, maxidx, idx,
3417 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003418 if (idx < 0)
3419 break;
3420 }
3421 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003422
Bram Moolenaar51485f02005-06-04 21:55:20 +00003423 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003424}
3425
3426/*
3427 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003428 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003429 */
3430 char_u *
3431did_set_spelllang(buf)
3432 buf_T *buf;
3433{
3434 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003435 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003436 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00003437 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003438 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003439 int region_mask;
3440 slang_T *lp;
3441 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003442 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003443 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003444 int len;
3445 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003446 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003447 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003448 char_u *use_region = NULL;
3449 int dont_use_region = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003450
3451 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003452 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003453
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003454 /* loop over comma separated language names. */
3455 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003456 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003457 /* Get one language name. */
3458 copy_option_part(&splp, lang, MAXWLEN, ",");
3459
Bram Moolenaar5482f332005-04-17 20:18:43 +00003460 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003461 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003462
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003463 /* If the name ends in ".spl" use it as the name of the spell file.
3464 * If there is a region name let "region" point to it and remove it
3465 * from the name. */
3466 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
3467 {
3468 filename = TRUE;
3469
Bram Moolenaarb6356332005-07-18 21:40:44 +00003470 /* Locate a region and remove it from the file name. */
3471 p = vim_strchr(gettail(lang), '_');
3472 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
3473 && !ASCII_ISALPHA(p[3]))
3474 {
3475 vim_strncpy(region_cp, p + 1, 2);
3476 mch_memmove(p, p + 3, len - (p - lang) - 2);
3477 len -= 3;
3478 region = region_cp;
3479 }
3480 else
3481 dont_use_region = TRUE;
3482
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003483 /* Check if we loaded this language before. */
3484 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3485 if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME)
3486 break;
3487 }
3488 else
3489 {
3490 filename = FALSE;
3491 if (len > 3 && lang[len - 3] == '_')
3492 {
3493 region = lang + len - 2;
3494 len -= 3;
3495 lang[len] = NUL;
3496 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003497 else
3498 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003499
3500 /* Check if we loaded this language before. */
3501 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3502 if (STRICMP(lang, lp->sl_name) == 0)
3503 break;
3504 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003505
Bram Moolenaarb6356332005-07-18 21:40:44 +00003506 if (region != NULL)
3507 {
3508 /* If the region differs from what was used before then don't
3509 * use it for 'spellfile'. */
3510 if (use_region != NULL && STRCMP(region, use_region) != 0)
3511 dont_use_region = TRUE;
3512 use_region = region;
3513 }
3514
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003515 /* If not found try loading the language now. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003516 if (lp == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003517 {
3518 if (filename)
3519 (void)spell_load_file(lang, lang, NULL, FALSE);
3520 else
3521 spell_load_lang(lang);
3522 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003523
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003524 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003525 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003526 */
3527 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003528 if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME
3529 : STRICMP(lang, lp->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003530 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003531 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003532 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003533 {
3534 /* find region in sl_regions */
3535 c = find_region(lp->sl_regions, region);
3536 if (c == REGION_ALL)
3537 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003538 if (lp->sl_add)
3539 {
3540 if (*lp->sl_regions != NUL)
3541 /* This addition file is for other regions. */
3542 region_mask = 0;
3543 }
3544 else
3545 /* This is probably an error. Give a warning and
3546 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003547 smsg((char_u *)
3548 _("Warning: region %s not supported"),
3549 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003550 }
3551 else
3552 region_mask = 1 << c;
3553 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003554
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003555 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003556 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003557 if (ga_grow(&ga, 1) == FAIL)
3558 {
3559 ga_clear(&ga);
3560 return e_outofmem;
3561 }
3562 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
3563 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
3564 ++ga.ga_len;
3565 use_midword(lp, buf);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003566 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003567 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003568 }
3569
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003570 /* round 0: load int_wordlist, if possible.
3571 * round 1: load first name in 'spellfile'.
3572 * round 2: load second name in 'spellfile.
3573 * etc. */
3574 spf = curbuf->b_p_spf;
3575 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003576 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003577 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003578 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003579 /* Internal wordlist, if there is one. */
3580 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003581 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003582 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003583 }
3584 else
3585 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003586 /* One entry in 'spellfile'. */
3587 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
3588 STRCAT(spf_name, ".spl");
3589
3590 /* If it was already found above then skip it. */
3591 for (c = 0; c < ga.ga_len; ++c)
3592 if (fullpathcmp(spf_name,
3593 LANGP_ENTRY(ga, c)->lp_slang->sl_fname,
3594 FALSE) == FPC_SAME)
3595 break;
3596 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003597 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003598 }
3599
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003600 /* Check if it was loaded already. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003601 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3602 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
3603 break;
3604 if (lp == NULL)
3605 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003606 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003607 * region name, the region is ignored otherwise. for int_wordlist
3608 * use an arbitrary name. */
3609 if (round == 0)
3610 STRCPY(lang, "internal wordlist");
3611 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00003612 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003613 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003614 p = vim_strchr(lang, '.');
3615 if (p != NULL)
3616 *p = NUL; /* truncate at ".encoding.add" */
3617 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003618 lp = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003619 }
3620 if (lp != NULL && ga_grow(&ga, 1) == OK)
3621 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003622 region_mask = REGION_ALL;
3623 if (use_region != NULL && !dont_use_region)
3624 {
3625 /* find region in sl_regions */
3626 c = find_region(lp->sl_regions, use_region);
3627 if (c != REGION_ALL)
3628 region_mask = 1 << c;
3629 else if (*lp->sl_regions != NUL)
3630 /* This spell file is for other regions. */
3631 region_mask = 0;
3632 }
3633
3634 if (region_mask != 0)
3635 {
3636 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
3637 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
3638 ++ga.ga_len;
3639 use_midword(lp, buf);
3640 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003641 }
3642 }
3643
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003644 /* Add a NULL entry to mark the end of the list. */
3645 if (ga_grow(&ga, 1) == FAIL)
3646 {
3647 ga_clear(&ga);
3648 return e_outofmem;
3649 }
3650 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
3651 ++ga.ga_len;
3652
3653 /* Everything is fine, store the new b_langp value. */
3654 ga_clear(&buf->b_langp);
3655 buf->b_langp = ga;
3656
3657 return NULL;
3658}
3659
3660/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003661 * Clear the midword characters for buffer "buf".
3662 */
3663 static void
3664clear_midword(buf)
3665 buf_T *buf;
3666{
3667 vim_memset(buf->b_spell_ismw, 0, 256);
3668#ifdef FEAT_MBYTE
3669 vim_free(buf->b_spell_ismw_mb);
3670 buf->b_spell_ismw_mb = NULL;
3671#endif
3672}
3673
3674/*
3675 * Use the "sl_midword" field of language "lp" for buffer "buf".
3676 * They add up to any currently used midword characters.
3677 */
3678 static void
3679use_midword(lp, buf)
3680 slang_T *lp;
3681 buf_T *buf;
3682{
3683 char_u *p;
3684
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003685 if (lp->sl_midword == NULL) /* there aren't any */
3686 return;
3687
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003688 for (p = lp->sl_midword; *p != NUL; )
3689#ifdef FEAT_MBYTE
3690 if (has_mbyte)
3691 {
3692 int c, l, n;
3693 char_u *bp;
3694
3695 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003696 l = (*mb_ptr2len)(p);
3697 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003698 buf->b_spell_ismw[c] = TRUE;
3699 else if (buf->b_spell_ismw_mb == NULL)
3700 /* First multi-byte char in "b_spell_ismw_mb". */
3701 buf->b_spell_ismw_mb = vim_strnsave(p, l);
3702 else
3703 {
3704 /* Append multi-byte chars to "b_spell_ismw_mb". */
3705 n = STRLEN(buf->b_spell_ismw_mb);
3706 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
3707 if (bp != NULL)
3708 {
3709 vim_free(buf->b_spell_ismw_mb);
3710 buf->b_spell_ismw_mb = bp;
3711 vim_strncpy(bp + n, p, l);
3712 }
3713 }
3714 p += l;
3715 }
3716 else
3717#endif
3718 buf->b_spell_ismw[*p++] = TRUE;
3719}
3720
3721/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003722 * Find the region "region[2]" in "rp" (points to "sl_regions").
3723 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003724 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003725 */
3726 static int
3727find_region(rp, region)
3728 char_u *rp;
3729 char_u *region;
3730{
3731 int i;
3732
3733 for (i = 0; ; i += 2)
3734 {
3735 if (rp[i] == NUL)
3736 return REGION_ALL;
3737 if (rp[i] == region[0] && rp[i + 1] == region[1])
3738 break;
3739 }
3740 return i / 2;
3741}
3742
3743/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003744 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003745 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00003746 * Word WF_ONECAP
3747 * W WORD WF_ALLCAP
3748 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003749 */
3750 static int
3751captype(word, end)
3752 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003753 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003754{
3755 char_u *p;
3756 int c;
3757 int firstcap;
3758 int allcap;
3759 int past_second = FALSE; /* past second word char */
3760
3761 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003762 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003763 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003764 return 0; /* only non-word characters, illegal word */
3765#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00003766 if (has_mbyte)
3767 c = mb_ptr2char_adv(&p);
3768 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003769#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00003770 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003771 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003772
3773 /*
3774 * Need to check all letters to find a word with mixed upper/lower.
3775 * But a word with an upper char only at start is a ONECAP.
3776 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003777 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003778 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003779 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003780 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003781 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003782 {
3783 /* UUl -> KEEPCAP */
3784 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003785 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003786 allcap = FALSE;
3787 }
3788 else if (!allcap)
3789 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003790 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003791 past_second = TRUE;
3792 }
3793
3794 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003795 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003796 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003797 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003798 return 0;
3799}
3800
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003801/*
3802 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
3803 * capital. So that make_case_word() can turn WOrd into Word.
3804 * Add ALLCAP for "WOrD".
3805 */
3806 static int
3807badword_captype(word, end)
3808 char_u *word;
3809 char_u *end;
3810{
3811 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003812 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003813 int l, u;
3814 int first;
3815 char_u *p;
3816
3817 if (flags & WF_KEEPCAP)
3818 {
3819 /* Count the number of UPPER and lower case letters. */
3820 l = u = 0;
3821 first = FALSE;
3822 for (p = word; p < end; mb_ptr_adv(p))
3823 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003824 c = PTR2CHAR(p);
3825 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003826 {
3827 ++u;
3828 if (p == word)
3829 first = TRUE;
3830 }
3831 else
3832 ++l;
3833 }
3834
3835 /* If there are more UPPER than lower case letters suggest an
3836 * ALLCAP word. Otherwise, if the first letter is UPPER then
3837 * suggest ONECAP. Exception: "ALl" most likely should be "All",
3838 * require three upper case letters. */
3839 if (u > l && u > 2)
3840 flags |= WF_ALLCAP;
3841 else if (first)
3842 flags |= WF_ONECAP;
3843 }
3844 return flags;
3845}
3846
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003847# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
3848/*
3849 * Free all languages.
3850 */
3851 void
3852spell_free_all()
3853{
3854 slang_T *lp;
3855 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003856 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003857
3858 /* Go through all buffers and handle 'spelllang'. */
3859 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3860 ga_clear(&buf->b_langp);
3861
3862 while (first_lang != NULL)
3863 {
3864 lp = first_lang;
3865 first_lang = lp->sl_next;
3866 slang_free(lp);
3867 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003868
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003869 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003870 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003871 /* Delete the internal wordlist and its .spl file */
3872 mch_remove(int_wordlist);
3873 int_wordlist_spl(fname);
3874 mch_remove(fname);
3875 vim_free(int_wordlist);
3876 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003877 }
3878
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003879 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003880}
3881# endif
3882
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003883# if defined(FEAT_MBYTE) || defined(PROTO)
3884/*
3885 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003886 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003887 */
3888 void
3889spell_reload()
3890{
3891 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003892 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003893
Bram Moolenaarea408852005-06-25 22:49:46 +00003894 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003895 init_spell_chartab();
3896
3897 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003898 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003899
3900 /* Go through all buffers and handle 'spelllang'. */
3901 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3902 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003903 /* Only load the wordlists when 'spelllang' is set and there is a
3904 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003905 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00003906 {
3907 FOR_ALL_WINDOWS(wp)
3908 if (wp->w_buffer == buf && wp->w_p_spell)
3909 {
3910 (void)did_set_spelllang(buf);
3911# ifdef FEAT_WINDOWS
3912 break;
3913# endif
3914 }
3915 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003916 }
3917}
3918# endif
3919
Bram Moolenaarb765d632005-06-07 21:00:02 +00003920/*
3921 * Reload the spell file "fname" if it's loaded.
3922 */
3923 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003924spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003925 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003926 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003927{
3928 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003929 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003930
Bram Moolenaarb765d632005-06-07 21:00:02 +00003931 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3932 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
3933 {
3934 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003935 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003936 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003937 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003938 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003939
3940 /* When "zg" was used and the file wasn't loaded yet, should redo
3941 * 'spelllang' to get it loaded. */
3942 if (added_word && !didit)
3943 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003944}
3945
3946
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003947/*
3948 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003949 */
3950
Bram Moolenaar51485f02005-06-04 21:55:20 +00003951#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003952 and .dic file. */
3953/*
3954 * Main structure to store the contents of a ".aff" file.
3955 */
3956typedef struct afffile_S
3957{
3958 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003959 int af_flagtype; /* AFT_CHAR, AFT_2CHAR, AFT_NUMBER or AFT_HUH */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003960 int af_slash; /* character used in word for slash */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003961 unsigned af_rar; /* RAR ID for rare word */
3962 unsigned af_kep; /* KEP ID for keep-case word */
3963 unsigned af_bad; /* BAD ID for banned word */
3964 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003965 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003966 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
3967 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003968 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003969} afffile_T;
3970
Bram Moolenaar6de68532005-08-24 22:08:48 +00003971#define AFT_CHAR 0 /* flags are one character */
3972#define AFT_2CHAR 1 /* flags are two characters */
3973#define AFT_HUH 2 /* flags are one or two characters */
3974#define AFT_NUMBER 3 /* flags are numbers, comma separated */
3975
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003976typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003977/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
3978struct affentry_S
3979{
3980 affentry_T *ae_next; /* next affix with same name/number */
3981 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
3982 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003983 char_u *ae_cond; /* condition (NULL for ".") */
3984 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003985 char_u ae_rare; /* rare affix */
3986 char_u ae_nocomp; /* word with affix not compoundable */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003987};
3988
Bram Moolenaar6de68532005-08-24 22:08:48 +00003989#ifdef FEAT_MBYTE
3990# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
3991#else
3992# define AH_KEY_LEN 3 /* 2 x 1 byte + NUL */
3993#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00003994
Bram Moolenaar51485f02005-06-04 21:55:20 +00003995/* Affix header from ".aff" file. Used for af_pref and af_suff. */
3996typedef struct affheader_S
3997{
Bram Moolenaar6de68532005-08-24 22:08:48 +00003998 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
3999 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4000 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004001 int ah_combine; /* suffix may combine with prefix */
4002 affentry_T *ah_first; /* first affix entry */
4003} affheader_T;
4004
4005#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4006
Bram Moolenaar6de68532005-08-24 22:08:48 +00004007/* Flag used in compound items. */
4008typedef struct compitem_S
4009{
4010 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4011 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4012 int ci_newID; /* affix ID after renumbering. */
4013} compitem_T;
4014
4015#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4016
Bram Moolenaar51485f02005-06-04 21:55:20 +00004017/*
4018 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004019 * the need to keep track of each allocated thing, everything is freed all at
4020 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004021 */
4022#define SBLOCKSIZE 16000 /* size of sb_data */
4023typedef struct sblock_S sblock_T;
4024struct sblock_S
4025{
4026 sblock_T *sb_next; /* next block in list */
4027 int sb_used; /* nr of bytes already in use */
4028 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004029};
4030
4031/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004032 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004033 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004034typedef struct wordnode_S wordnode_T;
4035struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004036{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004037 union /* shared to save space */
4038 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004039 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004040 int index; /* index in written nodes (valid after first
4041 round) */
4042 } wn_u1;
4043 union /* shared to save space */
4044 {
4045 wordnode_T *next; /* next node with same hash key */
4046 wordnode_T *wnode; /* parent node that will write this node */
4047 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004048 wordnode_T *wn_child; /* child (next byte in word) */
4049 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4050 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004051 int wn_refs; /* Nr. of references to this node. Only
4052 relevant for first node in a list of
4053 siblings, in following siblings it is
4054 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004055 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004056 char_u wn_affixID; /* when "wn_byte" is NUL: supported/required
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004057 prefix ID or 0 */
4058 short_u wn_flags; /* when "wn_byte" is NUL: WF_ flags */
4059 short wn_region; /* when "wn_byte" is NUL: region mask; for
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004060 PREFIXTREE it's the prefcondnr */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004061#ifdef SPELL_PRINTTREE
4062 int wn_nr; /* sequence nr for printing */
4063#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004064};
4065
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004066#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4067
Bram Moolenaar51485f02005-06-04 21:55:20 +00004068#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004069
Bram Moolenaar51485f02005-06-04 21:55:20 +00004070/*
4071 * Info used while reading the spell files.
4072 */
4073typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004074{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004075 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004076 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004077
Bram Moolenaar51485f02005-06-04 21:55:20 +00004078 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004079 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004080
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004081 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004082
Bram Moolenaar51485f02005-06-04 21:55:20 +00004083 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004084 long si_blocks_cnt; /* memory blocks allocated */
4085 long si_compress_cnt; /* words to add before lowering
4086 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004087 wordnode_T *si_first_free; /* List of nodes that have been freed during
4088 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004089 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004090#ifdef SPELL_PRINTTREE
4091 int si_wordnode_nr; /* sequence nr for nodes */
4092#endif
4093
4094
Bram Moolenaar51485f02005-06-04 21:55:20 +00004095 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004096 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004097 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004098 int si_region; /* region mask */
4099 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004100 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004101 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004102 int si_msg_count; /* number of words added since last message */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004103 int si_region_count; /* number of regions supported (1 when there
4104 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004105 char_u si_region_name[16]; /* region names; used only if
4106 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004107
4108 garray_T si_rep; /* list of fromto_T entries from REP lines */
4109 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004110 char_u *si_sofofr; /* SOFOFROM text */
4111 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004112 int si_followup; /* soundsalike: ? */
4113 int si_collapse; /* soundsalike: ? */
4114 int si_rem_accents; /* soundsalike: remove accents */
4115 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004116 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004117 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004118 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004119 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004120 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004121 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004122 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004123 garray_T si_prefcond; /* table with conditions for postponed
4124 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004125 int si_newprefID; /* current value for ah_newID */
4126 int si_compID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004127} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004128
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004129static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004130static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
4131static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
4132static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
4133static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
4134static void aff_check_number __ARGS((int spinval, int affval, char *name));
4135static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004136static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004137static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4138static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00004139static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004140static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004141static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004142static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004143static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004144static 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 +00004145static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
4146static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
4147static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004148static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004149static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004150static 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 +00004151static 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 +00004152static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
4153static void deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
4154static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
4155static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
4156static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004157static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004158static void write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00004159static void clear_node __ARGS((wordnode_T *node));
4160static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004161static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004162static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004163
Bram Moolenaar53805d12005-08-01 07:08:33 +00004164/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
4165 * but it must be negative to indicate the prefix tree to tree_add_word().
4166 * Use a negative number with the lower 8 bits zero. */
4167#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004168
Bram Moolenaar5195e452005-08-19 20:32:47 +00004169/*
4170 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
4171 */
4172static long compress_start = 30000; /* memory / SBLOCKSIZE */
4173static long compress_inc = 100; /* memory / SBLOCKSIZE */
4174static long compress_added = 500000; /* word count */
4175
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004176#ifdef SPELL_PRINTTREE
4177/*
4178 * For debugging the tree code: print the current tree in a (more or less)
4179 * readable format, so that we can see what happens when adding a word and/or
4180 * compressing the tree.
4181 * Based on code from Olaf Seibert.
4182 */
4183#define PRINTLINESIZE 1000
4184#define PRINTWIDTH 6
4185
4186#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4187 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4188
4189static char line1[PRINTLINESIZE];
4190static char line2[PRINTLINESIZE];
4191static char line3[PRINTLINESIZE];
4192
4193 static void
4194spell_clear_flags(wordnode_T *node)
4195{
4196 wordnode_T *np;
4197
4198 for (np = node; np != NULL; np = np->wn_sibling)
4199 {
4200 np->wn_u1.index = FALSE;
4201 spell_clear_flags(np->wn_child);
4202 }
4203}
4204
4205 static void
4206spell_print_node(wordnode_T *node, int depth)
4207{
4208 if (node->wn_u1.index)
4209 {
4210 /* Done this node before, print the reference. */
4211 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
4212 PRINTSOME(line2, depth, " ", 0, 0);
4213 PRINTSOME(line3, depth, " ", 0, 0);
4214 msg(line1);
4215 msg(line2);
4216 msg(line3);
4217 }
4218 else
4219 {
4220 node->wn_u1.index = TRUE;
4221
4222 if (node->wn_byte != NUL)
4223 {
4224 if (node->wn_child != NULL)
4225 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
4226 else
4227 /* Cannot happen? */
4228 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
4229 }
4230 else
4231 PRINTSOME(line1, depth, " $ ", 0, 0);
4232
4233 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
4234
4235 if (node->wn_sibling != NULL)
4236 PRINTSOME(line3, depth, " | ", 0, 0);
4237 else
4238 PRINTSOME(line3, depth, " ", 0, 0);
4239
4240 if (node->wn_byte == NUL)
4241 {
4242 msg(line1);
4243 msg(line2);
4244 msg(line3);
4245 }
4246
4247 /* do the children */
4248 if (node->wn_byte != NUL && node->wn_child != NULL)
4249 spell_print_node(node->wn_child, depth + 1);
4250
4251 /* do the siblings */
4252 if (node->wn_sibling != NULL)
4253 {
4254 /* get rid of all parent details except | */
4255 STRCPY(line1, line3);
4256 STRCPY(line2, line3);
4257 spell_print_node(node->wn_sibling, depth);
4258 }
4259 }
4260}
4261
4262 static void
4263spell_print_tree(wordnode_T *root)
4264{
4265 if (root != NULL)
4266 {
4267 /* Clear the "wn_u1.index" fields, used to remember what has been
4268 * done. */
4269 spell_clear_flags(root);
4270
4271 /* Recursively print the tree. */
4272 spell_print_node(root, 0);
4273 }
4274}
4275#endif /* SPELL_PRINTTREE */
4276
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004277/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004278 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00004279 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004280 */
4281 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004282spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004283 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004284 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004285{
4286 FILE *fd;
4287 afffile_T *aff;
4288 char_u rline[MAXLINELEN];
4289 char_u *line;
4290 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004291#define MAXITEMCNT 7
4292 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004293 int itemcnt;
4294 char_u *p;
4295 int lnum = 0;
4296 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004297 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004298 int aff_todo = 0;
4299 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004300 char_u *low = NULL;
4301 char_u *fol = NULL;
4302 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004303 int do_rep;
4304 int do_sal;
4305 int do_map;
4306 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004307 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004308 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004309 int compminlen = 0; /* COMPOUNDMIN value */
4310 int compsylmax = 0; /* COMPOUNDSYLMAX value */
4311 int compmax = 0; /* COMPOUNDMAX value */
4312 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDFLAGS
4313 concatenated */
4314 char_u *midword = NULL; /* MIDWORD value */
4315 char_u *syllable = NULL; /* SYLLABLE value */
4316 char_u *sofofrom = NULL; /* SOFOFROM value */
4317 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004318
Bram Moolenaar51485f02005-06-04 21:55:20 +00004319 /*
4320 * Open the file.
4321 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004322 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004323 if (fd == NULL)
4324 {
4325 EMSG2(_(e_notopen), fname);
4326 return NULL;
4327 }
4328
Bram Moolenaarb765d632005-06-07 21:00:02 +00004329 if (spin->si_verbose || p_verbose > 2)
4330 {
4331 if (!spin->si_verbose)
4332 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004333 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004334 out_flush();
4335 if (!spin->si_verbose)
4336 verbose_leave();
4337 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004338
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004339 /* Only do REP lines when not done in another .aff file already. */
4340 do_rep = spin->si_rep.ga_len == 0;
4341
4342 /* Only do SAL lines when not done in another .aff file already. */
4343 do_sal = spin->si_sal.ga_len == 0;
4344
4345 /* Only do MAP lines when not done in another .aff file already. */
4346 do_map = spin->si_map.ga_len == 0;
4347
Bram Moolenaar51485f02005-06-04 21:55:20 +00004348 /*
4349 * Allocate and init the afffile_T structure.
4350 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004351 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004352 if (aff == NULL)
4353 return NULL;
4354 hash_init(&aff->af_pref);
4355 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004356 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004357
4358 /*
4359 * Read all the lines in the file one by one.
4360 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004361 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004362 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004363 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004364 ++lnum;
4365
4366 /* Skip comment lines. */
4367 if (*rline == '#')
4368 continue;
4369
4370 /* Convert from "SET" to 'encoding' when needed. */
4371 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004372#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004373 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004374 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004375 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004376 if (pc == NULL)
4377 {
4378 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4379 fname, lnum, rline);
4380 continue;
4381 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004382 line = pc;
4383 }
4384 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004385#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004386 {
4387 pc = NULL;
4388 line = rline;
4389 }
4390
4391 /* Split the line up in white separated items. Put a NUL after each
4392 * item. */
4393 itemcnt = 0;
4394 for (p = line; ; )
4395 {
4396 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
4397 ++p;
4398 if (*p == NUL)
4399 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004400 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004401 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004402 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004403 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004404 ++p;
4405 if (*p == NUL)
4406 break;
4407 *p++ = NUL;
4408 }
4409
4410 /* Handle non-empty lines. */
4411 if (itemcnt > 0)
4412 {
4413 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
4414 && aff->af_enc == NULL)
4415 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004416#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004417 /* Setup for conversion from "ENC" to 'encoding'. */
4418 aff->af_enc = enc_canonize(items[1]);
4419 if (aff->af_enc != NULL && !spin->si_ascii
4420 && convert_setup(&spin->si_conv, aff->af_enc,
4421 p_enc) == FAIL)
4422 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
4423 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004424 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004425#else
4426 smsg((char_u *)_("Conversion in %s not supported"), fname);
4427#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004428 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00004429 else if (STRCMP(items[0], "FLAG") == 0 && itemcnt == 2
4430 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004431 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004432 if (STRCMP(items[1], "long") == 0)
4433 aff->af_flagtype = AFT_2CHAR;
4434 else if (STRCMP(items[1], "num") == 0)
4435 aff->af_flagtype = AFT_NUMBER;
4436 else if (STRCMP(items[1], "huh") == 0)
4437 aff->af_flagtype = AFT_HUH;
4438 else
4439 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
4440 fname, lnum, items[1]);
4441 if (aff->af_rar != 0 || aff->af_kep != 0 || aff->af_bad != 0
4442 || aff->af_needaffix != 0 || compflags != NULL
4443 || aff->af_suff.ht_used > 0
4444 || aff->af_pref.ht_used > 0)
4445 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
4446 fname, lnum, items[1]);
4447 }
4448 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2
4449 && midword == NULL)
4450 {
4451 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004452 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00004453 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
4454 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004455 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004456 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004457 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004458 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004459 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004460 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004461 else if (STRCMP(items[0], "SLASH") == 0 && itemcnt == 2
4462 && aff->af_slash == 0)
4463 {
4464 aff->af_slash = items[1][0];
4465 if (items[1][1] != NUL)
4466 smsg((char_u *)_("Character used for SLASH must be ASCII; in %s line %d: %s"),
4467 fname, lnum, items[1]);
4468 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004469 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
4470 && aff->af_rar == 0)
4471 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004472 aff->af_rar = affitem2flag(aff->af_flagtype, items[1],
4473 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004474 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004475 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
4476 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004477 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004478 aff->af_kep = affitem2flag(aff->af_flagtype, items[1],
4479 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004480 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00004481 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
4482 && aff->af_bad == 0)
4483 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004484 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
4485 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004486 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004487 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
4488 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004489 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004490 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
4491 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004492 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004493 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004494 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004495 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004496 /* Turn flag "c" into COMPOUNDFLAGS compatible string "c+",
4497 * "Na" into "Na+", "1234" into "1234+". */
4498 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004499 if (p != NULL)
4500 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004501 STRCPY(p, items[1]);
4502 STRCAT(p, "+");
4503 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004504 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004505 }
4506 else if (STRCMP(items[0], "COMPOUNDFLAGS") == 0 && itemcnt == 2)
4507 {
4508 /* Concatenate this string to previously defined ones, using a
4509 * slash to separate them. */
4510 l = STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004511 if (compflags != NULL)
4512 l += STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004513 p = getroom(spin, l, FALSE);
4514 if (p != NULL)
4515 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004516 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004517 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004518 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004519 STRCAT(p, "/");
4520 }
4521 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004522 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004523 }
4524 }
4525 else if (STRCMP(items[0], "COMPOUNDMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004526 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004527 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004528 compmax = atoi((char *)items[1]);
4529 if (compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004530 smsg((char_u *)_("Wrong COMPOUNDMAX value in %s line %d: %s"),
4531 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004532 }
4533 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004534 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004535 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004536 compminlen = atoi((char *)items[1]);
4537 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004538 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
4539 fname, lnum, items[1]);
4540 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004541 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004542 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004543 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004544 compsylmax = atoi((char *)items[1]);
4545 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004546 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
4547 fname, lnum, items[1]);
4548 }
4549 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004550 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004551 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004552 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004553 }
Bram Moolenaar78622822005-08-23 21:00:13 +00004554 else if (STRCMP(items[0], "NOBREAK") == 0 && itemcnt == 1)
4555 {
4556 spin->si_nobreak = TRUE;
4557 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004558 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
4559 {
4560 aff->af_pfxpostpone = TRUE;
4561 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004562 else if ((STRCMP(items[0], "PFX") == 0
4563 || STRCMP(items[0], "SFX") == 0)
4564 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00004565 && itemcnt >= 4)
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. */
4569 if (itemcnt > 4 && *items[4] != '#')
4570 smsg((char_u *)_("Trailing text in %s line %d: %s"),
4571 fname, lnum, items[4]);
4572
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004573 /* New affix letter. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004574 cur_aff = (affheader_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004575 sizeof(affheader_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004576 if (cur_aff == NULL)
4577 break;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004578 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
4579 fname, lnum);
4580 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
4581 break;
4582 STRCPY(cur_aff->ah_key, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004583 if (*items[2] == 'Y')
4584 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004585 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004586 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
4587 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004588
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004589 if (*items[0] == 'P')
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004590 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004591 tp = &aff->af_pref;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004592 if (aff->af_pfxpostpone)
Bram Moolenaar6de68532005-08-24 22:08:48 +00004593 {
4594 /* Use a new number in the .spl file later, to be able
4595 * to handle multiple .aff files. */
4596 cur_aff->ah_newID = ++spin->si_newprefID;
4597
4598 /* We only really use ah_newID if the prefix is
4599 * postponed. We know that only after handling all
4600 * the items. */
4601 did_postpone_prefix = FALSE;
4602 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004603 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004604 else
4605 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004606 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004607 hi = hash_find(tp, cur_aff->ah_key);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004608 if (!HASHITEM_EMPTY(hi)
4609 || cur_aff->ah_flag == aff->af_bad
4610 || cur_aff->ah_flag == aff->af_rar
4611 || cur_aff->ah_flag == aff->af_kep
4612 || cur_aff->ah_flag == aff->af_needaffix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004613 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004614 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
4615 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004616 aff_todo = 0;
4617 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004618 else
4619 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004620 }
4621 else if ((STRCMP(items[0], "PFX") == 0
4622 || STRCMP(items[0], "SFX") == 0)
4623 && aff_todo > 0
4624 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00004625 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004626 {
4627 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004628 int rare = FALSE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004629 int nocomp = FALSE;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004630 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004631 int lasti = 5;
4632
Bram Moolenaar5195e452005-08-19 20:32:47 +00004633 /* Check for "rare" and "nocomp" after the other info. */
4634 while (itemcnt > lasti)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004635 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004636 if (!rare && STRICMP(items[lasti], "rare") == 0)
4637 {
4638 rare = TRUE;
4639 ++lasti;
4640 }
4641 else if (!nocomp && STRICMP(items[lasti], "nocomp") == 0)
4642 {
4643 nocomp = TRUE;
4644 ++lasti;
4645 }
4646 else
4647 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004648 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004649
Bram Moolenaar8db73182005-06-17 21:51:16 +00004650 /* Myspell allows extra text after the item, but that might
4651 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004652 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004653 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00004654
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004655 /* New item for an affix letter. */
4656 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004657 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004658 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004659 if (aff_entry == NULL)
4660 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004661 aff_entry->ae_rare = rare;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004662 aff_entry->ae_nocomp = nocomp;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004663
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004664 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004665 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004666 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004667 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004668
Bram Moolenaar51485f02005-06-04 21:55:20 +00004669 /* Don't use an affix entry with non-ASCII characters when
4670 * "spin->si_ascii" is TRUE. */
4671 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004672 || has_non_ascii(aff_entry->ae_add)))
4673 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00004674 aff_entry->ae_next = cur_aff->ah_first;
4675 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004676
4677 if (STRCMP(items[4], ".") != 0)
4678 {
4679 char_u buf[MAXLINELEN];
4680
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004681 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004682 if (*items[0] == 'P')
4683 sprintf((char *)buf, "^%s", items[4]);
4684 else
4685 sprintf((char *)buf, "%s$", items[4]);
4686 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004687 RE_MAGIC + RE_STRING + RE_STRICT);
4688 if (aff_entry->ae_prog == NULL)
4689 smsg((char_u *)_("Broken condition in %s line %d: %s"),
4690 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004691 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004692
4693 /* For postponed prefixes we need an entry in si_prefcond
4694 * for the condition. Use an existing one if possible. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004695 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004696 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004697 /* When the chop string is one lower-case letter and
4698 * the add string ends in the upper-case letter we set
4699 * the "upper" flag, clear "ae_chop" and remove the
4700 * letters from "ae_add". The condition must either
4701 * be empty or start with the same letter. */
4702 if (aff_entry->ae_chop != NULL
4703 && aff_entry->ae_add != NULL
4704#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004705 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00004706 aff_entry->ae_chop)] == NUL
4707#else
4708 && aff_entry->ae_chop[1] == NUL
4709#endif
4710 )
4711 {
4712 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004713
Bram Moolenaar53805d12005-08-01 07:08:33 +00004714 c = PTR2CHAR(aff_entry->ae_chop);
4715 c_up = SPELL_TOUPPER(c);
4716 if (c_up != c
4717 && (aff_entry->ae_cond == NULL
4718 || PTR2CHAR(aff_entry->ae_cond) == c))
4719 {
4720 p = aff_entry->ae_add
4721 + STRLEN(aff_entry->ae_add);
4722 mb_ptr_back(aff_entry->ae_add, p);
4723 if (PTR2CHAR(p) == c_up)
4724 {
4725 upper = TRUE;
4726 aff_entry->ae_chop = NULL;
4727 *p = NUL;
4728
4729 /* The condition is matched with the
4730 * actual word, thus must check for the
4731 * upper-case letter. */
4732 if (aff_entry->ae_cond != NULL)
4733 {
4734 char_u buf[MAXLINELEN];
4735#ifdef FEAT_MBYTE
4736 if (has_mbyte)
4737 {
4738 onecap_copy(items[4], buf, TRUE);
4739 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004740 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004741 }
4742 else
4743#endif
4744 *aff_entry->ae_cond = c_up;
4745 if (aff_entry->ae_cond != NULL)
4746 {
4747 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004748 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004749 vim_free(aff_entry->ae_prog);
4750 aff_entry->ae_prog = vim_regcomp(
4751 buf, RE_MAGIC + RE_STRING);
4752 }
4753 }
4754 }
4755 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004756 }
4757
Bram Moolenaar53805d12005-08-01 07:08:33 +00004758 if (aff_entry->ae_chop == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004759 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004760 int idx;
4761 char_u **pp;
4762 int n;
4763
Bram Moolenaar6de68532005-08-24 22:08:48 +00004764 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004765 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
4766 --idx)
4767 {
4768 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
4769 if (str_equal(p, aff_entry->ae_cond))
4770 break;
4771 }
4772 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
4773 {
4774 /* Not found, add a new condition. */
4775 idx = spin->si_prefcond.ga_len++;
4776 pp = ((char_u **)spin->si_prefcond.ga_data)
4777 + idx;
4778 if (aff_entry->ae_cond == NULL)
4779 *pp = NULL;
4780 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004781 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004782 aff_entry->ae_cond);
4783 }
4784
4785 /* Add the prefix to the prefix tree. */
4786 if (aff_entry->ae_add == NULL)
4787 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004788 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00004789 p = aff_entry->ae_add;
4790 /* PFX_FLAGS is a negative number, so that
4791 * tree_add_word() knows this is the prefix tree. */
4792 n = PFX_FLAGS;
4793 if (rare)
4794 n |= WFP_RARE;
4795 if (!cur_aff->ah_combine)
4796 n |= WFP_NC;
4797 if (upper)
4798 n |= WFP_UP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004799 tree_add_word(spin, p, spin->si_prefroot, n,
4800 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004801 did_postpone_prefix = TRUE;
4802 }
4803
4804 /* Didn't actually use ah_newID, backup si_newprefID. */
4805 if (aff_todo == 0 && !did_postpone_prefix)
4806 {
4807 --spin->si_newprefID;
4808 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004809 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004810 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00004811 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004812 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00004813 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2
4814 && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004815 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004816 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004817 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00004818 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2
4819 && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004820 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004821 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004822 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00004823 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2
4824 && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004825 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004826 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004827 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004828 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004829 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004830 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004831 if (!isdigit(*items[1]))
4832 smsg((char_u *)_("Expected REP count in %s line %d"),
4833 fname, lnum);
4834 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004835 else if (STRCMP(items[0], "REP") == 0 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004836 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004837 /* REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004838 /* Myspell ignores extra arguments, we require it starts with
4839 * # to detect mistakes. */
4840 if (itemcnt > 3 && items[3][0] != '#')
4841 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004842 if (do_rep)
4843 add_fromto(spin, &spin->si_rep, items[1], items[2]);
4844 }
4845 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
4846 {
4847 /* MAP item or count */
4848 if (!found_map)
4849 {
4850 /* First line contains the count. */
4851 found_map = TRUE;
4852 if (!isdigit(*items[1]))
4853 smsg((char_u *)_("Expected MAP count in %s line %d"),
4854 fname, lnum);
4855 }
4856 else if (do_map)
4857 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004858 int c;
4859
4860 /* Check that every character appears only once. */
4861 for (p = items[1]; *p != NUL; )
4862 {
4863#ifdef FEAT_MBYTE
4864 c = mb_ptr2char_adv(&p);
4865#else
4866 c = *p++;
4867#endif
4868 if ((spin->si_map.ga_len > 0
4869 && vim_strchr(spin->si_map.ga_data, c)
4870 != NULL)
4871 || vim_strchr(p, c) != NULL)
4872 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
4873 fname, lnum);
4874 }
4875
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004876 /* We simply concatenate all the MAP strings, separated by
4877 * slashes. */
4878 ga_concat(&spin->si_map, items[1]);
4879 ga_append(&spin->si_map, '/');
4880 }
4881 }
4882 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
4883 {
4884 if (do_sal)
4885 {
4886 /* SAL item (sounds-a-like)
4887 * Either one of the known keys or a from-to pair. */
4888 if (STRCMP(items[1], "followup") == 0)
4889 spin->si_followup = sal_to_bool(items[2]);
4890 else if (STRCMP(items[1], "collapse_result") == 0)
4891 spin->si_collapse = sal_to_bool(items[2]);
4892 else if (STRCMP(items[1], "remove_accents") == 0)
4893 spin->si_rem_accents = sal_to_bool(items[2]);
4894 else
4895 /* when "to" is "_" it means empty */
4896 add_fromto(spin, &spin->si_sal, items[1],
4897 STRCMP(items[2], "_") == 0 ? (char_u *)""
4898 : items[2]);
4899 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004900 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004901 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004902 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004903 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004904 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004905 }
4906 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004907 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004908 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004909 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004910 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004911 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004912 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004913 fname, lnum, items[0]);
4914 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004915 }
4916
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004917 if (fol != NULL || low != NULL || upp != NULL)
4918 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004919 if (spin->si_clear_chartab)
4920 {
4921 /* Clear the char type tables, don't want to use any of the
4922 * currently used spell properties. */
4923 init_spell_chartab();
4924 spin->si_clear_chartab = FALSE;
4925 }
4926
Bram Moolenaar3982c542005-06-08 21:56:31 +00004927 /*
4928 * Don't write a word table for an ASCII file, so that we don't check
4929 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004930 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00004931 * mb_get_class(), the list of chars in the file will be incomplete.
4932 */
4933 if (!spin->si_ascii
4934#ifdef FEAT_MBYTE
4935 && !enc_utf8
4936#endif
4937 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004938 {
4939 if (fol == NULL || low == NULL || upp == NULL)
4940 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
4941 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00004942 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00004943 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004944
4945 vim_free(fol);
4946 vim_free(low);
4947 vim_free(upp);
4948 }
4949
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004950 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004951 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004952 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004953 aff_check_number(spin->si_compmax, compmax, "COMPOUNDMAX");
4954 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004955 }
4956
Bram Moolenaar6de68532005-08-24 22:08:48 +00004957 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004958 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004959 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
4960 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004961 }
4962
Bram Moolenaar6de68532005-08-24 22:08:48 +00004963 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004964 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004965 if (syllable == NULL)
4966 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
4967 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
4968 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004969 }
4970
Bram Moolenaar6de68532005-08-24 22:08:48 +00004971 if (compflags != NULL)
4972 process_compflags(spin, aff, compflags);
4973
4974 /* Check that we didn't use too many renumbered flags. */
4975 if (spin->si_compID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004976 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004977 if (spin->si_compID == 255)
4978 MSG(_("Too many postponed prefixes"));
4979 else if (spin->si_newprefID == 0)
4980 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004981 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00004982 MSG(_("Too many posponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004983 }
4984
Bram Moolenaar6de68532005-08-24 22:08:48 +00004985 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004986 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004987 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
4988 spin->si_syllable = syllable;
4989 }
4990
4991 if (sofofrom != NULL || sofoto != NULL)
4992 {
4993 if (sofofrom == NULL || sofoto == NULL)
4994 smsg((char_u *)_("Missing SOFO%s line in %s"),
4995 sofofrom == NULL ? "FROM" : "TO", fname);
4996 else if (spin->si_sal.ga_len > 0)
4997 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004998 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00004999 {
5000 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
5001 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
5002 spin->si_sofofr = sofofrom;
5003 spin->si_sofoto = sofoto;
5004 }
5005 }
5006
5007 if (midword != NULL)
5008 {
5009 aff_check_string(spin->si_midword, midword, "MIDWORD");
5010 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005011 }
5012
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005013 vim_free(pc);
5014 fclose(fd);
5015 return aff;
5016}
5017
5018/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005019 * Turn an affix flag name into a number, according to the FLAG type.
5020 * returns zero for failure.
5021 */
5022 static unsigned
5023affitem2flag(flagtype, item, fname, lnum)
5024 int flagtype;
5025 char_u *item;
5026 char_u *fname;
5027 int lnum;
5028{
5029 unsigned res;
5030 char_u *p = item;
5031
5032 res = get_affitem(flagtype, &p);
5033 if (res == 0)
5034 {
5035 if (flagtype == AFT_NUMBER)
5036 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
5037 fname, lnum, item);
5038 else
5039 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
5040 fname, lnum, item);
5041 }
5042 if (*p != NUL)
5043 {
5044 smsg((char_u *)_(e_affname), fname, lnum, item);
5045 return 0;
5046 }
5047
5048 return res;
5049}
5050
5051/*
5052 * Get one affix name from "*pp" and advance the pointer.
5053 * Returns zero for an error, still advances the pointer then.
5054 */
5055 static unsigned
5056get_affitem(flagtype, pp)
5057 int flagtype;
5058 char_u **pp;
5059{
5060 int res;
5061
5062 if (flagtype == AFT_NUMBER)
5063 {
5064 if (!VIM_ISDIGIT(**pp))
5065 {
5066 ++*pp;
5067 return 0;
5068 }
5069 res = getdigits(pp);
5070 }
5071 else
5072 {
5073#ifdef FEAT_MBYTE
5074 res = mb_ptr2char_adv(pp);
5075#else
5076 res = *(*pp)++;
5077#endif
5078 if (flagtype == AFT_2CHAR || (flagtype == AFT_HUH
5079 && res >= 'A' && res <= 'Z'))
5080 {
5081 if (**pp == NUL)
5082 return 0;
5083#ifdef FEAT_MBYTE
5084 res = mb_ptr2char_adv(pp) + (res << 16);
5085#else
5086 res = *(*pp)++ + (res << 16);
5087#endif
5088 }
5089 }
5090 return res;
5091}
5092
5093/*
5094 * Process the "compflags" string used in an affix file and append it to
5095 * spin->si_compflags.
5096 * The processing involves changing the affix names to ID numbers, so that
5097 * they fit in one byte.
5098 */
5099 static void
5100process_compflags(spin, aff, compflags)
5101 spellinfo_T *spin;
5102 afffile_T *aff;
5103 char_u *compflags;
5104{
5105 char_u *p;
5106 char_u *prevp;
5107 unsigned flag;
5108 compitem_T *ci;
5109 int id;
5110 int len;
5111 char_u *tp;
5112 char_u key[AH_KEY_LEN];
5113 hashitem_T *hi;
5114
5115 /* Make room for the old and the new compflags, concatenated with a / in
5116 * between. Processing it makes it shorter, but we don't know by how
5117 * much, thus allocate the maximum. */
5118 len = STRLEN(compflags) + 1;
5119 if (spin->si_compflags != NULL)
5120 len += STRLEN(spin->si_compflags) + 1;
5121 p = getroom(spin, len, FALSE);
5122 if (p == NULL)
5123 return;
5124 if (spin->si_compflags != NULL)
5125 {
5126 STRCPY(p, spin->si_compflags);
5127 STRCAT(p, "/");
5128 }
5129 else
5130 *p = NUL;
5131 spin->si_compflags = p;
5132 tp = p + STRLEN(p);
5133
5134 for (p = compflags; *p != NUL; )
5135 {
5136 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
5137 /* Copy non-flag characters directly. */
5138 *tp++ = *p++;
5139 else
5140 {
5141 /* First get the flag number, also checks validity. */
5142 prevp = p;
5143 flag = get_affitem(aff->af_flagtype, &p);
5144 if (flag != 0)
5145 {
5146 /* Find the flag in the hashtable. If it was used before, use
5147 * the existing ID. Otherwise add a new entry. */
5148 vim_strncpy(key, prevp, p - prevp);
5149 hi = hash_find(&aff->af_comp, key);
5150 if (!HASHITEM_EMPTY(hi))
5151 id = HI2CI(hi)->ci_newID;
5152 else
5153 {
5154 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
5155 if (ci == NULL)
5156 break;
5157 STRCPY(ci->ci_key, key);
5158 ci->ci_flag = flag;
5159 /* Avoid using a flag ID that has a special meaning in a
5160 * regexp (also inside []). */
5161 do
5162 {
5163 id = spin->si_compID--;
5164 } while (vim_strchr((char_u *)"/*+[]\\-^", id) != NULL);
5165 ci->ci_newID = id;
5166 hash_add(&aff->af_comp, ci->ci_key);
5167 }
5168 *tp++ = id;
5169 }
5170 if (aff->af_flagtype == AFT_NUMBER && *p == ',')
5171 ++p;
5172 }
5173 }
5174
5175 *tp = NUL;
5176}
5177
5178/*
5179 * Return TRUE if flag "flag" appears in affix list "afflist".
5180 */
5181 static int
5182flag_in_afflist(flagtype, afflist, flag)
5183 int flagtype;
5184 char_u *afflist;
5185 unsigned flag;
5186{
5187 char_u *p;
5188 unsigned n;
5189
5190 switch (flagtype)
5191 {
5192 case AFT_CHAR:
5193 return vim_strchr(afflist, flag) != NULL;
5194
5195 case AFT_HUH:
5196 case AFT_2CHAR:
5197 for (p = afflist; *p != NUL; )
5198 {
5199#ifdef FEAT_MBYTE
5200 n = mb_ptr2char_adv(&p);
5201#else
5202 n = *p++;
5203#endif
5204 if ((flagtype == AFT_2CHAR || (n >= 'A' && n <= 'Z'))
5205 && *p != NUL)
5206#ifdef FEAT_MBYTE
5207 n = mb_ptr2char_adv(&p) + (n << 16);
5208#else
5209 n = *p++ + (n << 16);
5210#endif
5211 if (n == flag)
5212 return TRUE;
5213 }
5214 break;
5215
5216 case AFT_NUMBER:
5217 for (p = afflist; *p != NUL; )
5218 {
5219 n = getdigits(&p);
5220 if (n == flag)
5221 return TRUE;
5222 if (*p != NUL) /* skip over comma */
5223 ++p;
5224 }
5225 break;
5226 }
5227 return FALSE;
5228}
5229
5230/*
5231 * Give a warning when "spinval" and "affval" numbers are set and not the same.
5232 */
5233 static void
5234aff_check_number(spinval, affval, name)
5235 int spinval;
5236 int affval;
5237 char *name;
5238{
5239 if (spinval != 0 && spinval != affval)
5240 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
5241}
5242
5243/*
5244 * Give a warning when "spinval" and "affval" strings are set and not the same.
5245 */
5246 static void
5247aff_check_string(spinval, affval, name)
5248 char_u *spinval;
5249 char_u *affval;
5250 char *name;
5251{
5252 if (spinval != NULL && STRCMP(spinval, affval) != 0)
5253 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
5254}
5255
5256/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005257 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
5258 * NULL as equal.
5259 */
5260 static int
5261str_equal(s1, s2)
5262 char_u *s1;
5263 char_u *s2;
5264{
5265 if (s1 == NULL || s2 == NULL)
5266 return s1 == s2;
5267 return STRCMP(s1, s2) == 0;
5268}
5269
5270/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005271 * Add a from-to item to "gap". Used for REP and SAL items.
5272 * They are stored case-folded.
5273 */
5274 static void
5275add_fromto(spin, gap, from, to)
5276 spellinfo_T *spin;
5277 garray_T *gap;
5278 char_u *from;
5279 char_u *to;
5280{
5281 fromto_T *ftp;
5282 char_u word[MAXWLEN];
5283
5284 if (ga_grow(gap, 1) == OK)
5285 {
5286 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
5287 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005288 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005289 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005290 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005291 ++gap->ga_len;
5292 }
5293}
5294
5295/*
5296 * Convert a boolean argument in a SAL line to TRUE or FALSE;
5297 */
5298 static int
5299sal_to_bool(s)
5300 char_u *s;
5301{
5302 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
5303}
5304
5305/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00005306 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
5307 * When "s" is NULL FALSE is returned.
5308 */
5309 static int
5310has_non_ascii(s)
5311 char_u *s;
5312{
5313 char_u *p;
5314
5315 if (s != NULL)
5316 for (p = s; *p != NUL; ++p)
5317 if (*p >= 128)
5318 return TRUE;
5319 return FALSE;
5320}
5321
5322/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005323 * Free the structure filled by spell_read_aff().
5324 */
5325 static void
5326spell_free_aff(aff)
5327 afffile_T *aff;
5328{
5329 hashtab_T *ht;
5330 hashitem_T *hi;
5331 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005332 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005333 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005334
5335 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005336
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005337 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005338 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
5339 {
5340 todo = ht->ht_used;
5341 for (hi = ht->ht_array; todo > 0; ++hi)
5342 {
5343 if (!HASHITEM_EMPTY(hi))
5344 {
5345 --todo;
5346 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005347 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
5348 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005349 }
5350 }
5351 if (ht == &aff->af_suff)
5352 break;
5353 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005354
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005355 hash_clear(&aff->af_pref);
5356 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005357 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005358}
5359
5360/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005361 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005362 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005363 */
5364 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005365spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005366 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005367 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005368 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005369{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005370 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005371 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005372 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005373 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005374 char_u store_afflist[MAXWLEN];
5375 int pfxlen;
5376 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005377 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005378 char_u *pc;
5379 char_u *w;
5380 int l;
5381 hash_T hash;
5382 hashitem_T *hi;
5383 FILE *fd;
5384 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005385 int non_ascii = 0;
5386 int retval = OK;
5387 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005388 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005389 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005390
Bram Moolenaar51485f02005-06-04 21:55:20 +00005391 /*
5392 * Open the file.
5393 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005394 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005395 if (fd == NULL)
5396 {
5397 EMSG2(_(e_notopen), fname);
5398 return FAIL;
5399 }
5400
Bram Moolenaar51485f02005-06-04 21:55:20 +00005401 /* The hashtable is only used to detect duplicated words. */
5402 hash_init(&ht);
5403
Bram Moolenaarb765d632005-06-07 21:00:02 +00005404 if (spin->si_verbose || p_verbose > 2)
5405 {
5406 if (!spin->si_verbose)
5407 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005408 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005409 out_flush();
5410 if (!spin->si_verbose)
5411 verbose_leave();
5412 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005413
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005414 /* start with a message for the first line */
5415 spin->si_msg_count = 999999;
5416
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005417 /* Read and ignore the first line: word count. */
5418 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005419 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005420 EMSG2(_("E760: No word count in %s"), fname);
5421
5422 /*
5423 * Read all the lines in the file one by one.
5424 * The words are converted to 'encoding' here, before being added to
5425 * the hashtable.
5426 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005427 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005428 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005429 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005430 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005431 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005432 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005433
Bram Moolenaar51485f02005-06-04 21:55:20 +00005434 /* Remove CR, LF and white space from the end. White space halfway
5435 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005436 l = STRLEN(line);
5437 while (l > 0 && line[l - 1] <= ' ')
5438 --l;
5439 if (l == 0)
5440 continue; /* empty line */
5441 line[l] = NUL;
5442
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005443 /* Find the optional affix names. Replace the SLASH character by a
5444 * slash. */
5445 afflist = NULL;
5446 for (p = line; *p != NUL; mb_ptr_adv(p))
5447 {
5448 if (*p == affile->af_slash)
5449 *p = '/';
5450 else if (*p == '/')
5451 {
5452 *p = NUL;
5453 afflist = p + 1;
5454 break;
5455 }
5456 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005457
5458 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
5459 if (spin->si_ascii && has_non_ascii(line))
5460 {
5461 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005462 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005463 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005464
Bram Moolenaarb765d632005-06-07 21:00:02 +00005465#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005466 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005467 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005468 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005469 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005470 if (pc == NULL)
5471 {
5472 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5473 fname, lnum, line);
5474 continue;
5475 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005476 w = pc;
5477 }
5478 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005479#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005480 {
5481 pc = NULL;
5482 w = line;
5483 }
5484
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005485 /* This takes time, print a message every 10000 words. */
5486 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005487 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005488 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005489 vim_snprintf((char *)message, sizeof(message),
5490 _("line %6d, word %6d - %s"),
5491 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
5492 msg_start();
5493 msg_puts_long_attr(message, 0);
5494 msg_clr_eos();
5495 msg_didout = FALSE;
5496 msg_col = 0;
5497 out_flush();
5498 }
5499
Bram Moolenaar51485f02005-06-04 21:55:20 +00005500 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005501 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005502 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005503 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005504 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005505 if (retval == FAIL)
5506 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005507
Bram Moolenaar51485f02005-06-04 21:55:20 +00005508 hash = hash_hash(dw);
5509 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005510 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005511 {
5512 if (p_verbose > 0)
5513 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005514 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005515 else if (duplicate == 0)
5516 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
5517 fname, lnum, dw);
5518 ++duplicate;
5519 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005520 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00005521 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005522
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005523 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005524 store_afflist[0] = NUL;
5525 pfxlen = 0;
5526 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005527 if (afflist != NULL)
5528 {
5529 /* Check for affix name that stands for keep-case word and stands
5530 * for rare word (if defined). */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005531 if (affile->af_kep != 0 && flag_in_afflist(
5532 affile->af_flagtype, afflist, affile->af_kep))
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005533 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005534 if (affile->af_rar != 0 && flag_in_afflist(
5535 affile->af_flagtype, afflist, affile->af_rar))
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005536 flags |= WF_RARE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005537 if (affile->af_bad != 0 && flag_in_afflist(
5538 affile->af_flagtype, afflist, affile->af_bad))
Bram Moolenaar0c405862005-06-22 22:26:26 +00005539 flags |= WF_BANNED;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005540 if (affile->af_needaffix != 0 && flag_in_afflist(
5541 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00005542 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005543
5544 if (affile->af_pfxpostpone)
5545 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005546 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005547
Bram Moolenaar5195e452005-08-19 20:32:47 +00005548 if (spin->si_compflags != NULL)
5549 /* Need to store the list of compound flags with the word.
5550 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005551 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005552 }
5553
Bram Moolenaar51485f02005-06-04 21:55:20 +00005554 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005555 if (store_word(spin, dw, flags, spin->si_region,
5556 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005557 retval = FAIL;
5558
5559 if (afflist != NULL)
5560 {
5561 /* Find all matching suffixes and add the resulting words.
5562 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005563 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005564 &affile->af_suff, &affile->af_pref,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005565 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005566 retval = FAIL;
5567
5568 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005569 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005570 &affile->af_pref, NULL,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005571 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005572 retval = FAIL;
5573 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005574 }
5575
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005576 if (duplicate > 0)
5577 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005578 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005579 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
5580 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005581 hash_clear(&ht);
5582
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005583 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005584 return retval;
5585}
5586
5587/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005588 * Get the list of prefix IDs from the affix list "afflist".
5589 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00005590 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
5591 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005592 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005593 static int
5594get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005595 afffile_T *affile;
5596 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005597 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005598{
5599 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005600 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005601 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005602 int id;
5603 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005604 hashitem_T *hi;
5605
Bram Moolenaar6de68532005-08-24 22:08:48 +00005606 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005607 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005608 prevp = p;
5609 if (get_affitem(affile->af_flagtype, &p) != 0)
5610 {
5611 /* A flag is a postponed prefix flag if it appears in "af_pref"
5612 * and it's ID is not zero. */
5613 vim_strncpy(key, prevp, p - prevp);
5614 hi = hash_find(&affile->af_pref, key);
5615 if (!HASHITEM_EMPTY(hi))
5616 {
5617 id = HI2AH(hi)->ah_newID;
5618 if (id != 0)
5619 store_afflist[cnt++] = id;
5620 }
5621 }
5622 if (affile->af_flagtype == AFT_NUMBER && *p == ',')
5623 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005624 }
5625
Bram Moolenaar5195e452005-08-19 20:32:47 +00005626 store_afflist[cnt] = NUL;
5627 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005628}
5629
5630/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005631 * Get the list of compound IDs from the affix list "afflist" that are used
5632 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00005633 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005634 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005635 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00005636get_compflags(affile, afflist, store_afflist)
5637 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005638 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005639 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005640{
5641 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005642 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005643 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005644 char_u key[AH_KEY_LEN];
5645 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005646
Bram Moolenaar6de68532005-08-24 22:08:48 +00005647 for (p = afflist; *p != NUL; )
5648 {
5649 prevp = p;
5650 if (get_affitem(affile->af_flagtype, &p) != 0)
5651 {
5652 /* A flag is a compound flag if it appears in "af_comp". */
5653 vim_strncpy(key, prevp, p - prevp);
5654 hi = hash_find(&affile->af_comp, key);
5655 if (!HASHITEM_EMPTY(hi))
5656 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
5657 }
5658 if (affile->af_flagtype == AFT_NUMBER && *p == ',')
5659 ++p;
5660 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005661
Bram Moolenaar5195e452005-08-19 20:32:47 +00005662 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005663}
5664
5665/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005666 * Apply affixes to a word and store the resulting words.
5667 * "ht" is the hashtable with affentry_T that need to be applied, either
5668 * prefixes or suffixes.
5669 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
5670 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005671 *
5672 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005673 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005674 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00005675store_aff_word(spin, word, afflist, affile, ht, xht, comb, flags,
5676 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005677 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005678 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005679 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005680 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005681 hashtab_T *ht;
5682 hashtab_T *xht;
5683 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005684 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005685 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005686 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
5687 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005688{
5689 int todo;
5690 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005691 affheader_T *ah;
5692 affentry_T *ae;
5693 regmatch_T regmatch;
5694 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005695 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005696 int i;
5697 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005698 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005699 char_u *use_pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005700 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00005701 size_t wordlen = STRLEN(word);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005702
Bram Moolenaar51485f02005-06-04 21:55:20 +00005703 todo = ht->ht_used;
5704 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005705 {
5706 if (!HASHITEM_EMPTY(hi))
5707 {
5708 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005709 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00005710
Bram Moolenaar51485f02005-06-04 21:55:20 +00005711 /* Check that the affix combines, if required, and that the word
5712 * supports this affix. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005713 if ((!comb || ah->ah_combine) && flag_in_afflist(
5714 affile->af_flagtype, afflist, ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00005715 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005716 /* Loop over all affix entries with this name. */
5717 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005718 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005719 /* Check the condition. It's not logical to match case
5720 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005721 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005722 * Another requirement from Myspell is that the chop
5723 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005724 * For prefixes, when "PFXPOSTPONE" was used, only do
5725 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005726 regmatch.regprog = ae->ae_prog;
5727 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005728 if ((xht != NULL || !affile->af_pfxpostpone
5729 || ae->ae_chop != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005730 && (ae->ae_chop == NULL
5731 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005732 && (ae->ae_prog == NULL
5733 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005734 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005735 /* Match. Remove the chop and add the affix. */
5736 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005737 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005738 /* prefix: chop/add at the start of the word */
5739 if (ae->ae_add == NULL)
5740 *newword = NUL;
5741 else
5742 STRCPY(newword, ae->ae_add);
5743 p = word;
5744 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005745 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005746 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005747#ifdef FEAT_MBYTE
5748 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005749 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005750 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005751 for ( ; i > 0; --i)
5752 mb_ptr_adv(p);
5753 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005754 else
5755#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005756 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005757 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005758 STRCAT(newword, p);
5759 }
5760 else
5761 {
5762 /* suffix: chop/add at the end of the word */
5763 STRCPY(newword, word);
5764 if (ae->ae_chop != NULL)
5765 {
5766 /* Remove chop string. */
5767 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005768 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005769 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005770 mb_ptr_back(newword, p);
5771 *p = NUL;
5772 }
5773 if (ae->ae_add != NULL)
5774 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005775 }
5776
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005777 /* Obey the "rare" flag of the affix. */
5778 if (ae->ae_rare)
5779 use_flags = flags | WF_RARE;
5780 else
5781 use_flags = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005782
5783 /* Obey the "nocomp" flag of the affix: don't use the
5784 * compound flags. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005785 use_pfxlist = pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005786 if (ae->ae_nocomp && pfxlist != NULL)
5787 {
5788 vim_strncpy(pfx_pfxlist, pfxlist, pfxlen);
5789 use_pfxlist = pfx_pfxlist;
5790 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005791
5792 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00005793 if (spin->si_prefroot != NULL
5794 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005795 {
5796 /* ... add a flag to indicate an affix was used. */
5797 use_flags |= WF_HAS_AFF;
5798
5799 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00005800 * affixes is not allowed. But do use the
5801 * compound flags after them. */
5802 if ((!ah->ah_combine || comb) && pfxlist != NULL)
5803 use_pfxlist += pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005804 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005805
Bram Moolenaar51485f02005-06-04 21:55:20 +00005806 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005807 if (store_word(spin, newword, use_flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005808 spin->si_region, use_pfxlist, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005809 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005810
Bram Moolenaar51485f02005-06-04 21:55:20 +00005811 /* When added a suffix and combining is allowed also
5812 * try adding prefixes additionally. */
5813 if (xht != NULL && ah->ah_combine)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005814 if (store_aff_word(spin, newword, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005815 xht, NULL, TRUE,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005816 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005817 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005818 }
5819 }
5820 }
5821 }
5822 }
5823
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005824 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005825}
5826
5827/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005828 * Read a file with a list of words.
5829 */
5830 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005831spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005832 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005833 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005834{
5835 FILE *fd;
5836 long lnum = 0;
5837 char_u rline[MAXLINELEN];
5838 char_u *line;
5839 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005840 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005841 int l;
5842 int retval = OK;
5843 int did_word = FALSE;
5844 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005845 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00005846 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005847
5848 /*
5849 * Open the file.
5850 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005851 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00005852 if (fd == NULL)
5853 {
5854 EMSG2(_(e_notopen), fname);
5855 return FAIL;
5856 }
5857
Bram Moolenaarb765d632005-06-07 21:00:02 +00005858 if (spin->si_verbose || p_verbose > 2)
5859 {
5860 if (!spin->si_verbose)
5861 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005862 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005863 out_flush();
5864 if (!spin->si_verbose)
5865 verbose_leave();
5866 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005867
5868 /*
5869 * Read all the lines in the file one by one.
5870 */
5871 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
5872 {
5873 line_breakcheck();
5874 ++lnum;
5875
5876 /* Skip comment lines. */
5877 if (*rline == '#')
5878 continue;
5879
5880 /* Remove CR, LF and white space from the end. */
5881 l = STRLEN(rline);
5882 while (l > 0 && rline[l - 1] <= ' ')
5883 --l;
5884 if (l == 0)
5885 continue; /* empty or blank line */
5886 rline[l] = NUL;
5887
5888 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
5889 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005890#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005891 if (spin->si_conv.vc_type != CONV_NONE)
5892 {
5893 pc = string_convert(&spin->si_conv, rline, NULL);
5894 if (pc == NULL)
5895 {
5896 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5897 fname, lnum, rline);
5898 continue;
5899 }
5900 line = pc;
5901 }
5902 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005903#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00005904 {
5905 pc = NULL;
5906 line = rline;
5907 }
5908
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005909 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00005910 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005911 ++line;
5912 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005913 {
5914 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00005915 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
5916 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005917 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00005918 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
5919 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005920 else
5921 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005922#ifdef FEAT_MBYTE
5923 char_u *enc;
5924
Bram Moolenaar51485f02005-06-04 21:55:20 +00005925 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00005926 line += 10;
5927 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005928 if (enc != NULL && !spin->si_ascii
5929 && convert_setup(&spin->si_conv, enc,
5930 p_enc) == FAIL)
5931 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00005932 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005933 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005934 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005935#else
5936 smsg((char_u *)_("Conversion in %s not supported"), fname);
5937#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00005938 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005939 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005940 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005941
Bram Moolenaar3982c542005-06-08 21:56:31 +00005942 if (STRNCMP(line, "regions=", 8) == 0)
5943 {
5944 if (spin->si_region_count > 1)
5945 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
5946 fname, lnum, line);
5947 else
5948 {
5949 line += 8;
5950 if (STRLEN(line) > 16)
5951 smsg((char_u *)_("Too many regions in %s line %d: %s"),
5952 fname, lnum, line);
5953 else
5954 {
5955 spin->si_region_count = STRLEN(line) / 2;
5956 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005957
5958 /* Adjust the mask for a word valid in all regions. */
5959 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00005960 }
5961 }
5962 continue;
5963 }
5964
Bram Moolenaar7887d882005-07-01 22:33:52 +00005965 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
5966 fname, lnum, line - 1);
5967 continue;
5968 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005969
Bram Moolenaar7887d882005-07-01 22:33:52 +00005970 flags = 0;
5971 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005972
Bram Moolenaar7887d882005-07-01 22:33:52 +00005973 /* Check for flags and region after a slash. */
5974 p = vim_strchr(line, '/');
5975 if (p != NULL)
5976 {
5977 *p++ = NUL;
5978 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00005979 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005980 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005981 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005982 else if (*p == '!') /* Bad, bad, wicked word. */
5983 flags |= WF_BANNED;
5984 else if (*p == '?') /* Rare word. */
5985 flags |= WF_RARE;
5986 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00005987 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005988 if ((flags & WF_REGION) == 0) /* first one */
5989 regionmask = 0;
5990 flags |= WF_REGION;
5991
5992 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00005993 if (l > spin->si_region_count)
5994 {
5995 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00005996 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00005997 break;
5998 }
5999 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006000 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00006001 else
6002 {
6003 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
6004 fname, lnum, p);
6005 break;
6006 }
6007 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006008 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006009 }
6010
6011 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6012 if (spin->si_ascii && has_non_ascii(line))
6013 {
6014 ++non_ascii;
6015 continue;
6016 }
6017
6018 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006019 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006020 {
6021 retval = FAIL;
6022 break;
6023 }
6024 did_word = TRUE;
6025 }
6026
6027 vim_free(pc);
6028 fclose(fd);
6029
Bram Moolenaarb765d632005-06-07 21:00:02 +00006030 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
6031 {
6032 if (p_verbose > 2)
6033 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00006034 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
6035 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006036 if (p_verbose > 2)
6037 verbose_leave();
6038 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006039 return retval;
6040}
6041
6042/*
6043 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006044 * This avoids calling free() for every little struct we use (and keeping
6045 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00006046 * The memory is cleared to all zeros.
6047 * Returns NULL when out of memory.
6048 */
6049 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006050getroom(spin, len, align)
6051 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006052 size_t len; /* length needed */
6053 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006054{
6055 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006056 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006057
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006058 if (align && bl != NULL)
6059 /* Round size up for alignment. On some systems structures need to be
6060 * aligned to the size of a pointer (e.g., SPARC). */
6061 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
6062 & ~(sizeof(char *) - 1);
6063
Bram Moolenaar51485f02005-06-04 21:55:20 +00006064 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
6065 {
6066 /* Allocate a block of memory. This is not freed until much later. */
6067 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
6068 if (bl == NULL)
6069 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006070 bl->sb_next = spin->si_blocks;
6071 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006072 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006073 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006074 }
6075
6076 p = bl->sb_data + bl->sb_used;
6077 bl->sb_used += len;
6078
6079 return p;
6080}
6081
6082/*
6083 * Make a copy of a string into memory allocated with getroom().
6084 */
6085 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006086getroom_save(spin, s)
6087 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006088 char_u *s;
6089{
6090 char_u *sc;
6091
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006092 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006093 if (sc != NULL)
6094 STRCPY(sc, s);
6095 return sc;
6096}
6097
6098
6099/*
6100 * Free the list of allocated sblock_T.
6101 */
6102 static void
6103free_blocks(bl)
6104 sblock_T *bl;
6105{
6106 sblock_T *next;
6107
6108 while (bl != NULL)
6109 {
6110 next = bl->sb_next;
6111 vim_free(bl);
6112 bl = next;
6113 }
6114}
6115
6116/*
6117 * Allocate the root of a word tree.
6118 */
6119 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006120wordtree_alloc(spin)
6121 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006122{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006123 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006124}
6125
6126/*
6127 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006128 * Always store it in the case-folded tree. For a keep-case word this is
6129 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
6130 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006131 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006132 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
6133 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006134 */
6135 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00006136store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006137 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006138 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006139 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006140 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006141 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006142 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006143{
6144 int len = STRLEN(word);
6145 int ct = captype(word, word + len);
6146 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006147 int res = OK;
6148 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006149
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006150 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006151 for (p = pfxlist; res == OK; ++p)
6152 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006153 if (!need_affix || (p != NULL && *p != NUL))
6154 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006155 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006156 if (p == NULL || *p == NUL)
6157 break;
6158 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00006159 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006160
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006161 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00006162 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006163 for (p = pfxlist; res == OK; ++p)
6164 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006165 if (!need_affix || (p != NULL && *p != NUL))
6166 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006167 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006168 if (p == NULL || *p == NUL)
6169 break;
6170 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00006171 ++spin->si_keepwcount;
6172 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006173 return res;
6174}
6175
6176/*
6177 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006178 * When "flags" < 0 we are adding to the prefix tree where flags is used for
6179 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006180 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006181 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006182 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006183tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006184 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006185 char_u *word;
6186 wordnode_T *root;
6187 int flags;
6188 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006189 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006190{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006191 wordnode_T *node = root;
6192 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006193 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006194 wordnode_T **prev = NULL;
6195 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006196
Bram Moolenaar51485f02005-06-04 21:55:20 +00006197 /* Add each byte of the word to the tree, including the NUL at the end. */
6198 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006199 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006200 /* When there is more than one reference to this node we need to make
6201 * a copy, so that we can modify it. Copy the whole list of siblings
6202 * (we don't optimize for a partly shared list of siblings). */
6203 if (node != NULL && node->wn_refs > 1)
6204 {
6205 --node->wn_refs;
6206 copyprev = prev;
6207 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
6208 {
6209 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006210 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006211 if (np == NULL)
6212 return FAIL;
6213 np->wn_child = copyp->wn_child;
6214 if (np->wn_child != NULL)
6215 ++np->wn_child->wn_refs; /* child gets extra ref */
6216 np->wn_byte = copyp->wn_byte;
6217 if (np->wn_byte == NUL)
6218 {
6219 np->wn_flags = copyp->wn_flags;
6220 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006221 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006222 }
6223
6224 /* Link the new node in the list, there will be one ref. */
6225 np->wn_refs = 1;
6226 *copyprev = np;
6227 copyprev = &np->wn_sibling;
6228
6229 /* Let "node" point to the head of the copied list. */
6230 if (copyp == node)
6231 node = np;
6232 }
6233 }
6234
Bram Moolenaar51485f02005-06-04 21:55:20 +00006235 /* Look for the sibling that has the same character. They are sorted
6236 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006237 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006238 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006239 while (node != NULL
6240 && (node->wn_byte < word[i]
6241 || (node->wn_byte == NUL
6242 && (flags < 0
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006243 ? node->wn_affixID < affixID
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006244 : node->wn_flags < (flags & WN_MASK)
6245 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006246 && node->wn_affixID < affixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006247 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006248 prev = &node->wn_sibling;
6249 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006250 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006251 if (node == NULL
6252 || node->wn_byte != word[i]
6253 || (word[i] == NUL
6254 && (flags < 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006255 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006256 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006257 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006258 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006259 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006260 if (np == NULL)
6261 return FAIL;
6262 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006263
6264 /* If "node" is NULL this is a new child or the end of the sibling
6265 * list: ref count is one. Otherwise use ref count of sibling and
6266 * make ref count of sibling one (matters when inserting in front
6267 * of the list of siblings). */
6268 if (node == NULL)
6269 np->wn_refs = 1;
6270 else
6271 {
6272 np->wn_refs = node->wn_refs;
6273 node->wn_refs = 1;
6274 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006275 *prev = np;
6276 np->wn_sibling = node;
6277 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006278 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006279
Bram Moolenaar51485f02005-06-04 21:55:20 +00006280 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006281 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006282 node->wn_flags = flags;
6283 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006284 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006285 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00006286 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006287 prev = &node->wn_child;
6288 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006289 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006290#ifdef SPELL_PRINTTREE
6291 smsg("Added \"%s\"", word);
6292 spell_print_tree(root->wn_sibling);
6293#endif
6294
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006295 /* count nr of words added since last message */
6296 ++spin->si_msg_count;
6297
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006298 if (spin->si_compress_cnt > 1)
6299 {
6300 if (--spin->si_compress_cnt == 1)
6301 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006302 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006303 }
6304
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006305 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006306 * When we have allocated lots of memory we need to compress the word tree
6307 * to free up some room. But compression is slow, and we might actually
6308 * need that room, thus only compress in the following situations:
6309 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00006310 * "compress_start" blocks.
6311 * 2. When compressed before and used "compress_inc" blocks before
6312 * adding "compress_added" words (si_compress_cnt > 1).
6313 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006314 * (si_compress_cnt == 1) and the number of free nodes drops below the
6315 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006316 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006317#ifndef SPELL_PRINTTREE
6318 if (spin->si_compress_cnt == 1
6319 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00006320 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006321#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006322 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006323 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00006324 * when the freed up room has been used and another "compress_inc"
6325 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006326 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006327 spin->si_blocks_cnt -= compress_inc;
6328 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006329
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006330 if (spin->si_verbose)
6331 {
6332 msg_start();
6333 msg_puts((char_u *)_(msg_compressing));
6334 msg_clr_eos();
6335 msg_didout = FALSE;
6336 msg_col = 0;
6337 out_flush();
6338 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006339
6340 /* Compress both trees. Either they both have many nodes, which makes
6341 * compression useful, or one of them is small, which means
6342 * compression goes fast. */
6343 wordtree_compress(spin, spin->si_foldroot);
6344 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006345 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006346
6347 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006348}
6349
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006350/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00006351 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
6352 * Sets "sps_flags".
6353 */
6354 int
6355spell_check_msm()
6356{
6357 char_u *p = p_msm;
6358 long start = 0;
6359 long inc = 0;
6360 long added = 0;
6361
6362 if (!VIM_ISDIGIT(*p))
6363 return FAIL;
6364 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
6365 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
6366 if (*p != ',')
6367 return FAIL;
6368 ++p;
6369 if (!VIM_ISDIGIT(*p))
6370 return FAIL;
6371 inc = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
6372 if (*p != ',')
6373 return FAIL;
6374 ++p;
6375 if (!VIM_ISDIGIT(*p))
6376 return FAIL;
6377 added = getdigits(&p) * 1024;
6378 if (*p != NUL)
6379 return FAIL;
6380
6381 if (start == 0 || inc == 0 || added == 0 || inc > start)
6382 return FAIL;
6383
6384 compress_start = start;
6385 compress_inc = inc;
6386 compress_added = added;
6387 return OK;
6388}
6389
6390
6391/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006392 * Get a wordnode_T, either from the list of previously freed nodes or
6393 * allocate a new one.
6394 */
6395 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006396get_wordnode(spin)
6397 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006398{
6399 wordnode_T *n;
6400
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006401 if (spin->si_first_free == NULL)
6402 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006403 else
6404 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006405 n = spin->si_first_free;
6406 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006407 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006408 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006409 }
6410#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006411 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006412#endif
6413 return n;
6414}
6415
6416/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006417 * Decrement the reference count on a node (which is the head of a list of
6418 * siblings). If the reference count becomes zero free the node and its
6419 * siblings.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006420 */
6421 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006422deref_wordnode(spin, node)
6423 spellinfo_T *spin;
6424 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006425{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006426 wordnode_T *np;
6427
6428 if (--node->wn_refs == 0)
6429 for (np = node; np != NULL; np = np->wn_sibling)
6430 {
6431 if (np->wn_child != NULL)
6432 deref_wordnode(spin, np->wn_child);
6433 free_wordnode(spin, np);
6434 }
6435}
6436
6437/*
6438 * Free a wordnode_T for re-use later.
6439 * Only the "wn_child" field becomes invalid.
6440 */
6441 static void
6442free_wordnode(spin, n)
6443 spellinfo_T *spin;
6444 wordnode_T *n;
6445{
6446 n->wn_child = spin->si_first_free;
6447 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006448 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006449}
6450
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006451/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006452 * Compress a tree: find tails that are identical and can be shared.
6453 */
6454 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006455wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006456 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006457 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006458{
6459 hashtab_T ht;
6460 int n;
6461 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006462 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006463
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006464 /* Skip the root itself, it's not actually used. The first sibling is the
6465 * start of the tree. */
6466 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006467 {
6468 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006469 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006470
6471#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00006472 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006473#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00006474 {
6475 if (!spin->si_verbose)
6476 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006477 if (tot > 1000000)
6478 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006479 else if (tot == 0)
6480 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006481 else
6482 perc = (tot - n) * 100 / tot;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006483 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006484 n, tot, perc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006485 if (p_verbose > 2)
6486 verbose_leave();
6487 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006488#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006489 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006490#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006491 hash_clear(&ht);
6492 }
6493}
6494
6495/*
6496 * Compress a node, its siblings and its children, depth first.
6497 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006498 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006499 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006500node_compress(spin, node, ht, tot)
6501 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006502 wordnode_T *node;
6503 hashtab_T *ht;
6504 int *tot; /* total count of nodes before compressing,
6505 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006506{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006507 wordnode_T *np;
6508 wordnode_T *tp;
6509 wordnode_T *child;
6510 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006511 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006512 int len = 0;
6513 unsigned nr, n;
6514 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006515
Bram Moolenaar51485f02005-06-04 21:55:20 +00006516 /*
6517 * Go through the list of siblings. Compress each child and then try
6518 * finding an identical child to replace it.
6519 * Note that with "child" we mean not just the node that is pointed to,
6520 * but the whole list of siblings, of which the node is the first.
6521 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006522 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006523 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006524 ++len;
6525 if ((child = np->wn_child) != NULL)
6526 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006527 /* Compress the child. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006528 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006529
6530 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006531 hash = hash_hash(child->wn_u1.hashkey);
6532 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006533 tp = NULL;
6534 if (!HASHITEM_EMPTY(hi))
6535 {
6536 /* There are children with an identical hash value. Now check
6537 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006538 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006539 if (node_equal(child, tp))
6540 {
6541 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006542 * current one. This means the current child and all
6543 * its siblings is unlinked from the tree. */
6544 ++tp->wn_refs;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006545 deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006546 np->wn_child = tp;
6547 ++compressed;
6548 break;
6549 }
6550 if (tp == NULL)
6551 {
6552 /* No other child with this hash value equals the child of
6553 * the node, add it to the linked list after the first
6554 * item. */
6555 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006556 child->wn_u2.next = tp->wn_u2.next;
6557 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006558 }
6559 }
6560 else
6561 /* No other child has this hash value, add it to the
6562 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006563 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006564 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006565 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006566 *tot += len;
6567
6568 /*
6569 * Make a hash key for the node and its siblings, so that we can quickly
6570 * find a lookalike node. This must be done after compressing the sibling
6571 * list, otherwise the hash key would become invalid by the compression.
6572 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006573 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006574 nr = 0;
6575 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006576 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006577 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006578 /* end node: use wn_flags, wn_region and wn_affixID */
6579 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006580 else
6581 /* byte node: use the byte value and the child pointer */
6582 n = np->wn_byte + ((long_u)np->wn_child << 8);
6583 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006584 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006585
6586 /* Avoid NUL bytes, it terminates the hash key. */
6587 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006588 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006589 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006590 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006591 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006592 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006593 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006594 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
6595 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006596
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006597 /* Check for CTRL-C pressed now and then. */
6598 fast_breakcheck();
6599
Bram Moolenaar51485f02005-06-04 21:55:20 +00006600 return compressed;
6601}
6602
6603/*
6604 * Return TRUE when two nodes have identical siblings and children.
6605 */
6606 static int
6607node_equal(n1, n2)
6608 wordnode_T *n1;
6609 wordnode_T *n2;
6610{
6611 wordnode_T *p1;
6612 wordnode_T *p2;
6613
6614 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
6615 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
6616 if (p1->wn_byte != p2->wn_byte
6617 || (p1->wn_byte == NUL
6618 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006619 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006620 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006621 : (p1->wn_child != p2->wn_child)))
6622 break;
6623
6624 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006625}
6626
6627/*
6628 * Write a number to file "fd", MSB first, in "len" bytes.
6629 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006630 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006631put_bytes(fd, nr, len)
6632 FILE *fd;
6633 long_u nr;
6634 int len;
6635{
6636 int i;
6637
6638 for (i = len - 1; i >= 0; --i)
6639 putc((int)(nr >> (i * 8)), fd);
6640}
6641
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006642static int
6643#ifdef __BORLANDC__
6644_RTLENTRYF
6645#endif
6646rep_compare __ARGS((const void *s1, const void *s2));
6647
6648/*
6649 * Function given to qsort() to sort the REP items on "from" string.
6650 */
6651 static int
6652#ifdef __BORLANDC__
6653_RTLENTRYF
6654#endif
6655rep_compare(s1, s2)
6656 const void *s1;
6657 const void *s2;
6658{
6659 fromto_T *p1 = (fromto_T *)s1;
6660 fromto_T *p2 = (fromto_T *)s2;
6661
6662 return STRCMP(p1->ft_from, p2->ft_from);
6663}
6664
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006665/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00006666 * Write the Vim .spl file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006667 */
6668 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006669write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006670 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006671 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006672{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006673 FILE *fd;
6674 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006675 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006676 wordnode_T *tree;
6677 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006678 int i;
6679 int l;
6680 garray_T *gap;
6681 fromto_T *ftp;
6682 char_u *p;
6683 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006684
Bram Moolenaarb765d632005-06-07 21:00:02 +00006685 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006686 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006687 {
6688 EMSG2(_(e_notopen), fname);
6689 return;
6690 }
6691
Bram Moolenaar5195e452005-08-19 20:32:47 +00006692 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006693 /* <fileID> */
6694 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
6695 EMSG(_(e_write));
Bram Moolenaar5195e452005-08-19 20:32:47 +00006696 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006697
Bram Moolenaar5195e452005-08-19 20:32:47 +00006698 /*
6699 * <SECTIONS>: <section> ... <sectionend>
6700 */
6701
6702 /* SN_REGION: <regionname> ...
6703 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006704 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006705 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006706 putc(SN_REGION, fd); /* <sectionID> */
6707 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6708 l = spin->si_region_count * 2;
6709 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6710 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
6711 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006712 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006713 }
6714 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006715 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006716
Bram Moolenaar5195e452005-08-19 20:32:47 +00006717 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
6718 *
6719 * The table with character flags and the table for case folding.
6720 * This makes sure the same characters are recognized as word characters
6721 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006722 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006723 * 'encoding'.
6724 * Also skip this for an .add.spl file, the main spell file must contain
6725 * the table (avoids that it conflicts). File is shorter too.
6726 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006727 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006728 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006729 char_u folchars[128 * 8];
6730 int flags;
6731
Bram Moolenaard12a1322005-08-21 22:08:24 +00006732 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006733 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6734
6735 /* Form the <folchars> string first, we need to know its length. */
6736 l = 0;
6737 for (i = 128; i < 256; ++i)
6738 {
6739#ifdef FEAT_MBYTE
6740 if (has_mbyte)
6741 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
6742 else
6743#endif
6744 folchars[l++] = spelltab.st_fold[i];
6745 }
6746 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
6747
6748 fputc(128, fd); /* <charflagslen> */
6749 for (i = 128; i < 256; ++i)
6750 {
6751 flags = 0;
6752 if (spelltab.st_isw[i])
6753 flags |= CF_WORD;
6754 if (spelltab.st_isu[i])
6755 flags |= CF_UPPER;
6756 fputc(flags, fd); /* <charflags> */
6757 }
6758
6759 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
6760 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006761 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006762
Bram Moolenaar5195e452005-08-19 20:32:47 +00006763 /* SN_MIDWORD: <midword> */
6764 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006765 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006766 putc(SN_MIDWORD, fd); /* <sectionID> */
6767 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6768
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006769 i = STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006770 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006771 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
6772 }
6773
Bram Moolenaar5195e452005-08-19 20:32:47 +00006774 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
6775 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006776 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006777 putc(SN_PREFCOND, fd); /* <sectionID> */
6778 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6779
6780 l = write_spell_prefcond(NULL, &spin->si_prefcond);
6781 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6782
6783 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006784 }
6785
Bram Moolenaar5195e452005-08-19 20:32:47 +00006786 /* SN_REP: <repcount> <rep> ...
6787 * SN_SAL: <salflags> <salcount> <sal> ... */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006788
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006789 /* Sort the REP items. */
6790 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
6791 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006792
Bram Moolenaar5195e452005-08-19 20:32:47 +00006793 /* round 1: SN_REP section
6794 * round 2: SN_SAL section (unless SN_SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006795 for (round = 1; round <= 2; ++round)
6796 {
6797 if (round == 1)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006798 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006799 gap = &spin->si_rep;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006800 putc(SN_REP, fd); /* <sectionID> */
6801 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006802 else
6803 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006804 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
6805 /* using SN_SOFO section instead of SN_SAL */
6806 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006807 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006808 putc(SN_SAL, fd); /* <sectionID> */
6809 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006810
Bram Moolenaar5195e452005-08-19 20:32:47 +00006811 /* This is for making suggestions, section is not required. */
6812 putc(0, fd); /* <sectionflags> */
6813
6814 /* Compute the length of what follows. */
6815 l = 2; /* count <repcount> or <salcount> */
6816 for (i = 0; i < gap->ga_len; ++i)
6817 {
6818 ftp = &((fromto_T *)gap->ga_data)[i];
6819 l += 1 + STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
6820 l += 1 + STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
6821 }
6822 if (round == 2)
6823 ++l; /* count <salflags> */
6824 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6825
6826 if (round == 2)
6827 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006828 i = 0;
6829 if (spin->si_followup)
6830 i |= SAL_F0LLOWUP;
6831 if (spin->si_collapse)
6832 i |= SAL_COLLAPSE;
6833 if (spin->si_rem_accents)
6834 i |= SAL_REM_ACCENTS;
6835 putc(i, fd); /* <salflags> */
6836 }
6837
6838 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
6839 for (i = 0; i < gap->ga_len; ++i)
6840 {
6841 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
6842 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
6843 ftp = &((fromto_T *)gap->ga_data)[i];
6844 for (rr = 1; rr <= 2; ++rr)
6845 {
6846 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
6847 l = STRLEN(p);
6848 putc(l, fd);
6849 fwrite(p, l, (size_t)1, fd);
6850 }
6851 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00006852
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006853 }
6854
Bram Moolenaar5195e452005-08-19 20:32:47 +00006855 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
6856 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006857 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
6858 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006859 putc(SN_SOFO, fd); /* <sectionID> */
6860 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006861
6862 l = STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006863 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
6864 /* <sectionlen> */
6865
6866 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
6867 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006868
6869 l = STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006870 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
6871 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006872 }
6873
Bram Moolenaar5195e452005-08-19 20:32:47 +00006874 /* SN_MAP: <mapstr>
6875 * This is for making suggestions, section is not required. */
6876 if (spin->si_map.ga_len > 0)
6877 {
6878 putc(SN_MAP, fd); /* <sectionID> */
6879 putc(0, fd); /* <sectionflags> */
6880 l = spin->si_map.ga_len;
6881 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6882 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
6883 /* <mapstr> */
6884 }
6885
6886 /* SN_COMPOUND: compound info.
6887 * We don't mark it required, when not supported all compound words will
6888 * be bad words. */
6889 if (spin->si_compflags != NULL)
6890 {
6891 putc(SN_COMPOUND, fd); /* <sectionID> */
6892 putc(0, fd); /* <sectionflags> */
6893
6894 l = STRLEN(spin->si_compflags);
6895 put_bytes(fd, (long_u)(l + 3), 4); /* <sectionlen> */
6896 putc(spin->si_compmax, fd); /* <compmax> */
6897 putc(spin->si_compminlen, fd); /* <compminlen> */
6898 putc(spin->si_compsylmax, fd); /* <compsylmax> */
6899 /* <compflags> */
6900 fwrite(spin->si_compflags, (size_t)l, (size_t)1, fd);
6901 }
6902
Bram Moolenaar78622822005-08-23 21:00:13 +00006903 /* SN_NOBREAK: NOBREAK flag */
6904 if (spin->si_nobreak)
6905 {
6906 putc(SN_NOBREAK, fd); /* <sectionID> */
6907 putc(0, fd); /* <sectionflags> */
6908
6909 /* It's empty, the precense of the section flags the feature. */
6910 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
6911 }
6912
Bram Moolenaar5195e452005-08-19 20:32:47 +00006913 /* SN_SYLLABLE: syllable info.
6914 * We don't mark it required, when not supported syllables will not be
6915 * counted. */
6916 if (spin->si_syllable != NULL)
6917 {
6918 putc(SN_SYLLABLE, fd); /* <sectionID> */
6919 putc(0, fd); /* <sectionflags> */
6920
6921 l = STRLEN(spin->si_syllable);
6922 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6923 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
6924 }
6925
6926 /* end of <SECTIONS> */
6927 putc(SN_END, fd); /* <sectionend> */
6928
Bram Moolenaar50cde822005-06-05 21:54:54 +00006929
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006930 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006931 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006932 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006933 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006934 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006935 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006936 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006937 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006938 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006939 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006940 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006941 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006942
Bram Moolenaar0c405862005-06-22 22:26:26 +00006943 /* Clear the index and wnode fields in the tree. */
6944 clear_node(tree);
6945
Bram Moolenaar51485f02005-06-04 21:55:20 +00006946 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00006947 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00006948 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006949 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006950
Bram Moolenaar51485f02005-06-04 21:55:20 +00006951 /* number of nodes in 4 bytes */
6952 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00006953 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006954
Bram Moolenaar51485f02005-06-04 21:55:20 +00006955 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006956 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006957 }
6958
Bram Moolenaar51485f02005-06-04 21:55:20 +00006959 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00006960}
6961
6962/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006963 * Clear the index and wnode fields of "node", it siblings and its
6964 * children. This is needed because they are a union with other items to save
6965 * space.
6966 */
6967 static void
6968clear_node(node)
6969 wordnode_T *node;
6970{
6971 wordnode_T *np;
6972
6973 if (node != NULL)
6974 for (np = node; np != NULL; np = np->wn_sibling)
6975 {
6976 np->wn_u1.index = 0;
6977 np->wn_u2.wnode = NULL;
6978
6979 if (np->wn_byte != NUL)
6980 clear_node(np->wn_child);
6981 }
6982}
6983
6984
6985/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006986 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006987 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00006988 * This first writes the list of possible bytes (siblings). Then for each
6989 * byte recursively write the children.
6990 *
6991 * NOTE: The code here must match the code in read_tree(), since assumptions
6992 * are made about the indexes (so that we don't have to write them in the
6993 * file).
6994 *
6995 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006996 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006997 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00006998put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006999 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007000 wordnode_T *node;
7001 int index;
7002 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007003 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007004{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007005 int newindex = index;
7006 int siblingcount = 0;
7007 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007008 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007009
Bram Moolenaar51485f02005-06-04 21:55:20 +00007010 /* If "node" is zero the tree is empty. */
7011 if (node == NULL)
7012 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007013
Bram Moolenaar51485f02005-06-04 21:55:20 +00007014 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007015 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007016
7017 /* Count the number of siblings. */
7018 for (np = node; np != NULL; np = np->wn_sibling)
7019 ++siblingcount;
7020
7021 /* Write the sibling count. */
7022 if (fd != NULL)
7023 putc(siblingcount, fd); /* <siblingcount> */
7024
7025 /* Write each sibling byte and optionally extra info. */
7026 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007027 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007028 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007029 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007030 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007031 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007032 /* For a NUL byte (end of word) write the flags etc. */
7033 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007034 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007035 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007036 * associated condition nr (stored in wn_region). The
7037 * byte value is misused to store the "rare" and "not
7038 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00007039 if (np->wn_flags == (short_u)PFX_FLAGS)
7040 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007041 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00007042 {
7043 putc(BY_FLAGS, fd); /* <byte> */
7044 putc(np->wn_flags, fd); /* <pflags> */
7045 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007046 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007047 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007048 }
7049 else
7050 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007051 /* For word trees we write the flag/region items. */
7052 flags = np->wn_flags;
7053 if (regionmask != 0 && np->wn_region != regionmask)
7054 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007055 if (np->wn_affixID != 0)
7056 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007057 if (flags == 0)
7058 {
7059 /* word without flags or region */
7060 putc(BY_NOFLAGS, fd); /* <byte> */
7061 }
7062 else
7063 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007064 if (np->wn_flags >= 0x100)
7065 {
7066 putc(BY_FLAGS2, fd); /* <byte> */
7067 putc(flags, fd); /* <flags> */
7068 putc((unsigned)flags >> 8, fd); /* <flags2> */
7069 }
7070 else
7071 {
7072 putc(BY_FLAGS, fd); /* <byte> */
7073 putc(flags, fd); /* <flags> */
7074 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007075 if (flags & WF_REGION)
7076 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007077 if (flags & WF_AFX)
7078 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007079 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007080 }
7081 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007082 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007083 else
7084 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00007085 if (np->wn_child->wn_u1.index != 0
7086 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007087 {
7088 /* The child is written elsewhere, write the reference. */
7089 if (fd != NULL)
7090 {
7091 putc(BY_INDEX, fd); /* <byte> */
7092 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007093 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007094 }
7095 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00007096 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007097 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007098 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007099
Bram Moolenaar51485f02005-06-04 21:55:20 +00007100 if (fd != NULL)
7101 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
7102 {
7103 EMSG(_(e_write));
7104 return 0;
7105 }
7106 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007107 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007108
7109 /* Space used in the array when reading: one for each sibling and one for
7110 * the count. */
7111 newindex += siblingcount + 1;
7112
7113 /* Recursively dump the children of each sibling. */
7114 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007115 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
7116 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007117 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007118
7119 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007120}
7121
7122
7123/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00007124 * ":mkspell [-ascii] outfile infile ..."
7125 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007126 */
7127 void
7128ex_mkspell(eap)
7129 exarg_T *eap;
7130{
7131 int fcount;
7132 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007133 char_u *arg = eap->arg;
7134 int ascii = FALSE;
7135
7136 if (STRNCMP(arg, "-ascii", 6) == 0)
7137 {
7138 ascii = TRUE;
7139 arg = skipwhite(arg + 6);
7140 }
7141
7142 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
7143 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
7144 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007145 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007146 FreeWild(fcount, fnames);
7147 }
7148}
7149
7150/*
7151 * Create a Vim spell file from one or more word lists.
7152 * "fnames[0]" is the output file name.
7153 * "fnames[fcount - 1]" is the last input file name.
7154 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
7155 * and ".spl" is appended to make the output file name.
7156 */
7157 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007158mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007159 int fcount;
7160 char_u **fnames;
7161 int ascii; /* -ascii argument given */
7162 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007163 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007164{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007165 char_u fname[MAXPATHL];
7166 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00007167 char_u **innames;
7168 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007169 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007170 int i;
7171 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007172 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007173 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007174 spellinfo_T spin;
7175
7176 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007177 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007178 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007179 spin.si_followup = TRUE;
7180 spin.si_rem_accents = TRUE;
7181 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
7182 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
7183 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007184 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar6de68532005-08-24 22:08:48 +00007185 spin.si_compID = 255; /* start compound ID at maximum, going down */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007186
Bram Moolenaarb765d632005-06-07 21:00:02 +00007187 /* default: fnames[0] is output file, following are input files */
7188 innames = &fnames[1];
7189 incount = fcount - 1;
7190
7191 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00007192 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007193 len = STRLEN(fnames[0]);
7194 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
7195 {
7196 /* For ":mkspell path/en.latin1.add" output file is
7197 * "path/en.latin1.add.spl". */
7198 innames = &fnames[0];
7199 incount = 1;
7200 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
7201 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007202 else if (fcount == 1)
7203 {
7204 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
7205 innames = &fnames[0];
7206 incount = 1;
7207 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
7208 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
7209 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007210 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
7211 {
7212 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007213 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007214 }
7215 else
7216 /* Name should be language, make the file name from it. */
7217 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
7218 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
7219
7220 /* Check for .ascii.spl. */
7221 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
7222 spin.si_ascii = TRUE;
7223
7224 /* Check for .add.spl. */
7225 if (strstr((char *)gettail(wfname), ".add.") != NULL)
7226 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00007227 }
7228
Bram Moolenaarb765d632005-06-07 21:00:02 +00007229 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007230 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007231 else if (vim_strchr(gettail(wfname), '_') != NULL)
7232 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00007233 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007234 EMSG(_("E754: Only up to 8 regions supported"));
7235 else
7236 {
7237 /* Check for overwriting before doing things that may take a lot of
7238 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007239 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007240 {
7241 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00007242 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007243 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007244 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007245 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007246 EMSG2(_(e_isadir2), wfname);
7247 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007248 }
7249
7250 /*
7251 * Init the aff and dic pointers.
7252 * Get the region names if there are more than 2 arguments.
7253 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007254 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007255 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007256 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007257
Bram Moolenaar3982c542005-06-08 21:56:31 +00007258 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007259 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007260 len = STRLEN(innames[i]);
7261 if (STRLEN(gettail(innames[i])) < 5
7262 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007263 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007264 EMSG2(_("E755: Invalid region in %s"), innames[i]);
7265 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007266 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00007267 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
7268 spin.si_region_name[i * 2 + 1] =
7269 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007270 }
7271 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00007272 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007273
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007274 spin.si_foldroot = wordtree_alloc(&spin);
7275 spin.si_keeproot = wordtree_alloc(&spin);
7276 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007277 if (spin.si_foldroot == NULL
7278 || spin.si_keeproot == NULL
7279 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007280 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007281 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007282 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007283 }
7284
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007285 /* When not producing a .add.spl file clear the character table when
7286 * we encounter one in the .aff file. This means we dump the current
7287 * one in the .spl file if the .aff file doesn't define one. That's
7288 * better than guessing the contents, the table will match a
7289 * previously loaded spell file. */
7290 if (!spin.si_add)
7291 spin.si_clear_chartab = TRUE;
7292
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007293 /*
7294 * Read all the .aff and .dic files.
7295 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007296 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007297 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007298 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007299 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007300 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007301 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007302
Bram Moolenaarb765d632005-06-07 21:00:02 +00007303 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007304 if (mch_stat((char *)fname, &st) >= 0)
7305 {
7306 /* Read the .aff file. Will init "spin->si_conv" based on the
7307 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007308 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007309 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007310 error = TRUE;
7311 else
7312 {
7313 /* Read the .dic file and store the words in the trees. */
7314 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00007315 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007316 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007317 error = TRUE;
7318 }
7319 }
7320 else
7321 {
7322 /* No .aff file, try reading the file as a word list. Store
7323 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007324 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007325 error = TRUE;
7326 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007327
Bram Moolenaarb765d632005-06-07 21:00:02 +00007328#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007329 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007330 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007331#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007332 }
7333
Bram Moolenaar78622822005-08-23 21:00:13 +00007334 if (spin.si_compflags != NULL && spin.si_nobreak)
7335 MSG(_("Warning: both compounding and NOBREAK specified"));
7336
Bram Moolenaar51485f02005-06-04 21:55:20 +00007337 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007338 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007339 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007340 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007341 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007342 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007343 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007344 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007345 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007346 MSG(_(msg_compressing));
Bram Moolenaarb765d632005-06-07 21:00:02 +00007347 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007348 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007349 verbose_leave();
7350 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007351 wordtree_compress(&spin, spin.si_foldroot);
7352 wordtree_compress(&spin, spin.si_keeproot);
7353 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007354 }
7355
Bram Moolenaar51485f02005-06-04 21:55:20 +00007356 if (!error)
7357 {
7358 /*
7359 * Write the info in the spell file.
7360 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007361 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007362 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007363 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007364 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007365 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007366 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007367 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007368 verbose_leave();
7369 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00007370
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007371 write_vim_spell(&spin, wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007372
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007373 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007374 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007375 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007376 verbose_enter();
7377 MSG(_("Done!"));
7378 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00007379 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007380 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007381 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007382 verbose_leave();
7383 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007384
Bram Moolenaarb765d632005-06-07 21:00:02 +00007385 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007386 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007387 }
7388
7389 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007390 ga_clear(&spin.si_rep);
7391 ga_clear(&spin.si_sal);
7392 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007393 ga_clear(&spin.si_prefcond);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007394
7395 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007396 for (i = 0; i < incount; ++i)
7397 if (afile[i] != NULL)
7398 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007399
7400 /* Free all the bits and pieces at once. */
7401 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007402 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007403}
7404
Bram Moolenaarb765d632005-06-07 21:00:02 +00007405
7406/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007407 * ":[count]spellgood {word}"
7408 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00007409 */
7410 void
7411ex_spell(eap)
7412 exarg_T *eap;
7413{
Bram Moolenaar7887d882005-07-01 22:33:52 +00007414 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007415 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007416}
7417
7418/*
7419 * Add "word[len]" to 'spellfile' as a good or bad word.
7420 */
7421 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007422spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007423 char_u *word;
7424 int len;
7425 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007426 int index; /* "zG" and "zW": zero, otherwise index in
7427 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007428{
7429 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007430 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007431 int new_spf = FALSE;
7432 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007433 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007434 char_u fnamebuf[MAXPATHL];
7435 char_u line[MAXWLEN * 2];
7436 long fpos, fpos_next = 0;
7437 int i;
7438 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007439
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007440 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007441 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007442 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00007443 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007444 int_wordlist = vim_tempname('s');
7445 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00007446 return;
7447 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007448 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007449 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007450 else
7451 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007452 /* If 'spellfile' isn't set figure out a good default value. */
7453 if (*curbuf->b_p_spf == NUL)
7454 {
7455 init_spellfile();
7456 new_spf = TRUE;
7457 }
7458
7459 if (*curbuf->b_p_spf == NUL)
7460 {
7461 EMSG(_("E764: 'spellfile' is not set"));
7462 return;
7463 }
7464
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007465 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
7466 {
7467 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
7468 if (i == index)
7469 break;
7470 if (*spf == NUL)
7471 {
7472 EMSGN(_("E765: 'spellfile' does not have %ld enties"), index);
7473 return;
7474 }
7475 }
7476
Bram Moolenaarb765d632005-06-07 21:00:02 +00007477 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007478 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007479 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
7480 buf = NULL;
7481 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00007482 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007483 EMSG(_(e_bufloaded));
7484 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007485 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007486
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007487 fname = fnamebuf;
7488 }
7489
7490 if (bad)
7491 {
7492 /* When the word also appears as good word we need to remove that one,
7493 * since its flags sort before the one with WF_BANNED. */
7494 fd = mch_fopen((char *)fname, "r");
7495 if (fd != NULL)
7496 {
7497 while (!vim_fgets(line, MAXWLEN * 2, fd))
7498 {
7499 fpos = fpos_next;
7500 fpos_next = ftell(fd);
7501 if (STRNCMP(word, line, len) == 0
7502 && (line[len] == '/' || line[len] < ' '))
7503 {
7504 /* Found duplicate word. Remove it by writing a '#' at
7505 * the start of the line. Mixing reading and writing
7506 * doesn't work for all systems, close the file first. */
7507 fclose(fd);
7508 fd = mch_fopen((char *)fname, "r+");
7509 if (fd == NULL)
7510 break;
7511 if (fseek(fd, fpos, SEEK_SET) == 0)
7512 fputc('#', fd);
7513 fseek(fd, fpos_next, SEEK_SET);
7514 }
7515 }
7516 fclose(fd);
7517 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007518 }
7519
7520 fd = mch_fopen((char *)fname, "a");
7521 if (fd == NULL && new_spf)
7522 {
7523 /* We just initialized the 'spellfile' option and can't open the file.
7524 * We may need to create the "spell" directory first. We already
7525 * checked the runtime directory is writable in init_spellfile(). */
7526 STRCPY(NameBuff, fname);
7527 *gettail_sep(NameBuff) = NUL;
7528 if (mch_stat((char *)NameBuff, &st) < 0)
7529 {
7530 /* The directory doesn't exist. Try creating it and opening the
7531 * file again. */
7532 vim_mkdir(NameBuff, 0755);
7533 fd = mch_fopen((char *)fname, "a");
7534 }
7535 }
7536
7537 if (fd == NULL)
7538 EMSG2(_(e_notopen), fname);
7539 else
7540 {
7541 if (bad)
7542 fprintf(fd, "%.*s/!\n", len, word);
7543 else
7544 fprintf(fd, "%.*s\n", len, word);
7545 fclose(fd);
7546
7547 /* Update the .add.spl file. */
7548 mkspell(1, &fname, FALSE, TRUE, TRUE);
7549
7550 /* If the .add file is edited somewhere, reload it. */
7551 if (buf != NULL)
7552 buf_reload(buf);
7553
7554 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007555 }
7556}
7557
7558/*
7559 * Initialize 'spellfile' for the current buffer.
7560 */
7561 static void
7562init_spellfile()
7563{
7564 char_u buf[MAXPATHL];
7565 int l;
7566 slang_T *sl;
7567 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007568 char_u *lend;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007569
7570 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
7571 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007572 /* Find the end of the language name. Exclude the region. */
7573 for (lend = curbuf->b_p_spl; *lend != NUL
7574 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
7575 ;
7576
7577 /* Loop over all entries in 'runtimepath'. Use the first one where we
7578 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007579 rtp = p_rtp;
7580 while (*rtp != NUL)
7581 {
7582 /* Copy the path from 'runtimepath' to buf[]. */
7583 copy_option_part(&rtp, buf, MAXPATHL, ",");
7584 if (filewritable(buf) == 2)
7585 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00007586 /* Use the first language name from 'spelllang' and the
7587 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007588 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
7589 l = STRLEN(buf);
7590 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00007591 "/spell/%.*s.%s.add",
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007592 (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00007593 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
7594 ? (char_u *)"ascii" : spell_enc());
7595 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
7596 break;
7597 }
7598 }
7599 }
7600}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007601
Bram Moolenaar51485f02005-06-04 21:55:20 +00007602
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007603/*
7604 * Init the chartab used for spelling for ASCII.
7605 * EBCDIC is not supported!
7606 */
7607 static void
7608clear_spell_chartab(sp)
7609 spelltab_T *sp;
7610{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007611 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007612
7613 /* Init everything to FALSE. */
7614 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
7615 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
7616 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007617 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007618 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007619 sp->st_upper[i] = i;
7620 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007621
7622 /* We include digits. A word shouldn't start with a digit, but handling
7623 * that is done separately. */
7624 for (i = '0'; i <= '9'; ++i)
7625 sp->st_isw[i] = TRUE;
7626 for (i = 'A'; i <= 'Z'; ++i)
7627 {
7628 sp->st_isw[i] = TRUE;
7629 sp->st_isu[i] = TRUE;
7630 sp->st_fold[i] = i + 0x20;
7631 }
7632 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007633 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007634 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007635 sp->st_upper[i] = i - 0x20;
7636 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007637}
7638
7639/*
7640 * Init the chartab used for spelling. Only depends on 'encoding'.
7641 * Called once while starting up and when 'encoding' changes.
7642 * The default is to use isalpha(), but the spell file should define the word
7643 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007644 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007645 */
7646 void
7647init_spell_chartab()
7648{
7649 int i;
7650
7651 did_set_spelltab = FALSE;
7652 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007653#ifdef FEAT_MBYTE
7654 if (enc_dbcs)
7655 {
7656 /* DBCS: assume double-wide characters are word characters. */
7657 for (i = 128; i <= 255; ++i)
7658 if (MB_BYTE2LEN(i) == 2)
7659 spelltab.st_isw[i] = TRUE;
7660 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007661 else if (enc_utf8)
7662 {
7663 for (i = 128; i < 256; ++i)
7664 {
7665 spelltab.st_isu[i] = utf_isupper(i);
7666 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
7667 spelltab.st_fold[i] = utf_fold(i);
7668 spelltab.st_upper[i] = utf_toupper(i);
7669 }
7670 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007671 else
7672#endif
7673 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007674 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007675 for (i = 128; i < 256; ++i)
7676 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007677 if (MB_ISUPPER(i))
7678 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007679 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007680 spelltab.st_isu[i] = TRUE;
7681 spelltab.st_fold[i] = MB_TOLOWER(i);
7682 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007683 else if (MB_ISLOWER(i))
7684 {
7685 spelltab.st_isw[i] = TRUE;
7686 spelltab.st_upper[i] = MB_TOUPPER(i);
7687 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007688 }
7689 }
7690}
7691
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007692/*
7693 * Set the spell character tables from strings in the affix file.
7694 */
7695 static int
7696set_spell_chartab(fol, low, upp)
7697 char_u *fol;
7698 char_u *low;
7699 char_u *upp;
7700{
7701 /* We build the new tables here first, so that we can compare with the
7702 * previous one. */
7703 spelltab_T new_st;
7704 char_u *pf = fol, *pl = low, *pu = upp;
7705 int f, l, u;
7706
7707 clear_spell_chartab(&new_st);
7708
7709 while (*pf != NUL)
7710 {
7711 if (*pl == NUL || *pu == NUL)
7712 {
7713 EMSG(_(e_affform));
7714 return FAIL;
7715 }
7716#ifdef FEAT_MBYTE
7717 f = mb_ptr2char_adv(&pf);
7718 l = mb_ptr2char_adv(&pl);
7719 u = mb_ptr2char_adv(&pu);
7720#else
7721 f = *pf++;
7722 l = *pl++;
7723 u = *pu++;
7724#endif
7725 /* Every character that appears is a word character. */
7726 if (f < 256)
7727 new_st.st_isw[f] = TRUE;
7728 if (l < 256)
7729 new_st.st_isw[l] = TRUE;
7730 if (u < 256)
7731 new_st.st_isw[u] = TRUE;
7732
7733 /* if "LOW" and "FOL" are not the same the "LOW" char needs
7734 * case-folding */
7735 if (l < 256 && l != f)
7736 {
7737 if (f >= 256)
7738 {
7739 EMSG(_(e_affrange));
7740 return FAIL;
7741 }
7742 new_st.st_fold[l] = f;
7743 }
7744
7745 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007746 * case-folding, it's upper case and the "UPP" is the upper case of
7747 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007748 if (u < 256 && u != f)
7749 {
7750 if (f >= 256)
7751 {
7752 EMSG(_(e_affrange));
7753 return FAIL;
7754 }
7755 new_st.st_fold[u] = f;
7756 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007757 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007758 }
7759 }
7760
7761 if (*pl != NUL || *pu != NUL)
7762 {
7763 EMSG(_(e_affform));
7764 return FAIL;
7765 }
7766
7767 return set_spell_finish(&new_st);
7768}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007769
7770/*
7771 * Set the spell character tables from strings in the .spl file.
7772 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007773 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007774set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007775 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007776 int cnt; /* length of "flags" */
7777 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007778{
7779 /* We build the new tables here first, so that we can compare with the
7780 * previous one. */
7781 spelltab_T new_st;
7782 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007783 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007784 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007785
7786 clear_spell_chartab(&new_st);
7787
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007788 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007789 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007790 if (i < cnt)
7791 {
7792 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
7793 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
7794 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007795
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007796 if (*p != NUL)
7797 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007798#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007799 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007800#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007801 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007802#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007803 new_st.st_fold[i + 128] = c;
7804 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
7805 new_st.st_upper[c] = i + 128;
7806 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007807 }
7808
Bram Moolenaar5195e452005-08-19 20:32:47 +00007809 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007810}
7811
7812 static int
7813set_spell_finish(new_st)
7814 spelltab_T *new_st;
7815{
7816 int i;
7817
7818 if (did_set_spelltab)
7819 {
7820 /* check that it's the same table */
7821 for (i = 0; i < 256; ++i)
7822 {
7823 if (spelltab.st_isw[i] != new_st->st_isw[i]
7824 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007825 || spelltab.st_fold[i] != new_st->st_fold[i]
7826 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007827 {
7828 EMSG(_("E763: Word characters differ between spell files"));
7829 return FAIL;
7830 }
7831 }
7832 }
7833 else
7834 {
7835 /* copy the new spelltab into the one being used */
7836 spelltab = *new_st;
7837 did_set_spelltab = TRUE;
7838 }
7839
7840 return OK;
7841}
7842
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007843/*
Bram Moolenaarea408852005-06-25 22:49:46 +00007844 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007845 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00007846 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007847 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00007848 */
7849 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007850spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00007851 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007852 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00007853{
Bram Moolenaarea408852005-06-25 22:49:46 +00007854#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007855 char_u *s;
7856 int l;
7857 int c;
7858
7859 if (has_mbyte)
7860 {
7861 l = MB_BYTE2LEN(*p);
7862 s = p;
7863 if (l == 1)
7864 {
7865 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007866 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007867 {
7868 s = p + 1; /* skip a mid-word character */
7869 l = MB_BYTE2LEN(*s);
7870 }
7871 }
7872 else
7873 {
7874 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007875 if (c < 256 ? buf->b_spell_ismw[c]
7876 : (buf->b_spell_ismw_mb != NULL
7877 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007878 {
7879 s = p + l;
7880 l = MB_BYTE2LEN(*s);
7881 }
7882 }
7883
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007884 c = mb_ptr2char(s);
7885 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007886 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007887 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007888 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007889#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007890
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007891 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
7892}
7893
7894/*
7895 * Return TRUE if "p" points to a word character.
7896 * Unlike spell_iswordp() this doesn't check for "midword" characters.
7897 */
7898 static int
7899spell_iswordp_nmw(p)
7900 char_u *p;
7901{
7902#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007903 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007904
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007905 if (has_mbyte)
7906 {
7907 c = mb_ptr2char(p);
7908 if (c > 255)
7909 return mb_get_class(p) >= 2;
7910 return spelltab.st_isw[c];
7911 }
7912#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007913 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00007914}
7915
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007916#ifdef FEAT_MBYTE
7917/*
7918 * Return TRUE if "p" points to a word character.
7919 * Wide version of spell_iswordp().
7920 */
7921 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007922spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007923 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007924 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007925{
7926 int *s;
7927
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007928 if (*p < 256 ? buf->b_spell_ismw[*p]
7929 : (buf->b_spell_ismw_mb != NULL
7930 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007931 s = p + 1;
7932 else
7933 s = p;
7934
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007935 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007936 {
7937 if (enc_utf8)
7938 return utf_class(*s) >= 2;
7939 if (enc_dbcs)
7940 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
7941 return 0;
7942 }
7943 return spelltab.st_isw[*s];
7944}
7945#endif
7946
Bram Moolenaarea408852005-06-25 22:49:46 +00007947/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007948 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00007949 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007950 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007951 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007952write_spell_prefcond(fd, gap)
7953 FILE *fd;
7954 garray_T *gap;
7955{
7956 int i;
7957 char_u *p;
7958 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007959 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007960
Bram Moolenaar5195e452005-08-19 20:32:47 +00007961 if (fd != NULL)
7962 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
7963
7964 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007965
7966 for (i = 0; i < gap->ga_len; ++i)
7967 {
7968 /* <prefcond> : <condlen> <condstr> */
7969 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00007970 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007971 {
7972 len = STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007973 if (fd != NULL)
7974 {
7975 fputc(len, fd);
7976 fwrite(p, (size_t)len, (size_t)1, fd);
7977 }
7978 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007979 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007980 else if (fd != NULL)
7981 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007982 }
7983
Bram Moolenaar5195e452005-08-19 20:32:47 +00007984 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007985}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007986
7987/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007988 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
7989 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007990 * When using a multi-byte 'encoding' the length may change!
7991 * Returns FAIL when something wrong.
7992 */
7993 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007994spell_casefold(str, len, buf, buflen)
7995 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007996 int len;
7997 char_u *buf;
7998 int buflen;
7999{
8000 int i;
8001
8002 if (len >= buflen)
8003 {
8004 buf[0] = NUL;
8005 return FAIL; /* result will not fit */
8006 }
8007
8008#ifdef FEAT_MBYTE
8009 if (has_mbyte)
8010 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008011 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008012 char_u *p;
8013 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008014
8015 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008016 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008017 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008018 if (outi + MB_MAXBYTES > buflen)
8019 {
8020 buf[outi] = NUL;
8021 return FAIL;
8022 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008023 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008024 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008025 }
8026 buf[outi] = NUL;
8027 }
8028 else
8029#endif
8030 {
8031 /* Be quick for non-multibyte encodings. */
8032 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008033 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008034 buf[i] = NUL;
8035 }
8036
8037 return OK;
8038}
8039
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008040#define SPS_BEST 1
8041#define SPS_FAST 2
8042#define SPS_DOUBLE 4
8043
8044static int sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008045static int sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008046
8047/*
8048 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00008049 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008050 */
8051 int
8052spell_check_sps()
8053{
8054 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008055 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008056 char_u buf[MAXPATHL];
8057 int f;
8058
8059 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008060 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008061
8062 for (p = p_sps; *p != NUL; )
8063 {
8064 copy_option_part(&p, buf, MAXPATHL, ",");
8065
8066 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008067 if (VIM_ISDIGIT(*buf))
8068 {
8069 s = buf;
8070 sps_limit = getdigits(&s);
8071 if (*s != NUL && !VIM_ISDIGIT(*s))
8072 f = -1;
8073 }
8074 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008075 f = SPS_BEST;
8076 else if (STRCMP(buf, "fast") == 0)
8077 f = SPS_FAST;
8078 else if (STRCMP(buf, "double") == 0)
8079 f = SPS_DOUBLE;
8080 else if (STRNCMP(buf, "expr:", 5) != 0
8081 && STRNCMP(buf, "file:", 5) != 0)
8082 f = -1;
8083
8084 if (f == -1 || (sps_flags != 0 && f != 0))
8085 {
8086 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008087 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008088 return FAIL;
8089 }
8090 if (f != 0)
8091 sps_flags = f;
8092 }
8093
8094 if (sps_flags == 0)
8095 sps_flags = SPS_BEST;
8096
8097 return OK;
8098}
8099
8100/* Remember what "z?" replaced. */
8101static char_u *repl_from = NULL;
8102static char_u *repl_to = NULL;
8103
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008104/*
8105 * "z?": Find badly spelled word under or after the cursor.
8106 * Give suggestions for the properly spelled word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00008107 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008108 */
8109 void
Bram Moolenaard12a1322005-08-21 22:08:24 +00008110spell_suggest(count)
8111 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008112{
8113 char_u *line;
8114 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008115 char_u wcopy[MAXWLEN + 2];
8116 char_u *p;
8117 int i;
8118 int c;
8119 suginfo_T sug;
8120 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008121 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008122 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008123 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00008124 int selected = count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008125
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008126 /* Find the start of the badly spelled word. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00008127 if (spell_move_to(FORWARD, TRUE, TRUE) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00008128 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008129 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008130 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
8131 return;
8132
8133 /* No bad word or it starts after the cursor: use the word under the
8134 * cursor. */
8135 curwin->w_cursor = prev_cursor;
8136 line = ml_get_curline();
8137 p = line + curwin->w_cursor.col;
8138 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008139 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00008140 mb_ptr_back(line, p);
8141 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008142 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00008143 mb_ptr_adv(p);
8144
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008145 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008146 {
8147 beep_flush();
8148 return;
8149 }
8150 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008151 }
8152
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008153 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008154
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008155 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008156 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008157
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008158 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008159
Bram Moolenaar5195e452005-08-19 20:32:47 +00008160 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
8161 * 'spellsuggest', whatever is smaller. */
8162 if (sps_limit > (int)Rows - 2)
8163 limit = (int)Rows - 2;
8164 else
8165 limit = sps_limit;
8166 spell_find_suggest(line + curwin->w_cursor.col, &sug, limit,
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008167 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008168
8169 if (sug.su_ga.ga_len == 0)
8170 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00008171 else if (count > 0)
8172 {
8173 if (count > sug.su_ga.ga_len)
8174 smsg((char_u *)_("Sorry, only %ld suggestions"),
8175 (long)sug.su_ga.ga_len);
8176 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008177 else
8178 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008179 vim_free(repl_from);
8180 repl_from = NULL;
8181 vim_free(repl_to);
8182 repl_to = NULL;
8183
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008184#ifdef FEAT_RIGHTLEFT
8185 /* When 'rightleft' is set the list is drawn right-left. */
8186 cmdmsg_rl = curwin->w_p_rl;
8187 if (cmdmsg_rl)
8188 msg_col = Columns - 1;
8189#endif
8190
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008191 /* List the suggestions. */
8192 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008193 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008194 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
8195 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008196#ifdef FEAT_RIGHTLEFT
8197 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
8198 {
8199 /* And now the rabbit from the high hat: Avoid showing the
8200 * untranslated message rightleft. */
8201 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
8202 sug.su_badlen, sug.su_badptr);
8203 }
8204#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008205 msg_puts(IObuff);
8206 msg_clr_eos();
8207 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00008208
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008209 msg_scroll = TRUE;
8210 for (i = 0; i < sug.su_ga.ga_len; ++i)
8211 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008212 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008213
8214 /* The suggested word may replace only part of the bad word, add
8215 * the not replaced part. */
8216 STRCPY(wcopy, stp->st_word);
8217 if (sug.su_badlen > stp->st_orglen)
8218 vim_strncpy(wcopy + STRLEN(wcopy),
8219 sug.su_badptr + stp->st_orglen,
8220 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008221 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
8222#ifdef FEAT_RIGHTLEFT
8223 if (cmdmsg_rl)
8224 rl_mirror(IObuff);
8225#endif
8226 msg_puts(IObuff);
8227
8228 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008229 msg_puts(IObuff);
8230
8231 /* The word may replace more than "su_badlen". */
8232 if (sug.su_badlen < stp->st_orglen)
8233 {
8234 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
8235 stp->st_orglen, sug.su_badptr);
8236 msg_puts(IObuff);
8237 }
8238
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008239 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008240 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008241 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008242 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008243 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008244 stp->st_salscore ? "s " : "",
8245 stp->st_score, stp->st_altscore);
8246 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008247 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00008248 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008249#ifdef FEAT_RIGHTLEFT
8250 if (cmdmsg_rl)
8251 /* Mirror the numbers, but keep the leading space. */
8252 rl_mirror(IObuff + 1);
8253#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00008254 msg_advance(30);
8255 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008256 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008257 msg_putchar('\n');
8258 }
8259
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008260#ifdef FEAT_RIGHTLEFT
8261 cmdmsg_rl = FALSE;
8262 msg_col = 0;
8263#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008264 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00008265 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008266 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00008267 selected -= lines_left;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008268 }
8269
Bram Moolenaard12a1322005-08-21 22:08:24 +00008270 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
8271 {
8272 /* Save the from and to text for :spellrepall. */
8273 stp = &SUG(sug.su_ga, selected - 1);
8274 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
8275 repl_to = vim_strsave(stp->st_word);
8276
8277 /* Replace the word. */
8278 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
8279 if (p != NULL)
8280 {
8281 c = sug.su_badptr - line;
8282 mch_memmove(p, line, c);
8283 STRCPY(p + c, stp->st_word);
8284 STRCAT(p, sug.su_badptr + stp->st_orglen);
8285 ml_replace(curwin->w_cursor.lnum, p, FALSE);
8286 curwin->w_cursor.col = c;
8287 changed_bytes(curwin->w_cursor.lnum, c);
8288
8289 /* For redo we use a change-word command. */
8290 ResetRedobuff();
8291 AppendToRedobuff((char_u *)"ciw");
8292 AppendToRedobuff(stp->st_word);
8293 AppendCharToRedobuff(ESC);
8294 }
8295 }
8296 else
8297 curwin->w_cursor = prev_cursor;
8298
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008299 spell_find_cleanup(&sug);
8300}
8301
8302/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008303 * Check if the word at line "lnum" column "col" is required to start with a
8304 * capital. This uses 'spellcapcheck' of the current buffer.
8305 */
8306 static int
8307check_need_cap(lnum, col)
8308 linenr_T lnum;
8309 colnr_T col;
8310{
8311 int need_cap = FALSE;
8312 char_u *line;
8313 char_u *line_copy = NULL;
8314 char_u *p;
8315 colnr_T endcol;
8316 regmatch_T regmatch;
8317
8318 if (curbuf->b_cap_prog == NULL)
8319 return FALSE;
8320
8321 line = ml_get_curline();
8322 endcol = 0;
8323 if ((int)(skipwhite(line) - line) >= (int)col)
8324 {
8325 /* At start of line, check if previous line is empty or sentence
8326 * ends there. */
8327 if (lnum == 1)
8328 need_cap = TRUE;
8329 else
8330 {
8331 line = ml_get(lnum - 1);
8332 if (*skipwhite(line) == NUL)
8333 need_cap = TRUE;
8334 else
8335 {
8336 /* Append a space in place of the line break. */
8337 line_copy = concat_str(line, (char_u *)" ");
8338 line = line_copy;
8339 endcol = STRLEN(line);
8340 }
8341 }
8342 }
8343 else
8344 endcol = col;
8345
8346 if (endcol > 0)
8347 {
8348 /* Check if sentence ends before the bad word. */
8349 regmatch.regprog = curbuf->b_cap_prog;
8350 regmatch.rm_ic = FALSE;
8351 p = line + endcol;
8352 for (;;)
8353 {
8354 mb_ptr_back(line, p);
8355 if (p == line || spell_iswordp_nmw(p))
8356 break;
8357 if (vim_regexec(&regmatch, p, 0)
8358 && regmatch.endp[0] == line + endcol)
8359 {
8360 need_cap = TRUE;
8361 break;
8362 }
8363 }
8364 }
8365
8366 vim_free(line_copy);
8367
8368 return need_cap;
8369}
8370
8371
8372/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008373 * ":spellrepall"
8374 */
8375/*ARGSUSED*/
8376 void
8377ex_spellrepall(eap)
8378 exarg_T *eap;
8379{
8380 pos_T pos = curwin->w_cursor;
8381 char_u *frompat;
8382 int addlen;
8383 char_u *line;
8384 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008385 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008386 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008387
8388 if (repl_from == NULL || repl_to == NULL)
8389 {
8390 EMSG(_("E752: No previous spell replacement"));
8391 return;
8392 }
8393 addlen = STRLEN(repl_to) - STRLEN(repl_from);
8394
8395 frompat = alloc(STRLEN(repl_from) + 7);
8396 if (frompat == NULL)
8397 return;
8398 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
8399 p_ws = FALSE;
8400
Bram Moolenaar5195e452005-08-19 20:32:47 +00008401 sub_nsubs = 0;
8402 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008403 curwin->w_cursor.lnum = 0;
8404 while (!got_int)
8405 {
8406 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
8407 || u_save_cursor() == FAIL)
8408 break;
8409
8410 /* Only replace when the right word isn't there yet. This happens
8411 * when changing "etc" to "etc.". */
8412 line = ml_get_curline();
8413 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
8414 repl_to, STRLEN(repl_to)) != 0)
8415 {
8416 p = alloc(STRLEN(line) + addlen + 1);
8417 if (p == NULL)
8418 break;
8419 mch_memmove(p, line, curwin->w_cursor.col);
8420 STRCPY(p + curwin->w_cursor.col, repl_to);
8421 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
8422 ml_replace(curwin->w_cursor.lnum, p, FALSE);
8423 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008424
8425 if (curwin->w_cursor.lnum != prev_lnum)
8426 {
8427 ++sub_nlines;
8428 prev_lnum = curwin->w_cursor.lnum;
8429 }
8430 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008431 }
8432 curwin->w_cursor.col += STRLEN(repl_to);
8433 }
8434
8435 p_ws = save_ws;
8436 curwin->w_cursor = pos;
8437 vim_free(frompat);
8438
Bram Moolenaar5195e452005-08-19 20:32:47 +00008439 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008440 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008441 else
8442 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008443}
8444
8445/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008446 * Find spell suggestions for "word". Return them in the growarray "*gap" as
8447 * a list of allocated strings.
8448 */
8449 void
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008450spell_suggest_list(gap, word, maxcount, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008451 garray_T *gap;
8452 char_u *word;
8453 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008454 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008455{
8456 suginfo_T sug;
8457 int i;
8458 suggest_T *stp;
8459 char_u *wcopy;
8460
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008461 spell_find_suggest(word, &sug, maxcount, FALSE, need_cap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008462
8463 /* Make room in "gap". */
8464 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
8465 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
8466 return;
8467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008468 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008469 {
8470 stp = &SUG(sug.su_ga, i);
8471
8472 /* The suggested word may replace only part of "word", add the not
8473 * replaced part. */
8474 wcopy = alloc(STRLEN(stp->st_word)
8475 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
8476 if (wcopy == NULL)
8477 break;
8478 STRCPY(wcopy, stp->st_word);
8479 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
8480 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
8481 }
8482
8483 spell_find_cleanup(&sug);
8484}
8485
8486/*
8487 * Find spell suggestions for the word at the start of "badptr".
8488 * Return the suggestions in "su->su_ga".
8489 * The maximum number of suggestions is "maxcount".
8490 * Note: does use info for the current window.
8491 * This is based on the mechanisms of Aspell, but completely reimplemented.
8492 */
8493 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008494spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008495 char_u *badptr;
8496 suginfo_T *su;
8497 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00008498 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008499 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008500{
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008501 int attr = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008502 char_u buf[MAXPATHL];
8503 char_u *p;
8504 int do_combine = FALSE;
8505 char_u *sps_copy;
8506#ifdef FEAT_EVAL
8507 static int expr_busy = FALSE;
8508#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008509 int c;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008510
8511 /*
8512 * Set the info in "*su".
8513 */
8514 vim_memset(su, 0, sizeof(suginfo_T));
8515 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
8516 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008517 if (*badptr == NUL)
8518 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008519 hash_init(&su->su_banned);
8520
8521 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008522 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008523 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008524 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008525
8526 if (su->su_badlen >= MAXWLEN)
8527 su->su_badlen = MAXWLEN - 1; /* just in case */
8528 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
8529 (void)spell_casefold(su->su_badptr, su->su_badlen,
8530 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008531 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008532 su->su_badflags = badword_captype(su->su_badptr,
8533 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008534 if (need_cap)
8535 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008536
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008537 /* If the word is not capitalised and spell_check() doesn't consider the
8538 * word to be bad then it might need to be capitalised. Add a suggestion
8539 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008540 c = PTR2CHAR(su->su_badptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008541 if (!SPELL_ISUPPER(c) && attr == 0)
8542 {
8543 make_case_word(su->su_badword, buf, WF_ONECAP);
8544 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
8545 0, TRUE);
8546 }
8547
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008548 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00008549 if (banbadword)
8550 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008551
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008552 /* Make a copy of 'spellsuggest', because the expression may change it. */
8553 sps_copy = vim_strsave(p_sps);
8554 if (sps_copy == NULL)
8555 return;
8556
8557 /* Loop over the items in 'spellsuggest'. */
8558 for (p = sps_copy; *p != NUL; )
8559 {
8560 copy_option_part(&p, buf, MAXPATHL, ",");
8561
8562 if (STRNCMP(buf, "expr:", 5) == 0)
8563 {
8564#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008565 /* Evaluate an expression. Skip this when called recursively,
8566 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008567 if (!expr_busy)
8568 {
8569 expr_busy = TRUE;
8570 spell_suggest_expr(su, buf + 5);
8571 expr_busy = FALSE;
8572 }
8573#endif
8574 }
8575 else if (STRNCMP(buf, "file:", 5) == 0)
8576 /* Use list of suggestions in a file. */
8577 spell_suggest_file(su, buf + 5);
8578 else
8579 {
8580 /* Use internal method. */
8581 spell_suggest_intern(su);
8582 if (sps_flags & SPS_DOUBLE)
8583 do_combine = TRUE;
8584 }
8585 }
8586
8587 vim_free(sps_copy);
8588
8589 if (do_combine)
8590 /* Combine the two list of suggestions. This must be done last,
8591 * because sorting changes the order again. */
8592 score_combine(su);
8593}
8594
8595#ifdef FEAT_EVAL
8596/*
8597 * Find suggestions by evaluating expression "expr".
8598 */
8599 static void
8600spell_suggest_expr(su, expr)
8601 suginfo_T *su;
8602 char_u *expr;
8603{
8604 list_T *list;
8605 listitem_T *li;
8606 int score;
8607 char_u *p;
8608
8609 /* The work is split up in a few parts to avoid having to export
8610 * suginfo_T.
8611 * First evaluate the expression and get the resulting list. */
8612 list = eval_spell_expr(su->su_badword, expr);
8613 if (list != NULL)
8614 {
8615 /* Loop over the items in the list. */
8616 for (li = list->lv_first; li != NULL; li = li->li_next)
8617 if (li->li_tv.v_type == VAR_LIST)
8618 {
8619 /* Get the word and the score from the items. */
8620 score = get_spellword(li->li_tv.vval.v_list, &p);
8621 if (score >= 0)
8622 add_suggestion(su, &su->su_ga, p,
8623 su->su_badlen, score, 0, TRUE);
8624 }
8625 list_unref(list);
8626 }
8627
8628 /* Sort the suggestions and truncate at "maxcount". */
8629 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8630}
8631#endif
8632
8633/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008634 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008635 */
8636 static void
8637spell_suggest_file(su, fname)
8638 suginfo_T *su;
8639 char_u *fname;
8640{
8641 FILE *fd;
8642 char_u line[MAXWLEN * 2];
8643 char_u *p;
8644 int len;
8645 char_u cword[MAXWLEN];
8646
8647 /* Open the file. */
8648 fd = mch_fopen((char *)fname, "r");
8649 if (fd == NULL)
8650 {
8651 EMSG2(_(e_notopen), fname);
8652 return;
8653 }
8654
8655 /* Read it line by line. */
8656 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
8657 {
8658 line_breakcheck();
8659
8660 p = vim_strchr(line, '/');
8661 if (p == NULL)
8662 continue; /* No Tab found, just skip the line. */
8663 *p++ = NUL;
8664 if (STRICMP(su->su_badword, line) == 0)
8665 {
8666 /* Match! Isolate the good word, until CR or NL. */
8667 for (len = 0; p[len] >= ' '; ++len)
8668 ;
8669 p[len] = NUL;
8670
8671 /* If the suggestion doesn't have specific case duplicate the case
8672 * of the bad word. */
8673 if (captype(p, NULL) == 0)
8674 {
8675 make_case_word(p, cword, su->su_badflags);
8676 p = cword;
8677 }
8678
8679 add_suggestion(su, &su->su_ga, p, su->su_badlen,
8680 SCORE_FILE, 0, TRUE);
8681 }
8682 }
8683
8684 fclose(fd);
8685
8686 /* Sort the suggestions and truncate at "maxcount". */
8687 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8688}
8689
8690/*
8691 * Find suggestions for the internal method indicated by "sps_flags".
8692 */
8693 static void
8694spell_suggest_intern(su)
8695 suginfo_T *su;
8696{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008697 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008698 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008699 *
8700 * Set a maximum score to limit the combination of operations that is
8701 * tried.
8702 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008703 suggest_try_special(su);
8704
8705 /*
8706 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
8707 * from the .aff file and inserting a space (split the word).
8708 */
8709 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008710
8711 /* For the resulting top-scorers compute the sound-a-like score. */
8712 if (sps_flags & SPS_DOUBLE)
8713 score_comp_sal(su);
8714
8715 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008716 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008717 *
8718 * Only do this when we don't have a lot of suggestions yet, because it's
8719 * very slow and often doesn't find new suggestions.
8720 */
8721 if ((sps_flags & SPS_DOUBLE)
8722 || (!(sps_flags & SPS_FAST)
8723 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
8724 {
8725 /* Allow a higher score now. */
8726 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008727 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008728 }
8729
8730 /* When CTRL-C was hit while searching do show the results. */
8731 ui_breakcheck();
8732 if (got_int)
8733 {
8734 (void)vgetc();
8735 got_int = FALSE;
8736 }
8737
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008738 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008739 {
8740 if (sps_flags & SPS_BEST)
8741 /* Adjust the word score for how it sounds like. */
8742 rescore_suggestions(su);
8743
8744 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008745 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008746 }
8747}
8748
8749/*
8750 * Free the info put in "*su" by spell_find_suggest().
8751 */
8752 static void
8753spell_find_cleanup(su)
8754 suginfo_T *su;
8755{
8756 int i;
8757
8758 /* Free the suggestions. */
8759 for (i = 0; i < su->su_ga.ga_len; ++i)
8760 vim_free(SUG(su->su_ga, i).st_word);
8761 ga_clear(&su->su_ga);
8762 for (i = 0; i < su->su_sga.ga_len; ++i)
8763 vim_free(SUG(su->su_sga, i).st_word);
8764 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008765
8766 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008767 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008768}
8769
8770/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008771 * Make a copy of "word", with the first letter upper or lower cased, to
8772 * "wcopy[MAXWLEN]". "word" must not be empty.
8773 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008774 */
8775 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008776onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008777 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008778 char_u *wcopy;
8779 int upper; /* TRUE: first letter made upper case */
8780{
8781 char_u *p;
8782 int c;
8783 int l;
8784
8785 p = word;
8786#ifdef FEAT_MBYTE
8787 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008788 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008789 else
8790#endif
8791 c = *p++;
8792 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008793 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008794 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008795 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008796#ifdef FEAT_MBYTE
8797 if (has_mbyte)
8798 l = mb_char2bytes(c, wcopy);
8799 else
8800#endif
8801 {
8802 l = 1;
8803 wcopy[0] = c;
8804 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008805 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008806}
8807
8808/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008809 * Make a copy of "word" with all the letters upper cased into
8810 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008811 */
8812 static void
8813allcap_copy(word, wcopy)
8814 char_u *word;
8815 char_u *wcopy;
8816{
8817 char_u *s;
8818 char_u *d;
8819 int c;
8820
8821 d = wcopy;
8822 for (s = word; *s != NUL; )
8823 {
8824#ifdef FEAT_MBYTE
8825 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008826 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008827 else
8828#endif
8829 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00008830
8831#ifdef FEAT_MBYTE
8832 /* We only change ß to SS when we are certain latin1 is used. It
8833 * would cause weird errors in other 8-bit encodings. */
8834 if (enc_latin1like && c == 0xdf)
8835 {
8836 c = 'S';
8837 if (d - wcopy >= MAXWLEN - 1)
8838 break;
8839 *d++ = c;
8840 }
8841 else
8842#endif
8843 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008844
8845#ifdef FEAT_MBYTE
8846 if (has_mbyte)
8847 {
8848 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
8849 break;
8850 d += mb_char2bytes(c, d);
8851 }
8852 else
8853#endif
8854 {
8855 if (d - wcopy >= MAXWLEN - 1)
8856 break;
8857 *d++ = c;
8858 }
8859 }
8860 *d = NUL;
8861}
8862
8863/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008864 * Try finding suggestions by recognizing specific situations.
8865 */
8866 static void
8867suggest_try_special(su)
8868 suginfo_T *su;
8869{
8870 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008871 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008872 int c;
8873 char_u word[MAXWLEN];
8874
8875 /*
8876 * Recognize a word that is repeated: "the the".
8877 */
8878 p = skiptowhite(su->su_fbadword);
8879 len = p - su->su_fbadword;
8880 p = skipwhite(p);
8881 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
8882 {
8883 /* Include badflags: if the badword is onecap or allcap
8884 * use that for the goodword too: "The the" -> "The". */
8885 c = su->su_fbadword[len];
8886 su->su_fbadword[len] = NUL;
8887 make_case_word(su->su_fbadword, word, su->su_badflags);
8888 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008889 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008890 }
8891}
8892
8893/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008894 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00008895 *
8896 * This uses a state machine. At each node in the tree we try various
8897 * operations. When trying if an operation work "depth" is increased and the
8898 * stack[] is used to store info. This allows combinations, thus insert one
8899 * character, replace one and delete another. The number of changes is
8900 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaard12a1322005-08-21 22:08:24 +00008901 *
8902 * After implementing this I noticed an article by Kemal Oflazer that
8903 * describes something similar: "Error-tolerant Finite State Recognition with
8904 * Applications to Morphological Analysis and Spelling Correction" (1996).
8905 * The implementation in the article is simplified and requires a stack of
8906 * unknown depth. The implementation here only needs a stack depth of the
8907 * length of the word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008908 */
8909 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00008910suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008911 suginfo_T *su;
8912{
8913 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
8914 char_u tword[MAXWLEN]; /* good word collected so far */
8915 trystate_T stack[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00008916 char_u preword[MAXWLEN * 3]; /* word found with proper case;
8917 * concatanation of prefix compound
8918 * words and split word. NUL terminated
8919 * when going deeper but not when coming
8920 * back. */
8921 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008922 trystate_T *sp;
8923 int newscore;
8924 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008925 char_u *byts, *fbyts, *pbyts;
8926 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008927 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00008928 int c, c2, c3;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008929 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008930 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008931 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008932 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008933 int len;
8934 char_u *p;
8935 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00008936 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008937 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008938 slang_T *slang;
8939 int fword_ends;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008940
8941 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00008942 * to find matches (esp. REP items). Append some more text, changing
8943 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008944 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008945 n = STRLEN(fword);
8946 p = su->su_badptr + su->su_badlen;
8947 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008948
8949 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8950 lp->lp_slang != NULL; ++lp)
8951 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008952 slang = lp->lp_slang;
8953
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008954 /*
8955 * Go through the whole case-fold tree, try changes at each node.
8956 * "tword[]" contains the word collected from nodes in the tree.
8957 * "fword[]" the word we are trying to match with (initially the bad
8958 * word).
8959 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008960 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008961 sp = &stack[0];
Bram Moolenaar5195e452005-08-19 20:32:47 +00008962 vim_memset(sp, 0, sizeof(trystate_T));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008963 sp->ts_curi = 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008964
Bram Moolenaarea424162005-06-16 21:51:00 +00008965 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008966 * When there are postponed prefixes we need to use these first. At
8967 * the end of the prefix we continue in the case-fold tree.
8968 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008969 fbyts = slang->sl_fbyts;
8970 fidxs = slang->sl_fidxs;
8971 pbyts = slang->sl_pbyts;
8972 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008973 if (pbyts != NULL)
8974 {
8975 byts = pbyts;
8976 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008977 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008978 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
8979 }
8980 else
8981 {
8982 byts = fbyts;
8983 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00008984 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +00008985 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008986 }
8987
8988 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00008989 * Loop to find all suggestions. At each round we either:
8990 * - For the current state try one operation, advance "ts_curi",
8991 * increase "depth".
8992 * - When a state is done go to the next, set "ts_state".
8993 * - When all states are tried decrease "depth".
8994 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008995 while (depth >= 0 && !got_int)
8996 {
8997 sp = &stack[depth];
8998 switch (sp->ts_state)
8999 {
9000 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009001 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009002 /*
9003 * Start of node: Deal with NUL bytes, which means
9004 * tword[] may end here.
9005 */
9006 arridx = sp->ts_arridx; /* current node in the tree */
9007 len = byts[arridx]; /* bytes in this node */
9008 arridx += sp->ts_curi; /* index of current byte */
9009
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009010 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009011 {
9012 /* Skip over the NUL bytes, we use them later. */
9013 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
9014 ;
9015 sp->ts_curi += n;
9016
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009017 /* Always past NUL bytes now. */
9018 n = (int)sp->ts_state;
9019 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00009020 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009021
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009022 /* At end of a prefix or at start of prefixtree: check for
9023 * following word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009024 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009025 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00009026 /* Set su->su_badflags to the caps type at this
9027 * position. Use the caps type until here for the
9028 * prefix itself. */
9029#ifdef FEAT_MBYTE
9030 if (has_mbyte)
9031 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
9032 else
9033#endif
9034 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009035 flags = badword_captype(su->su_badptr,
9036 su->su_badptr + n);
9037 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00009038 su->su_badptr + su->su_badlen);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009039 ++depth;
9040 stack[depth] = stack[depth - 1];
9041 sp = &stack[depth];
9042 sp->ts_prefixdepth = depth - 1;
9043 byts = fbyts;
9044 idxs = fidxs;
9045 sp->ts_state = STATE_START;
9046 sp->ts_curi = 1; /* start just after length byte */
9047 sp->ts_arridx = 0;
9048
Bram Moolenaar53805d12005-08-01 07:08:33 +00009049 /* Move the prefix to preword[] with the right case
9050 * and make find_keepcap_word() works. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00009051 tword[sp->ts_twordlen] = NUL;
9052 make_case_word(tword + sp->ts_splitoff,
9053 preword + sp->ts_prewordlen,
9054 flags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009055 sp->ts_prewordlen = STRLEN(preword);
Bram Moolenaard12a1322005-08-21 22:08:24 +00009056 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009057 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009058 break;
9059 }
9060
Bram Moolenaar0c405862005-06-22 22:26:26 +00009061 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009062 {
9063 /* Past bytes in node and/or past NUL bytes. */
9064 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00009065 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009066 break;
9067 }
9068
9069 /*
9070 * End of word in tree.
9071 */
9072 ++sp->ts_curi; /* eat one NUL byte */
9073
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009074 flags = (int)idxs[arridx];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009075 fword_ends = (fword[sp->ts_fidx] == NUL
9076 || !spell_iswordp(fword + sp->ts_fidx, curbuf));
9077 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009078
Bram Moolenaard12a1322005-08-21 22:08:24 +00009079 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
9080 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009081 {
9082 /* There was a prefix before the word. Check that the
9083 * prefix can be used with this word. */
9084 /* Count the length of the NULs in the prefix. If there
9085 * are none this must be the first try without a prefix.
9086 */
9087 n = stack[sp->ts_prefixdepth].ts_arridx;
9088 len = pbyts[n++];
9089 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
9090 ;
9091 if (c > 0)
9092 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009093 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00009094 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009095 if (c == 0)
9096 break;
9097
9098 /* Use the WF_RARE flag for a rare prefix. */
9099 if (c & WF_RAREPFX)
9100 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009101
9102 /* Tricky: when checking for both prefix and
9103 * compounding we run into the prefix flag first.
9104 * Remember that it's OK, so that we accept the prefix
9105 * when arriving at a compound flag. */
9106 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009107 }
9108 }
9109
Bram Moolenaard12a1322005-08-21 22:08:24 +00009110 if (sp->ts_complen > sp->ts_compsplit)
9111 {
Bram Moolenaar78622822005-08-23 21:00:13 +00009112 if (slang->sl_nobreak)
9113 {
9114 /* There was a word before this word. When there was
9115 * no change in this word (it was correct) add the
9116 * first word as a suggestion. If this word was
9117 * corrected too, we need to check if a correct word
9118 * follows. */
9119 if (sp->ts_fidx - sp->ts_splitfidx
9120 == sp->ts_twordlen - sp->ts_splitoff
9121 && STRNCMP(fword + sp->ts_splitfidx,
9122 tword + sp->ts_splitoff,
9123 sp->ts_fidx - sp->ts_splitfidx) == 0)
9124 {
9125 preword[sp->ts_prewordlen] = NUL;
9126 add_suggestion(su, &su->su_ga, preword,
9127 sp->ts_splitfidx - repextra,
9128 sp->ts_score, 0, FALSE);
9129 break;
9130 }
9131 }
9132 else
9133 {
9134 /* There was a compound word before this word. If
9135 * this word does not support compounding then give up
9136 * (splitting is tried for the word without compound
9137 * flag). */
9138 if (((unsigned)flags >> 24) == 0
9139 || sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaard12a1322005-08-21 22:08:24 +00009140 < slang->sl_compminlen)
Bram Moolenaar78622822005-08-23 21:00:13 +00009141 break;
9142 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
9143 compflags[sp->ts_complen + 1] = NUL;
9144 vim_strncpy(preword + sp->ts_prewordlen,
9145 tword + sp->ts_splitoff,
9146 sp->ts_twordlen - sp->ts_splitoff);
9147 p = preword;
9148 while (*skiptowhite(p) != NUL)
9149 p = skipwhite(skiptowhite(p));
9150 if (fword_ends && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00009151 compflags + sp->ts_compsplit))
Bram Moolenaar78622822005-08-23 21:00:13 +00009152 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00009153
Bram Moolenaar78622822005-08-23 21:00:13 +00009154 /* Get pointer to last char of previous word. */
9155 p = preword + sp->ts_prewordlen;
9156 mb_ptr_back(preword, p);
9157 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00009158 }
Bram Moolenaare52325c2005-08-22 22:54:29 +00009159 else
9160 p = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009161
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009162 /*
9163 * Form the word with proper case in preword.
9164 * If there is a word from a previous split, append.
9165 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009166 if (flags & WF_KEEPCAP)
9167 /* Must find the word in the keep-case tree. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009168 find_keepcap_word(slang, tword + sp->ts_splitoff,
9169 preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009170 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00009171 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009172 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00009173 * use that for the goodword too. But if the badword is
9174 * allcap and it's only one char long use onecap. */
9175 c = su->su_badflags;
9176 if ((c & WF_ALLCAP)
9177#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009178 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
Bram Moolenaar0c405862005-06-22 22:26:26 +00009179#else
9180 && su->su_badlen == 1
9181#endif
9182 )
9183 c = WF_ONECAP;
Bram Moolenaare52325c2005-08-22 22:54:29 +00009184 c |= flags;
9185
9186 /* When appending a compound word after a word character
9187 * don't use Onecap. */
9188 if (p != NULL && spell_iswordp_nmw(p))
9189 c &= ~WF_ONECAP;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009190 make_case_word(tword + sp->ts_splitoff,
Bram Moolenaare52325c2005-08-22 22:54:29 +00009191 preword + sp->ts_prewordlen, c);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009192 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009193
9194 /* Don't use a banned word. It may appear again as a good
9195 * word, thus remember it. */
9196 if (flags & WF_BANNED)
9197 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00009198 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009199 break;
9200 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009201 if (was_banned(su, preword + sp->ts_prewordlen)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009202 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009203 break;
9204
9205 newscore = 0;
9206 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009207 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009208 newscore += SCORE_REGION;
9209 if (flags & WF_RARE)
9210 newscore += SCORE_RARE;
9211
Bram Moolenaar0c405862005-06-22 22:26:26 +00009212 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00009213 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009214 newscore += SCORE_ICASE;
9215
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009216 if (fword_ends && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009217 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009218 /* The badword also ends: add suggestions. Give a penalty
9219 * when changing non-word char to word char, e.g., "thes,"
9220 * -> "these". */
9221 p = fword + sp->ts_fidx;
9222#ifdef FEAT_MBYTE
9223 if (has_mbyte)
9224 mb_ptr_back(fword, p);
9225 else
9226#endif
9227 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009228 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009229 {
9230 p = preword + STRLEN(preword);
9231#ifdef FEAT_MBYTE
9232 if (has_mbyte)
9233 mb_ptr_back(preword, p);
9234 else
9235#endif
9236 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009237 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009238 newscore += SCORE_NONWORD;
9239 }
9240
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009241 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00009242 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009243 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009244 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009245 else if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +00009246#ifdef FEAT_MBYTE
9247 /* Don't split halfway a character. */
9248 && (!has_mbyte || sp->ts_tcharlen == 0)
9249#endif
9250 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009251 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009252 int try_compound;
9253
9254 /* Get here in two situations:
9255 * 1. The word in the tree ends but the badword continues:
9256 * If the word allows compounding try that. Otherwise
9257 * try a split by inserting a space. For both check
9258 * that a valid words starts at fword[sp->ts_fidx].
Bram Moolenaar78622822005-08-23 21:00:13 +00009259 * For NOBREAK do like compounding to be able to check
9260 * if the next word is valid.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009261 * 2. The badword does end, but it was due to a change
9262 * (e.g., a swap). No need to split, but do check that
9263 * the following word is valid.
9264 */
Bram Moolenaard12a1322005-08-21 22:08:24 +00009265 try_compound = FALSE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009266 if (!fword_ends
Bram Moolenaar6de68532005-08-24 22:08:48 +00009267 && slang->sl_compprog != NULL
Bram Moolenaar5195e452005-08-19 20:32:47 +00009268 && ((unsigned)flags >> 24) != 0
9269 && sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009270 >= slang->sl_compminlen
Bram Moolenaare52325c2005-08-22 22:54:29 +00009271 && (slang->sl_compsylmax < MAXWLEN
9272 || sp->ts_complen + 1 - sp->ts_compsplit
9273 < slang->sl_compmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00009274 && (byte_in_str(sp->ts_complen == sp->ts_compsplit
Bram Moolenaard12a1322005-08-21 22:08:24 +00009275 ? slang->sl_compstartflags
9276 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00009277 ((unsigned)flags >> 24))))
Bram Moolenaar5195e452005-08-19 20:32:47 +00009278 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009279 try_compound = TRUE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009280 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
9281 compflags[sp->ts_complen + 1] = NUL;
9282 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00009283
Bram Moolenaar78622822005-08-23 21:00:13 +00009284 /* For NOBREAK we never try splitting, it won't make any
9285 * word valid. */
9286 if (slang->sl_nobreak)
9287 try_compound = TRUE;
9288
Bram Moolenaard12a1322005-08-21 22:08:24 +00009289 /* If we could add a compound word, and it's also possible
9290 * to split at this point, do the split first and set
9291 * TSF_DIDSPLIT to avoid doing it again. */
Bram Moolenaar78622822005-08-23 21:00:13 +00009292 else if (!fword_ends
Bram Moolenaard12a1322005-08-21 22:08:24 +00009293 && try_compound
9294 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009295 {
9296 try_compound = FALSE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009297 sp->ts_flags |= TSF_DIDSPLIT;
9298 --sp->ts_curi; /* do the same NUL again */
9299 compflags[sp->ts_complen] = NUL;
9300 }
9301 else
9302 sp->ts_flags &= ~TSF_DIDSPLIT;
9303
9304 if (!try_compound && !fword_ends)
9305 {
9306 /* If we're going to split need to check that the
9307 * words so far are valid for compounding. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00009308 p = preword;
9309 while (*skiptowhite(p) != NUL)
9310 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +00009311 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +00009312 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00009313 compflags + sp->ts_compsplit))
9314 break;
9315 newscore += SCORE_SPLIT;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009316 }
9317
9318 if (try_deeper(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009319 {
9320 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009321 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009322 sp->ts_state = STATE_SPLITUNDO;
9323
9324 ++depth;
9325 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009326
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009327 /* Append a space to preword when splitting. */
9328 if (!try_compound && !fword_ends)
9329 STRCAT(preword, " ");
Bram Moolenaar5195e452005-08-19 20:32:47 +00009330 sp->ts_prewordlen = STRLEN(preword);
9331 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +00009332 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009333
9334 /* If the badword has a non-word character at this
9335 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009336 * non-word character with a space. Always skip a
9337 * character when the word ends. */
9338 if ((!try_compound
9339 && !spell_iswordp_nmw(fword + sp->ts_fidx))
9340 || fword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009341 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009342 int l;
9343
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009344#ifdef FEAT_MBYTE
9345 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009346 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009347 else
9348#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009349 l = 1;
9350 if (fword_ends)
9351 {
9352 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009353 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009354 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009355 sp->ts_prewordlen += l;
9356 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009357 }
9358 else
9359 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
9360 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009361 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00009362
Bram Moolenaard12a1322005-08-21 22:08:24 +00009363 /* When compounding include compound flag in
9364 * compflags[] (already set above). When splitting we
9365 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009366 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +00009367 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009368 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00009369 sp->ts_compsplit = sp->ts_complen;
9370 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009371
Bram Moolenaar53805d12005-08-01 07:08:33 +00009372 /* set su->su_badflags to the caps type at this
9373 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009374#ifdef FEAT_MBYTE
9375 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00009376 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009377 else
9378#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00009379 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009380 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00009381 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009382
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009383 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009384 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009385
9386 /* If there are postponed prefixes, try these too. */
9387 if (pbyts != NULL)
9388 {
9389 byts = pbyts;
9390 idxs = pidxs;
9391 sp->ts_prefixdepth = PFD_PREFIXTREE;
9392 sp->ts_state = STATE_NOPREFIX;
9393 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009394 }
9395 }
9396 break;
9397
9398 case STATE_SPLITUNDO:
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009399 /* Undo the changes done for word split or compound word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009400 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009401
9402 /* Continue looking for NUL bytes. */
9403 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009404
9405 /* In case we went into the prefix tree. */
9406 byts = fbyts;
9407 idxs = fidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009408 break;
9409
9410 case STATE_ENDNUL:
9411 /* Past the NUL bytes in the node. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00009412 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009413 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009414 {
9415 /* The badword ends, can't use the bytes in this node. */
9416 sp->ts_state = STATE_DEL;
9417 break;
9418 }
9419 sp->ts_state = STATE_PLAIN;
9420 /*FALLTHROUGH*/
9421
9422 case STATE_PLAIN:
9423 /*
9424 * Go over all possible bytes at this node, add each to
9425 * tword[] and use child node. "ts_curi" is the index.
9426 */
9427 arridx = sp->ts_arridx;
9428 if (sp->ts_curi > byts[arridx])
9429 {
9430 /* Done all bytes at this node, do next state. When still
9431 * at already changed bytes skip the other tricks. */
9432 if (sp->ts_fidx >= sp->ts_fidxtry)
9433 sp->ts_state = STATE_DEL;
9434 else
9435 sp->ts_state = STATE_FINAL;
9436 }
9437 else
9438 {
9439 arridx += sp->ts_curi++;
9440 c = byts[arridx];
9441
9442 /* Normal byte, go one level deeper. If it's not equal to
9443 * the byte in the bad word adjust the score. But don't
9444 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00009445 if (c == fword[sp->ts_fidx]
9446#ifdef FEAT_MBYTE
9447 || (sp->ts_tcharlen > 0
9448 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009449#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00009450 )
9451 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009452 else
9453 newscore = SCORE_SUBST;
9454 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
9455 && try_deeper(su, stack, depth, newscore))
9456 {
9457 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009458 sp = &stack[depth];
9459 ++sp->ts_fidx;
9460 tword[sp->ts_twordlen++] = c;
9461 sp->ts_arridx = idxs[arridx];
9462#ifdef FEAT_MBYTE
9463 if (newscore == SCORE_SUBST)
9464 sp->ts_isdiff = DIFF_YES;
9465 if (has_mbyte)
9466 {
9467 /* Multi-byte characters are a bit complicated to
9468 * handle: They differ when any of the bytes
9469 * differ and then their length may also differ. */
9470 if (sp->ts_tcharlen == 0)
9471 {
9472 /* First byte. */
9473 sp->ts_tcharidx = 0;
9474 sp->ts_tcharlen = MB_BYTE2LEN(c);
9475 sp->ts_fcharstart = sp->ts_fidx - 1;
9476 sp->ts_isdiff = (newscore != 0)
9477 ? DIFF_YES : DIFF_NONE;
9478 }
9479 else if (sp->ts_isdiff == DIFF_INSERT)
9480 /* When inserting trail bytes don't advance in
9481 * the bad word. */
9482 --sp->ts_fidx;
9483 if (++sp->ts_tcharidx == sp->ts_tcharlen)
9484 {
9485 /* Last byte of character. */
9486 if (sp->ts_isdiff == DIFF_YES)
9487 {
9488 /* Correct ts_fidx for the byte length of
9489 * the character (we didn't check that
9490 * before). */
9491 sp->ts_fidx = sp->ts_fcharstart
9492 + MB_BYTE2LEN(
9493 fword[sp->ts_fcharstart]);
9494
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00009495 /* For changing a composing character
9496 * adjust the score from SCORE_SUBST to
9497 * SCORE_SUBCOMP. */
9498 if (enc_utf8
9499 && utf_iscomposing(
9500 mb_ptr2char(tword
9501 + sp->ts_twordlen
9502 - sp->ts_tcharlen))
9503 && utf_iscomposing(
9504 mb_ptr2char(fword
9505 + sp->ts_fcharstart)))
9506 sp->ts_score -=
9507 SCORE_SUBST - SCORE_SUBCOMP;
9508
Bram Moolenaarea424162005-06-16 21:51:00 +00009509 /* For a similar character adjust score
9510 * from SCORE_SUBST to SCORE_SIMILAR. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009511 else if (slang->sl_has_map
9512 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00009513 mb_ptr2char(tword
9514 + sp->ts_twordlen
9515 - sp->ts_tcharlen),
9516 mb_ptr2char(fword
9517 + sp->ts_fcharstart)))
9518 sp->ts_score -=
9519 SCORE_SUBST - SCORE_SIMILAR;
9520 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009521 else if (sp->ts_isdiff == DIFF_INSERT
9522 && sp->ts_twordlen > sp->ts_tcharlen)
9523 {
Bram Moolenaarea408852005-06-25 22:49:46 +00009524 p = tword + sp->ts_twordlen
9525 - sp->ts_tcharlen;
9526 c = mb_ptr2char(p);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009527 if (enc_utf8 && utf_iscomposing(c))
9528 {
9529 /* Inserting a composing char doesn't
9530 * count that much. */
Bram Moolenaarea408852005-06-25 22:49:46 +00009531 sp->ts_score -= SCORE_INS
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009532 - SCORE_INSCOMP;
9533 }
9534 else
9535 {
9536 /* If the previous character was the
9537 * same, thus doubling a character,
9538 * give a bonus to the score. */
9539 mb_ptr_back(tword, p);
9540 if (c == mb_ptr2char(p))
9541 sp->ts_score -= SCORE_INS
Bram Moolenaarea408852005-06-25 22:49:46 +00009542 - SCORE_INSDUP;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009543 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009544 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009545
9546 /* Starting a new char, reset the length. */
9547 sp->ts_tcharlen = 0;
9548 }
9549 }
9550 else
9551#endif
9552 {
9553 /* If we found a similar char adjust the score.
9554 * We do this after calling try_deeper() because
9555 * it's slow. */
9556 if (newscore != 0
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009557 && slang->sl_has_map
9558 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00009559 c, fword[sp->ts_fidx - 1]))
9560 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
9561 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009562 }
9563 }
9564 break;
9565
9566 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00009567#ifdef FEAT_MBYTE
9568 /* When past the first byte of a multi-byte char don't try
9569 * delete/insert/swap a character. */
9570 if (has_mbyte && sp->ts_tcharlen > 0)
9571 {
9572 sp->ts_state = STATE_FINAL;
9573 break;
9574 }
9575#endif
9576 /*
9577 * Try skipping one character in the bad word (delete it).
9578 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009579 sp->ts_state = STATE_INS;
9580 sp->ts_curi = 1;
9581 if (fword[sp->ts_fidx] != NUL
9582 && try_deeper(su, stack, depth, SCORE_DEL))
9583 {
9584 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00009585
9586 /* Advance over the character in fword[]. Give a bonus to
9587 * the score if the same character is following "nn" ->
9588 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00009589#ifdef FEAT_MBYTE
9590 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00009591 {
9592 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00009593 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00009594 if (enc_utf8 && utf_iscomposing(c))
9595 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
9596 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
Bram Moolenaarea408852005-06-25 22:49:46 +00009597 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
9598 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009599 else
9600#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00009601 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009602 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00009603 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
9604 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
9605 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009606 break;
9607 }
9608 /*FALLTHROUGH*/
9609
9610 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00009611 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009612 * node. */
9613 n = sp->ts_arridx;
9614 if (sp->ts_curi > byts[n])
9615 {
9616 /* Done all bytes at this node, do next state. */
9617 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009618 }
9619 else
9620 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009621 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009622 n += sp->ts_curi++;
9623 c = byts[n];
9624 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
9625 {
9626 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009627 sp = &stack[depth];
9628 tword[sp->ts_twordlen++] = c;
9629 sp->ts_arridx = idxs[n];
9630#ifdef FEAT_MBYTE
9631 if (has_mbyte)
9632 {
9633 fl = MB_BYTE2LEN(c);
9634 if (fl > 1)
9635 {
9636 /* There are following bytes for the same
9637 * character. We must find all bytes before
9638 * trying delete/insert/swap/etc. */
9639 sp->ts_tcharlen = fl;
9640 sp->ts_tcharidx = 1;
9641 sp->ts_isdiff = DIFF_INSERT;
9642 }
9643 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009644 else
9645 fl = 1;
9646 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00009647#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00009648 {
9649 /* If the previous character was the same, thus
9650 * doubling a character, give a bonus to the
9651 * score. */
9652 if (sp->ts_twordlen >= 2
9653 && tword[sp->ts_twordlen - 2] == c)
9654 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
9655 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009656 }
9657 }
9658 break;
9659
9660 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00009661 /*
9662 * Swap two bytes in the bad word: "12" -> "21".
9663 * We change "fword" here, it's changed back afterwards.
9664 */
9665 p = fword + sp->ts_fidx;
9666 c = *p;
9667 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009668 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009669 /* End of word, can't swap or replace. */
9670 sp->ts_state = STATE_FINAL;
9671 break;
9672 }
9673#ifdef FEAT_MBYTE
9674 if (has_mbyte)
9675 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009676 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009677 c = mb_ptr2char(p);
9678 c2 = mb_ptr2char(p + n);
9679 }
9680 else
9681#endif
9682 c2 = p[1];
9683 if (c == c2)
9684 {
9685 /* Characters are identical, swap won't do anything. */
9686 sp->ts_state = STATE_SWAP3;
9687 break;
9688 }
9689 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
9690 {
9691 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009692 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009693#ifdef FEAT_MBYTE
9694 if (has_mbyte)
9695 {
9696 fl = mb_char2len(c2);
9697 mch_memmove(p, p + n, fl);
9698 mb_char2bytes(c, p + fl);
9699 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
9700 }
9701 else
9702#endif
9703 {
9704 p[0] = c2;
9705 p[1] = c;
9706 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
9707 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009708 }
9709 else
9710 /* If this swap doesn't work then SWAP3 won't either. */
9711 sp->ts_state = STATE_REP_INI;
9712 break;
9713
Bram Moolenaarea424162005-06-16 21:51:00 +00009714 case STATE_UNSWAP:
9715 /* Undo the STATE_SWAP swap: "21" -> "12". */
9716 p = fword + sp->ts_fidx;
9717#ifdef FEAT_MBYTE
9718 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009719 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009720 n = MB_BYTE2LEN(*p);
9721 c = mb_ptr2char(p + n);
9722 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
9723 mb_char2bytes(c, p);
9724 }
9725 else
9726#endif
9727 {
9728 c = *p;
9729 *p = p[1];
9730 p[1] = c;
9731 }
9732 /*FALLTHROUGH*/
9733
9734 case STATE_SWAP3:
9735 /* Swap two bytes, skipping one: "123" -> "321". We change
9736 * "fword" here, it's changed back afterwards. */
9737 p = fword + sp->ts_fidx;
9738#ifdef FEAT_MBYTE
9739 if (has_mbyte)
9740 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009741 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009742 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009743 fl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00009744 c2 = mb_ptr2char(p + n);
9745 c3 = mb_ptr2char(p + n + fl);
9746 }
9747 else
9748#endif
9749 {
9750 c = *p;
9751 c2 = p[1];
9752 c3 = p[2];
9753 }
9754
9755 /* When characters are identical: "121" then SWAP3 result is
9756 * identical, ROT3L result is same as SWAP: "211", ROT3L
9757 * result is same as SWAP on next char: "112". Thus skip all
9758 * swapping. Also skip when c3 is NUL. */
9759 if (c == c3 || c3 == NUL)
9760 {
9761 sp->ts_state = STATE_REP_INI;
9762 break;
9763 }
9764 if (try_deeper(su, stack, depth, SCORE_SWAP3))
9765 {
9766 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009767 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009768#ifdef FEAT_MBYTE
9769 if (has_mbyte)
9770 {
9771 tl = mb_char2len(c3);
9772 mch_memmove(p, p + n + fl, tl);
9773 mb_char2bytes(c2, p + tl);
9774 mb_char2bytes(c, p + fl + tl);
9775 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
9776 }
9777 else
9778#endif
9779 {
9780 p[0] = p[2];
9781 p[2] = c;
9782 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
9783 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009784 }
9785 else
9786 sp->ts_state = STATE_REP_INI;
9787 break;
9788
Bram Moolenaarea424162005-06-16 21:51:00 +00009789 case STATE_UNSWAP3:
9790 /* Undo STATE_SWAP3: "321" -> "123" */
9791 p = fword + sp->ts_fidx;
9792#ifdef FEAT_MBYTE
9793 if (has_mbyte)
9794 {
9795 n = MB_BYTE2LEN(*p);
9796 c2 = mb_ptr2char(p + n);
9797 fl = MB_BYTE2LEN(p[n]);
9798 c = mb_ptr2char(p + n + fl);
9799 tl = MB_BYTE2LEN(p[n + fl]);
9800 mch_memmove(p + fl + tl, p, n);
9801 mb_char2bytes(c, p);
9802 mb_char2bytes(c2, p + tl);
9803 }
9804 else
9805#endif
9806 {
9807 c = *p;
9808 *p = p[2];
9809 p[2] = c;
9810 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009811
Bram Moolenaarea424162005-06-16 21:51:00 +00009812 /* Rotate three characters left: "123" -> "231". We change
9813 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009814 if (try_deeper(su, stack, depth, SCORE_SWAP3))
9815 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009816 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009817 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009818 p = fword + sp->ts_fidx;
9819#ifdef FEAT_MBYTE
9820 if (has_mbyte)
9821 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009822 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009823 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009824 fl = mb_cptr2len(p + n);
9825 fl += mb_cptr2len(p + n + fl);
Bram Moolenaarea424162005-06-16 21:51:00 +00009826 mch_memmove(p, p + n, fl);
9827 mb_char2bytes(c, p + fl);
9828 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
9829 }
9830 else
9831#endif
9832 {
9833 c = *p;
9834 *p = p[1];
9835 p[1] = p[2];
9836 p[2] = c;
9837 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
9838 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009839 }
9840 else
9841 sp->ts_state = STATE_REP_INI;
9842 break;
9843
Bram Moolenaarea424162005-06-16 21:51:00 +00009844 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00009845 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00009846 p = fword + sp->ts_fidx;
9847#ifdef FEAT_MBYTE
9848 if (has_mbyte)
9849 {
9850 n = MB_BYTE2LEN(*p);
9851 n += MB_BYTE2LEN(p[n]);
9852 c = mb_ptr2char(p + n);
9853 tl = MB_BYTE2LEN(p[n]);
9854 mch_memmove(p + tl, p, n);
9855 mb_char2bytes(c, p);
9856 }
9857 else
9858#endif
9859 {
9860 c = p[2];
9861 p[2] = p[1];
9862 p[1] = *p;
9863 *p = c;
9864 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009866 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00009867 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009868 if (try_deeper(su, stack, depth, SCORE_SWAP3))
9869 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009870 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009871 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009872 p = fword + sp->ts_fidx;
9873#ifdef FEAT_MBYTE
9874 if (has_mbyte)
9875 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009876 n = mb_cptr2len(p);
9877 n += mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00009878 c = mb_ptr2char(p + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009879 tl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00009880 mch_memmove(p + tl, p, n);
9881 mb_char2bytes(c, p);
9882 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
9883 }
9884 else
9885#endif
9886 {
9887 c = p[2];
9888 p[2] = p[1];
9889 p[1] = *p;
9890 *p = c;
9891 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
9892 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009893 }
9894 else
9895 sp->ts_state = STATE_REP_INI;
9896 break;
9897
Bram Moolenaarea424162005-06-16 21:51:00 +00009898 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00009899 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00009900 p = fword + sp->ts_fidx;
9901#ifdef FEAT_MBYTE
9902 if (has_mbyte)
9903 {
9904 c = mb_ptr2char(p);
9905 tl = MB_BYTE2LEN(*p);
9906 n = MB_BYTE2LEN(p[tl]);
9907 n += MB_BYTE2LEN(p[tl + n]);
9908 mch_memmove(p, p + tl, n);
9909 mb_char2bytes(c, p + n);
9910 }
9911 else
9912#endif
9913 {
9914 c = *p;
9915 *p = p[1];
9916 p[1] = p[2];
9917 p[2] = c;
9918 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009919 /*FALLTHROUGH*/
9920
9921 case STATE_REP_INI:
9922 /* Check if matching with REP items from the .aff file would
9923 * work. Quickly skip if there are no REP items or the score
9924 * is going to be too high anyway. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009925 gap = &slang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009926 if (gap->ga_len == 0
9927 || sp->ts_score + SCORE_REP >= su->su_maxscore)
9928 {
9929 sp->ts_state = STATE_FINAL;
9930 break;
9931 }
9932
9933 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00009934 * may match. If the index is -1 there is none. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009935 sp->ts_curi = slang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009936 if (sp->ts_curi < 0)
9937 {
9938 sp->ts_state = STATE_FINAL;
9939 break;
9940 }
9941
9942 sp->ts_state = STATE_REP;
9943 /*FALLTHROUGH*/
9944
9945 case STATE_REP:
9946 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00009947 * match replace the characters and check if the resulting
9948 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009949 p = fword + sp->ts_fidx;
9950
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009951 gap = &slang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009952 while (sp->ts_curi < gap->ga_len)
9953 {
9954 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
9955 if (*ftp->ft_from != *p)
9956 {
9957 /* past possible matching entries */
9958 sp->ts_curi = gap->ga_len;
9959 break;
9960 }
9961 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
9962 && try_deeper(su, stack, depth, SCORE_REP))
9963 {
9964 /* Need to undo this afterwards. */
9965 sp->ts_state = STATE_REP_UNDO;
9966
9967 /* Change the "from" to the "to" string. */
9968 ++depth;
9969 fl = STRLEN(ftp->ft_from);
9970 tl = STRLEN(ftp->ft_to);
9971 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00009972 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009973 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009974 repextra += tl - fl;
9975 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009976 mch_memmove(p, ftp->ft_to, tl);
9977 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00009978#ifdef FEAT_MBYTE
9979 stack[depth].ts_tcharlen = 0;
9980#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009981 break;
9982 }
9983 }
9984
9985 if (sp->ts_curi >= gap->ga_len)
9986 /* No (more) matches. */
9987 sp->ts_state = STATE_FINAL;
9988
9989 break;
9990
9991 case STATE_REP_UNDO:
9992 /* Undo a REP replacement and continue with the next one. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009993 ftp = (fromto_T *)slang->sl_rep.ga_data + sp->ts_curi - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009994 fl = STRLEN(ftp->ft_from);
9995 tl = STRLEN(ftp->ft_to);
9996 p = fword + sp->ts_fidx;
9997 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00009998 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009999 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010000 repextra -= tl - fl;
10001 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010002 mch_memmove(p, ftp->ft_from, fl);
10003 sp->ts_state = STATE_REP;
10004 break;
10005
10006 default:
10007 /* Did all possible states at this level, go up one level. */
10008 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010009
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010010 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010011 {
10012 /* Continue in or go back to the prefix tree. */
10013 byts = pbyts;
10014 idxs = pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010015 }
10016
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010017 /* Don't check for CTRL-C too often, it takes time. */
10018 line_breakcheck();
10019 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010020 }
10021 }
10022}
10023
10024/*
10025 * Try going one level deeper in the tree.
10026 */
10027 static int
10028try_deeper(su, stack, depth, score_add)
10029 suginfo_T *su;
10030 trystate_T *stack;
10031 int depth;
10032 int score_add;
10033{
10034 int newscore;
10035
10036 /* Refuse to go deeper if the scrore is getting too big. */
10037 newscore = stack[depth].ts_score + score_add;
10038 if (newscore >= su->su_maxscore)
10039 return FALSE;
10040
Bram Moolenaarea424162005-06-16 21:51:00 +000010041 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010042 stack[depth + 1].ts_state = STATE_START;
10043 stack[depth + 1].ts_score = newscore;
10044 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010045 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010046 return TRUE;
10047}
10048
Bram Moolenaar53805d12005-08-01 07:08:33 +000010049#ifdef FEAT_MBYTE
10050/*
10051 * Case-folding may change the number of bytes: Count nr of chars in
10052 * fword[flen] and return the byte length of that many chars in "word".
10053 */
10054 static int
10055nofold_len(fword, flen, word)
10056 char_u *fword;
10057 int flen;
10058 char_u *word;
10059{
10060 char_u *p;
10061 int i = 0;
10062
10063 for (p = fword; p < fword + flen; mb_ptr_adv(p))
10064 ++i;
10065 for (p = word; i > 0; mb_ptr_adv(p))
10066 --i;
10067 return (int)(p - word);
10068}
10069#endif
10070
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010071/*
10072 * "fword" is a good word with case folded. Find the matching keep-case
10073 * words and put it in "kword".
10074 * Theoretically there could be several keep-case words that result in the
10075 * same case-folded word, but we only find one...
10076 */
10077 static void
10078find_keepcap_word(slang, fword, kword)
10079 slang_T *slang;
10080 char_u *fword;
10081 char_u *kword;
10082{
10083 char_u uword[MAXWLEN]; /* "fword" in upper-case */
10084 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010085 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010086
10087 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010088 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010089 int round[MAXWLEN];
10090 int fwordidx[MAXWLEN];
10091 int uwordidx[MAXWLEN];
10092 int kwordlen[MAXWLEN];
10093
10094 int flen, ulen;
10095 int l;
10096 int len;
10097 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010098 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010099 char_u *p;
10100 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010101 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010102
10103 if (byts == NULL)
10104 {
10105 /* array is empty: "cannot happen" */
10106 *kword = NUL;
10107 return;
10108 }
10109
10110 /* Make an all-cap version of "fword". */
10111 allcap_copy(fword, uword);
10112
10113 /*
10114 * Each character needs to be tried both case-folded and upper-case.
10115 * All this gets very complicated if we keep in mind that changing case
10116 * may change the byte length of a multi-byte character...
10117 */
10118 depth = 0;
10119 arridx[0] = 0;
10120 round[0] = 0;
10121 fwordidx[0] = 0;
10122 uwordidx[0] = 0;
10123 kwordlen[0] = 0;
10124 while (depth >= 0)
10125 {
10126 if (fword[fwordidx[depth]] == NUL)
10127 {
10128 /* We are at the end of "fword". If the tree allows a word to end
10129 * here we have found a match. */
10130 if (byts[arridx[depth] + 1] == 0)
10131 {
10132 kword[kwordlen[depth]] = NUL;
10133 return;
10134 }
10135
10136 /* kword is getting too long, continue one level up */
10137 --depth;
10138 }
10139 else if (++round[depth] > 2)
10140 {
10141 /* tried both fold-case and upper-case character, continue one
10142 * level up */
10143 --depth;
10144 }
10145 else
10146 {
10147 /*
10148 * round[depth] == 1: Try using the folded-case character.
10149 * round[depth] == 2: Try using the upper-case character.
10150 */
10151#ifdef FEAT_MBYTE
10152 if (has_mbyte)
10153 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010154 flen = mb_cptr2len(fword + fwordidx[depth]);
10155 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010156 }
10157 else
10158#endif
10159 ulen = flen = 1;
10160 if (round[depth] == 1)
10161 {
10162 p = fword + fwordidx[depth];
10163 l = flen;
10164 }
10165 else
10166 {
10167 p = uword + uwordidx[depth];
10168 l = ulen;
10169 }
10170
10171 for (tryidx = arridx[depth]; l > 0; --l)
10172 {
10173 /* Perform a binary search in the list of accepted bytes. */
10174 len = byts[tryidx++];
10175 c = *p++;
10176 lo = tryidx;
10177 hi = tryidx + len - 1;
10178 while (lo < hi)
10179 {
10180 m = (lo + hi) / 2;
10181 if (byts[m] > c)
10182 hi = m - 1;
10183 else if (byts[m] < c)
10184 lo = m + 1;
10185 else
10186 {
10187 lo = hi = m;
10188 break;
10189 }
10190 }
10191
10192 /* Stop if there is no matching byte. */
10193 if (hi < lo || byts[lo] != c)
10194 break;
10195
10196 /* Continue at the child (if there is one). */
10197 tryidx = idxs[lo];
10198 }
10199
10200 if (l == 0)
10201 {
10202 /*
10203 * Found the matching char. Copy it to "kword" and go a
10204 * level deeper.
10205 */
10206 if (round[depth] == 1)
10207 {
10208 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
10209 flen);
10210 kwordlen[depth + 1] = kwordlen[depth] + flen;
10211 }
10212 else
10213 {
10214 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
10215 ulen);
10216 kwordlen[depth + 1] = kwordlen[depth] + ulen;
10217 }
10218 fwordidx[depth + 1] = fwordidx[depth] + flen;
10219 uwordidx[depth + 1] = uwordidx[depth] + ulen;
10220
10221 ++depth;
10222 arridx[depth] = tryidx;
10223 round[depth] = 0;
10224 }
10225 }
10226 }
10227
10228 /* Didn't find it: "cannot happen". */
10229 *kword = NUL;
10230}
10231
10232/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010233 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
10234 * su->su_sga.
10235 */
10236 static void
10237score_comp_sal(su)
10238 suginfo_T *su;
10239{
10240 langp_T *lp;
10241 char_u badsound[MAXWLEN];
10242 int i;
10243 suggest_T *stp;
10244 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010245 int score;
10246
10247 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
10248 return;
10249
10250 /* Use the sound-folding of the first language that supports it. */
10251 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
10252 lp->lp_slang != NULL; ++lp)
10253 if (lp->lp_slang->sl_sal.ga_len > 0)
10254 {
10255 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010256 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010257
10258 for (i = 0; i < su->su_ga.ga_len; ++i)
10259 {
10260 stp = &SUG(su->su_ga, i);
10261
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010262 /* Case-fold the suggested word, sound-fold it and compute the
10263 * sound-a-like score. */
10264 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010265 if (score < SCORE_MAXMAX)
10266 {
10267 /* Add the suggestion. */
10268 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
10269 sstp->st_word = vim_strsave(stp->st_word);
10270 if (sstp->st_word != NULL)
10271 {
10272 sstp->st_score = score;
10273 sstp->st_altscore = 0;
10274 sstp->st_orglen = stp->st_orglen;
10275 ++su->su_sga.ga_len;
10276 }
10277 }
10278 }
10279 break;
10280 }
10281}
10282
10283/*
10284 * Combine the list of suggestions in su->su_ga and su->su_sga.
10285 * They are intwined.
10286 */
10287 static void
10288score_combine(su)
10289 suginfo_T *su;
10290{
10291 int i;
10292 int j;
10293 garray_T ga;
10294 garray_T *gap;
10295 langp_T *lp;
10296 suggest_T *stp;
10297 char_u *p;
10298 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010299 int round;
10300
10301 /* Add the alternate score to su_ga. */
10302 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
10303 lp->lp_slang != NULL; ++lp)
10304 {
10305 if (lp->lp_slang->sl_sal.ga_len > 0)
10306 {
10307 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010308 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010309
10310 for (i = 0; i < su->su_ga.ga_len; ++i)
10311 {
10312 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010313 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
10314 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010315 if (stp->st_altscore == SCORE_MAXMAX)
10316 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
10317 else
10318 stp->st_score = (stp->st_score * 3
10319 + stp->st_altscore) / 4;
10320 stp->st_salscore = FALSE;
10321 }
10322 break;
10323 }
10324 }
10325
10326 /* Add the alternate score to su_sga. */
10327 for (i = 0; i < su->su_sga.ga_len; ++i)
10328 {
10329 stp = &SUG(su->su_sga, i);
10330 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
10331 if (stp->st_score == SCORE_MAXMAX)
10332 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
10333 else
10334 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
10335 stp->st_salscore = TRUE;
10336 }
10337
10338 /* Sort the suggestions and truncate at "maxcount" for both lists. */
10339 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10340 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
10341
10342 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
10343 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
10344 return;
10345
10346 stp = &SUG(ga, 0);
10347 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
10348 {
10349 /* round 1: get a suggestion from su_ga
10350 * round 2: get a suggestion from su_sga */
10351 for (round = 1; round <= 2; ++round)
10352 {
10353 gap = round == 1 ? &su->su_ga : &su->su_sga;
10354 if (i < gap->ga_len)
10355 {
10356 /* Don't add a word if it's already there. */
10357 p = SUG(*gap, i).st_word;
10358 for (j = 0; j < ga.ga_len; ++j)
10359 if (STRCMP(stp[j].st_word, p) == 0)
10360 break;
10361 if (j == ga.ga_len)
10362 stp[ga.ga_len++] = SUG(*gap, i);
10363 else
10364 vim_free(p);
10365 }
10366 }
10367 }
10368
10369 ga_clear(&su->su_ga);
10370 ga_clear(&su->su_sga);
10371
10372 /* Truncate the list to the number of suggestions that will be displayed. */
10373 if (ga.ga_len > su->su_maxcount)
10374 {
10375 for (i = su->su_maxcount; i < ga.ga_len; ++i)
10376 vim_free(stp[i].st_word);
10377 ga.ga_len = su->su_maxcount;
10378 }
10379
10380 su->su_ga = ga;
10381}
10382
10383/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010384 * For the goodword in "stp" compute the soundalike score compared to the
10385 * badword.
10386 */
10387 static int
10388stp_sal_score(stp, su, slang, badsound)
10389 suggest_T *stp;
10390 suginfo_T *su;
10391 slang_T *slang;
10392 char_u *badsound; /* sound-folded badword */
10393{
10394 char_u *p;
10395 char_u badsound2[MAXWLEN];
10396 char_u fword[MAXWLEN];
10397 char_u goodsound[MAXWLEN];
10398
10399 if (stp->st_orglen <= su->su_badlen)
10400 p = badsound;
10401 else
10402 {
10403 /* soundfold the bad word with more characters following */
10404 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
10405
10406 /* When joining two words the sound often changes a lot. E.g., "t he"
10407 * sounds like "t h" while "the" sounds like "@". Avoid that by
10408 * removing the space. Don't do it when the good word also contains a
10409 * space. */
10410 if (vim_iswhite(su->su_badptr[su->su_badlen])
10411 && *skiptowhite(stp->st_word) == NUL)
10412 for (p = fword; *(p = skiptowhite(p)) != NUL; )
10413 mch_memmove(p, p + 1, STRLEN(p));
10414
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010415 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010416 p = badsound2;
10417 }
10418
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010419 /* Sound-fold the word and compute the score for the difference. */
10420 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010421
10422 return soundalike_score(goodsound, p);
10423}
10424
10425/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010426 * Find suggestions by comparing the word in a sound-a-like form.
10427 */
10428 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000010429suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010430 suginfo_T *su;
10431{
10432 char_u salword[MAXWLEN];
10433 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010434 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010435 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010436 int curi[MAXWLEN];
10437 langp_T *lp;
10438 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010439 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010440 int depth;
10441 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010442 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010443 int round;
10444 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010445 int sound_score;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010446 int local_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010447
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010448 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010449 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
10450 lp->lp_slang != NULL; ++lp)
10451 {
10452 if (lp->lp_slang->sl_sal.ga_len > 0)
10453 {
10454 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010455 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010456
10457 /*
10458 * Go through the whole tree, soundfold each word and compare.
10459 * round 1: use the case-folded tree.
10460 * round 2: use the keep-case tree.
10461 */
10462 for (round = 1; round <= 2; ++round)
10463 {
10464 if (round == 1)
10465 {
10466 byts = lp->lp_slang->sl_fbyts;
10467 idxs = lp->lp_slang->sl_fidxs;
10468 }
10469 else
10470 {
10471 byts = lp->lp_slang->sl_kbyts;
10472 idxs = lp->lp_slang->sl_kidxs;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010473 if (byts == NULL) /* no keep-case words */
10474 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010475 }
10476
10477 depth = 0;
10478 arridx[0] = 0;
10479 curi[0] = 1;
10480 while (depth >= 0 && !got_int)
10481 {
10482 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010483 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010484 /* Done all bytes at this node, go up one level. */
10485 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010486 line_breakcheck();
10487 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010488 else
10489 {
10490 /* Do one more byte at this node. */
10491 n = arridx[depth] + curi[depth];
10492 ++curi[depth];
10493 c = byts[n];
10494 if (c == 0)
10495 {
10496 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010497 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010498 if (round == 2 || (flags & WF_KEEPCAP) == 0)
10499 {
10500 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010501 /* Sound-fold. Only in keep-case tree need to
10502 * case-fold the word. */
10503 spell_soundfold(lp->lp_slang, tword,
10504 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010505
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010506 /* Compute the edit distance between the
10507 * sound-a-like words. */
10508 sound_score = soundalike_score(salword,
10509 tsalword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010510
10511 /* Add a penalty for words in another region. */
10512 if ((flags & WF_REGION) && (((unsigned)flags
10513 >> 16) & lp->lp_region) == 0)
10514 local_score = SCORE_REGION;
10515 else
10516 local_score = 0;
10517 sound_score += local_score;
10518
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010519 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010520 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010521 char_u cword[MAXWLEN];
10522 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010523 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010524
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010525 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010526 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010527 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010528 /* Need to fix case according to
10529 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010530 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010531 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010532 }
10533 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010534 p = tword;
10535
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010536 if (sps_flags & SPS_DOUBLE)
10537 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000010538 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010539 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010540 else
10541 {
10542 /* Compute the score. */
10543 score = spell_edit_score(
Bram Moolenaar5195e452005-08-19 20:32:47 +000010544 su->su_badword, p)
10545 + local_score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010546 if (sps_flags & SPS_BEST)
10547 /* give a bonus for the good word
10548 * sounding the same as the bad
10549 * word */
10550 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000010551 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010552 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010553 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010554 else
10555 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000010556 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010557 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010558 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010559 }
10560 }
10561
10562 /* Skip over other NUL bytes. */
10563 while (byts[n + 1] == 0)
10564 {
10565 ++n;
10566 ++curi[depth];
10567 }
10568 }
10569 else
10570 {
10571 /* Normal char, go one level deeper. */
10572 tword[depth++] = c;
10573 arridx[depth] = idxs[n];
10574 curi[depth] = 1;
10575 }
10576 }
10577 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010578 }
10579 }
10580 }
10581}
10582
10583/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010584 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010585 */
10586 static void
10587make_case_word(fword, cword, flags)
10588 char_u *fword;
10589 char_u *cword;
10590 int flags;
10591{
10592 if (flags & WF_ALLCAP)
10593 /* Make it all upper-case */
10594 allcap_copy(fword, cword);
10595 else if (flags & WF_ONECAP)
10596 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010597 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010598 else
10599 /* Use goodword as-is. */
10600 STRCPY(cword, fword);
10601}
10602
Bram Moolenaarea424162005-06-16 21:51:00 +000010603/*
10604 * Use map string "map" for languages "lp".
10605 */
10606 static void
10607set_map_str(lp, map)
10608 slang_T *lp;
10609 char_u *map;
10610{
10611 char_u *p;
10612 int headc = 0;
10613 int c;
10614 int i;
10615
10616 if (*map == NUL)
10617 {
10618 lp->sl_has_map = FALSE;
10619 return;
10620 }
10621 lp->sl_has_map = TRUE;
10622
10623 /* Init the array and hash table empty. */
10624 for (i = 0; i < 256; ++i)
10625 lp->sl_map_array[i] = 0;
10626#ifdef FEAT_MBYTE
10627 hash_init(&lp->sl_map_hash);
10628#endif
10629
10630 /*
10631 * The similar characters are stored separated with slashes:
10632 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
10633 * before the same slash. For characters above 255 sl_map_hash is used.
10634 */
10635 for (p = map; *p != NUL; )
10636 {
10637#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010638 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000010639#else
10640 c = *p++;
10641#endif
10642 if (c == '/')
10643 headc = 0;
10644 else
10645 {
10646 if (headc == 0)
10647 headc = c;
10648
10649#ifdef FEAT_MBYTE
10650 /* Characters above 255 don't fit in sl_map_array[], put them in
10651 * the hash table. Each entry is the char, a NUL the headchar and
10652 * a NUL. */
10653 if (c >= 256)
10654 {
10655 int cl = mb_char2len(c);
10656 int headcl = mb_char2len(headc);
10657 char_u *b;
10658 hash_T hash;
10659 hashitem_T *hi;
10660
10661 b = alloc((unsigned)(cl + headcl + 2));
10662 if (b == NULL)
10663 return;
10664 mb_char2bytes(c, b);
10665 b[cl] = NUL;
10666 mb_char2bytes(headc, b + cl + 1);
10667 b[cl + 1 + headcl] = NUL;
10668 hash = hash_hash(b);
10669 hi = hash_lookup(&lp->sl_map_hash, b, hash);
10670 if (HASHITEM_EMPTY(hi))
10671 hash_add_item(&lp->sl_map_hash, hi, b, hash);
10672 else
10673 {
10674 /* This should have been checked when generating the .spl
10675 * file. */
10676 EMSG(_("E999: duplicate char in MAP entry"));
10677 vim_free(b);
10678 }
10679 }
10680 else
10681#endif
10682 lp->sl_map_array[c] = headc;
10683 }
10684 }
10685}
10686
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010687/*
10688 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
10689 * lines in the .aff file.
10690 */
10691 static int
10692similar_chars(slang, c1, c2)
10693 slang_T *slang;
10694 int c1;
10695 int c2;
10696{
Bram Moolenaarea424162005-06-16 21:51:00 +000010697 int m1, m2;
10698#ifdef FEAT_MBYTE
10699 char_u buf[MB_MAXBYTES];
10700 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010701
Bram Moolenaarea424162005-06-16 21:51:00 +000010702 if (c1 >= 256)
10703 {
10704 buf[mb_char2bytes(c1, buf)] = 0;
10705 hi = hash_find(&slang->sl_map_hash, buf);
10706 if (HASHITEM_EMPTY(hi))
10707 m1 = 0;
10708 else
10709 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
10710 }
10711 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010712#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000010713 m1 = slang->sl_map_array[c1];
10714 if (m1 == 0)
10715 return FALSE;
10716
10717
10718#ifdef FEAT_MBYTE
10719 if (c2 >= 256)
10720 {
10721 buf[mb_char2bytes(c2, buf)] = 0;
10722 hi = hash_find(&slang->sl_map_hash, buf);
10723 if (HASHITEM_EMPTY(hi))
10724 m2 = 0;
10725 else
10726 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
10727 }
10728 else
10729#endif
10730 m2 = slang->sl_map_array[c2];
10731
10732 return m1 == m2;
10733}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010734
10735/*
10736 * Add a suggestion to the list of suggestions.
10737 * Do not add a duplicate suggestion or suggestions with a bad score.
10738 * When "use_score" is not zero it's used, otherwise the score is computed
10739 * with spell_edit_score().
10740 */
10741 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010742add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010743 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010744 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010745 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010746 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010747 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010748 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010749 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010750{
10751 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010752 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010753 char_u *p = NULL;
10754 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010755
10756 /* Check that the word wasn't banned. */
10757 if (was_banned(su, goodword))
10758 return;
10759
Bram Moolenaar0c405862005-06-22 22:26:26 +000010760 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
10761 * Remove the common part from "goodword". */
10762 i = badlen - su->su_badlen;
10763 if (i > 0)
10764 {
10765 /* This assumes there was no case folding or it didn't change the
10766 * length... */
10767 p = goodword + STRLEN(goodword) - i;
10768 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
10769 {
10770 badlen = su->su_badlen;
10771 c = *p;
10772 *p = NUL;
10773 }
10774 else
10775 p = NULL;
10776 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010777 else if (i < 0)
10778 {
10779 /* When replacing part of the word check that we actually change
10780 * something. For "the the" a suggestion can be replacing the first
10781 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010782 if (badlen == (int)STRLEN(goodword)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010783 && STRNCMP(su->su_badword, goodword, badlen) == 0)
10784 return;
10785 }
10786
Bram Moolenaar0c405862005-06-22 22:26:26 +000010787
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010788 if (score <= su->su_maxscore)
10789 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010790 /* Check if the word is already there. Also check the length that is
10791 * being replaced "thes," -> "these" is a different suggestion from
10792 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010793 stp = &SUG(*gap, 0);
10794 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010795 if (STRCMP(stp[i].st_word, goodword) == 0
10796 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010797 {
10798 /* Found it. Remember the lowest score. */
10799 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010800 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010801 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010802 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010803 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010804 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010805 break;
10806 }
10807
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010808 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010809 {
10810 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010811 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010812 stp->st_word = vim_strsave(goodword);
10813 if (stp->st_word != NULL)
10814 {
10815 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010816 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010817 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010818 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010819 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010820
10821 /* If we have too many suggestions now, sort the list and keep
10822 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010823 if (gap->ga_len > SUG_MAX_COUNT(su))
10824 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
10825 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010826 }
10827 }
10828 }
Bram Moolenaar0c405862005-06-22 22:26:26 +000010829
10830 if (p != NULL)
10831 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010832}
10833
10834/*
10835 * Add a word to be banned.
10836 */
10837 static void
10838add_banned(su, word)
10839 suginfo_T *su;
10840 char_u *word;
10841{
10842 char_u *s = vim_strsave(word);
10843 hash_T hash;
10844 hashitem_T *hi;
10845
10846 if (s != NULL)
10847 {
10848 hash = hash_hash(s);
10849 hi = hash_lookup(&su->su_banned, s, hash);
10850 if (HASHITEM_EMPTY(hi))
10851 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010852 else
10853 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010854 }
10855}
10856
10857/*
10858 * Return TRUE if a word appears in the list of banned words.
10859 */
10860 static int
10861was_banned(su, word)
10862 suginfo_T *su;
10863 char_u *word;
10864{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010865 hashitem_T *hi = hash_find(&su->su_banned, word);
10866
10867 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010868}
10869
10870/*
10871 * Free the banned words in "su".
10872 */
10873 static void
10874free_banned(su)
10875 suginfo_T *su;
10876{
10877 int todo;
10878 hashitem_T *hi;
10879
10880 todo = su->su_banned.ht_used;
10881 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
10882 {
10883 if (!HASHITEM_EMPTY(hi))
10884 {
10885 vim_free(hi->hi_key);
10886 --todo;
10887 }
10888 }
10889 hash_clear(&su->su_banned);
10890}
10891
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010892/*
10893 * Recompute the score if sound-folding is possible. This is slow,
10894 * thus only done for the final results.
10895 */
10896 static void
10897rescore_suggestions(su)
10898 suginfo_T *su;
10899{
10900 langp_T *lp;
10901 suggest_T *stp;
10902 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010903 int i;
10904
10905 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
10906 lp->lp_slang != NULL; ++lp)
10907 {
10908 if (lp->lp_slang->sl_sal.ga_len > 0)
10909 {
10910 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010911 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010912
10913 for (i = 0; i < su->su_ga.ga_len; ++i)
10914 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010915 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010916 if (!stp->st_had_bonus)
10917 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010918 stp->st_altscore = stp_sal_score(stp, su,
10919 lp->lp_slang, sal_badword);
10920 if (stp->st_altscore == SCORE_MAXMAX)
10921 stp->st_altscore = SCORE_BIG;
10922 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010923 }
10924 }
10925 break;
10926 }
10927 }
10928}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010929
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010930static int
10931#ifdef __BORLANDC__
10932_RTLENTRYF
10933#endif
10934sug_compare __ARGS((const void *s1, const void *s2));
10935
10936/*
10937 * Function given to qsort() to sort the suggestions on st_score.
10938 */
10939 static int
10940#ifdef __BORLANDC__
10941_RTLENTRYF
10942#endif
10943sug_compare(s1, s2)
10944 const void *s1;
10945 const void *s2;
10946{
10947 suggest_T *p1 = (suggest_T *)s1;
10948 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010949 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010950
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010951 if (n == 0)
10952 return p1->st_altscore - p2->st_altscore;
10953 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010954}
10955
10956/*
10957 * Cleanup the suggestions:
10958 * - Sort on score.
10959 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010960 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010961 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010962 static int
10963cleanup_suggestions(gap, maxscore, keep)
10964 garray_T *gap;
10965 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010966 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010967{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010968 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010969 int i;
10970
10971 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010972 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010973
10974 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010975 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010976 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010977 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010978 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010979 gap->ga_len = keep;
10980 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010981 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010982 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010983}
10984
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010985#if defined(FEAT_EVAL) || defined(PROTO)
10986/*
10987 * Soundfold a string, for soundfold().
10988 * Result is in allocated memory, NULL for an error.
10989 */
10990 char_u *
10991eval_soundfold(word)
10992 char_u *word;
10993{
10994 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010995 char_u sound[MAXWLEN];
10996
10997 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
10998 /* Use the sound-folding of the first language that supports it. */
10999 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
11000 lp->lp_slang != NULL; ++lp)
11001 if (lp->lp_slang->sl_sal.ga_len > 0)
11002 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011003 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011004 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011005 return vim_strsave(sound);
11006 }
11007
11008 /* No language with sound folding, return word as-is. */
11009 return vim_strsave(word);
11010}
11011#endif
11012
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011013/*
11014 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000011015 *
11016 * There are many ways to turn a word into a sound-a-like representation. The
11017 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
11018 * swedish name matching - survey and test of different algorithms" by Klas
11019 * Erikson.
11020 *
11021 * We support two methods:
11022 * 1. SOFOFROM/SOFOTO do a simple character mapping.
11023 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011024 */
11025 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011026spell_soundfold(slang, inword, folded, res)
11027 slang_T *slang;
11028 char_u *inword;
11029 int folded; /* "inword" is already case-folded */
11030 char_u *res;
11031{
11032 char_u fword[MAXWLEN];
11033 char_u *word;
11034
11035 if (slang->sl_sofo)
11036 /* SOFOFROM and SOFOTO used */
11037 spell_soundfold_sofo(slang, inword, res);
11038 else
11039 {
11040 /* SAL items used. Requires the word to be case-folded. */
11041 if (folded)
11042 word = inword;
11043 else
11044 {
11045 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
11046 word = fword;
11047 }
11048
11049#ifdef FEAT_MBYTE
11050 if (has_mbyte)
11051 spell_soundfold_wsal(slang, word, res);
11052 else
11053#endif
11054 spell_soundfold_sal(slang, word, res);
11055 }
11056}
11057
11058/*
11059 * Perform sound folding of "inword" into "res" according to SOFOFROM and
11060 * SOFOTO lines.
11061 */
11062 static void
11063spell_soundfold_sofo(slang, inword, res)
11064 slang_T *slang;
11065 char_u *inword;
11066 char_u *res;
11067{
11068 char_u *s;
11069 int ri = 0;
11070 int c;
11071
11072#ifdef FEAT_MBYTE
11073 if (has_mbyte)
11074 {
11075 int prevc = 0;
11076 int *ip;
11077
11078 /* The sl_sal_first[] table contains the translation for chars up to
11079 * 255, sl_sal the rest. */
11080 for (s = inword; *s != NUL; )
11081 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011082 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011083 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
11084 c = ' ';
11085 else if (c < 256)
11086 c = slang->sl_sal_first[c];
11087 else
11088 {
11089 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
11090 if (ip == NULL) /* empty list, can't match */
11091 c = NUL;
11092 else
11093 for (;;) /* find "c" in the list */
11094 {
11095 if (*ip == 0) /* not found */
11096 {
11097 c = NUL;
11098 break;
11099 }
11100 if (*ip == c) /* match! */
11101 {
11102 c = ip[1];
11103 break;
11104 }
11105 ip += 2;
11106 }
11107 }
11108
11109 if (c != NUL && c != prevc)
11110 {
11111 ri += mb_char2bytes(c, res + ri);
11112 if (ri + MB_MAXBYTES > MAXWLEN)
11113 break;
11114 prevc = c;
11115 }
11116 }
11117 }
11118 else
11119#endif
11120 {
11121 /* The sl_sal_first[] table contains the translation. */
11122 for (s = inword; (c = *s) != NUL; ++s)
11123 {
11124 if (vim_iswhite(c))
11125 c = ' ';
11126 else
11127 c = slang->sl_sal_first[c];
11128 if (c != NUL && (ri == 0 || res[ri - 1] != c))
11129 res[ri++] = c;
11130 }
11131 }
11132
11133 res[ri] = NUL;
11134}
11135
11136 static void
11137spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011138 slang_T *slang;
11139 char_u *inword;
11140 char_u *res;
11141{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011142 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011143 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011144 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011145 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011146 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011147 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011148 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011149 int n, k = 0;
11150 int z0;
11151 int k0;
11152 int n0;
11153 int c;
11154 int pri;
11155 int p0 = -333;
11156 int c0;
11157
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011158 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011159 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011160 if (slang->sl_rem_accents)
11161 {
11162 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011163 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011164 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011165 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011166 {
11167 *t++ = ' ';
11168 s = skipwhite(s);
11169 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011170 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011171 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011172 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011173 *t++ = *s;
11174 ++s;
11175 }
11176 }
11177 *t = NUL;
11178 }
11179 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011180 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011181
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011182 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011183
11184 /*
11185 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011186 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011187 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011188 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011189 while ((c = word[i]) != NUL)
11190 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011191 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011192 n = slang->sl_sal_first[c];
11193 z0 = 0;
11194
11195 if (n >= 0)
11196 {
11197 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011198 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011199 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011200 /* Quickly skip entries that don't match the word. Most
11201 * entries are less then three chars, optimize for that. */
11202 k = smp[n].sm_leadlen;
11203 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011204 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011205 if (word[i + 1] != s[1])
11206 continue;
11207 if (k > 2)
11208 {
11209 for (j = 2; j < k; ++j)
11210 if (word[i + j] != s[j])
11211 break;
11212 if (j < k)
11213 continue;
11214 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011215 }
11216
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011217 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011218 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011219 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011220 while (*pf != NUL && *pf != word[i + k])
11221 ++pf;
11222 if (*pf == NUL)
11223 continue;
11224 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011225 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011226 s = smp[n].sm_rules;
11227 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011228
11229 p0 = *s;
11230 k0 = k;
11231 while (*s == '-' && k > 1)
11232 {
11233 k--;
11234 s++;
11235 }
11236 if (*s == '<')
11237 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011238 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011239 {
11240 /* determine priority */
11241 pri = *s - '0';
11242 s++;
11243 }
11244 if (*s == '^' && *(s + 1) == '^')
11245 s++;
11246
11247 if (*s == NUL
11248 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011249 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011250 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011251 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011252 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011253 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011254 && spell_iswordp(word + i - 1, curbuf)
11255 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011256 {
11257 /* search for followup rules, if: */
11258 /* followup and k > 1 and NO '-' in searchstring */
11259 c0 = word[i + k - 1];
11260 n0 = slang->sl_sal_first[c0];
11261
11262 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011263 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011264 {
11265 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011266 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011267 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011268 /* Quickly skip entries that don't match the word.
11269 * */
11270 k0 = smp[n0].sm_leadlen;
11271 if (k0 > 1)
11272 {
11273 if (word[i + k] != s[1])
11274 continue;
11275 if (k0 > 2)
11276 {
11277 pf = word + i + k + 1;
11278 for (j = 2; j < k0; ++j)
11279 if (*pf++ != s[j])
11280 break;
11281 if (j < k0)
11282 continue;
11283 }
11284 }
11285 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011286
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011287 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011288 {
11289 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011290 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011291 while (*pf != NUL && *pf != word[i + k0])
11292 ++pf;
11293 if (*pf == NUL)
11294 continue;
11295 ++k0;
11296 }
11297
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011298 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011299 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011300 while (*s == '-')
11301 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011302 /* "k0" gets NOT reduced because
11303 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011304 s++;
11305 }
11306 if (*s == '<')
11307 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011308 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011309 {
11310 p0 = *s - '0';
11311 s++;
11312 }
11313
11314 if (*s == NUL
11315 /* *s == '^' cuts */
11316 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011317 && !spell_iswordp(word + i + k0,
11318 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011319 {
11320 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011321 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011322 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011323
11324 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011325 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011326 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011327 /* rule fits; stop search */
11328 break;
11329 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011330 }
11331
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011332 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011333 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011334 }
11335
11336 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011337 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011338 if (s == NULL)
11339 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011340 pf = smp[n].sm_rules;
11341 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011342 if (p0 == 1 && z == 0)
11343 {
11344 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011345 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
11346 || res[reslen - 1] == *s))
11347 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011348 z0 = 1;
11349 z = 1;
11350 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011351 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011352 {
11353 word[i + k0] = *s;
11354 k0++;
11355 s++;
11356 }
11357 if (k > k0)
11358 mch_memmove(word + i + k0, word + i + k,
11359 STRLEN(word + i + k) + 1);
11360
11361 /* new "actual letter" */
11362 c = word[i];
11363 }
11364 else
11365 {
11366 /* no '<' rule used */
11367 i += k - 1;
11368 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011369 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011370 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011371 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011372 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011373 s++;
11374 }
11375 /* new "actual letter" */
11376 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011377 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011378 {
11379 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011380 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011381 mch_memmove(word, word + i + 1,
11382 STRLEN(word + i + 1) + 1);
11383 i = 0;
11384 z0 = 1;
11385 }
11386 }
11387 break;
11388 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011389 }
11390 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011391 else if (vim_iswhite(c))
11392 {
11393 c = ' ';
11394 k = 1;
11395 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011396
11397 if (z0 == 0)
11398 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011399 if (k && !p0 && reslen < MAXWLEN && c != NUL
11400 && (!slang->sl_collapse || reslen == 0
11401 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011402 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011403 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011404
11405 i++;
11406 z = 0;
11407 k = 0;
11408 }
11409 }
11410
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011411 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011412}
11413
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011414#ifdef FEAT_MBYTE
11415/*
11416 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
11417 * Multi-byte version of spell_soundfold().
11418 */
11419 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011420spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011421 slang_T *slang;
11422 char_u *inword;
11423 char_u *res;
11424{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011425 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011426 int word[MAXWLEN];
11427 int wres[MAXWLEN];
11428 int l;
11429 char_u *s;
11430 int *ws;
11431 char_u *t;
11432 int *pf;
11433 int i, j, z;
11434 int reslen;
11435 int n, k = 0;
11436 int z0;
11437 int k0;
11438 int n0;
11439 int c;
11440 int pri;
11441 int p0 = -333;
11442 int c0;
11443 int did_white = FALSE;
11444
11445 /*
11446 * Convert the multi-byte string to a wide-character string.
11447 * Remove accents, if wanted. We actually remove all non-word characters.
11448 * But keep white space.
11449 */
11450 n = 0;
11451 for (s = inword; *s != NUL; )
11452 {
11453 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011454 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011455 if (slang->sl_rem_accents)
11456 {
11457 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
11458 {
11459 if (did_white)
11460 continue;
11461 c = ' ';
11462 did_white = TRUE;
11463 }
11464 else
11465 {
11466 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011467 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011468 continue;
11469 }
11470 }
11471 word[n++] = c;
11472 }
11473 word[n] = NUL;
11474
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011475 /*
11476 * This comes from Aspell phonet.cpp.
11477 * Converted from C++ to C. Added support for multi-byte chars.
11478 * Changed to keep spaces.
11479 */
11480 i = reslen = z = 0;
11481 while ((c = word[i]) != NUL)
11482 {
11483 /* Start with the first rule that has the character in the word. */
11484 n = slang->sl_sal_first[c & 0xff];
11485 z0 = 0;
11486
11487 if (n >= 0)
11488 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011489 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011490 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
11491 {
11492 /* Quickly skip entries that don't match the word. Most
11493 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011494 if (c != ws[0])
11495 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011496 k = smp[n].sm_leadlen;
11497 if (k > 1)
11498 {
11499 if (word[i + 1] != ws[1])
11500 continue;
11501 if (k > 2)
11502 {
11503 for (j = 2; j < k; ++j)
11504 if (word[i + j] != ws[j])
11505 break;
11506 if (j < k)
11507 continue;
11508 }
11509 }
11510
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011511 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011512 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011513 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011514 while (*pf != NUL && *pf != word[i + k])
11515 ++pf;
11516 if (*pf == NUL)
11517 continue;
11518 ++k;
11519 }
11520 s = smp[n].sm_rules;
11521 pri = 5; /* default priority */
11522
11523 p0 = *s;
11524 k0 = k;
11525 while (*s == '-' && k > 1)
11526 {
11527 k--;
11528 s++;
11529 }
11530 if (*s == '<')
11531 s++;
11532 if (VIM_ISDIGIT(*s))
11533 {
11534 /* determine priority */
11535 pri = *s - '0';
11536 s++;
11537 }
11538 if (*s == '^' && *(s + 1) == '^')
11539 s++;
11540
11541 if (*s == NUL
11542 || (*s == '^'
11543 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011544 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011545 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011546 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011547 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011548 && spell_iswordp_w(word + i - 1, curbuf)
11549 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011550 {
11551 /* search for followup rules, if: */
11552 /* followup and k > 1 and NO '-' in searchstring */
11553 c0 = word[i + k - 1];
11554 n0 = slang->sl_sal_first[c0 & 0xff];
11555
11556 if (slang->sl_followup && k > 1 && n0 >= 0
11557 && p0 != '-' && word[i + k] != NUL)
11558 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011559 /* Test follow-up rule for "word[i + k]"; loop over
11560 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011561 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
11562 == (c0 & 0xff); ++n0)
11563 {
11564 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011565 */
11566 if (c0 != ws[0])
11567 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011568 k0 = smp[n0].sm_leadlen;
11569 if (k0 > 1)
11570 {
11571 if (word[i + k] != ws[1])
11572 continue;
11573 if (k0 > 2)
11574 {
11575 pf = word + i + k + 1;
11576 for (j = 2; j < k0; ++j)
11577 if (*pf++ != ws[j])
11578 break;
11579 if (j < k0)
11580 continue;
11581 }
11582 }
11583 k0 += k - 1;
11584
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011585 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011586 {
11587 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011588 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011589 while (*pf != NUL && *pf != word[i + k0])
11590 ++pf;
11591 if (*pf == NUL)
11592 continue;
11593 ++k0;
11594 }
11595
11596 p0 = 5;
11597 s = smp[n0].sm_rules;
11598 while (*s == '-')
11599 {
11600 /* "k0" gets NOT reduced because
11601 * "if (k0 == k)" */
11602 s++;
11603 }
11604 if (*s == '<')
11605 s++;
11606 if (VIM_ISDIGIT(*s))
11607 {
11608 p0 = *s - '0';
11609 s++;
11610 }
11611
11612 if (*s == NUL
11613 /* *s == '^' cuts */
11614 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011615 && !spell_iswordp_w(word + i + k0,
11616 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011617 {
11618 if (k0 == k)
11619 /* this is just a piece of the string */
11620 continue;
11621
11622 if (p0 < pri)
11623 /* priority too low */
11624 continue;
11625 /* rule fits; stop search */
11626 break;
11627 }
11628 }
11629
11630 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
11631 == (c0 & 0xff))
11632 continue;
11633 }
11634
11635 /* replace string */
11636 ws = smp[n].sm_to_w;
11637 s = smp[n].sm_rules;
11638 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
11639 if (p0 == 1 && z == 0)
11640 {
11641 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011642 if (reslen > 0 && ws != NULL && *ws != NUL
11643 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011644 || wres[reslen - 1] == *ws))
11645 reslen--;
11646 z0 = 1;
11647 z = 1;
11648 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011649 if (ws != NULL)
11650 while (*ws != NUL && word[i + k0] != NUL)
11651 {
11652 word[i + k0] = *ws;
11653 k0++;
11654 ws++;
11655 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011656 if (k > k0)
11657 mch_memmove(word + i + k0, word + i + k,
11658 sizeof(int) * (STRLEN(word + i + k) + 1));
11659
11660 /* new "actual letter" */
11661 c = word[i];
11662 }
11663 else
11664 {
11665 /* no '<' rule used */
11666 i += k - 1;
11667 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011668 if (ws != NULL)
11669 while (*ws != NUL && ws[1] != NUL
11670 && reslen < MAXWLEN)
11671 {
11672 if (reslen == 0 || wres[reslen - 1] != *ws)
11673 wres[reslen++] = *ws;
11674 ws++;
11675 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011676 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011677 if (ws == NULL)
11678 c = NUL;
11679 else
11680 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011681 if (strstr((char *)s, "^^") != NULL)
11682 {
11683 if (c != NUL)
11684 wres[reslen++] = c;
11685 mch_memmove(word, word + i + 1,
11686 sizeof(int) * (STRLEN(word + i + 1) + 1));
11687 i = 0;
11688 z0 = 1;
11689 }
11690 }
11691 break;
11692 }
11693 }
11694 }
11695 else if (vim_iswhite(c))
11696 {
11697 c = ' ';
11698 k = 1;
11699 }
11700
11701 if (z0 == 0)
11702 {
11703 if (k && !p0 && reslen < MAXWLEN && c != NUL
11704 && (!slang->sl_collapse || reslen == 0
11705 || wres[reslen - 1] != c))
11706 /* condense only double letters */
11707 wres[reslen++] = c;
11708
11709 i++;
11710 z = 0;
11711 k = 0;
11712 }
11713 }
11714
11715 /* Convert wide characters in "wres" to a multi-byte string in "res". */
11716 l = 0;
11717 for (n = 0; n < reslen; ++n)
11718 {
11719 l += mb_char2bytes(wres[n], res + l);
11720 if (l + MB_MAXBYTES > MAXWLEN)
11721 break;
11722 }
11723 res[l] = NUL;
11724}
11725#endif
11726
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011727/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011728 * Compute a score for two sound-a-like words.
11729 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
11730 * Instead of a generic loop we write out the code. That keeps it fast by
11731 * avoiding checks that will not be possible.
11732 */
11733 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011734soundalike_score(goodstart, badstart)
11735 char_u *goodstart; /* sound-folded good word */
11736 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011737{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011738 char_u *goodsound = goodstart;
11739 char_u *badsound = badstart;
11740 int goodlen;
11741 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011742 int n;
11743 char_u *pl, *ps;
11744 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011745 int score = 0;
11746
11747 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
11748 * counted so much, vowels halfway the word aren't counted at all. */
11749 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
11750 {
11751 score = SCORE_DEL / 2;
11752 if (*badsound == '*')
11753 ++badsound;
11754 else
11755 ++goodsound;
11756 }
11757
11758 goodlen = STRLEN(goodsound);
11759 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011760
11761 /* Return quickly if the lenghts are too different to be fixed by two
11762 * changes. */
11763 n = goodlen - badlen;
11764 if (n < -2 || n > 2)
11765 return SCORE_MAXMAX;
11766
11767 if (n > 0)
11768 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011769 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011770 ps = badsound;
11771 }
11772 else
11773 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011774 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011775 ps = goodsound;
11776 }
11777
11778 /* Skip over the identical part. */
11779 while (*pl == *ps && *pl != NUL)
11780 {
11781 ++pl;
11782 ++ps;
11783 }
11784
11785 switch (n)
11786 {
11787 case -2:
11788 case 2:
11789 /*
11790 * Must delete two characters from "pl".
11791 */
11792 ++pl; /* first delete */
11793 while (*pl == *ps)
11794 {
11795 ++pl;
11796 ++ps;
11797 }
11798 /* strings must be equal after second delete */
11799 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011800 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011801
11802 /* Failed to compare. */
11803 break;
11804
11805 case -1:
11806 case 1:
11807 /*
11808 * Minimal one delete from "pl" required.
11809 */
11810
11811 /* 1: delete */
11812 pl2 = pl + 1;
11813 ps2 = ps;
11814 while (*pl2 == *ps2)
11815 {
11816 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011817 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011818 ++pl2;
11819 ++ps2;
11820 }
11821
11822 /* 2: delete then swap, then rest must be equal */
11823 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
11824 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011825 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011826
11827 /* 3: delete then substitute, then the rest must be equal */
11828 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011829 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011830
11831 /* 4: first swap then delete */
11832 if (pl[0] == ps[1] && pl[1] == ps[0])
11833 {
11834 pl2 = pl + 2; /* swap, skip two chars */
11835 ps2 = ps + 2;
11836 while (*pl2 == *ps2)
11837 {
11838 ++pl2;
11839 ++ps2;
11840 }
11841 /* delete a char and then strings must be equal */
11842 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011843 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011844 }
11845
11846 /* 5: first substitute then delete */
11847 pl2 = pl + 1; /* substitute, skip one char */
11848 ps2 = ps + 1;
11849 while (*pl2 == *ps2)
11850 {
11851 ++pl2;
11852 ++ps2;
11853 }
11854 /* delete a char and then strings must be equal */
11855 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011856 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011857
11858 /* Failed to compare. */
11859 break;
11860
11861 case 0:
11862 /*
11863 * Lenghts are equal, thus changes must result in same length: An
11864 * insert is only possible in combination with a delete.
11865 * 1: check if for identical strings
11866 */
11867 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011868 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011869
11870 /* 2: swap */
11871 if (pl[0] == ps[1] && pl[1] == ps[0])
11872 {
11873 pl2 = pl + 2; /* swap, skip two chars */
11874 ps2 = ps + 2;
11875 while (*pl2 == *ps2)
11876 {
11877 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011878 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011879 ++pl2;
11880 ++ps2;
11881 }
11882 /* 3: swap and swap again */
11883 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
11884 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011885 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011886
11887 /* 4: swap and substitute */
11888 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011889 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011890 }
11891
11892 /* 5: substitute */
11893 pl2 = pl + 1;
11894 ps2 = ps + 1;
11895 while (*pl2 == *ps2)
11896 {
11897 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011898 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011899 ++pl2;
11900 ++ps2;
11901 }
11902
11903 /* 6: substitute and swap */
11904 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
11905 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011906 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011907
11908 /* 7: substitute and substitute */
11909 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011910 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011911
11912 /* 8: insert then delete */
11913 pl2 = pl;
11914 ps2 = ps + 1;
11915 while (*pl2 == *ps2)
11916 {
11917 ++pl2;
11918 ++ps2;
11919 }
11920 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011921 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011922
11923 /* 9: delete then insert */
11924 pl2 = pl + 1;
11925 ps2 = ps;
11926 while (*pl2 == *ps2)
11927 {
11928 ++pl2;
11929 ++ps2;
11930 }
11931 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011932 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011933
11934 /* Failed to compare. */
11935 break;
11936 }
11937
11938 return SCORE_MAXMAX;
11939}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011940
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011941/*
11942 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011943 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011944 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000011945 * The algorithm is described by Du and Chang, 1992.
11946 * The implementation of the algorithm comes from Aspell editdist.cpp,
11947 * edit_distance(). It has been converted from C++ to C and modified to
11948 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011949 */
11950 static int
11951spell_edit_score(badword, goodword)
11952 char_u *badword;
11953 char_u *goodword;
11954{
11955 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011956 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011957 int j, i;
11958 int t;
11959 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011960 int pbc, pgc;
11961#ifdef FEAT_MBYTE
11962 char_u *p;
11963 int wbadword[MAXWLEN];
11964 int wgoodword[MAXWLEN];
11965
11966 if (has_mbyte)
11967 {
11968 /* Get the characters from the multi-byte strings and put them in an
11969 * int array for easy access. */
11970 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011971 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000011972 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011973 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011974 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000011975 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011976 }
11977 else
11978#endif
11979 {
11980 badlen = STRLEN(badword) + 1;
11981 goodlen = STRLEN(goodword) + 1;
11982 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011983
11984 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
11985#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011986 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
11987 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011988 if (cnt == NULL)
11989 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011990
11991 CNT(0, 0) = 0;
11992 for (j = 1; j <= goodlen; ++j)
11993 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
11994
11995 for (i = 1; i <= badlen; ++i)
11996 {
11997 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
11998 for (j = 1; j <= goodlen; ++j)
11999 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012000#ifdef FEAT_MBYTE
12001 if (has_mbyte)
12002 {
12003 bc = wbadword[i - 1];
12004 gc = wgoodword[j - 1];
12005 }
12006 else
12007#endif
12008 {
12009 bc = badword[i - 1];
12010 gc = goodword[j - 1];
12011 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012012 if (bc == gc)
12013 CNT(i, j) = CNT(i - 1, j - 1);
12014 else
12015 {
12016 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012017 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012018 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
12019 else
12020 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
12021
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012022 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012023 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012024#ifdef FEAT_MBYTE
12025 if (has_mbyte)
12026 {
12027 pbc = wbadword[i - 2];
12028 pgc = wgoodword[j - 2];
12029 }
12030 else
12031#endif
12032 {
12033 pbc = badword[i - 2];
12034 pgc = goodword[j - 2];
12035 }
12036 if (bc == pgc && pbc == gc)
12037 {
12038 t = SCORE_SWAP + CNT(i - 2, j - 2);
12039 if (t < CNT(i, j))
12040 CNT(i, j) = t;
12041 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012042 }
12043 t = SCORE_DEL + CNT(i - 1, j);
12044 if (t < CNT(i, j))
12045 CNT(i, j) = t;
12046 t = SCORE_INS + CNT(i, j - 1);
12047 if (t < CNT(i, j))
12048 CNT(i, j) = t;
12049 }
12050 }
12051 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012052
12053 i = CNT(badlen - 1, goodlen - 1);
12054 vim_free(cnt);
12055 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012056}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000012057
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012058/*
12059 * ":spelldump"
12060 */
12061/*ARGSUSED*/
12062 void
12063ex_spelldump(eap)
12064 exarg_T *eap;
12065{
12066 buf_T *buf = curbuf;
12067 langp_T *lp;
12068 slang_T *slang;
12069 idx_T arridx[MAXWLEN];
12070 int curi[MAXWLEN];
12071 char_u word[MAXWLEN];
12072 int c;
12073 char_u *byts;
12074 idx_T *idxs;
12075 linenr_T lnum = 0;
12076 int round;
12077 int depth;
12078 int n;
12079 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000012080 char_u *region_names = NULL; /* region names being used */
12081 int do_region = TRUE; /* dump region names and numbers */
12082 char_u *p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012083
12084 if (no_spell_checking())
12085 return;
12086
12087 /* Create a new empty buffer by splitting the window. */
12088 do_cmdline_cmd((char_u *)"new");
12089 if (!bufempty() || !buf_valid(buf))
12090 return;
12091
Bram Moolenaar7887d882005-07-01 22:33:52 +000012092 /* Find out if we can support regions: All languages must support the same
12093 * regions or none at all. */
12094 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
12095 {
12096 p = lp->lp_slang->sl_regions;
12097 if (p[0] != 0)
12098 {
12099 if (region_names == NULL) /* first language with regions */
12100 region_names = p;
12101 else if (STRCMP(region_names, p) != 0)
12102 {
12103 do_region = FALSE; /* region names are different */
12104 break;
12105 }
12106 }
12107 }
12108
12109 if (do_region && region_names != NULL)
12110 {
12111 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
12112 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
12113 }
12114 else
12115 do_region = FALSE;
12116
12117 /*
12118 * Loop over all files loaded for the entries in 'spelllang'.
12119 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012120 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
12121 {
12122 slang = lp->lp_slang;
12123
12124 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
12125 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
12126
12127 /* round 1: case-folded tree
12128 * round 2: keep-case tree */
12129 for (round = 1; round <= 2; ++round)
12130 {
12131 if (round == 1)
12132 {
12133 byts = slang->sl_fbyts;
12134 idxs = slang->sl_fidxs;
12135 }
12136 else
12137 {
12138 byts = slang->sl_kbyts;
12139 idxs = slang->sl_kidxs;
12140 }
12141 if (byts == NULL)
12142 continue; /* array is empty */
12143
12144 depth = 0;
12145 arridx[0] = 0;
12146 curi[0] = 1;
12147 while (depth >= 0 && !got_int)
12148 {
12149 if (curi[depth] > byts[arridx[depth]])
12150 {
12151 /* Done all bytes at this node, go up one level. */
12152 --depth;
12153 line_breakcheck();
12154 }
12155 else
12156 {
12157 /* Do one more byte at this node. */
12158 n = arridx[depth] + curi[depth];
12159 ++curi[depth];
12160 c = byts[n];
12161 if (c == 0)
12162 {
12163 /* End of word, deal with the word.
12164 * Don't use keep-case words in the fold-case tree,
12165 * they will appear in the keep-case tree.
12166 * Only use the word when the region matches. */
12167 flags = (int)idxs[n];
12168 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +000012169 && (do_region
12170 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012171 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012172 & lp->lp_region) != 0))
12173 {
12174 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000012175 if (!do_region)
12176 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000012177
12178 /* Dump the basic word if there is no prefix or
12179 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012180 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000012181 if (c == 0 || curi[depth] == 2)
12182 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012183
12184 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000012185 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012186 lnum = apply_prefixes(slang, word, round,
12187 flags, lnum);
12188 }
12189 }
12190 else
12191 {
12192 /* Normal char, go one level deeper. */
12193 word[depth++] = c;
12194 arridx[depth] = idxs[n];
12195 curi[depth] = 1;
12196 }
12197 }
12198 }
12199 }
12200 }
12201
12202 /* Delete the empty line that we started with. */
12203 if (curbuf->b_ml.ml_line_count > 1)
12204 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
12205
12206 redraw_later(NOT_VALID);
12207}
12208
12209/*
12210 * Dump one word: apply case modifications and append a line to the buffer.
12211 */
12212 static void
12213dump_word(word, round, flags, lnum)
12214 char_u *word;
12215 int round;
12216 int flags;
12217 linenr_T lnum;
12218{
12219 int keepcap = FALSE;
12220 char_u *p;
12221 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000012222 char_u badword[MAXWLEN + 10];
12223 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012224
12225 if (round == 1 && (flags & WF_CAPMASK) != 0)
12226 {
12227 /* Need to fix case according to "flags". */
12228 make_case_word(word, cword, flags);
12229 p = cword;
12230 }
12231 else
12232 {
12233 p = word;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000012234 if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0
12235 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012236 keepcap = TRUE;
12237 }
12238
Bram Moolenaar7887d882005-07-01 22:33:52 +000012239 /* Add flags and regions after a slash. */
12240 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012241 {
Bram Moolenaar7887d882005-07-01 22:33:52 +000012242 STRCPY(badword, p);
12243 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012244 if (keepcap)
12245 STRCAT(badword, "=");
12246 if (flags & WF_BANNED)
12247 STRCAT(badword, "!");
12248 else if (flags & WF_RARE)
12249 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +000012250 if (flags & WF_REGION)
12251 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012252 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +000012253 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012254 p = badword;
12255 }
12256
12257 ml_append(lnum, p, (colnr_T)0, FALSE);
12258}
12259
12260/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012261 * For ":spelldump": Find matching prefixes for "word". Prepend each to
12262 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012263 * Return the updated line number.
12264 */
12265 static linenr_T
12266apply_prefixes(slang, word, round, flags, startlnum)
12267 slang_T *slang;
12268 char_u *word; /* case-folded word */
12269 int round;
12270 int flags; /* flags with prefix ID */
12271 linenr_T startlnum;
12272{
12273 idx_T arridx[MAXWLEN];
12274 int curi[MAXWLEN];
12275 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000012276 char_u word_up[MAXWLEN];
12277 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012278 int c;
12279 char_u *byts;
12280 idx_T *idxs;
12281 linenr_T lnum = startlnum;
12282 int depth;
12283 int n;
12284 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012285 int i;
12286
Bram Moolenaar53805d12005-08-01 07:08:33 +000012287 /* if the word starts with a lower-case letter make the word with an
12288 * upper-case letter in word_up[]. */
12289 c = PTR2CHAR(word);
12290 if (SPELL_TOUPPER(c) != c)
12291 {
12292 onecap_copy(word, word_up, TRUE);
12293 has_word_up = TRUE;
12294 }
12295
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012296 byts = slang->sl_pbyts;
12297 idxs = slang->sl_pidxs;
12298 if (byts != NULL) /* array not is empty */
12299 {
12300 /*
12301 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012302 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012303 */
12304 depth = 0;
12305 arridx[0] = 0;
12306 curi[0] = 1;
12307 while (depth >= 0 && !got_int)
12308 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012309 n = arridx[depth];
12310 len = byts[n];
12311 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012312 {
12313 /* Done all bytes at this node, go up one level. */
12314 --depth;
12315 line_breakcheck();
12316 }
12317 else
12318 {
12319 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012320 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012321 ++curi[depth];
12322 c = byts[n];
12323 if (c == 0)
12324 {
12325 /* End of prefix, find out how many IDs there are. */
12326 for (i = 1; i < len; ++i)
12327 if (byts[n + i] != 0)
12328 break;
12329 curi[depth] += i - 1;
12330
Bram Moolenaar53805d12005-08-01 07:08:33 +000012331 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
12332 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012333 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000012334 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000012335 dump_word(prefix, round,
Bram Moolenaar53805d12005-08-01 07:08:33 +000012336 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000012337 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012338 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000012339
12340 /* Check for prefix that matches the word when the
12341 * first letter is upper-case, but only if the prefix has
12342 * a condition. */
12343 if (has_word_up)
12344 {
12345 c = valid_word_prefix(i, n, flags, word_up, slang,
12346 TRUE);
12347 if (c != 0)
12348 {
12349 vim_strncpy(prefix + depth, word_up,
12350 MAXWLEN - depth - 1);
12351 dump_word(prefix, round,
12352 (c & WF_RAREPFX) ? (flags | WF_RARE)
12353 : flags, lnum++);
12354 }
12355 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012356 }
12357 else
12358 {
12359 /* Normal char, go one level deeper. */
12360 prefix[depth++] = c;
12361 arridx[depth] = idxs[n];
12362 curi[depth] = 1;
12363 }
12364 }
12365 }
12366 }
12367
12368 return lnum;
12369}
12370
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012371#if defined(FEAT_INS_EXPAND) || defined(PROTO)
12372static int spell_expand_need_cap;
12373
12374/*
12375 * Find start of the word in front of the cursor. We don't check if it is
12376 * badly spelled, with completion we can only change the word in front of the
12377 * cursor.
12378 * Used for Insert mode completion CTRL-X ?.
12379 * Returns the column number of the word.
12380 */
12381 int
12382spell_word_start(startcol)
12383 int startcol;
12384{
12385 char_u *line;
12386 char_u *p;
12387 int col = 0;
12388
12389 if (no_spell_checking())
12390 return startcol;
12391
12392 /* Find a word character before "startcol". */
12393 line = ml_get_curline();
12394 for (p = line + startcol; p > line; )
12395 {
12396 mb_ptr_back(line, p);
12397 if (spell_iswordp_nmw(p))
12398 break;
12399 }
12400
12401 /* Go back to start of the word. */
12402 while (p > line)
12403 {
12404 col = p - line;
12405 mb_ptr_back(line, p);
12406 if (!spell_iswordp(p, curbuf))
12407 break;
12408 col = 0;
12409 }
12410
12411 /* Need to check for 'spellcapcheck' now, the word is removed before
12412 * expand_spelling() is called. Therefore the ugly global variable. */
12413 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
12414
12415 return col;
12416}
12417
12418/*
12419 * Get list of spelling suggestions.
12420 * Used for Insert mode completion CTRL-X ?.
12421 * Returns the number of matches. The matches are in "matchp[]", array of
12422 * allocated strings.
12423 */
12424/*ARGSUSED*/
12425 int
12426expand_spelling(lnum, col, pat, matchp)
12427 linenr_T lnum;
12428 int col;
12429 char_u *pat;
12430 char_u ***matchp;
12431{
12432 garray_T ga;
12433
12434 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap);
12435 *matchp = ga.ga_data;
12436 return ga.ga_len;
12437}
12438#endif
12439
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000012440#endif /* FEAT_SYN_HL */