blob: 56c891d82cb454f1bf3040002bb36e913e31cde2 [file] [log] [blame]
Bram Moolenaare19defe2005-03-21 08:23:33 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * spell.c: code for spell checking
Bram Moolenaarfc735152005-03-22 22:54:12 +000012 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000013 * The spell checking mechanism uses a tree (aka trie). Each node in the tree
14 * has a list of bytes that can appear (siblings). For each byte there is a
15 * pointer to the node with the byte that follows in the word (child).
Bram Moolenaar9f30f502005-06-14 22:01:04 +000016 *
17 * A NUL byte is used where the word may end. The bytes are sorted, so that
18 * binary searching can be used and the NUL bytes are at the start. The
19 * number of possible bytes is stored before the list of bytes.
20 *
21 * The tree uses two arrays: "byts" stores the characters, "idxs" stores
22 * either the next index or flags. The tree starts at index 0. For example,
23 * to lookup "vi" this sequence is followed:
24 * i = 0
25 * len = byts[i]
26 * n = where "v" appears in byts[i + 1] to byts[i + len]
27 * i = idxs[n]
28 * len = byts[i]
29 * n = where "i" appears in byts[i + 1] to byts[i + len]
30 * i = idxs[n]
31 * len = byts[i]
32 * find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi".
Bram Moolenaar51485f02005-06-04 21:55:20 +000033 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000034 * There are two word trees: one with case-folded words and one with words in
Bram Moolenaar51485f02005-06-04 21:55:20 +000035 * original case. The second one is only used for keep-case words and is
36 * usually small.
37 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +000038 * There is one additional tree for when not all prefixes are applied when
Bram Moolenaar1d73c882005-06-19 22:48:47 +000039 * generating the .spl file. This tree stores all the possible prefixes, as
40 * if they were words. At each word (prefix) end the prefix nr is stored, the
41 * following word must support this prefix nr. And the condition nr is
42 * stored, used to lookup the condition that the word must match with.
43 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000044 * Thanks to Olaf Seibert for providing an example implementation of this tree
45 * and the compression mechanism.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000046 *
47 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000048 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000049 * Why doesn't Vim use aspell/ispell/myspell/etc.?
50 * See ":help develop-spell".
51 */
52
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000053/* Use SPELL_PRINTTREE for debugging: dump the word tree after adding a word.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000054 * Only use it for small word lists! */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000055#if 0
56# define SPELL_PRINTTREE
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000057#endif
58
Bram Moolenaar51485f02005-06-04 21:55:20 +000059/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000060 * Use this to adjust the score after finding suggestions, based on the
61 * suggested word sounding like the bad word. This is much faster than doing
62 * it for every possible suggestion.
63 * Disadvantage: When "the" is typed as "hte" it sounds different and goes
64 * down in the list.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000065 * Used when 'spellsuggest' is set to "best".
66 */
67#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
68
69/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000070 * Vim spell file format: <HEADER>
Bram Moolenaar5195e452005-08-19 20:32:47 +000071 * <SECTIONS>
Bram Moolenaar1d73c882005-06-19 22:48:47 +000072 * <LWORDTREE>
73 * <KWORDTREE>
74 * <PREFIXTREE>
Bram Moolenaar51485f02005-06-04 21:55:20 +000075 *
Bram Moolenaar5195e452005-08-19 20:32:47 +000076 * <HEADER>: <fileID> <versionnr>
Bram Moolenaar51485f02005-06-04 21:55:20 +000077 *
Bram Moolenaar5195e452005-08-19 20:32:47 +000078 * <fileID> 8 bytes "VIMspell"
79 * <versionnr> 1 byte VIMSPELLVERSION
80 *
81 *
82 * Sections make it possible to add information to the .spl file without
83 * making it incompatible with previous versions. There are two kinds of
84 * sections:
85 * 1. Not essential for correct spell checking. E.g. for making suggestions.
86 * These are skipped when not supported.
87 * 2. Optional information, but essential for spell checking when present.
88 * E.g. conditions for affixes. When this section is present but not
89 * supported an error message is given.
90 *
91 * <SECTIONS>: <section> ... <sectionend>
92 *
93 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
94 *
95 * <sectionID> 1 byte number from 0 to 254 identifying the section
96 *
97 * <sectionflags> 1 byte SNF_REQUIRED: this section is required for correct
98 * spell checking
99 *
100 * <sectionlen> 4 bytes length of section contents, MSB first
101 *
102 * <sectionend> 1 byte SN_END
103 *
104 *
105 * sectionID == SN_REGION: <regionname> ...
106 * <regionname> 2 bytes Up to 8 region names: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000107 * First <regionname> is region 1.
108 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000109 * sectionID == SN_CHARFLAGS: <charflagslen> <charflags>
110 * <folcharslen> <folchars>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000111 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
112 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000113 * 0x01 word character CF_WORD
114 * 0x02 upper-case character CF_UPPER
Bram Moolenaar5195e452005-08-19 20:32:47 +0000115 * <folcharslen> 2 bytes Number of bytes in <folchars>.
116 * <folchars> N bytes Folded characters, first one is for character 128.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000117 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000118 * sectionID == SN_MIDWORD: <midword>
119 * <midword> N bytes Characters that are word characters only when used
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000120 * in the middle of a word.
121 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000122 * sectionID == SN_PREFCOND: <prefcondcnt> <prefcond> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000123 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000124 * <prefcond> : <condlen> <condstr>
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000125 * <condlen> 1 byte Length of <condstr>.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000126 * <condstr> N bytes Condition for the prefix.
127 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000128 * sectionID == SN_REP: <repcount> <rep> ...
129 * <repcount> 2 bytes number of <rep> items, MSB first.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000130 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000131 * <repfromlen> 1 byte length of <repfrom>
132 * <repfrom> N bytes "from" part of replacement
133 * <reptolen> 1 byte length of <repto>
134 * <repto> N bytes "to" part of replacement
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000135 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000136 * sectionID == SN_SAL: <salflags> <salcount> <sal> ...
137 * <salflags> 1 byte flags for soundsalike conversion:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000138 * SAL_F0LLOWUP
139 * SAL_COLLAPSE
140 * SAL_REM_ACCENTS
Bram Moolenaar5195e452005-08-19 20:32:47 +0000141 * <salcount> 2 bytes number of <sal> items following
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000142 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000143 * <salfromlen> 1 byte length of <salfrom>
144 * <salfrom> N bytes "from" part of soundsalike
145 * <saltolen> 1 byte length of <salto>
146 * <salto> N bytes "to" part of soundsalike
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000147 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000148 * sectionID == SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
149 * <sofofromlen> 2 bytes length of <sofofrom>
150 * <sofofrom> N bytes "from" part of soundfold
151 * <sofotolen> 2 bytes length of <sofoto>
152 * <sofoto> N bytes "to" part of soundfold
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000153 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000154 * sectionID == SN_MAP: <mapstr>
155 * <mapstr> N bytes String with sequences of similar characters,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000156 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000157 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000158 * sectionID == SN_COMPOUND: <compmax> <compminlen> <compsylmax> <compflags>
159 * <compmax> 1 byte Maximum nr of words in compound word.
160 * <compminlen> 1 byte Minimal word length for compounding.
161 * <compsylmax> 1 byte Maximum nr of syllables in compound word.
162 * <compflags> N bytes Flags from COMPOUNDFLAGS items, separated by
163 * slashes.
164 *
Bram Moolenaar78622822005-08-23 21:00:13 +0000165 * sectionID == SN_NOBREAK: (empty, its presence is enough)
166 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000167 * sectionID == SN_SYLLABLE: <syllable>
168 * <syllable> N bytes String from SYLLABLE item.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000169 *
170 * <LWORDTREE>: <wordtree>
171 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000172 * <KWORDTREE>: <wordtree>
173 *
174 * <PREFIXTREE>: <wordtree>
175 *
176 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000177 * <wordtree>: <nodecount> <nodedata> ...
178 *
179 * <nodecount> 4 bytes Number of nodes following. MSB first.
180 *
181 * <nodedata>: <siblingcount> <sibling> ...
182 *
183 * <siblingcount> 1 byte Number of siblings in this node. The siblings
184 * follow in sorted order.
185 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000186 * <sibling>: <byte> [ <nodeidx> <xbyte>
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000187 * | <flags> [<flags2>] [<region>] [<affixID>]
188 * | [<pflags>] <affixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000189 *
190 * <byte> 1 byte Byte value of the sibling. Special cases:
191 * BY_NOFLAGS: End of word without flags and for all
192 * regions.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000193 * For PREFIXTREE <affixID> and
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000194 * <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000195 * BY_FLAGS: End of word, <flags> follow.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000196 * For PREFIXTREE <pflags>, <affixID>
Bram Moolenaar53805d12005-08-01 07:08:33 +0000197 * and <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000198 * BY_FLAGS2: End of word, <flags> and <flags2>
199 * follow. Not used in PREFIXTREE.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000200 * BY_INDEX: Child of sibling is shared, <nodeidx>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000201 * and <xbyte> follow.
202 *
203 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
204 *
205 * <xbyte> 1 byte byte value of the sibling.
206 *
207 * <flags> 1 byte bitmask of:
208 * WF_ALLCAP word must have only capitals
209 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000210 * WF_KEEPCAP keep-case word
211 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000212 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000213 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000214 * WF_REGION <region> follows
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000215 * WF_AFX <affixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000216 *
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000217 * <flags2> 1 byte Bitmask of:
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000218 * WF_HAS_AFF >> 8 word includes affix
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000219 * WF_NEEDCOMP >> 8 word only valid in compound
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000220 *
Bram Moolenaar53805d12005-08-01 07:08:33 +0000221 * <pflags> 1 byte bitmask of:
222 * WFP_RARE rare prefix
223 * WFP_NC non-combining prefix
224 * WFP_UP letter after prefix made upper case
225 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000226 * <region> 1 byte Bitmask for regions in which word is valid. When
227 * omitted it's valid in all regions.
228 * Lowest bit is for region 1.
229 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000230 * <affixID> 1 byte ID of affix that can be used with this word. In
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000231 * PREFIXTREE used for the required prefix ID.
232 *
233 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
234 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000235 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000236 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000237 */
238
Bram Moolenaare19defe2005-03-21 08:23:33 +0000239#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
240# include <io.h> /* for lseek(), must be before vim.h */
241#endif
242
243#include "vim.h"
244
245#if defined(FEAT_SYN_HL) || defined(PROTO)
246
247#ifdef HAVE_FCNTL_H
248# include <fcntl.h>
249#endif
250
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000251#define MAXWLEN 250 /* Assume max. word len is this many bytes.
252 Some places assume a word length fits in a
253 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000254
Bram Moolenaare52325c2005-08-22 22:54:29 +0000255/* Type used for indexes in the word tree need to be at least 4 bytes. If int
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000256 * is 8 bytes we could use something smaller, but what? */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000257#if SIZEOF_INT > 3
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000258typedef int idx_T;
259#else
260typedef long idx_T;
261#endif
262
263/* Flags used for a word. Only the lowest byte can be used, the region byte
264 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000265#define WF_REGION 0x01 /* region byte follows */
266#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
267#define WF_ALLCAP 0x04 /* word must be all capitals */
268#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000269#define WF_BANNED 0x10 /* bad word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000270#define WF_AFX 0x20 /* affix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000271#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000272#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000273
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000274/* for <flags2>, shifted up one byte to be used in wn_flags */
275#define WF_HAS_AFF 0x0100 /* word includes affix */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000276#define WF_NEEDCOMP 0x0200 /* word only valid in compound */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000277
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000278#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000279
Bram Moolenaar53805d12005-08-01 07:08:33 +0000280/* flags for <pflags> */
281#define WFP_RARE 0x01 /* rare prefix */
282#define WFP_NC 0x02 /* prefix is not combining */
283#define WFP_UP 0x04 /* to-upper prefix */
284
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000285/* Flags for postponed prefixes. Must be above affixID (one byte)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000286 * and prefcondnr (two bytes). */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000287#define WF_RAREPFX (WFP_RARE << 24) /* in sl_pidxs: flag for rare
288 * postponed prefix */
289#define WF_PFX_NC (WFP_NC << 24) /* in sl_pidxs: flag for non-combining
290 * postponed prefix */
291#define WF_PFX_UP (WFP_UP << 24) /* in sl_pidxs: flag for to-upper
292 * postponed prefix */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000293
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000294/* Special byte values for <byte>. Some are only used in the tree for
295 * postponed prefixes, some only in the other trees. This is a bit messy... */
296#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000297 * postponed prefix: no <pflags> */
298#define BY_INDEX 1 /* child is shared, index follows */
299#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
300 * postponed prefix: <pflags> follows */
301#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
302 * follow; never used in prefix tree */
303#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000305/* Info from "REP" and "SAL" entries in ".aff" file used in si_rep, sl_rep,
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000306 * and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000307 * One replacement: from "ft_from" to "ft_to". */
308typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000309{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000310 char_u *ft_from;
311 char_u *ft_to;
312} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000313
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000314/* Info from "SAL" entries in ".aff" file used in sl_sal.
315 * The info is split for quick processing by spell_soundfold().
316 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
317typedef struct salitem_S
318{
319 char_u *sm_lead; /* leading letters */
320 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000321 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000322 char_u *sm_rules; /* rules like ^, $, priority */
323 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000324#ifdef FEAT_MBYTE
325 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000326 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000327 int *sm_to_w; /* wide character copy of "sm_to" */
328#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000329} salitem_T;
330
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000331#ifdef FEAT_MBYTE
332typedef int salfirst_T;
333#else
334typedef short salfirst_T;
335#endif
336
Bram Moolenaar5195e452005-08-19 20:32:47 +0000337/* Values for SP_*ERROR are negative, positive values are used by
338 * read_cnt_string(). */
339#define SP_TRUNCERROR -1 /* spell file truncated error */
340#define SP_FORMERROR -2 /* format error in spell file */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000341#define SP_OTHERERROR -3 /* other error while reading spell file */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000342
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000343/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000344 * Structure used to store words and other info for one language, loaded from
345 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000346 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
347 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
348 *
349 * The "byts" array stores the possible bytes in each tree node, preceded by
350 * the number of possible bytes, sorted on byte value:
351 * <len> <byte1> <byte2> ...
352 * The "idxs" array stores the index of the child node corresponding to the
353 * byte in "byts".
354 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000355 * the flags, region mask and affixID for the word. There may be several
356 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000357 */
358typedef struct slang_S slang_T;
359struct slang_S
360{
361 slang_T *sl_next; /* next language */
362 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000363 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000364 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000365
Bram Moolenaar51485f02005-06-04 21:55:20 +0000366 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000367 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000368 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000369 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000370 char_u *sl_pbyts; /* prefix tree word bytes */
371 idx_T *sl_pidxs; /* prefix tree word indexes */
372
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000373 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000374
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000375 char_u *sl_midword; /* MIDWORD string or NULL */
376
Bram Moolenaar5195e452005-08-19 20:32:47 +0000377 int sl_compmax; /* COMPOUNDMAX (default: MAXWLEN) */
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000378 int sl_compminlen; /* COMPOUNDMIN (default: 0) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000379 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
380 regprog_T *sl_compprog; /* COMPOUNDFLAGS turned into a regexp progrm
381 * (NULL when no compounding) */
382 char_u *sl_compstartflags; /* flags for first compound word */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000383 char_u *sl_compallflags; /* all flags for compound words */
Bram Moolenaar78622822005-08-23 21:00:13 +0000384 char_u sl_nobreak; /* When TRUE: no spaces between words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000385 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
386 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000387
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000388 int sl_prefixcnt; /* number of items in "sl_prefprog" */
389 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
390
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000391 garray_T sl_rep; /* list of fromto_T entries from REP lines */
392 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
393 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000394 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000395 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000396 there is none */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000397 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
398 * "sl_sal_first" maps chars, when has_mbyte
399 * "sl_sal" is a list of wide char lists. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000400 int sl_followup; /* SAL followup */
401 int sl_collapse; /* SAL collapse_result */
402 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaarea424162005-06-16 21:51:00 +0000403 int sl_has_map; /* TRUE if there is a MAP line */
404#ifdef FEAT_MBYTE
405 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
406 int sl_map_array[256]; /* MAP for first 256 chars */
407#else
408 char_u sl_map_array[256]; /* MAP for first 256 chars */
409#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000410};
411
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000412/* First language that is loaded, start of the linked list of loaded
413 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000414static slang_T *first_lang = NULL;
415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000416/* Flags used in .spl file for soundsalike flags. */
417#define SAL_F0LLOWUP 1
418#define SAL_COLLAPSE 2
419#define SAL_REM_ACCENTS 4
420
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000421/*
422 * Structure used in "b_langp", filled from 'spelllang'.
423 */
424typedef struct langp_S
425{
426 slang_T *lp_slang; /* info for this language (NULL for last one) */
427 int lp_region; /* bitmask for region or REGION_ALL */
428} langp_T;
429
430#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
431
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000432#define REGION_ALL 0xff /* word valid in all regions */
433
Bram Moolenaar5195e452005-08-19 20:32:47 +0000434#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
435#define VIMSPELLMAGICL 8
436#define VIMSPELLVERSION 50
437
438/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
439#define SN_REGION 0 /* <regionname> section */
440#define SN_CHARFLAGS 1 /* charflags section */
441#define SN_MIDWORD 2 /* <midword> section */
442#define SN_PREFCOND 3 /* <prefcond> section */
443#define SN_REP 4 /* REP items section */
444#define SN_SAL 5 /* SAL items section */
445#define SN_SOFO 6 /* soundfolding section */
446#define SN_MAP 7 /* MAP items section */
447#define SN_COMPOUND 8 /* compound words section */
448#define SN_SYLLABLE 9 /* syllable section */
Bram Moolenaar78622822005-08-23 21:00:13 +0000449#define SN_NOBREAK 10 /* NOBREAK section */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000450#define SN_END 255 /* end of sections */
451
452#define SNF_REQUIRED 1 /* <sectionflags>: required section */
453
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000454/* Result values. Lower number is accepted over higher one. */
455#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000456#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000457#define SP_RARE 1
458#define SP_LOCAL 2
459#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000460
Bram Moolenaar7887d882005-07-01 22:33:52 +0000461/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000462static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000463
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000464/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000465 * Information used when looking for suggestions.
466 */
467typedef struct suginfo_S
468{
469 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000470 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000471 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000472 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000473 char_u *su_badptr; /* start of bad word in line */
474 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000475 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000476 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
477 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
478 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000479} suginfo_T;
480
481/* One word suggestion. Used in "si_ga". */
482typedef struct suggest_S
483{
484 char_u *st_word; /* suggested word, allocated string */
485 int st_orglen; /* length of replaced text */
486 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000487 int st_altscore; /* used when st_score compares equal */
488 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000489 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000490} suggest_T;
491
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000492#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000493
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000494/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
495 * called the score may change, thus we need to keep more than what is
496 * displayed. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000497#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 50 ? 50 : (su)->su_maxcount)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000498
499/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
500 * of suggestions that are not going to be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000501#define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000502
503/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000504#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000505#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000506#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000507#define SCORE_RARE 180 /* rare word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000508#define SCORE_SWAP 90 /* swap two characters */
509#define SCORE_SWAP3 110 /* swap two characters in three */
510#define SCORE_REP 87 /* REP replacement */
511#define SCORE_SUBST 93 /* substitute a character */
512#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000513#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000514#define SCORE_DEL 94 /* delete a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000515#define SCORE_DELDUP 64 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000516#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000517#define SCORE_INS 96 /* insert a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000518#define SCORE_INSDUP 66 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000519#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000520#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000521
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000522#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000523#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
524 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000525
526#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000527#define SCORE_MAXMAX 999999 /* accept any score */
528
529/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000530 * Structure to store info for word matching.
531 */
532typedef struct matchinf_S
533{
534 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000535
536 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000537 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000538 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000539 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000540 char_u *mi_cend; /* char after what was used for
541 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000542
543 /* case-folded text */
544 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000545 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000546
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000547 /* for when checking word after a prefix */
548 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000549 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000550 int mi_prefcnt; /* number of entries at mi_prefarridx */
551 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000552#ifdef FEAT_MBYTE
553 int mi_cprefixlen; /* byte length of prefix in original
554 case */
555#else
556# define mi_cprefixlen mi_prefixlen /* it's the same value */
557#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000558
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000559 /* for when checking a compound word */
560 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000561 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
562 int mi_complen; /* nr of compound words used */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000563
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000564 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000565 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000566 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000567 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000568
569 /* for NOBREAK */
570 int mi_result2; /* "mi_resul" without following word */
571 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000572} matchinf_T;
573
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000574/*
575 * The tables used for recognizing word characters according to spelling.
576 * These are only used for the first 256 characters of 'encoding'.
577 */
578typedef struct spelltab_S
579{
580 char_u st_isw[256]; /* flags: is word char */
581 char_u st_isu[256]; /* flags: is uppercase char */
582 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000583 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000584} spelltab_T;
585
586static spelltab_T spelltab;
587static int did_set_spelltab;
588
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000589#define CF_WORD 0x01
590#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000591
592static void clear_spell_chartab __ARGS((spelltab_T *sp));
593static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000594static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
595static int spell_iswordp_nmw __ARGS((char_u *p));
596#ifdef FEAT_MBYTE
597static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
598#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000599static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000600
601/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000602 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000603 */
604typedef enum
605{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000606 STATE_START = 0, /* At start of node check for NUL bytes (goodword
607 * ends); if badword ends there is a match, otherwise
608 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000609 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000610 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000611 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
612 STATE_PLAIN, /* Use each byte of the node. */
613 STATE_DEL, /* Delete a byte from the bad word. */
614 STATE_INS, /* Insert a byte in the bad word. */
615 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000616 STATE_UNSWAP, /* Undo swap two characters. */
617 STATE_SWAP3, /* Swap two characters over three. */
618 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000619 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000620 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000621 STATE_REP_INI, /* Prepare for using REP items. */
622 STATE_REP, /* Use matching REP items from the .aff file. */
623 STATE_REP_UNDO, /* Undo a REP item replacement. */
624 STATE_FINAL /* End of this node. */
625} state_T;
626
627/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000628 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000629 */
630typedef struct trystate_S
631{
Bram Moolenaarea424162005-06-16 21:51:00 +0000632 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000633 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000634 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000635 short ts_curi; /* index in list of child nodes */
636 char_u ts_fidx; /* index in fword[], case-folded bad word */
637 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
638 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000639 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000640 * PFD_PREFIXTREE or PFD_NOPREFIX */
641 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000642#ifdef FEAT_MBYTE
643 char_u ts_tcharlen; /* number of bytes in tword character */
644 char_u ts_tcharidx; /* current byte index in tword character */
645 char_u ts_isdiff; /* DIFF_ values */
646 char_u ts_fcharstart; /* index in fword where badword char started */
647#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000648 char_u ts_prewordlen; /* length of word in "preword[]" */
649 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000650 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000651 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000652 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000653 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000654} trystate_T;
655
Bram Moolenaarea424162005-06-16 21:51:00 +0000656/* values for ts_isdiff */
657#define DIFF_NONE 0 /* no different byte (yet) */
658#define DIFF_YES 1 /* different byte found */
659#define DIFF_INSERT 2 /* inserting character */
660
Bram Moolenaard12a1322005-08-21 22:08:24 +0000661/* values for ts_flags */
662#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
663#define TSF_DIDSPLIT 2 /* tried split at this point */
664
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000665/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000666#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000667#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
668#define PFD_NOTSPECIAL 0xfd /* first value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000669
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000670/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000671#define FIND_FOLDWORD 0 /* find word case-folded */
672#define FIND_KEEPWORD 1 /* find keep-case word */
673#define FIND_PREFIX 2 /* find word after prefix */
674#define FIND_COMPOUND 3 /* find case-folded compound word */
675#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000676
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000677static slang_T *slang_alloc __ARGS((char_u *lang));
678static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000679static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000680static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000681static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000682static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req));
Bram Moolenaard12a1322005-08-21 22:08:24 +0000683static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000684static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000685static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000686static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000687static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000688static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000689static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000690static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000691static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000692static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000693static char_u *read_string __ARGS((FILE *fd, int cnt));
694static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
695static int read_charflags_section __ARGS((FILE *fd));
696static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
697static int read_rep_section __ARGS((FILE *fd, slang_T *slang));
698static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
699static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
700static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000701static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000702static int init_syl_tab __ARGS((slang_T *slang));
703static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000704static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
705static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000706#ifdef FEAT_MBYTE
707static int *mb_str2wide __ARGS((char_u *s));
708#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000709static idx_T read_tree __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, int startidx, int prefixtree, int maxprefcondnr));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000710static void clear_midword __ARGS((buf_T *buf));
711static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000712static int find_region __ARGS((char_u *rp, char_u *region));
713static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000714static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000715static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000716static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000717static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000718static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000719static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +0000720static void spell_find_suggest __ARGS((char_u *badptr, suginfo_T *su, int maxcount, int banbadword, int need_cap));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000721#ifdef FEAT_EVAL
722static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
723#endif
724static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
725static void spell_suggest_intern __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000726static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000727static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000728static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000729static void suggest_try_special __ARGS((suginfo_T *su));
730static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000731static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000732#ifdef FEAT_MBYTE
733static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
734#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000735static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000736static void score_comp_sal __ARGS((suginfo_T *su));
737static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000738static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000739static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000740static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000741static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000742static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000743static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000744static void add_banned __ARGS((suginfo_T *su, char_u *word));
745static int was_banned __ARGS((suginfo_T *su, char_u *word));
746static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000747static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000748static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000749static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
750static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
751static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000752#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000753static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000754#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000755static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000756static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000757static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000758static linenr_T dump_prefixes __ARGS((slang_T *slang, char_u *word, int round, int flags, linenr_T startlnum));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000759
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000760/*
761 * Use our own character-case definitions, because the current locale may
762 * differ from what the .spl file uses.
763 * These must not be called with negative number!
764 */
765#ifndef FEAT_MBYTE
766/* Non-multi-byte implementation. */
767# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
768# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
769# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
770#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000771# if defined(HAVE_WCHAR_H)
772# include <wchar.h> /* for towupper() and towlower() */
773# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000774/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
775 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
776 * the "w" library function for characters above 255 if available. */
777# ifdef HAVE_TOWLOWER
778# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
779 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
780# else
781# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
782 : (c) < 256 ? spelltab.st_fold[c] : (c))
783# endif
784
785# ifdef HAVE_TOWUPPER
786# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
787 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
788# else
789# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
790 : (c) < 256 ? spelltab.st_upper[c] : (c))
791# endif
792
793# ifdef HAVE_ISWUPPER
794# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
795 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
796# else
797# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000798 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000799# endif
800#endif
801
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000802
803static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000804static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000805static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000806static char *e_affname = N_("Affix name too long in %s line %d: %s");
807static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
808static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000809static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000810
811/*
812 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000813 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000814 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
815 * or when it's OK it remains unchanged.
816 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000817 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000818 * "capcol" is used to check for a Capitalised word after the end of a
819 * sentence. If it's zero then perform the check. Return the column where to
820 * check next, or -1 when no sentence end was found. If it's NULL then don't
821 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000822 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000823 * Returns the length of the word in bytes, also when it's OK, so that the
824 * caller can skip over the word.
825 */
826 int
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000827spell_check(wp, ptr, attrp, capcol)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000828 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000829 char_u *ptr;
830 int *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000831 int *capcol; /* column to check for Capital */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000832{
833 matchinf_T mi; /* Most things are put in "mi" so that it can
834 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000835 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000836 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000837 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000838 int lpi;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000839
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000840 /* A word never starts at a space or a control character. Return quickly
841 * then, skipping over the character. */
842 if (*ptr <= ' ')
843 return 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +0000844 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000845
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000846 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000847 * 0X99FF. But when a word character follows do check spelling to find
848 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000849 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000850 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000851 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
852 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000853 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000854 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000855 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000856 nrlen = mi.mi_end - ptr;
857 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000858 if (!spell_iswordp(mi.mi_end, wp->w_buffer))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000859 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000860
861 /* Try including the digits in the word. */
862 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000863 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000864 else
865 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000866
Bram Moolenaar0c405862005-06-22 22:26:26 +0000867 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000868 mi.mi_word = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000869 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000870 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000871 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000872 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000873 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000874 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000875
876 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
877 {
878 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000879 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000880 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +0000881 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000882 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000883 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000884 if (capcol != NULL)
885 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000886
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000887 /* We always use the characters up to the next non-word character,
888 * also for bad words. */
889 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000890
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000891 /* Check caps type later. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000892 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000893
Bram Moolenaar5195e452005-08-19 20:32:47 +0000894 /* case-fold the word with one non-word character, so that we can check
895 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000896 if (*mi.mi_fend != NUL)
897 mb_ptr_adv(mi.mi_fend);
898
899 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
900 MAXWLEN + 1);
901 mi.mi_fwordlen = STRLEN(mi.mi_fword);
902
903 /* The word is bad unless we recognize it. */
904 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +0000905 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000906
907 /*
908 * Loop over the languages specified in 'spelllang'.
909 * We check them all, because a matching word may be longer than an
910 * already found matching word.
911 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000912 for (lpi = 0; lpi < wp->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000913 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000914 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, lpi);
915
916 /* If reloading fails the language is still in the list but everything
917 * has been cleared. */
918 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
919 continue;
920
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000921 /* Check for a matching word in case-folded words. */
922 find_word(&mi, FIND_FOLDWORD);
923
924 /* Check for a matching word in keep-case words. */
925 find_word(&mi, FIND_KEEPWORD);
926
927 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000928 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +0000929
930 /* For a NOBREAK language, may want to use a word without a following
931 * word as a backup. */
932 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
933 && mi.mi_result2 != SP_BAD)
934 {
935 mi.mi_result = mi.mi_result2;
936 mi.mi_end = mi.mi_end2;
937 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000938 }
939
940 if (mi.mi_result != SP_OK)
941 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000942 /* If we found a number skip over it. Allows for "42nd". Do flag
943 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000944 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000945 {
946 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
947 return nrlen;
948 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000949
950 /* When we are at a non-word character there is no error, just
951 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000952 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000953 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000954 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
955 {
956 regmatch_T regmatch;
957
958 /* Check for end of sentence. */
959 regmatch.regprog = wp->w_buffer->b_cap_prog;
960 regmatch.rm_ic = FALSE;
961 if (vim_regexec(&regmatch, ptr, 0))
962 *capcol = (int)(regmatch.endp[0] - ptr);
963 }
964
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000965#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000966 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000967 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000968#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000969 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000970 }
Bram Moolenaar5195e452005-08-19 20:32:47 +0000971 else if (mi.mi_end == ptr)
972 /* Always include at least one character. Required for when there
973 * is a mixup in "midword". */
974 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +0000975 else if (mi.mi_result == SP_BAD
976 && LANGP_ENTRY(wp->w_buffer->b_langp, 0)->lp_slang->sl_nobreak)
977 {
978 char_u *p, *fp;
979 int save_result = mi.mi_result;
980
981 /* First language in 'spelllang' is NOBREAK. Find first position
982 * at which any word would be valid. */
983 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000984 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +0000985 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000986 p = mi.mi_word;
987 fp = mi.mi_fword;
988 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +0000989 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000990 mb_ptr_adv(p);
991 mb_ptr_adv(fp);
992 if (p >= mi.mi_end)
993 break;
994 mi.mi_compoff = fp - mi.mi_fword;
995 find_word(&mi, FIND_COMPOUND);
996 if (mi.mi_result != SP_BAD)
997 {
998 mi.mi_end = p;
999 break;
1000 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001001 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001002 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001003 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001004 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001005
1006 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1007 *attrp = highlight_attr[HLF_SPB];
1008 else if (mi.mi_result == SP_RARE)
1009 *attrp = highlight_attr[HLF_SPR];
1010 else
1011 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001012 }
1013
Bram Moolenaar5195e452005-08-19 20:32:47 +00001014 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1015 {
1016 /* Report SpellCap only when the word isn't badly spelled. */
1017 *attrp = highlight_attr[HLF_SPC];
1018 return wrongcaplen;
1019 }
1020
Bram Moolenaar51485f02005-06-04 21:55:20 +00001021 return (int)(mi.mi_end - ptr);
1022}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001023
Bram Moolenaar51485f02005-06-04 21:55:20 +00001024/*
1025 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001026 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1027 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1028 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1029 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001030 *
1031 * For a match mip->mi_result is updated.
1032 */
1033 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001034find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001035 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001036 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001037{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001038 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001039 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001040 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001041 int endidxcnt = 0;
1042 int len;
1043 int wlen = 0;
1044 int flen;
1045 int c;
1046 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001047 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001048#ifdef FEAT_MBYTE
1049 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001050#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001051 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001052 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001053 slang_T *slang = mip->mi_lp->lp_slang;
1054 unsigned flags;
1055 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001056 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001057 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001058 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001059 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001060
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001061 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001062 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001063 /* Check for word with matching case in keep-case tree. */
1064 ptr = mip->mi_word;
1065 flen = 9999; /* no case folding, always enough bytes */
1066 byts = slang->sl_kbyts;
1067 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001068
1069 if (mode == FIND_KEEPCOMPOUND)
1070 /* Skip over the previously found word(s). */
1071 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001072 }
1073 else
1074 {
1075 /* Check for case-folded in case-folded tree. */
1076 ptr = mip->mi_fword;
1077 flen = mip->mi_fwordlen; /* available case-folded bytes */
1078 byts = slang->sl_fbyts;
1079 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001080
1081 if (mode == FIND_PREFIX)
1082 {
1083 /* Skip over the prefix. */
1084 wlen = mip->mi_prefixlen;
1085 flen -= mip->mi_prefixlen;
1086 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001087 else if (mode == FIND_COMPOUND)
1088 {
1089 /* Skip over the previously found word(s). */
1090 wlen = mip->mi_compoff;
1091 flen -= mip->mi_compoff;
1092 }
1093
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001094 }
1095
Bram Moolenaar51485f02005-06-04 21:55:20 +00001096 if (byts == NULL)
1097 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001098
Bram Moolenaar51485f02005-06-04 21:55:20 +00001099 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001100 * Repeat advancing in the tree until:
1101 * - there is a byte that doesn't match,
1102 * - we reach the end of the tree,
1103 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001104 */
1105 for (;;)
1106 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001107 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001108 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001109
1110 len = byts[arridx++];
1111
1112 /* If the first possible byte is a zero the word could end here.
1113 * Remember this index, we first check for the longest word. */
1114 if (byts[arridx] == 0)
1115 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001116 if (endidxcnt == MAXWLEN)
1117 {
1118 /* Must be a corrupted spell file. */
1119 EMSG(_(e_format));
1120 return;
1121 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001122 endlen[endidxcnt] = wlen;
1123 endidx[endidxcnt++] = arridx++;
1124 --len;
1125
1126 /* Skip over the zeros, there can be several flag/region
1127 * combinations. */
1128 while (len > 0 && byts[arridx] == 0)
1129 {
1130 ++arridx;
1131 --len;
1132 }
1133 if (len == 0)
1134 break; /* no children, word must end here */
1135 }
1136
1137 /* Stop looking at end of the line. */
1138 if (ptr[wlen] == NUL)
1139 break;
1140
1141 /* Perform a binary search in the list of accepted bytes. */
1142 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001143 if (c == TAB) /* <Tab> is handled like <Space> */
1144 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001145 lo = arridx;
1146 hi = arridx + len - 1;
1147 while (lo < hi)
1148 {
1149 m = (lo + hi) / 2;
1150 if (byts[m] > c)
1151 hi = m - 1;
1152 else if (byts[m] < c)
1153 lo = m + 1;
1154 else
1155 {
1156 lo = hi = m;
1157 break;
1158 }
1159 }
1160
1161 /* Stop if there is no matching byte. */
1162 if (hi < lo || byts[lo] != c)
1163 break;
1164
1165 /* Continue at the child (if there is one). */
1166 arridx = idxs[lo];
1167 ++wlen;
1168 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001169
1170 /* One space in the good word may stand for several spaces in the
1171 * checked word. */
1172 if (c == ' ')
1173 {
1174 for (;;)
1175 {
1176 if (flen <= 0 && *mip->mi_fend != NUL)
1177 flen = fold_more(mip);
1178 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1179 break;
1180 ++wlen;
1181 --flen;
1182 }
1183 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001184 }
1185
1186 /*
1187 * Verify that one of the possible endings is valid. Try the longest
1188 * first.
1189 */
1190 while (endidxcnt > 0)
1191 {
1192 --endidxcnt;
1193 arridx = endidx[endidxcnt];
1194 wlen = endlen[endidxcnt];
1195
1196#ifdef FEAT_MBYTE
1197 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1198 continue; /* not at first byte of character */
1199#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001200 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001201 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001202 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001203 continue; /* next char is a word character */
1204 word_ends = FALSE;
1205 }
1206 else
1207 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001208 /* The prefix flag is before compound flags. Once a valid prefix flag
1209 * has been found we try compound flags. */
1210 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001211
1212#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001213 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001214 {
1215 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001216 * when folding case. This can be slow, take a shortcut when the
1217 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001218 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001219 if (STRNCMP(ptr, p, wlen) != 0)
1220 {
1221 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1222 mb_ptr_adv(p);
1223 wlen = p - mip->mi_word;
1224 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001225 }
1226#endif
1227
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001228 /* Check flags and region. For FIND_PREFIX check the condition and
1229 * prefix ID.
1230 * Repeat this if there are more flags/region alternatives until there
1231 * is a match. */
1232 res = SP_BAD;
1233 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1234 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001235 {
1236 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001237
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001238 /* For the fold-case tree check that the case of the checked word
1239 * matches with what the word in the tree requires.
1240 * For keep-case tree the case is always right. For prefixes we
1241 * don't bother to check. */
1242 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001243 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001244 if (mip->mi_cend != mip->mi_word + wlen)
1245 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001246 /* mi_capflags was set for a different word length, need
1247 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001248 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001249 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001250 }
1251
Bram Moolenaar0c405862005-06-22 22:26:26 +00001252 if (mip->mi_capflags == WF_KEEPCAP
1253 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001254 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001255 }
1256
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001257 /* When mode is FIND_PREFIX the word must support the prefix:
1258 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001259 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001260 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001261 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001262 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001263 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001264 mip->mi_word + mip->mi_cprefixlen, slang,
1265 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001266 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001267 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001268
1269 /* Use the WF_RARE flag for a rare prefix. */
1270 if (c & WF_RAREPFX)
1271 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001272 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001273 }
1274
Bram Moolenaar78622822005-08-23 21:00:13 +00001275 if (slang->sl_nobreak)
1276 {
1277 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1278 && (flags & WF_BANNED) == 0)
1279 {
1280 /* NOBREAK: found a valid following word. That's all we
1281 * need to know, so return. */
1282 mip->mi_result = SP_OK;
1283 break;
1284 }
1285 }
1286
1287 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1288 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001289 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00001290 /* If there is no flag or the word is shorter than
1291 * COMPOUNDMIN reject it quickly.
1292 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001293 * that's too short... Myspell compatibility requires this
1294 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001295 if (((unsigned)flags >> 24) == 0
1296 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001297 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001298#ifdef FEAT_MBYTE
1299 /* For multi-byte chars check character length against
1300 * COMPOUNDMIN. */
1301 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001302 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001303 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1304 wlen - mip->mi_compoff) < slang->sl_compminlen)
1305 continue;
1306#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001307
Bram Moolenaare52325c2005-08-22 22:54:29 +00001308 /* Limit the number of compound words to COMPOUNDMAX if no
1309 * maximum for syllables is specified. */
1310 if (!word_ends && mip->mi_complen + 2 > slang->sl_compmax
1311 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001312 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001313
Bram Moolenaard12a1322005-08-21 22:08:24 +00001314 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001315 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001316 ? slang->sl_compstartflags
1317 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001318 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001319 continue;
1320
Bram Moolenaare52325c2005-08-22 22:54:29 +00001321 if (mode == FIND_COMPOUND)
1322 {
1323 int capflags;
1324
1325 /* Need to check the caps type of the appended compound
1326 * word. */
1327#ifdef FEAT_MBYTE
1328 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1329 mip->mi_compoff) != 0)
1330 {
1331 /* case folding may have changed the length */
1332 p = mip->mi_word;
1333 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1334 mb_ptr_adv(p);
1335 }
1336 else
1337#endif
1338 p = mip->mi_word + mip->mi_compoff;
1339 capflags = captype(p, mip->mi_word + wlen);
1340 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1341 && (flags & WF_FIXCAP) != 0))
1342 continue;
1343
1344 if (capflags != WF_ALLCAP)
1345 {
1346 /* When the character before the word is a word
1347 * character we do not accept a Onecap word. We do
1348 * accept a no-caps word, even when the dictionary
1349 * word specifies ONECAP. */
1350 mb_ptr_back(mip->mi_word, p);
1351 if (spell_iswordp_nmw(p)
1352 ? capflags == WF_ONECAP
1353 : (flags & WF_ONECAP) != 0
1354 && capflags != WF_ONECAP)
1355 continue;
1356 }
1357 }
1358
Bram Moolenaar5195e452005-08-19 20:32:47 +00001359 /* If the word ends the sequence of compound flags of the
1360 * words must match with one of the COMPOUNDFLAGS items and
1361 * the number of syllables must not be too large. */
1362 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1363 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1364 if (word_ends)
1365 {
1366 char_u fword[MAXWLEN];
1367
1368 if (slang->sl_compsylmax < MAXWLEN)
1369 {
1370 /* "fword" is only needed for checking syllables. */
1371 if (ptr == mip->mi_word)
1372 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1373 else
1374 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1375 }
1376 if (!can_compound(slang, fword, mip->mi_compflags))
1377 continue;
1378 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001379 }
1380
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001381 /* Check NEEDCOMPOUND: can't use word without compounding. */
1382 else if (flags & WF_NEEDCOMP)
1383 continue;
1384
Bram Moolenaar78622822005-08-23 21:00:13 +00001385 nobreak_result = SP_OK;
1386
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001387 if (!word_ends)
1388 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001389 int save_result = mip->mi_result;
1390 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001391 langp_T *save_lp = mip->mi_lp;
1392 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001393
1394 /* Check that a valid word follows. If there is one and we
1395 * are compounding, it will set "mi_result", thus we are
1396 * always finished here. For NOBREAK we only check that a
1397 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001398 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001399 if (slang->sl_nobreak)
1400 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001401
1402 /* Find following word in case-folded tree. */
1403 mip->mi_compoff = endlen[endidxcnt];
1404#ifdef FEAT_MBYTE
1405 if (has_mbyte && mode == FIND_KEEPWORD)
1406 {
1407 /* Compute byte length in case-folded word from "wlen":
1408 * byte length in keep-case word. Length may change when
1409 * folding case. This can be slow, take a shortcut when
1410 * the case-folded word is equal to the keep-case word. */
1411 p = mip->mi_fword;
1412 if (STRNCMP(ptr, p, wlen) != 0)
1413 {
1414 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1415 mb_ptr_adv(p);
1416 mip->mi_compoff = p - mip->mi_fword;
1417 }
1418 }
1419#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001420 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001421 ++mip->mi_complen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001422
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001423 /* For NOBREAK we need to try all NOBREAK languages, at least
1424 * to find the ".add" file(s). */
1425 for (lpi = 0; lpi < mip->mi_buf->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001426 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001427 if (slang->sl_nobreak)
1428 {
1429 mip->mi_lp = LANGP_ENTRY(mip->mi_buf->b_langp, lpi);
1430 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1431 || !mip->mi_lp->lp_slang->sl_nobreak)
1432 continue;
1433 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001434
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001435 find_word(mip, FIND_COMPOUND);
1436
1437 /* When NOBREAK any word that matches is OK. Otherwise we
1438 * need to find the longest match, thus try with keep-case
1439 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001440 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1441 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001442 /* Find following word in keep-case tree. */
1443 mip->mi_compoff = wlen;
1444 find_word(mip, FIND_KEEPCOMPOUND);
1445
1446 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1447 {
1448 /* Check for following word with prefix. */
1449 mip->mi_compoff = c;
1450 find_prefix(mip, FIND_COMPOUND);
1451 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001452 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001453
1454 if (!slang->sl_nobreak)
1455 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001456 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001457 --mip->mi_complen;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001458 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001459
Bram Moolenaar78622822005-08-23 21:00:13 +00001460 if (slang->sl_nobreak)
1461 {
1462 nobreak_result = mip->mi_result;
1463 mip->mi_result = save_result;
1464 mip->mi_end = save_end;
1465 }
1466 else
1467 {
1468 if (mip->mi_result == SP_OK)
1469 break;
1470 continue;
1471 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001472 }
1473
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001474 if (flags & WF_BANNED)
1475 res = SP_BANNED;
1476 else if (flags & WF_REGION)
1477 {
1478 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001479 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001480 res = SP_OK;
1481 else
1482 res = SP_LOCAL;
1483 }
1484 else if (flags & WF_RARE)
1485 res = SP_RARE;
1486 else
1487 res = SP_OK;
1488
Bram Moolenaar78622822005-08-23 21:00:13 +00001489 /* Always use the longest match and the best result. For NOBREAK
1490 * we separately keep the longest match without a following good
1491 * word as a fall-back. */
1492 if (nobreak_result == SP_BAD)
1493 {
1494 if (mip->mi_result2 > res)
1495 {
1496 mip->mi_result2 = res;
1497 mip->mi_end2 = mip->mi_word + wlen;
1498 }
1499 else if (mip->mi_result2 == res
1500 && mip->mi_end2 < mip->mi_word + wlen)
1501 mip->mi_end2 = mip->mi_word + wlen;
1502 }
1503 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001504 {
1505 mip->mi_result = res;
1506 mip->mi_end = mip->mi_word + wlen;
1507 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001508 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001509 mip->mi_end = mip->mi_word + wlen;
1510
Bram Moolenaar78622822005-08-23 21:00:13 +00001511 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001512 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001513 }
1514
Bram Moolenaar78622822005-08-23 21:00:13 +00001515 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001516 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001517 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001518}
1519
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001520/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00001521 * Return TRUE if "flags" is a valid sequence of compound flags and
1522 * "word[len]" does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001523 */
1524 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001525can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001526 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001527 char_u *word;
1528 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001529{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001530 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001531#ifdef FEAT_MBYTE
1532 char_u uflags[MAXWLEN * 2];
1533 int i;
1534#endif
1535 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001536
1537 if (slang->sl_compprog == NULL)
1538 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001539#ifdef FEAT_MBYTE
1540 if (enc_utf8)
1541 {
1542 /* Need to convert the single byte flags to utf8 characters. */
1543 p = uflags;
1544 for (i = 0; flags[i] != NUL; ++i)
1545 p += mb_char2bytes(flags[i], p);
1546 *p = NUL;
1547 p = uflags;
1548 }
1549 else
1550#endif
1551 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001552 regmatch.regprog = slang->sl_compprog;
1553 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001554 if (!vim_regexec(&regmatch, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001555 return FALSE;
1556
Bram Moolenaare52325c2005-08-22 22:54:29 +00001557 /* Count the number of syllables. This may be slow, do it last. If there
1558 * are too many syllables AND the number of compound words is above
1559 * COMPOUNDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001560 if (slang->sl_compsylmax < MAXWLEN
1561 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001562 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001563 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001564}
1565
1566/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001567 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1568 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001569 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001570 */
1571 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001572valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001573 int totprefcnt; /* nr of prefix IDs */
1574 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001575 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001576 char_u *word;
1577 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001578 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001579{
1580 int prefcnt;
1581 int pidx;
1582 regprog_T *rp;
1583 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001584 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001585
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001586 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001587 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1588 {
1589 pidx = slang->sl_pidxs[arridx + prefcnt];
1590
1591 /* Check the prefix ID. */
1592 if (prefid != (pidx & 0xff))
1593 continue;
1594
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001595 /* Check if the prefix doesn't combine and the word already has a
1596 * suffix. */
1597 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1598 continue;
1599
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001600 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001601 * stored in the two bytes above the prefix ID byte. */
1602 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001603 if (rp != NULL)
1604 {
1605 regmatch.regprog = rp;
1606 regmatch.rm_ic = FALSE;
1607 if (!vim_regexec(&regmatch, word, 0))
1608 continue;
1609 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001610 else if (cond_req)
1611 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001612
Bram Moolenaar53805d12005-08-01 07:08:33 +00001613 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001614 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001615 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001616 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001617}
1618
1619/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001620 * Check if the word at "mip->mi_word" has a matching prefix.
1621 * If it does, then check the following word.
1622 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001623 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1624 * prefix in a compound word.
1625 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001626 * For a match mip->mi_result is updated.
1627 */
1628 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001629find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001630 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001631 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001632{
1633 idx_T arridx = 0;
1634 int len;
1635 int wlen = 0;
1636 int flen;
1637 int c;
1638 char_u *ptr;
1639 idx_T lo, hi, m;
1640 slang_T *slang = mip->mi_lp->lp_slang;
1641 char_u *byts;
1642 idx_T *idxs;
1643
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001644 byts = slang->sl_pbyts;
1645 if (byts == NULL)
1646 return; /* array is empty */
1647
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001648 /* We use the case-folded word here, since prefixes are always
1649 * case-folded. */
1650 ptr = mip->mi_fword;
1651 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001652 if (mode == FIND_COMPOUND)
1653 {
1654 /* Skip over the previously found word(s). */
1655 ptr += mip->mi_compoff;
1656 flen -= mip->mi_compoff;
1657 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001658 idxs = slang->sl_pidxs;
1659
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001660 /*
1661 * Repeat advancing in the tree until:
1662 * - there is a byte that doesn't match,
1663 * - we reach the end of the tree,
1664 * - or we reach the end of the line.
1665 */
1666 for (;;)
1667 {
1668 if (flen == 0 && *mip->mi_fend != NUL)
1669 flen = fold_more(mip);
1670
1671 len = byts[arridx++];
1672
1673 /* If the first possible byte is a zero the prefix could end here.
1674 * Check if the following word matches and supports the prefix. */
1675 if (byts[arridx] == 0)
1676 {
1677 /* There can be several prefixes with different conditions. We
1678 * try them all, since we don't know which one will give the
1679 * longest match. The word is the same each time, pass the list
1680 * of possible prefixes to find_word(). */
1681 mip->mi_prefarridx = arridx;
1682 mip->mi_prefcnt = len;
1683 while (len > 0 && byts[arridx] == 0)
1684 {
1685 ++arridx;
1686 --len;
1687 }
1688 mip->mi_prefcnt -= len;
1689
1690 /* Find the word that comes after the prefix. */
1691 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001692 if (mode == FIND_COMPOUND)
1693 /* Skip over the previously found word(s). */
1694 mip->mi_prefixlen += mip->mi_compoff;
1695
Bram Moolenaar53805d12005-08-01 07:08:33 +00001696#ifdef FEAT_MBYTE
1697 if (has_mbyte)
1698 {
1699 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001700 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1701 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001702 }
1703 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001704 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001705#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001706 find_word(mip, FIND_PREFIX);
1707
1708
1709 if (len == 0)
1710 break; /* no children, word must end here */
1711 }
1712
1713 /* Stop looking at end of the line. */
1714 if (ptr[wlen] == NUL)
1715 break;
1716
1717 /* Perform a binary search in the list of accepted bytes. */
1718 c = ptr[wlen];
1719 lo = arridx;
1720 hi = arridx + len - 1;
1721 while (lo < hi)
1722 {
1723 m = (lo + hi) / 2;
1724 if (byts[m] > c)
1725 hi = m - 1;
1726 else if (byts[m] < c)
1727 lo = m + 1;
1728 else
1729 {
1730 lo = hi = m;
1731 break;
1732 }
1733 }
1734
1735 /* Stop if there is no matching byte. */
1736 if (hi < lo || byts[lo] != c)
1737 break;
1738
1739 /* Continue at the child (if there is one). */
1740 arridx = idxs[lo];
1741 ++wlen;
1742 --flen;
1743 }
1744}
1745
1746/*
1747 * Need to fold at least one more character. Do until next non-word character
1748 * for efficiency.
1749 * Return the length of the folded chars in bytes.
1750 */
1751 static int
1752fold_more(mip)
1753 matchinf_T *mip;
1754{
1755 int flen;
1756 char_u *p;
1757
1758 p = mip->mi_fend;
1759 do
1760 {
1761 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001762 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001763
1764 /* Include the non-word character so that we can check for the
1765 * word end. */
1766 if (*mip->mi_fend != NUL)
1767 mb_ptr_adv(mip->mi_fend);
1768
1769 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1770 mip->mi_fword + mip->mi_fwordlen,
1771 MAXWLEN - mip->mi_fwordlen);
1772 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1773 mip->mi_fwordlen += flen;
1774 return flen;
1775}
1776
1777/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001778 * Check case flags for a word. Return TRUE if the word has the requested
1779 * case.
1780 */
1781 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001782spell_valid_case(wordflags, treeflags)
1783 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001784 int treeflags; /* flags for the word in the spell tree */
1785{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001786 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001787 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001788 && ((treeflags & WF_ONECAP) == 0
1789 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001790}
1791
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001792/*
1793 * Return TRUE if spell checking is not enabled.
1794 */
1795 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00001796no_spell_checking(wp)
1797 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001798{
Bram Moolenaar95529562005-08-25 21:21:38 +00001799 if (!wp->w_p_spell || *wp->w_buffer->b_p_spl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001800 {
1801 EMSG(_("E756: Spell checking is not enabled"));
1802 return TRUE;
1803 }
1804 return FALSE;
1805}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001806
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001807/*
1808 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001809 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
1810 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00001811 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
1812 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00001813 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001814 */
1815 int
Bram Moolenaar95529562005-08-25 21:21:38 +00001816spell_move_to(wp, dir, allwords, curline, attrp)
1817 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001818 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001819 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001820 int curline;
Bram Moolenaar95529562005-08-25 21:21:38 +00001821 int *attrp; /* return: attributes of bad word or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001822{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001823 linenr_T lnum;
1824 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001825 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001826 char_u *line;
1827 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001828 char_u *endp;
1829 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001830 int len;
Bram Moolenaar95529562005-08-25 21:21:38 +00001831 int has_syntax = syntax_present(wp->w_buffer);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001832 int col;
1833 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001834 char_u *buf = NULL;
1835 int buflen = 0;
1836 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001837 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001838 int found_one = FALSE;
1839 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001840
Bram Moolenaar95529562005-08-25 21:21:38 +00001841 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00001842 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001843
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001844 /*
1845 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001846 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001847 *
1848 * When searching backwards, we continue in the line to find the last
1849 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001850 *
1851 * We concatenate the start of the next line, so that wrapped words work
1852 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1853 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001854 */
Bram Moolenaar95529562005-08-25 21:21:38 +00001855 lnum = wp->w_cursor.lnum;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001856 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001857
1858 while (!got_int)
1859 {
Bram Moolenaar95529562005-08-25 21:21:38 +00001860 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001861
Bram Moolenaar0c405862005-06-22 22:26:26 +00001862 len = STRLEN(line);
1863 if (buflen < len + MAXWLEN + 2)
1864 {
1865 vim_free(buf);
1866 buflen = len + MAXWLEN + 2;
1867 buf = alloc(buflen);
1868 if (buf == NULL)
1869 break;
1870 }
1871
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001872 /* In first line check first word for Capital. */
1873 if (lnum == 1)
1874 capcol = 0;
1875
1876 /* For checking first word with a capital skip white space. */
1877 if (capcol == 0)
1878 capcol = skipwhite(line) - line;
1879
Bram Moolenaar0c405862005-06-22 22:26:26 +00001880 /* Copy the line into "buf" and append the start of the next line if
1881 * possible. */
1882 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00001883 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001884 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1885
1886 p = buf + skip;
1887 endp = buf + len;
1888 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001889 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001890 /* When searching backward don't search after the cursor. Unless
1891 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001892 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00001893 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001894 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00001895 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001896 break;
1897
1898 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001899 attr = 0;
Bram Moolenaar95529562005-08-25 21:21:38 +00001900 len = spell_check(wp, p, &attr, &capcol);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001901
1902 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001903 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001904 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001905 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001906 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001907 found_one = TRUE;
1908
Bram Moolenaar51485f02005-06-04 21:55:20 +00001909 /* When searching forward only accept a bad word after
1910 * the cursor. */
1911 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001912 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00001913 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001914 && (wrapped
1915 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00001916 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001917 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001918 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001919 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001920 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001921 col = p - buf;
Bram Moolenaar95529562005-08-25 21:21:38 +00001922 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar51485f02005-06-04 21:55:20 +00001923 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001924 }
1925 else
1926 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001927
Bram Moolenaar51485f02005-06-04 21:55:20 +00001928 if (can_spell)
1929 {
1930 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001931 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001932#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001933 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001934#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001935 if (dir == FORWARD)
1936 {
1937 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00001938 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001939 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00001940 if (attrp != NULL)
1941 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001942 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001943 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001944 else if (curline)
1945 /* Insert mode completion: put cursor after
1946 * the bad word. */
1947 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001948 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001949 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001950 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001951 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001952 }
1953
Bram Moolenaar51485f02005-06-04 21:55:20 +00001954 /* advance to character after the word */
1955 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001956 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001957 }
1958
Bram Moolenaar5195e452005-08-19 20:32:47 +00001959 if (dir == BACKWARD && found_pos.lnum != 0)
1960 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001961 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00001962 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001963 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00001964 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001965 }
1966
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001967 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001968 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001969
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001970 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001971 if (dir == BACKWARD)
1972 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001973 /* If we are back at the starting line and searched it again there
1974 * is no match, give up. */
1975 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001976 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001977
1978 if (lnum > 1)
1979 --lnum;
1980 else if (!p_ws)
1981 break; /* at first line and 'nowrapscan' */
1982 else
1983 {
1984 /* Wrap around to the end of the buffer. May search the
1985 * starting line again and accept the last match. */
1986 lnum = wp->w_buffer->b_ml.ml_line_count;
1987 wrapped = TRUE;
1988 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001989 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001990 }
1991 else
1992 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001993 if (lnum < wp->w_buffer->b_ml.ml_line_count)
1994 ++lnum;
1995 else if (!p_ws)
1996 break; /* at first line and 'nowrapscan' */
1997 else
1998 {
1999 /* Wrap around to the start of the buffer. May search the
2000 * starting line again and accept the first match. */
2001 lnum = 1;
2002 wrapped = TRUE;
2003 }
2004
2005 /* If we are back at the starting line and there is no match then
2006 * give up. */
2007 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002008 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002009
2010 /* Skip the characters at the start of the next line that were
2011 * included in a match crossing line boundaries. */
2012 if (attr == 0)
2013 skip = p - endp;
2014 else
2015 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002016
2017 /* Capscol skips over the inserted space. */
2018 --capcol;
2019
2020 /* But after empty line check first word in next line */
2021 if (*skipwhite(line) == NUL)
2022 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002023 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002024
2025 line_breakcheck();
2026 }
2027
Bram Moolenaar0c405862005-06-22 22:26:26 +00002028 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002029 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002030}
2031
2032/*
2033 * For spell checking: concatenate the start of the following line "line" into
2034 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
2035 */
2036 void
2037spell_cat_line(buf, line, maxlen)
2038 char_u *buf;
2039 char_u *line;
2040 int maxlen;
2041{
2042 char_u *p;
2043 int n;
2044
2045 p = skipwhite(line);
2046 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2047 p = skipwhite(p + 1);
2048
2049 if (*p != NUL)
2050 {
2051 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002052 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002053 n = p - line;
2054 if (n >= maxlen)
2055 n = maxlen - 1;
2056 vim_memset(buf + 1, ' ', n);
2057 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002058}
2059
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002060typedef struct spelload_S
2061{
2062 char_u sl_lang[MAXWLEN + 1]; /* language name */
2063 slang_T *sl_slang; /* resulting slang_T struct */
2064 int sl_nobreak; /* NOBREAK language found */
2065} spelload_T;
2066
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002067/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002068 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002069 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002070 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002071 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002072spell_load_lang(lang)
2073 char_u *lang;
2074{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002075 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002076 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002077 spelload_T sl;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002078
Bram Moolenaarb765d632005-06-07 21:00:02 +00002079 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002080 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002081 STRCPY(sl.sl_lang, lang);
2082 sl.sl_slang = NULL;
2083 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002084
Bram Moolenaarb765d632005-06-07 21:00:02 +00002085 /*
2086 * Find the first spell file for "lang" in 'runtimepath' and load it.
2087 */
2088 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2089 "spell/%s.%s.spl", lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002090 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002091
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002092 if (r == FAIL && *sl.sl_lang != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002093 {
2094 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002095 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002096 "spell/%s.ascii.spl", lang);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002097 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002098 }
2099
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002100 if (r == FAIL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002101 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2102 lang, spell_enc(), lang);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002103 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002104 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002105 /* At least one file was loaded, now load all the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002106 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002107 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002108 }
2109}
2110
2111/*
2112 * Return the encoding used for spell checking: Use 'encoding', except that we
2113 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2114 */
2115 static char_u *
2116spell_enc()
2117{
2118
2119#ifdef FEAT_MBYTE
2120 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2121 return p_enc;
2122#endif
2123 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002124}
2125
2126/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002127 * Get the name of the .spl file for the internal wordlist into
2128 * "fname[MAXPATHL]".
2129 */
2130 static void
2131int_wordlist_spl(fname)
2132 char_u *fname;
2133{
2134 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2135 int_wordlist, spell_enc());
2136}
2137
2138/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002139 * Allocate a new slang_T.
2140 * Caller must fill "sl_next".
2141 */
2142 static slang_T *
2143slang_alloc(lang)
2144 char_u *lang;
2145{
2146 slang_T *lp;
2147
Bram Moolenaar51485f02005-06-04 21:55:20 +00002148 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002149 if (lp != NULL)
2150 {
2151 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002152 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002153 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002154 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002155 }
2156 return lp;
2157}
2158
2159/*
2160 * Free the contents of an slang_T and the structure itself.
2161 */
2162 static void
2163slang_free(lp)
2164 slang_T *lp;
2165{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002166 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002167 vim_free(lp->sl_fname);
2168 slang_clear(lp);
2169 vim_free(lp);
2170}
2171
2172/*
2173 * Clear an slang_T so that the file can be reloaded.
2174 */
2175 static void
2176slang_clear(lp)
2177 slang_T *lp;
2178{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002179 garray_T *gap;
2180 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002181 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002182 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002183
Bram Moolenaar51485f02005-06-04 21:55:20 +00002184 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002185 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002186 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002187 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002188 vim_free(lp->sl_pbyts);
2189 lp->sl_pbyts = NULL;
2190
Bram Moolenaar51485f02005-06-04 21:55:20 +00002191 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002192 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002193 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002194 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002195 vim_free(lp->sl_pidxs);
2196 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002197
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002198 gap = &lp->sl_rep;
2199 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002200 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002201 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2202 vim_free(ftp->ft_from);
2203 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002204 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002205 ga_clear(gap);
2206
2207 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002208 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002209 {
2210 /* "ga_len" is set to 1 without adding an item for latin1 */
2211 if (gap->ga_data != NULL)
2212 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2213 for (i = 0; i < gap->ga_len; ++i)
2214 vim_free(((int **)gap->ga_data)[i]);
2215 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002216 else
2217 /* SAL items: free salitem_T items */
2218 while (gap->ga_len > 0)
2219 {
2220 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2221 vim_free(smp->sm_lead);
2222 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2223 vim_free(smp->sm_to);
2224#ifdef FEAT_MBYTE
2225 vim_free(smp->sm_lead_w);
2226 vim_free(smp->sm_oneof_w);
2227 vim_free(smp->sm_to_w);
2228#endif
2229 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002230 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002231
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002232 for (i = 0; i < lp->sl_prefixcnt; ++i)
2233 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002234 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002235 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002236 lp->sl_prefprog = NULL;
2237
2238 vim_free(lp->sl_midword);
2239 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002240
Bram Moolenaar5195e452005-08-19 20:32:47 +00002241 vim_free(lp->sl_compprog);
2242 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002243 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002244 lp->sl_compprog = NULL;
2245 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002246 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002247
2248 vim_free(lp->sl_syllable);
2249 lp->sl_syllable = NULL;
2250 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002251
Bram Moolenaarea424162005-06-16 21:51:00 +00002252#ifdef FEAT_MBYTE
2253 {
2254 int todo = lp->sl_map_hash.ht_used;
2255 hashitem_T *hi;
2256
2257 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
2258 if (!HASHITEM_EMPTY(hi))
2259 {
2260 --todo;
2261 vim_free(hi->hi_key);
2262 }
2263 }
2264 hash_clear(&lp->sl_map_hash);
2265#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002266
2267 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002268 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002269 lp->sl_compsylmax = MAXWLEN;
2270 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002271}
2272
2273/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002274 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002275 * Invoked through do_in_runtimepath().
2276 */
2277 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002278spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002279 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002280 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002281{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002282 spelload_T *slp = (spelload_T *)cookie;
2283 slang_T *slang;
2284
2285 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2286 if (slang != NULL)
2287 {
2288 /* When a previously loaded file has NOBREAK also use it for the
2289 * ".add" files. */
2290 if (slp->sl_nobreak && slang->sl_add)
2291 slang->sl_nobreak = TRUE;
2292 else if (slang->sl_nobreak)
2293 slp->sl_nobreak = TRUE;
2294
2295 slp->sl_slang = slang;
2296 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002297}
2298
2299/*
2300 * Load one spell file and store the info into a slang_T.
2301 *
2302 * This is invoked in two ways:
2303 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2304 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2305 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2306 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002307 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002308 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002309 static slang_T *
2310spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002311 char_u *fname;
2312 char_u *lang;
2313 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002314 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002315{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002316 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002317 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002318 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002319 char_u *bp;
2320 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002321 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002322 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002323 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002324 int round;
2325 char_u *save_sourcing_name = sourcing_name;
2326 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002327 slang_T *lp = NULL;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002328 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002329 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002330 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002331
Bram Moolenaarb765d632005-06-07 21:00:02 +00002332 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002333 if (fd == NULL)
2334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002335 if (!silent)
2336 EMSG2(_(e_notopen), fname);
2337 else if (p_verbose > 2)
2338 {
2339 verbose_enter();
2340 smsg((char_u *)e_notopen, fname);
2341 verbose_leave();
2342 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002343 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002344 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002345 if (p_verbose > 2)
2346 {
2347 verbose_enter();
2348 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2349 verbose_leave();
2350 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002351
Bram Moolenaarb765d632005-06-07 21:00:02 +00002352 if (old_lp == NULL)
2353 {
2354 lp = slang_alloc(lang);
2355 if (lp == NULL)
2356 goto endFAIL;
2357
2358 /* Remember the file name, used to reload the file when it's updated. */
2359 lp->sl_fname = vim_strsave(fname);
2360 if (lp->sl_fname == NULL)
2361 goto endFAIL;
2362
2363 /* Check for .add.spl. */
2364 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2365 }
2366 else
2367 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002368
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002369 /* Set sourcing_name, so that error messages mention the file name. */
2370 sourcing_name = fname;
2371 sourcing_lnum = 0;
2372
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002373 /* <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002374 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002375 for (i = 0; i < VIMSPELLMAGICL; ++i)
2376 buf[i] = getc(fd); /* <fileID> */
2377 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2378 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002379 EMSG(_("E757: This does not look like a spell file"));
2380 goto endFAIL;
2381 }
2382 c = getc(fd); /* <versionnr> */
2383 if (c < VIMSPELLVERSION)
2384 {
2385 EMSG(_("E771: Old spell file, needs to be updated"));
2386 goto endFAIL;
2387 }
2388 else if (c > VIMSPELLVERSION)
2389 {
2390 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002391 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002392 }
2393
Bram Moolenaar5195e452005-08-19 20:32:47 +00002394
2395 /*
2396 * <SECTIONS>: <section> ... <sectionend>
2397 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2398 */
2399 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002400 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002401 n = getc(fd); /* <sectionID> or <sectionend> */
2402 if (n == SN_END)
2403 break;
2404 c = getc(fd); /* <sectionflags> */
2405 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2406 /* <sectionlen> */
2407 if (len < 0)
2408 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002409
Bram Moolenaar5195e452005-08-19 20:32:47 +00002410 res = 0;
2411 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002412 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002413 case SN_REGION:
2414 res = read_region_section(fd, lp, len);
2415 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002416
Bram Moolenaar5195e452005-08-19 20:32:47 +00002417 case SN_CHARFLAGS:
2418 res = read_charflags_section(fd);
2419 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002420
Bram Moolenaar5195e452005-08-19 20:32:47 +00002421 case SN_MIDWORD:
2422 lp->sl_midword = read_string(fd, len); /* <midword> */
2423 if (lp->sl_midword == NULL)
2424 goto endFAIL;
2425 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002426
Bram Moolenaar5195e452005-08-19 20:32:47 +00002427 case SN_PREFCOND:
2428 res = read_prefcond_section(fd, lp);
2429 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002430
Bram Moolenaar5195e452005-08-19 20:32:47 +00002431 case SN_REP:
2432 res = read_rep_section(fd, lp);
2433 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002434
Bram Moolenaar5195e452005-08-19 20:32:47 +00002435 case SN_SAL:
2436 res = read_sal_section(fd, lp);
2437 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002438
Bram Moolenaar5195e452005-08-19 20:32:47 +00002439 case SN_SOFO:
2440 res = read_sofo_section(fd, lp);
2441 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002442
Bram Moolenaar5195e452005-08-19 20:32:47 +00002443 case SN_MAP:
2444 p = read_string(fd, len); /* <mapstr> */
2445 if (p == NULL)
2446 goto endFAIL;
2447 set_map_str(lp, p);
2448 vim_free(p);
2449 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002450
Bram Moolenaar5195e452005-08-19 20:32:47 +00002451 case SN_COMPOUND:
2452 res = read_compound(fd, lp, len);
2453 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002454
Bram Moolenaar78622822005-08-23 21:00:13 +00002455 case SN_NOBREAK:
2456 lp->sl_nobreak = TRUE;
2457 break;
2458
Bram Moolenaar5195e452005-08-19 20:32:47 +00002459 case SN_SYLLABLE:
2460 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2461 if (lp->sl_syllable == NULL)
2462 goto endFAIL;
2463 if (init_syl_tab(lp) == FAIL)
2464 goto endFAIL;
2465 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002466
Bram Moolenaar5195e452005-08-19 20:32:47 +00002467 default:
2468 /* Unsupported section. When it's required give an error
2469 * message. When it's not required skip the contents. */
2470 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002471 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002472 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002473 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002474 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002475 while (--len >= 0)
2476 if (getc(fd) < 0)
2477 goto truncerr;
2478 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002479 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002480 if (res == SP_FORMERROR)
2481 {
2482formerr:
2483 EMSG(_(e_format));
2484 goto endFAIL;
2485 }
2486 if (res == SP_TRUNCERROR)
2487 {
2488truncerr:
2489 EMSG(_(e_spell_trunc));
2490 goto endFAIL;
2491 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002492 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002493 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002494 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002495
Bram Moolenaar51485f02005-06-04 21:55:20 +00002496 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002497 * round 2: <KWORDTREE>
2498 * round 3: <PREFIXTREE> */
2499 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002500 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002501 /* The tree size was computed when writing the file, so that we can
2502 * allocate it as one long block. <nodecount> */
2503 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2504 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002505 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002506 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002507 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002508 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002509 bp = lalloc((long_u)len, TRUE);
2510 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002511 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002512 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002513 lp->sl_fbyts = bp;
2514 else if (round == 2)
2515 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002516 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002517 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002518
2519 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002520 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2521 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002522 goto endFAIL;
2523 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002524 lp->sl_fidxs = ip;
2525 else if (round == 2)
2526 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002527 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002528 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002529
2530 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002531 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002532 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002533 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002534 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002535 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002536 }
2537 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002538
Bram Moolenaarb765d632005-06-07 21:00:02 +00002539 /* For a new file link it in the list of spell files. */
2540 if (old_lp == NULL)
2541 {
2542 lp->sl_next = first_lang;
2543 first_lang = lp;
2544 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002545
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002546 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002547
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002548endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002549 if (lang != NULL)
2550 /* truncating the name signals the error to spell_load_lang() */
2551 *lang = NUL;
2552 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002553 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002554 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002555
2556endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002557 if (fd != NULL)
2558 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002559 sourcing_name = save_sourcing_name;
2560 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002561
2562 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002563}
2564
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002565/*
2566 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002567 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002568 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002569 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2570 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002571 */
2572 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002573read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002574 FILE *fd;
2575 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002576 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002577{
2578 int cnt = 0;
2579 int i;
2580 char_u *str;
2581
2582 /* read the length bytes, MSB first */
2583 for (i = 0; i < cnt_bytes; ++i)
2584 cnt = (cnt << 8) + getc(fd);
2585 if (cnt < 0)
2586 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002587 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002588 return NULL;
2589 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002590 *cntp = cnt;
2591 if (cnt == 0)
2592 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002593
Bram Moolenaar5195e452005-08-19 20:32:47 +00002594 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002595 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002596 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002597 return str;
2598}
2599
Bram Moolenaar7887d882005-07-01 22:33:52 +00002600/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002601 * Read a string of length "cnt" from "fd" into allocated memory.
2602 * Returns NULL when out of memory.
2603 */
2604 static char_u *
2605read_string(fd, cnt)
2606 FILE *fd;
2607 int cnt;
2608{
2609 char_u *str;
2610 int i;
2611
2612 /* allocate memory */
2613 str = alloc((unsigned)cnt + 1);
2614 if (str != NULL)
2615 {
2616 /* Read the string. Doesn't check for truncated file. */
2617 for (i = 0; i < cnt; ++i)
2618 str[i] = getc(fd);
2619 str[i] = NUL;
2620 }
2621 return str;
2622}
2623
2624/*
2625 * Read SN_REGION: <regionname> ...
2626 * Return SP_*ERROR flags.
2627 */
2628 static int
2629read_region_section(fd, lp, len)
2630 FILE *fd;
2631 slang_T *lp;
2632 int len;
2633{
2634 int i;
2635
2636 if (len > 16)
2637 return SP_FORMERROR;
2638 for (i = 0; i < len; ++i)
2639 lp->sl_regions[i] = getc(fd); /* <regionname> */
2640 lp->sl_regions[len] = NUL;
2641 return 0;
2642}
2643
2644/*
2645 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2646 * <folcharslen> <folchars>
2647 * Return SP_*ERROR flags.
2648 */
2649 static int
2650read_charflags_section(fd)
2651 FILE *fd;
2652{
2653 char_u *flags;
2654 char_u *fol;
2655 int flagslen, follen;
2656
2657 /* <charflagslen> <charflags> */
2658 flags = read_cnt_string(fd, 1, &flagslen);
2659 if (flagslen < 0)
2660 return flagslen;
2661
2662 /* <folcharslen> <folchars> */
2663 fol = read_cnt_string(fd, 2, &follen);
2664 if (follen < 0)
2665 {
2666 vim_free(flags);
2667 return follen;
2668 }
2669
2670 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
2671 if (flags != NULL && fol != NULL)
2672 set_spell_charflags(flags, flagslen, fol);
2673
2674 vim_free(flags);
2675 vim_free(fol);
2676
2677 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
2678 if ((flags == NULL) != (fol == NULL))
2679 return SP_FORMERROR;
2680 return 0;
2681}
2682
2683/*
2684 * Read SN_PREFCOND section.
2685 * Return SP_*ERROR flags.
2686 */
2687 static int
2688read_prefcond_section(fd, lp)
2689 FILE *fd;
2690 slang_T *lp;
2691{
2692 int cnt;
2693 int i;
2694 int n;
2695 char_u *p;
2696 char_u buf[MAXWLEN + 1];
2697
2698 /* <prefcondcnt> <prefcond> ... */
2699 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
2700 if (cnt <= 0)
2701 return SP_FORMERROR;
2702
2703 lp->sl_prefprog = (regprog_T **)alloc_clear(
2704 (unsigned)sizeof(regprog_T *) * cnt);
2705 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002706 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002707 lp->sl_prefixcnt = cnt;
2708
2709 for (i = 0; i < cnt; ++i)
2710 {
2711 /* <prefcond> : <condlen> <condstr> */
2712 n = getc(fd); /* <condlen> */
2713 if (n < 0 || n >= MAXWLEN)
2714 return SP_FORMERROR;
2715
2716 /* When <condlen> is zero we have an empty condition. Otherwise
2717 * compile the regexp program used to check for the condition. */
2718 if (n > 0)
2719 {
2720 buf[0] = '^'; /* always match at one position only */
2721 p = buf + 1;
2722 while (n-- > 0)
2723 *p++ = getc(fd); /* <condstr> */
2724 *p = NUL;
2725 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
2726 }
2727 }
2728 return 0;
2729}
2730
2731/*
2732 * Read REP items section from "fd": <repcount> <rep> ...
2733 * Return SP_*ERROR flags.
2734 */
2735 static int
2736read_rep_section(fd, slang)
2737 FILE *fd;
2738 slang_T *slang;
2739{
2740 int cnt;
2741 garray_T *gap;
2742 fromto_T *ftp;
2743 short *first;
2744 int i;
2745
2746 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
2747 if (cnt < 0)
2748 return SP_TRUNCERROR;
2749
2750 gap = &slang->sl_rep;
2751 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002752 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002753
2754 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
2755 for (; gap->ga_len < cnt; ++gap->ga_len)
2756 {
2757 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
2758 ftp->ft_from = read_cnt_string(fd, 1, &i);
2759 if (i < 0)
2760 return i;
2761 if (i == 0)
2762 return SP_FORMERROR;
2763 ftp->ft_to = read_cnt_string(fd, 1, &i);
2764 if (i <= 0)
2765 {
2766 vim_free(ftp->ft_from);
2767 if (i < 0)
2768 return i;
2769 return SP_FORMERROR;
2770 }
2771 }
2772
2773 /* Fill the first-index table. */
2774 first = slang->sl_rep_first;
2775 for (i = 0; i < 256; ++i)
2776 first[i] = -1;
2777 for (i = 0; i < gap->ga_len; ++i)
2778 {
2779 ftp = &((fromto_T *)gap->ga_data)[i];
2780 if (first[*ftp->ft_from] == -1)
2781 first[*ftp->ft_from] = i;
2782 }
2783 return 0;
2784}
2785
2786/*
2787 * Read SN_SAL section: <salflags> <salcount> <sal> ...
2788 * Return SP_*ERROR flags.
2789 */
2790 static int
2791read_sal_section(fd, slang)
2792 FILE *fd;
2793 slang_T *slang;
2794{
2795 int i;
2796 int cnt;
2797 garray_T *gap;
2798 salitem_T *smp;
2799 int ccnt;
2800 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002801 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002802
2803 slang->sl_sofo = FALSE;
2804
2805 i = getc(fd); /* <salflags> */
2806 if (i & SAL_F0LLOWUP)
2807 slang->sl_followup = TRUE;
2808 if (i & SAL_COLLAPSE)
2809 slang->sl_collapse = TRUE;
2810 if (i & SAL_REM_ACCENTS)
2811 slang->sl_rem_accents = TRUE;
2812
2813 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
2814 if (cnt < 0)
2815 return SP_TRUNCERROR;
2816
2817 gap = &slang->sl_sal;
2818 ga_init2(gap, sizeof(salitem_T), 10);
2819 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002820 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002821
2822 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
2823 for (; gap->ga_len < cnt; ++gap->ga_len)
2824 {
2825 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2826 ccnt = getc(fd); /* <salfromlen> */
2827 if (ccnt < 0)
2828 return SP_TRUNCERROR;
2829 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002830 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002831 smp->sm_lead = p;
2832
2833 /* Read up to the first special char into sm_lead. */
2834 for (i = 0; i < ccnt; ++i)
2835 {
2836 c = getc(fd); /* <salfrom> */
2837 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
2838 break;
2839 *p++ = c;
2840 }
2841 smp->sm_leadlen = p - smp->sm_lead;
2842 *p++ = NUL;
2843
2844 /* Put (abc) chars in sm_oneof, if any. */
2845 if (c == '(')
2846 {
2847 smp->sm_oneof = p;
2848 for (++i; i < ccnt; ++i)
2849 {
2850 c = getc(fd); /* <salfrom> */
2851 if (c == ')')
2852 break;
2853 *p++ = c;
2854 }
2855 *p++ = NUL;
2856 if (++i < ccnt)
2857 c = getc(fd);
2858 }
2859 else
2860 smp->sm_oneof = NULL;
2861
2862 /* Any following chars go in sm_rules. */
2863 smp->sm_rules = p;
2864 if (i < ccnt)
2865 /* store the char we got while checking for end of sm_lead */
2866 *p++ = c;
2867 for (++i; i < ccnt; ++i)
2868 *p++ = getc(fd); /* <salfrom> */
2869 *p++ = NUL;
2870
2871 /* <saltolen> <salto> */
2872 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
2873 if (ccnt < 0)
2874 {
2875 vim_free(smp->sm_lead);
2876 return ccnt;
2877 }
2878
2879#ifdef FEAT_MBYTE
2880 if (has_mbyte)
2881 {
2882 /* convert the multi-byte strings to wide char strings */
2883 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2884 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2885 if (smp->sm_oneof == NULL)
2886 smp->sm_oneof_w = NULL;
2887 else
2888 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
2889 if (smp->sm_to == NULL)
2890 smp->sm_to_w = NULL;
2891 else
2892 smp->sm_to_w = mb_str2wide(smp->sm_to);
2893 if (smp->sm_lead_w == NULL
2894 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
2895 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
2896 {
2897 vim_free(smp->sm_lead);
2898 vim_free(smp->sm_to);
2899 vim_free(smp->sm_lead_w);
2900 vim_free(smp->sm_oneof_w);
2901 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002902 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002903 }
2904 }
2905#endif
2906 }
2907
2908 /* Fill the first-index table. */
2909 set_sal_first(slang);
2910
2911 return 0;
2912}
2913
2914/*
2915 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
2916 * Return SP_*ERROR flags.
2917 */
2918 static int
2919read_sofo_section(fd, slang)
2920 FILE *fd;
2921 slang_T *slang;
2922{
2923 int cnt;
2924 char_u *from, *to;
2925 int res;
2926
2927 slang->sl_sofo = TRUE;
2928
2929 /* <sofofromlen> <sofofrom> */
2930 from = read_cnt_string(fd, 2, &cnt);
2931 if (cnt < 0)
2932 return cnt;
2933
2934 /* <sofotolen> <sofoto> */
2935 to = read_cnt_string(fd, 2, &cnt);
2936 if (cnt < 0)
2937 {
2938 vim_free(from);
2939 return cnt;
2940 }
2941
2942 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
2943 if (from != NULL && to != NULL)
2944 res = set_sofo(slang, from, to);
2945 else if (from != NULL || to != NULL)
2946 res = SP_FORMERROR; /* only one of two strings is an error */
2947 else
2948 res = 0;
2949
2950 vim_free(from);
2951 vim_free(to);
2952 return res;
2953}
2954
2955/*
2956 * Read the compound section from the .spl file:
2957 * <compmax> <compminlen> <compsylmax> <compflags>
2958 * Returns SP_*ERROR flags.
2959 */
2960 static int
2961read_compound(fd, slang, len)
2962 FILE *fd;
2963 slang_T *slang;
2964 int len;
2965{
2966 int todo = len;
2967 int c;
2968 int atstart;
2969 char_u *pat;
2970 char_u *pp;
2971 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002972 char_u *ap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002973
2974 if (todo < 2)
2975 return SP_FORMERROR; /* need at least two bytes */
2976
2977 --todo;
2978 c = getc(fd); /* <compmax> */
2979 if (c < 2)
2980 c = MAXWLEN;
2981 slang->sl_compmax = c;
2982
2983 --todo;
2984 c = getc(fd); /* <compminlen> */
2985 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002986 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002987 slang->sl_compminlen = c;
2988
2989 --todo;
2990 c = getc(fd); /* <compsylmax> */
2991 if (c < 1)
2992 c = MAXWLEN;
2993 slang->sl_compsylmax = c;
2994
2995 /* Turn the COMPOUNDFLAGS items into a regexp pattern:
2996 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00002997 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
2998 * Conversion to utf-8 may double the size. */
2999 c = todo * 2 + 7;
3000#ifdef FEAT_MBYTE
3001 if (enc_utf8)
3002 c += todo * 2;
3003#endif
3004 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003005 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003006 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003007
Bram Moolenaard12a1322005-08-21 22:08:24 +00003008 /* We also need a list of all flags that can appear at the start and one
3009 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003010 cp = alloc(todo + 1);
3011 if (cp == NULL)
3012 {
3013 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003014 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003015 }
3016 slang->sl_compstartflags = cp;
3017 *cp = NUL;
3018
Bram Moolenaard12a1322005-08-21 22:08:24 +00003019 ap = alloc(todo + 1);
3020 if (ap == NULL)
3021 {
3022 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003023 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003024 }
3025 slang->sl_compallflags = ap;
3026 *ap = NUL;
3027
Bram Moolenaar5195e452005-08-19 20:32:47 +00003028 pp = pat;
3029 *pp++ = '^';
3030 *pp++ = '\\';
3031 *pp++ = '(';
3032
3033 atstart = 1;
3034 while (todo-- > 0)
3035 {
3036 c = getc(fd); /* <compflags> */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003037
3038 /* Add all flags to "sl_compallflags". */
3039 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003040 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003041 {
3042 *ap++ = c;
3043 *ap = NUL;
3044 }
3045
Bram Moolenaar5195e452005-08-19 20:32:47 +00003046 if (atstart != 0)
3047 {
3048 /* At start of item: copy flags to "sl_compstartflags". For a
3049 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3050 if (c == '[')
3051 atstart = 2;
3052 else if (c == ']')
3053 atstart = 0;
3054 else
3055 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003056 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003057 {
3058 *cp++ = c;
3059 *cp = NUL;
3060 }
3061 if (atstart == 1)
3062 atstart = 0;
3063 }
3064 }
3065 if (c == '/') /* slash separates two items */
3066 {
3067 *pp++ = '\\';
3068 *pp++ = '|';
3069 atstart = 1;
3070 }
3071 else /* normal char, "[abc]" and '*' are copied as-is */
3072 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003073 if (c == '+' || c == '~')
Bram Moolenaar5195e452005-08-19 20:32:47 +00003074 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003075#ifdef FEAT_MBYTE
3076 if (enc_utf8)
3077 pp += mb_char2bytes(c, pp);
3078 else
3079#endif
3080 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003081 }
3082 }
3083
3084 *pp++ = '\\';
3085 *pp++ = ')';
3086 *pp++ = '$';
3087 *pp = NUL;
3088
3089 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3090 vim_free(pat);
3091 if (slang->sl_compprog == NULL)
3092 return SP_FORMERROR;
3093
3094 return 0;
3095}
3096
Bram Moolenaar6de68532005-08-24 22:08:48 +00003097/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003098 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003099 * Like strchr() but independent of locale.
3100 */
3101 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003102byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003103 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003104 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003105{
3106 char_u *p;
3107
3108 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003109 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003110 return TRUE;
3111 return FALSE;
3112}
3113
Bram Moolenaar5195e452005-08-19 20:32:47 +00003114#define SY_MAXLEN 30
3115typedef struct syl_item_S
3116{
3117 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3118 int sy_len;
3119} syl_item_T;
3120
3121/*
3122 * Truncate "slang->sl_syllable" at the first slash and put the following items
3123 * in "slang->sl_syl_items".
3124 */
3125 static int
3126init_syl_tab(slang)
3127 slang_T *slang;
3128{
3129 char_u *p;
3130 char_u *s;
3131 int l;
3132 syl_item_T *syl;
3133
3134 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3135 p = vim_strchr(slang->sl_syllable, '/');
3136 while (p != NULL)
3137 {
3138 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003139 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003140 break;
3141 s = p;
3142 p = vim_strchr(p, '/');
3143 if (p == NULL)
3144 l = STRLEN(s);
3145 else
3146 l = p - s;
3147 if (l >= SY_MAXLEN)
3148 return SP_FORMERROR;
3149 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003150 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003151 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3152 + slang->sl_syl_items.ga_len++;
3153 vim_strncpy(syl->sy_chars, s, l);
3154 syl->sy_len = l;
3155 }
3156 return OK;
3157}
3158
3159/*
3160 * Count the number of syllables in "word".
3161 * When "word" contains spaces the syllables after the last space are counted.
3162 * Returns zero if syllables are not defines.
3163 */
3164 static int
3165count_syllables(slang, word)
3166 slang_T *slang;
3167 char_u *word;
3168{
3169 int cnt = 0;
3170 int skip = FALSE;
3171 char_u *p;
3172 int len;
3173 int i;
3174 syl_item_T *syl;
3175 int c;
3176
3177 if (slang->sl_syllable == NULL)
3178 return 0;
3179
3180 for (p = word; *p != NUL; p += len)
3181 {
3182 /* When running into a space reset counter. */
3183 if (*p == ' ')
3184 {
3185 len = 1;
3186 cnt = 0;
3187 continue;
3188 }
3189
3190 /* Find longest match of syllable items. */
3191 len = 0;
3192 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3193 {
3194 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3195 if (syl->sy_len > len
3196 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3197 len = syl->sy_len;
3198 }
3199 if (len != 0) /* found a match, count syllable */
3200 {
3201 ++cnt;
3202 skip = FALSE;
3203 }
3204 else
3205 {
3206 /* No recognized syllable item, at least a syllable char then? */
3207#ifdef FEAT_MBYTE
3208 c = mb_ptr2char(p);
3209 len = (*mb_ptr2len)(p);
3210#else
3211 c = *p;
3212 len = 1;
3213#endif
3214 if (vim_strchr(slang->sl_syllable, c) == NULL)
3215 skip = FALSE; /* No, search for next syllable */
3216 else if (!skip)
3217 {
3218 ++cnt; /* Yes, count it */
3219 skip = TRUE; /* don't count following syllable chars */
3220 }
3221 }
3222 }
3223 return cnt;
3224}
3225
3226/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003227 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003228 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003229 */
3230 static int
3231set_sofo(lp, from, to)
3232 slang_T *lp;
3233 char_u *from;
3234 char_u *to;
3235{
3236 int i;
3237
3238#ifdef FEAT_MBYTE
3239 garray_T *gap;
3240 char_u *s;
3241 char_u *p;
3242 int c;
3243 int *inp;
3244
3245 if (has_mbyte)
3246 {
3247 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3248 * characters. The index is the low byte of the character.
3249 * The list contains from-to pairs with a terminating NUL.
3250 * sl_sal_first[] is used for latin1 "from" characters. */
3251 gap = &lp->sl_sal;
3252 ga_init2(gap, sizeof(int *), 1);
3253 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003254 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003255 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3256 gap->ga_len = 256;
3257
3258 /* First count the number of items for each list. Temporarily use
3259 * sl_sal_first[] for this. */
3260 for (p = from, s = to; *p != NUL && *s != NUL; )
3261 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003262 c = mb_cptr2char_adv(&p);
3263 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003264 if (c >= 256)
3265 ++lp->sl_sal_first[c & 0xff];
3266 }
3267 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003268 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003269
3270 /* Allocate the lists. */
3271 for (i = 0; i < 256; ++i)
3272 if (lp->sl_sal_first[i] > 0)
3273 {
3274 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3275 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003276 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003277 ((int **)gap->ga_data)[i] = (int *)p;
3278 *(int *)p = 0;
3279 }
3280
3281 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3282 * list. */
3283 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3284 for (p = from, s = to; *p != NUL && *s != NUL; )
3285 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003286 c = mb_cptr2char_adv(&p);
3287 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003288 if (c >= 256)
3289 {
3290 /* Append the from-to chars at the end of the list with
3291 * the low byte. */
3292 inp = ((int **)gap->ga_data)[c & 0xff];
3293 while (*inp != 0)
3294 ++inp;
3295 *inp++ = c; /* from char */
3296 *inp++ = i; /* to char */
3297 *inp++ = NUL; /* NUL at the end */
3298 }
3299 else
3300 /* mapping byte to char is done in sl_sal_first[] */
3301 lp->sl_sal_first[c] = i;
3302 }
3303 }
3304 else
3305#endif
3306 {
3307 /* mapping bytes to bytes is done in sl_sal_first[] */
3308 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003309 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003310
3311 for (i = 0; to[i] != NUL; ++i)
3312 lp->sl_sal_first[from[i]] = to[i];
3313 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3314 }
3315
Bram Moolenaar5195e452005-08-19 20:32:47 +00003316 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003317}
3318
3319/*
3320 * Fill the first-index table for "lp".
3321 */
3322 static void
3323set_sal_first(lp)
3324 slang_T *lp;
3325{
3326 salfirst_T *sfirst;
3327 int i;
3328 salitem_T *smp;
3329 int c;
3330 garray_T *gap = &lp->sl_sal;
3331
3332 sfirst = lp->sl_sal_first;
3333 for (i = 0; i < 256; ++i)
3334 sfirst[i] = -1;
3335 smp = (salitem_T *)gap->ga_data;
3336 for (i = 0; i < gap->ga_len; ++i)
3337 {
3338#ifdef FEAT_MBYTE
3339 if (has_mbyte)
3340 /* Use the lowest byte of the first character. For latin1 it's
3341 * the character, for other encodings it should differ for most
3342 * characters. */
3343 c = *smp[i].sm_lead_w & 0xff;
3344 else
3345#endif
3346 c = *smp[i].sm_lead;
3347 if (sfirst[c] == -1)
3348 {
3349 sfirst[c] = i;
3350#ifdef FEAT_MBYTE
3351 if (has_mbyte)
3352 {
3353 int n;
3354
3355 /* Make sure all entries with this byte are following each
3356 * other. Move the ones that are in the wrong position. Do
3357 * keep the same ordering! */
3358 while (i + 1 < gap->ga_len
3359 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3360 /* Skip over entry with same index byte. */
3361 ++i;
3362
3363 for (n = 1; i + n < gap->ga_len; ++n)
3364 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3365 {
3366 salitem_T tsal;
3367
3368 /* Move entry with same index byte after the entries
3369 * we already found. */
3370 ++i;
3371 --n;
3372 tsal = smp[i + n];
3373 mch_memmove(smp + i + 1, smp + i,
3374 sizeof(salitem_T) * n);
3375 smp[i] = tsal;
3376 }
3377 }
3378#endif
3379 }
3380 }
3381}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003382
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003383#ifdef FEAT_MBYTE
3384/*
3385 * Turn a multi-byte string into a wide character string.
3386 * Return it in allocated memory (NULL for out-of-memory)
3387 */
3388 static int *
3389mb_str2wide(s)
3390 char_u *s;
3391{
3392 int *res;
3393 char_u *p;
3394 int i = 0;
3395
3396 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3397 if (res != NULL)
3398 {
3399 for (p = s; *p != NUL; )
3400 res[i++] = mb_ptr2char_adv(&p);
3401 res[i] = NUL;
3402 }
3403 return res;
3404}
3405#endif
3406
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003407/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003408 * Read one row of siblings from the spell file and store it in the byte array
3409 * "byts" and index array "idxs". Recursively read the children.
3410 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00003411 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00003412 *
3413 * Returns the index follosing the siblings.
3414 * Returns -1 if the file is shorter than expected.
3415 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003416 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003417 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003418read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003419 FILE *fd;
3420 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003421 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003422 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003423 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003424 int prefixtree; /* TRUE for reading PREFIXTREE */
3425 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003426{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003427 int len;
3428 int i;
3429 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003430 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003431 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003432 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003433#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003434
Bram Moolenaar51485f02005-06-04 21:55:20 +00003435 len = getc(fd); /* <siblingcount> */
3436 if (len <= 0)
3437 return -1;
3438
3439 if (startidx + len >= maxidx)
3440 return -2;
3441 byts[idx++] = len;
3442
3443 /* Read the byte values, flag/region bytes and shared indexes. */
3444 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003445 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003446 c = getc(fd); /* <byte> */
3447 if (c < 0)
3448 return -1;
3449 if (c <= BY_SPECIAL)
3450 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003451 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003452 {
3453 /* No flags, all regions. */
3454 idxs[idx] = 0;
3455 c = 0;
3456 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003457 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003458 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003459 if (prefixtree)
3460 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003461 /* Read the optional pflags byte, the prefix ID and the
3462 * condition nr. In idxs[] store the prefix ID in the low
3463 * byte, the condition index shifted up 8 bits, the flags
3464 * shifted up 24 bits. */
3465 if (c == BY_FLAGS)
3466 c = getc(fd) << 24; /* <pflags> */
3467 else
3468 c = 0;
3469
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003470 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003471
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003472 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
3473 if (n >= maxprefcondnr)
3474 return -2;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003475 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003476 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003477 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003478 {
3479 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003480 * idxs[] the flags go in the low two bytes, region above
3481 * that and prefix ID above the region. */
3482 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003483 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003484 if (c2 == BY_FLAGS2)
3485 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003486 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003487 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003488 if (c & WF_AFX)
3489 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003490 }
3491
Bram Moolenaar51485f02005-06-04 21:55:20 +00003492 idxs[idx] = c;
3493 c = 0;
3494 }
3495 else /* c == BY_INDEX */
3496 {
3497 /* <nodeidx> */
3498 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
3499 if (n < 0 || n >= maxidx)
3500 return -2;
3501 idxs[idx] = n + SHARED_MASK;
3502 c = getc(fd); /* <xbyte> */
3503 }
3504 }
3505 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003506 }
3507
Bram Moolenaar51485f02005-06-04 21:55:20 +00003508 /* Recursively read the children for non-shared siblings.
3509 * Skip the end-of-word ones (zero byte value) and the shared ones (and
3510 * remove SHARED_MASK) */
3511 for (i = 1; i <= len; ++i)
3512 if (byts[startidx + i] != 0)
3513 {
3514 if (idxs[startidx + i] & SHARED_MASK)
3515 idxs[startidx + i] &= ~SHARED_MASK;
3516 else
3517 {
3518 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003519 idx = read_tree(fd, byts, idxs, maxidx, idx,
3520 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003521 if (idx < 0)
3522 break;
3523 }
3524 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003525
Bram Moolenaar51485f02005-06-04 21:55:20 +00003526 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003527}
3528
3529/*
3530 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003531 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003532 */
3533 char_u *
3534did_set_spelllang(buf)
3535 buf_T *buf;
3536{
3537 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003538 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003539 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00003540 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003541 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003542 int region_mask;
3543 slang_T *lp;
3544 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003545 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003546 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003547 int len;
3548 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003549 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003550 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003551 char_u *use_region = NULL;
3552 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003553 int nobreak = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003554
3555 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003556 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003557
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003558 /* loop over comma separated language names. */
3559 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003560 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003561 /* Get one language name. */
3562 copy_option_part(&splp, lang, MAXWLEN, ",");
3563
Bram Moolenaar5482f332005-04-17 20:18:43 +00003564 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003565 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003566
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003567 /* If the name ends in ".spl" use it as the name of the spell file.
3568 * If there is a region name let "region" point to it and remove it
3569 * from the name. */
3570 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
3571 {
3572 filename = TRUE;
3573
Bram Moolenaarb6356332005-07-18 21:40:44 +00003574 /* Locate a region and remove it from the file name. */
3575 p = vim_strchr(gettail(lang), '_');
3576 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
3577 && !ASCII_ISALPHA(p[3]))
3578 {
3579 vim_strncpy(region_cp, p + 1, 2);
3580 mch_memmove(p, p + 3, len - (p - lang) - 2);
3581 len -= 3;
3582 region = region_cp;
3583 }
3584 else
3585 dont_use_region = TRUE;
3586
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003587 /* Check if we loaded this language before. */
3588 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3589 if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME)
3590 break;
3591 }
3592 else
3593 {
3594 filename = FALSE;
3595 if (len > 3 && lang[len - 3] == '_')
3596 {
3597 region = lang + len - 2;
3598 len -= 3;
3599 lang[len] = NUL;
3600 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003601 else
3602 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003603
3604 /* Check if we loaded this language before. */
3605 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3606 if (STRICMP(lang, lp->sl_name) == 0)
3607 break;
3608 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003609
Bram Moolenaarb6356332005-07-18 21:40:44 +00003610 if (region != NULL)
3611 {
3612 /* If the region differs from what was used before then don't
3613 * use it for 'spellfile'. */
3614 if (use_region != NULL && STRCMP(region, use_region) != 0)
3615 dont_use_region = TRUE;
3616 use_region = region;
3617 }
3618
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003619 /* If not found try loading the language now. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003620 if (lp == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003621 {
3622 if (filename)
3623 (void)spell_load_file(lang, lang, NULL, FALSE);
3624 else
3625 spell_load_lang(lang);
3626 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003627
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003628 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003629 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003630 */
3631 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003632 if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME
3633 : STRICMP(lang, lp->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003634 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003635 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003636 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003637 {
3638 /* find region in sl_regions */
3639 c = find_region(lp->sl_regions, region);
3640 if (c == REGION_ALL)
3641 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003642 if (lp->sl_add)
3643 {
3644 if (*lp->sl_regions != NUL)
3645 /* This addition file is for other regions. */
3646 region_mask = 0;
3647 }
3648 else
3649 /* This is probably an error. Give a warning and
3650 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003651 smsg((char_u *)
3652 _("Warning: region %s not supported"),
3653 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003654 }
3655 else
3656 region_mask = 1 << c;
3657 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003658
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003659 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003660 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003661 if (ga_grow(&ga, 1) == FAIL)
3662 {
3663 ga_clear(&ga);
3664 return e_outofmem;
3665 }
3666 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
3667 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
3668 ++ga.ga_len;
3669 use_midword(lp, buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003670 if (lp->sl_nobreak)
3671 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003672 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003673 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003674 }
3675
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003676 /* round 0: load int_wordlist, if possible.
3677 * round 1: load first name in 'spellfile'.
3678 * round 2: load second name in 'spellfile.
3679 * etc. */
3680 spf = curbuf->b_p_spf;
3681 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003682 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003683 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003684 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003685 /* Internal wordlist, if there is one. */
3686 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003687 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003688 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003689 }
3690 else
3691 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003692 /* One entry in 'spellfile'. */
3693 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
3694 STRCAT(spf_name, ".spl");
3695
3696 /* If it was already found above then skip it. */
3697 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003698 {
3699 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
3700 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003701 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003702 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003703 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003704 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003705 }
3706
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003707 /* Check if it was loaded already. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003708 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3709 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
3710 break;
3711 if (lp == NULL)
3712 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003713 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003714 * region name, the region is ignored otherwise. for int_wordlist
3715 * use an arbitrary name. */
3716 if (round == 0)
3717 STRCPY(lang, "internal wordlist");
3718 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00003719 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003720 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003721 p = vim_strchr(lang, '.');
3722 if (p != NULL)
3723 *p = NUL; /* truncate at ".encoding.add" */
3724 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003725 lp = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003726
3727 /* If one of the languages has NOBREAK we assume the addition
3728 * files also have this. */
3729 if (lp != NULL && nobreak)
3730 lp->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003731 }
3732 if (lp != NULL && ga_grow(&ga, 1) == OK)
3733 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003734 region_mask = REGION_ALL;
3735 if (use_region != NULL && !dont_use_region)
3736 {
3737 /* find region in sl_regions */
3738 c = find_region(lp->sl_regions, use_region);
3739 if (c != REGION_ALL)
3740 region_mask = 1 << c;
3741 else if (*lp->sl_regions != NUL)
3742 /* This spell file is for other regions. */
3743 region_mask = 0;
3744 }
3745
3746 if (region_mask != 0)
3747 {
3748 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
3749 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
3750 ++ga.ga_len;
3751 use_midword(lp, buf);
3752 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003753 }
3754 }
3755
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003756 /* Everything is fine, store the new b_langp value. */
3757 ga_clear(&buf->b_langp);
3758 buf->b_langp = ga;
3759
3760 return NULL;
3761}
3762
3763/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003764 * Clear the midword characters for buffer "buf".
3765 */
3766 static void
3767clear_midword(buf)
3768 buf_T *buf;
3769{
3770 vim_memset(buf->b_spell_ismw, 0, 256);
3771#ifdef FEAT_MBYTE
3772 vim_free(buf->b_spell_ismw_mb);
3773 buf->b_spell_ismw_mb = NULL;
3774#endif
3775}
3776
3777/*
3778 * Use the "sl_midword" field of language "lp" for buffer "buf".
3779 * They add up to any currently used midword characters.
3780 */
3781 static void
3782use_midword(lp, buf)
3783 slang_T *lp;
3784 buf_T *buf;
3785{
3786 char_u *p;
3787
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003788 if (lp->sl_midword == NULL) /* there aren't any */
3789 return;
3790
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003791 for (p = lp->sl_midword; *p != NUL; )
3792#ifdef FEAT_MBYTE
3793 if (has_mbyte)
3794 {
3795 int c, l, n;
3796 char_u *bp;
3797
3798 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003799 l = (*mb_ptr2len)(p);
3800 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003801 buf->b_spell_ismw[c] = TRUE;
3802 else if (buf->b_spell_ismw_mb == NULL)
3803 /* First multi-byte char in "b_spell_ismw_mb". */
3804 buf->b_spell_ismw_mb = vim_strnsave(p, l);
3805 else
3806 {
3807 /* Append multi-byte chars to "b_spell_ismw_mb". */
3808 n = STRLEN(buf->b_spell_ismw_mb);
3809 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
3810 if (bp != NULL)
3811 {
3812 vim_free(buf->b_spell_ismw_mb);
3813 buf->b_spell_ismw_mb = bp;
3814 vim_strncpy(bp + n, p, l);
3815 }
3816 }
3817 p += l;
3818 }
3819 else
3820#endif
3821 buf->b_spell_ismw[*p++] = TRUE;
3822}
3823
3824/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003825 * Find the region "region[2]" in "rp" (points to "sl_regions").
3826 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003827 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003828 */
3829 static int
3830find_region(rp, region)
3831 char_u *rp;
3832 char_u *region;
3833{
3834 int i;
3835
3836 for (i = 0; ; i += 2)
3837 {
3838 if (rp[i] == NUL)
3839 return REGION_ALL;
3840 if (rp[i] == region[0] && rp[i + 1] == region[1])
3841 break;
3842 }
3843 return i / 2;
3844}
3845
3846/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003847 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003848 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00003849 * Word WF_ONECAP
3850 * W WORD WF_ALLCAP
3851 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003852 */
3853 static int
3854captype(word, end)
3855 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003856 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003857{
3858 char_u *p;
3859 int c;
3860 int firstcap;
3861 int allcap;
3862 int past_second = FALSE; /* past second word char */
3863
3864 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003865 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003866 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003867 return 0; /* only non-word characters, illegal word */
3868#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00003869 if (has_mbyte)
3870 c = mb_ptr2char_adv(&p);
3871 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003872#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00003873 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003874 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003875
3876 /*
3877 * Need to check all letters to find a word with mixed upper/lower.
3878 * But a word with an upper char only at start is a ONECAP.
3879 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003880 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003881 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003882 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003883 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003884 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003885 {
3886 /* UUl -> KEEPCAP */
3887 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003888 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003889 allcap = FALSE;
3890 }
3891 else if (!allcap)
3892 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003893 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003894 past_second = TRUE;
3895 }
3896
3897 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003898 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003899 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003900 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003901 return 0;
3902}
3903
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003904/*
3905 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
3906 * capital. So that make_case_word() can turn WOrd into Word.
3907 * Add ALLCAP for "WOrD".
3908 */
3909 static int
3910badword_captype(word, end)
3911 char_u *word;
3912 char_u *end;
3913{
3914 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003915 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003916 int l, u;
3917 int first;
3918 char_u *p;
3919
3920 if (flags & WF_KEEPCAP)
3921 {
3922 /* Count the number of UPPER and lower case letters. */
3923 l = u = 0;
3924 first = FALSE;
3925 for (p = word; p < end; mb_ptr_adv(p))
3926 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00003927 c = PTR2CHAR(p);
3928 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003929 {
3930 ++u;
3931 if (p == word)
3932 first = TRUE;
3933 }
3934 else
3935 ++l;
3936 }
3937
3938 /* If there are more UPPER than lower case letters suggest an
3939 * ALLCAP word. Otherwise, if the first letter is UPPER then
3940 * suggest ONECAP. Exception: "ALl" most likely should be "All",
3941 * require three upper case letters. */
3942 if (u > l && u > 2)
3943 flags |= WF_ALLCAP;
3944 else if (first)
3945 flags |= WF_ONECAP;
3946 }
3947 return flags;
3948}
3949
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003950# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
3951/*
3952 * Free all languages.
3953 */
3954 void
3955spell_free_all()
3956{
3957 slang_T *lp;
3958 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003959 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003960
3961 /* Go through all buffers and handle 'spelllang'. */
3962 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3963 ga_clear(&buf->b_langp);
3964
3965 while (first_lang != NULL)
3966 {
3967 lp = first_lang;
3968 first_lang = lp->sl_next;
3969 slang_free(lp);
3970 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003971
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003972 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00003973 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00003974 /* Delete the internal wordlist and its .spl file */
3975 mch_remove(int_wordlist);
3976 int_wordlist_spl(fname);
3977 mch_remove(fname);
3978 vim_free(int_wordlist);
3979 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003980 }
3981
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003982 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00003983}
3984# endif
3985
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003986# if defined(FEAT_MBYTE) || defined(PROTO)
3987/*
3988 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003989 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003990 */
3991 void
3992spell_reload()
3993{
3994 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00003995 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003996
Bram Moolenaarea408852005-06-25 22:49:46 +00003997 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003998 init_spell_chartab();
3999
4000 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004001 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004002
4003 /* Go through all buffers and handle 'spelllang'. */
4004 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4005 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004006 /* Only load the wordlists when 'spelllang' is set and there is a
4007 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004008 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004009 {
4010 FOR_ALL_WINDOWS(wp)
4011 if (wp->w_buffer == buf && wp->w_p_spell)
4012 {
4013 (void)did_set_spelllang(buf);
4014# ifdef FEAT_WINDOWS
4015 break;
4016# endif
4017 }
4018 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004019 }
4020}
4021# endif
4022
Bram Moolenaarb765d632005-06-07 21:00:02 +00004023/*
4024 * Reload the spell file "fname" if it's loaded.
4025 */
4026 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004027spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004028 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004029 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004030{
4031 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004032 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004033
Bram Moolenaarb765d632005-06-07 21:00:02 +00004034 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004035 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004036 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
4037 {
4038 slang_clear(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004039 if (spell_load_file(fname, NULL, lp, FALSE) == NULL)
4040 /* reloading failed, clear the language */
4041 slang_clear(lp);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004042 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004043 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004044 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004045 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004046
4047 /* When "zg" was used and the file wasn't loaded yet, should redo
4048 * 'spelllang' to get it loaded. */
4049 if (added_word && !didit)
4050 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004051}
4052
4053
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004054/*
4055 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004056 */
4057
Bram Moolenaar51485f02005-06-04 21:55:20 +00004058#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004059 and .dic file. */
4060/*
4061 * Main structure to store the contents of a ".aff" file.
4062 */
4063typedef struct afffile_S
4064{
4065 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004066 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004067 int af_slash; /* character used in word for slash */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004068 unsigned af_rar; /* RAR ID for rare word */
4069 unsigned af_kep; /* KEP ID for keep-case word */
4070 unsigned af_bad; /* BAD ID for banned word */
4071 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004072 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004073 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004074 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4075 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004076 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004077} afffile_T;
4078
Bram Moolenaar6de68532005-08-24 22:08:48 +00004079#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004080#define AFT_LONG 1 /* flags are two characters */
4081#define AFT_CAPLONG 2 /* flags are one or two characters */
4082#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004083
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004084typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004085/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4086struct affentry_S
4087{
4088 affentry_T *ae_next; /* next affix with same name/number */
4089 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4090 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004091 char_u *ae_cond; /* condition (NULL for ".") */
4092 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004093 char_u ae_rare; /* rare affix */
4094 char_u ae_nocomp; /* word with affix not compoundable */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004095};
4096
Bram Moolenaar6de68532005-08-24 22:08:48 +00004097#ifdef FEAT_MBYTE
4098# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4099#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004100# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004101#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004102
Bram Moolenaar51485f02005-06-04 21:55:20 +00004103/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4104typedef struct affheader_S
4105{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004106 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4107 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4108 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004109 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004110 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004111 affentry_T *ah_first; /* first affix entry */
4112} affheader_T;
4113
4114#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4115
Bram Moolenaar6de68532005-08-24 22:08:48 +00004116/* Flag used in compound items. */
4117typedef struct compitem_S
4118{
4119 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4120 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4121 int ci_newID; /* affix ID after renumbering. */
4122} compitem_T;
4123
4124#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4125
Bram Moolenaar51485f02005-06-04 21:55:20 +00004126/*
4127 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004128 * the need to keep track of each allocated thing, everything is freed all at
4129 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004130 */
4131#define SBLOCKSIZE 16000 /* size of sb_data */
4132typedef struct sblock_S sblock_T;
4133struct sblock_S
4134{
4135 sblock_T *sb_next; /* next block in list */
4136 int sb_used; /* nr of bytes already in use */
4137 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004138};
4139
4140/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004141 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004142 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004143typedef struct wordnode_S wordnode_T;
4144struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004145{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004146 union /* shared to save space */
4147 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004148 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004149 int index; /* index in written nodes (valid after first
4150 round) */
4151 } wn_u1;
4152 union /* shared to save space */
4153 {
4154 wordnode_T *next; /* next node with same hash key */
4155 wordnode_T *wnode; /* parent node that will write this node */
4156 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004157 wordnode_T *wn_child; /* child (next byte in word) */
4158 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4159 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004160 int wn_refs; /* Nr. of references to this node. Only
4161 relevant for first node in a list of
4162 siblings, in following siblings it is
4163 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004164 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004165 char_u wn_affixID; /* when "wn_byte" is NUL: supported/required
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004166 prefix ID or 0 */
4167 short_u wn_flags; /* when "wn_byte" is NUL: WF_ flags */
4168 short wn_region; /* when "wn_byte" is NUL: region mask; for
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004169 PREFIXTREE it's the prefcondnr */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004170#ifdef SPELL_PRINTTREE
4171 int wn_nr; /* sequence nr for printing */
4172#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004173};
4174
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004175#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4176
Bram Moolenaar51485f02005-06-04 21:55:20 +00004177#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004178
Bram Moolenaar51485f02005-06-04 21:55:20 +00004179/*
4180 * Info used while reading the spell files.
4181 */
4182typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004183{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004184 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004185 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004186
Bram Moolenaar51485f02005-06-04 21:55:20 +00004187 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004188 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004189
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004190 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004191
Bram Moolenaar51485f02005-06-04 21:55:20 +00004192 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004193 long si_blocks_cnt; /* memory blocks allocated */
4194 long si_compress_cnt; /* words to add before lowering
4195 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004196 wordnode_T *si_first_free; /* List of nodes that have been freed during
4197 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004198 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004199#ifdef SPELL_PRINTTREE
4200 int si_wordnode_nr; /* sequence nr for nodes */
4201#endif
4202
4203
Bram Moolenaar51485f02005-06-04 21:55:20 +00004204 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004205 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004206 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004207 int si_region; /* region mask */
4208 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004209 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004210 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004211 int si_msg_count; /* number of words added since last message */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004212 int si_region_count; /* number of regions supported (1 when there
4213 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004214 char_u si_region_name[16]; /* region names; used only if
4215 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004216
4217 garray_T si_rep; /* list of fromto_T entries from REP lines */
4218 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004219 char_u *si_sofofr; /* SOFOFROM text */
4220 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004221 int si_followup; /* soundsalike: ? */
4222 int si_collapse; /* soundsalike: ? */
4223 int si_rem_accents; /* soundsalike: remove accents */
4224 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004225 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004226 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004227 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004228 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004229 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004230 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004231 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004232 garray_T si_prefcond; /* table with conditions for postponed
4233 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004234 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004235 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004236} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004237
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004238static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004239static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
4240static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
4241static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004242static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004243static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
4244static void aff_check_number __ARGS((int spinval, int affval, char *name));
4245static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004246static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004247static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4248static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00004249static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004250static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004251static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004252static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004253static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004254static 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 +00004255static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
4256static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
4257static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004258static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004259static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004260static 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 +00004261static 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 +00004262static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
4263static void deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
4264static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
4265static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
4266static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004267static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004268static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00004269static void clear_node __ARGS((wordnode_T *node));
4270static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004271static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004272static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004273
Bram Moolenaar53805d12005-08-01 07:08:33 +00004274/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
4275 * but it must be negative to indicate the prefix tree to tree_add_word().
4276 * Use a negative number with the lower 8 bits zero. */
4277#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004278
Bram Moolenaar5195e452005-08-19 20:32:47 +00004279/*
4280 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
4281 */
4282static long compress_start = 30000; /* memory / SBLOCKSIZE */
4283static long compress_inc = 100; /* memory / SBLOCKSIZE */
4284static long compress_added = 500000; /* word count */
4285
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004286#ifdef SPELL_PRINTTREE
4287/*
4288 * For debugging the tree code: print the current tree in a (more or less)
4289 * readable format, so that we can see what happens when adding a word and/or
4290 * compressing the tree.
4291 * Based on code from Olaf Seibert.
4292 */
4293#define PRINTLINESIZE 1000
4294#define PRINTWIDTH 6
4295
4296#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4297 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4298
4299static char line1[PRINTLINESIZE];
4300static char line2[PRINTLINESIZE];
4301static char line3[PRINTLINESIZE];
4302
4303 static void
4304spell_clear_flags(wordnode_T *node)
4305{
4306 wordnode_T *np;
4307
4308 for (np = node; np != NULL; np = np->wn_sibling)
4309 {
4310 np->wn_u1.index = FALSE;
4311 spell_clear_flags(np->wn_child);
4312 }
4313}
4314
4315 static void
4316spell_print_node(wordnode_T *node, int depth)
4317{
4318 if (node->wn_u1.index)
4319 {
4320 /* Done this node before, print the reference. */
4321 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
4322 PRINTSOME(line2, depth, " ", 0, 0);
4323 PRINTSOME(line3, depth, " ", 0, 0);
4324 msg(line1);
4325 msg(line2);
4326 msg(line3);
4327 }
4328 else
4329 {
4330 node->wn_u1.index = TRUE;
4331
4332 if (node->wn_byte != NUL)
4333 {
4334 if (node->wn_child != NULL)
4335 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
4336 else
4337 /* Cannot happen? */
4338 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
4339 }
4340 else
4341 PRINTSOME(line1, depth, " $ ", 0, 0);
4342
4343 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
4344
4345 if (node->wn_sibling != NULL)
4346 PRINTSOME(line3, depth, " | ", 0, 0);
4347 else
4348 PRINTSOME(line3, depth, " ", 0, 0);
4349
4350 if (node->wn_byte == NUL)
4351 {
4352 msg(line1);
4353 msg(line2);
4354 msg(line3);
4355 }
4356
4357 /* do the children */
4358 if (node->wn_byte != NUL && node->wn_child != NULL)
4359 spell_print_node(node->wn_child, depth + 1);
4360
4361 /* do the siblings */
4362 if (node->wn_sibling != NULL)
4363 {
4364 /* get rid of all parent details except | */
4365 STRCPY(line1, line3);
4366 STRCPY(line2, line3);
4367 spell_print_node(node->wn_sibling, depth);
4368 }
4369 }
4370}
4371
4372 static void
4373spell_print_tree(wordnode_T *root)
4374{
4375 if (root != NULL)
4376 {
4377 /* Clear the "wn_u1.index" fields, used to remember what has been
4378 * done. */
4379 spell_clear_flags(root);
4380
4381 /* Recursively print the tree. */
4382 spell_print_node(root, 0);
4383 }
4384}
4385#endif /* SPELL_PRINTTREE */
4386
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004387/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004388 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00004389 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004390 */
4391 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004392spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004393 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004394 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004395{
4396 FILE *fd;
4397 afffile_T *aff;
4398 char_u rline[MAXLINELEN];
4399 char_u *line;
4400 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004401#define MAXITEMCNT 7
4402 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004403 int itemcnt;
4404 char_u *p;
4405 int lnum = 0;
4406 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004407 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004408 int aff_todo = 0;
4409 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004410 char_u *low = NULL;
4411 char_u *fol = NULL;
4412 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004413 int do_rep;
4414 int do_sal;
4415 int do_map;
4416 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004417 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004418 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004419 int compminlen = 0; /* COMPOUNDMIN value */
4420 int compsylmax = 0; /* COMPOUNDSYLMAX value */
4421 int compmax = 0; /* COMPOUNDMAX value */
4422 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDFLAGS
4423 concatenated */
4424 char_u *midword = NULL; /* MIDWORD value */
4425 char_u *syllable = NULL; /* SYLLABLE value */
4426 char_u *sofofrom = NULL; /* SOFOFROM value */
4427 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004428
Bram Moolenaar51485f02005-06-04 21:55:20 +00004429 /*
4430 * Open the file.
4431 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004432 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004433 if (fd == NULL)
4434 {
4435 EMSG2(_(e_notopen), fname);
4436 return NULL;
4437 }
4438
Bram Moolenaarb765d632005-06-07 21:00:02 +00004439 if (spin->si_verbose || p_verbose > 2)
4440 {
4441 if (!spin->si_verbose)
4442 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004443 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004444 out_flush();
4445 if (!spin->si_verbose)
4446 verbose_leave();
4447 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004448
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004449 /* Only do REP lines when not done in another .aff file already. */
4450 do_rep = spin->si_rep.ga_len == 0;
4451
4452 /* Only do SAL lines when not done in another .aff file already. */
4453 do_sal = spin->si_sal.ga_len == 0;
4454
4455 /* Only do MAP lines when not done in another .aff file already. */
4456 do_map = spin->si_map.ga_len == 0;
4457
Bram Moolenaar51485f02005-06-04 21:55:20 +00004458 /*
4459 * Allocate and init the afffile_T structure.
4460 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004461 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004462 if (aff == NULL)
4463 return NULL;
4464 hash_init(&aff->af_pref);
4465 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004466 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004467
4468 /*
4469 * Read all the lines in the file one by one.
4470 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004471 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004472 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004473 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004474 ++lnum;
4475
4476 /* Skip comment lines. */
4477 if (*rline == '#')
4478 continue;
4479
4480 /* Convert from "SET" to 'encoding' when needed. */
4481 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004482#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004483 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004484 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004485 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004486 if (pc == NULL)
4487 {
4488 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4489 fname, lnum, rline);
4490 continue;
4491 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004492 line = pc;
4493 }
4494 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004495#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004496 {
4497 pc = NULL;
4498 line = rline;
4499 }
4500
4501 /* Split the line up in white separated items. Put a NUL after each
4502 * item. */
4503 itemcnt = 0;
4504 for (p = line; ; )
4505 {
4506 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
4507 ++p;
4508 if (*p == NUL)
4509 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00004510 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004511 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004512 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004513 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004514 ++p;
4515 if (*p == NUL)
4516 break;
4517 *p++ = NUL;
4518 }
4519
4520 /* Handle non-empty lines. */
4521 if (itemcnt > 0)
4522 {
4523 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
4524 && aff->af_enc == NULL)
4525 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004526#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004527 /* Setup for conversion from "ENC" to 'encoding'. */
4528 aff->af_enc = enc_canonize(items[1]);
4529 if (aff->af_enc != NULL && !spin->si_ascii
4530 && convert_setup(&spin->si_conv, aff->af_enc,
4531 p_enc) == FAIL)
4532 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
4533 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004534 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004535#else
4536 smsg((char_u *)_("Conversion in %s not supported"), fname);
4537#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004538 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00004539 else if (STRCMP(items[0], "FLAG") == 0 && itemcnt == 2
4540 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004541 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004542 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00004543 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004544 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00004545 aff->af_flagtype = AFT_NUM;
4546 else if (STRCMP(items[1], "caplong") == 0)
4547 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004548 else
4549 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
4550 fname, lnum, items[1]);
4551 if (aff->af_rar != 0 || aff->af_kep != 0 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004552 || aff->af_needaffix != 0
4553 || aff->af_needcomp != 0
4554 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00004555 || aff->af_suff.ht_used > 0
4556 || aff->af_pref.ht_used > 0)
4557 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
4558 fname, lnum, items[1]);
4559 }
4560 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2
4561 && midword == NULL)
4562 {
4563 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004564 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00004565 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
4566 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004567 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004568 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004569 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004570 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004571 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004572 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004573 else if (STRCMP(items[0], "SLASH") == 0 && itemcnt == 2
4574 && aff->af_slash == 0)
4575 {
4576 aff->af_slash = items[1][0];
4577 if (items[1][1] != NUL)
4578 smsg((char_u *)_("Character used for SLASH must be ASCII; in %s line %d: %s"),
4579 fname, lnum, items[1]);
4580 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004581 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
4582 && aff->af_rar == 0)
4583 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004584 aff->af_rar = affitem2flag(aff->af_flagtype, items[1],
4585 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004586 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004587 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
4588 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004589 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004590 aff->af_kep = affitem2flag(aff->af_flagtype, items[1],
4591 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004592 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00004593 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
4594 && aff->af_bad == 0)
4595 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004596 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
4597 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00004598 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004599 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
4600 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004601 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004602 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
4603 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004604 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004605 else if (STRCMP(items[0], "NEEDCOMPOUND") == 0 && itemcnt == 2
4606 && aff->af_needcomp == 0)
4607 {
4608 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
4609 fname, lnum);
4610 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004611 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004612 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004613 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004614 /* Turn flag "c" into COMPOUNDFLAGS compatible string "c+",
4615 * "Na" into "Na+", "1234" into "1234+". */
4616 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004617 if (p != NULL)
4618 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004619 STRCPY(p, items[1]);
4620 STRCAT(p, "+");
4621 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004622 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004623 }
4624 else if (STRCMP(items[0], "COMPOUNDFLAGS") == 0 && itemcnt == 2)
4625 {
4626 /* Concatenate this string to previously defined ones, using a
4627 * slash to separate them. */
4628 l = STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00004629 if (compflags != NULL)
4630 l += STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004631 p = getroom(spin, l, FALSE);
4632 if (p != NULL)
4633 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004634 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004635 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004636 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004637 STRCAT(p, "/");
4638 }
4639 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004640 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004641 }
4642 }
4643 else if (STRCMP(items[0], "COMPOUNDMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004644 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004645 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004646 compmax = atoi((char *)items[1]);
4647 if (compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004648 smsg((char_u *)_("Wrong COMPOUNDMAX value in %s line %d: %s"),
4649 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004650 }
4651 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004652 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004653 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004654 compminlen = atoi((char *)items[1]);
4655 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004656 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
4657 fname, lnum, items[1]);
4658 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00004659 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004660 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004661 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004662 compsylmax = atoi((char *)items[1]);
4663 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004664 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
4665 fname, lnum, items[1]);
4666 }
4667 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00004668 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00004669 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004670 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00004671 }
Bram Moolenaar78622822005-08-23 21:00:13 +00004672 else if (STRCMP(items[0], "NOBREAK") == 0 && itemcnt == 1)
4673 {
4674 spin->si_nobreak = TRUE;
4675 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004676 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
4677 {
4678 aff->af_pfxpostpone = TRUE;
4679 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004680 else if ((STRCMP(items[0], "PFX") == 0
4681 || STRCMP(items[0], "SFX") == 0)
4682 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00004683 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004684 {
Bram Moolenaar95529562005-08-25 21:21:38 +00004685 int lasti = 4;
4686 char_u key[AH_KEY_LEN];
4687
4688 if (*items[0] == 'P')
4689 tp = &aff->af_pref;
4690 else
4691 tp = &aff->af_suff;
4692
4693 /* Myspell allows the same affix name to be used multiple
4694 * times. The affix files that do this have an undocumented
4695 * "S" flag on all but the last block, thus we check for that
4696 * and store it in ah_follows. */
4697 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
4698 hi = hash_find(tp, key);
4699 if (!HASHITEM_EMPTY(hi))
4700 {
4701 cur_aff = HI2AH(hi);
4702 if (cur_aff->ah_combine != (*items[2] == 'Y'))
4703 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
4704 fname, lnum, items[1]);
4705 if (!cur_aff->ah_follows)
4706 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
4707 fname, lnum, items[1]);
4708 }
4709 else
4710 {
4711 /* New affix letter. */
4712 cur_aff = (affheader_T *)getroom(spin,
4713 sizeof(affheader_T), TRUE);
4714 if (cur_aff == NULL)
4715 break;
4716 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
4717 fname, lnum);
4718 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
4719 break;
4720 if (cur_aff->ah_flag == aff->af_bad
4721 || cur_aff->ah_flag == aff->af_rar
4722 || cur_aff->ah_flag == aff->af_kep
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004723 || cur_aff->ah_flag == aff->af_needaffix
4724 || cur_aff->ah_flag == aff->af_needcomp)
4725 smsg((char_u *)_("Affix also used for BAD/RAR/KEP/NEEDAFFIX/NEEDCOMPOUND in %s line %d: %s"),
Bram Moolenaar95529562005-08-25 21:21:38 +00004726 fname, lnum, items[1]);
4727 STRCPY(cur_aff->ah_key, items[1]);
4728 hash_add(tp, cur_aff->ah_key);
4729
4730 cur_aff->ah_combine = (*items[2] == 'Y');
4731 }
4732
4733 /* Check for the "S" flag, which apparently means that another
4734 * block with the same affix name is following. */
4735 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
4736 {
4737 ++lasti;
4738 cur_aff->ah_follows = TRUE;
4739 }
4740 else
4741 cur_aff->ah_follows = FALSE;
4742
Bram Moolenaar8db73182005-06-17 21:51:16 +00004743 /* Myspell allows extra text after the item, but that might
4744 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00004745 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00004746 smsg((char_u *)_("Trailing text in %s line %d: %s"),
4747 fname, lnum, items[4]);
4748
Bram Moolenaar95529562005-08-25 21:21:38 +00004749 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004750 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
4751 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004752
Bram Moolenaar95529562005-08-25 21:21:38 +00004753 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004754 {
Bram Moolenaar95529562005-08-25 21:21:38 +00004755 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00004756 {
4757 /* Use a new number in the .spl file later, to be able
4758 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004759 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004760 cur_aff->ah_newID = ++spin->si_newprefID;
4761
4762 /* We only really use ah_newID if the prefix is
4763 * postponed. We know that only after handling all
4764 * the items. */
4765 did_postpone_prefix = FALSE;
4766 }
Bram Moolenaar95529562005-08-25 21:21:38 +00004767 else
4768 /* Did use the ID in a previous block. */
4769 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004770 }
Bram Moolenaar95529562005-08-25 21:21:38 +00004771
Bram Moolenaar51485f02005-06-04 21:55:20 +00004772 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004773 }
4774 else if ((STRCMP(items[0], "PFX") == 0
4775 || STRCMP(items[0], "SFX") == 0)
4776 && aff_todo > 0
4777 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00004778 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004779 {
4780 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004781 int rare = FALSE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004782 int nocomp = FALSE;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004783 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004784 int lasti = 5;
4785
Bram Moolenaar5195e452005-08-19 20:32:47 +00004786 /* Check for "rare" and "nocomp" after the other info. */
4787 while (itemcnt > lasti)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004788 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00004789 if (!rare && STRICMP(items[lasti], "rare") == 0)
4790 {
4791 rare = TRUE;
4792 ++lasti;
4793 }
4794 else if (!nocomp && STRICMP(items[lasti], "nocomp") == 0)
4795 {
4796 nocomp = TRUE;
4797 ++lasti;
4798 }
4799 else
4800 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004801 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004802
Bram Moolenaar8db73182005-06-17 21:51:16 +00004803 /* Myspell allows extra text after the item, but that might
4804 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004805 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004806 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00004807
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004808 /* New item for an affix letter. */
4809 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004810 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004811 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004812 if (aff_entry == NULL)
4813 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004814 aff_entry->ae_rare = rare;
Bram Moolenaar5195e452005-08-19 20:32:47 +00004815 aff_entry->ae_nocomp = nocomp;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004816
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004817 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004818 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004819 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004820 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004821
Bram Moolenaar51485f02005-06-04 21:55:20 +00004822 /* Don't use an affix entry with non-ASCII characters when
4823 * "spin->si_ascii" is TRUE. */
4824 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004825 || has_non_ascii(aff_entry->ae_add)))
4826 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00004827 aff_entry->ae_next = cur_aff->ah_first;
4828 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004829
4830 if (STRCMP(items[4], ".") != 0)
4831 {
4832 char_u buf[MAXLINELEN];
4833
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004834 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004835 if (*items[0] == 'P')
4836 sprintf((char *)buf, "^%s", items[4]);
4837 else
4838 sprintf((char *)buf, "%s$", items[4]);
4839 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004840 RE_MAGIC + RE_STRING + RE_STRICT);
4841 if (aff_entry->ae_prog == NULL)
4842 smsg((char_u *)_("Broken condition in %s line %d: %s"),
4843 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004844 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004845
4846 /* For postponed prefixes we need an entry in si_prefcond
4847 * for the condition. Use an existing one if possible. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004848 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004849 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004850 /* When the chop string is one lower-case letter and
4851 * the add string ends in the upper-case letter we set
4852 * the "upper" flag, clear "ae_chop" and remove the
4853 * letters from "ae_add". The condition must either
4854 * be empty or start with the same letter. */
4855 if (aff_entry->ae_chop != NULL
4856 && aff_entry->ae_add != NULL
4857#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004858 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00004859 aff_entry->ae_chop)] == NUL
4860#else
4861 && aff_entry->ae_chop[1] == NUL
4862#endif
4863 )
4864 {
4865 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004866
Bram Moolenaar53805d12005-08-01 07:08:33 +00004867 c = PTR2CHAR(aff_entry->ae_chop);
4868 c_up = SPELL_TOUPPER(c);
4869 if (c_up != c
4870 && (aff_entry->ae_cond == NULL
4871 || PTR2CHAR(aff_entry->ae_cond) == c))
4872 {
4873 p = aff_entry->ae_add
4874 + STRLEN(aff_entry->ae_add);
4875 mb_ptr_back(aff_entry->ae_add, p);
4876 if (PTR2CHAR(p) == c_up)
4877 {
4878 upper = TRUE;
4879 aff_entry->ae_chop = NULL;
4880 *p = NUL;
4881
4882 /* The condition is matched with the
4883 * actual word, thus must check for the
4884 * upper-case letter. */
4885 if (aff_entry->ae_cond != NULL)
4886 {
4887 char_u buf[MAXLINELEN];
4888#ifdef FEAT_MBYTE
4889 if (has_mbyte)
4890 {
4891 onecap_copy(items[4], buf, TRUE);
4892 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004893 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004894 }
4895 else
4896#endif
4897 *aff_entry->ae_cond = c_up;
4898 if (aff_entry->ae_cond != NULL)
4899 {
4900 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004901 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00004902 vim_free(aff_entry->ae_prog);
4903 aff_entry->ae_prog = vim_regcomp(
4904 buf, RE_MAGIC + RE_STRING);
4905 }
4906 }
4907 }
4908 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004909 }
4910
Bram Moolenaar53805d12005-08-01 07:08:33 +00004911 if (aff_entry->ae_chop == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004912 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004913 int idx;
4914 char_u **pp;
4915 int n;
4916
Bram Moolenaar6de68532005-08-24 22:08:48 +00004917 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004918 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
4919 --idx)
4920 {
4921 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
4922 if (str_equal(p, aff_entry->ae_cond))
4923 break;
4924 }
4925 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
4926 {
4927 /* Not found, add a new condition. */
4928 idx = spin->si_prefcond.ga_len++;
4929 pp = ((char_u **)spin->si_prefcond.ga_data)
4930 + idx;
4931 if (aff_entry->ae_cond == NULL)
4932 *pp = NULL;
4933 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004934 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00004935 aff_entry->ae_cond);
4936 }
4937
4938 /* Add the prefix to the prefix tree. */
4939 if (aff_entry->ae_add == NULL)
4940 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004941 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00004942 p = aff_entry->ae_add;
4943 /* PFX_FLAGS is a negative number, so that
4944 * tree_add_word() knows this is the prefix tree. */
4945 n = PFX_FLAGS;
4946 if (rare)
4947 n |= WFP_RARE;
4948 if (!cur_aff->ah_combine)
4949 n |= WFP_NC;
4950 if (upper)
4951 n |= WFP_UP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004952 tree_add_word(spin, p, spin->si_prefroot, n,
4953 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00004954 did_postpone_prefix = TRUE;
4955 }
4956
4957 /* Didn't actually use ah_newID, backup si_newprefID. */
4958 if (aff_todo == 0 && !did_postpone_prefix)
4959 {
4960 --spin->si_newprefID;
4961 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004962 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004963 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00004964 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004965 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00004966 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2
4967 && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004968 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004969 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004970 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00004971 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2
4972 && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004973 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004974 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004975 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00004976 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2
4977 && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004978 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00004979 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004980 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004981 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004982 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004983 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004984 if (!isdigit(*items[1]))
4985 smsg((char_u *)_("Expected REP count in %s line %d"),
4986 fname, lnum);
4987 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004988 else if (STRCMP(items[0], "REP") == 0 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004989 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004990 /* REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004991 /* Myspell ignores extra arguments, we require it starts with
4992 * # to detect mistakes. */
4993 if (itemcnt > 3 && items[3][0] != '#')
4994 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004995 if (do_rep)
4996 add_fromto(spin, &spin->si_rep, items[1], items[2]);
4997 }
4998 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
4999 {
5000 /* MAP item or count */
5001 if (!found_map)
5002 {
5003 /* First line contains the count. */
5004 found_map = TRUE;
5005 if (!isdigit(*items[1]))
5006 smsg((char_u *)_("Expected MAP count in %s line %d"),
5007 fname, lnum);
5008 }
5009 else if (do_map)
5010 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005011 int c;
5012
5013 /* Check that every character appears only once. */
5014 for (p = items[1]; *p != NUL; )
5015 {
5016#ifdef FEAT_MBYTE
5017 c = mb_ptr2char_adv(&p);
5018#else
5019 c = *p++;
5020#endif
5021 if ((spin->si_map.ga_len > 0
5022 && vim_strchr(spin->si_map.ga_data, c)
5023 != NULL)
5024 || vim_strchr(p, c) != NULL)
5025 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5026 fname, lnum);
5027 }
5028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005029 /* We simply concatenate all the MAP strings, separated by
5030 * slashes. */
5031 ga_concat(&spin->si_map, items[1]);
5032 ga_append(&spin->si_map, '/');
5033 }
5034 }
5035 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
5036 {
5037 if (do_sal)
5038 {
5039 /* SAL item (sounds-a-like)
5040 * Either one of the known keys or a from-to pair. */
5041 if (STRCMP(items[1], "followup") == 0)
5042 spin->si_followup = sal_to_bool(items[2]);
5043 else if (STRCMP(items[1], "collapse_result") == 0)
5044 spin->si_collapse = sal_to_bool(items[2]);
5045 else if (STRCMP(items[1], "remove_accents") == 0)
5046 spin->si_rem_accents = sal_to_bool(items[2]);
5047 else
5048 /* when "to" is "_" it means empty */
5049 add_fromto(spin, &spin->si_sal, items[1],
5050 STRCMP(items[2], "_") == 0 ? (char_u *)""
5051 : items[2]);
5052 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005053 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005054 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005055 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005056 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005057 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005058 }
5059 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005060 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005061 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005062 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005063 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005064 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005065 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005066 fname, lnum, items[0]);
5067 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005068 }
5069
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005070 if (fol != NULL || low != NULL || upp != NULL)
5071 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005072 if (spin->si_clear_chartab)
5073 {
5074 /* Clear the char type tables, don't want to use any of the
5075 * currently used spell properties. */
5076 init_spell_chartab();
5077 spin->si_clear_chartab = FALSE;
5078 }
5079
Bram Moolenaar3982c542005-06-08 21:56:31 +00005080 /*
5081 * Don't write a word table for an ASCII file, so that we don't check
5082 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005083 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00005084 * mb_get_class(), the list of chars in the file will be incomplete.
5085 */
5086 if (!spin->si_ascii
5087#ifdef FEAT_MBYTE
5088 && !enc_utf8
5089#endif
5090 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005091 {
5092 if (fol == NULL || low == NULL || upp == NULL)
5093 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
5094 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00005095 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005096 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005097
5098 vim_free(fol);
5099 vim_free(low);
5100 vim_free(upp);
5101 }
5102
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005103 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005104 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005105 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005106 aff_check_number(spin->si_compmax, compmax, "COMPOUNDMAX");
5107 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005108 }
5109
Bram Moolenaar6de68532005-08-24 22:08:48 +00005110 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005111 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005112 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
5113 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005114 }
5115
Bram Moolenaar6de68532005-08-24 22:08:48 +00005116 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005117 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005118 if (syllable == NULL)
5119 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
5120 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
5121 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005122 }
5123
Bram Moolenaar6de68532005-08-24 22:08:48 +00005124 if (compflags != NULL)
5125 process_compflags(spin, aff, compflags);
5126
5127 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005128 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005129 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005130 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005131 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005132 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005133 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005134 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005135 MSG(_("Too many posponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005136 }
5137
Bram Moolenaar6de68532005-08-24 22:08:48 +00005138 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005139 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005140 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
5141 spin->si_syllable = syllable;
5142 }
5143
5144 if (sofofrom != NULL || sofoto != NULL)
5145 {
5146 if (sofofrom == NULL || sofoto == NULL)
5147 smsg((char_u *)_("Missing SOFO%s line in %s"),
5148 sofofrom == NULL ? "FROM" : "TO", fname);
5149 else if (spin->si_sal.ga_len > 0)
5150 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005151 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005152 {
5153 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
5154 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
5155 spin->si_sofofr = sofofrom;
5156 spin->si_sofoto = sofoto;
5157 }
5158 }
5159
5160 if (midword != NULL)
5161 {
5162 aff_check_string(spin->si_midword, midword, "MIDWORD");
5163 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005164 }
5165
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005166 vim_free(pc);
5167 fclose(fd);
5168 return aff;
5169}
5170
5171/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005172 * Turn an affix flag name into a number, according to the FLAG type.
5173 * returns zero for failure.
5174 */
5175 static unsigned
5176affitem2flag(flagtype, item, fname, lnum)
5177 int flagtype;
5178 char_u *item;
5179 char_u *fname;
5180 int lnum;
5181{
5182 unsigned res;
5183 char_u *p = item;
5184
5185 res = get_affitem(flagtype, &p);
5186 if (res == 0)
5187 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005188 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005189 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
5190 fname, lnum, item);
5191 else
5192 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
5193 fname, lnum, item);
5194 }
5195 if (*p != NUL)
5196 {
5197 smsg((char_u *)_(e_affname), fname, lnum, item);
5198 return 0;
5199 }
5200
5201 return res;
5202}
5203
5204/*
5205 * Get one affix name from "*pp" and advance the pointer.
5206 * Returns zero for an error, still advances the pointer then.
5207 */
5208 static unsigned
5209get_affitem(flagtype, pp)
5210 int flagtype;
5211 char_u **pp;
5212{
5213 int res;
5214
Bram Moolenaar95529562005-08-25 21:21:38 +00005215 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005216 {
5217 if (!VIM_ISDIGIT(**pp))
5218 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005219 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005220 return 0;
5221 }
5222 res = getdigits(pp);
5223 }
5224 else
5225 {
5226#ifdef FEAT_MBYTE
5227 res = mb_ptr2char_adv(pp);
5228#else
5229 res = *(*pp)++;
5230#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00005231 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00005232 && res >= 'A' && res <= 'Z'))
5233 {
5234 if (**pp == NUL)
5235 return 0;
5236#ifdef FEAT_MBYTE
5237 res = mb_ptr2char_adv(pp) + (res << 16);
5238#else
5239 res = *(*pp)++ + (res << 16);
5240#endif
5241 }
5242 }
5243 return res;
5244}
5245
5246/*
5247 * Process the "compflags" string used in an affix file and append it to
5248 * spin->si_compflags.
5249 * The processing involves changing the affix names to ID numbers, so that
5250 * they fit in one byte.
5251 */
5252 static void
5253process_compflags(spin, aff, compflags)
5254 spellinfo_T *spin;
5255 afffile_T *aff;
5256 char_u *compflags;
5257{
5258 char_u *p;
5259 char_u *prevp;
5260 unsigned flag;
5261 compitem_T *ci;
5262 int id;
5263 int len;
5264 char_u *tp;
5265 char_u key[AH_KEY_LEN];
5266 hashitem_T *hi;
5267
5268 /* Make room for the old and the new compflags, concatenated with a / in
5269 * between. Processing it makes it shorter, but we don't know by how
5270 * much, thus allocate the maximum. */
5271 len = STRLEN(compflags) + 1;
5272 if (spin->si_compflags != NULL)
5273 len += STRLEN(spin->si_compflags) + 1;
5274 p = getroom(spin, len, FALSE);
5275 if (p == NULL)
5276 return;
5277 if (spin->si_compflags != NULL)
5278 {
5279 STRCPY(p, spin->si_compflags);
5280 STRCAT(p, "/");
5281 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005282 spin->si_compflags = p;
5283 tp = p + STRLEN(p);
5284
5285 for (p = compflags; *p != NUL; )
5286 {
5287 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
5288 /* Copy non-flag characters directly. */
5289 *tp++ = *p++;
5290 else
5291 {
5292 /* First get the flag number, also checks validity. */
5293 prevp = p;
5294 flag = get_affitem(aff->af_flagtype, &p);
5295 if (flag != 0)
5296 {
5297 /* Find the flag in the hashtable. If it was used before, use
5298 * the existing ID. Otherwise add a new entry. */
5299 vim_strncpy(key, prevp, p - prevp);
5300 hi = hash_find(&aff->af_comp, key);
5301 if (!HASHITEM_EMPTY(hi))
5302 id = HI2CI(hi)->ci_newID;
5303 else
5304 {
5305 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
5306 if (ci == NULL)
5307 break;
5308 STRCPY(ci->ci_key, key);
5309 ci->ci_flag = flag;
5310 /* Avoid using a flag ID that has a special meaning in a
5311 * regexp (also inside []). */
5312 do
5313 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005314 check_renumber(spin);
5315 id = spin->si_newcompID--;
5316 } while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005317 ci->ci_newID = id;
5318 hash_add(&aff->af_comp, ci->ci_key);
5319 }
5320 *tp++ = id;
5321 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005322 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005323 ++p;
5324 }
5325 }
5326
5327 *tp = NUL;
5328}
5329
5330/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005331 * Check that the new IDs for postponed affixes and compounding don't overrun
5332 * each other. We have almost 255 available, but start at 0-127 to avoid
5333 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
5334 * When that is used up an error message is given.
5335 */
5336 static void
5337check_renumber(spin)
5338 spellinfo_T *spin;
5339{
5340 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
5341 {
5342 spin->si_newprefID = 127;
5343 spin->si_newcompID = 255;
5344 }
5345}
5346
5347/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005348 * Return TRUE if flag "flag" appears in affix list "afflist".
5349 */
5350 static int
5351flag_in_afflist(flagtype, afflist, flag)
5352 int flagtype;
5353 char_u *afflist;
5354 unsigned flag;
5355{
5356 char_u *p;
5357 unsigned n;
5358
5359 switch (flagtype)
5360 {
5361 case AFT_CHAR:
5362 return vim_strchr(afflist, flag) != NULL;
5363
Bram Moolenaar95529562005-08-25 21:21:38 +00005364 case AFT_CAPLONG:
5365 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00005366 for (p = afflist; *p != NUL; )
5367 {
5368#ifdef FEAT_MBYTE
5369 n = mb_ptr2char_adv(&p);
5370#else
5371 n = *p++;
5372#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00005373 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00005374 && *p != NUL)
5375#ifdef FEAT_MBYTE
5376 n = mb_ptr2char_adv(&p) + (n << 16);
5377#else
5378 n = *p++ + (n << 16);
5379#endif
5380 if (n == flag)
5381 return TRUE;
5382 }
5383 break;
5384
Bram Moolenaar95529562005-08-25 21:21:38 +00005385 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00005386 for (p = afflist; *p != NUL; )
5387 {
5388 n = getdigits(&p);
5389 if (n == flag)
5390 return TRUE;
5391 if (*p != NUL) /* skip over comma */
5392 ++p;
5393 }
5394 break;
5395 }
5396 return FALSE;
5397}
5398
5399/*
5400 * Give a warning when "spinval" and "affval" numbers are set and not the same.
5401 */
5402 static void
5403aff_check_number(spinval, affval, name)
5404 int spinval;
5405 int affval;
5406 char *name;
5407{
5408 if (spinval != 0 && spinval != affval)
5409 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
5410}
5411
5412/*
5413 * Give a warning when "spinval" and "affval" strings are set and not the same.
5414 */
5415 static void
5416aff_check_string(spinval, affval, name)
5417 char_u *spinval;
5418 char_u *affval;
5419 char *name;
5420{
5421 if (spinval != NULL && STRCMP(spinval, affval) != 0)
5422 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
5423}
5424
5425/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005426 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
5427 * NULL as equal.
5428 */
5429 static int
5430str_equal(s1, s2)
5431 char_u *s1;
5432 char_u *s2;
5433{
5434 if (s1 == NULL || s2 == NULL)
5435 return s1 == s2;
5436 return STRCMP(s1, s2) == 0;
5437}
5438
5439/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005440 * Add a from-to item to "gap". Used for REP and SAL items.
5441 * They are stored case-folded.
5442 */
5443 static void
5444add_fromto(spin, gap, from, to)
5445 spellinfo_T *spin;
5446 garray_T *gap;
5447 char_u *from;
5448 char_u *to;
5449{
5450 fromto_T *ftp;
5451 char_u word[MAXWLEN];
5452
5453 if (ga_grow(gap, 1) == OK)
5454 {
5455 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
5456 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005457 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005458 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005459 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005460 ++gap->ga_len;
5461 }
5462}
5463
5464/*
5465 * Convert a boolean argument in a SAL line to TRUE or FALSE;
5466 */
5467 static int
5468sal_to_bool(s)
5469 char_u *s;
5470{
5471 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
5472}
5473
5474/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00005475 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
5476 * When "s" is NULL FALSE is returned.
5477 */
5478 static int
5479has_non_ascii(s)
5480 char_u *s;
5481{
5482 char_u *p;
5483
5484 if (s != NULL)
5485 for (p = s; *p != NUL; ++p)
5486 if (*p >= 128)
5487 return TRUE;
5488 return FALSE;
5489}
5490
5491/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005492 * Free the structure filled by spell_read_aff().
5493 */
5494 static void
5495spell_free_aff(aff)
5496 afffile_T *aff;
5497{
5498 hashtab_T *ht;
5499 hashitem_T *hi;
5500 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005501 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005502 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005503
5504 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005505
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005506 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005507 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
5508 {
5509 todo = ht->ht_used;
5510 for (hi = ht->ht_array; todo > 0; ++hi)
5511 {
5512 if (!HASHITEM_EMPTY(hi))
5513 {
5514 --todo;
5515 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005516 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
5517 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005518 }
5519 }
5520 if (ht == &aff->af_suff)
5521 break;
5522 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005523
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005524 hash_clear(&aff->af_pref);
5525 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005526 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005527}
5528
5529/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005530 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005531 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005532 */
5533 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005534spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005535 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005536 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005537 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005538{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005539 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005540 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005541 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005542 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005543 char_u store_afflist[MAXWLEN];
5544 int pfxlen;
5545 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005546 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005547 char_u *pc;
5548 char_u *w;
5549 int l;
5550 hash_T hash;
5551 hashitem_T *hi;
5552 FILE *fd;
5553 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005554 int non_ascii = 0;
5555 int retval = OK;
5556 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005557 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005558 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005559
Bram Moolenaar51485f02005-06-04 21:55:20 +00005560 /*
5561 * Open the file.
5562 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005563 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005564 if (fd == NULL)
5565 {
5566 EMSG2(_(e_notopen), fname);
5567 return FAIL;
5568 }
5569
Bram Moolenaar51485f02005-06-04 21:55:20 +00005570 /* The hashtable is only used to detect duplicated words. */
5571 hash_init(&ht);
5572
Bram Moolenaarb765d632005-06-07 21:00:02 +00005573 if (spin->si_verbose || p_verbose > 2)
5574 {
5575 if (!spin->si_verbose)
5576 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005577 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005578 out_flush();
5579 if (!spin->si_verbose)
5580 verbose_leave();
5581 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005582
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005583 /* start with a message for the first line */
5584 spin->si_msg_count = 999999;
5585
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005586 /* Read and ignore the first line: word count. */
5587 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005588 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005589 EMSG2(_("E760: No word count in %s"), fname);
5590
5591 /*
5592 * Read all the lines in the file one by one.
5593 * The words are converted to 'encoding' here, before being added to
5594 * the hashtable.
5595 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005596 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005597 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005598 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005599 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005600 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005601 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005602
Bram Moolenaar51485f02005-06-04 21:55:20 +00005603 /* Remove CR, LF and white space from the end. White space halfway
5604 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005605 l = STRLEN(line);
5606 while (l > 0 && line[l - 1] <= ' ')
5607 --l;
5608 if (l == 0)
5609 continue; /* empty line */
5610 line[l] = NUL;
5611
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005612 /* Find the optional affix names. Replace the SLASH character by a
5613 * slash. */
5614 afflist = NULL;
5615 for (p = line; *p != NUL; mb_ptr_adv(p))
5616 {
5617 if (*p == affile->af_slash)
5618 *p = '/';
5619 else if (*p == '/')
5620 {
5621 *p = NUL;
5622 afflist = p + 1;
5623 break;
5624 }
5625 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005626
5627 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
5628 if (spin->si_ascii && has_non_ascii(line))
5629 {
5630 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005631 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005632 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005633
Bram Moolenaarb765d632005-06-07 21:00:02 +00005634#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005635 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005636 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005637 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005638 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005639 if (pc == NULL)
5640 {
5641 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5642 fname, lnum, line);
5643 continue;
5644 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005645 w = pc;
5646 }
5647 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005648#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005649 {
5650 pc = NULL;
5651 w = line;
5652 }
5653
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005654 /* This takes time, print a message every 10000 words. */
5655 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005656 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005657 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005658 vim_snprintf((char *)message, sizeof(message),
5659 _("line %6d, word %6d - %s"),
5660 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
5661 msg_start();
5662 msg_puts_long_attr(message, 0);
5663 msg_clr_eos();
5664 msg_didout = FALSE;
5665 msg_col = 0;
5666 out_flush();
5667 }
5668
Bram Moolenaar51485f02005-06-04 21:55:20 +00005669 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005670 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005671 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005672 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005673 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005674 if (retval == FAIL)
5675 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005676
Bram Moolenaar51485f02005-06-04 21:55:20 +00005677 hash = hash_hash(dw);
5678 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005679 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005680 {
5681 if (p_verbose > 0)
5682 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005683 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005684 else if (duplicate == 0)
5685 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
5686 fname, lnum, dw);
5687 ++duplicate;
5688 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005689 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00005690 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005691
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005692 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005693 store_afflist[0] = NUL;
5694 pfxlen = 0;
5695 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005696 if (afflist != NULL)
5697 {
5698 /* Check for affix name that stands for keep-case word and stands
5699 * for rare word (if defined). */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005700 if (affile->af_kep != 0 && flag_in_afflist(
5701 affile->af_flagtype, afflist, affile->af_kep))
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00005702 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005703 if (affile->af_rar != 0 && flag_in_afflist(
5704 affile->af_flagtype, afflist, affile->af_rar))
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005705 flags |= WF_RARE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005706 if (affile->af_bad != 0 && flag_in_afflist(
5707 affile->af_flagtype, afflist, affile->af_bad))
Bram Moolenaar0c405862005-06-22 22:26:26 +00005708 flags |= WF_BANNED;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005709 if (affile->af_needaffix != 0 && flag_in_afflist(
5710 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00005711 need_affix = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005712 if (affile->af_needcomp != 0 && flag_in_afflist(
5713 affile->af_flagtype, afflist, affile->af_needcomp))
5714 flags |= WF_NEEDCOMP;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005715
5716 if (affile->af_pfxpostpone)
5717 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005718 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005719
Bram Moolenaar5195e452005-08-19 20:32:47 +00005720 if (spin->si_compflags != NULL)
5721 /* Need to store the list of compound flags with the word.
5722 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005723 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005724 }
5725
Bram Moolenaar51485f02005-06-04 21:55:20 +00005726 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005727 if (store_word(spin, dw, flags, spin->si_region,
5728 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005729 retval = FAIL;
5730
5731 if (afflist != NULL)
5732 {
5733 /* Find all matching suffixes and add the resulting words.
5734 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005735 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005736 &affile->af_suff, &affile->af_pref,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005737 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005738 retval = FAIL;
5739
5740 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005741 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005742 &affile->af_pref, NULL,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005743 FALSE, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005744 retval = FAIL;
5745 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005746 }
5747
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005748 if (duplicate > 0)
5749 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005750 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005751 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
5752 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005753 hash_clear(&ht);
5754
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005755 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005756 return retval;
5757}
5758
5759/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005760 * Get the list of prefix IDs from the affix list "afflist".
5761 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00005762 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
5763 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005764 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005765 static int
5766get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005767 afffile_T *affile;
5768 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005769 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005770{
5771 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005772 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005773 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005774 int id;
5775 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005776 hashitem_T *hi;
5777
Bram Moolenaar6de68532005-08-24 22:08:48 +00005778 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005779 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005780 prevp = p;
5781 if (get_affitem(affile->af_flagtype, &p) != 0)
5782 {
5783 /* A flag is a postponed prefix flag if it appears in "af_pref"
5784 * and it's ID is not zero. */
5785 vim_strncpy(key, prevp, p - prevp);
5786 hi = hash_find(&affile->af_pref, key);
5787 if (!HASHITEM_EMPTY(hi))
5788 {
5789 id = HI2AH(hi)->ah_newID;
5790 if (id != 0)
5791 store_afflist[cnt++] = id;
5792 }
5793 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005794 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005795 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005796 }
5797
Bram Moolenaar5195e452005-08-19 20:32:47 +00005798 store_afflist[cnt] = NUL;
5799 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005800}
5801
5802/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005803 * Get the list of compound IDs from the affix list "afflist" that are used
5804 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00005805 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005806 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005807 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00005808get_compflags(affile, afflist, store_afflist)
5809 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005810 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005811 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005812{
5813 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005814 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005815 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005816 char_u key[AH_KEY_LEN];
5817 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005818
Bram Moolenaar6de68532005-08-24 22:08:48 +00005819 for (p = afflist; *p != NUL; )
5820 {
5821 prevp = p;
5822 if (get_affitem(affile->af_flagtype, &p) != 0)
5823 {
5824 /* A flag is a compound flag if it appears in "af_comp". */
5825 vim_strncpy(key, prevp, p - prevp);
5826 hi = hash_find(&affile->af_comp, key);
5827 if (!HASHITEM_EMPTY(hi))
5828 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
5829 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005830 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00005831 ++p;
5832 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005833
Bram Moolenaar5195e452005-08-19 20:32:47 +00005834 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005835}
5836
5837/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005838 * Apply affixes to a word and store the resulting words.
5839 * "ht" is the hashtable with affentry_T that need to be applied, either
5840 * prefixes or suffixes.
5841 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
5842 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005843 *
5844 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005845 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005846 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00005847store_aff_word(spin, word, afflist, affile, ht, xht, comb, flags,
5848 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005849 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005850 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005851 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005852 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005853 hashtab_T *ht;
5854 hashtab_T *xht;
5855 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005856 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005857 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005858 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
5859 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005860{
5861 int todo;
5862 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005863 affheader_T *ah;
5864 affentry_T *ae;
5865 regmatch_T regmatch;
5866 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005867 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005868 int i;
5869 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005870 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005871 char_u *use_pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005872 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00005873 size_t wordlen = STRLEN(word);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005874
Bram Moolenaar51485f02005-06-04 21:55:20 +00005875 todo = ht->ht_used;
5876 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005877 {
5878 if (!HASHITEM_EMPTY(hi))
5879 {
5880 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005881 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00005882
Bram Moolenaar51485f02005-06-04 21:55:20 +00005883 /* Check that the affix combines, if required, and that the word
5884 * supports this affix. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005885 if ((!comb || ah->ah_combine) && flag_in_afflist(
5886 affile->af_flagtype, afflist, ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00005887 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005888 /* Loop over all affix entries with this name. */
5889 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005890 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005891 /* Check the condition. It's not logical to match case
5892 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005893 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005894 * Another requirement from Myspell is that the chop
5895 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005896 * For prefixes, when "PFXPOSTPONE" was used, only do
5897 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005898 regmatch.regprog = ae->ae_prog;
5899 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005900 if ((xht != NULL || !affile->af_pfxpostpone
5901 || ae->ae_chop != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005902 && (ae->ae_chop == NULL
5903 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005904 && (ae->ae_prog == NULL
5905 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005906 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005907 /* Match. Remove the chop and add the affix. */
5908 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005909 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005910 /* prefix: chop/add at the start of the word */
5911 if (ae->ae_add == NULL)
5912 *newword = NUL;
5913 else
5914 STRCPY(newword, ae->ae_add);
5915 p = word;
5916 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005917 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005918 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005919#ifdef FEAT_MBYTE
5920 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005921 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005922 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005923 for ( ; i > 0; --i)
5924 mb_ptr_adv(p);
5925 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005926 else
5927#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005928 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005929 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005930 STRCAT(newword, p);
5931 }
5932 else
5933 {
5934 /* suffix: chop/add at the end of the word */
5935 STRCPY(newword, word);
5936 if (ae->ae_chop != NULL)
5937 {
5938 /* Remove chop string. */
5939 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00005940 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005941 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005942 mb_ptr_back(newword, p);
5943 *p = NUL;
5944 }
5945 if (ae->ae_add != NULL)
5946 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005947 }
5948
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005949 /* Obey the "rare" flag of the affix. */
5950 if (ae->ae_rare)
5951 use_flags = flags | WF_RARE;
5952 else
5953 use_flags = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005954
5955 /* Obey the "nocomp" flag of the affix: don't use the
5956 * compound flags. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005957 use_pfxlist = pfxlist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005958 if (ae->ae_nocomp && pfxlist != NULL)
5959 {
5960 vim_strncpy(pfx_pfxlist, pfxlist, pfxlen);
5961 use_pfxlist = pfx_pfxlist;
5962 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005963
5964 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00005965 if (spin->si_prefroot != NULL
5966 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005967 {
5968 /* ... add a flag to indicate an affix was used. */
5969 use_flags |= WF_HAS_AFF;
5970
5971 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00005972 * affixes is not allowed. But do use the
5973 * compound flags after them. */
5974 if ((!ah->ah_combine || comb) && pfxlist != NULL)
5975 use_pfxlist += pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005976 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005977
Bram Moolenaar51485f02005-06-04 21:55:20 +00005978 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005979 if (store_word(spin, newword, use_flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005980 spin->si_region, use_pfxlist, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005981 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005982
Bram Moolenaar51485f02005-06-04 21:55:20 +00005983 /* When added a suffix and combining is allowed also
5984 * try adding prefixes additionally. */
5985 if (xht != NULL && ah->ah_combine)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005986 if (store_aff_word(spin, newword, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005987 xht, NULL, TRUE,
Bram Moolenaar5195e452005-08-19 20:32:47 +00005988 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005989 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005990 }
5991 }
5992 }
5993 }
5994 }
5995
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005996 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005997}
5998
5999/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006000 * Read a file with a list of words.
6001 */
6002 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006003spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006004 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006005 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006006{
6007 FILE *fd;
6008 long lnum = 0;
6009 char_u rline[MAXLINELEN];
6010 char_u *line;
6011 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006012 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006013 int l;
6014 int retval = OK;
6015 int did_word = FALSE;
6016 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006017 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006018 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006019
6020 /*
6021 * Open the file.
6022 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006023 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006024 if (fd == NULL)
6025 {
6026 EMSG2(_(e_notopen), fname);
6027 return FAIL;
6028 }
6029
Bram Moolenaarb765d632005-06-07 21:00:02 +00006030 if (spin->si_verbose || p_verbose > 2)
6031 {
6032 if (!spin->si_verbose)
6033 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006034 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006035 out_flush();
6036 if (!spin->si_verbose)
6037 verbose_leave();
6038 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006039
6040 /*
6041 * Read all the lines in the file one by one.
6042 */
6043 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
6044 {
6045 line_breakcheck();
6046 ++lnum;
6047
6048 /* Skip comment lines. */
6049 if (*rline == '#')
6050 continue;
6051
6052 /* Remove CR, LF and white space from the end. */
6053 l = STRLEN(rline);
6054 while (l > 0 && rline[l - 1] <= ' ')
6055 --l;
6056 if (l == 0)
6057 continue; /* empty or blank line */
6058 rline[l] = NUL;
6059
6060 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
6061 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006062#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00006063 if (spin->si_conv.vc_type != CONV_NONE)
6064 {
6065 pc = string_convert(&spin->si_conv, rline, NULL);
6066 if (pc == NULL)
6067 {
6068 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6069 fname, lnum, rline);
6070 continue;
6071 }
6072 line = pc;
6073 }
6074 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006075#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006076 {
6077 pc = NULL;
6078 line = rline;
6079 }
6080
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006081 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00006082 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006083 ++line;
6084 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006085 {
6086 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006087 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
6088 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006089 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006090 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
6091 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006092 else
6093 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006094#ifdef FEAT_MBYTE
6095 char_u *enc;
6096
Bram Moolenaar51485f02005-06-04 21:55:20 +00006097 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006098 line += 10;
6099 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006100 if (enc != NULL && !spin->si_ascii
6101 && convert_setup(&spin->si_conv, enc,
6102 p_enc) == FAIL)
6103 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00006104 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006105 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006106 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006107#else
6108 smsg((char_u *)_("Conversion in %s not supported"), fname);
6109#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006110 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006111 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006112 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006113
Bram Moolenaar3982c542005-06-08 21:56:31 +00006114 if (STRNCMP(line, "regions=", 8) == 0)
6115 {
6116 if (spin->si_region_count > 1)
6117 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
6118 fname, lnum, line);
6119 else
6120 {
6121 line += 8;
6122 if (STRLEN(line) > 16)
6123 smsg((char_u *)_("Too many regions in %s line %d: %s"),
6124 fname, lnum, line);
6125 else
6126 {
6127 spin->si_region_count = STRLEN(line) / 2;
6128 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006129
6130 /* Adjust the mask for a word valid in all regions. */
6131 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006132 }
6133 }
6134 continue;
6135 }
6136
Bram Moolenaar7887d882005-07-01 22:33:52 +00006137 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
6138 fname, lnum, line - 1);
6139 continue;
6140 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006141
Bram Moolenaar7887d882005-07-01 22:33:52 +00006142 flags = 0;
6143 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006144
Bram Moolenaar7887d882005-07-01 22:33:52 +00006145 /* Check for flags and region after a slash. */
6146 p = vim_strchr(line, '/');
6147 if (p != NULL)
6148 {
6149 *p++ = NUL;
6150 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006151 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006152 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006153 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006154 else if (*p == '!') /* Bad, bad, wicked word. */
6155 flags |= WF_BANNED;
6156 else if (*p == '?') /* Rare word. */
6157 flags |= WF_RARE;
6158 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006159 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00006160 if ((flags & WF_REGION) == 0) /* first one */
6161 regionmask = 0;
6162 flags |= WF_REGION;
6163
6164 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00006165 if (l > spin->si_region_count)
6166 {
6167 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00006168 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006169 break;
6170 }
6171 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00006172 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00006173 else
6174 {
6175 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
6176 fname, lnum, p);
6177 break;
6178 }
6179 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006180 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006181 }
6182
6183 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6184 if (spin->si_ascii && has_non_ascii(line))
6185 {
6186 ++non_ascii;
6187 continue;
6188 }
6189
6190 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006191 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006192 {
6193 retval = FAIL;
6194 break;
6195 }
6196 did_word = TRUE;
6197 }
6198
6199 vim_free(pc);
6200 fclose(fd);
6201
Bram Moolenaarb765d632005-06-07 21:00:02 +00006202 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
6203 {
6204 if (p_verbose > 2)
6205 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00006206 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
6207 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006208 if (p_verbose > 2)
6209 verbose_leave();
6210 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006211 return retval;
6212}
6213
6214/*
6215 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006216 * This avoids calling free() for every little struct we use (and keeping
6217 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00006218 * The memory is cleared to all zeros.
6219 * Returns NULL when out of memory.
6220 */
6221 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006222getroom(spin, len, align)
6223 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006224 size_t len; /* length needed */
6225 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006226{
6227 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006228 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006229
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00006230 if (align && bl != NULL)
6231 /* Round size up for alignment. On some systems structures need to be
6232 * aligned to the size of a pointer (e.g., SPARC). */
6233 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
6234 & ~(sizeof(char *) - 1);
6235
Bram Moolenaar51485f02005-06-04 21:55:20 +00006236 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
6237 {
6238 /* Allocate a block of memory. This is not freed until much later. */
6239 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
6240 if (bl == NULL)
6241 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006242 bl->sb_next = spin->si_blocks;
6243 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006244 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006245 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006246 }
6247
6248 p = bl->sb_data + bl->sb_used;
6249 bl->sb_used += len;
6250
6251 return p;
6252}
6253
6254/*
6255 * Make a copy of a string into memory allocated with getroom().
6256 */
6257 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006258getroom_save(spin, s)
6259 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006260 char_u *s;
6261{
6262 char_u *sc;
6263
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006264 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006265 if (sc != NULL)
6266 STRCPY(sc, s);
6267 return sc;
6268}
6269
6270
6271/*
6272 * Free the list of allocated sblock_T.
6273 */
6274 static void
6275free_blocks(bl)
6276 sblock_T *bl;
6277{
6278 sblock_T *next;
6279
6280 while (bl != NULL)
6281 {
6282 next = bl->sb_next;
6283 vim_free(bl);
6284 bl = next;
6285 }
6286}
6287
6288/*
6289 * Allocate the root of a word tree.
6290 */
6291 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006292wordtree_alloc(spin)
6293 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006294{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006295 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006296}
6297
6298/*
6299 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006300 * Always store it in the case-folded tree. For a keep-case word this is
6301 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
6302 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006303 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006304 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
6305 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00006306 */
6307 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00006308store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006309 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006310 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006311 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006312 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006313 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006314 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006315{
6316 int len = STRLEN(word);
6317 int ct = captype(word, word + len);
6318 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006319 int res = OK;
6320 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006321
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006322 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006323 for (p = pfxlist; res == OK; ++p)
6324 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006325 if (!need_affix || (p != NULL && *p != NUL))
6326 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006327 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006328 if (p == NULL || *p == NUL)
6329 break;
6330 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00006331 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006332
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006333 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00006334 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006335 for (p = pfxlist; res == OK; ++p)
6336 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006337 if (!need_affix || (p != NULL && *p != NUL))
6338 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006339 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006340 if (p == NULL || *p == NUL)
6341 break;
6342 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00006343 ++spin->si_keepwcount;
6344 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006345 return res;
6346}
6347
6348/*
6349 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006350 * When "flags" < 0 we are adding to the prefix tree where flags is used for
6351 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006352 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006353 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006354 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006355tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006356 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006357 char_u *word;
6358 wordnode_T *root;
6359 int flags;
6360 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006361 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006362{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006363 wordnode_T *node = root;
6364 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006365 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006366 wordnode_T **prev = NULL;
6367 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006368
Bram Moolenaar51485f02005-06-04 21:55:20 +00006369 /* Add each byte of the word to the tree, including the NUL at the end. */
6370 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006371 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006372 /* When there is more than one reference to this node we need to make
6373 * a copy, so that we can modify it. Copy the whole list of siblings
6374 * (we don't optimize for a partly shared list of siblings). */
6375 if (node != NULL && node->wn_refs > 1)
6376 {
6377 --node->wn_refs;
6378 copyprev = prev;
6379 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
6380 {
6381 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006382 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006383 if (np == NULL)
6384 return FAIL;
6385 np->wn_child = copyp->wn_child;
6386 if (np->wn_child != NULL)
6387 ++np->wn_child->wn_refs; /* child gets extra ref */
6388 np->wn_byte = copyp->wn_byte;
6389 if (np->wn_byte == NUL)
6390 {
6391 np->wn_flags = copyp->wn_flags;
6392 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006393 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006394 }
6395
6396 /* Link the new node in the list, there will be one ref. */
6397 np->wn_refs = 1;
6398 *copyprev = np;
6399 copyprev = &np->wn_sibling;
6400
6401 /* Let "node" point to the head of the copied list. */
6402 if (copyp == node)
6403 node = np;
6404 }
6405 }
6406
Bram Moolenaar51485f02005-06-04 21:55:20 +00006407 /* Look for the sibling that has the same character. They are sorted
6408 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006409 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006410 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006411 while (node != NULL
6412 && (node->wn_byte < word[i]
6413 || (node->wn_byte == NUL
6414 && (flags < 0
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006415 ? node->wn_affixID < affixID
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006416 : node->wn_flags < (flags & WN_MASK)
6417 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006418 && node->wn_affixID < affixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006419 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006420 prev = &node->wn_sibling;
6421 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006422 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006423 if (node == NULL
6424 || node->wn_byte != word[i]
6425 || (word[i] == NUL
6426 && (flags < 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006427 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006428 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006429 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006430 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006431 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006432 if (np == NULL)
6433 return FAIL;
6434 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006435
6436 /* If "node" is NULL this is a new child or the end of the sibling
6437 * list: ref count is one. Otherwise use ref count of sibling and
6438 * make ref count of sibling one (matters when inserting in front
6439 * of the list of siblings). */
6440 if (node == NULL)
6441 np->wn_refs = 1;
6442 else
6443 {
6444 np->wn_refs = node->wn_refs;
6445 node->wn_refs = 1;
6446 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006447 *prev = np;
6448 np->wn_sibling = node;
6449 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006450 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006451
Bram Moolenaar51485f02005-06-04 21:55:20 +00006452 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006453 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006454 node->wn_flags = flags;
6455 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006456 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006457 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00006458 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006459 prev = &node->wn_child;
6460 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006461 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006462#ifdef SPELL_PRINTTREE
6463 smsg("Added \"%s\"", word);
6464 spell_print_tree(root->wn_sibling);
6465#endif
6466
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006467 /* count nr of words added since last message */
6468 ++spin->si_msg_count;
6469
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006470 if (spin->si_compress_cnt > 1)
6471 {
6472 if (--spin->si_compress_cnt == 1)
6473 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006474 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006475 }
6476
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006477 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006478 * When we have allocated lots of memory we need to compress the word tree
6479 * to free up some room. But compression is slow, and we might actually
6480 * need that room, thus only compress in the following situations:
6481 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00006482 * "compress_start" blocks.
6483 * 2. When compressed before and used "compress_inc" blocks before
6484 * adding "compress_added" words (si_compress_cnt > 1).
6485 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006486 * (si_compress_cnt == 1) and the number of free nodes drops below the
6487 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006488 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006489#ifndef SPELL_PRINTTREE
6490 if (spin->si_compress_cnt == 1
6491 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00006492 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006493#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006494 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006495 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00006496 * when the freed up room has been used and another "compress_inc"
6497 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006498 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006499 spin->si_blocks_cnt -= compress_inc;
6500 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006501
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006502 if (spin->si_verbose)
6503 {
6504 msg_start();
6505 msg_puts((char_u *)_(msg_compressing));
6506 msg_clr_eos();
6507 msg_didout = FALSE;
6508 msg_col = 0;
6509 out_flush();
6510 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006511
6512 /* Compress both trees. Either they both have many nodes, which makes
6513 * compression useful, or one of them is small, which means
6514 * compression goes fast. */
6515 wordtree_compress(spin, spin->si_foldroot);
6516 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006517 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006518
6519 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006520}
6521
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006522/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00006523 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
6524 * Sets "sps_flags".
6525 */
6526 int
6527spell_check_msm()
6528{
6529 char_u *p = p_msm;
6530 long start = 0;
6531 long inc = 0;
6532 long added = 0;
6533
6534 if (!VIM_ISDIGIT(*p))
6535 return FAIL;
6536 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
6537 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
6538 if (*p != ',')
6539 return FAIL;
6540 ++p;
6541 if (!VIM_ISDIGIT(*p))
6542 return FAIL;
6543 inc = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
6544 if (*p != ',')
6545 return FAIL;
6546 ++p;
6547 if (!VIM_ISDIGIT(*p))
6548 return FAIL;
6549 added = getdigits(&p) * 1024;
6550 if (*p != NUL)
6551 return FAIL;
6552
6553 if (start == 0 || inc == 0 || added == 0 || inc > start)
6554 return FAIL;
6555
6556 compress_start = start;
6557 compress_inc = inc;
6558 compress_added = added;
6559 return OK;
6560}
6561
6562
6563/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006564 * Get a wordnode_T, either from the list of previously freed nodes or
6565 * allocate a new one.
6566 */
6567 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006568get_wordnode(spin)
6569 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006570{
6571 wordnode_T *n;
6572
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006573 if (spin->si_first_free == NULL)
6574 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006575 else
6576 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006577 n = spin->si_first_free;
6578 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006579 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006580 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006581 }
6582#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006583 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006584#endif
6585 return n;
6586}
6587
6588/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006589 * Decrement the reference count on a node (which is the head of a list of
6590 * siblings). If the reference count becomes zero free the node and its
6591 * siblings.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006592 */
6593 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006594deref_wordnode(spin, node)
6595 spellinfo_T *spin;
6596 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006597{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006598 wordnode_T *np;
6599
6600 if (--node->wn_refs == 0)
6601 for (np = node; np != NULL; np = np->wn_sibling)
6602 {
6603 if (np->wn_child != NULL)
6604 deref_wordnode(spin, np->wn_child);
6605 free_wordnode(spin, np);
6606 }
6607}
6608
6609/*
6610 * Free a wordnode_T for re-use later.
6611 * Only the "wn_child" field becomes invalid.
6612 */
6613 static void
6614free_wordnode(spin, n)
6615 spellinfo_T *spin;
6616 wordnode_T *n;
6617{
6618 n->wn_child = spin->si_first_free;
6619 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006620 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006621}
6622
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006623/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006624 * Compress a tree: find tails that are identical and can be shared.
6625 */
6626 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006627wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006628 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006629 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006630{
6631 hashtab_T ht;
6632 int n;
6633 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006634 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006635
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006636 /* Skip the root itself, it's not actually used. The first sibling is the
6637 * start of the tree. */
6638 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006639 {
6640 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006641 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006642
6643#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00006644 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006645#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00006646 {
6647 if (!spin->si_verbose)
6648 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006649 if (tot > 1000000)
6650 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006651 else if (tot == 0)
6652 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006653 else
6654 perc = (tot - n) * 100 / tot;
Bram Moolenaarb765d632005-06-07 21:00:02 +00006655 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006656 n, tot, perc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006657 if (p_verbose > 2)
6658 verbose_leave();
6659 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006660#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006661 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006662#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006663 hash_clear(&ht);
6664 }
6665}
6666
6667/*
6668 * Compress a node, its siblings and its children, depth first.
6669 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006670 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006671 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006672node_compress(spin, node, ht, tot)
6673 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006674 wordnode_T *node;
6675 hashtab_T *ht;
6676 int *tot; /* total count of nodes before compressing,
6677 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006678{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006679 wordnode_T *np;
6680 wordnode_T *tp;
6681 wordnode_T *child;
6682 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006683 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006684 int len = 0;
6685 unsigned nr, n;
6686 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006687
Bram Moolenaar51485f02005-06-04 21:55:20 +00006688 /*
6689 * Go through the list of siblings. Compress each child and then try
6690 * finding an identical child to replace it.
6691 * Note that with "child" we mean not just the node that is pointed to,
6692 * but the whole list of siblings, of which the node is the first.
6693 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006694 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006695 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006696 ++len;
6697 if ((child = np->wn_child) != NULL)
6698 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006699 /* Compress the child. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006700 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006701
6702 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006703 hash = hash_hash(child->wn_u1.hashkey);
6704 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006705 tp = NULL;
6706 if (!HASHITEM_EMPTY(hi))
6707 {
6708 /* There are children with an identical hash value. Now check
6709 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006710 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006711 if (node_equal(child, tp))
6712 {
6713 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00006714 * current one. This means the current child and all
6715 * its siblings is unlinked from the tree. */
6716 ++tp->wn_refs;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006717 deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006718 np->wn_child = tp;
6719 ++compressed;
6720 break;
6721 }
6722 if (tp == NULL)
6723 {
6724 /* No other child with this hash value equals the child of
6725 * the node, add it to the linked list after the first
6726 * item. */
6727 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006728 child->wn_u2.next = tp->wn_u2.next;
6729 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006730 }
6731 }
6732 else
6733 /* No other child has this hash value, add it to the
6734 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006735 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006736 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006737 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006738 *tot += len;
6739
6740 /*
6741 * Make a hash key for the node and its siblings, so that we can quickly
6742 * find a lookalike node. This must be done after compressing the sibling
6743 * list, otherwise the hash key would become invalid by the compression.
6744 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006745 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006746 nr = 0;
6747 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006748 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006749 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006750 /* end node: use wn_flags, wn_region and wn_affixID */
6751 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006752 else
6753 /* byte node: use the byte value and the child pointer */
6754 n = np->wn_byte + ((long_u)np->wn_child << 8);
6755 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006756 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006757
6758 /* Avoid NUL bytes, it terminates the hash key. */
6759 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006760 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006761 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006762 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006763 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006764 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006765 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00006766 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
6767 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006768
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006769 /* Check for CTRL-C pressed now and then. */
6770 fast_breakcheck();
6771
Bram Moolenaar51485f02005-06-04 21:55:20 +00006772 return compressed;
6773}
6774
6775/*
6776 * Return TRUE when two nodes have identical siblings and children.
6777 */
6778 static int
6779node_equal(n1, n2)
6780 wordnode_T *n1;
6781 wordnode_T *n2;
6782{
6783 wordnode_T *p1;
6784 wordnode_T *p2;
6785
6786 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
6787 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
6788 if (p1->wn_byte != p2->wn_byte
6789 || (p1->wn_byte == NUL
6790 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006791 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006792 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006793 : (p1->wn_child != p2->wn_child)))
6794 break;
6795
6796 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006797}
6798
6799/*
6800 * Write a number to file "fd", MSB first, in "len" bytes.
6801 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006802 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006803put_bytes(fd, nr, len)
6804 FILE *fd;
6805 long_u nr;
6806 int len;
6807{
6808 int i;
6809
6810 for (i = len - 1; i >= 0; --i)
6811 putc((int)(nr >> (i * 8)), fd);
6812}
6813
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006814static int
6815#ifdef __BORLANDC__
6816_RTLENTRYF
6817#endif
6818rep_compare __ARGS((const void *s1, const void *s2));
6819
6820/*
6821 * Function given to qsort() to sort the REP items on "from" string.
6822 */
6823 static int
6824#ifdef __BORLANDC__
6825_RTLENTRYF
6826#endif
6827rep_compare(s1, s2)
6828 const void *s1;
6829 const void *s2;
6830{
6831 fromto_T *p1 = (fromto_T *)s1;
6832 fromto_T *p2 = (fromto_T *)s2;
6833
6834 return STRCMP(p1->ft_from, p2->ft_from);
6835}
6836
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006837/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00006838 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006839 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006840 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006841 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006842write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006843 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006844 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006845{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006846 FILE *fd;
6847 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006848 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006849 wordnode_T *tree;
6850 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006851 int i;
6852 int l;
6853 garray_T *gap;
6854 fromto_T *ftp;
6855 char_u *p;
6856 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006857 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006858
Bram Moolenaarb765d632005-06-07 21:00:02 +00006859 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006860 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006861 {
6862 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006863 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006864 }
6865
Bram Moolenaar5195e452005-08-19 20:32:47 +00006866 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006867 /* <fileID> */
6868 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006869 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006870 EMSG(_(e_write));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006871 retval = FAIL;
6872 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00006873 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006874
Bram Moolenaar5195e452005-08-19 20:32:47 +00006875 /*
6876 * <SECTIONS>: <section> ... <sectionend>
6877 */
6878
6879 /* SN_REGION: <regionname> ...
6880 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006881 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006882 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006883 putc(SN_REGION, fd); /* <sectionID> */
6884 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6885 l = spin->si_region_count * 2;
6886 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6887 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
6888 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00006889 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006890 }
6891 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006892 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006893
Bram Moolenaar5195e452005-08-19 20:32:47 +00006894 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
6895 *
6896 * The table with character flags and the table for case folding.
6897 * This makes sure the same characters are recognized as word characters
6898 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006899 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006900 * 'encoding'.
6901 * Also skip this for an .add.spl file, the main spell file must contain
6902 * the table (avoids that it conflicts). File is shorter too.
6903 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006904 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006905 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006906 char_u folchars[128 * 8];
6907 int flags;
6908
Bram Moolenaard12a1322005-08-21 22:08:24 +00006909 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006910 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6911
6912 /* Form the <folchars> string first, we need to know its length. */
6913 l = 0;
6914 for (i = 128; i < 256; ++i)
6915 {
6916#ifdef FEAT_MBYTE
6917 if (has_mbyte)
6918 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
6919 else
6920#endif
6921 folchars[l++] = spelltab.st_fold[i];
6922 }
6923 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
6924
6925 fputc(128, fd); /* <charflagslen> */
6926 for (i = 128; i < 256; ++i)
6927 {
6928 flags = 0;
6929 if (spelltab.st_isw[i])
6930 flags |= CF_WORD;
6931 if (spelltab.st_isu[i])
6932 flags |= CF_UPPER;
6933 fputc(flags, fd); /* <charflags> */
6934 }
6935
6936 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
6937 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006938 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006939
Bram Moolenaar5195e452005-08-19 20:32:47 +00006940 /* SN_MIDWORD: <midword> */
6941 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006942 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006943 putc(SN_MIDWORD, fd); /* <sectionID> */
6944 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6945
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006946 i = STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006947 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006948 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
6949 }
6950
Bram Moolenaar5195e452005-08-19 20:32:47 +00006951 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
6952 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006953 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006954 putc(SN_PREFCOND, fd); /* <sectionID> */
6955 putc(SNF_REQUIRED, fd); /* <sectionflags> */
6956
6957 l = write_spell_prefcond(NULL, &spin->si_prefcond);
6958 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
6959
6960 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006961 }
6962
Bram Moolenaar5195e452005-08-19 20:32:47 +00006963 /* SN_REP: <repcount> <rep> ...
6964 * SN_SAL: <salflags> <salcount> <sal> ... */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006965
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006966 /* Sort the REP items. */
6967 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
6968 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006969
Bram Moolenaar5195e452005-08-19 20:32:47 +00006970 /* round 1: SN_REP section
6971 * round 2: SN_SAL section (unless SN_SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006972 for (round = 1; round <= 2; ++round)
6973 {
6974 if (round == 1)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006975 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006976 gap = &spin->si_rep;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006977 putc(SN_REP, fd); /* <sectionID> */
6978 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006979 else
6980 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00006981 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
6982 /* using SN_SOFO section instead of SN_SAL */
6983 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006984 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006985 putc(SN_SAL, fd); /* <sectionID> */
6986 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006987
Bram Moolenaar5195e452005-08-19 20:32:47 +00006988 /* This is for making suggestions, section is not required. */
6989 putc(0, fd); /* <sectionflags> */
6990
6991 /* Compute the length of what follows. */
6992 l = 2; /* count <repcount> or <salcount> */
6993 for (i = 0; i < gap->ga_len; ++i)
6994 {
6995 ftp = &((fromto_T *)gap->ga_data)[i];
6996 l += 1 + STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
6997 l += 1 + STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
6998 }
6999 if (round == 2)
7000 ++l; /* count <salflags> */
7001 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7002
7003 if (round == 2)
7004 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007005 i = 0;
7006 if (spin->si_followup)
7007 i |= SAL_F0LLOWUP;
7008 if (spin->si_collapse)
7009 i |= SAL_COLLAPSE;
7010 if (spin->si_rem_accents)
7011 i |= SAL_REM_ACCENTS;
7012 putc(i, fd); /* <salflags> */
7013 }
7014
7015 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
7016 for (i = 0; i < gap->ga_len; ++i)
7017 {
7018 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
7019 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
7020 ftp = &((fromto_T *)gap->ga_data)[i];
7021 for (rr = 1; rr <= 2; ++rr)
7022 {
7023 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
7024 l = STRLEN(p);
7025 putc(l, fd);
7026 fwrite(p, l, (size_t)1, fd);
7027 }
7028 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007029
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007030 }
7031
Bram Moolenaar5195e452005-08-19 20:32:47 +00007032 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
7033 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007034 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
7035 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007036 putc(SN_SOFO, fd); /* <sectionID> */
7037 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007038
7039 l = STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007040 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
7041 /* <sectionlen> */
7042
7043 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
7044 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007045
7046 l = STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007047 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
7048 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007049 }
7050
Bram Moolenaar5195e452005-08-19 20:32:47 +00007051 /* SN_MAP: <mapstr>
7052 * This is for making suggestions, section is not required. */
7053 if (spin->si_map.ga_len > 0)
7054 {
7055 putc(SN_MAP, fd); /* <sectionID> */
7056 putc(0, fd); /* <sectionflags> */
7057 l = spin->si_map.ga_len;
7058 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7059 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
7060 /* <mapstr> */
7061 }
7062
7063 /* SN_COMPOUND: compound info.
7064 * We don't mark it required, when not supported all compound words will
7065 * be bad words. */
7066 if (spin->si_compflags != NULL)
7067 {
7068 putc(SN_COMPOUND, fd); /* <sectionID> */
7069 putc(0, fd); /* <sectionflags> */
7070
7071 l = STRLEN(spin->si_compflags);
7072 put_bytes(fd, (long_u)(l + 3), 4); /* <sectionlen> */
7073 putc(spin->si_compmax, fd); /* <compmax> */
7074 putc(spin->si_compminlen, fd); /* <compminlen> */
7075 putc(spin->si_compsylmax, fd); /* <compsylmax> */
7076 /* <compflags> */
7077 fwrite(spin->si_compflags, (size_t)l, (size_t)1, fd);
7078 }
7079
Bram Moolenaar78622822005-08-23 21:00:13 +00007080 /* SN_NOBREAK: NOBREAK flag */
7081 if (spin->si_nobreak)
7082 {
7083 putc(SN_NOBREAK, fd); /* <sectionID> */
7084 putc(0, fd); /* <sectionflags> */
7085
7086 /* It's empty, the precense of the section flags the feature. */
7087 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
7088 }
7089
Bram Moolenaar5195e452005-08-19 20:32:47 +00007090 /* SN_SYLLABLE: syllable info.
7091 * We don't mark it required, when not supported syllables will not be
7092 * counted. */
7093 if (spin->si_syllable != NULL)
7094 {
7095 putc(SN_SYLLABLE, fd); /* <sectionID> */
7096 putc(0, fd); /* <sectionflags> */
7097
7098 l = STRLEN(spin->si_syllable);
7099 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7100 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
7101 }
7102
7103 /* end of <SECTIONS> */
7104 putc(SN_END, fd); /* <sectionend> */
7105
Bram Moolenaar50cde822005-06-05 21:54:54 +00007106
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007107 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007108 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007109 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007110 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007111 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007112 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007113 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007114 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007115 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007116 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007117 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007118 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007119
Bram Moolenaar0c405862005-06-22 22:26:26 +00007120 /* Clear the index and wnode fields in the tree. */
7121 clear_node(tree);
7122
Bram Moolenaar51485f02005-06-04 21:55:20 +00007123 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00007124 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00007125 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007126 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007127
Bram Moolenaar51485f02005-06-04 21:55:20 +00007128 /* number of nodes in 4 bytes */
7129 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00007130 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007131
Bram Moolenaar51485f02005-06-04 21:55:20 +00007132 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007133 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007134 }
7135
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007136 /* Write another byte to check for errors. */
7137 if (putc(0, fd) == EOF)
7138 retval = FAIL;
7139
7140 if (fclose(fd) == EOF)
7141 retval = FAIL;
7142
7143 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007144}
7145
7146/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007147 * Clear the index and wnode fields of "node", it siblings and its
7148 * children. This is needed because they are a union with other items to save
7149 * space.
7150 */
7151 static void
7152clear_node(node)
7153 wordnode_T *node;
7154{
7155 wordnode_T *np;
7156
7157 if (node != NULL)
7158 for (np = node; np != NULL; np = np->wn_sibling)
7159 {
7160 np->wn_u1.index = 0;
7161 np->wn_u2.wnode = NULL;
7162
7163 if (np->wn_byte != NUL)
7164 clear_node(np->wn_child);
7165 }
7166}
7167
7168
7169/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007170 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007171 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00007172 * This first writes the list of possible bytes (siblings). Then for each
7173 * byte recursively write the children.
7174 *
7175 * NOTE: The code here must match the code in read_tree(), since assumptions
7176 * are made about the indexes (so that we don't have to write them in the
7177 * file).
7178 *
7179 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007180 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007181 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00007182put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007183 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007184 wordnode_T *node;
7185 int index;
7186 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007187 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007188{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007189 int newindex = index;
7190 int siblingcount = 0;
7191 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007192 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007193
Bram Moolenaar51485f02005-06-04 21:55:20 +00007194 /* If "node" is zero the tree is empty. */
7195 if (node == NULL)
7196 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007197
Bram Moolenaar51485f02005-06-04 21:55:20 +00007198 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007199 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007200
7201 /* Count the number of siblings. */
7202 for (np = node; np != NULL; np = np->wn_sibling)
7203 ++siblingcount;
7204
7205 /* Write the sibling count. */
7206 if (fd != NULL)
7207 putc(siblingcount, fd); /* <siblingcount> */
7208
7209 /* Write each sibling byte and optionally extra info. */
7210 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007211 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007212 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007213 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007214 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007215 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007216 /* For a NUL byte (end of word) write the flags etc. */
7217 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007218 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007219 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007220 * associated condition nr (stored in wn_region). The
7221 * byte value is misused to store the "rare" and "not
7222 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00007223 if (np->wn_flags == (short_u)PFX_FLAGS)
7224 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007225 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00007226 {
7227 putc(BY_FLAGS, fd); /* <byte> */
7228 putc(np->wn_flags, fd); /* <pflags> */
7229 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007230 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007231 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007232 }
7233 else
7234 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007235 /* For word trees we write the flag/region items. */
7236 flags = np->wn_flags;
7237 if (regionmask != 0 && np->wn_region != regionmask)
7238 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007239 if (np->wn_affixID != 0)
7240 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007241 if (flags == 0)
7242 {
7243 /* word without flags or region */
7244 putc(BY_NOFLAGS, fd); /* <byte> */
7245 }
7246 else
7247 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007248 if (np->wn_flags >= 0x100)
7249 {
7250 putc(BY_FLAGS2, fd); /* <byte> */
7251 putc(flags, fd); /* <flags> */
7252 putc((unsigned)flags >> 8, fd); /* <flags2> */
7253 }
7254 else
7255 {
7256 putc(BY_FLAGS, fd); /* <byte> */
7257 putc(flags, fd); /* <flags> */
7258 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007259 if (flags & WF_REGION)
7260 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007261 if (flags & WF_AFX)
7262 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007263 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007264 }
7265 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00007266 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007267 else
7268 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00007269 if (np->wn_child->wn_u1.index != 0
7270 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007271 {
7272 /* The child is written elsewhere, write the reference. */
7273 if (fd != NULL)
7274 {
7275 putc(BY_INDEX, fd); /* <byte> */
7276 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007277 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007278 }
7279 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00007280 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007281 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007282 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007283
Bram Moolenaar51485f02005-06-04 21:55:20 +00007284 if (fd != NULL)
7285 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
7286 {
7287 EMSG(_(e_write));
7288 return 0;
7289 }
7290 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007291 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007292
7293 /* Space used in the array when reading: one for each sibling and one for
7294 * the count. */
7295 newindex += siblingcount + 1;
7296
7297 /* Recursively dump the children of each sibling. */
7298 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00007299 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
7300 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007301 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007302
7303 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007304}
7305
7306
7307/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00007308 * ":mkspell [-ascii] outfile infile ..."
7309 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007310 */
7311 void
7312ex_mkspell(eap)
7313 exarg_T *eap;
7314{
7315 int fcount;
7316 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007317 char_u *arg = eap->arg;
7318 int ascii = FALSE;
7319
7320 if (STRNCMP(arg, "-ascii", 6) == 0)
7321 {
7322 ascii = TRUE;
7323 arg = skipwhite(arg + 6);
7324 }
7325
7326 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
7327 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
7328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007329 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007330 FreeWild(fcount, fnames);
7331 }
7332}
7333
7334/*
7335 * Create a Vim spell file from one or more word lists.
7336 * "fnames[0]" is the output file name.
7337 * "fnames[fcount - 1]" is the last input file name.
7338 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
7339 * and ".spl" is appended to make the output file name.
7340 */
7341 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007342mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007343 int fcount;
7344 char_u **fnames;
7345 int ascii; /* -ascii argument given */
7346 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007347 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007348{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007349 char_u fname[MAXPATHL];
7350 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00007351 char_u **innames;
7352 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007353 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007354 int i;
7355 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007356 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007357 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007358 spellinfo_T spin;
7359
7360 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007361 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007362 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007363 spin.si_followup = TRUE;
7364 spin.si_rem_accents = TRUE;
7365 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
7366 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
7367 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007368 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007369 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007370
Bram Moolenaarb765d632005-06-07 21:00:02 +00007371 /* default: fnames[0] is output file, following are input files */
7372 innames = &fnames[1];
7373 incount = fcount - 1;
7374
7375 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00007376 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007377 len = STRLEN(fnames[0]);
7378 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
7379 {
7380 /* For ":mkspell path/en.latin1.add" output file is
7381 * "path/en.latin1.add.spl". */
7382 innames = &fnames[0];
7383 incount = 1;
7384 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
7385 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007386 else if (fcount == 1)
7387 {
7388 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
7389 innames = &fnames[0];
7390 incount = 1;
7391 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
7392 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
7393 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007394 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
7395 {
7396 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007397 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007398 }
7399 else
7400 /* Name should be language, make the file name from it. */
7401 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
7402 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
7403
7404 /* Check for .ascii.spl. */
7405 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
7406 spin.si_ascii = TRUE;
7407
7408 /* Check for .add.spl. */
7409 if (strstr((char *)gettail(wfname), ".add.") != NULL)
7410 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00007411 }
7412
Bram Moolenaarb765d632005-06-07 21:00:02 +00007413 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007414 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007415 else if (vim_strchr(gettail(wfname), '_') != NULL)
7416 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00007417 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007418 EMSG(_("E754: Only up to 8 regions supported"));
7419 else
7420 {
7421 /* Check for overwriting before doing things that may take a lot of
7422 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007423 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007424 {
7425 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00007426 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007427 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007428 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007429 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007430 EMSG2(_(e_isadir2), wfname);
7431 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007432 }
7433
7434 /*
7435 * Init the aff and dic pointers.
7436 * Get the region names if there are more than 2 arguments.
7437 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007438 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007439 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007440 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007441
Bram Moolenaar3982c542005-06-08 21:56:31 +00007442 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007443 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007444 len = STRLEN(innames[i]);
7445 if (STRLEN(gettail(innames[i])) < 5
7446 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007447 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007448 EMSG2(_("E755: Invalid region in %s"), innames[i]);
7449 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007450 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00007451 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
7452 spin.si_region_name[i * 2 + 1] =
7453 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007454 }
7455 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00007456 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007457
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007458 spin.si_foldroot = wordtree_alloc(&spin);
7459 spin.si_keeproot = wordtree_alloc(&spin);
7460 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007461 if (spin.si_foldroot == NULL
7462 || spin.si_keeproot == NULL
7463 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007464 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007465 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007466 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007467 }
7468
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007469 /* When not producing a .add.spl file clear the character table when
7470 * we encounter one in the .aff file. This means we dump the current
7471 * one in the .spl file if the .aff file doesn't define one. That's
7472 * better than guessing the contents, the table will match a
7473 * previously loaded spell file. */
7474 if (!spin.si_add)
7475 spin.si_clear_chartab = TRUE;
7476
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007477 /*
7478 * Read all the .aff and .dic files.
7479 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007480 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007481 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007482 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007483 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007484 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007485 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007486
Bram Moolenaarb765d632005-06-07 21:00:02 +00007487 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007488 if (mch_stat((char *)fname, &st) >= 0)
7489 {
7490 /* Read the .aff file. Will init "spin->si_conv" based on the
7491 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007492 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007493 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007494 error = TRUE;
7495 else
7496 {
7497 /* Read the .dic file and store the words in the trees. */
7498 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00007499 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007500 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007501 error = TRUE;
7502 }
7503 }
7504 else
7505 {
7506 /* No .aff file, try reading the file as a word list. Store
7507 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007508 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007509 error = TRUE;
7510 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007511
Bram Moolenaarb765d632005-06-07 21:00:02 +00007512#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007513 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007514 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007515#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007516 }
7517
Bram Moolenaar78622822005-08-23 21:00:13 +00007518 if (spin.si_compflags != NULL && spin.si_nobreak)
7519 MSG(_("Warning: both compounding and NOBREAK specified"));
7520
Bram Moolenaar51485f02005-06-04 21:55:20 +00007521 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007522 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007523 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007524 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007525 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007526 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007527 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007528 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007529 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007530 MSG(_(msg_compressing));
Bram Moolenaarb765d632005-06-07 21:00:02 +00007531 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007532 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007533 verbose_leave();
7534 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007535 wordtree_compress(&spin, spin.si_foldroot);
7536 wordtree_compress(&spin, spin.si_keeproot);
7537 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007538 }
7539
Bram Moolenaar51485f02005-06-04 21:55:20 +00007540 if (!error)
7541 {
7542 /*
7543 * Write the info in the spell file.
7544 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007545 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007546 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007547 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007548 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007549 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007550 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007551 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007552 verbose_leave();
7553 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00007554
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007555 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007556
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007557 if (spin.si_verbose || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007558 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007559 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007560 verbose_enter();
7561 MSG(_("Done!"));
7562 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00007563 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007564 out_flush();
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007565 if (!spin.si_verbose)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007566 verbose_leave();
7567 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007568
Bram Moolenaarb765d632005-06-07 21:00:02 +00007569 /* If the file is loaded need to reload it. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007570 if (!error)
7571 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007572 }
7573
7574 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007575 ga_clear(&spin.si_rep);
7576 ga_clear(&spin.si_sal);
7577 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007578 ga_clear(&spin.si_prefcond);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007579
7580 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007581 for (i = 0; i < incount; ++i)
7582 if (afile[i] != NULL)
7583 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007584
7585 /* Free all the bits and pieces at once. */
7586 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007587 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007588}
7589
Bram Moolenaarb765d632005-06-07 21:00:02 +00007590
7591/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007592 * ":[count]spellgood {word}"
7593 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00007594 */
7595 void
7596ex_spell(eap)
7597 exarg_T *eap;
7598{
Bram Moolenaar7887d882005-07-01 22:33:52 +00007599 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007600 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007601}
7602
7603/*
7604 * Add "word[len]" to 'spellfile' as a good or bad word.
7605 */
7606 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007607spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007608 char_u *word;
7609 int len;
7610 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007611 int index; /* "zG" and "zW": zero, otherwise index in
7612 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007613{
7614 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007615 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007616 int new_spf = FALSE;
7617 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007618 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007619 char_u fnamebuf[MAXPATHL];
7620 char_u line[MAXWLEN * 2];
7621 long fpos, fpos_next = 0;
7622 int i;
7623 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007624
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007625 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007626 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007627 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00007628 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007629 int_wordlist = vim_tempname('s');
7630 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00007631 return;
7632 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007633 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007634 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007635 else
7636 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007637 /* If 'spellfile' isn't set figure out a good default value. */
7638 if (*curbuf->b_p_spf == NUL)
7639 {
7640 init_spellfile();
7641 new_spf = TRUE;
7642 }
7643
7644 if (*curbuf->b_p_spf == NUL)
7645 {
7646 EMSG(_("E764: 'spellfile' is not set"));
7647 return;
7648 }
7649
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007650 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
7651 {
7652 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
7653 if (i == index)
7654 break;
7655 if (*spf == NUL)
7656 {
7657 EMSGN(_("E765: 'spellfile' does not have %ld enties"), index);
7658 return;
7659 }
7660 }
7661
Bram Moolenaarb765d632005-06-07 21:00:02 +00007662 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007663 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007664 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
7665 buf = NULL;
7666 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00007667 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007668 EMSG(_(e_bufloaded));
7669 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007670 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007671
Bram Moolenaarf9184a12005-07-02 23:10:47 +00007672 fname = fnamebuf;
7673 }
7674
7675 if (bad)
7676 {
7677 /* When the word also appears as good word we need to remove that one,
7678 * since its flags sort before the one with WF_BANNED. */
7679 fd = mch_fopen((char *)fname, "r");
7680 if (fd != NULL)
7681 {
7682 while (!vim_fgets(line, MAXWLEN * 2, fd))
7683 {
7684 fpos = fpos_next;
7685 fpos_next = ftell(fd);
7686 if (STRNCMP(word, line, len) == 0
7687 && (line[len] == '/' || line[len] < ' '))
7688 {
7689 /* Found duplicate word. Remove it by writing a '#' at
7690 * the start of the line. Mixing reading and writing
7691 * doesn't work for all systems, close the file first. */
7692 fclose(fd);
7693 fd = mch_fopen((char *)fname, "r+");
7694 if (fd == NULL)
7695 break;
7696 if (fseek(fd, fpos, SEEK_SET) == 0)
7697 fputc('#', fd);
7698 fseek(fd, fpos_next, SEEK_SET);
7699 }
7700 }
7701 fclose(fd);
7702 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007703 }
7704
7705 fd = mch_fopen((char *)fname, "a");
7706 if (fd == NULL && new_spf)
7707 {
7708 /* We just initialized the 'spellfile' option and can't open the file.
7709 * We may need to create the "spell" directory first. We already
7710 * checked the runtime directory is writable in init_spellfile(). */
7711 STRCPY(NameBuff, fname);
7712 *gettail_sep(NameBuff) = NUL;
7713 if (mch_stat((char *)NameBuff, &st) < 0)
7714 {
7715 /* The directory doesn't exist. Try creating it and opening the
7716 * file again. */
7717 vim_mkdir(NameBuff, 0755);
7718 fd = mch_fopen((char *)fname, "a");
7719 }
7720 }
7721
7722 if (fd == NULL)
7723 EMSG2(_(e_notopen), fname);
7724 else
7725 {
7726 if (bad)
7727 fprintf(fd, "%.*s/!\n", len, word);
7728 else
7729 fprintf(fd, "%.*s\n", len, word);
7730 fclose(fd);
7731
7732 /* Update the .add.spl file. */
7733 mkspell(1, &fname, FALSE, TRUE, TRUE);
7734
7735 /* If the .add file is edited somewhere, reload it. */
7736 if (buf != NULL)
7737 buf_reload(buf);
7738
7739 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007740 }
7741}
7742
7743/*
7744 * Initialize 'spellfile' for the current buffer.
7745 */
7746 static void
7747init_spellfile()
7748{
7749 char_u buf[MAXPATHL];
7750 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007751 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007752 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007753 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007754 int aspath = FALSE;
7755 char_u *lstart = curbuf->b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007756
7757 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
7758 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007759 /* Find the end of the language name. Exclude the region. If there
7760 * is a path separator remember the start of the tail. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007761 for (lend = curbuf->b_p_spl; *lend != NUL
7762 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007763 if (vim_ispathsep(*lend))
7764 {
7765 aspath = TRUE;
7766 lstart = lend + 1;
7767 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007768
7769 /* Loop over all entries in 'runtimepath'. Use the first one where we
7770 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007771 rtp = p_rtp;
7772 while (*rtp != NUL)
7773 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007774 if (aspath)
7775 /* Use directory of an entry with path, e.g., for
7776 * "/dir/lg.utf-8.spl" use "/dir". */
7777 vim_strncpy(buf, curbuf->b_p_spl, lstart - curbuf->b_p_spl - 1);
7778 else
7779 /* Copy the path from 'runtimepath' to buf[]. */
7780 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00007781 if (filewritable(buf) == 2)
7782 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00007783 /* Use the first language name from 'spelllang' and the
7784 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007785 if (aspath)
7786 vim_strncpy(buf, curbuf->b_p_spl, lend - curbuf->b_p_spl);
7787 else
7788 {
7789 l = STRLEN(buf);
7790 vim_snprintf((char *)buf + l, MAXPATHL - l,
7791 "/spell/%.*s", (int)(lend - lstart), lstart);
7792 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00007793 l = STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007794 fname = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang->sl_fname;
7795 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
7796 fname != NULL
7797 && strstr((char *)gettail(fname), ".ascii.") != NULL
7798 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00007799 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
7800 break;
7801 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00007802 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007803 }
7804 }
7805}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007806
Bram Moolenaar51485f02005-06-04 21:55:20 +00007807
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007808/*
7809 * Init the chartab used for spelling for ASCII.
7810 * EBCDIC is not supported!
7811 */
7812 static void
7813clear_spell_chartab(sp)
7814 spelltab_T *sp;
7815{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007816 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007817
7818 /* Init everything to FALSE. */
7819 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
7820 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
7821 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007822 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007823 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007824 sp->st_upper[i] = i;
7825 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007826
7827 /* We include digits. A word shouldn't start with a digit, but handling
7828 * that is done separately. */
7829 for (i = '0'; i <= '9'; ++i)
7830 sp->st_isw[i] = TRUE;
7831 for (i = 'A'; i <= 'Z'; ++i)
7832 {
7833 sp->st_isw[i] = TRUE;
7834 sp->st_isu[i] = TRUE;
7835 sp->st_fold[i] = i + 0x20;
7836 }
7837 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007838 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007839 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007840 sp->st_upper[i] = i - 0x20;
7841 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007842}
7843
7844/*
7845 * Init the chartab used for spelling. Only depends on 'encoding'.
7846 * Called once while starting up and when 'encoding' changes.
7847 * The default is to use isalpha(), but the spell file should define the word
7848 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007849 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007850 */
7851 void
7852init_spell_chartab()
7853{
7854 int i;
7855
7856 did_set_spelltab = FALSE;
7857 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007858#ifdef FEAT_MBYTE
7859 if (enc_dbcs)
7860 {
7861 /* DBCS: assume double-wide characters are word characters. */
7862 for (i = 128; i <= 255; ++i)
7863 if (MB_BYTE2LEN(i) == 2)
7864 spelltab.st_isw[i] = TRUE;
7865 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007866 else if (enc_utf8)
7867 {
7868 for (i = 128; i < 256; ++i)
7869 {
7870 spelltab.st_isu[i] = utf_isupper(i);
7871 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
7872 spelltab.st_fold[i] = utf_fold(i);
7873 spelltab.st_upper[i] = utf_toupper(i);
7874 }
7875 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007876 else
7877#endif
7878 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007879 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007880 for (i = 128; i < 256; ++i)
7881 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007882 if (MB_ISUPPER(i))
7883 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007884 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007885 spelltab.st_isu[i] = TRUE;
7886 spelltab.st_fold[i] = MB_TOLOWER(i);
7887 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007888 else if (MB_ISLOWER(i))
7889 {
7890 spelltab.st_isw[i] = TRUE;
7891 spelltab.st_upper[i] = MB_TOUPPER(i);
7892 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007893 }
7894 }
7895}
7896
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007897/*
7898 * Set the spell character tables from strings in the affix file.
7899 */
7900 static int
7901set_spell_chartab(fol, low, upp)
7902 char_u *fol;
7903 char_u *low;
7904 char_u *upp;
7905{
7906 /* We build the new tables here first, so that we can compare with the
7907 * previous one. */
7908 spelltab_T new_st;
7909 char_u *pf = fol, *pl = low, *pu = upp;
7910 int f, l, u;
7911
7912 clear_spell_chartab(&new_st);
7913
7914 while (*pf != NUL)
7915 {
7916 if (*pl == NUL || *pu == NUL)
7917 {
7918 EMSG(_(e_affform));
7919 return FAIL;
7920 }
7921#ifdef FEAT_MBYTE
7922 f = mb_ptr2char_adv(&pf);
7923 l = mb_ptr2char_adv(&pl);
7924 u = mb_ptr2char_adv(&pu);
7925#else
7926 f = *pf++;
7927 l = *pl++;
7928 u = *pu++;
7929#endif
7930 /* Every character that appears is a word character. */
7931 if (f < 256)
7932 new_st.st_isw[f] = TRUE;
7933 if (l < 256)
7934 new_st.st_isw[l] = TRUE;
7935 if (u < 256)
7936 new_st.st_isw[u] = TRUE;
7937
7938 /* if "LOW" and "FOL" are not the same the "LOW" char needs
7939 * case-folding */
7940 if (l < 256 && l != f)
7941 {
7942 if (f >= 256)
7943 {
7944 EMSG(_(e_affrange));
7945 return FAIL;
7946 }
7947 new_st.st_fold[l] = f;
7948 }
7949
7950 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007951 * case-folding, it's upper case and the "UPP" is the upper case of
7952 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007953 if (u < 256 && u != f)
7954 {
7955 if (f >= 256)
7956 {
7957 EMSG(_(e_affrange));
7958 return FAIL;
7959 }
7960 new_st.st_fold[u] = f;
7961 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007962 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007963 }
7964 }
7965
7966 if (*pl != NUL || *pu != NUL)
7967 {
7968 EMSG(_(e_affform));
7969 return FAIL;
7970 }
7971
7972 return set_spell_finish(&new_st);
7973}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007974
7975/*
7976 * Set the spell character tables from strings in the .spl file.
7977 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007978 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007979set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007980 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007981 int cnt; /* length of "flags" */
7982 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007983{
7984 /* We build the new tables here first, so that we can compare with the
7985 * previous one. */
7986 spelltab_T new_st;
7987 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007988 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007989 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007990
7991 clear_spell_chartab(&new_st);
7992
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007993 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007994 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007995 if (i < cnt)
7996 {
7997 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
7998 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
7999 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008000
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008001 if (*p != NUL)
8002 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008003#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008004 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008005#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008006 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008007#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008008 new_st.st_fold[i + 128] = c;
8009 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
8010 new_st.st_upper[c] = i + 128;
8011 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008012 }
8013
Bram Moolenaar5195e452005-08-19 20:32:47 +00008014 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008015}
8016
8017 static int
8018set_spell_finish(new_st)
8019 spelltab_T *new_st;
8020{
8021 int i;
8022
8023 if (did_set_spelltab)
8024 {
8025 /* check that it's the same table */
8026 for (i = 0; i < 256; ++i)
8027 {
8028 if (spelltab.st_isw[i] != new_st->st_isw[i]
8029 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008030 || spelltab.st_fold[i] != new_st->st_fold[i]
8031 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008032 {
8033 EMSG(_("E763: Word characters differ between spell files"));
8034 return FAIL;
8035 }
8036 }
8037 }
8038 else
8039 {
8040 /* copy the new spelltab into the one being used */
8041 spelltab = *new_st;
8042 did_set_spelltab = TRUE;
8043 }
8044
8045 return OK;
8046}
8047
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008048/*
Bram Moolenaarea408852005-06-25 22:49:46 +00008049 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008050 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00008051 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008052 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00008053 */
8054 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008055spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00008056 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008057 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00008058{
Bram Moolenaarea408852005-06-25 22:49:46 +00008059#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008060 char_u *s;
8061 int l;
8062 int c;
8063
8064 if (has_mbyte)
8065 {
8066 l = MB_BYTE2LEN(*p);
8067 s = p;
8068 if (l == 1)
8069 {
8070 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008071 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008072 {
8073 s = p + 1; /* skip a mid-word character */
8074 l = MB_BYTE2LEN(*s);
8075 }
8076 }
8077 else
8078 {
8079 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008080 if (c < 256 ? buf->b_spell_ismw[c]
8081 : (buf->b_spell_ismw_mb != NULL
8082 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008083 {
8084 s = p + l;
8085 l = MB_BYTE2LEN(*s);
8086 }
8087 }
8088
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008089 c = mb_ptr2char(s);
8090 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008091 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008092 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008093 }
Bram Moolenaarea408852005-06-25 22:49:46 +00008094#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008095
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008096 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
8097}
8098
8099/*
8100 * Return TRUE if "p" points to a word character.
8101 * Unlike spell_iswordp() this doesn't check for "midword" characters.
8102 */
8103 static int
8104spell_iswordp_nmw(p)
8105 char_u *p;
8106{
8107#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008108 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008109
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008110 if (has_mbyte)
8111 {
8112 c = mb_ptr2char(p);
8113 if (c > 255)
8114 return mb_get_class(p) >= 2;
8115 return spelltab.st_isw[c];
8116 }
8117#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008118 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00008119}
8120
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008121#ifdef FEAT_MBYTE
8122/*
8123 * Return TRUE if "p" points to a word character.
8124 * Wide version of spell_iswordp().
8125 */
8126 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008127spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008128 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008129 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008130{
8131 int *s;
8132
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008133 if (*p < 256 ? buf->b_spell_ismw[*p]
8134 : (buf->b_spell_ismw_mb != NULL
8135 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008136 s = p + 1;
8137 else
8138 s = p;
8139
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008140 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008141 {
8142 if (enc_utf8)
8143 return utf_class(*s) >= 2;
8144 if (enc_dbcs)
8145 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
8146 return 0;
8147 }
8148 return spelltab.st_isw[*s];
8149}
8150#endif
8151
Bram Moolenaarea408852005-06-25 22:49:46 +00008152/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008153 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00008154 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008155 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008156 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008157write_spell_prefcond(fd, gap)
8158 FILE *fd;
8159 garray_T *gap;
8160{
8161 int i;
8162 char_u *p;
8163 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008164 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008165
Bram Moolenaar5195e452005-08-19 20:32:47 +00008166 if (fd != NULL)
8167 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
8168
8169 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008170
8171 for (i = 0; i < gap->ga_len; ++i)
8172 {
8173 /* <prefcond> : <condlen> <condstr> */
8174 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00008175 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008176 {
8177 len = STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008178 if (fd != NULL)
8179 {
8180 fputc(len, fd);
8181 fwrite(p, (size_t)len, (size_t)1, fd);
8182 }
8183 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008184 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008185 else if (fd != NULL)
8186 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008187 }
8188
Bram Moolenaar5195e452005-08-19 20:32:47 +00008189 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008190}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008191
8192/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008193 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
8194 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008195 * When using a multi-byte 'encoding' the length may change!
8196 * Returns FAIL when something wrong.
8197 */
8198 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008199spell_casefold(str, len, buf, buflen)
8200 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008201 int len;
8202 char_u *buf;
8203 int buflen;
8204{
8205 int i;
8206
8207 if (len >= buflen)
8208 {
8209 buf[0] = NUL;
8210 return FAIL; /* result will not fit */
8211 }
8212
8213#ifdef FEAT_MBYTE
8214 if (has_mbyte)
8215 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008216 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008217 char_u *p;
8218 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008219
8220 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008221 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008222 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008223 if (outi + MB_MAXBYTES > buflen)
8224 {
8225 buf[outi] = NUL;
8226 return FAIL;
8227 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008228 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008229 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008230 }
8231 buf[outi] = NUL;
8232 }
8233 else
8234#endif
8235 {
8236 /* Be quick for non-multibyte encodings. */
8237 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008238 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00008239 buf[i] = NUL;
8240 }
8241
8242 return OK;
8243}
8244
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008245#define SPS_BEST 1
8246#define SPS_FAST 2
8247#define SPS_DOUBLE 4
8248
8249static int sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008250static int sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008251
8252/*
8253 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00008254 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008255 */
8256 int
8257spell_check_sps()
8258{
8259 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008260 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008261 char_u buf[MAXPATHL];
8262 int f;
8263
8264 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008265 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008266
8267 for (p = p_sps; *p != NUL; )
8268 {
8269 copy_option_part(&p, buf, MAXPATHL, ",");
8270
8271 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008272 if (VIM_ISDIGIT(*buf))
8273 {
8274 s = buf;
8275 sps_limit = getdigits(&s);
8276 if (*s != NUL && !VIM_ISDIGIT(*s))
8277 f = -1;
8278 }
8279 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008280 f = SPS_BEST;
8281 else if (STRCMP(buf, "fast") == 0)
8282 f = SPS_FAST;
8283 else if (STRCMP(buf, "double") == 0)
8284 f = SPS_DOUBLE;
8285 else if (STRNCMP(buf, "expr:", 5) != 0
8286 && STRNCMP(buf, "file:", 5) != 0)
8287 f = -1;
8288
8289 if (f == -1 || (sps_flags != 0 && f != 0))
8290 {
8291 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008292 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008293 return FAIL;
8294 }
8295 if (f != 0)
8296 sps_flags = f;
8297 }
8298
8299 if (sps_flags == 0)
8300 sps_flags = SPS_BEST;
8301
8302 return OK;
8303}
8304
8305/* Remember what "z?" replaced. */
8306static char_u *repl_from = NULL;
8307static char_u *repl_to = NULL;
8308
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008309/*
8310 * "z?": Find badly spelled word under or after the cursor.
8311 * Give suggestions for the properly spelled word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00008312 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008313 */
8314 void
Bram Moolenaard12a1322005-08-21 22:08:24 +00008315spell_suggest(count)
8316 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008317{
8318 char_u *line;
8319 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008320 char_u wcopy[MAXWLEN + 2];
8321 char_u *p;
8322 int i;
8323 int c;
8324 suginfo_T sug;
8325 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008326 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008327 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008328 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00008329 int selected = count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008330
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008331 /* Find the start of the badly spelled word. */
Bram Moolenaar95529562005-08-25 21:21:38 +00008332 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00008333 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008334 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008335 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
8336 return;
8337
8338 /* No bad word or it starts after the cursor: use the word under the
8339 * cursor. */
8340 curwin->w_cursor = prev_cursor;
8341 line = ml_get_curline();
8342 p = line + curwin->w_cursor.col;
8343 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008344 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00008345 mb_ptr_back(line, p);
8346 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008347 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00008348 mb_ptr_adv(p);
8349
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008350 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008351 {
8352 beep_flush();
8353 return;
8354 }
8355 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008356 }
8357
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008358 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008359
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008360 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008361 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008362
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008363 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008364
Bram Moolenaar5195e452005-08-19 20:32:47 +00008365 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
8366 * 'spellsuggest', whatever is smaller. */
8367 if (sps_limit > (int)Rows - 2)
8368 limit = (int)Rows - 2;
8369 else
8370 limit = sps_limit;
8371 spell_find_suggest(line + curwin->w_cursor.col, &sug, limit,
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008372 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008373
8374 if (sug.su_ga.ga_len == 0)
8375 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00008376 else if (count > 0)
8377 {
8378 if (count > sug.su_ga.ga_len)
8379 smsg((char_u *)_("Sorry, only %ld suggestions"),
8380 (long)sug.su_ga.ga_len);
8381 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008382 else
8383 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008384 vim_free(repl_from);
8385 repl_from = NULL;
8386 vim_free(repl_to);
8387 repl_to = NULL;
8388
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008389#ifdef FEAT_RIGHTLEFT
8390 /* When 'rightleft' is set the list is drawn right-left. */
8391 cmdmsg_rl = curwin->w_p_rl;
8392 if (cmdmsg_rl)
8393 msg_col = Columns - 1;
8394#endif
8395
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008396 /* List the suggestions. */
8397 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008398 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008399 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
8400 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008401#ifdef FEAT_RIGHTLEFT
8402 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
8403 {
8404 /* And now the rabbit from the high hat: Avoid showing the
8405 * untranslated message rightleft. */
8406 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
8407 sug.su_badlen, sug.su_badptr);
8408 }
8409#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008410 msg_puts(IObuff);
8411 msg_clr_eos();
8412 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00008413
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008414 msg_scroll = TRUE;
8415 for (i = 0; i < sug.su_ga.ga_len; ++i)
8416 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008417 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008418
8419 /* The suggested word may replace only part of the bad word, add
8420 * the not replaced part. */
8421 STRCPY(wcopy, stp->st_word);
8422 if (sug.su_badlen > stp->st_orglen)
8423 vim_strncpy(wcopy + STRLEN(wcopy),
8424 sug.su_badptr + stp->st_orglen,
8425 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008426 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
8427#ifdef FEAT_RIGHTLEFT
8428 if (cmdmsg_rl)
8429 rl_mirror(IObuff);
8430#endif
8431 msg_puts(IObuff);
8432
8433 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008434 msg_puts(IObuff);
8435
8436 /* The word may replace more than "su_badlen". */
8437 if (sug.su_badlen < stp->st_orglen)
8438 {
8439 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
8440 stp->st_orglen, sug.su_badptr);
8441 msg_puts(IObuff);
8442 }
8443
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008444 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008445 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008446 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008447 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008448 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008449 stp->st_salscore ? "s " : "",
8450 stp->st_score, stp->st_altscore);
8451 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008452 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +00008453 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008454#ifdef FEAT_RIGHTLEFT
8455 if (cmdmsg_rl)
8456 /* Mirror the numbers, but keep the leading space. */
8457 rl_mirror(IObuff + 1);
8458#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +00008459 msg_advance(30);
8460 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008461 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008462 msg_putchar('\n');
8463 }
8464
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008465#ifdef FEAT_RIGHTLEFT
8466 cmdmsg_rl = FALSE;
8467 msg_col = 0;
8468#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008469 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00008470 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008471 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +00008472 selected -= lines_left;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008473 }
8474
Bram Moolenaard12a1322005-08-21 22:08:24 +00008475 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
8476 {
8477 /* Save the from and to text for :spellrepall. */
8478 stp = &SUG(sug.su_ga, selected - 1);
8479 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
8480 repl_to = vim_strsave(stp->st_word);
8481
8482 /* Replace the word. */
8483 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
8484 if (p != NULL)
8485 {
8486 c = sug.su_badptr - line;
8487 mch_memmove(p, line, c);
8488 STRCPY(p + c, stp->st_word);
8489 STRCAT(p, sug.su_badptr + stp->st_orglen);
8490 ml_replace(curwin->w_cursor.lnum, p, FALSE);
8491 curwin->w_cursor.col = c;
8492 changed_bytes(curwin->w_cursor.lnum, c);
8493
8494 /* For redo we use a change-word command. */
8495 ResetRedobuff();
8496 AppendToRedobuff((char_u *)"ciw");
8497 AppendToRedobuff(stp->st_word);
8498 AppendCharToRedobuff(ESC);
8499 }
8500 }
8501 else
8502 curwin->w_cursor = prev_cursor;
8503
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008504 spell_find_cleanup(&sug);
8505}
8506
8507/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008508 * Check if the word at line "lnum" column "col" is required to start with a
8509 * capital. This uses 'spellcapcheck' of the current buffer.
8510 */
8511 static int
8512check_need_cap(lnum, col)
8513 linenr_T lnum;
8514 colnr_T col;
8515{
8516 int need_cap = FALSE;
8517 char_u *line;
8518 char_u *line_copy = NULL;
8519 char_u *p;
8520 colnr_T endcol;
8521 regmatch_T regmatch;
8522
8523 if (curbuf->b_cap_prog == NULL)
8524 return FALSE;
8525
8526 line = ml_get_curline();
8527 endcol = 0;
8528 if ((int)(skipwhite(line) - line) >= (int)col)
8529 {
8530 /* At start of line, check if previous line is empty or sentence
8531 * ends there. */
8532 if (lnum == 1)
8533 need_cap = TRUE;
8534 else
8535 {
8536 line = ml_get(lnum - 1);
8537 if (*skipwhite(line) == NUL)
8538 need_cap = TRUE;
8539 else
8540 {
8541 /* Append a space in place of the line break. */
8542 line_copy = concat_str(line, (char_u *)" ");
8543 line = line_copy;
8544 endcol = STRLEN(line);
8545 }
8546 }
8547 }
8548 else
8549 endcol = col;
8550
8551 if (endcol > 0)
8552 {
8553 /* Check if sentence ends before the bad word. */
8554 regmatch.regprog = curbuf->b_cap_prog;
8555 regmatch.rm_ic = FALSE;
8556 p = line + endcol;
8557 for (;;)
8558 {
8559 mb_ptr_back(line, p);
8560 if (p == line || spell_iswordp_nmw(p))
8561 break;
8562 if (vim_regexec(&regmatch, p, 0)
8563 && regmatch.endp[0] == line + endcol)
8564 {
8565 need_cap = TRUE;
8566 break;
8567 }
8568 }
8569 }
8570
8571 vim_free(line_copy);
8572
8573 return need_cap;
8574}
8575
8576
8577/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008578 * ":spellrepall"
8579 */
8580/*ARGSUSED*/
8581 void
8582ex_spellrepall(eap)
8583 exarg_T *eap;
8584{
8585 pos_T pos = curwin->w_cursor;
8586 char_u *frompat;
8587 int addlen;
8588 char_u *line;
8589 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008590 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008591 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008592
8593 if (repl_from == NULL || repl_to == NULL)
8594 {
8595 EMSG(_("E752: No previous spell replacement"));
8596 return;
8597 }
8598 addlen = STRLEN(repl_to) - STRLEN(repl_from);
8599
8600 frompat = alloc(STRLEN(repl_from) + 7);
8601 if (frompat == NULL)
8602 return;
8603 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
8604 p_ws = FALSE;
8605
Bram Moolenaar5195e452005-08-19 20:32:47 +00008606 sub_nsubs = 0;
8607 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008608 curwin->w_cursor.lnum = 0;
8609 while (!got_int)
8610 {
8611 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
8612 || u_save_cursor() == FAIL)
8613 break;
8614
8615 /* Only replace when the right word isn't there yet. This happens
8616 * when changing "etc" to "etc.". */
8617 line = ml_get_curline();
8618 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
8619 repl_to, STRLEN(repl_to)) != 0)
8620 {
8621 p = alloc(STRLEN(line) + addlen + 1);
8622 if (p == NULL)
8623 break;
8624 mch_memmove(p, line, curwin->w_cursor.col);
8625 STRCPY(p + curwin->w_cursor.col, repl_to);
8626 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
8627 ml_replace(curwin->w_cursor.lnum, p, FALSE);
8628 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008629
8630 if (curwin->w_cursor.lnum != prev_lnum)
8631 {
8632 ++sub_nlines;
8633 prev_lnum = curwin->w_cursor.lnum;
8634 }
8635 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008636 }
8637 curwin->w_cursor.col += STRLEN(repl_to);
8638 }
8639
8640 p_ws = save_ws;
8641 curwin->w_cursor = pos;
8642 vim_free(frompat);
8643
Bram Moolenaar5195e452005-08-19 20:32:47 +00008644 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008645 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008646 else
8647 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008648}
8649
8650/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008651 * Find spell suggestions for "word". Return them in the growarray "*gap" as
8652 * a list of allocated strings.
8653 */
8654 void
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008655spell_suggest_list(gap, word, maxcount, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008656 garray_T *gap;
8657 char_u *word;
8658 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008659 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008660{
8661 suginfo_T sug;
8662 int i;
8663 suggest_T *stp;
8664 char_u *wcopy;
8665
Bram Moolenaar8b59de92005-08-11 19:59:29 +00008666 spell_find_suggest(word, &sug, maxcount, FALSE, need_cap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008667
8668 /* Make room in "gap". */
8669 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
8670 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
8671 return;
8672
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008673 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008674 {
8675 stp = &SUG(sug.su_ga, i);
8676
8677 /* The suggested word may replace only part of "word", add the not
8678 * replaced part. */
8679 wcopy = alloc(STRLEN(stp->st_word)
8680 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
8681 if (wcopy == NULL)
8682 break;
8683 STRCPY(wcopy, stp->st_word);
8684 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
8685 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
8686 }
8687
8688 spell_find_cleanup(&sug);
8689}
8690
8691/*
8692 * Find spell suggestions for the word at the start of "badptr".
8693 * Return the suggestions in "su->su_ga".
8694 * The maximum number of suggestions is "maxcount".
8695 * Note: does use info for the current window.
8696 * This is based on the mechanisms of Aspell, but completely reimplemented.
8697 */
8698 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008699spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008700 char_u *badptr;
8701 suginfo_T *su;
8702 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00008703 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008704 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008705{
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008706 int attr = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008707 char_u buf[MAXPATHL];
8708 char_u *p;
8709 int do_combine = FALSE;
8710 char_u *sps_copy;
8711#ifdef FEAT_EVAL
8712 static int expr_busy = FALSE;
8713#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008714 int c;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008715
8716 /*
8717 * Set the info in "*su".
8718 */
8719 vim_memset(su, 0, sizeof(suginfo_T));
8720 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
8721 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008722 if (*badptr == NUL)
8723 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008724 hash_init(&su->su_banned);
8725
8726 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008727 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008728 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008729 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008730
8731 if (su->su_badlen >= MAXWLEN)
8732 su->su_badlen = MAXWLEN - 1; /* just in case */
8733 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
8734 (void)spell_casefold(su->su_badptr, su->su_badlen,
8735 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008736 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008737 su->su_badflags = badword_captype(su->su_badptr,
8738 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008739 if (need_cap)
8740 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008741
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008742 /* If the word is not capitalised and spell_check() doesn't consider the
8743 * word to be bad then it might need to be capitalised. Add a suggestion
8744 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008745 c = PTR2CHAR(su->su_badptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00008746 if (!SPELL_ISUPPER(c) && attr == 0)
8747 {
8748 make_case_word(su->su_badword, buf, WF_ONECAP);
8749 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
8750 0, TRUE);
8751 }
8752
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008753 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00008754 if (banbadword)
8755 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008756
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008757 /* Make a copy of 'spellsuggest', because the expression may change it. */
8758 sps_copy = vim_strsave(p_sps);
8759 if (sps_copy == NULL)
8760 return;
8761
8762 /* Loop over the items in 'spellsuggest'. */
8763 for (p = sps_copy; *p != NUL; )
8764 {
8765 copy_option_part(&p, buf, MAXPATHL, ",");
8766
8767 if (STRNCMP(buf, "expr:", 5) == 0)
8768 {
8769#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008770 /* Evaluate an expression. Skip this when called recursively,
8771 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008772 if (!expr_busy)
8773 {
8774 expr_busy = TRUE;
8775 spell_suggest_expr(su, buf + 5);
8776 expr_busy = FALSE;
8777 }
8778#endif
8779 }
8780 else if (STRNCMP(buf, "file:", 5) == 0)
8781 /* Use list of suggestions in a file. */
8782 spell_suggest_file(su, buf + 5);
8783 else
8784 {
8785 /* Use internal method. */
8786 spell_suggest_intern(su);
8787 if (sps_flags & SPS_DOUBLE)
8788 do_combine = TRUE;
8789 }
8790 }
8791
8792 vim_free(sps_copy);
8793
8794 if (do_combine)
8795 /* Combine the two list of suggestions. This must be done last,
8796 * because sorting changes the order again. */
8797 score_combine(su);
8798}
8799
8800#ifdef FEAT_EVAL
8801/*
8802 * Find suggestions by evaluating expression "expr".
8803 */
8804 static void
8805spell_suggest_expr(su, expr)
8806 suginfo_T *su;
8807 char_u *expr;
8808{
8809 list_T *list;
8810 listitem_T *li;
8811 int score;
8812 char_u *p;
8813
8814 /* The work is split up in a few parts to avoid having to export
8815 * suginfo_T.
8816 * First evaluate the expression and get the resulting list. */
8817 list = eval_spell_expr(su->su_badword, expr);
8818 if (list != NULL)
8819 {
8820 /* Loop over the items in the list. */
8821 for (li = list->lv_first; li != NULL; li = li->li_next)
8822 if (li->li_tv.v_type == VAR_LIST)
8823 {
8824 /* Get the word and the score from the items. */
8825 score = get_spellword(li->li_tv.vval.v_list, &p);
8826 if (score >= 0)
8827 add_suggestion(su, &su->su_ga, p,
8828 su->su_badlen, score, 0, TRUE);
8829 }
8830 list_unref(list);
8831 }
8832
8833 /* Sort the suggestions and truncate at "maxcount". */
8834 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8835}
8836#endif
8837
8838/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008839 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008840 */
8841 static void
8842spell_suggest_file(su, fname)
8843 suginfo_T *su;
8844 char_u *fname;
8845{
8846 FILE *fd;
8847 char_u line[MAXWLEN * 2];
8848 char_u *p;
8849 int len;
8850 char_u cword[MAXWLEN];
8851
8852 /* Open the file. */
8853 fd = mch_fopen((char *)fname, "r");
8854 if (fd == NULL)
8855 {
8856 EMSG2(_(e_notopen), fname);
8857 return;
8858 }
8859
8860 /* Read it line by line. */
8861 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
8862 {
8863 line_breakcheck();
8864
8865 p = vim_strchr(line, '/');
8866 if (p == NULL)
8867 continue; /* No Tab found, just skip the line. */
8868 *p++ = NUL;
8869 if (STRICMP(su->su_badword, line) == 0)
8870 {
8871 /* Match! Isolate the good word, until CR or NL. */
8872 for (len = 0; p[len] >= ' '; ++len)
8873 ;
8874 p[len] = NUL;
8875
8876 /* If the suggestion doesn't have specific case duplicate the case
8877 * of the bad word. */
8878 if (captype(p, NULL) == 0)
8879 {
8880 make_case_word(p, cword, su->su_badflags);
8881 p = cword;
8882 }
8883
8884 add_suggestion(su, &su->su_ga, p, su->su_badlen,
8885 SCORE_FILE, 0, TRUE);
8886 }
8887 }
8888
8889 fclose(fd);
8890
8891 /* Sort the suggestions and truncate at "maxcount". */
8892 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8893}
8894
8895/*
8896 * Find suggestions for the internal method indicated by "sps_flags".
8897 */
8898 static void
8899spell_suggest_intern(su)
8900 suginfo_T *su;
8901{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008902 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008903 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008904 *
8905 * Set a maximum score to limit the combination of operations that is
8906 * tried.
8907 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008908 suggest_try_special(su);
8909
8910 /*
8911 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
8912 * from the .aff file and inserting a space (split the word).
8913 */
8914 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008915
8916 /* For the resulting top-scorers compute the sound-a-like score. */
8917 if (sps_flags & SPS_DOUBLE)
8918 score_comp_sal(su);
8919
8920 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008921 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008922 *
8923 * Only do this when we don't have a lot of suggestions yet, because it's
8924 * very slow and often doesn't find new suggestions.
8925 */
8926 if ((sps_flags & SPS_DOUBLE)
8927 || (!(sps_flags & SPS_FAST)
8928 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
8929 {
8930 /* Allow a higher score now. */
8931 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008932 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008933 }
8934
8935 /* When CTRL-C was hit while searching do show the results. */
8936 ui_breakcheck();
8937 if (got_int)
8938 {
8939 (void)vgetc();
8940 got_int = FALSE;
8941 }
8942
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008943 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008944 {
8945 if (sps_flags & SPS_BEST)
8946 /* Adjust the word score for how it sounds like. */
8947 rescore_suggestions(su);
8948
8949 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008950 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008951 }
8952}
8953
8954/*
8955 * Free the info put in "*su" by spell_find_suggest().
8956 */
8957 static void
8958spell_find_cleanup(su)
8959 suginfo_T *su;
8960{
8961 int i;
8962
8963 /* Free the suggestions. */
8964 for (i = 0; i < su->su_ga.ga_len; ++i)
8965 vim_free(SUG(su->su_ga, i).st_word);
8966 ga_clear(&su->su_ga);
8967 for (i = 0; i < su->su_sga.ga_len; ++i)
8968 vim_free(SUG(su->su_sga, i).st_word);
8969 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008970
8971 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008972 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008973}
8974
8975/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008976 * Make a copy of "word", with the first letter upper or lower cased, to
8977 * "wcopy[MAXWLEN]". "word" must not be empty.
8978 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008979 */
8980 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008981onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008982 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008983 char_u *wcopy;
8984 int upper; /* TRUE: first letter made upper case */
8985{
8986 char_u *p;
8987 int c;
8988 int l;
8989
8990 p = word;
8991#ifdef FEAT_MBYTE
8992 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008993 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008994 else
8995#endif
8996 c = *p++;
8997 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008998 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008999 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009000 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009001#ifdef FEAT_MBYTE
9002 if (has_mbyte)
9003 l = mb_char2bytes(c, wcopy);
9004 else
9005#endif
9006 {
9007 l = 1;
9008 wcopy[0] = c;
9009 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009010 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009011}
9012
9013/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009014 * Make a copy of "word" with all the letters upper cased into
9015 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009016 */
9017 static void
9018allcap_copy(word, wcopy)
9019 char_u *word;
9020 char_u *wcopy;
9021{
9022 char_u *s;
9023 char_u *d;
9024 int c;
9025
9026 d = wcopy;
9027 for (s = word; *s != NUL; )
9028 {
9029#ifdef FEAT_MBYTE
9030 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009031 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009032 else
9033#endif
9034 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +00009035
9036#ifdef FEAT_MBYTE
9037 /* We only change ß to SS when we are certain latin1 is used. It
9038 * would cause weird errors in other 8-bit encodings. */
9039 if (enc_latin1like && c == 0xdf)
9040 {
9041 c = 'S';
9042 if (d - wcopy >= MAXWLEN - 1)
9043 break;
9044 *d++ = c;
9045 }
9046 else
9047#endif
9048 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009049
9050#ifdef FEAT_MBYTE
9051 if (has_mbyte)
9052 {
9053 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
9054 break;
9055 d += mb_char2bytes(c, d);
9056 }
9057 else
9058#endif
9059 {
9060 if (d - wcopy >= MAXWLEN - 1)
9061 break;
9062 *d++ = c;
9063 }
9064 }
9065 *d = NUL;
9066}
9067
9068/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00009069 * Try finding suggestions by recognizing specific situations.
9070 */
9071 static void
9072suggest_try_special(su)
9073 suginfo_T *su;
9074{
9075 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009076 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009077 int c;
9078 char_u word[MAXWLEN];
9079
9080 /*
9081 * Recognize a word that is repeated: "the the".
9082 */
9083 p = skiptowhite(su->su_fbadword);
9084 len = p - su->su_fbadword;
9085 p = skipwhite(p);
9086 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
9087 {
9088 /* Include badflags: if the badword is onecap or allcap
9089 * use that for the goodword too: "The the" -> "The". */
9090 c = su->su_fbadword[len];
9091 su->su_fbadword[len] = NUL;
9092 make_case_word(su->su_fbadword, word, su->su_badflags);
9093 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009094 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009095 }
9096}
9097
9098/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009099 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00009100 *
9101 * This uses a state machine. At each node in the tree we try various
9102 * operations. When trying if an operation work "depth" is increased and the
9103 * stack[] is used to store info. This allows combinations, thus insert one
9104 * character, replace one and delete another. The number of changes is
9105 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaard12a1322005-08-21 22:08:24 +00009106 *
9107 * After implementing this I noticed an article by Kemal Oflazer that
9108 * describes something similar: "Error-tolerant Finite State Recognition with
9109 * Applications to Morphological Analysis and Spelling Correction" (1996).
9110 * The implementation in the article is simplified and requires a stack of
9111 * unknown depth. The implementation here only needs a stack depth of the
9112 * length of the word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009113 */
9114 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00009115suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009116 suginfo_T *su;
9117{
9118 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
9119 char_u tword[MAXWLEN]; /* good word collected so far */
9120 trystate_T stack[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009121 char_u preword[MAXWLEN * 3]; /* word found with proper case;
9122 * concatanation of prefix compound
9123 * words and split word. NUL terminated
9124 * when going deeper but not when coming
9125 * back. */
9126 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009127 trystate_T *sp;
9128 int newscore;
9129 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009130 char_u *byts, *fbyts, *pbyts;
9131 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009132 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009133 int c, c2, c3;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009134 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009135 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009136 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009137 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009138 int len;
9139 char_u *p;
9140 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00009141 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00009142 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009143 slang_T *slang;
9144 int fword_ends;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009145 int lpi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009146
9147 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00009148 * to find matches (esp. REP items). Append some more text, changing
9149 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009150 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009151 n = STRLEN(fword);
9152 p = su->su_badptr + su->su_badlen;
9153 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009154
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009155 for (lpi = 0; lpi < curwin->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009156 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009157 lp = LANGP_ENTRY(curwin->w_buffer->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009158 slang = lp->lp_slang;
9159
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009160 /* If reloading a spell file fails it's still in the list but
9161 * everything has been cleared. */
9162 if (slang->sl_fbyts == NULL)
9163 continue;
9164
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009165 /*
9166 * Go through the whole case-fold tree, try changes at each node.
9167 * "tword[]" contains the word collected from nodes in the tree.
9168 * "fword[]" the word we are trying to match with (initially the bad
9169 * word).
9170 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009171 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009172 sp = &stack[0];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009173 vim_memset(sp, 0, sizeof(trystate_T));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009174 sp->ts_curi = 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009175
Bram Moolenaarea424162005-06-16 21:51:00 +00009176 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009177 * When there are postponed prefixes we need to use these first. At
9178 * the end of the prefix we continue in the case-fold tree.
9179 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009180 fbyts = slang->sl_fbyts;
9181 fidxs = slang->sl_fidxs;
9182 pbyts = slang->sl_pbyts;
9183 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009184 if (pbyts != NULL)
9185 {
9186 byts = pbyts;
9187 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009188 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009189 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
9190 }
9191 else
9192 {
9193 byts = fbyts;
9194 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009195 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009196 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009197 }
9198
9199 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00009200 * Loop to find all suggestions. At each round we either:
9201 * - For the current state try one operation, advance "ts_curi",
9202 * increase "depth".
9203 * - When a state is done go to the next, set "ts_state".
9204 * - When all states are tried decrease "depth".
9205 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009206 while (depth >= 0 && !got_int)
9207 {
9208 sp = &stack[depth];
9209 switch (sp->ts_state)
9210 {
9211 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009212 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009213 /*
9214 * Start of node: Deal with NUL bytes, which means
9215 * tword[] may end here.
9216 */
9217 arridx = sp->ts_arridx; /* current node in the tree */
9218 len = byts[arridx]; /* bytes in this node */
9219 arridx += sp->ts_curi; /* index of current byte */
9220
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009221 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009222 {
9223 /* Skip over the NUL bytes, we use them later. */
9224 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
9225 ;
9226 sp->ts_curi += n;
9227
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009228 /* Always past NUL bytes now. */
9229 n = (int)sp->ts_state;
9230 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00009231 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009232
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009233 /* At end of a prefix or at start of prefixtree: check for
9234 * following word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009235 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009236 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00009237 /* Set su->su_badflags to the caps type at this
9238 * position. Use the caps type until here for the
9239 * prefix itself. */
9240#ifdef FEAT_MBYTE
9241 if (has_mbyte)
9242 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
9243 else
9244#endif
9245 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009246 flags = badword_captype(su->su_badptr,
9247 su->su_badptr + n);
9248 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00009249 su->su_badptr + su->su_badlen);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009250 ++depth;
9251 stack[depth] = stack[depth - 1];
9252 sp = &stack[depth];
9253 sp->ts_prefixdepth = depth - 1;
9254 byts = fbyts;
9255 idxs = fidxs;
9256 sp->ts_state = STATE_START;
9257 sp->ts_curi = 1; /* start just after length byte */
9258 sp->ts_arridx = 0;
9259
Bram Moolenaar53805d12005-08-01 07:08:33 +00009260 /* Move the prefix to preword[] with the right case
9261 * and make find_keepcap_word() works. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00009262 tword[sp->ts_twordlen] = NUL;
9263 make_case_word(tword + sp->ts_splitoff,
9264 preword + sp->ts_prewordlen,
9265 flags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009266 sp->ts_prewordlen = STRLEN(preword);
Bram Moolenaard12a1322005-08-21 22:08:24 +00009267 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009268 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009269 break;
9270 }
9271
Bram Moolenaar0c405862005-06-22 22:26:26 +00009272 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009273 {
9274 /* Past bytes in node and/or past NUL bytes. */
9275 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00009276 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009277 break;
9278 }
9279
9280 /*
9281 * End of word in tree.
9282 */
9283 ++sp->ts_curi; /* eat one NUL byte */
9284
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009285 flags = (int)idxs[arridx];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009286 fword_ends = (fword[sp->ts_fidx] == NUL
9287 || !spell_iswordp(fword + sp->ts_fidx, curbuf));
9288 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009289
Bram Moolenaard12a1322005-08-21 22:08:24 +00009290 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
9291 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009292 {
9293 /* There was a prefix before the word. Check that the
9294 * prefix can be used with this word. */
9295 /* Count the length of the NULs in the prefix. If there
9296 * are none this must be the first try without a prefix.
9297 */
9298 n = stack[sp->ts_prefixdepth].ts_arridx;
9299 len = pbyts[n++];
9300 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
9301 ;
9302 if (c > 0)
9303 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009304 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00009305 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009306 if (c == 0)
9307 break;
9308
9309 /* Use the WF_RARE flag for a rare prefix. */
9310 if (c & WF_RAREPFX)
9311 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009312
9313 /* Tricky: when checking for both prefix and
9314 * compounding we run into the prefix flag first.
9315 * Remember that it's OK, so that we accept the prefix
9316 * when arriving at a compound flag. */
9317 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009318 }
9319 }
9320
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009321 /* Check NEEDCOMPOUND: can't use word without compounding. */
9322 if (sp->ts_complen == sp->ts_compsplit && fword_ends
9323 && (flags & WF_NEEDCOMP))
9324 break;
9325
Bram Moolenaard12a1322005-08-21 22:08:24 +00009326 if (sp->ts_complen > sp->ts_compsplit)
9327 {
Bram Moolenaar78622822005-08-23 21:00:13 +00009328 if (slang->sl_nobreak)
9329 {
9330 /* There was a word before this word. When there was
9331 * no change in this word (it was correct) add the
9332 * first word as a suggestion. If this word was
9333 * corrected too, we need to check if a correct word
9334 * follows. */
9335 if (sp->ts_fidx - sp->ts_splitfidx
9336 == sp->ts_twordlen - sp->ts_splitoff
9337 && STRNCMP(fword + sp->ts_splitfidx,
9338 tword + sp->ts_splitoff,
9339 sp->ts_fidx - sp->ts_splitfidx) == 0)
9340 {
9341 preword[sp->ts_prewordlen] = NUL;
9342 add_suggestion(su, &su->su_ga, preword,
9343 sp->ts_splitfidx - repextra,
9344 sp->ts_score, 0, FALSE);
9345 break;
9346 }
9347 }
9348 else
9349 {
9350 /* There was a compound word before this word. If
9351 * this word does not support compounding then give up
9352 * (splitting is tried for the word without compound
9353 * flag). */
9354 if (((unsigned)flags >> 24) == 0
9355 || sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaard12a1322005-08-21 22:08:24 +00009356 < slang->sl_compminlen)
Bram Moolenaar78622822005-08-23 21:00:13 +00009357 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009358#ifdef FEAT_MBYTE
9359 /* For multi-byte chars check character length against
9360 * COMPOUNDMIN. */
9361 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009362 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009363 && mb_charlen(tword + sp->ts_splitoff)
9364 < slang->sl_compminlen)
9365 break;
9366#endif
9367
Bram Moolenaar78622822005-08-23 21:00:13 +00009368 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
9369 compflags[sp->ts_complen + 1] = NUL;
9370 vim_strncpy(preword + sp->ts_prewordlen,
9371 tword + sp->ts_splitoff,
9372 sp->ts_twordlen - sp->ts_splitoff);
9373 p = preword;
9374 while (*skiptowhite(p) != NUL)
9375 p = skipwhite(skiptowhite(p));
9376 if (fword_ends && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00009377 compflags + sp->ts_compsplit))
Bram Moolenaar78622822005-08-23 21:00:13 +00009378 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00009379
Bram Moolenaar78622822005-08-23 21:00:13 +00009380 /* Get pointer to last char of previous word. */
9381 p = preword + sp->ts_prewordlen;
9382 mb_ptr_back(preword, p);
9383 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00009384 }
Bram Moolenaare52325c2005-08-22 22:54:29 +00009385 else
9386 p = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009387
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009388 /*
9389 * Form the word with proper case in preword.
9390 * If there is a word from a previous split, append.
9391 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009392 if (flags & WF_KEEPCAP)
9393 /* Must find the word in the keep-case tree. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009394 find_keepcap_word(slang, tword + sp->ts_splitoff,
9395 preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009396 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00009397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009398 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00009399 * use that for the goodword too. But if the badword is
9400 * allcap and it's only one char long use onecap. */
9401 c = su->su_badflags;
9402 if ((c & WF_ALLCAP)
9403#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009404 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
Bram Moolenaar0c405862005-06-22 22:26:26 +00009405#else
9406 && su->su_badlen == 1
9407#endif
9408 )
9409 c = WF_ONECAP;
Bram Moolenaare52325c2005-08-22 22:54:29 +00009410 c |= flags;
9411
9412 /* When appending a compound word after a word character
9413 * don't use Onecap. */
9414 if (p != NULL && spell_iswordp_nmw(p))
9415 c &= ~WF_ONECAP;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009416 make_case_word(tword + sp->ts_splitoff,
Bram Moolenaare52325c2005-08-22 22:54:29 +00009417 preword + sp->ts_prewordlen, c);
Bram Moolenaar0c405862005-06-22 22:26:26 +00009418 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009419
9420 /* Don't use a banned word. It may appear again as a good
9421 * word, thus remember it. */
9422 if (flags & WF_BANNED)
9423 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00009424 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009425 break;
9426 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009427 if (was_banned(su, preword + sp->ts_prewordlen)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009428 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009429 break;
9430
9431 newscore = 0;
9432 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009433 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009434 newscore += SCORE_REGION;
9435 if (flags & WF_RARE)
9436 newscore += SCORE_RARE;
9437
Bram Moolenaar0c405862005-06-22 22:26:26 +00009438 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00009439 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009440 newscore += SCORE_ICASE;
9441
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009442 if (fword_ends && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009443 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009444 /* The badword also ends: add suggestions. Give a penalty
9445 * when changing non-word char to word char, e.g., "thes,"
9446 * -> "these". */
9447 p = fword + sp->ts_fidx;
9448#ifdef FEAT_MBYTE
9449 if (has_mbyte)
9450 mb_ptr_back(fword, p);
9451 else
9452#endif
9453 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009454 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009455 {
9456 p = preword + STRLEN(preword);
9457#ifdef FEAT_MBYTE
9458 if (has_mbyte)
9459 mb_ptr_back(preword, p);
9460 else
9461#endif
9462 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009463 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009464 newscore += SCORE_NONWORD;
9465 }
9466
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009467 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00009468 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009469 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009470 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009471 else if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +00009472#ifdef FEAT_MBYTE
9473 /* Don't split halfway a character. */
9474 && (!has_mbyte || sp->ts_tcharlen == 0)
9475#endif
9476 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009477 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009478 int try_compound;
9479
9480 /* Get here in two situations:
9481 * 1. The word in the tree ends but the badword continues:
9482 * If the word allows compounding try that. Otherwise
9483 * try a split by inserting a space. For both check
9484 * that a valid words starts at fword[sp->ts_fidx].
Bram Moolenaar78622822005-08-23 21:00:13 +00009485 * For NOBREAK do like compounding to be able to check
9486 * if the next word is valid.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009487 * 2. The badword does end, but it was due to a change
9488 * (e.g., a swap). No need to split, but do check that
9489 * the following word is valid.
9490 */
Bram Moolenaard12a1322005-08-21 22:08:24 +00009491 try_compound = FALSE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009492 if (!fword_ends
Bram Moolenaar6de68532005-08-24 22:08:48 +00009493 && slang->sl_compprog != NULL
Bram Moolenaar5195e452005-08-19 20:32:47 +00009494 && ((unsigned)flags >> 24) != 0
9495 && sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009496 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009497#ifdef FEAT_MBYTE
9498 && (!has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009499 || slang->sl_compminlen == 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009500 || mb_charlen(tword + sp->ts_splitoff)
9501 >= slang->sl_compminlen)
9502#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00009503 && (slang->sl_compsylmax < MAXWLEN
9504 || sp->ts_complen + 1 - sp->ts_compsplit
9505 < slang->sl_compmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00009506 && (byte_in_str(sp->ts_complen == sp->ts_compsplit
Bram Moolenaard12a1322005-08-21 22:08:24 +00009507 ? slang->sl_compstartflags
9508 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00009509 ((unsigned)flags >> 24))))
Bram Moolenaar5195e452005-08-19 20:32:47 +00009510 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009511 try_compound = TRUE;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009512 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
9513 compflags[sp->ts_complen + 1] = NUL;
9514 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00009515
Bram Moolenaar78622822005-08-23 21:00:13 +00009516 /* For NOBREAK we never try splitting, it won't make any
9517 * word valid. */
9518 if (slang->sl_nobreak)
9519 try_compound = TRUE;
9520
Bram Moolenaard12a1322005-08-21 22:08:24 +00009521 /* If we could add a compound word, and it's also possible
9522 * to split at this point, do the split first and set
9523 * TSF_DIDSPLIT to avoid doing it again. */
Bram Moolenaar78622822005-08-23 21:00:13 +00009524 else if (!fword_ends
Bram Moolenaard12a1322005-08-21 22:08:24 +00009525 && try_compound
9526 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009527 {
9528 try_compound = FALSE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009529 sp->ts_flags |= TSF_DIDSPLIT;
9530 --sp->ts_curi; /* do the same NUL again */
9531 compflags[sp->ts_complen] = NUL;
9532 }
9533 else
9534 sp->ts_flags &= ~TSF_DIDSPLIT;
9535
9536 if (!try_compound && !fword_ends)
9537 {
9538 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009539 * words so far are valid for compounding. If there
9540 * is only one word it must not have the NEEDCOMPOUND
9541 * flag. */
9542 if (sp->ts_complen == sp->ts_compsplit
9543 && (flags & WF_NEEDCOMP))
9544 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +00009545 p = preword;
9546 while (*skiptowhite(p) != NUL)
9547 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +00009548 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +00009549 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +00009550 compflags + sp->ts_compsplit))
9551 break;
9552 newscore += SCORE_SPLIT;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009553 }
9554
9555 if (try_deeper(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009556 {
9557 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009558 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009559 sp->ts_state = STATE_SPLITUNDO;
9560
9561 ++depth;
9562 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009563
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009564 /* Append a space to preword when splitting. */
9565 if (!try_compound && !fword_ends)
9566 STRCAT(preword, " ");
Bram Moolenaar5195e452005-08-19 20:32:47 +00009567 sp->ts_prewordlen = STRLEN(preword);
9568 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +00009569 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009570
9571 /* If the badword has a non-word character at this
9572 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009573 * non-word character with a space. Always skip a
9574 * character when the word ends. */
9575 if ((!try_compound
9576 && !spell_iswordp_nmw(fword + sp->ts_fidx))
9577 || fword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009578 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009579 int l;
9580
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009581#ifdef FEAT_MBYTE
9582 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009583 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009584 else
9585#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009586 l = 1;
9587 if (fword_ends)
9588 {
9589 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009590 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009591 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009592 sp->ts_prewordlen += l;
9593 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009594 }
9595 else
9596 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
9597 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009598 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00009599
Bram Moolenaard12a1322005-08-21 22:08:24 +00009600 /* When compounding include compound flag in
9601 * compflags[] (already set above). When splitting we
9602 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009603 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +00009604 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009605 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00009606 sp->ts_compsplit = sp->ts_complen;
9607 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009608
Bram Moolenaar53805d12005-08-01 07:08:33 +00009609 /* set su->su_badflags to the caps type at this
9610 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009611#ifdef FEAT_MBYTE
9612 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00009613 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009614 else
9615#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00009616 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009617 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +00009618 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009620 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009621 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009622
9623 /* If there are postponed prefixes, try these too. */
9624 if (pbyts != NULL)
9625 {
9626 byts = pbyts;
9627 idxs = pidxs;
9628 sp->ts_prefixdepth = PFD_PREFIXTREE;
9629 sp->ts_state = STATE_NOPREFIX;
9630 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009631 }
9632 }
9633 break;
9634
9635 case STATE_SPLITUNDO:
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009636 /* Undo the changes done for word split or compound word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009637 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009638
9639 /* Continue looking for NUL bytes. */
9640 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009641
9642 /* In case we went into the prefix tree. */
9643 byts = fbyts;
9644 idxs = fidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009645 break;
9646
9647 case STATE_ENDNUL:
9648 /* Past the NUL bytes in the node. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00009649 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009650 if (fword[sp->ts_fidx] == NUL
9651#ifdef FEAT_MBYTE
9652 && sp->ts_tcharlen == 0
9653#endif
9654 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009655 {
9656 /* The badword ends, can't use the bytes in this node. */
9657 sp->ts_state = STATE_DEL;
9658 break;
9659 }
9660 sp->ts_state = STATE_PLAIN;
9661 /*FALLTHROUGH*/
9662
9663 case STATE_PLAIN:
9664 /*
9665 * Go over all possible bytes at this node, add each to
9666 * tword[] and use child node. "ts_curi" is the index.
9667 */
9668 arridx = sp->ts_arridx;
9669 if (sp->ts_curi > byts[arridx])
9670 {
9671 /* Done all bytes at this node, do next state. When still
9672 * at already changed bytes skip the other tricks. */
9673 if (sp->ts_fidx >= sp->ts_fidxtry)
9674 sp->ts_state = STATE_DEL;
9675 else
9676 sp->ts_state = STATE_FINAL;
9677 }
9678 else
9679 {
9680 arridx += sp->ts_curi++;
9681 c = byts[arridx];
9682
9683 /* Normal byte, go one level deeper. If it's not equal to
9684 * the byte in the bad word adjust the score. But don't
9685 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00009686 if (c == fword[sp->ts_fidx]
9687#ifdef FEAT_MBYTE
9688 || (sp->ts_tcharlen > 0
9689 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009690#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00009691 )
9692 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009693 else
9694 newscore = SCORE_SUBST;
9695 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
9696 && try_deeper(su, stack, depth, newscore))
9697 {
9698 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009699 sp = &stack[depth];
9700 ++sp->ts_fidx;
9701 tword[sp->ts_twordlen++] = c;
9702 sp->ts_arridx = idxs[arridx];
9703#ifdef FEAT_MBYTE
9704 if (newscore == SCORE_SUBST)
9705 sp->ts_isdiff = DIFF_YES;
9706 if (has_mbyte)
9707 {
9708 /* Multi-byte characters are a bit complicated to
9709 * handle: They differ when any of the bytes
9710 * differ and then their length may also differ. */
9711 if (sp->ts_tcharlen == 0)
9712 {
9713 /* First byte. */
9714 sp->ts_tcharidx = 0;
9715 sp->ts_tcharlen = MB_BYTE2LEN(c);
9716 sp->ts_fcharstart = sp->ts_fidx - 1;
9717 sp->ts_isdiff = (newscore != 0)
9718 ? DIFF_YES : DIFF_NONE;
9719 }
9720 else if (sp->ts_isdiff == DIFF_INSERT)
9721 /* When inserting trail bytes don't advance in
9722 * the bad word. */
9723 --sp->ts_fidx;
9724 if (++sp->ts_tcharidx == sp->ts_tcharlen)
9725 {
9726 /* Last byte of character. */
9727 if (sp->ts_isdiff == DIFF_YES)
9728 {
9729 /* Correct ts_fidx for the byte length of
9730 * the character (we didn't check that
9731 * before). */
9732 sp->ts_fidx = sp->ts_fcharstart
9733 + MB_BYTE2LEN(
9734 fword[sp->ts_fcharstart]);
9735
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00009736 /* For changing a composing character
9737 * adjust the score from SCORE_SUBST to
9738 * SCORE_SUBCOMP. */
9739 if (enc_utf8
9740 && utf_iscomposing(
9741 mb_ptr2char(tword
9742 + sp->ts_twordlen
9743 - sp->ts_tcharlen))
9744 && utf_iscomposing(
9745 mb_ptr2char(fword
9746 + sp->ts_fcharstart)))
9747 sp->ts_score -=
9748 SCORE_SUBST - SCORE_SUBCOMP;
9749
Bram Moolenaarea424162005-06-16 21:51:00 +00009750 /* For a similar character adjust score
9751 * from SCORE_SUBST to SCORE_SIMILAR. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009752 else if (slang->sl_has_map
9753 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00009754 mb_ptr2char(tword
9755 + sp->ts_twordlen
9756 - sp->ts_tcharlen),
9757 mb_ptr2char(fword
9758 + sp->ts_fcharstart)))
9759 sp->ts_score -=
9760 SCORE_SUBST - SCORE_SIMILAR;
9761 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009762 else if (sp->ts_isdiff == DIFF_INSERT
9763 && sp->ts_twordlen > sp->ts_tcharlen)
9764 {
Bram Moolenaarea408852005-06-25 22:49:46 +00009765 p = tword + sp->ts_twordlen
9766 - sp->ts_tcharlen;
9767 c = mb_ptr2char(p);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009768 if (enc_utf8 && utf_iscomposing(c))
9769 {
9770 /* Inserting a composing char doesn't
9771 * count that much. */
Bram Moolenaarea408852005-06-25 22:49:46 +00009772 sp->ts_score -= SCORE_INS
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009773 - SCORE_INSCOMP;
9774 }
9775 else
9776 {
9777 /* If the previous character was the
9778 * same, thus doubling a character,
9779 * give a bonus to the score. */
9780 mb_ptr_back(tword, p);
9781 if (c == mb_ptr2char(p))
9782 sp->ts_score -= SCORE_INS
Bram Moolenaarea408852005-06-25 22:49:46 +00009783 - SCORE_INSDUP;
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009784 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009785 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009786
9787 /* Starting a new char, reset the length. */
9788 sp->ts_tcharlen = 0;
9789 }
9790 }
9791 else
9792#endif
9793 {
9794 /* If we found a similar char adjust the score.
9795 * We do this after calling try_deeper() because
9796 * it's slow. */
9797 if (newscore != 0
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00009798 && slang->sl_has_map
9799 && similar_chars(slang,
Bram Moolenaarea424162005-06-16 21:51:00 +00009800 c, fword[sp->ts_fidx - 1]))
9801 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
9802 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009803 }
9804 }
9805 break;
9806
9807 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00009808#ifdef FEAT_MBYTE
9809 /* When past the first byte of a multi-byte char don't try
9810 * delete/insert/swap a character. */
9811 if (has_mbyte && sp->ts_tcharlen > 0)
9812 {
9813 sp->ts_state = STATE_FINAL;
9814 break;
9815 }
9816#endif
9817 /*
9818 * Try skipping one character in the bad word (delete it).
9819 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009820 sp->ts_state = STATE_INS;
9821 sp->ts_curi = 1;
9822 if (fword[sp->ts_fidx] != NUL
9823 && try_deeper(su, stack, depth, SCORE_DEL))
9824 {
9825 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00009826
9827 /* Advance over the character in fword[]. Give a bonus to
9828 * the score if the same character is following "nn" ->
9829 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00009830#ifdef FEAT_MBYTE
9831 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00009832 {
9833 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00009834 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +00009835 if (enc_utf8 && utf_iscomposing(c))
9836 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
9837 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
Bram Moolenaarea408852005-06-25 22:49:46 +00009838 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
9839 }
Bram Moolenaarea424162005-06-16 21:51:00 +00009840 else
9841#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00009842 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009843 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00009844 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
9845 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
9846 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009847 break;
9848 }
9849 /*FALLTHROUGH*/
9850
9851 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00009852 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009853 * node. */
9854 n = sp->ts_arridx;
9855 if (sp->ts_curi > byts[n])
9856 {
9857 /* Done all bytes at this node, do next state. */
9858 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009859 }
9860 else
9861 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009862 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009863 n += sp->ts_curi++;
9864 c = byts[n];
9865 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
9866 {
9867 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009868 sp = &stack[depth];
9869 tword[sp->ts_twordlen++] = c;
9870 sp->ts_arridx = idxs[n];
9871#ifdef FEAT_MBYTE
9872 if (has_mbyte)
9873 {
9874 fl = MB_BYTE2LEN(c);
9875 if (fl > 1)
9876 {
9877 /* There are following bytes for the same
9878 * character. We must find all bytes before
9879 * trying delete/insert/swap/etc. */
9880 sp->ts_tcharlen = fl;
9881 sp->ts_tcharidx = 1;
9882 sp->ts_isdiff = DIFF_INSERT;
9883 }
9884 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009885 else
9886 fl = 1;
9887 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00009888#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00009889 {
9890 /* If the previous character was the same, thus
9891 * doubling a character, give a bonus to the
9892 * score. */
9893 if (sp->ts_twordlen >= 2
9894 && tword[sp->ts_twordlen - 2] == c)
9895 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
9896 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009897 }
9898 }
9899 break;
9900
9901 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00009902 /*
9903 * Swap two bytes in the bad word: "12" -> "21".
9904 * We change "fword" here, it's changed back afterwards.
9905 */
9906 p = fword + sp->ts_fidx;
9907 c = *p;
9908 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009909 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009910 /* End of word, can't swap or replace. */
9911 sp->ts_state = STATE_FINAL;
9912 break;
9913 }
9914#ifdef FEAT_MBYTE
9915 if (has_mbyte)
9916 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009917 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009918 c = mb_ptr2char(p);
9919 c2 = mb_ptr2char(p + n);
9920 }
9921 else
9922#endif
9923 c2 = p[1];
9924 if (c == c2)
9925 {
9926 /* Characters are identical, swap won't do anything. */
9927 sp->ts_state = STATE_SWAP3;
9928 break;
9929 }
9930 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
9931 {
9932 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009933 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00009934#ifdef FEAT_MBYTE
9935 if (has_mbyte)
9936 {
9937 fl = mb_char2len(c2);
9938 mch_memmove(p, p + n, fl);
9939 mb_char2bytes(c, p + fl);
9940 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
9941 }
9942 else
9943#endif
9944 {
9945 p[0] = c2;
9946 p[1] = c;
9947 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
9948 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009949 }
9950 else
9951 /* If this swap doesn't work then SWAP3 won't either. */
9952 sp->ts_state = STATE_REP_INI;
9953 break;
9954
Bram Moolenaarea424162005-06-16 21:51:00 +00009955 case STATE_UNSWAP:
9956 /* Undo the STATE_SWAP swap: "21" -> "12". */
9957 p = fword + sp->ts_fidx;
9958#ifdef FEAT_MBYTE
9959 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009960 {
Bram Moolenaarea424162005-06-16 21:51:00 +00009961 n = MB_BYTE2LEN(*p);
9962 c = mb_ptr2char(p + n);
9963 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
9964 mb_char2bytes(c, p);
9965 }
9966 else
9967#endif
9968 {
9969 c = *p;
9970 *p = p[1];
9971 p[1] = c;
9972 }
9973 /*FALLTHROUGH*/
9974
9975 case STATE_SWAP3:
9976 /* Swap two bytes, skipping one: "123" -> "321". We change
9977 * "fword" here, it's changed back afterwards. */
9978 p = fword + sp->ts_fidx;
9979#ifdef FEAT_MBYTE
9980 if (has_mbyte)
9981 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009982 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +00009983 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009984 fl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +00009985 c2 = mb_ptr2char(p + n);
9986 c3 = mb_ptr2char(p + n + fl);
9987 }
9988 else
9989#endif
9990 {
9991 c = *p;
9992 c2 = p[1];
9993 c3 = p[2];
9994 }
9995
9996 /* When characters are identical: "121" then SWAP3 result is
9997 * identical, ROT3L result is same as SWAP: "211", ROT3L
9998 * result is same as SWAP on next char: "112". Thus skip all
9999 * swapping. Also skip when c3 is NUL. */
10000 if (c == c3 || c3 == NUL)
10001 {
10002 sp->ts_state = STATE_REP_INI;
10003 break;
10004 }
10005 if (try_deeper(su, stack, depth, SCORE_SWAP3))
10006 {
10007 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010008 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000010009#ifdef FEAT_MBYTE
10010 if (has_mbyte)
10011 {
10012 tl = mb_char2len(c3);
10013 mch_memmove(p, p + n + fl, tl);
10014 mb_char2bytes(c2, p + tl);
10015 mb_char2bytes(c, p + fl + tl);
10016 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
10017 }
10018 else
10019#endif
10020 {
10021 p[0] = p[2];
10022 p[2] = c;
10023 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
10024 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010025 }
10026 else
10027 sp->ts_state = STATE_REP_INI;
10028 break;
10029
Bram Moolenaarea424162005-06-16 21:51:00 +000010030 case STATE_UNSWAP3:
10031 /* Undo STATE_SWAP3: "321" -> "123" */
10032 p = fword + sp->ts_fidx;
10033#ifdef FEAT_MBYTE
10034 if (has_mbyte)
10035 {
10036 n = MB_BYTE2LEN(*p);
10037 c2 = mb_ptr2char(p + n);
10038 fl = MB_BYTE2LEN(p[n]);
10039 c = mb_ptr2char(p + n + fl);
10040 tl = MB_BYTE2LEN(p[n + fl]);
10041 mch_memmove(p + fl + tl, p, n);
10042 mb_char2bytes(c, p);
10043 mb_char2bytes(c2, p + tl);
10044 }
10045 else
10046#endif
10047 {
10048 c = *p;
10049 *p = p[2];
10050 p[2] = c;
10051 }
Bram Moolenaarea424162005-06-16 21:51:00 +000010052
Bram Moolenaarea424162005-06-16 21:51:00 +000010053 /* Rotate three characters left: "123" -> "231". We change
10054 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010055 if (try_deeper(su, stack, depth, SCORE_SWAP3))
10056 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010057 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010058 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000010059 p = fword + sp->ts_fidx;
10060#ifdef FEAT_MBYTE
10061 if (has_mbyte)
10062 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010063 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000010064 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010065 fl = mb_cptr2len(p + n);
10066 fl += mb_cptr2len(p + n + fl);
Bram Moolenaarea424162005-06-16 21:51:00 +000010067 mch_memmove(p, p + n, fl);
10068 mb_char2bytes(c, p + fl);
10069 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
10070 }
10071 else
10072#endif
10073 {
10074 c = *p;
10075 *p = p[1];
10076 p[1] = p[2];
10077 p[2] = c;
10078 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
10079 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010080 }
10081 else
10082 sp->ts_state = STATE_REP_INI;
10083 break;
10084
Bram Moolenaarea424162005-06-16 21:51:00 +000010085 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +000010086 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +000010087 p = fword + sp->ts_fidx;
10088#ifdef FEAT_MBYTE
10089 if (has_mbyte)
10090 {
10091 n = MB_BYTE2LEN(*p);
10092 n += MB_BYTE2LEN(p[n]);
10093 c = mb_ptr2char(p + n);
10094 tl = MB_BYTE2LEN(p[n]);
10095 mch_memmove(p + tl, p, n);
10096 mb_char2bytes(c, p);
10097 }
10098 else
10099#endif
10100 {
10101 c = p[2];
10102 p[2] = p[1];
10103 p[1] = *p;
10104 *p = c;
10105 }
Bram Moolenaarea424162005-06-16 21:51:00 +000010106
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010107 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +000010108 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010109 if (try_deeper(su, stack, depth, SCORE_SWAP3))
10110 {
Bram Moolenaarea424162005-06-16 21:51:00 +000010111 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010112 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000010113 p = fword + sp->ts_fidx;
10114#ifdef FEAT_MBYTE
10115 if (has_mbyte)
10116 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010117 n = mb_cptr2len(p);
10118 n += mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +000010119 c = mb_ptr2char(p + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010120 tl = mb_cptr2len(p + n);
Bram Moolenaarea424162005-06-16 21:51:00 +000010121 mch_memmove(p + tl, p, n);
10122 mb_char2bytes(c, p);
10123 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
10124 }
10125 else
10126#endif
10127 {
10128 c = p[2];
10129 p[2] = p[1];
10130 p[1] = *p;
10131 *p = c;
10132 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
10133 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010134 }
10135 else
10136 sp->ts_state = STATE_REP_INI;
10137 break;
10138
Bram Moolenaarea424162005-06-16 21:51:00 +000010139 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +000010140 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +000010141 p = fword + sp->ts_fidx;
10142#ifdef FEAT_MBYTE
10143 if (has_mbyte)
10144 {
10145 c = mb_ptr2char(p);
10146 tl = MB_BYTE2LEN(*p);
10147 n = MB_BYTE2LEN(p[tl]);
10148 n += MB_BYTE2LEN(p[tl + n]);
10149 mch_memmove(p, p + tl, n);
10150 mb_char2bytes(c, p + n);
10151 }
10152 else
10153#endif
10154 {
10155 c = *p;
10156 *p = p[1];
10157 p[1] = p[2];
10158 p[2] = c;
10159 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010160 /*FALLTHROUGH*/
10161
10162 case STATE_REP_INI:
10163 /* Check if matching with REP items from the .aff file would
10164 * work. Quickly skip if there are no REP items or the score
10165 * is going to be too high anyway. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010166 gap = &slang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010167 if (gap->ga_len == 0
10168 || sp->ts_score + SCORE_REP >= su->su_maxscore)
10169 {
10170 sp->ts_state = STATE_FINAL;
10171 break;
10172 }
10173
10174 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +000010175 * may match. If the index is -1 there is none. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010176 sp->ts_curi = slang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010177 if (sp->ts_curi < 0)
10178 {
10179 sp->ts_state = STATE_FINAL;
10180 break;
10181 }
10182
10183 sp->ts_state = STATE_REP;
10184 /*FALLTHROUGH*/
10185
10186 case STATE_REP:
10187 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +000010188 * match replace the characters and check if the resulting
10189 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010190 p = fword + sp->ts_fidx;
10191
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010192 gap = &slang->sl_rep;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010193 while (sp->ts_curi < gap->ga_len)
10194 {
10195 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
10196 if (*ftp->ft_from != *p)
10197 {
10198 /* past possible matching entries */
10199 sp->ts_curi = gap->ga_len;
10200 break;
10201 }
10202 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
10203 && try_deeper(su, stack, depth, SCORE_REP))
10204 {
10205 /* Need to undo this afterwards. */
10206 sp->ts_state = STATE_REP_UNDO;
10207
10208 /* Change the "from" to the "to" string. */
10209 ++depth;
10210 fl = STRLEN(ftp->ft_from);
10211 tl = STRLEN(ftp->ft_to);
10212 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +000010213 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010214 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010215 repextra += tl - fl;
10216 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010217 mch_memmove(p, ftp->ft_to, tl);
10218 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +000010219#ifdef FEAT_MBYTE
10220 stack[depth].ts_tcharlen = 0;
10221#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010222 break;
10223 }
10224 }
10225
10226 if (sp->ts_curi >= gap->ga_len)
10227 /* No (more) matches. */
10228 sp->ts_state = STATE_FINAL;
10229
10230 break;
10231
10232 case STATE_REP_UNDO:
10233 /* Undo a REP replacement and continue with the next one. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010234 ftp = (fromto_T *)slang->sl_rep.ga_data + sp->ts_curi - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010235 fl = STRLEN(ftp->ft_from);
10236 tl = STRLEN(ftp->ft_to);
10237 p = fword + sp->ts_fidx;
10238 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +000010239 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010240 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010241 repextra -= tl - fl;
10242 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010243 mch_memmove(p, ftp->ft_from, fl);
10244 sp->ts_state = STATE_REP;
10245 break;
10246
10247 default:
10248 /* Did all possible states at this level, go up one level. */
10249 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010250
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000010251 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010252 {
10253 /* Continue in or go back to the prefix tree. */
10254 byts = pbyts;
10255 idxs = pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010256 }
10257
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010258 /* Don't check for CTRL-C too often, it takes time. */
10259 line_breakcheck();
10260 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010261 }
10262 }
10263}
10264
10265/*
10266 * Try going one level deeper in the tree.
10267 */
10268 static int
10269try_deeper(su, stack, depth, score_add)
10270 suginfo_T *su;
10271 trystate_T *stack;
10272 int depth;
10273 int score_add;
10274{
10275 int newscore;
10276
10277 /* Refuse to go deeper if the scrore is getting too big. */
10278 newscore = stack[depth].ts_score + score_add;
10279 if (newscore >= su->su_maxscore)
10280 return FALSE;
10281
Bram Moolenaarea424162005-06-16 21:51:00 +000010282 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010283 stack[depth + 1].ts_state = STATE_START;
10284 stack[depth + 1].ts_score = newscore;
10285 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010286 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010287 return TRUE;
10288}
10289
Bram Moolenaar53805d12005-08-01 07:08:33 +000010290#ifdef FEAT_MBYTE
10291/*
10292 * Case-folding may change the number of bytes: Count nr of chars in
10293 * fword[flen] and return the byte length of that many chars in "word".
10294 */
10295 static int
10296nofold_len(fword, flen, word)
10297 char_u *fword;
10298 int flen;
10299 char_u *word;
10300{
10301 char_u *p;
10302 int i = 0;
10303
10304 for (p = fword; p < fword + flen; mb_ptr_adv(p))
10305 ++i;
10306 for (p = word; i > 0; mb_ptr_adv(p))
10307 --i;
10308 return (int)(p - word);
10309}
10310#endif
10311
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010312/*
10313 * "fword" is a good word with case folded. Find the matching keep-case
10314 * words and put it in "kword".
10315 * Theoretically there could be several keep-case words that result in the
10316 * same case-folded word, but we only find one...
10317 */
10318 static void
10319find_keepcap_word(slang, fword, kword)
10320 slang_T *slang;
10321 char_u *fword;
10322 char_u *kword;
10323{
10324 char_u uword[MAXWLEN]; /* "fword" in upper-case */
10325 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010326 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010327
10328 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010329 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010330 int round[MAXWLEN];
10331 int fwordidx[MAXWLEN];
10332 int uwordidx[MAXWLEN];
10333 int kwordlen[MAXWLEN];
10334
10335 int flen, ulen;
10336 int l;
10337 int len;
10338 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010339 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010340 char_u *p;
10341 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010342 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010343
10344 if (byts == NULL)
10345 {
10346 /* array is empty: "cannot happen" */
10347 *kword = NUL;
10348 return;
10349 }
10350
10351 /* Make an all-cap version of "fword". */
10352 allcap_copy(fword, uword);
10353
10354 /*
10355 * Each character needs to be tried both case-folded and upper-case.
10356 * All this gets very complicated if we keep in mind that changing case
10357 * may change the byte length of a multi-byte character...
10358 */
10359 depth = 0;
10360 arridx[0] = 0;
10361 round[0] = 0;
10362 fwordidx[0] = 0;
10363 uwordidx[0] = 0;
10364 kwordlen[0] = 0;
10365 while (depth >= 0)
10366 {
10367 if (fword[fwordidx[depth]] == NUL)
10368 {
10369 /* We are at the end of "fword". If the tree allows a word to end
10370 * here we have found a match. */
10371 if (byts[arridx[depth] + 1] == 0)
10372 {
10373 kword[kwordlen[depth]] = NUL;
10374 return;
10375 }
10376
10377 /* kword is getting too long, continue one level up */
10378 --depth;
10379 }
10380 else if (++round[depth] > 2)
10381 {
10382 /* tried both fold-case and upper-case character, continue one
10383 * level up */
10384 --depth;
10385 }
10386 else
10387 {
10388 /*
10389 * round[depth] == 1: Try using the folded-case character.
10390 * round[depth] == 2: Try using the upper-case character.
10391 */
10392#ifdef FEAT_MBYTE
10393 if (has_mbyte)
10394 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010395 flen = mb_cptr2len(fword + fwordidx[depth]);
10396 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010397 }
10398 else
10399#endif
10400 ulen = flen = 1;
10401 if (round[depth] == 1)
10402 {
10403 p = fword + fwordidx[depth];
10404 l = flen;
10405 }
10406 else
10407 {
10408 p = uword + uwordidx[depth];
10409 l = ulen;
10410 }
10411
10412 for (tryidx = arridx[depth]; l > 0; --l)
10413 {
10414 /* Perform a binary search in the list of accepted bytes. */
10415 len = byts[tryidx++];
10416 c = *p++;
10417 lo = tryidx;
10418 hi = tryidx + len - 1;
10419 while (lo < hi)
10420 {
10421 m = (lo + hi) / 2;
10422 if (byts[m] > c)
10423 hi = m - 1;
10424 else if (byts[m] < c)
10425 lo = m + 1;
10426 else
10427 {
10428 lo = hi = m;
10429 break;
10430 }
10431 }
10432
10433 /* Stop if there is no matching byte. */
10434 if (hi < lo || byts[lo] != c)
10435 break;
10436
10437 /* Continue at the child (if there is one). */
10438 tryidx = idxs[lo];
10439 }
10440
10441 if (l == 0)
10442 {
10443 /*
10444 * Found the matching char. Copy it to "kword" and go a
10445 * level deeper.
10446 */
10447 if (round[depth] == 1)
10448 {
10449 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
10450 flen);
10451 kwordlen[depth + 1] = kwordlen[depth] + flen;
10452 }
10453 else
10454 {
10455 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
10456 ulen);
10457 kwordlen[depth + 1] = kwordlen[depth] + ulen;
10458 }
10459 fwordidx[depth + 1] = fwordidx[depth] + flen;
10460 uwordidx[depth + 1] = uwordidx[depth] + ulen;
10461
10462 ++depth;
10463 arridx[depth] = tryidx;
10464 round[depth] = 0;
10465 }
10466 }
10467 }
10468
10469 /* Didn't find it: "cannot happen". */
10470 *kword = NUL;
10471}
10472
10473/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010474 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
10475 * su->su_sga.
10476 */
10477 static void
10478score_comp_sal(su)
10479 suginfo_T *su;
10480{
10481 langp_T *lp;
10482 char_u badsound[MAXWLEN];
10483 int i;
10484 suggest_T *stp;
10485 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010486 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010487 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010488
10489 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
10490 return;
10491
10492 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010493 for (lpi = 0; lpi < curwin->w_buffer->b_langp.ga_len; ++lpi)
10494 {
10495 lp = LANGP_ENTRY(curwin->w_buffer->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010496 if (lp->lp_slang->sl_sal.ga_len > 0)
10497 {
10498 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010499 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010500
10501 for (i = 0; i < su->su_ga.ga_len; ++i)
10502 {
10503 stp = &SUG(su->su_ga, i);
10504
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010505 /* Case-fold the suggested word, sound-fold it and compute the
10506 * sound-a-like score. */
10507 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010508 if (score < SCORE_MAXMAX)
10509 {
10510 /* Add the suggestion. */
10511 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
10512 sstp->st_word = vim_strsave(stp->st_word);
10513 if (sstp->st_word != NULL)
10514 {
10515 sstp->st_score = score;
10516 sstp->st_altscore = 0;
10517 sstp->st_orglen = stp->st_orglen;
10518 ++su->su_sga.ga_len;
10519 }
10520 }
10521 }
10522 break;
10523 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010524 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010525}
10526
10527/*
10528 * Combine the list of suggestions in su->su_ga and su->su_sga.
10529 * They are intwined.
10530 */
10531 static void
10532score_combine(su)
10533 suginfo_T *su;
10534{
10535 int i;
10536 int j;
10537 garray_T ga;
10538 garray_T *gap;
10539 langp_T *lp;
10540 suggest_T *stp;
10541 char_u *p;
10542 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010543 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010544 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010545
10546 /* Add the alternate score to su_ga. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010547 for (lpi = 0; lpi < curwin->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010548 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010549 lp = LANGP_ENTRY(curwin->w_buffer->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010550 if (lp->lp_slang->sl_sal.ga_len > 0)
10551 {
10552 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010553 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010554
10555 for (i = 0; i < su->su_ga.ga_len; ++i)
10556 {
10557 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010558 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
10559 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010560 if (stp->st_altscore == SCORE_MAXMAX)
10561 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
10562 else
10563 stp->st_score = (stp->st_score * 3
10564 + stp->st_altscore) / 4;
10565 stp->st_salscore = FALSE;
10566 }
10567 break;
10568 }
10569 }
10570
10571 /* Add the alternate score to su_sga. */
10572 for (i = 0; i < su->su_sga.ga_len; ++i)
10573 {
10574 stp = &SUG(su->su_sga, i);
10575 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
10576 if (stp->st_score == SCORE_MAXMAX)
10577 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
10578 else
10579 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
10580 stp->st_salscore = TRUE;
10581 }
10582
10583 /* Sort the suggestions and truncate at "maxcount" for both lists. */
10584 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10585 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
10586
10587 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
10588 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
10589 return;
10590
10591 stp = &SUG(ga, 0);
10592 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
10593 {
10594 /* round 1: get a suggestion from su_ga
10595 * round 2: get a suggestion from su_sga */
10596 for (round = 1; round <= 2; ++round)
10597 {
10598 gap = round == 1 ? &su->su_ga : &su->su_sga;
10599 if (i < gap->ga_len)
10600 {
10601 /* Don't add a word if it's already there. */
10602 p = SUG(*gap, i).st_word;
10603 for (j = 0; j < ga.ga_len; ++j)
10604 if (STRCMP(stp[j].st_word, p) == 0)
10605 break;
10606 if (j == ga.ga_len)
10607 stp[ga.ga_len++] = SUG(*gap, i);
10608 else
10609 vim_free(p);
10610 }
10611 }
10612 }
10613
10614 ga_clear(&su->su_ga);
10615 ga_clear(&su->su_sga);
10616
10617 /* Truncate the list to the number of suggestions that will be displayed. */
10618 if (ga.ga_len > su->su_maxcount)
10619 {
10620 for (i = su->su_maxcount; i < ga.ga_len; ++i)
10621 vim_free(stp[i].st_word);
10622 ga.ga_len = su->su_maxcount;
10623 }
10624
10625 su->su_ga = ga;
10626}
10627
10628/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010629 * For the goodword in "stp" compute the soundalike score compared to the
10630 * badword.
10631 */
10632 static int
10633stp_sal_score(stp, su, slang, badsound)
10634 suggest_T *stp;
10635 suginfo_T *su;
10636 slang_T *slang;
10637 char_u *badsound; /* sound-folded badword */
10638{
10639 char_u *p;
10640 char_u badsound2[MAXWLEN];
10641 char_u fword[MAXWLEN];
10642 char_u goodsound[MAXWLEN];
10643
10644 if (stp->st_orglen <= su->su_badlen)
10645 p = badsound;
10646 else
10647 {
10648 /* soundfold the bad word with more characters following */
10649 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
10650
10651 /* When joining two words the sound often changes a lot. E.g., "t he"
10652 * sounds like "t h" while "the" sounds like "@". Avoid that by
10653 * removing the space. Don't do it when the good word also contains a
10654 * space. */
10655 if (vim_iswhite(su->su_badptr[su->su_badlen])
10656 && *skiptowhite(stp->st_word) == NUL)
10657 for (p = fword; *(p = skiptowhite(p)) != NUL; )
10658 mch_memmove(p, p + 1, STRLEN(p));
10659
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010660 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010661 p = badsound2;
10662 }
10663
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010664 /* Sound-fold the word and compute the score for the difference. */
10665 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010666
10667 return soundalike_score(goodsound, p);
10668}
10669
10670/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010671 * Find suggestions by comparing the word in a sound-a-like form.
10672 */
10673 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000010674suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010675 suginfo_T *su;
10676{
10677 char_u salword[MAXWLEN];
10678 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010679 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010680 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010681 int curi[MAXWLEN];
10682 langp_T *lp;
10683 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010684 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010685 int depth;
10686 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010687 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010688 int round;
10689 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010690 int sound_score;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010691 int local_score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010692 int lpi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010693
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010694 /* Do this for all languages that support sound folding. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010695 for (lpi = 0; lpi < curwin->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010696 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010697 lp = LANGP_ENTRY(curwin->w_buffer->b_langp, lpi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010698 if (lp->lp_slang->sl_sal.ga_len > 0)
10699 {
10700 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010701 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010702
10703 /*
10704 * Go through the whole tree, soundfold each word and compare.
10705 * round 1: use the case-folded tree.
10706 * round 2: use the keep-case tree.
10707 */
10708 for (round = 1; round <= 2; ++round)
10709 {
10710 if (round == 1)
10711 {
10712 byts = lp->lp_slang->sl_fbyts;
10713 idxs = lp->lp_slang->sl_fidxs;
10714 }
10715 else
10716 {
10717 byts = lp->lp_slang->sl_kbyts;
10718 idxs = lp->lp_slang->sl_kidxs;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010719 if (byts == NULL) /* no keep-case words */
10720 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010721 }
10722
10723 depth = 0;
10724 arridx[0] = 0;
10725 curi[0] = 1;
10726 while (depth >= 0 && !got_int)
10727 {
10728 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010729 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010730 /* Done all bytes at this node, go up one level. */
10731 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010732 line_breakcheck();
10733 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010734 else
10735 {
10736 /* Do one more byte at this node. */
10737 n = arridx[depth] + curi[depth];
10738 ++curi[depth];
10739 c = byts[n];
10740 if (c == 0)
10741 {
10742 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010743 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010744 if (round == 2 || (flags & WF_KEEPCAP) == 0)
10745 {
10746 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010747 /* Sound-fold. Only in keep-case tree need to
10748 * case-fold the word. */
10749 spell_soundfold(lp->lp_slang, tword,
10750 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010751
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010752 /* Compute the edit distance between the
10753 * sound-a-like words. */
10754 sound_score = soundalike_score(salword,
10755 tsalword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010756
10757 /* Add a penalty for words in another region. */
10758 if ((flags & WF_REGION) && (((unsigned)flags
10759 >> 16) & lp->lp_region) == 0)
10760 local_score = SCORE_REGION;
10761 else
10762 local_score = 0;
10763 sound_score += local_score;
10764
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010765 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010766 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010767 char_u cword[MAXWLEN];
10768 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010769 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010770
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010771 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010772 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010773 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010774 /* Need to fix case according to
10775 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010776 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010777 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010778 }
10779 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010780 p = tword;
10781
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010782 if (sps_flags & SPS_DOUBLE)
10783 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000010784 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010785 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010786 else
10787 {
10788 /* Compute the score. */
10789 score = spell_edit_score(
Bram Moolenaar5195e452005-08-19 20:32:47 +000010790 su->su_badword, p)
10791 + local_score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010792 if (sps_flags & SPS_BEST)
10793 /* give a bonus for the good word
10794 * sounding the same as the bad
10795 * word */
10796 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000010797 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010798 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010799 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010800 else
10801 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +000010802 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010803 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010804 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010805 }
10806 }
10807
10808 /* Skip over other NUL bytes. */
10809 while (byts[n + 1] == 0)
10810 {
10811 ++n;
10812 ++curi[depth];
10813 }
10814 }
10815 else
10816 {
10817 /* Normal char, go one level deeper. */
10818 tword[depth++] = c;
10819 arridx[depth] = idxs[n];
10820 curi[depth] = 1;
10821 }
10822 }
10823 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010824 }
10825 }
10826 }
10827}
10828
10829/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010830 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010831 */
10832 static void
10833make_case_word(fword, cword, flags)
10834 char_u *fword;
10835 char_u *cword;
10836 int flags;
10837{
10838 if (flags & WF_ALLCAP)
10839 /* Make it all upper-case */
10840 allcap_copy(fword, cword);
10841 else if (flags & WF_ONECAP)
10842 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010843 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010844 else
10845 /* Use goodword as-is. */
10846 STRCPY(cword, fword);
10847}
10848
Bram Moolenaarea424162005-06-16 21:51:00 +000010849/*
10850 * Use map string "map" for languages "lp".
10851 */
10852 static void
10853set_map_str(lp, map)
10854 slang_T *lp;
10855 char_u *map;
10856{
10857 char_u *p;
10858 int headc = 0;
10859 int c;
10860 int i;
10861
10862 if (*map == NUL)
10863 {
10864 lp->sl_has_map = FALSE;
10865 return;
10866 }
10867 lp->sl_has_map = TRUE;
10868
10869 /* Init the array and hash table empty. */
10870 for (i = 0; i < 256; ++i)
10871 lp->sl_map_array[i] = 0;
10872#ifdef FEAT_MBYTE
10873 hash_init(&lp->sl_map_hash);
10874#endif
10875
10876 /*
10877 * The similar characters are stored separated with slashes:
10878 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
10879 * before the same slash. For characters above 255 sl_map_hash is used.
10880 */
10881 for (p = map; *p != NUL; )
10882 {
10883#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010884 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000010885#else
10886 c = *p++;
10887#endif
10888 if (c == '/')
10889 headc = 0;
10890 else
10891 {
10892 if (headc == 0)
10893 headc = c;
10894
10895#ifdef FEAT_MBYTE
10896 /* Characters above 255 don't fit in sl_map_array[], put them in
10897 * the hash table. Each entry is the char, a NUL the headchar and
10898 * a NUL. */
10899 if (c >= 256)
10900 {
10901 int cl = mb_char2len(c);
10902 int headcl = mb_char2len(headc);
10903 char_u *b;
10904 hash_T hash;
10905 hashitem_T *hi;
10906
10907 b = alloc((unsigned)(cl + headcl + 2));
10908 if (b == NULL)
10909 return;
10910 mb_char2bytes(c, b);
10911 b[cl] = NUL;
10912 mb_char2bytes(headc, b + cl + 1);
10913 b[cl + 1 + headcl] = NUL;
10914 hash = hash_hash(b);
10915 hi = hash_lookup(&lp->sl_map_hash, b, hash);
10916 if (HASHITEM_EMPTY(hi))
10917 hash_add_item(&lp->sl_map_hash, hi, b, hash);
10918 else
10919 {
10920 /* This should have been checked when generating the .spl
10921 * file. */
10922 EMSG(_("E999: duplicate char in MAP entry"));
10923 vim_free(b);
10924 }
10925 }
10926 else
10927#endif
10928 lp->sl_map_array[c] = headc;
10929 }
10930 }
10931}
10932
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010933/*
10934 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
10935 * lines in the .aff file.
10936 */
10937 static int
10938similar_chars(slang, c1, c2)
10939 slang_T *slang;
10940 int c1;
10941 int c2;
10942{
Bram Moolenaarea424162005-06-16 21:51:00 +000010943 int m1, m2;
10944#ifdef FEAT_MBYTE
10945 char_u buf[MB_MAXBYTES];
10946 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010947
Bram Moolenaarea424162005-06-16 21:51:00 +000010948 if (c1 >= 256)
10949 {
10950 buf[mb_char2bytes(c1, buf)] = 0;
10951 hi = hash_find(&slang->sl_map_hash, buf);
10952 if (HASHITEM_EMPTY(hi))
10953 m1 = 0;
10954 else
10955 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
10956 }
10957 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010958#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000010959 m1 = slang->sl_map_array[c1];
10960 if (m1 == 0)
10961 return FALSE;
10962
10963
10964#ifdef FEAT_MBYTE
10965 if (c2 >= 256)
10966 {
10967 buf[mb_char2bytes(c2, buf)] = 0;
10968 hi = hash_find(&slang->sl_map_hash, buf);
10969 if (HASHITEM_EMPTY(hi))
10970 m2 = 0;
10971 else
10972 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
10973 }
10974 else
10975#endif
10976 m2 = slang->sl_map_array[c2];
10977
10978 return m1 == m2;
10979}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010980
10981/*
10982 * Add a suggestion to the list of suggestions.
10983 * Do not add a duplicate suggestion or suggestions with a bad score.
10984 * When "use_score" is not zero it's used, otherwise the score is computed
10985 * with spell_edit_score().
10986 */
10987 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010988add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010989 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010990 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010991 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010992 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010993 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010994 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010995 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010996{
10997 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010998 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010999 char_u *p = NULL;
11000 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011001
11002 /* Check that the word wasn't banned. */
11003 if (was_banned(su, goodword))
11004 return;
11005
Bram Moolenaar0c405862005-06-22 22:26:26 +000011006 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
11007 * Remove the common part from "goodword". */
11008 i = badlen - su->su_badlen;
11009 if (i > 0)
11010 {
11011 /* This assumes there was no case folding or it didn't change the
11012 * length... */
11013 p = goodword + STRLEN(goodword) - i;
11014 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
11015 {
11016 badlen = su->su_badlen;
11017 c = *p;
11018 *p = NUL;
11019 }
11020 else
11021 p = NULL;
11022 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011023 else if (i < 0)
11024 {
11025 /* When replacing part of the word check that we actually change
11026 * something. For "the the" a suggestion can be replacing the first
11027 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011028 if (badlen == (int)STRLEN(goodword)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011029 && STRNCMP(su->su_badword, goodword, badlen) == 0)
11030 return;
11031 }
11032
Bram Moolenaar0c405862005-06-22 22:26:26 +000011033
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011034 if (score <= su->su_maxscore)
11035 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011036 /* Check if the word is already there. Also check the length that is
11037 * being replaced "thes," -> "these" is a different suggestion from
11038 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011039 stp = &SUG(*gap, 0);
11040 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011041 if (STRCMP(stp[i].st_word, goodword) == 0
11042 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011043 {
11044 /* Found it. Remember the lowest score. */
11045 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011046 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011047 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011048 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011049 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011050 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011051 break;
11052 }
11053
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011054 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011055 {
11056 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011057 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011058 stp->st_word = vim_strsave(goodword);
11059 if (stp->st_word != NULL)
11060 {
11061 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011062 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011063 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011064 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011065 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011066
11067 /* If we have too many suggestions now, sort the list and keep
11068 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011069 if (gap->ga_len > SUG_MAX_COUNT(su))
11070 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
11071 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011072 }
11073 }
11074 }
Bram Moolenaar0c405862005-06-22 22:26:26 +000011075
11076 if (p != NULL)
11077 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011078}
11079
11080/*
11081 * Add a word to be banned.
11082 */
11083 static void
11084add_banned(su, word)
11085 suginfo_T *su;
11086 char_u *word;
11087{
11088 char_u *s = vim_strsave(word);
11089 hash_T hash;
11090 hashitem_T *hi;
11091
11092 if (s != NULL)
11093 {
11094 hash = hash_hash(s);
11095 hi = hash_lookup(&su->su_banned, s, hash);
11096 if (HASHITEM_EMPTY(hi))
11097 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000011098 else
11099 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011100 }
11101}
11102
11103/*
11104 * Return TRUE if a word appears in the list of banned words.
11105 */
11106 static int
11107was_banned(su, word)
11108 suginfo_T *su;
11109 char_u *word;
11110{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011111 hashitem_T *hi = hash_find(&su->su_banned, word);
11112
11113 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011114}
11115
11116/*
11117 * Free the banned words in "su".
11118 */
11119 static void
11120free_banned(su)
11121 suginfo_T *su;
11122{
11123 int todo;
11124 hashitem_T *hi;
11125
11126 todo = su->su_banned.ht_used;
11127 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
11128 {
11129 if (!HASHITEM_EMPTY(hi))
11130 {
11131 vim_free(hi->hi_key);
11132 --todo;
11133 }
11134 }
11135 hash_clear(&su->su_banned);
11136}
11137
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011138/*
11139 * Recompute the score if sound-folding is possible. This is slow,
11140 * thus only done for the final results.
11141 */
11142 static void
11143rescore_suggestions(su)
11144 suginfo_T *su;
11145{
11146 langp_T *lp;
11147 suggest_T *stp;
11148 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011149 int i;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011150 int lpi;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011151
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011152 for (lpi = 0; lpi < curwin->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011153 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011154 lp = LANGP_ENTRY(curwin->w_buffer->b_langp, lpi);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011155 if (lp->lp_slang->sl_sal.ga_len > 0)
11156 {
11157 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011158 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011159
11160 for (i = 0; i < su->su_ga.ga_len; ++i)
11161 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011162 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011163 if (!stp->st_had_bonus)
11164 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011165 stp->st_altscore = stp_sal_score(stp, su,
11166 lp->lp_slang, sal_badword);
11167 if (stp->st_altscore == SCORE_MAXMAX)
11168 stp->st_altscore = SCORE_BIG;
11169 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011170 }
11171 }
11172 break;
11173 }
11174 }
11175}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011176
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011177static int
11178#ifdef __BORLANDC__
11179_RTLENTRYF
11180#endif
11181sug_compare __ARGS((const void *s1, const void *s2));
11182
11183/*
11184 * Function given to qsort() to sort the suggestions on st_score.
11185 */
11186 static int
11187#ifdef __BORLANDC__
11188_RTLENTRYF
11189#endif
11190sug_compare(s1, s2)
11191 const void *s1;
11192 const void *s2;
11193{
11194 suggest_T *p1 = (suggest_T *)s1;
11195 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011196 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011197
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011198 if (n == 0)
11199 return p1->st_altscore - p2->st_altscore;
11200 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011201}
11202
11203/*
11204 * Cleanup the suggestions:
11205 * - Sort on score.
11206 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011207 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011208 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011209 static int
11210cleanup_suggestions(gap, maxscore, keep)
11211 garray_T *gap;
11212 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011213 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011214{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011215 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011216 int i;
11217
11218 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011219 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011220
11221 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011222 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011223 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011224 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011225 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011226 gap->ga_len = keep;
11227 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011228 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011229 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011230}
11231
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011232#if defined(FEAT_EVAL) || defined(PROTO)
11233/*
11234 * Soundfold a string, for soundfold().
11235 * Result is in allocated memory, NULL for an error.
11236 */
11237 char_u *
11238eval_soundfold(word)
11239 char_u *word;
11240{
11241 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011242 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011243 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011244
11245 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
11246 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011247 for (lpi = 0; lpi < curwin->w_buffer->b_langp.ga_len; ++lpi)
11248 {
11249 lp = LANGP_ENTRY(curwin->w_buffer->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011250 if (lp->lp_slang->sl_sal.ga_len > 0)
11251 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011252 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011253 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011254 return vim_strsave(sound);
11255 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011256 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011257
11258 /* No language with sound folding, return word as-is. */
11259 return vim_strsave(word);
11260}
11261#endif
11262
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011263/*
11264 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000011265 *
11266 * There are many ways to turn a word into a sound-a-like representation. The
11267 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
11268 * swedish name matching - survey and test of different algorithms" by Klas
11269 * Erikson.
11270 *
11271 * We support two methods:
11272 * 1. SOFOFROM/SOFOTO do a simple character mapping.
11273 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011274 */
11275 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011276spell_soundfold(slang, inword, folded, res)
11277 slang_T *slang;
11278 char_u *inword;
11279 int folded; /* "inword" is already case-folded */
11280 char_u *res;
11281{
11282 char_u fword[MAXWLEN];
11283 char_u *word;
11284
11285 if (slang->sl_sofo)
11286 /* SOFOFROM and SOFOTO used */
11287 spell_soundfold_sofo(slang, inword, res);
11288 else
11289 {
11290 /* SAL items used. Requires the word to be case-folded. */
11291 if (folded)
11292 word = inword;
11293 else
11294 {
11295 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
11296 word = fword;
11297 }
11298
11299#ifdef FEAT_MBYTE
11300 if (has_mbyte)
11301 spell_soundfold_wsal(slang, word, res);
11302 else
11303#endif
11304 spell_soundfold_sal(slang, word, res);
11305 }
11306}
11307
11308/*
11309 * Perform sound folding of "inword" into "res" according to SOFOFROM and
11310 * SOFOTO lines.
11311 */
11312 static void
11313spell_soundfold_sofo(slang, inword, res)
11314 slang_T *slang;
11315 char_u *inword;
11316 char_u *res;
11317{
11318 char_u *s;
11319 int ri = 0;
11320 int c;
11321
11322#ifdef FEAT_MBYTE
11323 if (has_mbyte)
11324 {
11325 int prevc = 0;
11326 int *ip;
11327
11328 /* The sl_sal_first[] table contains the translation for chars up to
11329 * 255, sl_sal the rest. */
11330 for (s = inword; *s != NUL; )
11331 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011332 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011333 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
11334 c = ' ';
11335 else if (c < 256)
11336 c = slang->sl_sal_first[c];
11337 else
11338 {
11339 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
11340 if (ip == NULL) /* empty list, can't match */
11341 c = NUL;
11342 else
11343 for (;;) /* find "c" in the list */
11344 {
11345 if (*ip == 0) /* not found */
11346 {
11347 c = NUL;
11348 break;
11349 }
11350 if (*ip == c) /* match! */
11351 {
11352 c = ip[1];
11353 break;
11354 }
11355 ip += 2;
11356 }
11357 }
11358
11359 if (c != NUL && c != prevc)
11360 {
11361 ri += mb_char2bytes(c, res + ri);
11362 if (ri + MB_MAXBYTES > MAXWLEN)
11363 break;
11364 prevc = c;
11365 }
11366 }
11367 }
11368 else
11369#endif
11370 {
11371 /* The sl_sal_first[] table contains the translation. */
11372 for (s = inword; (c = *s) != NUL; ++s)
11373 {
11374 if (vim_iswhite(c))
11375 c = ' ';
11376 else
11377 c = slang->sl_sal_first[c];
11378 if (c != NUL && (ri == 0 || res[ri - 1] != c))
11379 res[ri++] = c;
11380 }
11381 }
11382
11383 res[ri] = NUL;
11384}
11385
11386 static void
11387spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011388 slang_T *slang;
11389 char_u *inword;
11390 char_u *res;
11391{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011392 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011393 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011394 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011395 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011396 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011397 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011398 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011399 int n, k = 0;
11400 int z0;
11401 int k0;
11402 int n0;
11403 int c;
11404 int pri;
11405 int p0 = -333;
11406 int c0;
11407
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011408 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011409 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011410 if (slang->sl_rem_accents)
11411 {
11412 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011413 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011414 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011415 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011416 {
11417 *t++ = ' ';
11418 s = skipwhite(s);
11419 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011420 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011421 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011422 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011423 *t++ = *s;
11424 ++s;
11425 }
11426 }
11427 *t = NUL;
11428 }
11429 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011430 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011431
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011432 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011433
11434 /*
11435 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011436 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011437 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011438 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011439 while ((c = word[i]) != NUL)
11440 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011441 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011442 n = slang->sl_sal_first[c];
11443 z0 = 0;
11444
11445 if (n >= 0)
11446 {
11447 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011448 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011449 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011450 /* Quickly skip entries that don't match the word. Most
11451 * entries are less then three chars, optimize for that. */
11452 k = smp[n].sm_leadlen;
11453 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011454 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011455 if (word[i + 1] != s[1])
11456 continue;
11457 if (k > 2)
11458 {
11459 for (j = 2; j < k; ++j)
11460 if (word[i + j] != s[j])
11461 break;
11462 if (j < k)
11463 continue;
11464 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011465 }
11466
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011467 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011468 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011469 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011470 while (*pf != NUL && *pf != word[i + k])
11471 ++pf;
11472 if (*pf == NUL)
11473 continue;
11474 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011475 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011476 s = smp[n].sm_rules;
11477 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011478
11479 p0 = *s;
11480 k0 = k;
11481 while (*s == '-' && k > 1)
11482 {
11483 k--;
11484 s++;
11485 }
11486 if (*s == '<')
11487 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011488 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011489 {
11490 /* determine priority */
11491 pri = *s - '0';
11492 s++;
11493 }
11494 if (*s == '^' && *(s + 1) == '^')
11495 s++;
11496
11497 if (*s == NUL
11498 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011499 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011500 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011501 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011502 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011503 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011504 && spell_iswordp(word + i - 1, curbuf)
11505 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011506 {
11507 /* search for followup rules, if: */
11508 /* followup and k > 1 and NO '-' in searchstring */
11509 c0 = word[i + k - 1];
11510 n0 = slang->sl_sal_first[c0];
11511
11512 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011513 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011514 {
11515 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011516 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011517 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011518 /* Quickly skip entries that don't match the word.
11519 * */
11520 k0 = smp[n0].sm_leadlen;
11521 if (k0 > 1)
11522 {
11523 if (word[i + k] != s[1])
11524 continue;
11525 if (k0 > 2)
11526 {
11527 pf = word + i + k + 1;
11528 for (j = 2; j < k0; ++j)
11529 if (*pf++ != s[j])
11530 break;
11531 if (j < k0)
11532 continue;
11533 }
11534 }
11535 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011536
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011537 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011538 {
11539 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011540 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011541 while (*pf != NUL && *pf != word[i + k0])
11542 ++pf;
11543 if (*pf == NUL)
11544 continue;
11545 ++k0;
11546 }
11547
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011548 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011549 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011550 while (*s == '-')
11551 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011552 /* "k0" gets NOT reduced because
11553 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011554 s++;
11555 }
11556 if (*s == '<')
11557 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011558 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011559 {
11560 p0 = *s - '0';
11561 s++;
11562 }
11563
11564 if (*s == NUL
11565 /* *s == '^' cuts */
11566 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011567 && !spell_iswordp(word + i + k0,
11568 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011569 {
11570 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011571 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011572 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011573
11574 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011575 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011576 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011577 /* rule fits; stop search */
11578 break;
11579 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011580 }
11581
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011582 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011583 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011584 }
11585
11586 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011587 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011588 if (s == NULL)
11589 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011590 pf = smp[n].sm_rules;
11591 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011592 if (p0 == 1 && z == 0)
11593 {
11594 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011595 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
11596 || res[reslen - 1] == *s))
11597 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011598 z0 = 1;
11599 z = 1;
11600 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011601 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011602 {
11603 word[i + k0] = *s;
11604 k0++;
11605 s++;
11606 }
11607 if (k > k0)
11608 mch_memmove(word + i + k0, word + i + k,
11609 STRLEN(word + i + k) + 1);
11610
11611 /* new "actual letter" */
11612 c = word[i];
11613 }
11614 else
11615 {
11616 /* no '<' rule used */
11617 i += k - 1;
11618 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011619 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011620 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011621 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011622 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011623 s++;
11624 }
11625 /* new "actual letter" */
11626 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011627 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011628 {
11629 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011630 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011631 mch_memmove(word, word + i + 1,
11632 STRLEN(word + i + 1) + 1);
11633 i = 0;
11634 z0 = 1;
11635 }
11636 }
11637 break;
11638 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011639 }
11640 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011641 else if (vim_iswhite(c))
11642 {
11643 c = ' ';
11644 k = 1;
11645 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011646
11647 if (z0 == 0)
11648 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011649 if (k && !p0 && reslen < MAXWLEN && c != NUL
11650 && (!slang->sl_collapse || reslen == 0
11651 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011652 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011653 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011654
11655 i++;
11656 z = 0;
11657 k = 0;
11658 }
11659 }
11660
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011661 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011662}
11663
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011664#ifdef FEAT_MBYTE
11665/*
11666 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
11667 * Multi-byte version of spell_soundfold().
11668 */
11669 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011670spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011671 slang_T *slang;
11672 char_u *inword;
11673 char_u *res;
11674{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011675 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011676 int word[MAXWLEN];
11677 int wres[MAXWLEN];
11678 int l;
11679 char_u *s;
11680 int *ws;
11681 char_u *t;
11682 int *pf;
11683 int i, j, z;
11684 int reslen;
11685 int n, k = 0;
11686 int z0;
11687 int k0;
11688 int n0;
11689 int c;
11690 int pri;
11691 int p0 = -333;
11692 int c0;
11693 int did_white = FALSE;
11694
11695 /*
11696 * Convert the multi-byte string to a wide-character string.
11697 * Remove accents, if wanted. We actually remove all non-word characters.
11698 * But keep white space.
11699 */
11700 n = 0;
11701 for (s = inword; *s != NUL; )
11702 {
11703 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011704 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011705 if (slang->sl_rem_accents)
11706 {
11707 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
11708 {
11709 if (did_white)
11710 continue;
11711 c = ' ';
11712 did_white = TRUE;
11713 }
11714 else
11715 {
11716 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011717 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011718 continue;
11719 }
11720 }
11721 word[n++] = c;
11722 }
11723 word[n] = NUL;
11724
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011725 /*
11726 * This comes from Aspell phonet.cpp.
11727 * Converted from C++ to C. Added support for multi-byte chars.
11728 * Changed to keep spaces.
11729 */
11730 i = reslen = z = 0;
11731 while ((c = word[i]) != NUL)
11732 {
11733 /* Start with the first rule that has the character in the word. */
11734 n = slang->sl_sal_first[c & 0xff];
11735 z0 = 0;
11736
11737 if (n >= 0)
11738 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011739 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011740 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
11741 {
11742 /* Quickly skip entries that don't match the word. Most
11743 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011744 if (c != ws[0])
11745 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011746 k = smp[n].sm_leadlen;
11747 if (k > 1)
11748 {
11749 if (word[i + 1] != ws[1])
11750 continue;
11751 if (k > 2)
11752 {
11753 for (j = 2; j < k; ++j)
11754 if (word[i + j] != ws[j])
11755 break;
11756 if (j < k)
11757 continue;
11758 }
11759 }
11760
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011761 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011762 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011763 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011764 while (*pf != NUL && *pf != word[i + k])
11765 ++pf;
11766 if (*pf == NUL)
11767 continue;
11768 ++k;
11769 }
11770 s = smp[n].sm_rules;
11771 pri = 5; /* default priority */
11772
11773 p0 = *s;
11774 k0 = k;
11775 while (*s == '-' && k > 1)
11776 {
11777 k--;
11778 s++;
11779 }
11780 if (*s == '<')
11781 s++;
11782 if (VIM_ISDIGIT(*s))
11783 {
11784 /* determine priority */
11785 pri = *s - '0';
11786 s++;
11787 }
11788 if (*s == '^' && *(s + 1) == '^')
11789 s++;
11790
11791 if (*s == NUL
11792 || (*s == '^'
11793 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011794 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011795 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011796 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011797 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011798 && spell_iswordp_w(word + i - 1, curbuf)
11799 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011800 {
11801 /* search for followup rules, if: */
11802 /* followup and k > 1 and NO '-' in searchstring */
11803 c0 = word[i + k - 1];
11804 n0 = slang->sl_sal_first[c0 & 0xff];
11805
11806 if (slang->sl_followup && k > 1 && n0 >= 0
11807 && p0 != '-' && word[i + k] != NUL)
11808 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011809 /* Test follow-up rule for "word[i + k]"; loop over
11810 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011811 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
11812 == (c0 & 0xff); ++n0)
11813 {
11814 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011815 */
11816 if (c0 != ws[0])
11817 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011818 k0 = smp[n0].sm_leadlen;
11819 if (k0 > 1)
11820 {
11821 if (word[i + k] != ws[1])
11822 continue;
11823 if (k0 > 2)
11824 {
11825 pf = word + i + k + 1;
11826 for (j = 2; j < k0; ++j)
11827 if (*pf++ != ws[j])
11828 break;
11829 if (j < k0)
11830 continue;
11831 }
11832 }
11833 k0 += k - 1;
11834
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011835 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011836 {
11837 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011838 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011839 while (*pf != NUL && *pf != word[i + k0])
11840 ++pf;
11841 if (*pf == NUL)
11842 continue;
11843 ++k0;
11844 }
11845
11846 p0 = 5;
11847 s = smp[n0].sm_rules;
11848 while (*s == '-')
11849 {
11850 /* "k0" gets NOT reduced because
11851 * "if (k0 == k)" */
11852 s++;
11853 }
11854 if (*s == '<')
11855 s++;
11856 if (VIM_ISDIGIT(*s))
11857 {
11858 p0 = *s - '0';
11859 s++;
11860 }
11861
11862 if (*s == NUL
11863 /* *s == '^' cuts */
11864 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011865 && !spell_iswordp_w(word + i + k0,
11866 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011867 {
11868 if (k0 == k)
11869 /* this is just a piece of the string */
11870 continue;
11871
11872 if (p0 < pri)
11873 /* priority too low */
11874 continue;
11875 /* rule fits; stop search */
11876 break;
11877 }
11878 }
11879
11880 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
11881 == (c0 & 0xff))
11882 continue;
11883 }
11884
11885 /* replace string */
11886 ws = smp[n].sm_to_w;
11887 s = smp[n].sm_rules;
11888 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
11889 if (p0 == 1 && z == 0)
11890 {
11891 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011892 if (reslen > 0 && ws != NULL && *ws != NUL
11893 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011894 || wres[reslen - 1] == *ws))
11895 reslen--;
11896 z0 = 1;
11897 z = 1;
11898 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011899 if (ws != NULL)
11900 while (*ws != NUL && word[i + k0] != NUL)
11901 {
11902 word[i + k0] = *ws;
11903 k0++;
11904 ws++;
11905 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011906 if (k > k0)
11907 mch_memmove(word + i + k0, word + i + k,
11908 sizeof(int) * (STRLEN(word + i + k) + 1));
11909
11910 /* new "actual letter" */
11911 c = word[i];
11912 }
11913 else
11914 {
11915 /* no '<' rule used */
11916 i += k - 1;
11917 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011918 if (ws != NULL)
11919 while (*ws != NUL && ws[1] != NUL
11920 && reslen < MAXWLEN)
11921 {
11922 if (reslen == 0 || wres[reslen - 1] != *ws)
11923 wres[reslen++] = *ws;
11924 ws++;
11925 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011926 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000011927 if (ws == NULL)
11928 c = NUL;
11929 else
11930 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000011931 if (strstr((char *)s, "^^") != NULL)
11932 {
11933 if (c != NUL)
11934 wres[reslen++] = c;
11935 mch_memmove(word, word + i + 1,
11936 sizeof(int) * (STRLEN(word + i + 1) + 1));
11937 i = 0;
11938 z0 = 1;
11939 }
11940 }
11941 break;
11942 }
11943 }
11944 }
11945 else if (vim_iswhite(c))
11946 {
11947 c = ' ';
11948 k = 1;
11949 }
11950
11951 if (z0 == 0)
11952 {
11953 if (k && !p0 && reslen < MAXWLEN && c != NUL
11954 && (!slang->sl_collapse || reslen == 0
11955 || wres[reslen - 1] != c))
11956 /* condense only double letters */
11957 wres[reslen++] = c;
11958
11959 i++;
11960 z = 0;
11961 k = 0;
11962 }
11963 }
11964
11965 /* Convert wide characters in "wres" to a multi-byte string in "res". */
11966 l = 0;
11967 for (n = 0; n < reslen; ++n)
11968 {
11969 l += mb_char2bytes(wres[n], res + l);
11970 if (l + MB_MAXBYTES > MAXWLEN)
11971 break;
11972 }
11973 res[l] = NUL;
11974}
11975#endif
11976
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011977/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011978 * Compute a score for two sound-a-like words.
11979 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
11980 * Instead of a generic loop we write out the code. That keeps it fast by
11981 * avoiding checks that will not be possible.
11982 */
11983 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011984soundalike_score(goodstart, badstart)
11985 char_u *goodstart; /* sound-folded good word */
11986 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011987{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011988 char_u *goodsound = goodstart;
11989 char_u *badsound = badstart;
11990 int goodlen;
11991 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011992 int n;
11993 char_u *pl, *ps;
11994 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000011995 int score = 0;
11996
11997 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
11998 * counted so much, vowels halfway the word aren't counted at all. */
11999 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
12000 {
12001 score = SCORE_DEL / 2;
12002 if (*badsound == '*')
12003 ++badsound;
12004 else
12005 ++goodsound;
12006 }
12007
12008 goodlen = STRLEN(goodsound);
12009 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012010
12011 /* Return quickly if the lenghts are too different to be fixed by two
12012 * changes. */
12013 n = goodlen - badlen;
12014 if (n < -2 || n > 2)
12015 return SCORE_MAXMAX;
12016
12017 if (n > 0)
12018 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012019 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012020 ps = badsound;
12021 }
12022 else
12023 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012024 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012025 ps = goodsound;
12026 }
12027
12028 /* Skip over the identical part. */
12029 while (*pl == *ps && *pl != NUL)
12030 {
12031 ++pl;
12032 ++ps;
12033 }
12034
12035 switch (n)
12036 {
12037 case -2:
12038 case 2:
12039 /*
12040 * Must delete two characters from "pl".
12041 */
12042 ++pl; /* first delete */
12043 while (*pl == *ps)
12044 {
12045 ++pl;
12046 ++ps;
12047 }
12048 /* strings must be equal after second delete */
12049 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012050 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012051
12052 /* Failed to compare. */
12053 break;
12054
12055 case -1:
12056 case 1:
12057 /*
12058 * Minimal one delete from "pl" required.
12059 */
12060
12061 /* 1: delete */
12062 pl2 = pl + 1;
12063 ps2 = ps;
12064 while (*pl2 == *ps2)
12065 {
12066 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012067 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012068 ++pl2;
12069 ++ps2;
12070 }
12071
12072 /* 2: delete then swap, then rest must be equal */
12073 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
12074 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012075 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012076
12077 /* 3: delete then substitute, then the rest must be equal */
12078 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012079 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012080
12081 /* 4: first swap then delete */
12082 if (pl[0] == ps[1] && pl[1] == ps[0])
12083 {
12084 pl2 = pl + 2; /* swap, skip two chars */
12085 ps2 = ps + 2;
12086 while (*pl2 == *ps2)
12087 {
12088 ++pl2;
12089 ++ps2;
12090 }
12091 /* delete a char and then strings must be equal */
12092 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012093 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012094 }
12095
12096 /* 5: first substitute then delete */
12097 pl2 = pl + 1; /* substitute, skip one char */
12098 ps2 = ps + 1;
12099 while (*pl2 == *ps2)
12100 {
12101 ++pl2;
12102 ++ps2;
12103 }
12104 /* delete a char and then strings must be equal */
12105 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012106 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012107
12108 /* Failed to compare. */
12109 break;
12110
12111 case 0:
12112 /*
12113 * Lenghts are equal, thus changes must result in same length: An
12114 * insert is only possible in combination with a delete.
12115 * 1: check if for identical strings
12116 */
12117 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012118 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012119
12120 /* 2: swap */
12121 if (pl[0] == ps[1] && pl[1] == ps[0])
12122 {
12123 pl2 = pl + 2; /* swap, skip two chars */
12124 ps2 = ps + 2;
12125 while (*pl2 == *ps2)
12126 {
12127 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012128 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012129 ++pl2;
12130 ++ps2;
12131 }
12132 /* 3: swap and swap again */
12133 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
12134 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012135 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012136
12137 /* 4: swap and substitute */
12138 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012139 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012140 }
12141
12142 /* 5: substitute */
12143 pl2 = pl + 1;
12144 ps2 = ps + 1;
12145 while (*pl2 == *ps2)
12146 {
12147 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012148 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012149 ++pl2;
12150 ++ps2;
12151 }
12152
12153 /* 6: substitute and swap */
12154 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
12155 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012156 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012157
12158 /* 7: substitute and substitute */
12159 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012160 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012161
12162 /* 8: insert then delete */
12163 pl2 = pl;
12164 ps2 = ps + 1;
12165 while (*pl2 == *ps2)
12166 {
12167 ++pl2;
12168 ++ps2;
12169 }
12170 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012171 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012172
12173 /* 9: delete then insert */
12174 pl2 = pl + 1;
12175 ps2 = ps;
12176 while (*pl2 == *ps2)
12177 {
12178 ++pl2;
12179 ++ps2;
12180 }
12181 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012182 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012183
12184 /* Failed to compare. */
12185 break;
12186 }
12187
12188 return SCORE_MAXMAX;
12189}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012190
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012191/*
12192 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012193 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012194 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000012195 * The algorithm is described by Du and Chang, 1992.
12196 * The implementation of the algorithm comes from Aspell editdist.cpp,
12197 * edit_distance(). It has been converted from C++ to C and modified to
12198 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012199 */
12200 static int
12201spell_edit_score(badword, goodword)
12202 char_u *badword;
12203 char_u *goodword;
12204{
12205 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012206 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012207 int j, i;
12208 int t;
12209 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012210 int pbc, pgc;
12211#ifdef FEAT_MBYTE
12212 char_u *p;
12213 int wbadword[MAXWLEN];
12214 int wgoodword[MAXWLEN];
12215
12216 if (has_mbyte)
12217 {
12218 /* Get the characters from the multi-byte strings and put them in an
12219 * int array for easy access. */
12220 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012221 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000012222 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012223 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012224 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000012225 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012226 }
12227 else
12228#endif
12229 {
12230 badlen = STRLEN(badword) + 1;
12231 goodlen = STRLEN(goodword) + 1;
12232 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012233
12234 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
12235#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012236 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
12237 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012238 if (cnt == NULL)
12239 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012240
12241 CNT(0, 0) = 0;
12242 for (j = 1; j <= goodlen; ++j)
12243 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
12244
12245 for (i = 1; i <= badlen; ++i)
12246 {
12247 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
12248 for (j = 1; j <= goodlen; ++j)
12249 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012250#ifdef FEAT_MBYTE
12251 if (has_mbyte)
12252 {
12253 bc = wbadword[i - 1];
12254 gc = wgoodword[j - 1];
12255 }
12256 else
12257#endif
12258 {
12259 bc = badword[i - 1];
12260 gc = goodword[j - 1];
12261 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012262 if (bc == gc)
12263 CNT(i, j) = CNT(i - 1, j - 1);
12264 else
12265 {
12266 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012267 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012268 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
12269 else
12270 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
12271
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012272 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012273 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012274#ifdef FEAT_MBYTE
12275 if (has_mbyte)
12276 {
12277 pbc = wbadword[i - 2];
12278 pgc = wgoodword[j - 2];
12279 }
12280 else
12281#endif
12282 {
12283 pbc = badword[i - 2];
12284 pgc = goodword[j - 2];
12285 }
12286 if (bc == pgc && pbc == gc)
12287 {
12288 t = SCORE_SWAP + CNT(i - 2, j - 2);
12289 if (t < CNT(i, j))
12290 CNT(i, j) = t;
12291 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012292 }
12293 t = SCORE_DEL + CNT(i - 1, j);
12294 if (t < CNT(i, j))
12295 CNT(i, j) = t;
12296 t = SCORE_INS + CNT(i, j - 1);
12297 if (t < CNT(i, j))
12298 CNT(i, j) = t;
12299 }
12300 }
12301 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012302
12303 i = CNT(badlen - 1, goodlen - 1);
12304 vim_free(cnt);
12305 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012306}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000012307
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012308/*
12309 * ":spelldump"
12310 */
12311/*ARGSUSED*/
12312 void
12313ex_spelldump(eap)
12314 exarg_T *eap;
12315{
12316 buf_T *buf = curbuf;
12317 langp_T *lp;
12318 slang_T *slang;
12319 idx_T arridx[MAXWLEN];
12320 int curi[MAXWLEN];
12321 char_u word[MAXWLEN];
12322 int c;
12323 char_u *byts;
12324 idx_T *idxs;
12325 linenr_T lnum = 0;
12326 int round;
12327 int depth;
12328 int n;
12329 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000012330 char_u *region_names = NULL; /* region names being used */
12331 int do_region = TRUE; /* dump region names and numbers */
12332 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012333 int lpi;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012334
Bram Moolenaar95529562005-08-25 21:21:38 +000012335 if (no_spell_checking(curwin))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012336 return;
12337
12338 /* Create a new empty buffer by splitting the window. */
12339 do_cmdline_cmd((char_u *)"new");
12340 if (!bufempty() || !buf_valid(buf))
12341 return;
12342
Bram Moolenaar7887d882005-07-01 22:33:52 +000012343 /* Find out if we can support regions: All languages must support the same
12344 * regions or none at all. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012345 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000012346 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012347 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000012348 p = lp->lp_slang->sl_regions;
12349 if (p[0] != 0)
12350 {
12351 if (region_names == NULL) /* first language with regions */
12352 region_names = p;
12353 else if (STRCMP(region_names, p) != 0)
12354 {
12355 do_region = FALSE; /* region names are different */
12356 break;
12357 }
12358 }
12359 }
12360
12361 if (do_region && region_names != NULL)
12362 {
12363 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
12364 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
12365 }
12366 else
12367 do_region = FALSE;
12368
12369 /*
12370 * Loop over all files loaded for the entries in 'spelllang'.
12371 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012372 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012373 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012374 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012375 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012376 if (slang->sl_fbyts == NULL) /* reloading failed */
12377 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012378
12379 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
12380 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
12381
12382 /* round 1: case-folded tree
12383 * round 2: keep-case tree */
12384 for (round = 1; round <= 2; ++round)
12385 {
12386 if (round == 1)
12387 {
12388 byts = slang->sl_fbyts;
12389 idxs = slang->sl_fidxs;
12390 }
12391 else
12392 {
12393 byts = slang->sl_kbyts;
12394 idxs = slang->sl_kidxs;
12395 }
12396 if (byts == NULL)
12397 continue; /* array is empty */
12398
12399 depth = 0;
12400 arridx[0] = 0;
12401 curi[0] = 1;
12402 while (depth >= 0 && !got_int)
12403 {
12404 if (curi[depth] > byts[arridx[depth]])
12405 {
12406 /* Done all bytes at this node, go up one level. */
12407 --depth;
12408 line_breakcheck();
12409 }
12410 else
12411 {
12412 /* Do one more byte at this node. */
12413 n = arridx[depth] + curi[depth];
12414 ++curi[depth];
12415 c = byts[n];
12416 if (c == 0)
12417 {
12418 /* End of word, deal with the word.
12419 * Don't use keep-case words in the fold-case tree,
12420 * they will appear in the keep-case tree.
12421 * Only use the word when the region matches. */
12422 flags = (int)idxs[n];
12423 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012424 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000012425 && (do_region
12426 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012427 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012428 & lp->lp_region) != 0))
12429 {
12430 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000012431 if (!do_region)
12432 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000012433
12434 /* Dump the basic word if there is no prefix or
12435 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012436 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000012437 if (c == 0 || curi[depth] == 2)
12438 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012439
12440 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000012441 if (c != 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012442 lnum = dump_prefixes(slang, word, round,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012443 flags, lnum);
12444 }
12445 }
12446 else
12447 {
12448 /* Normal char, go one level deeper. */
12449 word[depth++] = c;
12450 arridx[depth] = idxs[n];
12451 curi[depth] = 1;
12452 }
12453 }
12454 }
12455 }
12456 }
12457
12458 /* Delete the empty line that we started with. */
12459 if (curbuf->b_ml.ml_line_count > 1)
12460 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
12461
12462 redraw_later(NOT_VALID);
12463}
12464
12465/*
12466 * Dump one word: apply case modifications and append a line to the buffer.
12467 */
12468 static void
12469dump_word(word, round, flags, lnum)
12470 char_u *word;
12471 int round;
12472 int flags;
12473 linenr_T lnum;
12474{
12475 int keepcap = FALSE;
12476 char_u *p;
12477 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000012478 char_u badword[MAXWLEN + 10];
12479 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012480
12481 if (round == 1 && (flags & WF_CAPMASK) != 0)
12482 {
12483 /* Need to fix case according to "flags". */
12484 make_case_word(word, cword, flags);
12485 p = cword;
12486 }
12487 else
12488 {
12489 p = word;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000012490 if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0
12491 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012492 keepcap = TRUE;
12493 }
12494
Bram Moolenaar7887d882005-07-01 22:33:52 +000012495 /* Add flags and regions after a slash. */
12496 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012497 {
Bram Moolenaar7887d882005-07-01 22:33:52 +000012498 STRCPY(badword, p);
12499 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012500 if (keepcap)
12501 STRCAT(badword, "=");
12502 if (flags & WF_BANNED)
12503 STRCAT(badword, "!");
12504 else if (flags & WF_RARE)
12505 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +000012506 if (flags & WF_REGION)
12507 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012508 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +000012509 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012510 p = badword;
12511 }
12512
12513 ml_append(lnum, p, (colnr_T)0, FALSE);
12514}
12515
12516/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000012517 * For ":spelldump": Find matching prefixes for "word". Prepend each to
12518 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012519 * Return the updated line number.
12520 */
12521 static linenr_T
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012522dump_prefixes(slang, word, round, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012523 slang_T *slang;
12524 char_u *word; /* case-folded word */
12525 int round;
12526 int flags; /* flags with prefix ID */
12527 linenr_T startlnum;
12528{
12529 idx_T arridx[MAXWLEN];
12530 int curi[MAXWLEN];
12531 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000012532 char_u word_up[MAXWLEN];
12533 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012534 int c;
12535 char_u *byts;
12536 idx_T *idxs;
12537 linenr_T lnum = startlnum;
12538 int depth;
12539 int n;
12540 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012541 int i;
12542
Bram Moolenaar53805d12005-08-01 07:08:33 +000012543 /* if the word starts with a lower-case letter make the word with an
12544 * upper-case letter in word_up[]. */
12545 c = PTR2CHAR(word);
12546 if (SPELL_TOUPPER(c) != c)
12547 {
12548 onecap_copy(word, word_up, TRUE);
12549 has_word_up = TRUE;
12550 }
12551
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012552 byts = slang->sl_pbyts;
12553 idxs = slang->sl_pidxs;
12554 if (byts != NULL) /* array not is empty */
12555 {
12556 /*
12557 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012558 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012559 */
12560 depth = 0;
12561 arridx[0] = 0;
12562 curi[0] = 1;
12563 while (depth >= 0 && !got_int)
12564 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012565 n = arridx[depth];
12566 len = byts[n];
12567 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012568 {
12569 /* Done all bytes at this node, go up one level. */
12570 --depth;
12571 line_breakcheck();
12572 }
12573 else
12574 {
12575 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000012576 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012577 ++curi[depth];
12578 c = byts[n];
12579 if (c == 0)
12580 {
12581 /* End of prefix, find out how many IDs there are. */
12582 for (i = 1; i < len; ++i)
12583 if (byts[n + i] != 0)
12584 break;
12585 curi[depth] += i - 1;
12586
Bram Moolenaar53805d12005-08-01 07:08:33 +000012587 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
12588 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012589 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000012590 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000012591 dump_word(prefix, round,
Bram Moolenaar53805d12005-08-01 07:08:33 +000012592 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000012593 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012594 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000012595
12596 /* Check for prefix that matches the word when the
12597 * first letter is upper-case, but only if the prefix has
12598 * a condition. */
12599 if (has_word_up)
12600 {
12601 c = valid_word_prefix(i, n, flags, word_up, slang,
12602 TRUE);
12603 if (c != 0)
12604 {
12605 vim_strncpy(prefix + depth, word_up,
12606 MAXWLEN - depth - 1);
12607 dump_word(prefix, round,
12608 (c & WF_RAREPFX) ? (flags | WF_RARE)
12609 : flags, lnum++);
12610 }
12611 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012612 }
12613 else
12614 {
12615 /* Normal char, go one level deeper. */
12616 prefix[depth++] = c;
12617 arridx[depth] = idxs[n];
12618 curi[depth] = 1;
12619 }
12620 }
12621 }
12622 }
12623
12624 return lnum;
12625}
12626
Bram Moolenaar95529562005-08-25 21:21:38 +000012627/*
12628 * Move "p" to end of word.
12629 */
12630 char_u *
12631spell_to_word_end(start, buf)
12632 char_u *start;
12633 buf_T *buf;
12634{
12635 char_u *p = start;
12636
12637 while (*p != NUL && spell_iswordp(p, buf))
12638 mb_ptr_adv(p);
12639 return p;
12640}
12641
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012642#if defined(FEAT_INS_EXPAND) || defined(PROTO)
12643static int spell_expand_need_cap;
12644
12645/*
12646 * Find start of the word in front of the cursor. We don't check if it is
12647 * badly spelled, with completion we can only change the word in front of the
12648 * cursor.
12649 * Used for Insert mode completion CTRL-X ?.
12650 * Returns the column number of the word.
12651 */
12652 int
12653spell_word_start(startcol)
12654 int startcol;
12655{
12656 char_u *line;
12657 char_u *p;
12658 int col = 0;
12659
Bram Moolenaar95529562005-08-25 21:21:38 +000012660 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000012661 return startcol;
12662
12663 /* Find a word character before "startcol". */
12664 line = ml_get_curline();
12665 for (p = line + startcol; p > line; )
12666 {
12667 mb_ptr_back(line, p);
12668 if (spell_iswordp_nmw(p))
12669 break;
12670 }
12671
12672 /* Go back to start of the word. */
12673 while (p > line)
12674 {
12675 col = p - line;
12676 mb_ptr_back(line, p);
12677 if (!spell_iswordp(p, curbuf))
12678 break;
12679 col = 0;
12680 }
12681
12682 /* Need to check for 'spellcapcheck' now, the word is removed before
12683 * expand_spelling() is called. Therefore the ugly global variable. */
12684 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
12685
12686 return col;
12687}
12688
12689/*
12690 * Get list of spelling suggestions.
12691 * Used for Insert mode completion CTRL-X ?.
12692 * Returns the number of matches. The matches are in "matchp[]", array of
12693 * allocated strings.
12694 */
12695/*ARGSUSED*/
12696 int
12697expand_spelling(lnum, col, pat, matchp)
12698 linenr_T lnum;
12699 int col;
12700 char_u *pat;
12701 char_u ***matchp;
12702{
12703 garray_T ga;
12704
12705 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap);
12706 *matchp = ga.ga_data;
12707 return ga.ga_len;
12708}
12709#endif
12710
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000012711#endif /* FEAT_SYN_HL */