blob: 9c3742f7a72b68a4791452ba16270d0136b49be6 [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 Moolenaar1d73c882005-06-19 22:48:47 +000038 * There is one additional tree for when prefixes are not applied when
39 * 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.
54 * Only use for small word lists!
55 * SPELL_COMPRESS_CNT is in how many words we compress the tree. */
56#if 0
57# define SPELL_PRINTTREE
58# define SPELL_COMPRESS_CNT 1
59#else
60# define SPELL_COMPRESS_CNT 1000000
61#endif
62
Bram Moolenaar51485f02005-06-04 21:55:20 +000063/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000064 * Use this to adjust the score after finding suggestions, based on the
65 * suggested word sounding like the bad word. This is much faster than doing
66 * it for every possible suggestion.
67 * Disadvantage: When "the" is typed as "hte" it sounds different and goes
68 * down in the list.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000069 * Used when 'spellsuggest' is set to "best".
70 */
71#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
72
73/*
74 * The double scoring mechanism is based on the principle that there are two
75 * kinds of spelling mistakes:
76 * 1. You know how to spell the word, but mistype something. This results in
77 * a small editing distance (character swapped/omitted/inserted) and
78 * possibly a word that sounds completely different.
79 * 2. You don't know how to spell the word and type something that sounds
80 * right. The edit distance can be big but the word is similar after
81 * sound-folding.
82 * Since scores for these two mistakes will be very different we use a list
83 * for each.
84 * The sound-folding is slow, only do double scoring when 'spellsuggest' is
85 * "double".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000086 */
87
88/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000089 * Vim spell file format: <HEADER>
90 * <SUGGEST>
91 * <LWORDTREE>
92 * <KWORDTREE>
93 * <PREFIXTREE>
Bram Moolenaar51485f02005-06-04 21:55:20 +000094 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000095 * <HEADER>: <fileID>
96 * <regioncnt> <regionname> ...
97 * <charflagslen> <charflags>
98 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000099 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000100 * <prefcondcnt> <prefcond> ...
Bram Moolenaar51485f02005-06-04 21:55:20 +0000101 *
Bram Moolenaar53805d12005-08-01 07:08:33 +0000102 * <fileID> 10 bytes "VIMspell09"
Bram Moolenaar51485f02005-06-04 21:55:20 +0000103 * <regioncnt> 1 byte number of regions following (8 supported)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000104 * <regionname> 2 bytes Region name: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000105 * First <regionname> is region 1.
106 *
107 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
108 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000109 * 0x01 word character CF_WORD
110 * 0x02 upper-case character CF_UPPER
Bram Moolenaar51485f02005-06-04 21:55:20 +0000111 * <fcharslen> 2 bytes Number of bytes in <fchars>.
112 * <fchars> N bytes Folded characters, first one is for character 128.
113 *
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000114 * <midwordlen> 2 bytes Number of bytes in <midword>.
115 * <midword> N bytes Characters that are word characters only when used
116 * in the middle of a word.
117 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000118 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
119 *
120 * <prefcond> : <condlen> <condstr>
121 *
122 * <condlen> 1 byte Length of <condstr>.
123 *
124 * <condstr> N bytes Condition for the prefix.
125 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000126 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000127 * <SUGGEST> : <repcount> <rep> ...
128 * <salflags> <salcount> <sal> ...
129 * <maplen> <mapstr>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000130 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000131 * <repcount> 2 bytes number of <rep> items, MSB first.
132 *
133 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
134 *
135 * <repfromlen> 1 byte length of <repfrom>
136 *
137 * <repfrom> N bytes "from" part of replacement
138 *
139 * <reptolen> 1 byte length of <repto>
140 *
141 * <repto> N bytes "to" part of replacement
142 *
143 * <salflags> 1 byte flags for soundsalike conversion:
144 * SAL_F0LLOWUP
145 * SAL_COLLAPSE
146 * SAL_REM_ACCENTS
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000147 * SAL_SOFO: SOFOFROM and SOFOTO used instead of SAL
148 *
149 * <salcount> 2 bytes number of <sal> items following
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000150 *
151 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
152 *
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000153 * <salfromlen> 1-2 bytes length of <salfrom> (2 bytes for SAL_SOFO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000154 *
155 * <salfrom> N bytes "from" part of soundsalike
156 *
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000157 * <saltolen> 1-2 bytes length of <salto> (2 bytes for SAL_SOFO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000158 *
159 * <salto> N bytes "to" part of soundsalike
160 *
161 * <maplen> 2 bytes length of <mapstr>, MSB first
162 *
163 * <mapstr> N bytes String with sequences of similar characters,
164 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000165 *
166 *
167 * <LWORDTREE>: <wordtree>
168 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000169 * <KWORDTREE>: <wordtree>
170 *
171 * <PREFIXTREE>: <wordtree>
172 *
173 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000174 * <wordtree>: <nodecount> <nodedata> ...
175 *
176 * <nodecount> 4 bytes Number of nodes following. MSB first.
177 *
178 * <nodedata>: <siblingcount> <sibling> ...
179 *
180 * <siblingcount> 1 byte Number of siblings in this node. The siblings
181 * follow in sorted order.
182 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000183 * <sibling>: <byte> [ <nodeidx> <xbyte>
Bram Moolenaar53805d12005-08-01 07:08:33 +0000184 * | <flags> [<flags2>] [<region>] [<prefixID>]
185 * | [<pflags>] <prefixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000186 *
187 * <byte> 1 byte Byte value of the sibling. Special cases:
188 * BY_NOFLAGS: End of word without flags and for all
189 * regions.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000190 * For PREFIXTREE <prefixID> and
191 * <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000192 * BY_FLAGS: End of word, <flags> follow.
Bram Moolenaar53805d12005-08-01 07:08:33 +0000193 * For PREFIXTREE <pflags>, <prefixID>
194 * and <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000195 * BY_FLAGS2: End of word, <flags> and <flags2>
196 * follow. Not used in PREFIXTREE.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000197 * BY_INDEX: Child of sibling is shared, <nodeidx>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000198 * and <xbyte> follow.
199 *
200 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
201 *
202 * <xbyte> 1 byte byte value of the sibling.
203 *
204 * <flags> 1 byte bitmask of:
205 * WF_ALLCAP word must have only capitals
206 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000207 * WF_KEEPCAP keep-case word
208 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000209 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000210 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000211 * WF_REGION <region> follows
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000212 * WF_PFX <prefixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000213 *
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000214 * <flags2> 1 byte Only used when there are postponed prefixes.
215 * Bitmask of:
216 * WF_HAS_AFF >> 8 word includes affix
217 *
Bram Moolenaar53805d12005-08-01 07:08:33 +0000218 * <pflags> 1 byte bitmask of:
219 * WFP_RARE rare prefix
220 * WFP_NC non-combining prefix
221 * WFP_UP letter after prefix made upper case
222 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000223 * <region> 1 byte Bitmask for regions in which word is valid. When
224 * omitted it's valid in all regions.
225 * Lowest bit is for region 1.
226 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000227 * <prefixID> 1 byte ID of prefix that can be used with this word. For
228 * PREFIXTREE used for the required prefix ID.
229 *
230 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
231 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000232 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000233 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000234 */
235
Bram Moolenaare19defe2005-03-21 08:23:33 +0000236#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
237# include <io.h> /* for lseek(), must be before vim.h */
238#endif
239
240#include "vim.h"
241
242#if defined(FEAT_SYN_HL) || defined(PROTO)
243
244#ifdef HAVE_FCNTL_H
245# include <fcntl.h>
246#endif
247
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000248#define MAXWLEN 250 /* Assume max. word len is this many bytes.
249 Some places assume a word length fits in a
250 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000251
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000252/* Type used for indexes in the word tree need to be at least 3 bytes. If int
253 * is 8 bytes we could use something smaller, but what? */
254#if SIZEOF_INT > 2
255typedef int idx_T;
256#else
257typedef long idx_T;
258#endif
259
260/* Flags used for a word. Only the lowest byte can be used, the region byte
261 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000262#define WF_REGION 0x01 /* region byte follows */
263#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
264#define WF_ALLCAP 0x04 /* word must be all capitals */
265#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000266#define WF_BANNED 0x10 /* bad word */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000267#define WF_PFX 0x20 /* prefix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000268#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000269#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000270
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000271/* for <flags2>, shifted up one byte to be used in wn_flags */
272#define WF_HAS_AFF 0x0100 /* word includes affix */
273
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000274#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000275
Bram Moolenaar53805d12005-08-01 07:08:33 +0000276/* flags for <pflags> */
277#define WFP_RARE 0x01 /* rare prefix */
278#define WFP_NC 0x02 /* prefix is not combining */
279#define WFP_UP 0x04 /* to-upper prefix */
280
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000281/* flags for postponed prefixes. Must be above prefixID (one byte)
282 * and prefcondnr (two bytes). */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000283#define WF_RAREPFX (WFP_RARE << 24) /* in sl_pidxs: flag for rare
284 * postponed prefix */
285#define WF_PFX_NC (WFP_NC << 24) /* in sl_pidxs: flag for non-combining
286 * postponed prefix */
287#define WF_PFX_UP (WFP_UP << 24) /* in sl_pidxs: flag for to-upper
288 * postponed prefix */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000289
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000290/* Special byte values for <byte>. Some are only used in the tree for
291 * postponed prefixes, some only in the other trees. This is a bit messy... */
292#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000293 * postponed prefix: no <pflags> */
294#define BY_INDEX 1 /* child is shared, index follows */
295#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
296 * postponed prefix: <pflags> follows */
297#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
298 * follow; never used in prefix tree */
299#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000300
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000301/* Info from "REP" and "SAL" entries in ".aff" file used in si_rep, sl_rep,
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000302 * and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000303 * One replacement: from "ft_from" to "ft_to". */
304typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000305{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000306 char_u *ft_from;
307 char_u *ft_to;
308} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000309
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000310/* Info from "SAL" entries in ".aff" file used in sl_sal.
311 * The info is split for quick processing by spell_soundfold().
312 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
313typedef struct salitem_S
314{
315 char_u *sm_lead; /* leading letters */
316 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000317 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000318 char_u *sm_rules; /* rules like ^, $, priority */
319 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000320#ifdef FEAT_MBYTE
321 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000322 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000323 int *sm_to_w; /* wide character copy of "sm_to" */
324#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000325} salitem_T;
326
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000327#ifdef FEAT_MBYTE
328typedef int salfirst_T;
329#else
330typedef short salfirst_T;
331#endif
332
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000333/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000334 * Structure used to store words and other info for one language, loaded from
335 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000336 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
337 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
338 *
339 * The "byts" array stores the possible bytes in each tree node, preceded by
340 * the number of possible bytes, sorted on byte value:
341 * <len> <byte1> <byte2> ...
342 * The "idxs" array stores the index of the child node corresponding to the
343 * byte in "byts".
344 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000345 * the flags, region mask and prefixID for the word. There may be several
346 * zeros in sequence for alternative flag/region combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000347 */
348typedef struct slang_S slang_T;
349struct slang_S
350{
351 slang_T *sl_next; /* next language */
352 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000353 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000354 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000355
Bram Moolenaar51485f02005-06-04 21:55:20 +0000356 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000357 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000358 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000359 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000360 char_u *sl_pbyts; /* prefix tree word bytes */
361 idx_T *sl_pidxs; /* prefix tree word indexes */
362
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000363 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000364
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000365 char_u *sl_midword; /* MIDWORD string or NULL */
366
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000367 int sl_prefixcnt; /* number of items in "sl_prefprog" */
368 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
369
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000370 garray_T sl_rep; /* list of fromto_T entries from REP lines */
371 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
372 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000373 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000374 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000375 there is none */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000376 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
377 * "sl_sal_first" maps chars, when has_mbyte
378 * "sl_sal" is a list of wide char lists. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000379 int sl_followup; /* SAL followup */
380 int sl_collapse; /* SAL collapse_result */
381 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaarea424162005-06-16 21:51:00 +0000382 int sl_has_map; /* TRUE if there is a MAP line */
383#ifdef FEAT_MBYTE
384 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
385 int sl_map_array[256]; /* MAP for first 256 chars */
386#else
387 char_u sl_map_array[256]; /* MAP for first 256 chars */
388#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000389};
390
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000391/* First language that is loaded, start of the linked list of loaded
392 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000393static slang_T *first_lang = NULL;
394
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000395/* Flags used in .spl file for soundsalike flags. */
396#define SAL_F0LLOWUP 1
397#define SAL_COLLAPSE 2
398#define SAL_REM_ACCENTS 4
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000399#define SAL_SOFO 8 /* SOFOFROM and SOFOTO instead of SAL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000400
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000401/*
402 * Structure used in "b_langp", filled from 'spelllang'.
403 */
404typedef struct langp_S
405{
406 slang_T *lp_slang; /* info for this language (NULL for last one) */
407 int lp_region; /* bitmask for region or REGION_ALL */
408} langp_T;
409
410#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
411
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000412#define REGION_ALL 0xff /* word valid in all regions */
413
414/* Result values. Lower number is accepted over higher one. */
415#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000416#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000417#define SP_RARE 1
418#define SP_LOCAL 2
419#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000420
Bram Moolenaar53805d12005-08-01 07:08:33 +0000421#define VIMSPELLMAGIC "VIMspell09" /* string at start of Vim spell file */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000422#define VIMSPELLMAGICL 10
423
Bram Moolenaar7887d882005-07-01 22:33:52 +0000424/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000425static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000426
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000427/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000428 * Information used when looking for suggestions.
429 */
430typedef struct suginfo_S
431{
432 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000433 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000434 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000435 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000436 char_u *su_badptr; /* start of bad word in line */
437 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000438 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000439 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
440 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
441 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000442} suginfo_T;
443
444/* One word suggestion. Used in "si_ga". */
445typedef struct suggest_S
446{
447 char_u *st_word; /* suggested word, allocated string */
448 int st_orglen; /* length of replaced text */
449 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000450 int st_altscore; /* used when st_score compares equal */
451 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000452 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000453} suggest_T;
454
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000455#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000456
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000457/* Number of suggestions kept when cleaning up. When rescore_suggestions() is
458 * called the score may change, thus we need to keep more than what is
459 * displayed. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000460#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 50 ? 50 : (su)->su_maxcount)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000461
462/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
463 * of suggestions that are not going to be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000464#define SUG_MAX_COUNT(su) ((su)->su_maxcount + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000465
466/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000467#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000468#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000469#define SCORE_REGION 70 /* word is for different region */
470#define SCORE_RARE 180 /* rare word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000471#define SCORE_SWAP 90 /* swap two characters */
472#define SCORE_SWAP3 110 /* swap two characters in three */
473#define SCORE_REP 87 /* REP replacement */
474#define SCORE_SUBST 93 /* substitute a character */
475#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000476#define SCORE_DEL 94 /* delete a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000477#define SCORE_DELDUP 64 /* delete a duplicated character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000478#define SCORE_INS 96 /* insert a character */
Bram Moolenaarea408852005-06-25 22:49:46 +0000479#define SCORE_INSDUP 66 /* insert a duplicate character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000480#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000481
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000482#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000483#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
484 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000485
486#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000487#define SCORE_MAXMAX 999999 /* accept any score */
488
489/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000490 * Structure to store info for word matching.
491 */
492typedef struct matchinf_S
493{
494 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000495
496 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000497 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000498 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000499 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000500 char_u *mi_cend; /* char after what was used for
501 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000502
503 /* case-folded text */
504 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000505 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000506
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000507 /* for when checking word after a prefix */
508 int mi_prefarridx; /* index in sl_pidxs with list of
509 prefixID/condition */
510 int mi_prefcnt; /* number of entries at mi_prefarridx */
511 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000512#ifdef FEAT_MBYTE
513 int mi_cprefixlen; /* byte length of prefix in original
514 case */
515#else
516# define mi_cprefixlen mi_prefixlen /* it's the same value */
517#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000518
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000519 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000520 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000521 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000522 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000523} matchinf_T;
524
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000525/*
526 * The tables used for recognizing word characters according to spelling.
527 * These are only used for the first 256 characters of 'encoding'.
528 */
529typedef struct spelltab_S
530{
531 char_u st_isw[256]; /* flags: is word char */
532 char_u st_isu[256]; /* flags: is uppercase char */
533 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000534 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000535} spelltab_T;
536
537static spelltab_T spelltab;
538static int did_set_spelltab;
539
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000540#define CF_WORD 0x01
541#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000542
543static void clear_spell_chartab __ARGS((spelltab_T *sp));
544static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000545static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
546static int spell_iswordp_nmw __ARGS((char_u *p));
547#ifdef FEAT_MBYTE
548static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
549#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000550static void write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000551
552/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000553 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000554 */
555typedef enum
556{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000557 STATE_START = 0, /* At start of node check for NUL bytes (goodword
558 * ends); if badword ends there is a match, otherwise
559 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000560 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000561 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000562 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
563 STATE_PLAIN, /* Use each byte of the node. */
564 STATE_DEL, /* Delete a byte from the bad word. */
565 STATE_INS, /* Insert a byte in the bad word. */
566 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000567 STATE_UNSWAP, /* Undo swap two characters. */
568 STATE_SWAP3, /* Swap two characters over three. */
569 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000570 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000571 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000572 STATE_REP_INI, /* Prepare for using REP items. */
573 STATE_REP, /* Use matching REP items from the .aff file. */
574 STATE_REP_UNDO, /* Undo a REP item replacement. */
575 STATE_FINAL /* End of this node. */
576} state_T;
577
578/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000579 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000580 */
581typedef struct trystate_S
582{
Bram Moolenaarea424162005-06-16 21:51:00 +0000583 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000584 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000585 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000586 short ts_curi; /* index in list of child nodes */
587 char_u ts_fidx; /* index in fword[], case-folded bad word */
588 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
589 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000590 char_u ts_prefixdepth; /* stack depth for end of prefix or PREFIXTREE
591 * or NOPREFIX */
Bram Moolenaarea424162005-06-16 21:51:00 +0000592#ifdef FEAT_MBYTE
593 char_u ts_tcharlen; /* number of bytes in tword character */
594 char_u ts_tcharidx; /* current byte index in tword character */
595 char_u ts_isdiff; /* DIFF_ values */
596 char_u ts_fcharstart; /* index in fword where badword char started */
597#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000598 char_u ts_save_prewordlen; /* saved "prewordlen" */
Bram Moolenaarea424162005-06-16 21:51:00 +0000599 char_u ts_save_splitoff; /* su_splitoff saved here */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000600 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000601} trystate_T;
602
Bram Moolenaarea424162005-06-16 21:51:00 +0000603/* values for ts_isdiff */
604#define DIFF_NONE 0 /* no different byte (yet) */
605#define DIFF_YES 1 /* different byte found */
606#define DIFF_INSERT 2 /* inserting character */
607
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000608/* special values ts_prefixdepth */
609#define PREFIXTREE 0xfe /* walking through the prefix tree */
610#define NOPREFIX 0xff /* not using prefixes */
611
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000612/* mode values for find_word */
613#define FIND_FOLDWORD 0 /* find word case-folded */
614#define FIND_KEEPWORD 1 /* find keep-case word */
615#define FIND_PREFIX 2 /* find word after prefix */
616
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000617static slang_T *slang_alloc __ARGS((char_u *lang));
618static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000619static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000620static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000621static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000622static void find_prefix __ARGS((matchinf_T *mip));
623static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000624static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000625static int no_spell_checking __ARGS((void));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000626static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000627static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000628static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000629static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000630static 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 +0000631static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000632static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
633static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000634#ifdef FEAT_MBYTE
635static int *mb_str2wide __ARGS((char_u *s));
636#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000637static 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 +0000638static void clear_midword __ARGS((buf_T *buf));
639static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000640static int find_region __ARGS((char_u *rp, char_u *region));
641static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000642static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000643static int set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000644static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
645static void write_spell_chartab __ARGS((FILE *fd));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000646static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +0000647static 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 +0000648#ifdef FEAT_EVAL
649static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
650#endif
651static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
652static void spell_suggest_intern __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000653static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000654static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000655static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000656static void suggest_try_special __ARGS((suginfo_T *su));
657static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000658static int try_deeper __ARGS((suginfo_T *su, trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000659#ifdef FEAT_MBYTE
660static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
661#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000662static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000663static void score_comp_sal __ARGS((suginfo_T *su));
664static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000665static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000666static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000667static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000668static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000669static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000670static 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 +0000671static void add_banned __ARGS((suginfo_T *su, char_u *word));
672static int was_banned __ARGS((suginfo_T *su, char_u *word));
673static void free_banned __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000674static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000675static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000676static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
677static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
678static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000679#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000680static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000681#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000682static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000683static int spell_edit_score __ARGS((char_u *badword, char_u *goodword));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000684static void dump_word __ARGS((char_u *word, int round, int flags, linenr_T lnum));
685static linenr_T apply_prefixes __ARGS((slang_T *slang, char_u *word, int round, int flags, linenr_T startlnum));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000686
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000687/*
688 * Use our own character-case definitions, because the current locale may
689 * differ from what the .spl file uses.
690 * These must not be called with negative number!
691 */
692#ifndef FEAT_MBYTE
693/* Non-multi-byte implementation. */
694# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
695# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
696# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
697#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000698# if defined(HAVE_WCHAR_H)
699# include <wchar.h> /* for towupper() and towlower() */
700# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000701/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
702 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
703 * the "w" library function for characters above 255 if available. */
704# ifdef HAVE_TOWLOWER
705# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
706 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
707# else
708# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
709 : (c) < 256 ? spelltab.st_fold[c] : (c))
710# endif
711
712# ifdef HAVE_TOWUPPER
713# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
714 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
715# else
716# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
717 : (c) < 256 ? spelltab.st_upper[c] : (c))
718# endif
719
720# ifdef HAVE_ISWUPPER
721# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
722 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
723# else
724# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
725 : (c) < 256 ? spelltab.st_isu[c] : (c))
726# endif
727#endif
728
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000729
730static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000731static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000732static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000733
734/*
735 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000736 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000737 * "*attrp" is set to the attributes for a badly spelled word. For a non-word
738 * or when it's OK it remains unchanged.
739 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000740 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000741 * "capcol" is used to check for a Capitalised word after the end of a
742 * sentence. If it's zero then perform the check. Return the column where to
743 * check next, or -1 when no sentence end was found. If it's NULL then don't
744 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000745 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000746 * Returns the length of the word in bytes, also when it's OK, so that the
747 * caller can skip over the word.
748 */
749 int
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000750spell_check(wp, ptr, attrp, capcol)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000751 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000752 char_u *ptr;
753 int *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000754 int *capcol; /* column to check for Capital */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000755{
756 matchinf_T mi; /* Most things are put in "mi" so that it can
757 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000758 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000759 int c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000760
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000761 /* A word never starts at a space or a control character. Return quickly
762 * then, skipping over the character. */
763 if (*ptr <= ' ')
764 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000765
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000766 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar0c405862005-06-22 22:26:26 +0000767 * 0X99FF. But when a word character follows do check spelling to find
768 * "3GPP". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000769 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +0000770 {
Bram Moolenaar3982c542005-06-08 21:56:31 +0000771 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
772 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000773 else
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000774 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000775 mi.mi_end = skipdigits(ptr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000776 nrlen = mi.mi_end - ptr;
777 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000778 if (!spell_iswordp(mi.mi_end, wp->w_buffer))
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000779 return (int)(mi.mi_end - ptr);
Bram Moolenaar0c405862005-06-22 22:26:26 +0000780
781 /* Try including the digits in the word. */
782 mi.mi_fend = ptr + nrlen;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000783 }
Bram Moolenaar0c405862005-06-22 22:26:26 +0000784 else
785 mi.mi_fend = ptr;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000786
Bram Moolenaar0c405862005-06-22 22:26:26 +0000787 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000788 mi.mi_word = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000789 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +0000790 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000791 do
Bram Moolenaar51485f02005-06-04 21:55:20 +0000792 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000793 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000794 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000795
796 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
797 {
798 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000799 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000800 if (!SPELL_ISUPPER(c))
801 {
802 *attrp = highlight_attr[HLF_SPC];
803 return (int)(mi.mi_fend - ptr);
804 }
805 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000806 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000807 if (capcol != NULL)
808 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000809
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000810 /* We always use the characters up to the next non-word character,
811 * also for bad words. */
812 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000813
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000814 /* Check caps type later. */
815 mi.mi_capflags = 0;
816 mi.mi_cend = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000817 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000818
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000819 /* Include one non-word character so that we can check for the
820 * word end. */
821 if (*mi.mi_fend != NUL)
822 mb_ptr_adv(mi.mi_fend);
823
824 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
825 MAXWLEN + 1);
826 mi.mi_fwordlen = STRLEN(mi.mi_fword);
827
828 /* The word is bad unless we recognize it. */
829 mi.mi_result = SP_BAD;
830
831 /*
832 * Loop over the languages specified in 'spelllang'.
833 * We check them all, because a matching word may be longer than an
834 * already found matching word.
835 */
836 for (mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
837 mi.mi_lp->lp_slang != NULL; ++mi.mi_lp)
838 {
839 /* Check for a matching word in case-folded words. */
840 find_word(&mi, FIND_FOLDWORD);
841
842 /* Check for a matching word in keep-case words. */
843 find_word(&mi, FIND_KEEPWORD);
844
845 /* Check for matching prefixes. */
846 find_prefix(&mi);
847 }
848
849 if (mi.mi_result != SP_OK)
850 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000851 /* If we found a number skip over it. Allows for "42nd". Do flag
852 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000853 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +0000854 {
855 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
856 return nrlen;
857 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000858
859 /* When we are at a non-word character there is no error, just
860 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000861 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000862 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000863 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
864 {
865 regmatch_T regmatch;
866
867 /* Check for end of sentence. */
868 regmatch.regprog = wp->w_buffer->b_cap_prog;
869 regmatch.rm_ic = FALSE;
870 if (vim_regexec(&regmatch, ptr, 0))
871 *capcol = (int)(regmatch.endp[0] - ptr);
872 }
873
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000874#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000875 if (has_mbyte)
876 return mb_ptr2len_check(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000877#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000878 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000879 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000880
881 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
882 *attrp = highlight_attr[HLF_SPB];
883 else if (mi.mi_result == SP_RARE)
884 *attrp = highlight_attr[HLF_SPR];
885 else
886 *attrp = highlight_attr[HLF_SPL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000887 }
888
Bram Moolenaar51485f02005-06-04 21:55:20 +0000889 return (int)(mi.mi_end - ptr);
890}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000891
Bram Moolenaar51485f02005-06-04 21:55:20 +0000892/*
893 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000894 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
895 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
896 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
897 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000898 *
899 * For a match mip->mi_result is updated.
900 */
901 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000902find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000903 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000904 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000905{
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000906 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000907 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000908 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000909 int endidxcnt = 0;
910 int len;
911 int wlen = 0;
912 int flen;
913 int c;
914 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000915 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000916#ifdef FEAT_MBYTE
917 char_u *s;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000918 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000919#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000920 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000921 slang_T *slang = mip->mi_lp->lp_slang;
922 unsigned flags;
923 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000924 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +0000925
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000926 if (mode == FIND_KEEPWORD)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000927 {
Bram Moolenaar51485f02005-06-04 21:55:20 +0000928 /* Check for word with matching case in keep-case tree. */
929 ptr = mip->mi_word;
930 flen = 9999; /* no case folding, always enough bytes */
931 byts = slang->sl_kbyts;
932 idxs = slang->sl_kidxs;
933 }
934 else
935 {
936 /* Check for case-folded in case-folded tree. */
937 ptr = mip->mi_fword;
938 flen = mip->mi_fwordlen; /* available case-folded bytes */
939 byts = slang->sl_fbyts;
940 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000941
942 if (mode == FIND_PREFIX)
943 {
944 /* Skip over the prefix. */
945 wlen = mip->mi_prefixlen;
946 flen -= mip->mi_prefixlen;
947 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000948 }
949
Bram Moolenaar51485f02005-06-04 21:55:20 +0000950 if (byts == NULL)
951 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000952
Bram Moolenaar51485f02005-06-04 21:55:20 +0000953 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000954 * Repeat advancing in the tree until:
955 * - there is a byte that doesn't match,
956 * - we reach the end of the tree,
957 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000958 */
959 for (;;)
960 {
Bram Moolenaar0c405862005-06-22 22:26:26 +0000961 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000962 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +0000963
964 len = byts[arridx++];
965
966 /* If the first possible byte is a zero the word could end here.
967 * Remember this index, we first check for the longest word. */
968 if (byts[arridx] == 0)
969 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000970 if (endidxcnt == MAXWLEN)
971 {
972 /* Must be a corrupted spell file. */
973 EMSG(_(e_format));
974 return;
975 }
Bram Moolenaar51485f02005-06-04 21:55:20 +0000976 endlen[endidxcnt] = wlen;
977 endidx[endidxcnt++] = arridx++;
978 --len;
979
980 /* Skip over the zeros, there can be several flag/region
981 * combinations. */
982 while (len > 0 && byts[arridx] == 0)
983 {
984 ++arridx;
985 --len;
986 }
987 if (len == 0)
988 break; /* no children, word must end here */
989 }
990
991 /* Stop looking at end of the line. */
992 if (ptr[wlen] == NUL)
993 break;
994
995 /* Perform a binary search in the list of accepted bytes. */
996 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +0000997 if (c == TAB) /* <Tab> is handled like <Space> */
998 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +0000999 lo = arridx;
1000 hi = arridx + len - 1;
1001 while (lo < hi)
1002 {
1003 m = (lo + hi) / 2;
1004 if (byts[m] > c)
1005 hi = m - 1;
1006 else if (byts[m] < c)
1007 lo = m + 1;
1008 else
1009 {
1010 lo = hi = m;
1011 break;
1012 }
1013 }
1014
1015 /* Stop if there is no matching byte. */
1016 if (hi < lo || byts[lo] != c)
1017 break;
1018
1019 /* Continue at the child (if there is one). */
1020 arridx = idxs[lo];
1021 ++wlen;
1022 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001023
1024 /* One space in the good word may stand for several spaces in the
1025 * checked word. */
1026 if (c == ' ')
1027 {
1028 for (;;)
1029 {
1030 if (flen <= 0 && *mip->mi_fend != NUL)
1031 flen = fold_more(mip);
1032 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1033 break;
1034 ++wlen;
1035 --flen;
1036 }
1037 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001038 }
1039
1040 /*
1041 * Verify that one of the possible endings is valid. Try the longest
1042 * first.
1043 */
1044 while (endidxcnt > 0)
1045 {
1046 --endidxcnt;
1047 arridx = endidx[endidxcnt];
1048 wlen = endlen[endidxcnt];
1049
1050#ifdef FEAT_MBYTE
1051 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1052 continue; /* not at first byte of character */
1053#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001054 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001055 continue; /* next char is a word character */
1056
1057#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001058 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001059 {
1060 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001061 * when folding case. This can be slow, take a shortcut when the
1062 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001063 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001064 if (STRNCMP(ptr, p, wlen) != 0)
1065 {
1066 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1067 mb_ptr_adv(p);
1068 wlen = p - mip->mi_word;
1069 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001070 }
1071#endif
1072
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001073 /* Check flags and region. For FIND_PREFIX check the condition and
1074 * prefix ID.
1075 * Repeat this if there are more flags/region alternatives until there
1076 * is a match. */
1077 res = SP_BAD;
1078 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1079 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001080 {
1081 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001082
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001083 /* For the fold-case tree check that the case of the checked word
1084 * matches with what the word in the tree requires.
1085 * For keep-case tree the case is always right. For prefixes we
1086 * don't bother to check. */
1087 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001088 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001089 if (mip->mi_cend != mip->mi_word + wlen)
1090 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001091 /* mi_capflags was set for a different word length, need
1092 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001093 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001094 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001095 }
1096
Bram Moolenaar0c405862005-06-22 22:26:26 +00001097 if (mip->mi_capflags == WF_KEEPCAP
1098 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001099 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001100 }
1101
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001102 /* When mode is FIND_PREFIX the word must support the prefix:
1103 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001104 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001105 if (mode == FIND_PREFIX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001106 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001107 /* The prefix ID is stored two bytes above the flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001108 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001109 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001110 mip->mi_word + mip->mi_cprefixlen, slang,
1111 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001112 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001113 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001114
1115 /* Use the WF_RARE flag for a rare prefix. */
1116 if (c & WF_RAREPFX)
1117 flags |= WF_RARE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001118 }
1119
1120 if (flags & WF_BANNED)
1121 res = SP_BANNED;
1122 else if (flags & WF_REGION)
1123 {
1124 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001125 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001126 res = SP_OK;
1127 else
1128 res = SP_LOCAL;
1129 }
1130 else if (flags & WF_RARE)
1131 res = SP_RARE;
1132 else
1133 res = SP_OK;
1134
1135 /* Always use the longest match and the best result. */
1136 if (mip->mi_result > res)
1137 {
1138 mip->mi_result = res;
1139 mip->mi_end = mip->mi_word + wlen;
1140 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001141 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001142 mip->mi_end = mip->mi_word + wlen;
1143
1144 if (res == SP_OK)
1145 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001146 }
1147
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001148 if (res == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001149 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001150 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001151}
1152
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001153/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001154 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1155 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001156 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001157 */
1158 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001159valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001160 int totprefcnt; /* nr of prefix IDs */
1161 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001162 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001163 char_u *word;
1164 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001165 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001166{
1167 int prefcnt;
1168 int pidx;
1169 regprog_T *rp;
1170 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001171 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001172
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001173 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001174 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1175 {
1176 pidx = slang->sl_pidxs[arridx + prefcnt];
1177
1178 /* Check the prefix ID. */
1179 if (prefid != (pidx & 0xff))
1180 continue;
1181
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001182 /* Check if the prefix doesn't combine and the word already has a
1183 * suffix. */
1184 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1185 continue;
1186
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001187 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001188 * stored in the two bytes above the prefix ID byte. */
1189 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001190 if (rp != NULL)
1191 {
1192 regmatch.regprog = rp;
1193 regmatch.rm_ic = FALSE;
1194 if (!vim_regexec(&regmatch, word, 0))
1195 continue;
1196 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001197 else if (cond_req)
1198 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001199
Bram Moolenaar53805d12005-08-01 07:08:33 +00001200 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001201 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001202 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001203 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001204}
1205
1206/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001207 * Check if the word at "mip->mi_word" has a matching prefix.
1208 * If it does, then check the following word.
1209 *
1210 * For a match mip->mi_result is updated.
1211 */
1212 static void
1213find_prefix(mip)
1214 matchinf_T *mip;
1215{
1216 idx_T arridx = 0;
1217 int len;
1218 int wlen = 0;
1219 int flen;
1220 int c;
1221 char_u *ptr;
1222 idx_T lo, hi, m;
1223 slang_T *slang = mip->mi_lp->lp_slang;
1224 char_u *byts;
1225 idx_T *idxs;
1226
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001227 byts = slang->sl_pbyts;
1228 if (byts == NULL)
1229 return; /* array is empty */
1230
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001231 /* We use the case-folded word here, since prefixes are always
1232 * case-folded. */
1233 ptr = mip->mi_fword;
1234 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001235 idxs = slang->sl_pidxs;
1236
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001237 /*
1238 * Repeat advancing in the tree until:
1239 * - there is a byte that doesn't match,
1240 * - we reach the end of the tree,
1241 * - or we reach the end of the line.
1242 */
1243 for (;;)
1244 {
1245 if (flen == 0 && *mip->mi_fend != NUL)
1246 flen = fold_more(mip);
1247
1248 len = byts[arridx++];
1249
1250 /* If the first possible byte is a zero the prefix could end here.
1251 * Check if the following word matches and supports the prefix. */
1252 if (byts[arridx] == 0)
1253 {
1254 /* There can be several prefixes with different conditions. We
1255 * try them all, since we don't know which one will give the
1256 * longest match. The word is the same each time, pass the list
1257 * of possible prefixes to find_word(). */
1258 mip->mi_prefarridx = arridx;
1259 mip->mi_prefcnt = len;
1260 while (len > 0 && byts[arridx] == 0)
1261 {
1262 ++arridx;
1263 --len;
1264 }
1265 mip->mi_prefcnt -= len;
1266
1267 /* Find the word that comes after the prefix. */
1268 mip->mi_prefixlen = wlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001269#ifdef FEAT_MBYTE
1270 if (has_mbyte)
1271 {
1272 /* Case-folded length may differ from original length. */
1273 mip->mi_cprefixlen = nofold_len(mip->mi_fword, wlen,
1274 mip->mi_word);
1275 }
1276 else
1277 mip->mi_cprefixlen = wlen;
1278#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001279 find_word(mip, FIND_PREFIX);
1280
1281
1282 if (len == 0)
1283 break; /* no children, word must end here */
1284 }
1285
1286 /* Stop looking at end of the line. */
1287 if (ptr[wlen] == NUL)
1288 break;
1289
1290 /* Perform a binary search in the list of accepted bytes. */
1291 c = ptr[wlen];
1292 lo = arridx;
1293 hi = arridx + len - 1;
1294 while (lo < hi)
1295 {
1296 m = (lo + hi) / 2;
1297 if (byts[m] > c)
1298 hi = m - 1;
1299 else if (byts[m] < c)
1300 lo = m + 1;
1301 else
1302 {
1303 lo = hi = m;
1304 break;
1305 }
1306 }
1307
1308 /* Stop if there is no matching byte. */
1309 if (hi < lo || byts[lo] != c)
1310 break;
1311
1312 /* Continue at the child (if there is one). */
1313 arridx = idxs[lo];
1314 ++wlen;
1315 --flen;
1316 }
1317}
1318
1319/*
1320 * Need to fold at least one more character. Do until next non-word character
1321 * for efficiency.
1322 * Return the length of the folded chars in bytes.
1323 */
1324 static int
1325fold_more(mip)
1326 matchinf_T *mip;
1327{
1328 int flen;
1329 char_u *p;
1330
1331 p = mip->mi_fend;
1332 do
1333 {
1334 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001335 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001336
1337 /* Include the non-word character so that we can check for the
1338 * word end. */
1339 if (*mip->mi_fend != NUL)
1340 mb_ptr_adv(mip->mi_fend);
1341
1342 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1343 mip->mi_fword + mip->mi_fwordlen,
1344 MAXWLEN - mip->mi_fwordlen);
1345 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1346 mip->mi_fwordlen += flen;
1347 return flen;
1348}
1349
1350/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001351 * Check case flags for a word. Return TRUE if the word has the requested
1352 * case.
1353 */
1354 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001355spell_valid_case(wordflags, treeflags)
1356 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001357 int treeflags; /* flags for the word in the spell tree */
1358{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001359 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001360 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001361 && ((treeflags & WF_ONECAP) == 0 || wordflags == WF_ONECAP)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001362}
1363
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001364/*
1365 * Return TRUE if spell checking is not enabled.
1366 */
1367 static int
1368no_spell_checking()
1369{
1370 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
1371 {
1372 EMSG(_("E756: Spell checking is not enabled"));
1373 return TRUE;
1374 }
1375 return FALSE;
1376}
Bram Moolenaar51485f02005-06-04 21:55:20 +00001377
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001378/*
1379 * Move to next spell error.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001380 * "curline" is TRUE for "z?": find word under/after cursor in the same line.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001381 * Return OK if found, FAIL otherwise.
1382 */
1383 int
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001384spell_move_to(dir, allwords, curline)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001385 int dir; /* FORWARD or BACKWARD */
1386 int allwords; /* TRUE for "[s" and "]s" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001387 int curline;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001388{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001389 linenr_T lnum;
1390 pos_T found_pos;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001391 char_u *line;
1392 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001393 char_u *endp;
1394 int attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001395 int len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001396 int has_syntax = syntax_present(curbuf);
1397 int col;
1398 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001399 char_u *buf = NULL;
1400 int buflen = 0;
1401 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001402 int capcol = -1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001403
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001404 if (no_spell_checking())
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001405 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001406
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001407 /*
1408 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar0c405862005-06-22 22:26:26 +00001409 * start halfway a word, we don't know where the it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001410 *
1411 * When searching backwards, we continue in the line to find the last
1412 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00001413 *
1414 * We concatenate the start of the next line, so that wrapped words work
1415 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
1416 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001417 */
1418 lnum = curwin->w_cursor.lnum;
1419 found_pos.lnum = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001420
1421 while (!got_int)
1422 {
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001423 line = ml_get(lnum);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001424
Bram Moolenaar0c405862005-06-22 22:26:26 +00001425 len = STRLEN(line);
1426 if (buflen < len + MAXWLEN + 2)
1427 {
1428 vim_free(buf);
1429 buflen = len + MAXWLEN + 2;
1430 buf = alloc(buflen);
1431 if (buf == NULL)
1432 break;
1433 }
1434
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001435 /* In first line check first word for Capital. */
1436 if (lnum == 1)
1437 capcol = 0;
1438
1439 /* For checking first word with a capital skip white space. */
1440 if (capcol == 0)
1441 capcol = skipwhite(line) - line;
1442
Bram Moolenaar0c405862005-06-22 22:26:26 +00001443 /* Copy the line into "buf" and append the start of the next line if
1444 * possible. */
1445 STRCPY(buf, line);
1446 if (lnum < curbuf->b_ml.ml_line_count)
1447 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
1448
1449 p = buf + skip;
1450 endp = buf + len;
1451 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001452 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001453 /* When searching backward don't search after the cursor. */
1454 if (dir == BACKWARD
1455 && lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001456 && (colnr_T)(p - buf) >= curwin->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001457 break;
1458
1459 /* start of word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00001460 attr = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001461 len = spell_check(curwin, p, &attr, &capcol);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001462
1463 if (attr != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001464 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001465 /* We found a bad word. Check the attribute. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001466 if (allwords || attr == highlight_attr[HLF_SPB])
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001467 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001468 /* When searching forward only accept a bad word after
1469 * the cursor. */
1470 if (dir == BACKWARD
1471 || lnum > curwin->w_cursor.lnum
1472 || (lnum == curwin->w_cursor.lnum
Bram Moolenaar0c405862005-06-22 22:26:26 +00001473 && (colnr_T)(curline ? p - buf + len
1474 : p - buf)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001475 > curwin->w_cursor.col))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001476 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001477 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001478 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001479 col = p - buf;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001480 (void)syn_get_id(lnum, (colnr_T)col,
1481 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001482 }
1483 else
1484 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001485
Bram Moolenaar51485f02005-06-04 21:55:20 +00001486 if (can_spell)
1487 {
1488 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001489 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001490#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00001491 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001492#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00001493 if (dir == FORWARD)
1494 {
1495 /* No need to search further. */
1496 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001497 vim_free(buf);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001498 return OK;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001499 }
1500 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001501 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001502 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001503 }
1504
Bram Moolenaar51485f02005-06-04 21:55:20 +00001505 /* advance to character after the word */
1506 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001507 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001508 }
1509
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001510 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001511 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001512
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001513 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001514 if (dir == BACKWARD)
1515 {
1516 if (found_pos.lnum != 0)
1517 {
1518 /* Use the last match in the line. */
1519 curwin->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001520 vim_free(buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001521 return OK;
1522 }
1523 if (lnum == 1)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001524 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001525 --lnum;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001526 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001527 }
1528 else
1529 {
1530 if (lnum == curbuf->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001531 break;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001532 ++lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001533
1534 /* Skip the characters at the start of the next line that were
1535 * included in a match crossing line boundaries. */
1536 if (attr == 0)
1537 skip = p - endp;
1538 else
1539 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001540
1541 /* Capscol skips over the inserted space. */
1542 --capcol;
1543
1544 /* But after empty line check first word in next line */
1545 if (*skipwhite(line) == NUL)
1546 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00001547 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001548
1549 line_breakcheck();
1550 }
1551
Bram Moolenaar0c405862005-06-22 22:26:26 +00001552 vim_free(buf);
1553 return FAIL;
1554}
1555
1556/*
1557 * For spell checking: concatenate the start of the following line "line" into
1558 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
1559 */
1560 void
1561spell_cat_line(buf, line, maxlen)
1562 char_u *buf;
1563 char_u *line;
1564 int maxlen;
1565{
1566 char_u *p;
1567 int n;
1568
1569 p = skipwhite(line);
1570 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
1571 p = skipwhite(p + 1);
1572
1573 if (*p != NUL)
1574 {
1575 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001576 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00001577 n = p - line;
1578 if (n >= maxlen)
1579 n = maxlen - 1;
1580 vim_memset(buf + 1, ' ', n);
1581 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001582}
1583
1584/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001585 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00001586 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001587 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001588 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001589spell_load_lang(lang)
1590 char_u *lang;
1591{
Bram Moolenaarb765d632005-06-07 21:00:02 +00001592 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001593 int r;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001594 char_u langcp[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001595
Bram Moolenaarb765d632005-06-07 21:00:02 +00001596 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001597 * It's truncated when an error is detected. */
1598 STRCPY(langcp, lang);
1599
Bram Moolenaarb765d632005-06-07 21:00:02 +00001600 /*
1601 * Find the first spell file for "lang" in 'runtimepath' and load it.
1602 */
1603 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
1604 "spell/%s.%s.spl", lang, spell_enc());
1605 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001606
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001607 if (r == FAIL && *langcp != NUL)
1608 {
1609 /* Try loading the ASCII version. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001610 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001611 "spell/%s.ascii.spl", lang);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001612 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &langcp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001613 }
1614
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001615 if (r == FAIL)
1616 smsg((char_u *)_("Warning: Cannot find word list \"%s\""),
1617 fname_enc + 6);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001618 else if (*langcp != NUL)
1619 {
1620 /* Load all the additions. */
1621 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
1622 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &langcp);
1623 }
1624}
1625
1626/*
1627 * Return the encoding used for spell checking: Use 'encoding', except that we
1628 * use "latin1" for "latin9". And limit to 60 characters (just in case).
1629 */
1630 static char_u *
1631spell_enc()
1632{
1633
1634#ifdef FEAT_MBYTE
1635 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
1636 return p_enc;
1637#endif
1638 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001639}
1640
1641/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001642 * Get the name of the .spl file for the internal wordlist into
1643 * "fname[MAXPATHL]".
1644 */
1645 static void
1646int_wordlist_spl(fname)
1647 char_u *fname;
1648{
1649 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
1650 int_wordlist, spell_enc());
1651}
1652
1653/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001654 * Allocate a new slang_T.
1655 * Caller must fill "sl_next".
1656 */
1657 static slang_T *
1658slang_alloc(lang)
1659 char_u *lang;
1660{
1661 slang_T *lp;
1662
Bram Moolenaar51485f02005-06-04 21:55:20 +00001663 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001664 if (lp != NULL)
1665 {
1666 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001667 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001668 }
1669 return lp;
1670}
1671
1672/*
1673 * Free the contents of an slang_T and the structure itself.
1674 */
1675 static void
1676slang_free(lp)
1677 slang_T *lp;
1678{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001679 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001680 vim_free(lp->sl_fname);
1681 slang_clear(lp);
1682 vim_free(lp);
1683}
1684
1685/*
1686 * Clear an slang_T so that the file can be reloaded.
1687 */
1688 static void
1689slang_clear(lp)
1690 slang_T *lp;
1691{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001692 garray_T *gap;
1693 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001694 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001695 int i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001696
Bram Moolenaar51485f02005-06-04 21:55:20 +00001697 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001698 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001699 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001700 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001701 vim_free(lp->sl_pbyts);
1702 lp->sl_pbyts = NULL;
1703
Bram Moolenaar51485f02005-06-04 21:55:20 +00001704 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001705 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001706 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001707 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001708 vim_free(lp->sl_pidxs);
1709 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001710
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001711 gap = &lp->sl_rep;
1712 while (gap->ga_len > 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001713 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001714 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
1715 vim_free(ftp->ft_from);
1716 vim_free(ftp->ft_to);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001717 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001718 ga_clear(gap);
1719
1720 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001721 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001722 {
1723 /* "ga_len" is set to 1 without adding an item for latin1 */
1724 if (gap->ga_data != NULL)
1725 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
1726 for (i = 0; i < gap->ga_len; ++i)
1727 vim_free(((int **)gap->ga_data)[i]);
1728 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001729 else
1730 /* SAL items: free salitem_T items */
1731 while (gap->ga_len > 0)
1732 {
1733 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
1734 vim_free(smp->sm_lead);
1735 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
1736 vim_free(smp->sm_to);
1737#ifdef FEAT_MBYTE
1738 vim_free(smp->sm_lead_w);
1739 vim_free(smp->sm_oneof_w);
1740 vim_free(smp->sm_to_w);
1741#endif
1742 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001743 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001744
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001745 for (i = 0; i < lp->sl_prefixcnt; ++i)
1746 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001747 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001748 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001749 lp->sl_prefprog = NULL;
1750
1751 vim_free(lp->sl_midword);
1752 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001753
Bram Moolenaarea424162005-06-16 21:51:00 +00001754#ifdef FEAT_MBYTE
1755 {
1756 int todo = lp->sl_map_hash.ht_used;
1757 hashitem_T *hi;
1758
1759 for (hi = lp->sl_map_hash.ht_array; todo > 0; ++hi)
1760 if (!HASHITEM_EMPTY(hi))
1761 {
1762 --todo;
1763 vim_free(hi->hi_key);
1764 }
1765 }
1766 hash_clear(&lp->sl_map_hash);
1767#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001768}
1769
1770/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001771 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001772 * Invoked through do_in_runtimepath().
1773 */
1774 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00001775spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001776 char_u *fname;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001777 void *cookie; /* points to the language name */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001778{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001779 (void)spell_load_file(fname, (char_u *)cookie, NULL, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00001780}
1781
1782/*
1783 * Load one spell file and store the info into a slang_T.
1784 *
1785 * This is invoked in two ways:
1786 * - From spell_load_cb() to load a spell file for the first time. "lang" is
1787 * the language name, "old_lp" is NULL. Will allocate an slang_T.
1788 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
1789 * points to the existing slang_T.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001790 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00001791 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001792 static slang_T *
1793spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00001794 char_u *fname;
1795 char_u *lang;
1796 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001797 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00001798{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001799 FILE *fd;
1800 char_u buf[MAXWLEN + 1];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001801 char_u *p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001802 char_u *bp;
1803 idx_T *ip;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001804 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001805 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001806 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001807 int round;
1808 char_u *save_sourcing_name = sourcing_name;
1809 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001810 int cnt, ccnt;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001811 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001812 slang_T *lp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001813 garray_T *gap;
1814 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001815 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001816 short *first;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001817 idx_T idx;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001818 int c = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001819
Bram Moolenaarb765d632005-06-07 21:00:02 +00001820 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001821 if (fd == NULL)
1822 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001823 if (!silent)
1824 EMSG2(_(e_notopen), fname);
1825 else if (p_verbose > 2)
1826 {
1827 verbose_enter();
1828 smsg((char_u *)e_notopen, fname);
1829 verbose_leave();
1830 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001831 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001832 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00001833 if (p_verbose > 2)
1834 {
1835 verbose_enter();
1836 smsg((char_u *)_("Reading spell file \"%s\""), fname);
1837 verbose_leave();
1838 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001839
Bram Moolenaarb765d632005-06-07 21:00:02 +00001840 if (old_lp == NULL)
1841 {
1842 lp = slang_alloc(lang);
1843 if (lp == NULL)
1844 goto endFAIL;
1845
1846 /* Remember the file name, used to reload the file when it's updated. */
1847 lp->sl_fname = vim_strsave(fname);
1848 if (lp->sl_fname == NULL)
1849 goto endFAIL;
1850
1851 /* Check for .add.spl. */
1852 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
1853 }
1854 else
1855 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001856
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001857 /* Set sourcing_name, so that error messages mention the file name. */
1858 sourcing_name = fname;
1859 sourcing_lnum = 0;
1860
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001861 /* <HEADER>: <fileID>
1862 * <regioncnt> <regionname> ...
1863 * <charflagslen> <charflags>
1864 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001865 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001866 * <prefcondcnt> <prefcond> ...
1867 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001868 for (i = 0; i < VIMSPELLMAGICL; ++i)
1869 buf[i] = getc(fd); /* <fileID> */
1870 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
1871 {
1872 EMSG(_("E757: Wrong file ID in spell file"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001873 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001874 }
1875
1876 cnt = getc(fd); /* <regioncnt> */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001877 if (cnt < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001878 {
1879truncerr:
Bram Moolenaar7887d882005-07-01 22:33:52 +00001880 EMSG(_(e_spell_trunc));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001881 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001882 }
1883 if (cnt > 8)
1884 {
1885formerr:
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001886 EMSG(_(e_format));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001887 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001888 }
1889 for (i = 0; i < cnt; ++i)
1890 {
1891 lp->sl_regions[i * 2] = getc(fd); /* <regionname> */
1892 lp->sl_regions[i * 2 + 1] = getc(fd);
1893 }
1894 lp->sl_regions[cnt * 2] = NUL;
1895
Bram Moolenaar7887d882005-07-01 22:33:52 +00001896 /* <charflagslen> <charflags> */
1897 p = read_cnt_string(fd, 1, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001898 if (cnt < 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00001899 goto endFAIL;
1900
1901 /* <fcharslen> <fchars> */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001902 fol = read_cnt_string(fd, 2, &ccnt);
1903 if (ccnt < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001904 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001905 vim_free(p);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001906 goto endFAIL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001907 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00001908
1909 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
1910 if (p != NULL && fol != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001911 i = set_spell_charflags(p, cnt, fol);
Bram Moolenaar7887d882005-07-01 22:33:52 +00001912
1913 vim_free(p);
1914 vim_free(fol);
1915
1916 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
1917 if ((p == NULL) != (fol == NULL))
1918 goto formerr;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00001919
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001920 /* <midwordlen> <midword> */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001921 lp->sl_midword = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001922 if (cnt < 0)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001923 goto endFAIL;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001924
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001925 /* <prefcondcnt> <prefcond> ... */
1926 cnt = (getc(fd) << 8) + getc(fd); /* <prefcondcnt> */
1927 if (cnt > 0)
1928 {
1929 lp->sl_prefprog = (regprog_T **)alloc_clear(
1930 (unsigned)sizeof(regprog_T *) * cnt);
1931 if (lp->sl_prefprog == NULL)
1932 goto endFAIL;
1933 lp->sl_prefixcnt = cnt;
1934
1935 for (i = 0; i < cnt; ++i)
1936 {
1937 /* <prefcond> : <condlen> <condstr> */
1938 n = getc(fd); /* <condlen> */
1939 if (n < 0)
1940 goto formerr;
1941 /* When <condlen> is zero we have an empty condition. Otherwise
1942 * compile the regexp program used to check for the condition. */
1943 if (n > 0)
1944 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001945 buf[0] = '^'; /* always match at one position only */
1946 p = buf + 1;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001947 while (n-- > 0)
1948 *p++ = getc(fd); /* <condstr> */
1949 *p = NUL;
1950 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
1951 }
1952 }
1953 }
1954
1955
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001956 /* <SUGGEST> : <repcount> <rep> ...
1957 * <salflags> <salcount> <sal> ...
1958 * <maplen> <mapstr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001959
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001960 cnt = (getc(fd) << 8) + getc(fd); /* <repcount> */
1961 if (cnt < 0)
1962 goto formerr;
1963
1964 gap = &lp->sl_rep;
1965 if (ga_grow(gap, cnt) == FAIL)
1966 goto endFAIL;
1967
1968 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
1969 for (; gap->ga_len < cnt; ++gap->ga_len)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001970 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001971 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
Bram Moolenaar7887d882005-07-01 22:33:52 +00001972 ftp->ft_from = read_cnt_string(fd, 1, &i);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001973 if (i <= 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00001974 goto endFAIL;
1975 ftp->ft_to = read_cnt_string(fd, 1, &i);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001976 if (i <= 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001977 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00001978 vim_free(ftp->ft_from);
1979 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001980 }
1981 }
1982
1983 /* Fill the first-index table. */
1984 first = lp->sl_rep_first;
1985 for (i = 0; i < 256; ++i)
1986 first[i] = -1;
1987 for (i = 0; i < gap->ga_len; ++i)
1988 {
1989 ftp = &((fromto_T *)gap->ga_data)[i];
1990 if (first[*ftp->ft_from] == -1)
1991 first[*ftp->ft_from] = i;
1992 }
1993
1994 i = getc(fd); /* <salflags> */
1995 if (i & SAL_F0LLOWUP)
1996 lp->sl_followup = TRUE;
1997 if (i & SAL_COLLAPSE)
1998 lp->sl_collapse = TRUE;
1999 if (i & SAL_REM_ACCENTS)
2000 lp->sl_rem_accents = TRUE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002001 if (i & SAL_SOFO)
2002 lp->sl_sofo = TRUE;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002003 else
2004 lp->sl_sofo = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002005
2006 cnt = (getc(fd) << 8) + getc(fd); /* <salcount> */
2007 if (cnt < 0)
2008 goto formerr;
2009
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002010 if (lp->sl_sofo)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002011 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002012 /*
2013 * SOFOFROM and SOFOTO items come in one <salfrom> and <salto>
2014 */
2015 if (cnt != 1)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002016 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002017
Bram Moolenaar7887d882005-07-01 22:33:52 +00002018 /* <salfromlen> <salfrom> */
2019 bp = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002020 if (cnt < 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002021 goto endFAIL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002022
Bram Moolenaar7887d882005-07-01 22:33:52 +00002023 /* <saltolen> <salto> */
2024 fol = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002025 if (cnt < 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002026 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002027 vim_free(bp);
2028 goto endFAIL;
2029 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002030
Bram Moolenaar7887d882005-07-01 22:33:52 +00002031 /* Store the info in lp->sl_sal and/or lp->sl_sal_first. */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002032 if (bp != NULL && fol != NULL)
2033 i = set_sofo(lp, bp, fol);
2034 else if (bp != NULL || fol != NULL)
2035 i = FAIL; /* only one of two strings is an error */
2036 else
2037 i = OK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002038
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002039 vim_free(bp);
2040 vim_free(fol);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002041 if (i == FAIL)
2042 goto formerr;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002043 }
2044 else
2045 {
2046 /*
2047 * SAL items
2048 */
2049 gap = &lp->sl_sal;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002050 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002051 if (ga_grow(gap, cnt) == FAIL)
2052 goto endFAIL;
2053
2054 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
2055 for (; gap->ga_len < cnt; ++gap->ga_len)
2056 {
2057 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
2058 ccnt = getc(fd); /* <salfromlen> */
2059 if (ccnt < 0)
2060 goto formerr;
2061 if ((p = alloc(ccnt + 2)) == NULL)
2062 goto endFAIL;
2063 smp->sm_lead = p;
2064
2065 /* Read up to the first special char into sm_lead. */
2066 for (i = 0; i < ccnt; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002067 {
2068 c = getc(fd); /* <salfrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002069 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002070 break;
2071 *p++ = c;
2072 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002073 smp->sm_leadlen = p - smp->sm_lead;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002074 *p++ = NUL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002075
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002076 /* Put (abc) chars in sm_oneof, if any. */
2077 if (c == '(')
2078 {
2079 smp->sm_oneof = p;
2080 for (++i; i < ccnt; ++i)
2081 {
2082 c = getc(fd); /* <salfrom> */
2083 if (c == ')')
2084 break;
2085 *p++ = c;
2086 }
2087 *p++ = NUL;
2088 if (++i < ccnt)
2089 c = getc(fd);
2090 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002091 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002092 smp->sm_oneof = NULL;
2093
2094 /* Any following chars go in sm_rules. */
2095 smp->sm_rules = p;
2096 if (i < ccnt)
2097 /* store the char we got while checking for end of sm_lead */
2098 *p++ = c;
2099 for (++i; i < ccnt; ++i)
2100 *p++ = getc(fd); /* <salfrom> */
2101 *p++ = NUL;
2102
Bram Moolenaar7887d882005-07-01 22:33:52 +00002103 /* <saltolen> <salto> */
2104 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002105 if (ccnt < 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002106 {
2107 vim_free(smp->sm_lead);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002108 goto formerr;
2109 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002110
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002111#ifdef FEAT_MBYTE
2112 if (has_mbyte)
2113 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002114 /* convert the multi-byte strings to wide char strings */
2115 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
2116 smp->sm_leadlen = mb_charlen(smp->sm_lead);
2117 if (smp->sm_oneof == NULL)
2118 smp->sm_oneof_w = NULL;
2119 else
2120 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002121 if (smp->sm_to == NULL)
2122 smp->sm_to_w = NULL;
2123 else
2124 smp->sm_to_w = mb_str2wide(smp->sm_to);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002125 if (smp->sm_lead_w == NULL
2126 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002127 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002128 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002129 vim_free(smp->sm_lead);
2130 vim_free(smp->sm_to);
2131 vim_free(smp->sm_lead_w);
2132 vim_free(smp->sm_oneof_w);
2133 vim_free(smp->sm_to_w);
2134 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002135 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002136 }
2137#endif
2138 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002139
2140 /* Fill the first-index table. */
Bram Moolenaar7887d882005-07-01 22:33:52 +00002141 set_sal_first(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002142 }
2143
Bram Moolenaar7887d882005-07-01 22:33:52 +00002144 /* <maplen> <mapstr> */
2145 p = read_cnt_string(fd, 2, &cnt);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002146 if (cnt < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002147 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002148 if (p != NULL)
2149 {
2150 set_map_str(lp, p);
2151 vim_free(p);
2152 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002153
Bram Moolenaar51485f02005-06-04 21:55:20 +00002154 /* round 1: <LWORDTREE>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002155 * round 2: <KWORDTREE>
2156 * round 3: <PREFIXTREE> */
2157 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002158 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002159 /* The tree size was computed when writing the file, so that we can
2160 * allocate it as one long block. <nodecount> */
2161 len = (getc(fd) << 24) + (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2162 if (len < 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002163 goto truncerr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002164 if (len > 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002165 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002166 /* Allocate the byte array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002167 bp = lalloc((long_u)len, TRUE);
2168 if (bp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002169 goto endFAIL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002170 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002171 lp->sl_fbyts = bp;
2172 else if (round == 2)
2173 lp->sl_kbyts = bp;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002174 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002175 lp->sl_pbyts = bp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002176
2177 /* Allocate the index array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002178 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
2179 if (ip == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002180 goto endFAIL;
2181 if (round == 1)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002182 lp->sl_fidxs = ip;
2183 else if (round == 2)
2184 lp->sl_kidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002185 else
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002186 lp->sl_pidxs = ip;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002187
2188 /* Read the tree and store it in the array. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002189 idx = read_tree(fd, bp, ip, len, 0, round == 3, lp->sl_prefixcnt);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002190 if (idx == -1)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002191 goto truncerr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002192 if (idx < 0)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002193 goto formerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002194 }
2195 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00002196
Bram Moolenaarb765d632005-06-07 21:00:02 +00002197 /* For a new file link it in the list of spell files. */
2198 if (old_lp == NULL)
2199 {
2200 lp->sl_next = first_lang;
2201 first_lang = lp;
2202 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002203
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002204 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002205
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002206endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002207 if (lang != NULL)
2208 /* truncating the name signals the error to spell_load_lang() */
2209 *lang = NUL;
2210 if (lp != NULL && old_lp == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002211 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002212 slang_free(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002213 lp = NULL;
2214 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002215
2216endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002217 if (fd != NULL)
2218 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002219 sourcing_name = save_sourcing_name;
2220 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002221
2222 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002223}
2224
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002225/*
2226 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002227 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002228 * Returns NULL when the count is zero.
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002229 * Sets "*cntp" to -1 when there is an error, length of the result otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002230 */
2231 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002232read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002233 FILE *fd;
2234 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002235 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002236{
2237 int cnt = 0;
2238 int i;
2239 char_u *str;
2240
2241 /* read the length bytes, MSB first */
2242 for (i = 0; i < cnt_bytes; ++i)
2243 cnt = (cnt << 8) + getc(fd);
2244 if (cnt < 0)
2245 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00002246 EMSG(_(e_spell_trunc));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002247 *cntp = -1;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002248 return NULL;
2249 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002250 *cntp = cnt;
2251 if (cnt == 0)
2252 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002253
2254 /* allocate memory */
2255 str = alloc((unsigned)cnt + 1);
2256 if (str == NULL)
2257 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002258 *cntp = -1;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002259 return NULL;
2260 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002261
2262 /* Read the string. Doesn't check for truncated file. */
2263 for (i = 0; i < cnt; ++i)
2264 str[i] = getc(fd);
2265 str[i] = NUL;
2266
2267 return str;
2268}
2269
Bram Moolenaar7887d882005-07-01 22:33:52 +00002270/*
2271 * Set the SOFOFROM and SOFOTO items in language "lp".
2272 * Returns FAIL when there is something wrong.
2273 */
2274 static int
2275set_sofo(lp, from, to)
2276 slang_T *lp;
2277 char_u *from;
2278 char_u *to;
2279{
2280 int i;
2281
2282#ifdef FEAT_MBYTE
2283 garray_T *gap;
2284 char_u *s;
2285 char_u *p;
2286 int c;
2287 int *inp;
2288
2289 if (has_mbyte)
2290 {
2291 /* Use "sl_sal" as an array with 256 pointers to a list of wide
2292 * characters. The index is the low byte of the character.
2293 * The list contains from-to pairs with a terminating NUL.
2294 * sl_sal_first[] is used for latin1 "from" characters. */
2295 gap = &lp->sl_sal;
2296 ga_init2(gap, sizeof(int *), 1);
2297 if (ga_grow(gap, 256) == FAIL)
2298 return FAIL;
2299 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
2300 gap->ga_len = 256;
2301
2302 /* First count the number of items for each list. Temporarily use
2303 * sl_sal_first[] for this. */
2304 for (p = from, s = to; *p != NUL && *s != NUL; )
2305 {
2306 c = mb_ptr2char_adv(&p);
2307 mb_ptr_adv(s);
2308 if (c >= 256)
2309 ++lp->sl_sal_first[c & 0xff];
2310 }
2311 if (*p != NUL || *s != NUL) /* lengths differ */
2312 return FAIL;
2313
2314 /* Allocate the lists. */
2315 for (i = 0; i < 256; ++i)
2316 if (lp->sl_sal_first[i] > 0)
2317 {
2318 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
2319 if (p == NULL)
2320 return FAIL;
2321 ((int **)gap->ga_data)[i] = (int *)p;
2322 *(int *)p = 0;
2323 }
2324
2325 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
2326 * list. */
2327 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
2328 for (p = from, s = to; *p != NUL && *s != NUL; )
2329 {
2330 c = mb_ptr2char_adv(&p);
2331 i = mb_ptr2char_adv(&s);
2332 if (c >= 256)
2333 {
2334 /* Append the from-to chars at the end of the list with
2335 * the low byte. */
2336 inp = ((int **)gap->ga_data)[c & 0xff];
2337 while (*inp != 0)
2338 ++inp;
2339 *inp++ = c; /* from char */
2340 *inp++ = i; /* to char */
2341 *inp++ = NUL; /* NUL at the end */
2342 }
2343 else
2344 /* mapping byte to char is done in sl_sal_first[] */
2345 lp->sl_sal_first[c] = i;
2346 }
2347 }
2348 else
2349#endif
2350 {
2351 /* mapping bytes to bytes is done in sl_sal_first[] */
2352 if (STRLEN(from) != STRLEN(to))
2353 return FAIL;
2354
2355 for (i = 0; to[i] != NUL; ++i)
2356 lp->sl_sal_first[from[i]] = to[i];
2357 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
2358 }
2359
2360 return OK;
2361}
2362
2363/*
2364 * Fill the first-index table for "lp".
2365 */
2366 static void
2367set_sal_first(lp)
2368 slang_T *lp;
2369{
2370 salfirst_T *sfirst;
2371 int i;
2372 salitem_T *smp;
2373 int c;
2374 garray_T *gap = &lp->sl_sal;
2375
2376 sfirst = lp->sl_sal_first;
2377 for (i = 0; i < 256; ++i)
2378 sfirst[i] = -1;
2379 smp = (salitem_T *)gap->ga_data;
2380 for (i = 0; i < gap->ga_len; ++i)
2381 {
2382#ifdef FEAT_MBYTE
2383 if (has_mbyte)
2384 /* Use the lowest byte of the first character. For latin1 it's
2385 * the character, for other encodings it should differ for most
2386 * characters. */
2387 c = *smp[i].sm_lead_w & 0xff;
2388 else
2389#endif
2390 c = *smp[i].sm_lead;
2391 if (sfirst[c] == -1)
2392 {
2393 sfirst[c] = i;
2394#ifdef FEAT_MBYTE
2395 if (has_mbyte)
2396 {
2397 int n;
2398
2399 /* Make sure all entries with this byte are following each
2400 * other. Move the ones that are in the wrong position. Do
2401 * keep the same ordering! */
2402 while (i + 1 < gap->ga_len
2403 && (*smp[i + 1].sm_lead_w & 0xff) == c)
2404 /* Skip over entry with same index byte. */
2405 ++i;
2406
2407 for (n = 1; i + n < gap->ga_len; ++n)
2408 if ((*smp[i + n].sm_lead_w & 0xff) == c)
2409 {
2410 salitem_T tsal;
2411
2412 /* Move entry with same index byte after the entries
2413 * we already found. */
2414 ++i;
2415 --n;
2416 tsal = smp[i + n];
2417 mch_memmove(smp + i + 1, smp + i,
2418 sizeof(salitem_T) * n);
2419 smp[i] = tsal;
2420 }
2421 }
2422#endif
2423 }
2424 }
2425}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002426
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002427#ifdef FEAT_MBYTE
2428/*
2429 * Turn a multi-byte string into a wide character string.
2430 * Return it in allocated memory (NULL for out-of-memory)
2431 */
2432 static int *
2433mb_str2wide(s)
2434 char_u *s;
2435{
2436 int *res;
2437 char_u *p;
2438 int i = 0;
2439
2440 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
2441 if (res != NULL)
2442 {
2443 for (p = s; *p != NUL; )
2444 res[i++] = mb_ptr2char_adv(&p);
2445 res[i] = NUL;
2446 }
2447 return res;
2448}
2449#endif
2450
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002451/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00002452 * Read one row of siblings from the spell file and store it in the byte array
2453 * "byts" and index array "idxs". Recursively read the children.
2454 *
Bram Moolenaar0c405862005-06-22 22:26:26 +00002455 * NOTE: The code here must match put_node().
Bram Moolenaar51485f02005-06-04 21:55:20 +00002456 *
2457 * Returns the index follosing the siblings.
2458 * Returns -1 if the file is shorter than expected.
2459 * Returns -2 if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002460 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002461 static idx_T
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002462read_tree(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002463 FILE *fd;
2464 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002465 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002466 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002467 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002468 int prefixtree; /* TRUE for reading PREFIXTREE */
2469 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002470{
Bram Moolenaar51485f02005-06-04 21:55:20 +00002471 int len;
2472 int i;
2473 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002474 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002475 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002476 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002477#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002478
Bram Moolenaar51485f02005-06-04 21:55:20 +00002479 len = getc(fd); /* <siblingcount> */
2480 if (len <= 0)
2481 return -1;
2482
2483 if (startidx + len >= maxidx)
2484 return -2;
2485 byts[idx++] = len;
2486
2487 /* Read the byte values, flag/region bytes and shared indexes. */
2488 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002489 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002490 c = getc(fd); /* <byte> */
2491 if (c < 0)
2492 return -1;
2493 if (c <= BY_SPECIAL)
2494 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002495 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002496 {
2497 /* No flags, all regions. */
2498 idxs[idx] = 0;
2499 c = 0;
2500 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002501 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002502 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002503 if (prefixtree)
2504 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002505 /* Read the optional pflags byte, the prefix ID and the
2506 * condition nr. In idxs[] store the prefix ID in the low
2507 * byte, the condition index shifted up 8 bits, the flags
2508 * shifted up 24 bits. */
2509 if (c == BY_FLAGS)
2510 c = getc(fd) << 24; /* <pflags> */
2511 else
2512 c = 0;
2513
2514 c |= getc(fd); /* <prefixID> */
2515
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002516 n = (getc(fd) << 8) + getc(fd); /* <prefcondnr> */
2517 if (n >= maxprefcondnr)
2518 return -2;
Bram Moolenaar53805d12005-08-01 07:08:33 +00002519 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002520 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002521 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002522 {
2523 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002524 * idxs[] the flags go in the low two bytes, region above
2525 * that and prefix ID above the region. */
2526 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002527 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002528 if (c2 == BY_FLAGS2)
2529 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002530 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002531 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002532 if (c & WF_PFX)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00002533 c = (getc(fd) << 24) + c; /* <prefixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002534 }
2535
Bram Moolenaar51485f02005-06-04 21:55:20 +00002536 idxs[idx] = c;
2537 c = 0;
2538 }
2539 else /* c == BY_INDEX */
2540 {
2541 /* <nodeidx> */
2542 n = (getc(fd) << 16) + (getc(fd) << 8) + getc(fd);
2543 if (n < 0 || n >= maxidx)
2544 return -2;
2545 idxs[idx] = n + SHARED_MASK;
2546 c = getc(fd); /* <xbyte> */
2547 }
2548 }
2549 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002550 }
2551
Bram Moolenaar51485f02005-06-04 21:55:20 +00002552 /* Recursively read the children for non-shared siblings.
2553 * Skip the end-of-word ones (zero byte value) and the shared ones (and
2554 * remove SHARED_MASK) */
2555 for (i = 1; i <= len; ++i)
2556 if (byts[startidx + i] != 0)
2557 {
2558 if (idxs[startidx + i] & SHARED_MASK)
2559 idxs[startidx + i] &= ~SHARED_MASK;
2560 else
2561 {
2562 idxs[startidx + i] = idx;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002563 idx = read_tree(fd, byts, idxs, maxidx, idx,
2564 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002565 if (idx < 0)
2566 break;
2567 }
2568 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002569
Bram Moolenaar51485f02005-06-04 21:55:20 +00002570 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002571}
2572
2573/*
2574 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002575 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002576 */
2577 char_u *
2578did_set_spelllang(buf)
2579 buf_T *buf;
2580{
2581 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002582 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002583 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00002584 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002585 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002586 int region_mask;
2587 slang_T *lp;
2588 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002589 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002590 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002591 int len;
2592 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002593 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002594 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002595 char_u *use_region = NULL;
2596 int dont_use_region = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002597
2598 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002599 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002600
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002601 /* loop over comma separated language names. */
2602 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002603 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002604 /* Get one language name. */
2605 copy_option_part(&splp, lang, MAXWLEN, ",");
2606
Bram Moolenaar5482f332005-04-17 20:18:43 +00002607 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002608 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002609
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002610 /* If the name ends in ".spl" use it as the name of the spell file.
2611 * If there is a region name let "region" point to it and remove it
2612 * from the name. */
2613 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
2614 {
2615 filename = TRUE;
2616
Bram Moolenaarb6356332005-07-18 21:40:44 +00002617 /* Locate a region and remove it from the file name. */
2618 p = vim_strchr(gettail(lang), '_');
2619 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
2620 && !ASCII_ISALPHA(p[3]))
2621 {
2622 vim_strncpy(region_cp, p + 1, 2);
2623 mch_memmove(p, p + 3, len - (p - lang) - 2);
2624 len -= 3;
2625 region = region_cp;
2626 }
2627 else
2628 dont_use_region = TRUE;
2629
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002630 /* Check if we loaded this language before. */
2631 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2632 if (fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME)
2633 break;
2634 }
2635 else
2636 {
2637 filename = FALSE;
2638 if (len > 3 && lang[len - 3] == '_')
2639 {
2640 region = lang + len - 2;
2641 len -= 3;
2642 lang[len] = NUL;
2643 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002644 else
2645 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002646
2647 /* Check if we loaded this language before. */
2648 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2649 if (STRICMP(lang, lp->sl_name) == 0)
2650 break;
2651 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002652
Bram Moolenaarb6356332005-07-18 21:40:44 +00002653 if (region != NULL)
2654 {
2655 /* If the region differs from what was used before then don't
2656 * use it for 'spellfile'. */
2657 if (use_region != NULL && STRCMP(region, use_region) != 0)
2658 dont_use_region = TRUE;
2659 use_region = region;
2660 }
2661
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002662 /* If not found try loading the language now. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002663 if (lp == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002664 {
2665 if (filename)
2666 (void)spell_load_file(lang, lang, NULL, FALSE);
2667 else
2668 spell_load_lang(lang);
2669 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002670
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002671 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002672 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002673 */
2674 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002675 if (filename ? fullpathcmp(lang, lp->sl_fname, FALSE) == FPC_SAME
2676 : STRICMP(lang, lp->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002677 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00002678 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002679 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002680 {
2681 /* find region in sl_regions */
2682 c = find_region(lp->sl_regions, region);
2683 if (c == REGION_ALL)
2684 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002685 if (lp->sl_add)
2686 {
2687 if (*lp->sl_regions != NUL)
2688 /* This addition file is for other regions. */
2689 region_mask = 0;
2690 }
2691 else
2692 /* This is probably an error. Give a warning and
2693 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002694 smsg((char_u *)
2695 _("Warning: region %s not supported"),
2696 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002697 }
2698 else
2699 region_mask = 1 << c;
2700 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002701
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002702 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002703 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002704 if (ga_grow(&ga, 1) == FAIL)
2705 {
2706 ga_clear(&ga);
2707 return e_outofmem;
2708 }
2709 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2710 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2711 ++ga.ga_len;
2712 use_midword(lp, buf);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002713 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002714 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002715 }
2716
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002717 /* round 0: load int_wordlist, if possible.
2718 * round 1: load first name in 'spellfile'.
2719 * round 2: load second name in 'spellfile.
2720 * etc. */
2721 spf = curbuf->b_p_spf;
2722 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002723 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002724 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002725 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002726 /* Internal wordlist, if there is one. */
2727 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002728 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002729 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002730 }
2731 else
2732 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002733 /* One entry in 'spellfile'. */
2734 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
2735 STRCAT(spf_name, ".spl");
2736
2737 /* If it was already found above then skip it. */
2738 for (c = 0; c < ga.ga_len; ++c)
2739 if (fullpathcmp(spf_name,
2740 LANGP_ENTRY(ga, c)->lp_slang->sl_fname,
2741 FALSE) == FPC_SAME)
2742 break;
2743 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002744 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002745 }
2746
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002747 /* Check if it was loaded already. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002748 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
2749 if (fullpathcmp(spf_name, lp->sl_fname, FALSE) == FPC_SAME)
2750 break;
2751 if (lp == NULL)
2752 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002753 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002754 * region name, the region is ignored otherwise. for int_wordlist
2755 * use an arbitrary name. */
2756 if (round == 0)
2757 STRCPY(lang, "internal wordlist");
2758 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00002759 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002760 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00002761 p = vim_strchr(lang, '.');
2762 if (p != NULL)
2763 *p = NUL; /* truncate at ".encoding.add" */
2764 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002765 lp = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002766 }
2767 if (lp != NULL && ga_grow(&ga, 1) == OK)
2768 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002769 region_mask = REGION_ALL;
2770 if (use_region != NULL && !dont_use_region)
2771 {
2772 /* find region in sl_regions */
2773 c = find_region(lp->sl_regions, use_region);
2774 if (c != REGION_ALL)
2775 region_mask = 1 << c;
2776 else if (*lp->sl_regions != NUL)
2777 /* This spell file is for other regions. */
2778 region_mask = 0;
2779 }
2780
2781 if (region_mask != 0)
2782 {
2783 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = lp;
2784 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
2785 ++ga.ga_len;
2786 use_midword(lp, buf);
2787 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002788 }
2789 }
2790
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002791 /* Add a NULL entry to mark the end of the list. */
2792 if (ga_grow(&ga, 1) == FAIL)
2793 {
2794 ga_clear(&ga);
2795 return e_outofmem;
2796 }
2797 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = NULL;
2798 ++ga.ga_len;
2799
2800 /* Everything is fine, store the new b_langp value. */
2801 ga_clear(&buf->b_langp);
2802 buf->b_langp = ga;
2803
2804 return NULL;
2805}
2806
2807/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002808 * Clear the midword characters for buffer "buf".
2809 */
2810 static void
2811clear_midword(buf)
2812 buf_T *buf;
2813{
2814 vim_memset(buf->b_spell_ismw, 0, 256);
2815#ifdef FEAT_MBYTE
2816 vim_free(buf->b_spell_ismw_mb);
2817 buf->b_spell_ismw_mb = NULL;
2818#endif
2819}
2820
2821/*
2822 * Use the "sl_midword" field of language "lp" for buffer "buf".
2823 * They add up to any currently used midword characters.
2824 */
2825 static void
2826use_midword(lp, buf)
2827 slang_T *lp;
2828 buf_T *buf;
2829{
2830 char_u *p;
2831
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002832 if (lp->sl_midword == NULL) /* there aren't any */
2833 return;
2834
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002835 for (p = lp->sl_midword; *p != NUL; )
2836#ifdef FEAT_MBYTE
2837 if (has_mbyte)
2838 {
2839 int c, l, n;
2840 char_u *bp;
2841
2842 c = mb_ptr2char(p);
2843 l = mb_ptr2len_check(p);
2844 if (c < 256)
2845 buf->b_spell_ismw[c] = TRUE;
2846 else if (buf->b_spell_ismw_mb == NULL)
2847 /* First multi-byte char in "b_spell_ismw_mb". */
2848 buf->b_spell_ismw_mb = vim_strnsave(p, l);
2849 else
2850 {
2851 /* Append multi-byte chars to "b_spell_ismw_mb". */
2852 n = STRLEN(buf->b_spell_ismw_mb);
2853 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
2854 if (bp != NULL)
2855 {
2856 vim_free(buf->b_spell_ismw_mb);
2857 buf->b_spell_ismw_mb = bp;
2858 vim_strncpy(bp + n, p, l);
2859 }
2860 }
2861 p += l;
2862 }
2863 else
2864#endif
2865 buf->b_spell_ismw[*p++] = TRUE;
2866}
2867
2868/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002869 * Find the region "region[2]" in "rp" (points to "sl_regions").
2870 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002871 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002872 */
2873 static int
2874find_region(rp, region)
2875 char_u *rp;
2876 char_u *region;
2877{
2878 int i;
2879
2880 for (i = 0; ; i += 2)
2881 {
2882 if (rp[i] == NUL)
2883 return REGION_ALL;
2884 if (rp[i] == region[0] && rp[i + 1] == region[1])
2885 break;
2886 }
2887 return i / 2;
2888}
2889
2890/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002891 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002892 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00002893 * Word WF_ONECAP
2894 * W WORD WF_ALLCAP
2895 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002896 */
2897 static int
2898captype(word, end)
2899 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002900 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002901{
2902 char_u *p;
2903 int c;
2904 int firstcap;
2905 int allcap;
2906 int past_second = FALSE; /* past second word char */
2907
2908 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002909 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002910 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002911 return 0; /* only non-word characters, illegal word */
2912#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00002913 if (has_mbyte)
2914 c = mb_ptr2char_adv(&p);
2915 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002916#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00002917 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002918 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002919
2920 /*
2921 * Need to check all letters to find a word with mixed upper/lower.
2922 * But a word with an upper char only at start is a ONECAP.
2923 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002924 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002925 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002926 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00002927 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00002928 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002929 {
2930 /* UUl -> KEEPCAP */
2931 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002932 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002933 allcap = FALSE;
2934 }
2935 else if (!allcap)
2936 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002937 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002938 past_second = TRUE;
2939 }
2940
2941 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002942 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002943 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002944 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002945 return 0;
2946}
2947
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002948# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
2949/*
2950 * Free all languages.
2951 */
2952 void
2953spell_free_all()
2954{
2955 slang_T *lp;
2956 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002957 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002958
2959 /* Go through all buffers and handle 'spelllang'. */
2960 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2961 ga_clear(&buf->b_langp);
2962
2963 while (first_lang != NULL)
2964 {
2965 lp = first_lang;
2966 first_lang = lp->sl_next;
2967 slang_free(lp);
2968 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002969
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002970 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00002971 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002972 /* Delete the internal wordlist and its .spl file */
2973 mch_remove(int_wordlist);
2974 int_wordlist_spl(fname);
2975 mch_remove(fname);
2976 vim_free(int_wordlist);
2977 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00002978 }
2979
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00002980 init_spell_chartab();
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002981}
2982# endif
2983
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002984# if defined(FEAT_MBYTE) || defined(PROTO)
2985/*
2986 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002987 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002988 */
2989 void
2990spell_reload()
2991{
2992 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00002993 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002994
Bram Moolenaarea408852005-06-25 22:49:46 +00002995 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002996 init_spell_chartab();
2997
2998 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002999 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003000
3001 /* Go through all buffers and handle 'spelllang'. */
3002 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
3003 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00003004 /* Only load the wordlists when 'spelllang' is set and there is a
3005 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003006 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00003007 {
3008 FOR_ALL_WINDOWS(wp)
3009 if (wp->w_buffer == buf && wp->w_p_spell)
3010 {
3011 (void)did_set_spelllang(buf);
3012# ifdef FEAT_WINDOWS
3013 break;
3014# endif
3015 }
3016 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003017 }
3018}
3019# endif
3020
Bram Moolenaarb765d632005-06-07 21:00:02 +00003021/*
3022 * Reload the spell file "fname" if it's loaded.
3023 */
3024 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003025spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00003026 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003027 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003028{
3029 slang_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003030 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003031
Bram Moolenaarb765d632005-06-07 21:00:02 +00003032 for (lp = first_lang; lp != NULL; lp = lp->sl_next)
3033 if (fullpathcmp(fname, lp->sl_fname, FALSE) == FPC_SAME)
3034 {
3035 slang_clear(lp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003036 (void)spell_load_file(fname, NULL, lp, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003037 redraw_all_later(NOT_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003038 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003039 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003040
3041 /* When "zg" was used and the file wasn't loaded yet, should redo
3042 * 'spelllang' to get it loaded. */
3043 if (added_word && !didit)
3044 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003045}
3046
3047
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003048/*
3049 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003050 */
3051
Bram Moolenaar51485f02005-06-04 21:55:20 +00003052#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003053 and .dic file. */
3054/*
3055 * Main structure to store the contents of a ".aff" file.
3056 */
3057typedef struct afffile_S
3058{
3059 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003060 int af_rar; /* RAR ID for rare word */
3061 int af_kep; /* KEP ID for keep-case word */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003062 int af_bad; /* BAD ID for banned word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003063 int af_pfxpostpone; /* postpone prefixes without chop string */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003064 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
3065 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003066} afffile_T;
3067
3068typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003069/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
3070struct affentry_S
3071{
3072 affentry_T *ae_next; /* next affix with same name/number */
3073 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
3074 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003075 char_u *ae_cond; /* condition (NULL for ".") */
3076 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003077 int ae_rare; /* rare affix */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003078};
3079
Bram Moolenaar53805d12005-08-01 07:08:33 +00003080#define AH_KEY_LEN 10
3081
Bram Moolenaar51485f02005-06-04 21:55:20 +00003082/* Affix header from ".aff" file. Used for af_pref and af_suff. */
3083typedef struct affheader_S
3084{
Bram Moolenaar53805d12005-08-01 07:08:33 +00003085 /* key for hashtable == name of affix entry */
3086#ifdef FEAT_MBYTE
3087 char_u ah_key[AH_KEY_LEN]; /* multi-byte char plus NUL */
3088#else
3089 char_u ah_key[2]; /* one byte char plus NUL */
3090#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003091 int ah_newID; /* prefix ID after renumbering */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003092 int ah_combine; /* suffix may combine with prefix */
3093 affentry_T *ah_first; /* first affix entry */
3094} affheader_T;
3095
3096#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
3097
3098/*
3099 * Structure that is used to store the items in the word tree. This avoids
3100 * the need to keep track of each allocated thing, it's freed all at once
3101 * after ":mkspell" is done.
3102 */
3103#define SBLOCKSIZE 16000 /* size of sb_data */
3104typedef struct sblock_S sblock_T;
3105struct sblock_S
3106{
3107 sblock_T *sb_next; /* next block in list */
3108 int sb_used; /* nr of bytes already in use */
3109 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003110};
3111
3112/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003113 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003114 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003115typedef struct wordnode_S wordnode_T;
3116struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003117{
Bram Moolenaar0c405862005-06-22 22:26:26 +00003118 union /* shared to save space */
3119 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003120 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00003121 int index; /* index in written nodes (valid after first
3122 round) */
3123 } wn_u1;
3124 union /* shared to save space */
3125 {
3126 wordnode_T *next; /* next node with same hash key */
3127 wordnode_T *wnode; /* parent node that will write this node */
3128 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003129 wordnode_T *wn_child; /* child (next byte in word) */
3130 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
3131 always sorted) */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003132 int wn_refs; /* nr of references to this node */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003133 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003134 char_u wn_prefixID; /* when "wn_byte" is NUL: supported/required
3135 prefix ID or 0 */
3136 short_u wn_flags; /* when "wn_byte" is NUL: WF_ flags */
3137 short wn_region; /* when "wn_byte" is NUL: region mask; for
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003138 PREFIXTREE it's the prefcondnr */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003139#ifdef SPELL_PRINTTREE
3140 int wn_nr; /* sequence nr for printing */
3141#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003142};
3143
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003144#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
3145
Bram Moolenaar51485f02005-06-04 21:55:20 +00003146#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003147
Bram Moolenaar51485f02005-06-04 21:55:20 +00003148/*
3149 * Info used while reading the spell files.
3150 */
3151typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003152{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003153 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003154 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003155 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00003156 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003157 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003158 sblock_T *si_blocks; /* memory blocks used */
3159 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003160 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003161 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003162 int si_region; /* region mask */
3163 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003164 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003165 int si_verbose; /* verbose messages */
Bram Moolenaar3982c542005-06-08 21:56:31 +00003166 int si_region_count; /* number of regions supported (1 when there
3167 are no regions) */
3168 char_u si_region_name[16]; /* region names (if count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003169
3170 garray_T si_rep; /* list of fromto_T entries from REP lines */
3171 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003172 char_u *si_sofofr; /* SOFOFROM text */
3173 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003174 int si_followup; /* soundsalike: ? */
3175 int si_collapse; /* soundsalike: ? */
3176 int si_rem_accents; /* soundsalike: remove accents */
3177 garray_T si_map; /* MAP info concatenated */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003178 char_u *si_midword; /* MIDWORD chars, alloc'ed string or NULL */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003179 garray_T si_prefcond; /* table with conditions for postponed
3180 * prefixes, each stored as a string */
3181 int si_newID; /* current value for ah_newID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003182} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003183
Bram Moolenaar51485f02005-06-04 21:55:20 +00003184static afffile_T *spell_read_aff __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003185static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003186static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
3187static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00003188static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003189static void spell_free_aff __ARGS((afffile_T *aff));
3190static int spell_read_dic __ARGS((char_u *fname, spellinfo_T *spin, afffile_T *affile));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003191static char_u *get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, sblock_T **blp));
3192static int store_aff_word __ARGS((char_u *word, spellinfo_T *spin, char_u *afflist, afffile_T *affile, hashtab_T *ht, hashtab_T *xht, int comb, int flags, char_u *pfxlist));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003193static int spell_read_wordfile __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003194static void *getroom __ARGS((sblock_T **blp, size_t len, int align));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003195static char_u *getroom_save __ARGS((sblock_T **blp, char_u *s));
3196static void free_blocks __ARGS((sblock_T *bl));
3197static wordnode_T *wordtree_alloc __ARGS((sblock_T **blp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003198static int store_word __ARGS((char_u *word, spellinfo_T *spin, int flags, int region, char_u *pfxlist));
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003199static int tree_add_word __ARGS((char_u *word, wordnode_T *tree, int flags, int region, int prefixID, spellinfo_T *spin));
3200static wordnode_T *get_wordnode __ARGS((sblock_T **blp));
3201static void free_wordnode __ARGS((wordnode_T *n));
Bram Moolenaarb765d632005-06-07 21:00:02 +00003202static void wordtree_compress __ARGS((wordnode_T *root, spellinfo_T *spin));
Bram Moolenaar51485f02005-06-04 21:55:20 +00003203static int node_compress __ARGS((wordnode_T *node, hashtab_T *ht, int *tot));
3204static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar3982c542005-06-08 21:56:31 +00003205static void write_vim_spell __ARGS((char_u *fname, spellinfo_T *spin));
Bram Moolenaar0c405862005-06-22 22:26:26 +00003206static void clear_node __ARGS((wordnode_T *node));
3207static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003208static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaarb765d632005-06-07 21:00:02 +00003209static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003210
Bram Moolenaar53805d12005-08-01 07:08:33 +00003211/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
3212 * but it must be negative to indicate the prefix tree to tree_add_word().
3213 * Use a negative number with the lower 8 bits zero. */
3214#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003215
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003216static int words_added = 0; /* number of words added to tree */
3217
3218#ifdef SPELL_PRINTTREE
3219/*
3220 * For debugging the tree code: print the current tree in a (more or less)
3221 * readable format, so that we can see what happens when adding a word and/or
3222 * compressing the tree.
3223 * Based on code from Olaf Seibert.
3224 */
3225#define PRINTLINESIZE 1000
3226#define PRINTWIDTH 6
3227
3228#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
3229 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
3230
3231static char line1[PRINTLINESIZE];
3232static char line2[PRINTLINESIZE];
3233static char line3[PRINTLINESIZE];
3234
3235 static void
3236spell_clear_flags(wordnode_T *node)
3237{
3238 wordnode_T *np;
3239
3240 for (np = node; np != NULL; np = np->wn_sibling)
3241 {
3242 np->wn_u1.index = FALSE;
3243 spell_clear_flags(np->wn_child);
3244 }
3245}
3246
3247 static void
3248spell_print_node(wordnode_T *node, int depth)
3249{
3250 if (node->wn_u1.index)
3251 {
3252 /* Done this node before, print the reference. */
3253 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
3254 PRINTSOME(line2, depth, " ", 0, 0);
3255 PRINTSOME(line3, depth, " ", 0, 0);
3256 msg(line1);
3257 msg(line2);
3258 msg(line3);
3259 }
3260 else
3261 {
3262 node->wn_u1.index = TRUE;
3263
3264 if (node->wn_byte != NUL)
3265 {
3266 if (node->wn_child != NULL)
3267 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
3268 else
3269 /* Cannot happen? */
3270 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
3271 }
3272 else
3273 PRINTSOME(line1, depth, " $ ", 0, 0);
3274
3275 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
3276
3277 if (node->wn_sibling != NULL)
3278 PRINTSOME(line3, depth, " | ", 0, 0);
3279 else
3280 PRINTSOME(line3, depth, " ", 0, 0);
3281
3282 if (node->wn_byte == NUL)
3283 {
3284 msg(line1);
3285 msg(line2);
3286 msg(line3);
3287 }
3288
3289 /* do the children */
3290 if (node->wn_byte != NUL && node->wn_child != NULL)
3291 spell_print_node(node->wn_child, depth + 1);
3292
3293 /* do the siblings */
3294 if (node->wn_sibling != NULL)
3295 {
3296 /* get rid of all parent details except | */
3297 STRCPY(line1, line3);
3298 STRCPY(line2, line3);
3299 spell_print_node(node->wn_sibling, depth);
3300 }
3301 }
3302}
3303
3304 static void
3305spell_print_tree(wordnode_T *root)
3306{
3307 if (root != NULL)
3308 {
3309 /* Clear the "wn_u1.index" fields, used to remember what has been
3310 * done. */
3311 spell_clear_flags(root);
3312
3313 /* Recursively print the tree. */
3314 spell_print_node(root, 0);
3315 }
3316}
3317#endif /* SPELL_PRINTTREE */
3318
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003319/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003320 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00003321 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003322 */
3323 static afffile_T *
Bram Moolenaar51485f02005-06-04 21:55:20 +00003324spell_read_aff(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003325 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003326 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003327{
3328 FILE *fd;
3329 afffile_T *aff;
3330 char_u rline[MAXLINELEN];
3331 char_u *line;
3332 char_u *pc = NULL;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003333#define MAXITEMCNT 7
3334 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003335 int itemcnt;
3336 char_u *p;
3337 int lnum = 0;
3338 affheader_T *cur_aff = NULL;
3339 int aff_todo = 0;
3340 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003341 char_u *low = NULL;
3342 char_u *fol = NULL;
3343 char_u *upp = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003344 static char *e_affname = N_("Affix name too long in %s line %d: %s");
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003345 int do_rep;
3346 int do_sal;
3347 int do_map;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003348 int do_midword;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003349 int do_sofo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003350 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003351 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003352 int l;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003353
Bram Moolenaar51485f02005-06-04 21:55:20 +00003354 /*
3355 * Open the file.
3356 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00003357 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003358 if (fd == NULL)
3359 {
3360 EMSG2(_(e_notopen), fname);
3361 return NULL;
3362 }
3363
Bram Moolenaarb765d632005-06-07 21:00:02 +00003364 if (spin->si_verbose || p_verbose > 2)
3365 {
3366 if (!spin->si_verbose)
3367 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003368 smsg((char_u *)_("Reading affix file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003369 out_flush();
3370 if (!spin->si_verbose)
3371 verbose_leave();
3372 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003373
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003374 /* Only do REP lines when not done in another .aff file already. */
3375 do_rep = spin->si_rep.ga_len == 0;
3376
3377 /* Only do SAL lines when not done in another .aff file already. */
3378 do_sal = spin->si_sal.ga_len == 0;
3379
3380 /* Only do MAP lines when not done in another .aff file already. */
3381 do_map = spin->si_map.ga_len == 0;
3382
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003383 /* Only do MIDWORD line when not done in another .aff file already */
3384 do_midword = spin->si_midword == NULL;
3385
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003386 /* Only do SOFOFROM and SOFOTO when not done in another .aff file already */
3387 do_sofo = spin->si_sofofr == NULL;
3388
Bram Moolenaar51485f02005-06-04 21:55:20 +00003389 /*
3390 * Allocate and init the afffile_T structure.
3391 */
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003392 aff = (afffile_T *)getroom(&spin->si_blocks, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003393 if (aff == NULL)
3394 return NULL;
3395 hash_init(&aff->af_pref);
3396 hash_init(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003397
3398 /*
3399 * Read all the lines in the file one by one.
3400 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003401 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003402 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003403 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003404 ++lnum;
3405
3406 /* Skip comment lines. */
3407 if (*rline == '#')
3408 continue;
3409
3410 /* Convert from "SET" to 'encoding' when needed. */
3411 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00003412#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003413 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003414 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003415 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003416 if (pc == NULL)
3417 {
3418 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
3419 fname, lnum, rline);
3420 continue;
3421 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003422 line = pc;
3423 }
3424 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00003425#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003426 {
3427 pc = NULL;
3428 line = rline;
3429 }
3430
3431 /* Split the line up in white separated items. Put a NUL after each
3432 * item. */
3433 itemcnt = 0;
3434 for (p = line; ; )
3435 {
3436 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
3437 ++p;
3438 if (*p == NUL)
3439 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00003440 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003441 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003442 items[itemcnt++] = p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003443 while (*p > ' ') /* skip until white space or CR/NL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003444 ++p;
3445 if (*p == NUL)
3446 break;
3447 *p++ = NUL;
3448 }
3449
3450 /* Handle non-empty lines. */
3451 if (itemcnt > 0)
3452 {
3453 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
3454 && aff->af_enc == NULL)
3455 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003456#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00003457 /* Setup for conversion from "ENC" to 'encoding'. */
3458 aff->af_enc = enc_canonize(items[1]);
3459 if (aff->af_enc != NULL && !spin->si_ascii
3460 && convert_setup(&spin->si_conv, aff->af_enc,
3461 p_enc) == FAIL)
3462 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
3463 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003464 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00003465#else
3466 smsg((char_u *)_("Conversion in %s not supported"), fname);
3467#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003468 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003469 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2)
3470 {
3471 if (do_midword)
3472 spin->si_midword = vim_strsave(items[1]);
3473 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00003474 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
3475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003476 /* ignored, we always split */
Bram Moolenaar50cde822005-06-05 21:54:54 +00003477 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003478 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003479 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003480 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003481 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003482 else if (STRCMP(items[0], "RAR") == 0 && itemcnt == 2
3483 && aff->af_rar == 0)
3484 {
3485 aff->af_rar = items[1][0];
3486 if (items[1][1] != NUL)
3487 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3488 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00003489 else if (STRCMP(items[0], "KEP") == 0 && itemcnt == 2
3490 && aff->af_kep == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003491 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00003492 aff->af_kep = items[1][0];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003493 if (items[1][1] != NUL)
3494 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3495 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00003496 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
3497 && aff->af_bad == 0)
3498 {
3499 aff->af_bad = items[1][0];
3500 if (items[1][1] != NUL)
3501 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
3502 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003503 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
3504 {
3505 aff->af_pfxpostpone = TRUE;
3506 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003507 else if ((STRCMP(items[0], "PFX") == 0
3508 || STRCMP(items[0], "SFX") == 0)
3509 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003510 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003511 {
Bram Moolenaar8db73182005-06-17 21:51:16 +00003512 /* Myspell allows extra text after the item, but that might
3513 * mean mistakes go unnoticed. Require a comment-starter. */
3514 if (itemcnt > 4 && *items[4] != '#')
3515 smsg((char_u *)_("Trailing text in %s line %d: %s"),
3516 fname, lnum, items[4]);
3517
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003518 /* New affix letter. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00003519 cur_aff = (affheader_T *)getroom(&spin->si_blocks,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003520 sizeof(affheader_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003521 if (cur_aff == NULL)
3522 break;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003523#ifdef FEAT_MBYTE
3524 if (has_mbyte)
3525 {
3526 l = mb_ptr2len_check(items[1]);
3527 if (l >= AH_KEY_LEN)
3528 l = 1; /* too long, must be an overlong sequence */
3529 else
3530 mch_memmove(cur_aff->ah_key, items[1], l);
3531 }
3532 else
3533#endif
3534 {
3535 *cur_aff->ah_key = *items[1];
3536 l = 1;
3537 }
3538 cur_aff->ah_key[l] = NUL;
3539 if (items[1][l] != NUL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00003540 smsg((char_u *)_(e_affname), fname, lnum, items[1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003541 if (*items[2] == 'Y')
3542 cur_aff->ah_combine = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003543 else if (*items[2] != 'N')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003544 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
3545 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003546
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003547 if (*items[0] == 'P')
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003548 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003549 tp = &aff->af_pref;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003550 /* Use a new number in the .spl file later, to be able to
3551 * handle multiple .aff files. */
3552 if (aff->af_pfxpostpone)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003553 cur_aff->ah_newID = ++spin->si_newID;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003554 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003555 else
3556 tp = &aff->af_suff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003557 aff_todo = atoi((char *)items[3]);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003558 hi = hash_find(tp, cur_aff->ah_key);
3559 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar51485f02005-06-04 21:55:20 +00003560 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003561 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
3562 fname, lnum, items[1]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003563 aff_todo = 0;
3564 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003565 else
3566 hash_add(tp, cur_aff->ah_key);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003567 }
3568 else if ((STRCMP(items[0], "PFX") == 0
3569 || STRCMP(items[0], "SFX") == 0)
3570 && aff_todo > 0
3571 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00003572 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003573 {
3574 affentry_T *aff_entry;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003575 int rare = FALSE;
Bram Moolenaar53805d12005-08-01 07:08:33 +00003576 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003577 int lasti = 5;
3578
3579 /* Check for "rare" after the other info. */
3580 if (itemcnt > 5 && STRICMP(items[5], "rare") == 0)
3581 {
3582 rare = TRUE;
3583 lasti = 6;
3584 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003585
Bram Moolenaar8db73182005-06-17 21:51:16 +00003586 /* Myspell allows extra text after the item, but that might
3587 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003588 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar8db73182005-06-17 21:51:16 +00003589 smsg((char_u *)_("Trailing text in %s line %d: %s"),
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003590 fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00003591
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003592 /* New item for an affix letter. */
3593 --aff_todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003594 aff_entry = (affentry_T *)getroom(&spin->si_blocks,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00003595 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003596 if (aff_entry == NULL)
3597 break;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003598 aff_entry->ae_rare = rare;
Bram Moolenaar5482f332005-04-17 20:18:43 +00003599
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003600 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003601 aff_entry->ae_chop = getroom_save(&spin->si_blocks,
3602 items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003603 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003604 aff_entry->ae_add = getroom_save(&spin->si_blocks,
3605 items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003606
Bram Moolenaar51485f02005-06-04 21:55:20 +00003607 /* Don't use an affix entry with non-ASCII characters when
3608 * "spin->si_ascii" is TRUE. */
3609 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00003610 || has_non_ascii(aff_entry->ae_add)))
3611 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00003612 aff_entry->ae_next = cur_aff->ah_first;
3613 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003614
3615 if (STRCMP(items[4], ".") != 0)
3616 {
3617 char_u buf[MAXLINELEN];
3618
3619 aff_entry->ae_cond = getroom_save(&spin->si_blocks,
3620 items[4]);
3621 if (*items[0] == 'P')
3622 sprintf((char *)buf, "^%s", items[4]);
3623 else
3624 sprintf((char *)buf, "%s$", items[4]);
3625 aff_entry->ae_prog = vim_regcomp(buf,
3626 RE_MAGIC + RE_STRING);
3627 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003628
3629 /* For postponed prefixes we need an entry in si_prefcond
3630 * for the condition. Use an existing one if possible. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00003631 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003632 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003633 /* When the chop string is one lower-case letter and
3634 * the add string ends in the upper-case letter we set
3635 * the "upper" flag, clear "ae_chop" and remove the
3636 * letters from "ae_add". The condition must either
3637 * be empty or start with the same letter. */
3638 if (aff_entry->ae_chop != NULL
3639 && aff_entry->ae_add != NULL
3640#ifdef FEAT_MBYTE
3641 && aff_entry->ae_chop[mb_ptr2len_check(
3642 aff_entry->ae_chop)] == NUL
3643#else
3644 && aff_entry->ae_chop[1] == NUL
3645#endif
3646 )
3647 {
3648 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003649
Bram Moolenaar53805d12005-08-01 07:08:33 +00003650 c = PTR2CHAR(aff_entry->ae_chop);
3651 c_up = SPELL_TOUPPER(c);
3652 if (c_up != c
3653 && (aff_entry->ae_cond == NULL
3654 || PTR2CHAR(aff_entry->ae_cond) == c))
3655 {
3656 p = aff_entry->ae_add
3657 + STRLEN(aff_entry->ae_add);
3658 mb_ptr_back(aff_entry->ae_add, p);
3659 if (PTR2CHAR(p) == c_up)
3660 {
3661 upper = TRUE;
3662 aff_entry->ae_chop = NULL;
3663 *p = NUL;
3664
3665 /* The condition is matched with the
3666 * actual word, thus must check for the
3667 * upper-case letter. */
3668 if (aff_entry->ae_cond != NULL)
3669 {
3670 char_u buf[MAXLINELEN];
3671#ifdef FEAT_MBYTE
3672 if (has_mbyte)
3673 {
3674 onecap_copy(items[4], buf, TRUE);
3675 aff_entry->ae_cond = getroom_save(
3676 &spin->si_blocks, buf);
3677 }
3678 else
3679#endif
3680 *aff_entry->ae_cond = c_up;
3681 if (aff_entry->ae_cond != NULL)
3682 {
3683 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003684 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00003685 vim_free(aff_entry->ae_prog);
3686 aff_entry->ae_prog = vim_regcomp(
3687 buf, RE_MAGIC + RE_STRING);
3688 }
3689 }
3690 }
3691 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003692 }
3693
Bram Moolenaar53805d12005-08-01 07:08:33 +00003694 if (aff_entry->ae_chop == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003695 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003696 int idx;
3697 char_u **pp;
3698 int n;
3699
3700 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
3701 --idx)
3702 {
3703 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
3704 if (str_equal(p, aff_entry->ae_cond))
3705 break;
3706 }
3707 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
3708 {
3709 /* Not found, add a new condition. */
3710 idx = spin->si_prefcond.ga_len++;
3711 pp = ((char_u **)spin->si_prefcond.ga_data)
3712 + idx;
3713 if (aff_entry->ae_cond == NULL)
3714 *pp = NULL;
3715 else
3716 *pp = getroom_save(&spin->si_blocks,
3717 aff_entry->ae_cond);
3718 }
3719
3720 /* Add the prefix to the prefix tree. */
3721 if (aff_entry->ae_add == NULL)
3722 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003723 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00003724 p = aff_entry->ae_add;
3725 /* PFX_FLAGS is a negative number, so that
3726 * tree_add_word() knows this is the prefix tree. */
3727 n = PFX_FLAGS;
3728 if (rare)
3729 n |= WFP_RARE;
3730 if (!cur_aff->ah_combine)
3731 n |= WFP_NC;
3732 if (upper)
3733 n |= WFP_UP;
3734 tree_add_word(p, spin->si_prefroot, n,
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00003735 idx, cur_aff->ah_newID, spin);
Bram Moolenaar53805d12005-08-01 07:08:33 +00003736 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003737 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00003738 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003739 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003740 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2)
3741 {
3742 if (fol != NULL)
3743 smsg((char_u *)_("Duplicate FOL in %s line %d"),
3744 fname, lnum);
3745 else
3746 fol = vim_strsave(items[1]);
3747 }
3748 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2)
3749 {
3750 if (low != NULL)
3751 smsg((char_u *)_("Duplicate LOW in %s line %d"),
3752 fname, lnum);
3753 else
3754 low = vim_strsave(items[1]);
3755 }
3756 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2)
3757 {
3758 if (upp != NULL)
3759 smsg((char_u *)_("Duplicate UPP in %s line %d"),
3760 fname, lnum);
3761 else
3762 upp = vim_strsave(items[1]);
3763 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003764 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003765 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003766 /* Ignore REP count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003767 if (!isdigit(*items[1]))
3768 smsg((char_u *)_("Expected REP count in %s line %d"),
3769 fname, lnum);
3770 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003771 else if (STRCMP(items[0], "REP") == 0 && itemcnt == 3)
3772 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003773 /* REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003774 if (do_rep)
3775 add_fromto(spin, &spin->si_rep, items[1], items[2]);
3776 }
3777 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
3778 {
3779 /* MAP item or count */
3780 if (!found_map)
3781 {
3782 /* First line contains the count. */
3783 found_map = TRUE;
3784 if (!isdigit(*items[1]))
3785 smsg((char_u *)_("Expected MAP count in %s line %d"),
3786 fname, lnum);
3787 }
3788 else if (do_map)
3789 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00003790 int c;
3791
3792 /* Check that every character appears only once. */
3793 for (p = items[1]; *p != NUL; )
3794 {
3795#ifdef FEAT_MBYTE
3796 c = mb_ptr2char_adv(&p);
3797#else
3798 c = *p++;
3799#endif
3800 if ((spin->si_map.ga_len > 0
3801 && vim_strchr(spin->si_map.ga_data, c)
3802 != NULL)
3803 || vim_strchr(p, c) != NULL)
3804 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
3805 fname, lnum);
3806 }
3807
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003808 /* We simply concatenate all the MAP strings, separated by
3809 * slashes. */
3810 ga_concat(&spin->si_map, items[1]);
3811 ga_append(&spin->si_map, '/');
3812 }
3813 }
3814 else if (STRCMP(items[0], "SAL") == 0 && itemcnt == 3)
3815 {
3816 if (do_sal)
3817 {
3818 /* SAL item (sounds-a-like)
3819 * Either one of the known keys or a from-to pair. */
3820 if (STRCMP(items[1], "followup") == 0)
3821 spin->si_followup = sal_to_bool(items[2]);
3822 else if (STRCMP(items[1], "collapse_result") == 0)
3823 spin->si_collapse = sal_to_bool(items[2]);
3824 else if (STRCMP(items[1], "remove_accents") == 0)
3825 spin->si_rem_accents = sal_to_bool(items[2]);
3826 else
3827 /* when "to" is "_" it means empty */
3828 add_fromto(spin, &spin->si_sal, items[1],
3829 STRCMP(items[2], "_") == 0 ? (char_u *)""
3830 : items[2]);
3831 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003832 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003833 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
3834 && (!do_sofo || spin->si_sofofr == NULL))
3835 {
3836 if (do_sofo)
3837 spin->si_sofofr = vim_strsave(items[1]);
3838 }
3839 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
3840 && (!do_sofo || spin->si_sofoto == NULL))
3841 {
3842 if (do_sofo)
3843 spin->si_sofoto = vim_strsave(items[1]);
3844 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003845 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003846 smsg((char_u *)_("Unrecognized item in %s line %d: %s"),
3847 fname, lnum, items[0]);
3848 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003849 }
3850
Bram Moolenaar42eeac32005-06-29 22:40:58 +00003851 if (do_sofo && (spin->si_sofofr == NULL) != (spin->si_sofoto == NULL))
3852 smsg((char_u *)_("Missing SOFO%s line in %s"),
3853 spin->si_sofofr == NULL ? "FROM" : "TO", fname);
3854 if (spin->si_sofofr != NULL && spin->si_sal.ga_len > 0)
3855 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
3856
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003857 if (fol != NULL || low != NULL || upp != NULL)
3858 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00003859 if (spin->si_clear_chartab)
3860 {
3861 /* Clear the char type tables, don't want to use any of the
3862 * currently used spell properties. */
3863 init_spell_chartab();
3864 spin->si_clear_chartab = FALSE;
3865 }
3866
Bram Moolenaar3982c542005-06-08 21:56:31 +00003867 /*
3868 * Don't write a word table for an ASCII file, so that we don't check
3869 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003870 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00003871 * mb_get_class(), the list of chars in the file will be incomplete.
3872 */
3873 if (!spin->si_ascii
3874#ifdef FEAT_MBYTE
3875 && !enc_utf8
3876#endif
3877 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003878 {
3879 if (fol == NULL || low == NULL || upp == NULL)
3880 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
3881 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00003882 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00003883 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003884
3885 vim_free(fol);
3886 vim_free(low);
3887 vim_free(upp);
3888 }
3889
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003890 vim_free(pc);
3891 fclose(fd);
3892 return aff;
3893}
3894
3895/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003896 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
3897 * NULL as equal.
3898 */
3899 static int
3900str_equal(s1, s2)
3901 char_u *s1;
3902 char_u *s2;
3903{
3904 if (s1 == NULL || s2 == NULL)
3905 return s1 == s2;
3906 return STRCMP(s1, s2) == 0;
3907}
3908
3909/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003910 * Add a from-to item to "gap". Used for REP and SAL items.
3911 * They are stored case-folded.
3912 */
3913 static void
3914add_fromto(spin, gap, from, to)
3915 spellinfo_T *spin;
3916 garray_T *gap;
3917 char_u *from;
3918 char_u *to;
3919{
3920 fromto_T *ftp;
3921 char_u word[MAXWLEN];
3922
3923 if (ga_grow(gap, 1) == OK)
3924 {
3925 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
3926 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
3927 ftp->ft_from = getroom_save(&spin->si_blocks, word);
3928 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
3929 ftp->ft_to = getroom_save(&spin->si_blocks, word);
3930 ++gap->ga_len;
3931 }
3932}
3933
3934/*
3935 * Convert a boolean argument in a SAL line to TRUE or FALSE;
3936 */
3937 static int
3938sal_to_bool(s)
3939 char_u *s;
3940{
3941 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
3942}
3943
3944/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00003945 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
3946 * When "s" is NULL FALSE is returned.
3947 */
3948 static int
3949has_non_ascii(s)
3950 char_u *s;
3951{
3952 char_u *p;
3953
3954 if (s != NULL)
3955 for (p = s; *p != NUL; ++p)
3956 if (*p >= 128)
3957 return TRUE;
3958 return FALSE;
3959}
3960
3961/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003962 * Free the structure filled by spell_read_aff().
3963 */
3964 static void
3965spell_free_aff(aff)
3966 afffile_T *aff;
3967{
3968 hashtab_T *ht;
3969 hashitem_T *hi;
3970 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003971 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003972 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003973
3974 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003975
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003976 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003977 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
3978 {
3979 todo = ht->ht_used;
3980 for (hi = ht->ht_array; todo > 0; ++hi)
3981 {
3982 if (!HASHITEM_EMPTY(hi))
3983 {
3984 --todo;
3985 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00003986 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
3987 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003988 }
3989 }
3990 if (ht == &aff->af_suff)
3991 break;
3992 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00003993
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003994 hash_clear(&aff->af_pref);
3995 hash_clear(&aff->af_suff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003996}
3997
3998/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003999 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004000 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004001 */
4002 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00004003spell_read_dic(fname, spin, affile)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004004 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004005 spellinfo_T *spin;
4006 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004007{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004008 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004009 char_u line[MAXLINELEN];
Bram Moolenaar51485f02005-06-04 21:55:20 +00004010 char_u *afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004011 char_u *pfxlist;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004012 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004013 char_u *pc;
4014 char_u *w;
4015 int l;
4016 hash_T hash;
4017 hashitem_T *hi;
4018 FILE *fd;
4019 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004020 int non_ascii = 0;
4021 int retval = OK;
4022 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004023 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004024
Bram Moolenaar51485f02005-06-04 21:55:20 +00004025 /*
4026 * Open the file.
4027 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004028 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004029 if (fd == NULL)
4030 {
4031 EMSG2(_(e_notopen), fname);
4032 return FAIL;
4033 }
4034
Bram Moolenaar51485f02005-06-04 21:55:20 +00004035 /* The hashtable is only used to detect duplicated words. */
4036 hash_init(&ht);
4037
Bram Moolenaar8db73182005-06-17 21:51:16 +00004038 spin->si_foldwcount = 0;
4039 spin->si_keepwcount = 0;
4040
Bram Moolenaarb765d632005-06-07 21:00:02 +00004041 if (spin->si_verbose || p_verbose > 2)
4042 {
4043 if (!spin->si_verbose)
4044 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004045 smsg((char_u *)_("Reading dictionary file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004046 out_flush();
4047 if (!spin->si_verbose)
4048 verbose_leave();
4049 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004050
4051 /* Read and ignore the first line: word count. */
4052 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004053 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004054 EMSG2(_("E760: No word count in %s"), fname);
4055
4056 /*
4057 * Read all the lines in the file one by one.
4058 * The words are converted to 'encoding' here, before being added to
4059 * the hashtable.
4060 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004061 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004062 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004063 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004064 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004065 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004066 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004067
Bram Moolenaar51485f02005-06-04 21:55:20 +00004068 /* Remove CR, LF and white space from the end. White space halfway
4069 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004070 l = STRLEN(line);
4071 while (l > 0 && line[l - 1] <= ' ')
4072 --l;
4073 if (l == 0)
4074 continue; /* empty line */
4075 line[l] = NUL;
4076
Bram Moolenaar51485f02005-06-04 21:55:20 +00004077 /* Find the optional affix names. */
4078 afflist = vim_strchr(line, '/');
4079 if (afflist != NULL)
4080 *afflist++ = NUL;
4081
4082 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
4083 if (spin->si_ascii && has_non_ascii(line))
4084 {
4085 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00004086 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004087 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00004088
Bram Moolenaarb765d632005-06-07 21:00:02 +00004089#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004090 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004091 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004092 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004093 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004094 if (pc == NULL)
4095 {
4096 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4097 fname, lnum, line);
4098 continue;
4099 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004100 w = pc;
4101 }
4102 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004103#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004104 {
4105 pc = NULL;
4106 w = line;
4107 }
4108
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004109 /* This takes time, print a message now and then. */
4110 if (spin->si_verbose && (lnum & 0x3ff) == 0)
4111 {
4112 vim_snprintf((char *)message, sizeof(message),
4113 _("line %6d, word %6d - %s"),
4114 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
4115 msg_start();
4116 msg_puts_long_attr(message, 0);
4117 msg_clr_eos();
4118 msg_didout = FALSE;
4119 msg_col = 0;
4120 out_flush();
4121 }
4122
Bram Moolenaar51485f02005-06-04 21:55:20 +00004123 /* Store the word in the hashtable to be able to find duplicates. */
4124 dw = (char_u *)getroom_save(&spin->si_blocks, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004125 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004126 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004127 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004128 if (retval == FAIL)
4129 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004130
Bram Moolenaar51485f02005-06-04 21:55:20 +00004131 hash = hash_hash(dw);
4132 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004133 if (!HASHITEM_EMPTY(hi))
4134 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004135 fname, lnum, dw);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004136 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00004137 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004138
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004139 flags = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004140 pfxlist = NULL;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004141 if (afflist != NULL)
4142 {
4143 /* Check for affix name that stands for keep-case word and stands
4144 * for rare word (if defined). */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004145 if (affile->af_kep != NUL
4146 && vim_strchr(afflist, affile->af_kep) != NULL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004147 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004148 if (affile->af_rar != NUL
4149 && vim_strchr(afflist, affile->af_rar) != NULL)
4150 flags |= WF_RARE;
Bram Moolenaar0c405862005-06-22 22:26:26 +00004151 if (affile->af_bad != NUL
4152 && vim_strchr(afflist, affile->af_bad) != NULL)
4153 flags |= WF_BANNED;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004154
4155 if (affile->af_pfxpostpone)
4156 /* Need to store the list of prefix IDs with the word. */
4157 pfxlist = get_pfxlist(affile, afflist, &spin->si_blocks);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004158 }
4159
Bram Moolenaar51485f02005-06-04 21:55:20 +00004160 /* Add the word to the word tree(s). */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004161 if (store_word(dw, spin, flags, spin->si_region, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004162 retval = FAIL;
4163
4164 if (afflist != NULL)
4165 {
4166 /* Find all matching suffixes and add the resulting words.
4167 * Additionally do matching prefixes that combine. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004168 if (store_aff_word(dw, spin, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004169 &affile->af_suff, &affile->af_pref,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004170 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004171 retval = FAIL;
4172
4173 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004174 if (store_aff_word(dw, spin, afflist, affile,
4175 &affile->af_pref, NULL,
4176 FALSE, flags, pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004177 retval = FAIL;
4178 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004179 }
4180
Bram Moolenaar51485f02005-06-04 21:55:20 +00004181 if (spin->si_ascii && non_ascii > 0)
4182 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
4183 non_ascii);
4184 hash_clear(&ht);
4185
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004186 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004187 return retval;
4188}
4189
4190/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004191 * Get the list of prefix IDs from the affix list "afflist".
4192 * Used for PFXPOSTPONE.
4193 * Returns a string allocated with getroom(). NULL when there are no prefixes
4194 * or when out of memory.
4195 */
4196 static char_u *
4197get_pfxlist(affile, afflist, blp)
4198 afffile_T *affile;
4199 char_u *afflist;
4200 sblock_T **blp;
4201{
4202 char_u *p;
4203 int cnt;
4204 int round;
4205 char_u *res = NULL;
4206 char_u key[2];
4207 hashitem_T *hi;
4208
4209 key[1] = NUL;
4210
4211 /* round 1: count the number of prefix IDs.
4212 * round 2: move prefix IDs to "res" */
4213 for (round = 1; round <= 2; ++round)
4214 {
4215 cnt = 0;
4216 for (p = afflist; *p != NUL; ++p)
4217 {
4218 key[0] = *p;
4219 hi = hash_find(&affile->af_pref, key);
4220 if (!HASHITEM_EMPTY(hi))
4221 {
4222 /* This is a prefix ID, use the new number. */
4223 if (round == 2)
4224 res[cnt] = HI2AH(hi)->ah_newID;
4225 ++cnt;
4226 }
4227 }
4228 if (round == 1 && cnt > 0)
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004229 res = getroom(blp, cnt + 1, FALSE);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004230 if (res == NULL)
4231 break;
4232 }
4233
4234 if (res != NULL)
4235 res[cnt] = NUL;
4236 return res;
4237}
4238
4239/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004240 * Apply affixes to a word and store the resulting words.
4241 * "ht" is the hashtable with affentry_T that need to be applied, either
4242 * prefixes or suffixes.
4243 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
4244 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004245 *
4246 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004247 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004248 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004249store_aff_word(word, spin, afflist, affile, ht, xht, comb, flags, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004250 char_u *word; /* basic word start */
4251 spellinfo_T *spin; /* spell info */
4252 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004253 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004254 hashtab_T *ht;
4255 hashtab_T *xht;
4256 int comb; /* only use affixes that combine */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004257 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004258 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004259{
4260 int todo;
4261 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004262 affheader_T *ah;
4263 affentry_T *ae;
4264 regmatch_T regmatch;
4265 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004266 int retval = OK;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004267 int i;
4268 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004269 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004270 char_u *use_pfxlist;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004271 int c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004272
Bram Moolenaar51485f02005-06-04 21:55:20 +00004273 todo = ht->ht_used;
4274 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004275 {
4276 if (!HASHITEM_EMPTY(hi))
4277 {
4278 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004279 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00004280
Bram Moolenaar51485f02005-06-04 21:55:20 +00004281 /* Check that the affix combines, if required, and that the word
4282 * supports this affix. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004283 c = PTR2CHAR(ah->ah_key);
4284 if ((!comb || ah->ah_combine) && vim_strchr(afflist, c) != NULL)
Bram Moolenaar5482f332005-04-17 20:18:43 +00004285 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004286 /* Loop over all affix entries with this name. */
4287 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004288 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004289 /* Check the condition. It's not logical to match case
4290 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004291 * Myspell.
4292 * For prefixes, when "PFXPOSTPONE" was used, only do
4293 * prefixes with a chop string. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004294 regmatch.regprog = ae->ae_prog;
4295 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004296 if ((xht != NULL || !affile->af_pfxpostpone
4297 || ae->ae_chop != NULL)
4298 && (ae->ae_prog == NULL
4299 || vim_regexec(&regmatch, word, (colnr_T)0)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004300 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004301 /* Match. Remove the chop and add the affix. */
4302 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004303 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004304 /* prefix: chop/add at the start of the word */
4305 if (ae->ae_add == NULL)
4306 *newword = NUL;
4307 else
4308 STRCPY(newword, ae->ae_add);
4309 p = word;
4310 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004311 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004312 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004313#ifdef FEAT_MBYTE
4314 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004315 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004316 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004317 for ( ; i > 0; --i)
4318 mb_ptr_adv(p);
4319 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00004320 else
4321#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004322 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004323 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004324 STRCAT(newword, p);
4325 }
4326 else
4327 {
4328 /* suffix: chop/add at the end of the word */
4329 STRCPY(newword, word);
4330 if (ae->ae_chop != NULL)
4331 {
4332 /* Remove chop string. */
4333 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004334 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004335 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004336 mb_ptr_back(newword, p);
4337 *p = NUL;
4338 }
4339 if (ae->ae_add != NULL)
4340 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004341 }
4342
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004343 /* Obey the "rare" flag of the affix. */
4344 if (ae->ae_rare)
4345 use_flags = flags | WF_RARE;
4346 else
4347 use_flags = flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004348 use_pfxlist = pfxlist;
4349
4350 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00004351 if (spin->si_prefroot != NULL
4352 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004353 {
4354 /* ... add a flag to indicate an affix was used. */
4355 use_flags |= WF_HAS_AFF;
4356
4357 /* ... don't use a prefix list if combining
4358 * affixes is not allowed */
4359 if (!ah->ah_combine || comb)
4360 use_pfxlist = NULL;
4361 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004362
Bram Moolenaar51485f02005-06-04 21:55:20 +00004363 /* Store the modified word. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004364 if (store_word(newword, spin, use_flags,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004365 spin->si_region, use_pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004366 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004367
Bram Moolenaar51485f02005-06-04 21:55:20 +00004368 /* When added a suffix and combining is allowed also
4369 * try adding prefixes additionally. */
4370 if (xht != NULL && ah->ah_combine)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004371 if (store_aff_word(newword, spin, afflist, affile,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004372 xht, NULL, TRUE,
4373 use_flags, use_pfxlist) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004374 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004375 }
4376 }
4377 }
4378 }
4379 }
4380
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004381 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004382}
4383
4384/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004385 * Read a file with a list of words.
4386 */
4387 static int
4388spell_read_wordfile(fname, spin)
4389 char_u *fname;
4390 spellinfo_T *spin;
4391{
4392 FILE *fd;
4393 long lnum = 0;
4394 char_u rline[MAXLINELEN];
4395 char_u *line;
4396 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004397 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004398 int l;
4399 int retval = OK;
4400 int did_word = FALSE;
4401 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004402 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004403 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004404
4405 /*
4406 * Open the file.
4407 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004408 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00004409 if (fd == NULL)
4410 {
4411 EMSG2(_(e_notopen), fname);
4412 return FAIL;
4413 }
4414
Bram Moolenaarb765d632005-06-07 21:00:02 +00004415 if (spin->si_verbose || p_verbose > 2)
4416 {
4417 if (!spin->si_verbose)
4418 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004419 smsg((char_u *)_("Reading word file %s ..."), fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004420 out_flush();
4421 if (!spin->si_verbose)
4422 verbose_leave();
4423 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004424
4425 /*
4426 * Read all the lines in the file one by one.
4427 */
4428 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
4429 {
4430 line_breakcheck();
4431 ++lnum;
4432
4433 /* Skip comment lines. */
4434 if (*rline == '#')
4435 continue;
4436
4437 /* Remove CR, LF and white space from the end. */
4438 l = STRLEN(rline);
4439 while (l > 0 && rline[l - 1] <= ' ')
4440 --l;
4441 if (l == 0)
4442 continue; /* empty or blank line */
4443 rline[l] = NUL;
4444
4445 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
4446 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004447#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00004448 if (spin->si_conv.vc_type != CONV_NONE)
4449 {
4450 pc = string_convert(&spin->si_conv, rline, NULL);
4451 if (pc == NULL)
4452 {
4453 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
4454 fname, lnum, rline);
4455 continue;
4456 }
4457 line = pc;
4458 }
4459 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00004460#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004461 {
4462 pc = NULL;
4463 line = rline;
4464 }
4465
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004466 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00004467 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004468 ++line;
4469 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004470 {
4471 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004472 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
4473 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004474 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004475 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
4476 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004477 else
4478 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00004479#ifdef FEAT_MBYTE
4480 char_u *enc;
4481
Bram Moolenaar51485f02005-06-04 21:55:20 +00004482 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004483 line += 10;
4484 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004485 if (enc != NULL && !spin->si_ascii
4486 && convert_setup(&spin->si_conv, enc,
4487 p_enc) == FAIL)
4488 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00004489 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004490 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004491 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004492#else
4493 smsg((char_u *)_("Conversion in %s not supported"), fname);
4494#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004495 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004496 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004497 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004498
Bram Moolenaar3982c542005-06-08 21:56:31 +00004499 if (STRNCMP(line, "regions=", 8) == 0)
4500 {
4501 if (spin->si_region_count > 1)
4502 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
4503 fname, lnum, line);
4504 else
4505 {
4506 line += 8;
4507 if (STRLEN(line) > 16)
4508 smsg((char_u *)_("Too many regions in %s line %d: %s"),
4509 fname, lnum, line);
4510 else
4511 {
4512 spin->si_region_count = STRLEN(line) / 2;
4513 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004514
4515 /* Adjust the mask for a word valid in all regions. */
4516 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004517 }
4518 }
4519 continue;
4520 }
4521
Bram Moolenaar7887d882005-07-01 22:33:52 +00004522 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
4523 fname, lnum, line - 1);
4524 continue;
4525 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004526
Bram Moolenaar7887d882005-07-01 22:33:52 +00004527 flags = 0;
4528 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004529
Bram Moolenaar7887d882005-07-01 22:33:52 +00004530 /* Check for flags and region after a slash. */
4531 p = vim_strchr(line, '/');
4532 if (p != NULL)
4533 {
4534 *p++ = NUL;
4535 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004536 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004537 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004538 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004539 else if (*p == '!') /* Bad, bad, wicked word. */
4540 flags |= WF_BANNED;
4541 else if (*p == '?') /* Rare word. */
4542 flags |= WF_RARE;
4543 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004544 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00004545 if ((flags & WF_REGION) == 0) /* first one */
4546 regionmask = 0;
4547 flags |= WF_REGION;
4548
4549 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00004550 if (l > spin->si_region_count)
4551 {
4552 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00004553 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004554 break;
4555 }
4556 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004557 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00004558 else
4559 {
4560 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
4561 fname, lnum, p);
4562 break;
4563 }
4564 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004565 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004566 }
4567
4568 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
4569 if (spin->si_ascii && has_non_ascii(line))
4570 {
4571 ++non_ascii;
4572 continue;
4573 }
4574
4575 /* Normal word: store it. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004576 if (store_word(line, spin, flags, regionmask, NULL) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004577 {
4578 retval = FAIL;
4579 break;
4580 }
4581 did_word = TRUE;
4582 }
4583
4584 vim_free(pc);
4585 fclose(fd);
4586
Bram Moolenaarb765d632005-06-07 21:00:02 +00004587 if (spin->si_ascii && non_ascii > 0 && (spin->si_verbose || p_verbose > 2))
4588 {
4589 if (p_verbose > 2)
4590 verbose_enter();
Bram Moolenaar51485f02005-06-04 21:55:20 +00004591 smsg((char_u *)_("Ignored %d words with non-ASCII characters"),
4592 non_ascii);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004593 if (p_verbose > 2)
4594 verbose_leave();
4595 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004596 return retval;
4597}
4598
4599/*
4600 * Get part of an sblock_T, "len" bytes long.
4601 * This avoids calling free() for every little struct we use.
4602 * The memory is cleared to all zeros.
4603 * Returns NULL when out of memory.
4604 */
4605 static void *
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004606getroom(blp, len, align)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004607 sblock_T **blp;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004608 size_t len; /* length needed */
4609 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004610{
4611 char_u *p;
4612 sblock_T *bl = *blp;
4613
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004614 if (align && bl != NULL)
4615 /* Round size up for alignment. On some systems structures need to be
4616 * aligned to the size of a pointer (e.g., SPARC). */
4617 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
4618 & ~(sizeof(char *) - 1);
4619
Bram Moolenaar51485f02005-06-04 21:55:20 +00004620 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
4621 {
4622 /* Allocate a block of memory. This is not freed until much later. */
4623 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
4624 if (bl == NULL)
4625 return NULL;
4626 bl->sb_next = *blp;
4627 *blp = bl;
4628 bl->sb_used = 0;
4629 }
4630
4631 p = bl->sb_data + bl->sb_used;
4632 bl->sb_used += len;
4633
4634 return p;
4635}
4636
4637/*
4638 * Make a copy of a string into memory allocated with getroom().
4639 */
4640 static char_u *
4641getroom_save(blp, s)
4642 sblock_T **blp;
4643 char_u *s;
4644{
4645 char_u *sc;
4646
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004647 sc = (char_u *)getroom(blp, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004648 if (sc != NULL)
4649 STRCPY(sc, s);
4650 return sc;
4651}
4652
4653
4654/*
4655 * Free the list of allocated sblock_T.
4656 */
4657 static void
4658free_blocks(bl)
4659 sblock_T *bl;
4660{
4661 sblock_T *next;
4662
4663 while (bl != NULL)
4664 {
4665 next = bl->sb_next;
4666 vim_free(bl);
4667 bl = next;
4668 }
4669}
4670
4671/*
4672 * Allocate the root of a word tree.
4673 */
4674 static wordnode_T *
4675wordtree_alloc(blp)
4676 sblock_T **blp;
4677{
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00004678 return (wordnode_T *)getroom(blp, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004679}
4680
4681/*
4682 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004683 * Always store it in the case-folded tree. For a keep-case word this is
4684 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
4685 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004686 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004687 * When "pfxlist" is not NULL store the word for each postponed prefix ID.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004688 */
4689 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004690store_word(word, spin, flags, region, pfxlist)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004691 char_u *word;
4692 spellinfo_T *spin;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004693 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004694 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004695 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004696{
4697 int len = STRLEN(word);
4698 int ct = captype(word, word + len);
4699 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004700 int res = OK;
4701 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004702
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004703 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004704 for (p = pfxlist; res == OK; ++p)
4705 {
4706 res = tree_add_word(foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004707 region, p == NULL ? 0 : *p, spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004708 if (p == NULL || *p == NUL)
4709 break;
4710 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004711 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004712
4713 if (res == OK && (ct == WF_KEEPCAP || flags & WF_KEEPCAP))
Bram Moolenaar8db73182005-06-17 21:51:16 +00004714 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004715 for (p = pfxlist; res == OK; ++p)
4716 {
4717 res = tree_add_word(word, spin->si_keeproot, flags,
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004718 region, p == NULL ? 0 : *p, spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004719 if (p == NULL || *p == NUL)
4720 break;
4721 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00004722 ++spin->si_keepwcount;
4723 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004724 return res;
4725}
4726
4727/*
4728 * Add word "word" to a word tree at "root".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004729 * When "flags" < 0 we are adding to the prefix tree where flags is used for
4730 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004731 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004732 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004733 static int
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004734tree_add_word(word, root, flags, region, prefixID, spin)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004735 char_u *word;
4736 wordnode_T *root;
4737 int flags;
4738 int region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004739 int prefixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004740 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004741{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004742 wordnode_T *node = root;
4743 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004744 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004745 wordnode_T **prev = NULL;
4746 int i;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004747 sblock_T **blp = &spin->si_blocks;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004748
Bram Moolenaar51485f02005-06-04 21:55:20 +00004749 /* Add each byte of the word to the tree, including the NUL at the end. */
4750 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004751 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004752 /* When there is more than one reference to this node we need to make
4753 * a copy, so that we can modify it. Copy the whole list of siblings
4754 * (we don't optimize for a partly shared list of siblings). */
4755 if (node != NULL && node->wn_refs > 1)
4756 {
4757 --node->wn_refs;
4758 copyprev = prev;
4759 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
4760 {
4761 /* Allocate a new node and copy the info. */
4762 np = get_wordnode(blp);
4763 if (np == NULL)
4764 return FAIL;
4765 np->wn_child = copyp->wn_child;
4766 if (np->wn_child != NULL)
4767 ++np->wn_child->wn_refs; /* child gets extra ref */
4768 np->wn_byte = copyp->wn_byte;
4769 if (np->wn_byte == NUL)
4770 {
4771 np->wn_flags = copyp->wn_flags;
4772 np->wn_region = copyp->wn_region;
4773 np->wn_prefixID = copyp->wn_prefixID;
4774 }
4775
4776 /* Link the new node in the list, there will be one ref. */
4777 np->wn_refs = 1;
4778 *copyprev = np;
4779 copyprev = &np->wn_sibling;
4780
4781 /* Let "node" point to the head of the copied list. */
4782 if (copyp == node)
4783 node = np;
4784 }
4785 }
4786
Bram Moolenaar51485f02005-06-04 21:55:20 +00004787 /* Look for the sibling that has the same character. They are sorted
4788 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004789 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004790 * done on flags and then on prefixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004791 while (node != NULL
4792 && (node->wn_byte < word[i]
4793 || (node->wn_byte == NUL
4794 && (flags < 0
4795 ? node->wn_prefixID < prefixID
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004796 : node->wn_flags < (flags & WN_MASK)
4797 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004798 && node->wn_prefixID < prefixID)))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004799 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004800 prev = &node->wn_sibling;
4801 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004802 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004803 if (node == NULL
4804 || node->wn_byte != word[i]
4805 || (word[i] == NUL
4806 && (flags < 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004807 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004808 || node->wn_prefixID != prefixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004809 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004810 /* Allocate a new node. */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004811 np = get_wordnode(blp);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004812 if (np == NULL)
4813 return FAIL;
4814 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004815
4816 /* If "node" is NULL this is a new child or the end of the sibling
4817 * list: ref count is one. Otherwise use ref count of sibling and
4818 * make ref count of sibling one (matters when inserting in front
4819 * of the list of siblings). */
4820 if (node == NULL)
4821 np->wn_refs = 1;
4822 else
4823 {
4824 np->wn_refs = node->wn_refs;
4825 node->wn_refs = 1;
4826 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004827 *prev = np;
4828 np->wn_sibling = node;
4829 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004830 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004831
Bram Moolenaar51485f02005-06-04 21:55:20 +00004832 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004833 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004834 node->wn_flags = flags;
4835 node->wn_region |= region;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004836 node->wn_prefixID = prefixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004837 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00004838 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00004839 prev = &node->wn_child;
4840 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004841 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004842#ifdef SPELL_PRINTTREE
4843 smsg("Added \"%s\"", word);
4844 spell_print_tree(root->wn_sibling);
4845#endif
4846
4847 /*
4848 * Every so many words compress the tree, so that we don't use too much
4849 * memory.
4850 */
4851 if (++words_added >= SPELL_COMPRESS_CNT)
4852 {
4853 words_added = 0;
4854
4855 msg_start();
4856 msg_puts((char_u *)_(msg_compressing));
4857 msg_clr_eos();
4858 msg_didout = FALSE;
4859 msg_col = 0;
4860 out_flush();
4861 wordtree_compress(root->wn_sibling, spin);
4862 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004863
4864 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004865}
4866
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004867/* We keep a list of nodes that have been freed during compression. They are
4868 * re-used when adding a new node. The "wn_child" fields links them. */
4869static wordnode_T *first_free_node = NULL;
4870#ifdef SPELL_PRINTTREE
4871static int wordnode_nr = 0;
4872#endif
4873
4874/*
4875 * Get a wordnode_T, either from the list of previously freed nodes or
4876 * allocate a new one.
4877 */
4878 static wordnode_T *
4879get_wordnode(blp)
4880 sblock_T **blp;
4881{
4882 wordnode_T *n;
4883
4884 if (first_free_node == NULL)
4885 n = (wordnode_T *)getroom(blp, sizeof(wordnode_T), TRUE);
4886 else
4887 {
4888 n = first_free_node;
4889 first_free_node = n->wn_child;
4890 vim_memset(n, 0, sizeof(wordnode_T));
4891 }
4892#ifdef SPELL_PRINTTREE
4893 n->wn_nr = ++wordnode_nr;
4894#endif
4895 return n;
4896}
4897
4898/*
4899 * Free a wordnode_T for re-use later.
4900 */
4901 static void
4902free_wordnode(n)
4903 wordnode_T *n;
4904{
4905 n->wn_child = first_free_node;
4906 first_free_node = n;
4907}
4908
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004909/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004910 * Compress a tree: find tails that are identical and can be shared.
4911 */
4912 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00004913wordtree_compress(root, spin)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004914 wordnode_T *root;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004915 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004916{
4917 hashtab_T ht;
4918 int n;
4919 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004920 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004921
4922 if (root != NULL)
4923 {
4924 hash_init(&ht);
4925 n = node_compress(root, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004926
4927#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004928 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004929#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004930 {
4931 if (!spin->si_verbose)
4932 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004933 if (tot > 1000000)
4934 perc = (tot - n) / (tot / 100);
4935 else
4936 perc = (tot - n) * 100 / tot;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004937 smsg((char_u *)_("Compressed %d of %d nodes; %d%% remaining"),
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004938 n, tot, perc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004939 if (p_verbose > 2)
4940 verbose_leave();
4941 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004942#ifdef SPELL_PRINTTREE
4943 spell_print_tree(root);
4944#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00004945 hash_clear(&ht);
4946 }
4947}
4948
4949/*
4950 * Compress a node, its siblings and its children, depth first.
4951 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004952 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00004953 static int
Bram Moolenaar51485f02005-06-04 21:55:20 +00004954node_compress(node, ht, tot)
4955 wordnode_T *node;
4956 hashtab_T *ht;
4957 int *tot; /* total count of nodes before compressing,
4958 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004959{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004960 wordnode_T *np;
4961 wordnode_T *tp;
4962 wordnode_T *child;
4963 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004964 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004965 int len = 0;
4966 unsigned nr, n;
4967 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004968
Bram Moolenaar51485f02005-06-04 21:55:20 +00004969 /*
4970 * Go through the list of siblings. Compress each child and then try
4971 * finding an identical child to replace it.
4972 * Note that with "child" we mean not just the node that is pointed to,
4973 * but the whole list of siblings, of which the node is the first.
4974 */
4975 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004976 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004977 ++len;
4978 if ((child = np->wn_child) != NULL)
4979 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00004980 /* Compress the child. This fills hashkey. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004981 compressed += node_compress(child, ht, tot);
4982
4983 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004984 hash = hash_hash(child->wn_u1.hashkey);
4985 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004986 tp = NULL;
4987 if (!HASHITEM_EMPTY(hi))
4988 {
4989 /* There are children with an identical hash value. Now check
4990 * if there is one that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004991 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004992 if (node_equal(child, tp))
4993 {
4994 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004995 * current one. This means the current child and all
4996 * its siblings is unlinked from the tree. */
4997 ++tp->wn_refs;
4998 --child->wn_refs;
4999 if (child->wn_refs == 0)
5000 for (; child != NULL; child = child->wn_sibling)
5001 {
5002 if (child->wn_child != NULL)
5003 --child->wn_child->wn_refs;
5004 free_wordnode(child);
5005 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005006 np->wn_child = tp;
5007 ++compressed;
5008 break;
5009 }
5010 if (tp == NULL)
5011 {
5012 /* No other child with this hash value equals the child of
5013 * the node, add it to the linked list after the first
5014 * item. */
5015 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005016 child->wn_u2.next = tp->wn_u2.next;
5017 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005018 }
5019 }
5020 else
5021 /* No other child has this hash value, add it to the
5022 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005023 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005024 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005025 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005026 *tot += len;
5027
5028 /*
5029 * Make a hash key for the node and its siblings, so that we can quickly
5030 * find a lookalike node. This must be done after compressing the sibling
5031 * list, otherwise the hash key would become invalid by the compression.
5032 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005033 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005034 nr = 0;
5035 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005036 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005037 if (np->wn_byte == NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005038 /* end node: use wn_flags, wn_region and wn_prefixID */
5039 n = np->wn_flags + (np->wn_region << 8) + (np->wn_prefixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005040 else
5041 /* byte node: use the byte value and the child pointer */
5042 n = np->wn_byte + ((long_u)np->wn_child << 8);
5043 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005044 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005045
5046 /* Avoid NUL bytes, it terminates the hash key. */
5047 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005048 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005049 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005050 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005051 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005052 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005053 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00005054 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
5055 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005056
5057 return compressed;
5058}
5059
5060/*
5061 * Return TRUE when two nodes have identical siblings and children.
5062 */
5063 static int
5064node_equal(n1, n2)
5065 wordnode_T *n1;
5066 wordnode_T *n2;
5067{
5068 wordnode_T *p1;
5069 wordnode_T *p2;
5070
5071 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
5072 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
5073 if (p1->wn_byte != p2->wn_byte
5074 || (p1->wn_byte == NUL
5075 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005076 || p1->wn_region != p2->wn_region
5077 || p1->wn_prefixID != p2->wn_prefixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005078 : (p1->wn_child != p2->wn_child)))
5079 break;
5080
5081 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005082}
5083
5084/*
5085 * Write a number to file "fd", MSB first, in "len" bytes.
5086 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005087 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005088put_bytes(fd, nr, len)
5089 FILE *fd;
5090 long_u nr;
5091 int len;
5092{
5093 int i;
5094
5095 for (i = len - 1; i >= 0; --i)
5096 putc((int)(nr >> (i * 8)), fd);
5097}
5098
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005099static int
5100#ifdef __BORLANDC__
5101_RTLENTRYF
5102#endif
5103rep_compare __ARGS((const void *s1, const void *s2));
5104
5105/*
5106 * Function given to qsort() to sort the REP items on "from" string.
5107 */
5108 static int
5109#ifdef __BORLANDC__
5110_RTLENTRYF
5111#endif
5112rep_compare(s1, s2)
5113 const void *s1;
5114 const void *s2;
5115{
5116 fromto_T *p1 = (fromto_T *)s1;
5117 fromto_T *p2 = (fromto_T *)s2;
5118
5119 return STRCMP(p1->ft_from, p2->ft_from);
5120}
5121
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005122/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005123 * Write the Vim spell file "fname".
5124 */
5125 static void
Bram Moolenaar3982c542005-06-08 21:56:31 +00005126write_vim_spell(fname, spin)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005127 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005128 spellinfo_T *spin;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005129{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005130 FILE *fd;
5131 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005132 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005133 wordnode_T *tree;
5134 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005135 int i;
5136 int l;
5137 garray_T *gap;
5138 fromto_T *ftp;
5139 char_u *p;
5140 int rr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005141
Bram Moolenaarb765d632005-06-07 21:00:02 +00005142 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00005143 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005144 {
5145 EMSG2(_(e_notopen), fname);
5146 return;
5147 }
5148
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005149 /* <HEADER>: <fileID> <regioncnt> <regionname> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005150 * <charflagslen> <charflags>
5151 * <fcharslen> <fchars>
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005152 * <midwordlen> <midword>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005153 * <prefcondcnt> <prefcond> ... */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005154
5155 /* <fileID> */
5156 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
5157 EMSG(_(e_write));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005158
5159 /* write the region names if there is more than one */
Bram Moolenaar3982c542005-06-08 21:56:31 +00005160 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005161 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00005162 putc(spin->si_region_count, fd); /* <regioncnt> <regionname> ... */
5163 fwrite(spin->si_region_name, (size_t)(spin->si_region_count * 2),
5164 (size_t)1, fd);
5165 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005166 }
5167 else
5168 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005169 putc(0, fd);
5170 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005171 }
5172
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005173 /*
5174 * Write the table with character flags and table for case folding.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005175 * <charflagslen> <charflags> <fcharlen> <fchars>
5176 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005177 * 'encoding'.
5178 * Also skip this for an .add.spl file, the main spell file must contain
5179 * the table (avoids that it conflicts). File is shorter too.
5180 */
5181 if (spin->si_ascii || spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005182 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005183 putc(0, fd);
5184 putc(0, fd);
5185 putc(0, fd);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005186 }
5187 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00005188 write_spell_chartab(fd);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005189
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005190
5191 if (spin->si_midword == NULL)
5192 put_bytes(fd, 0L, 2); /* <midwordlen> */
5193 else
5194 {
5195 i = STRLEN(spin->si_midword);
5196 put_bytes(fd, (long_u)i, 2); /* <midwordlen> */
5197 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
5198 }
5199
5200
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005201 /* Write the prefix conditions. */
5202 write_spell_prefcond(fd, &spin->si_prefcond);
5203
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005204 /* <SUGGEST> : <repcount> <rep> ...
5205 * <salflags> <salcount> <sal> ...
5206 * <maplen> <mapstr> */
5207
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005208 /* Sort the REP items. */
5209 qsort(spin->si_rep.ga_data, (size_t)spin->si_rep.ga_len,
5210 sizeof(fromto_T), rep_compare);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005211
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005212 /* round 1: REP items
5213 * round 2: SAL items (unless SOFO is used) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005214 for (round = 1; round <= 2; ++round)
5215 {
5216 if (round == 1)
5217 gap = &spin->si_rep;
5218 else
5219 {
5220 gap = &spin->si_sal;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005221
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005222 i = 0;
5223 if (spin->si_followup)
5224 i |= SAL_F0LLOWUP;
5225 if (spin->si_collapse)
5226 i |= SAL_COLLAPSE;
5227 if (spin->si_rem_accents)
5228 i |= SAL_REM_ACCENTS;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005229 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
5230 i |= SAL_SOFO;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005231 putc(i, fd); /* <salflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005232 if (i & SAL_SOFO)
5233 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005234 }
5235
5236 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
5237 for (i = 0; i < gap->ga_len; ++i)
5238 {
5239 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
5240 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
5241 ftp = &((fromto_T *)gap->ga_data)[i];
5242 for (rr = 1; rr <= 2; ++rr)
5243 {
5244 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
5245 l = STRLEN(p);
5246 putc(l, fd);
5247 fwrite(p, l, (size_t)1, fd);
5248 }
5249 }
5250 }
5251
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005252 /* SOFOFROM and SOFOTO */
5253 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
5254 {
5255 put_bytes(fd, 1L, 2); /* <salcount> */
5256
5257 l = STRLEN(spin->si_sofofr);
5258 put_bytes(fd, (long_u)l, 2); /* <salfromlen> */
5259 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <salfrom> */
5260
5261 l = STRLEN(spin->si_sofoto);
5262 put_bytes(fd, (long_u)l, 2); /* <saltolen> */
5263 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <salto> */
5264 }
5265
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005266 put_bytes(fd, (long_u)spin->si_map.ga_len, 2); /* <maplen> */
5267 if (spin->si_map.ga_len > 0) /* <mapstr> */
5268 fwrite(spin->si_map.ga_data, (size_t)spin->si_map.ga_len,
5269 (size_t)1, fd);
Bram Moolenaar50cde822005-06-05 21:54:54 +00005270
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005271 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005272 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005273 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005274 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005275 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005276 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005277 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005278 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005279 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005280 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005281 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005282 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005283
Bram Moolenaar0c405862005-06-22 22:26:26 +00005284 /* Clear the index and wnode fields in the tree. */
5285 clear_node(tree);
5286
Bram Moolenaar51485f02005-06-04 21:55:20 +00005287 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00005288 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00005289 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005290 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005291
Bram Moolenaar51485f02005-06-04 21:55:20 +00005292 /* number of nodes in 4 bytes */
5293 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00005294 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005295
Bram Moolenaar51485f02005-06-04 21:55:20 +00005296 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005297 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005298 }
5299
Bram Moolenaar51485f02005-06-04 21:55:20 +00005300 fclose(fd);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005301}
5302
5303/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00005304 * Clear the index and wnode fields of "node", it siblings and its
5305 * children. This is needed because they are a union with other items to save
5306 * space.
5307 */
5308 static void
5309clear_node(node)
5310 wordnode_T *node;
5311{
5312 wordnode_T *np;
5313
5314 if (node != NULL)
5315 for (np = node; np != NULL; np = np->wn_sibling)
5316 {
5317 np->wn_u1.index = 0;
5318 np->wn_u2.wnode = NULL;
5319
5320 if (np->wn_byte != NUL)
5321 clear_node(np->wn_child);
5322 }
5323}
5324
5325
5326/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005327 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005328 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00005329 * This first writes the list of possible bytes (siblings). Then for each
5330 * byte recursively write the children.
5331 *
5332 * NOTE: The code here must match the code in read_tree(), since assumptions
5333 * are made about the indexes (so that we don't have to write them in the
5334 * file).
5335 *
5336 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005337 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005338 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00005339put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005340 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005341 wordnode_T *node;
5342 int index;
5343 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005344 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005345{
Bram Moolenaar51485f02005-06-04 21:55:20 +00005346 int newindex = index;
5347 int siblingcount = 0;
5348 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005349 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005350
Bram Moolenaar51485f02005-06-04 21:55:20 +00005351 /* If "node" is zero the tree is empty. */
5352 if (node == NULL)
5353 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005354
Bram Moolenaar51485f02005-06-04 21:55:20 +00005355 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005356 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005357
5358 /* Count the number of siblings. */
5359 for (np = node; np != NULL; np = np->wn_sibling)
5360 ++siblingcount;
5361
5362 /* Write the sibling count. */
5363 if (fd != NULL)
5364 putc(siblingcount, fd); /* <siblingcount> */
5365
5366 /* Write each sibling byte and optionally extra info. */
5367 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005368 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005369 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005370 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005371 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005372 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005373 /* For a NUL byte (end of word) write the flags etc. */
5374 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005375 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005376 /* In PREFIXTREE write the required prefixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005377 * associated condition nr (stored in wn_region). The
5378 * byte value is misused to store the "rare" and "not
5379 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005380 if (np->wn_flags == (short_u)PFX_FLAGS)
5381 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005382 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005383 {
5384 putc(BY_FLAGS, fd); /* <byte> */
5385 putc(np->wn_flags, fd); /* <pflags> */
5386 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005387 putc(np->wn_prefixID, fd); /* <prefixID> */
5388 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005389 }
5390 else
5391 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005392 /* For word trees we write the flag/region items. */
5393 flags = np->wn_flags;
5394 if (regionmask != 0 && np->wn_region != regionmask)
5395 flags |= WF_REGION;
5396 if (np->wn_prefixID != 0)
5397 flags |= WF_PFX;
5398 if (flags == 0)
5399 {
5400 /* word without flags or region */
5401 putc(BY_NOFLAGS, fd); /* <byte> */
5402 }
5403 else
5404 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005405 if (np->wn_flags >= 0x100)
5406 {
5407 putc(BY_FLAGS2, fd); /* <byte> */
5408 putc(flags, fd); /* <flags> */
5409 putc((unsigned)flags >> 8, fd); /* <flags2> */
5410 }
5411 else
5412 {
5413 putc(BY_FLAGS, fd); /* <byte> */
5414 putc(flags, fd); /* <flags> */
5415 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005416 if (flags & WF_REGION)
5417 putc(np->wn_region, fd); /* <region> */
5418 if (flags & WF_PFX)
5419 putc(np->wn_prefixID, fd); /* <prefixID> */
5420 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005421 }
5422 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00005423 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005424 else
5425 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005426 if (np->wn_child->wn_u1.index != 0
5427 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005428 {
5429 /* The child is written elsewhere, write the reference. */
5430 if (fd != NULL)
5431 {
5432 putc(BY_INDEX, fd); /* <byte> */
5433 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005434 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005435 }
5436 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005437 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005438 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00005439 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005440
Bram Moolenaar51485f02005-06-04 21:55:20 +00005441 if (fd != NULL)
5442 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
5443 {
5444 EMSG(_(e_write));
5445 return 0;
5446 }
5447 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005448 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005449
5450 /* Space used in the array when reading: one for each sibling and one for
5451 * the count. */
5452 newindex += siblingcount + 1;
5453
5454 /* Recursively dump the children of each sibling. */
5455 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005456 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
5457 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005458 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005459
5460 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005461}
5462
5463
5464/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00005465 * ":mkspell [-ascii] outfile infile ..."
5466 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005467 */
5468 void
5469ex_mkspell(eap)
5470 exarg_T *eap;
5471{
5472 int fcount;
5473 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005474 char_u *arg = eap->arg;
5475 int ascii = FALSE;
5476
5477 if (STRNCMP(arg, "-ascii", 6) == 0)
5478 {
5479 ascii = TRUE;
5480 arg = skipwhite(arg + 6);
5481 }
5482
5483 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
5484 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
5485 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005486 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005487 FreeWild(fcount, fnames);
5488 }
5489}
5490
5491/*
5492 * Create a Vim spell file from one or more word lists.
5493 * "fnames[0]" is the output file name.
5494 * "fnames[fcount - 1]" is the last input file name.
5495 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
5496 * and ".spl" is appended to make the output file name.
5497 */
5498 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005499mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005500 int fcount;
5501 char_u **fnames;
5502 int ascii; /* -ascii argument given */
5503 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005504 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005505{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005506 char_u fname[MAXPATHL];
5507 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00005508 char_u **innames;
5509 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005510 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005511 int i;
5512 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005513 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005514 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005515 spellinfo_T spin;
5516
5517 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005518 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005519 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005520 spin.si_followup = TRUE;
5521 spin.si_rem_accents = TRUE;
5522 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
5523 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
5524 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005525 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005526 first_free_node = NULL;
5527#ifdef SPELL_PRINTTREE
5528 wordnode_nr = 0;
5529#endif
5530 words_added = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005531
Bram Moolenaarb765d632005-06-07 21:00:02 +00005532 /* default: fnames[0] is output file, following are input files */
5533 innames = &fnames[1];
5534 incount = fcount - 1;
5535
5536 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005537 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005538 len = STRLEN(fnames[0]);
5539 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
5540 {
5541 /* For ":mkspell path/en.latin1.add" output file is
5542 * "path/en.latin1.add.spl". */
5543 innames = &fnames[0];
5544 incount = 1;
5545 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
5546 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005547 else if (fcount == 1)
5548 {
5549 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
5550 innames = &fnames[0];
5551 incount = 1;
5552 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5553 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5554 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005555 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
5556 {
5557 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005558 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005559 }
5560 else
5561 /* Name should be language, make the file name from it. */
5562 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
5563 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
5564
5565 /* Check for .ascii.spl. */
5566 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
5567 spin.si_ascii = TRUE;
5568
5569 /* Check for .add.spl. */
5570 if (strstr((char *)gettail(wfname), ".add.") != NULL)
5571 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005572 }
5573
Bram Moolenaarb765d632005-06-07 21:00:02 +00005574 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005575 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005576 else if (vim_strchr(gettail(wfname), '_') != NULL)
5577 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005578 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005579 EMSG(_("E754: Only up to 8 regions supported"));
5580 else
5581 {
5582 /* Check for overwriting before doing things that may take a lot of
5583 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005584 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005585 {
5586 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005587 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005588 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005589 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005590 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005591 EMSG2(_(e_isadir2), wfname);
5592 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005593 }
5594
5595 /*
5596 * Init the aff and dic pointers.
5597 * Get the region names if there are more than 2 arguments.
5598 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005599 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005600 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005601 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005602
Bram Moolenaar3982c542005-06-08 21:56:31 +00005603 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005604 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005605 len = STRLEN(innames[i]);
5606 if (STRLEN(gettail(innames[i])) < 5
5607 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005608 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005609 EMSG2(_("E755: Invalid region in %s"), innames[i]);
5610 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005611 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005612 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
5613 spin.si_region_name[i * 2 + 1] =
5614 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005615 }
5616 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00005617 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005618
Bram Moolenaar51485f02005-06-04 21:55:20 +00005619 spin.si_foldroot = wordtree_alloc(&spin.si_blocks);
5620 spin.si_keeproot = wordtree_alloc(&spin.si_blocks);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005621 spin.si_prefroot = wordtree_alloc(&spin.si_blocks);
5622 if (spin.si_foldroot == NULL
5623 || spin.si_keeproot == NULL
5624 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005625 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005626 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005627 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005628 }
5629
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005630 /* When not producing a .add.spl file clear the character table when
5631 * we encounter one in the .aff file. This means we dump the current
5632 * one in the .spl file if the .aff file doesn't define one. That's
5633 * better than guessing the contents, the table will match a
5634 * previously loaded spell file. */
5635 if (!spin.si_add)
5636 spin.si_clear_chartab = TRUE;
5637
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005638 /*
5639 * Read all the .aff and .dic files.
5640 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00005641 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005642 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005643 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005644 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005645 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005646 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005647
Bram Moolenaarb765d632005-06-07 21:00:02 +00005648 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005649 if (mch_stat((char *)fname, &st) >= 0)
5650 {
5651 /* Read the .aff file. Will init "spin->si_conv" based on the
5652 * "SET" line. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005653 afile[i] = spell_read_aff(fname, &spin);
5654 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005655 error = TRUE;
5656 else
5657 {
5658 /* Read the .dic file and store the words in the trees. */
5659 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00005660 innames[i]);
5661 if (spell_read_dic(fname, &spin, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005662 error = TRUE;
5663 }
5664 }
5665 else
5666 {
5667 /* No .aff file, try reading the file as a word list. Store
5668 * the words in the trees. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005669 if (spell_read_wordfile(innames[i], &spin) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005670 error = TRUE;
5671 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005672
Bram Moolenaarb765d632005-06-07 21:00:02 +00005673#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005674 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005675 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005676#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005677 }
5678
Bram Moolenaar51485f02005-06-04 21:55:20 +00005679 if (!error)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005680 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005681 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00005682 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005683 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005684 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005685 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005686 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005687 verbose_enter();
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005688 MSG(_(msg_compressing));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005689 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005690 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005691 verbose_leave();
5692 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005693 wordtree_compress(spin.si_foldroot->wn_sibling, &spin);
5694 wordtree_compress(spin.si_keeproot->wn_sibling, &spin);
5695 wordtree_compress(spin.si_prefroot->wn_sibling, &spin);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005696 }
5697
Bram Moolenaar51485f02005-06-04 21:55:20 +00005698 if (!error)
5699 {
5700 /*
5701 * Write the info in the spell file.
5702 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005703 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005704 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005705 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005706 verbose_enter();
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005707 smsg((char_u *)_("Writing spell file %s ..."), wfname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005708 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005709 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005710 verbose_leave();
5711 }
Bram Moolenaar50cde822005-06-05 21:54:54 +00005712
Bram Moolenaar3982c542005-06-08 21:56:31 +00005713 write_vim_spell(wfname, &spin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005714
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005715 if (!added_word || p_verbose > 2)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005717 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005718 verbose_enter();
5719 MSG(_("Done!"));
5720 smsg((char_u *)_("Estimated runtime memory use: %d bytes"),
Bram Moolenaar50cde822005-06-05 21:54:54 +00005721 spin.si_memtot);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005722 out_flush();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005723 if (added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005724 verbose_leave();
5725 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005726
Bram Moolenaarb765d632005-06-07 21:00:02 +00005727 /* If the file is loaded need to reload it. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005728 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005729 }
5730
5731 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005732 ga_clear(&spin.si_rep);
5733 ga_clear(&spin.si_sal);
5734 ga_clear(&spin.si_map);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005735 ga_clear(&spin.si_prefcond);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005736 vim_free(spin.si_midword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005737 vim_free(spin.si_sofofr);
5738 vim_free(spin.si_sofoto);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005739
5740 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005741 for (i = 0; i < incount; ++i)
5742 if (afile[i] != NULL)
5743 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005744
5745 /* Free all the bits and pieces at once. */
5746 free_blocks(spin.si_blocks);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005747 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005748}
5749
Bram Moolenaarb765d632005-06-07 21:00:02 +00005750
5751/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005752 * ":[count]spellgood {word}"
5753 * ":[count]spellwrong {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00005754 */
5755 void
5756ex_spell(eap)
5757 exarg_T *eap;
5758{
Bram Moolenaar7887d882005-07-01 22:33:52 +00005759 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005760 eap->forceit ? 0 : (int)eap->line2);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005761}
5762
5763/*
5764 * Add "word[len]" to 'spellfile' as a good or bad word.
5765 */
5766 void
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005767spell_add_word(word, len, bad, index)
Bram Moolenaarb765d632005-06-07 21:00:02 +00005768 char_u *word;
5769 int len;
5770 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005771 int index; /* "zG" and "zW": zero, otherwise index in
5772 'spellfile' */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005773{
5774 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005775 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005776 int new_spf = FALSE;
5777 struct stat st;
Bram Moolenaar7887d882005-07-01 22:33:52 +00005778 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005779 char_u fnamebuf[MAXPATHL];
5780 char_u line[MAXWLEN * 2];
5781 long fpos, fpos_next = 0;
5782 int i;
5783 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005784
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005785 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005786 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005787 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00005788 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005789 int_wordlist = vim_tempname('s');
5790 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00005791 return;
5792 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005793 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005794 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00005795 else
5796 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005797 /* If 'spellfile' isn't set figure out a good default value. */
5798 if (*curbuf->b_p_spf == NUL)
5799 {
5800 init_spellfile();
5801 new_spf = TRUE;
5802 }
5803
5804 if (*curbuf->b_p_spf == NUL)
5805 {
5806 EMSG(_("E764: 'spellfile' is not set"));
5807 return;
5808 }
5809
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005810 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
5811 {
5812 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
5813 if (i == index)
5814 break;
5815 if (*spf == NUL)
5816 {
5817 EMSGN(_("E765: 'spellfile' does not have %ld enties"), index);
5818 return;
5819 }
5820 }
5821
Bram Moolenaarb765d632005-06-07 21:00:02 +00005822 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005823 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005824 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
5825 buf = NULL;
5826 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00005827 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00005828 EMSG(_(e_bufloaded));
5829 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005830 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005831
Bram Moolenaarf9184a12005-07-02 23:10:47 +00005832 fname = fnamebuf;
5833 }
5834
5835 if (bad)
5836 {
5837 /* When the word also appears as good word we need to remove that one,
5838 * since its flags sort before the one with WF_BANNED. */
5839 fd = mch_fopen((char *)fname, "r");
5840 if (fd != NULL)
5841 {
5842 while (!vim_fgets(line, MAXWLEN * 2, fd))
5843 {
5844 fpos = fpos_next;
5845 fpos_next = ftell(fd);
5846 if (STRNCMP(word, line, len) == 0
5847 && (line[len] == '/' || line[len] < ' '))
5848 {
5849 /* Found duplicate word. Remove it by writing a '#' at
5850 * the start of the line. Mixing reading and writing
5851 * doesn't work for all systems, close the file first. */
5852 fclose(fd);
5853 fd = mch_fopen((char *)fname, "r+");
5854 if (fd == NULL)
5855 break;
5856 if (fseek(fd, fpos, SEEK_SET) == 0)
5857 fputc('#', fd);
5858 fseek(fd, fpos_next, SEEK_SET);
5859 }
5860 }
5861 fclose(fd);
5862 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00005863 }
5864
5865 fd = mch_fopen((char *)fname, "a");
5866 if (fd == NULL && new_spf)
5867 {
5868 /* We just initialized the 'spellfile' option and can't open the file.
5869 * We may need to create the "spell" directory first. We already
5870 * checked the runtime directory is writable in init_spellfile(). */
5871 STRCPY(NameBuff, fname);
5872 *gettail_sep(NameBuff) = NUL;
5873 if (mch_stat((char *)NameBuff, &st) < 0)
5874 {
5875 /* The directory doesn't exist. Try creating it and opening the
5876 * file again. */
5877 vim_mkdir(NameBuff, 0755);
5878 fd = mch_fopen((char *)fname, "a");
5879 }
5880 }
5881
5882 if (fd == NULL)
5883 EMSG2(_(e_notopen), fname);
5884 else
5885 {
5886 if (bad)
5887 fprintf(fd, "%.*s/!\n", len, word);
5888 else
5889 fprintf(fd, "%.*s\n", len, word);
5890 fclose(fd);
5891
5892 /* Update the .add.spl file. */
5893 mkspell(1, &fname, FALSE, TRUE, TRUE);
5894
5895 /* If the .add file is edited somewhere, reload it. */
5896 if (buf != NULL)
5897 buf_reload(buf);
5898
5899 redraw_all_later(NOT_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005900 }
5901}
5902
5903/*
5904 * Initialize 'spellfile' for the current buffer.
5905 */
5906 static void
5907init_spellfile()
5908{
5909 char_u buf[MAXPATHL];
5910 int l;
5911 slang_T *sl;
5912 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005913 char_u *lend;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005914
5915 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
5916 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005917 /* Find the end of the language name. Exclude the region. */
5918 for (lend = curbuf->b_p_spl; *lend != NUL
5919 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
5920 ;
5921
5922 /* Loop over all entries in 'runtimepath'. Use the first one where we
5923 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005924 rtp = p_rtp;
5925 while (*rtp != NUL)
5926 {
5927 /* Copy the path from 'runtimepath' to buf[]. */
5928 copy_option_part(&rtp, buf, MAXPATHL, ",");
5929 if (filewritable(buf) == 2)
5930 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00005931 /* Use the first language name from 'spelllang' and the
5932 * encoding used in the first loaded .spl file. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005933 sl = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang;
5934 l = STRLEN(buf);
5935 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar3982c542005-06-08 21:56:31 +00005936 "/spell/%.*s.%s.add",
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005937 (int)(lend - curbuf->b_p_spl), curbuf->b_p_spl,
Bram Moolenaarb765d632005-06-07 21:00:02 +00005938 strstr((char *)gettail(sl->sl_fname), ".ascii.") != NULL
5939 ? (char_u *)"ascii" : spell_enc());
5940 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
5941 break;
5942 }
5943 }
5944 }
5945}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005946
Bram Moolenaar51485f02005-06-04 21:55:20 +00005947
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005948/*
5949 * Init the chartab used for spelling for ASCII.
5950 * EBCDIC is not supported!
5951 */
5952 static void
5953clear_spell_chartab(sp)
5954 spelltab_T *sp;
5955{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005956 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005957
5958 /* Init everything to FALSE. */
5959 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
5960 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
5961 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005962 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005963 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005964 sp->st_upper[i] = i;
5965 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005966
5967 /* We include digits. A word shouldn't start with a digit, but handling
5968 * that is done separately. */
5969 for (i = '0'; i <= '9'; ++i)
5970 sp->st_isw[i] = TRUE;
5971 for (i = 'A'; i <= 'Z'; ++i)
5972 {
5973 sp->st_isw[i] = TRUE;
5974 sp->st_isu[i] = TRUE;
5975 sp->st_fold[i] = i + 0x20;
5976 }
5977 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005978 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005979 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005980 sp->st_upper[i] = i - 0x20;
5981 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005982}
5983
5984/*
5985 * Init the chartab used for spelling. Only depends on 'encoding'.
5986 * Called once while starting up and when 'encoding' changes.
5987 * The default is to use isalpha(), but the spell file should define the word
5988 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005989 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005990 */
5991 void
5992init_spell_chartab()
5993{
5994 int i;
5995
5996 did_set_spelltab = FALSE;
5997 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005998#ifdef FEAT_MBYTE
5999 if (enc_dbcs)
6000 {
6001 /* DBCS: assume double-wide characters are word characters. */
6002 for (i = 128; i <= 255; ++i)
6003 if (MB_BYTE2LEN(i) == 2)
6004 spelltab.st_isw[i] = TRUE;
6005 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006006 else if (enc_utf8)
6007 {
6008 for (i = 128; i < 256; ++i)
6009 {
6010 spelltab.st_isu[i] = utf_isupper(i);
6011 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
6012 spelltab.st_fold[i] = utf_fold(i);
6013 spelltab.st_upper[i] = utf_toupper(i);
6014 }
6015 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006016 else
6017#endif
6018 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006019 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006020 for (i = 128; i < 256; ++i)
6021 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006022 if (MB_ISUPPER(i))
6023 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006024 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006025 spelltab.st_isu[i] = TRUE;
6026 spelltab.st_fold[i] = MB_TOLOWER(i);
6027 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006028 else if (MB_ISLOWER(i))
6029 {
6030 spelltab.st_isw[i] = TRUE;
6031 spelltab.st_upper[i] = MB_TOUPPER(i);
6032 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006033 }
6034 }
6035}
6036
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006037static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
6038static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
6039
6040/*
6041 * Set the spell character tables from strings in the affix file.
6042 */
6043 static int
6044set_spell_chartab(fol, low, upp)
6045 char_u *fol;
6046 char_u *low;
6047 char_u *upp;
6048{
6049 /* We build the new tables here first, so that we can compare with the
6050 * previous one. */
6051 spelltab_T new_st;
6052 char_u *pf = fol, *pl = low, *pu = upp;
6053 int f, l, u;
6054
6055 clear_spell_chartab(&new_st);
6056
6057 while (*pf != NUL)
6058 {
6059 if (*pl == NUL || *pu == NUL)
6060 {
6061 EMSG(_(e_affform));
6062 return FAIL;
6063 }
6064#ifdef FEAT_MBYTE
6065 f = mb_ptr2char_adv(&pf);
6066 l = mb_ptr2char_adv(&pl);
6067 u = mb_ptr2char_adv(&pu);
6068#else
6069 f = *pf++;
6070 l = *pl++;
6071 u = *pu++;
6072#endif
6073 /* Every character that appears is a word character. */
6074 if (f < 256)
6075 new_st.st_isw[f] = TRUE;
6076 if (l < 256)
6077 new_st.st_isw[l] = TRUE;
6078 if (u < 256)
6079 new_st.st_isw[u] = TRUE;
6080
6081 /* if "LOW" and "FOL" are not the same the "LOW" char needs
6082 * case-folding */
6083 if (l < 256 && l != f)
6084 {
6085 if (f >= 256)
6086 {
6087 EMSG(_(e_affrange));
6088 return FAIL;
6089 }
6090 new_st.st_fold[l] = f;
6091 }
6092
6093 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006094 * case-folding, it's upper case and the "UPP" is the upper case of
6095 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006096 if (u < 256 && u != f)
6097 {
6098 if (f >= 256)
6099 {
6100 EMSG(_(e_affrange));
6101 return FAIL;
6102 }
6103 new_st.st_fold[u] = f;
6104 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006105 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006106 }
6107 }
6108
6109 if (*pl != NUL || *pu != NUL)
6110 {
6111 EMSG(_(e_affform));
6112 return FAIL;
6113 }
6114
6115 return set_spell_finish(&new_st);
6116}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006117
6118/*
6119 * Set the spell character tables from strings in the .spl file.
6120 */
6121 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006122set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006123 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006124 int cnt; /* length of "flags" */
6125 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006126{
6127 /* We build the new tables here first, so that we can compare with the
6128 * previous one. */
6129 spelltab_T new_st;
6130 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006131 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006132 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006133
6134 clear_spell_chartab(&new_st);
6135
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006136 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006137 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006138 if (i < cnt)
6139 {
6140 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
6141 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
6142 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006143
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006144 if (*p != NUL)
6145 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006146#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006147 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006148#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006149 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006150#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00006151 new_st.st_fold[i + 128] = c;
6152 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
6153 new_st.st_upper[c] = i + 128;
6154 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006155 }
6156
6157 return set_spell_finish(&new_st);
6158}
6159
6160 static int
6161set_spell_finish(new_st)
6162 spelltab_T *new_st;
6163{
6164 int i;
6165
6166 if (did_set_spelltab)
6167 {
6168 /* check that it's the same table */
6169 for (i = 0; i < 256; ++i)
6170 {
6171 if (spelltab.st_isw[i] != new_st->st_isw[i]
6172 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006173 || spelltab.st_fold[i] != new_st->st_fold[i]
6174 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006175 {
6176 EMSG(_("E763: Word characters differ between spell files"));
6177 return FAIL;
6178 }
6179 }
6180 }
6181 else
6182 {
6183 /* copy the new spelltab into the one being used */
6184 spelltab = *new_st;
6185 did_set_spelltab = TRUE;
6186 }
6187
6188 return OK;
6189}
6190
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006191/*
Bram Moolenaarea408852005-06-25 22:49:46 +00006192 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006193 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00006194 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006195 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00006196 */
6197 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006198spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00006199 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006200 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00006201{
Bram Moolenaarea408852005-06-25 22:49:46 +00006202#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006203 char_u *s;
6204 int l;
6205 int c;
6206
6207 if (has_mbyte)
6208 {
6209 l = MB_BYTE2LEN(*p);
6210 s = p;
6211 if (l == 1)
6212 {
6213 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006214 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006215 {
6216 s = p + 1; /* skip a mid-word character */
6217 l = MB_BYTE2LEN(*s);
6218 }
6219 }
6220 else
6221 {
6222 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006223 if (c < 256 ? buf->b_spell_ismw[c]
6224 : (buf->b_spell_ismw_mb != NULL
6225 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006226 {
6227 s = p + l;
6228 l = MB_BYTE2LEN(*s);
6229 }
6230 }
6231
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006232 c = mb_ptr2char(s);
6233 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006234 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006235 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006236 }
Bram Moolenaarea408852005-06-25 22:49:46 +00006237#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006238
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006239 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
6240}
6241
6242/*
6243 * Return TRUE if "p" points to a word character.
6244 * Unlike spell_iswordp() this doesn't check for "midword" characters.
6245 */
6246 static int
6247spell_iswordp_nmw(p)
6248 char_u *p;
6249{
6250#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006251 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006252
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006253 if (has_mbyte)
6254 {
6255 c = mb_ptr2char(p);
6256 if (c > 255)
6257 return mb_get_class(p) >= 2;
6258 return spelltab.st_isw[c];
6259 }
6260#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006261 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00006262}
6263
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006264#ifdef FEAT_MBYTE
6265/*
6266 * Return TRUE if "p" points to a word character.
6267 * Wide version of spell_iswordp().
6268 */
6269 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006270spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006271 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006272 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006273{
6274 int *s;
6275
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006276 if (*p < 256 ? buf->b_spell_ismw[*p]
6277 : (buf->b_spell_ismw_mb != NULL
6278 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006279 s = p + 1;
6280 else
6281 s = p;
6282
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006283 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006284 {
6285 if (enc_utf8)
6286 return utf_class(*s) >= 2;
6287 if (enc_dbcs)
6288 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
6289 return 0;
6290 }
6291 return spelltab.st_isw[*s];
6292}
6293#endif
6294
Bram Moolenaarea408852005-06-25 22:49:46 +00006295/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006296 * Write the table with prefix conditions to the .spl file.
6297 */
6298 static void
6299write_spell_prefcond(fd, gap)
6300 FILE *fd;
6301 garray_T *gap;
6302{
6303 int i;
6304 char_u *p;
6305 int len;
6306
6307 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
6308
6309 for (i = 0; i < gap->ga_len; ++i)
6310 {
6311 /* <prefcond> : <condlen> <condstr> */
6312 p = ((char_u **)gap->ga_data)[i];
6313 if (p == NULL)
6314 fputc(0, fd);
6315 else
6316 {
6317 len = STRLEN(p);
6318 fputc(len, fd);
6319 fwrite(p, (size_t)len, (size_t)1, fd);
6320 }
6321 }
6322}
6323
6324/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006325 * Write the current tables into the .spl file.
6326 * This makes sure the same characters are recognized as word characters when
6327 * generating an when using a spell file.
6328 */
6329 static void
6330write_spell_chartab(fd)
6331 FILE *fd;
6332{
6333 char_u charbuf[256 * 4];
6334 int len = 0;
6335 int flags;
6336 int i;
6337
6338 fputc(128, fd); /* <charflagslen> */
6339 for (i = 128; i < 256; ++i)
6340 {
6341 flags = 0;
6342 if (spelltab.st_isw[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006343 flags |= CF_WORD;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006344 if (spelltab.st_isu[i])
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006345 flags |= CF_UPPER;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006346 fputc(flags, fd); /* <charflags> */
6347
Bram Moolenaarb765d632005-06-07 21:00:02 +00006348#ifdef FEAT_MBYTE
6349 if (has_mbyte)
6350 len += mb_char2bytes(spelltab.st_fold[i], charbuf + len);
6351 else
6352#endif
6353 charbuf[len++] = spelltab.st_fold[i];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006354 }
6355
6356 put_bytes(fd, (long_u)len, 2); /* <fcharlen> */
6357 fwrite(charbuf, (size_t)len, (size_t)1, fd); /* <fchars> */
6358}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006359
6360/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006361 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
6362 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006363 * When using a multi-byte 'encoding' the length may change!
6364 * Returns FAIL when something wrong.
6365 */
6366 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006367spell_casefold(str, len, buf, buflen)
6368 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006369 int len;
6370 char_u *buf;
6371 int buflen;
6372{
6373 int i;
6374
6375 if (len >= buflen)
6376 {
6377 buf[0] = NUL;
6378 return FAIL; /* result will not fit */
6379 }
6380
6381#ifdef FEAT_MBYTE
6382 if (has_mbyte)
6383 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006384 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006385 char_u *p;
6386 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006387
6388 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006389 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006390 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006391 if (outi + MB_MAXBYTES > buflen)
6392 {
6393 buf[outi] = NUL;
6394 return FAIL;
6395 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006396 c = mb_ptr2char_adv(&p);
6397 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006398 }
6399 buf[outi] = NUL;
6400 }
6401 else
6402#endif
6403 {
6404 /* Be quick for non-multibyte encodings. */
6405 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006406 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006407 buf[i] = NUL;
6408 }
6409
6410 return OK;
6411}
6412
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006413#define SPS_BEST 1
6414#define SPS_FAST 2
6415#define SPS_DOUBLE 4
6416
6417static int sps_flags = SPS_BEST;
6418
6419/*
6420 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
6421 * Sets "sps_flags".
6422 */
6423 int
6424spell_check_sps()
6425{
6426 char_u *p;
6427 char_u buf[MAXPATHL];
6428 int f;
6429
6430 sps_flags = 0;
6431
6432 for (p = p_sps; *p != NUL; )
6433 {
6434 copy_option_part(&p, buf, MAXPATHL, ",");
6435
6436 f = 0;
6437 if (STRCMP(buf, "best") == 0)
6438 f = SPS_BEST;
6439 else if (STRCMP(buf, "fast") == 0)
6440 f = SPS_FAST;
6441 else if (STRCMP(buf, "double") == 0)
6442 f = SPS_DOUBLE;
6443 else if (STRNCMP(buf, "expr:", 5) != 0
6444 && STRNCMP(buf, "file:", 5) != 0)
6445 f = -1;
6446
6447 if (f == -1 || (sps_flags != 0 && f != 0))
6448 {
6449 sps_flags = SPS_BEST;
6450 return FAIL;
6451 }
6452 if (f != 0)
6453 sps_flags = f;
6454 }
6455
6456 if (sps_flags == 0)
6457 sps_flags = SPS_BEST;
6458
6459 return OK;
6460}
6461
6462/* Remember what "z?" replaced. */
6463static char_u *repl_from = NULL;
6464static char_u *repl_to = NULL;
6465
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006466/*
6467 * "z?": Find badly spelled word under or after the cursor.
6468 * Give suggestions for the properly spelled word.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006469 */
6470 void
6471spell_suggest()
6472{
6473 char_u *line;
6474 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006475 char_u wcopy[MAXWLEN + 2];
6476 char_u *p;
6477 int i;
6478 int c;
6479 suginfo_T sug;
6480 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006481 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006482 int need_cap;
6483 regmatch_T regmatch;
6484 int endcol;
6485 char_u *line_copy = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006486
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006487 /* Find the start of the badly spelled word. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006488 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL
6489 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006490 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006491 if (!curwin->w_p_spell || *curbuf->b_p_spl == NUL)
6492 return;
6493
6494 /* No bad word or it starts after the cursor: use the word under the
6495 * cursor. */
6496 curwin->w_cursor = prev_cursor;
6497 line = ml_get_curline();
6498 p = line + curwin->w_cursor.col;
6499 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006500 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006501 mb_ptr_back(line, p);
6502 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006503 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006504 mb_ptr_adv(p);
6505
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006506 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006507 {
6508 beep_flush();
6509 return;
6510 }
6511 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006512 }
6513
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006514 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006515 line = ml_get_curline();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006516
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006517 /* Figure out if the word should be capitalised. */
6518 need_cap = FALSE;
6519 if (curbuf->b_cap_prog != NULL)
6520 {
6521 endcol = 0;
Bram Moolenaar97409f12005-07-08 22:17:29 +00006522 if ((int)(skipwhite(line) - line) == (int)curwin->w_cursor.col)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006523 {
6524 /* At start of line, check if previous line is empty or sentence
6525 * ends there. */
6526 if (curwin->w_cursor.lnum == 1)
6527 need_cap = TRUE;
6528 else
6529 {
6530 line = ml_get(curwin->w_cursor.lnum - 1);
6531 if (*skipwhite(line) == NUL)
6532 need_cap = TRUE;
6533 else
6534 {
6535 /* Append a space in place of the line break. */
6536 line_copy = concat_str(line, (char_u *)" ");
6537 line = line_copy;
6538 endcol = STRLEN(line);
6539 }
6540 }
6541 }
6542 else
6543 endcol = curwin->w_cursor.col;
6544
6545 if (endcol > 0)
6546 {
6547 /* Check if sentence ends before the bad word. */
6548 regmatch.regprog = curbuf->b_cap_prog;
6549 regmatch.rm_ic = FALSE;
6550 p = line + endcol;
6551 for (;;)
6552 {
6553 mb_ptr_back(line, p);
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006554 if (p == line || spell_iswordp_nmw(p))
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006555 break;
6556 if (vim_regexec(&regmatch, p, 0)
6557 && regmatch.endp[0] == line + endcol)
6558 {
6559 need_cap = TRUE;
6560 break;
6561 }
6562 }
6563 }
6564
6565 /* get the line again, we may have been using the previous one */
6566 line = ml_get_curline();
6567 vim_free(line_copy);
6568 }
6569
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006570 /* Get the list of suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006571 spell_find_suggest(line + curwin->w_cursor.col, &sug, (int)Rows - 2,
6572 TRUE, need_cap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006573
6574 if (sug.su_ga.ga_len == 0)
6575 MSG(_("Sorry, no suggestions"));
6576 else
6577 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006578 vim_free(repl_from);
6579 repl_from = NULL;
6580 vim_free(repl_to);
6581 repl_to = NULL;
6582
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006583 /* List the suggestions. */
6584 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006585 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006586 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
6587 sug.su_badlen, sug.su_badptr);
6588 msg_puts(IObuff);
6589 msg_clr_eos();
6590 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +00006591
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006592 msg_scroll = TRUE;
6593 for (i = 0; i < sug.su_ga.ga_len; ++i)
6594 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006595 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006596
6597 /* The suggested word may replace only part of the bad word, add
6598 * the not replaced part. */
6599 STRCPY(wcopy, stp->st_word);
6600 if (sug.su_badlen > stp->st_orglen)
6601 vim_strncpy(wcopy + STRLEN(wcopy),
6602 sug.su_badptr + stp->st_orglen,
6603 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006604 vim_snprintf((char *)IObuff, IOSIZE, _("%2d \"%s\""), i + 1, wcopy);
6605 msg_puts(IObuff);
6606
6607 /* The word may replace more than "su_badlen". */
6608 if (sug.su_badlen < stp->st_orglen)
6609 {
6610 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
6611 stp->st_orglen, sug.su_badptr);
6612 msg_puts(IObuff);
6613 }
6614
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006615 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006616 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00006617 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006618 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0c405862005-06-22 22:26:26 +00006619 vim_snprintf((char *)IObuff, IOSIZE, _(" (%s%d - %d)"),
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006620 stp->st_salscore ? "s " : "",
6621 stp->st_score, stp->st_altscore);
6622 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00006623 vim_snprintf((char *)IObuff, IOSIZE, _(" (%d)"),
6624 stp->st_score);
6625 msg_advance(30);
6626 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006627 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006628 msg_putchar('\n');
6629 }
6630
6631 /* Ask for choice. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006632 i = prompt_for_number(&mouse_used);
6633 if (mouse_used)
6634 i -= lines_left;
6635
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006636 if (i > 0 && i <= sug.su_ga.ga_len && u_save_cursor() == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006637 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006638 /* Save the from and to text for :spellrepall. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006639 stp = &SUG(sug.su_ga, i - 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006640 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
6641 repl_to = vim_strsave(stp->st_word);
6642
6643 /* Replace the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006644 p = alloc(STRLEN(line) - stp->st_orglen + STRLEN(stp->st_word) + 1);
6645 if (p != NULL)
6646 {
6647 c = sug.su_badptr - line;
6648 mch_memmove(p, line, c);
6649 STRCPY(p + c, stp->st_word);
6650 STRCAT(p, sug.su_badptr + stp->st_orglen);
6651 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6652 curwin->w_cursor.col = c;
6653 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006654
6655 /* For redo we use a change-word command. */
6656 ResetRedobuff();
6657 AppendToRedobuff((char_u *)"ciw");
6658 AppendToRedobuff(stp->st_word);
6659 AppendCharToRedobuff(ESC);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006660 }
6661 }
6662 else
6663 curwin->w_cursor = prev_cursor;
6664 }
6665
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006666 spell_find_cleanup(&sug);
6667}
6668
6669/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006670 * ":spellrepall"
6671 */
6672/*ARGSUSED*/
6673 void
6674ex_spellrepall(eap)
6675 exarg_T *eap;
6676{
6677 pos_T pos = curwin->w_cursor;
6678 char_u *frompat;
6679 int addlen;
6680 char_u *line;
6681 char_u *p;
6682 int didone = FALSE;
6683 int save_ws = p_ws;
6684
6685 if (repl_from == NULL || repl_to == NULL)
6686 {
6687 EMSG(_("E752: No previous spell replacement"));
6688 return;
6689 }
6690 addlen = STRLEN(repl_to) - STRLEN(repl_from);
6691
6692 frompat = alloc(STRLEN(repl_from) + 7);
6693 if (frompat == NULL)
6694 return;
6695 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
6696 p_ws = FALSE;
6697
6698 curwin->w_cursor.lnum = 0;
6699 while (!got_int)
6700 {
6701 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
6702 || u_save_cursor() == FAIL)
6703 break;
6704
6705 /* Only replace when the right word isn't there yet. This happens
6706 * when changing "etc" to "etc.". */
6707 line = ml_get_curline();
6708 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
6709 repl_to, STRLEN(repl_to)) != 0)
6710 {
6711 p = alloc(STRLEN(line) + addlen + 1);
6712 if (p == NULL)
6713 break;
6714 mch_memmove(p, line, curwin->w_cursor.col);
6715 STRCPY(p + curwin->w_cursor.col, repl_to);
6716 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
6717 ml_replace(curwin->w_cursor.lnum, p, FALSE);
6718 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
6719 didone = TRUE;
6720 }
6721 curwin->w_cursor.col += STRLEN(repl_to);
6722 }
6723
6724 p_ws = save_ws;
6725 curwin->w_cursor = pos;
6726 vim_free(frompat);
6727
6728 if (!didone)
6729 EMSG2(_("E753: Not found: %s"), repl_from);
6730}
6731
6732/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006733 * Find spell suggestions for "word". Return them in the growarray "*gap" as
6734 * a list of allocated strings.
6735 */
6736 void
6737spell_suggest_list(gap, word, maxcount)
6738 garray_T *gap;
6739 char_u *word;
6740 int maxcount; /* maximum nr of suggestions */
6741{
6742 suginfo_T sug;
6743 int i;
6744 suggest_T *stp;
6745 char_u *wcopy;
6746
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006747 spell_find_suggest(word, &sug, maxcount, FALSE, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006748
6749 /* Make room in "gap". */
6750 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
6751 if (ga_grow(gap, sug.su_ga.ga_len) == FAIL)
6752 return;
6753
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006754 for (i = 0; i < sug.su_ga.ga_len; ++i)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006755 {
6756 stp = &SUG(sug.su_ga, i);
6757
6758 /* The suggested word may replace only part of "word", add the not
6759 * replaced part. */
6760 wcopy = alloc(STRLEN(stp->st_word)
6761 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
6762 if (wcopy == NULL)
6763 break;
6764 STRCPY(wcopy, stp->st_word);
6765 STRCAT(wcopy, sug.su_badptr + stp->st_orglen);
6766 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
6767 }
6768
6769 spell_find_cleanup(&sug);
6770}
6771
6772/*
6773 * Find spell suggestions for the word at the start of "badptr".
6774 * Return the suggestions in "su->su_ga".
6775 * The maximum number of suggestions is "maxcount".
6776 * Note: does use info for the current window.
6777 * This is based on the mechanisms of Aspell, but completely reimplemented.
6778 */
6779 static void
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006780spell_find_suggest(badptr, su, maxcount, banbadword, need_cap)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006781 char_u *badptr;
6782 suginfo_T *su;
6783 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +00006784 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006785 int need_cap; /* word should start with capital */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006786{
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006787 int attr = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006788 char_u buf[MAXPATHL];
6789 char_u *p;
6790 int do_combine = FALSE;
6791 char_u *sps_copy;
6792#ifdef FEAT_EVAL
6793 static int expr_busy = FALSE;
6794#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006795 int c;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006796
6797 /*
6798 * Set the info in "*su".
6799 */
6800 vim_memset(su, 0, sizeof(suginfo_T));
6801 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
6802 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006803 if (*badptr == NUL)
6804 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006805 hash_init(&su->su_banned);
6806
6807 su->su_badptr = badptr;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006808 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006809 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006810 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006811
6812 if (su->su_badlen >= MAXWLEN)
6813 su->su_badlen = MAXWLEN - 1; /* just in case */
6814 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
6815 (void)spell_casefold(su->su_badptr, su->su_badlen,
6816 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00006817 /* get caps flags for bad word */
6818 su->su_badflags = captype(su->su_badptr, su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006819 if (need_cap)
6820 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006821
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006822 /* If the word is not capitalised and spell_check() doesn't consider the
6823 * word to be bad then it might need to be capitalised. Add a suggestion
6824 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00006825 c = PTR2CHAR(su->su_badptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00006826 if (!SPELL_ISUPPER(c) && attr == 0)
6827 {
6828 make_case_word(su->su_badword, buf, WF_ONECAP);
6829 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
6830 0, TRUE);
6831 }
6832
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006833 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +00006834 if (banbadword)
6835 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006836
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006837 /* Make a copy of 'spellsuggest', because the expression may change it. */
6838 sps_copy = vim_strsave(p_sps);
6839 if (sps_copy == NULL)
6840 return;
6841
6842 /* Loop over the items in 'spellsuggest'. */
6843 for (p = sps_copy; *p != NUL; )
6844 {
6845 copy_option_part(&p, buf, MAXPATHL, ",");
6846
6847 if (STRNCMP(buf, "expr:", 5) == 0)
6848 {
6849#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006850 /* Evaluate an expression. Skip this when called recursively,
6851 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00006852 if (!expr_busy)
6853 {
6854 expr_busy = TRUE;
6855 spell_suggest_expr(su, buf + 5);
6856 expr_busy = FALSE;
6857 }
6858#endif
6859 }
6860 else if (STRNCMP(buf, "file:", 5) == 0)
6861 /* Use list of suggestions in a file. */
6862 spell_suggest_file(su, buf + 5);
6863 else
6864 {
6865 /* Use internal method. */
6866 spell_suggest_intern(su);
6867 if (sps_flags & SPS_DOUBLE)
6868 do_combine = TRUE;
6869 }
6870 }
6871
6872 vim_free(sps_copy);
6873
6874 if (do_combine)
6875 /* Combine the two list of suggestions. This must be done last,
6876 * because sorting changes the order again. */
6877 score_combine(su);
6878}
6879
6880#ifdef FEAT_EVAL
6881/*
6882 * Find suggestions by evaluating expression "expr".
6883 */
6884 static void
6885spell_suggest_expr(su, expr)
6886 suginfo_T *su;
6887 char_u *expr;
6888{
6889 list_T *list;
6890 listitem_T *li;
6891 int score;
6892 char_u *p;
6893
6894 /* The work is split up in a few parts to avoid having to export
6895 * suginfo_T.
6896 * First evaluate the expression and get the resulting list. */
6897 list = eval_spell_expr(su->su_badword, expr);
6898 if (list != NULL)
6899 {
6900 /* Loop over the items in the list. */
6901 for (li = list->lv_first; li != NULL; li = li->li_next)
6902 if (li->li_tv.v_type == VAR_LIST)
6903 {
6904 /* Get the word and the score from the items. */
6905 score = get_spellword(li->li_tv.vval.v_list, &p);
6906 if (score >= 0)
6907 add_suggestion(su, &su->su_ga, p,
6908 su->su_badlen, score, 0, TRUE);
6909 }
6910 list_unref(list);
6911 }
6912
6913 /* Sort the suggestions and truncate at "maxcount". */
6914 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6915}
6916#endif
6917
6918/*
6919 * Find suggestions a file "fname".
6920 */
6921 static void
6922spell_suggest_file(su, fname)
6923 suginfo_T *su;
6924 char_u *fname;
6925{
6926 FILE *fd;
6927 char_u line[MAXWLEN * 2];
6928 char_u *p;
6929 int len;
6930 char_u cword[MAXWLEN];
6931
6932 /* Open the file. */
6933 fd = mch_fopen((char *)fname, "r");
6934 if (fd == NULL)
6935 {
6936 EMSG2(_(e_notopen), fname);
6937 return;
6938 }
6939
6940 /* Read it line by line. */
6941 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
6942 {
6943 line_breakcheck();
6944
6945 p = vim_strchr(line, '/');
6946 if (p == NULL)
6947 continue; /* No Tab found, just skip the line. */
6948 *p++ = NUL;
6949 if (STRICMP(su->su_badword, line) == 0)
6950 {
6951 /* Match! Isolate the good word, until CR or NL. */
6952 for (len = 0; p[len] >= ' '; ++len)
6953 ;
6954 p[len] = NUL;
6955
6956 /* If the suggestion doesn't have specific case duplicate the case
6957 * of the bad word. */
6958 if (captype(p, NULL) == 0)
6959 {
6960 make_case_word(p, cword, su->su_badflags);
6961 p = cword;
6962 }
6963
6964 add_suggestion(su, &su->su_ga, p, su->su_badlen,
6965 SCORE_FILE, 0, TRUE);
6966 }
6967 }
6968
6969 fclose(fd);
6970
6971 /* Sort the suggestions and truncate at "maxcount". */
6972 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
6973}
6974
6975/*
6976 * Find suggestions for the internal method indicated by "sps_flags".
6977 */
6978 static void
6979spell_suggest_intern(su)
6980 suginfo_T *su;
6981{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006982 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00006983 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006984 *
6985 * Set a maximum score to limit the combination of operations that is
6986 * tried.
6987 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00006988 suggest_try_special(su);
6989
6990 /*
6991 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
6992 * from the .aff file and inserting a space (split the word).
6993 */
6994 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006995
6996 /* For the resulting top-scorers compute the sound-a-like score. */
6997 if (sps_flags & SPS_DOUBLE)
6998 score_comp_sal(su);
6999
7000 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007001 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007002 *
7003 * Only do this when we don't have a lot of suggestions yet, because it's
7004 * very slow and often doesn't find new suggestions.
7005 */
7006 if ((sps_flags & SPS_DOUBLE)
7007 || (!(sps_flags & SPS_FAST)
7008 && su->su_ga.ga_len < SUG_CLEAN_COUNT(su)))
7009 {
7010 /* Allow a higher score now. */
7011 su->su_maxscore = SCORE_MAXMAX;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007012 suggest_try_soundalike(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007013 }
7014
7015 /* When CTRL-C was hit while searching do show the results. */
7016 ui_breakcheck();
7017 if (got_int)
7018 {
7019 (void)vgetc();
7020 got_int = FALSE;
7021 }
7022
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007023 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007024 {
7025 if (sps_flags & SPS_BEST)
7026 /* Adjust the word score for how it sounds like. */
7027 rescore_suggestions(su);
7028
7029 /* Sort the suggestions and truncate at "maxcount". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007030 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007031 }
7032}
7033
7034/*
7035 * Free the info put in "*su" by spell_find_suggest().
7036 */
7037 static void
7038spell_find_cleanup(su)
7039 suginfo_T *su;
7040{
7041 int i;
7042
7043 /* Free the suggestions. */
7044 for (i = 0; i < su->su_ga.ga_len; ++i)
7045 vim_free(SUG(su->su_ga, i).st_word);
7046 ga_clear(&su->su_ga);
7047 for (i = 0; i < su->su_sga.ga_len; ++i)
7048 vim_free(SUG(su->su_sga, i).st_word);
7049 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007050
7051 /* Free the banned words. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007052 free_banned(su);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007053}
7054
7055/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007056 * Make a copy of "word", with the first letter upper or lower cased, to
7057 * "wcopy[MAXWLEN]". "word" must not be empty.
7058 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007059 */
7060 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007061onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007062 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007063 char_u *wcopy;
7064 int upper; /* TRUE: first letter made upper case */
7065{
7066 char_u *p;
7067 int c;
7068 int l;
7069
7070 p = word;
7071#ifdef FEAT_MBYTE
7072 if (has_mbyte)
7073 c = mb_ptr2char_adv(&p);
7074 else
7075#endif
7076 c = *p++;
7077 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007078 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007079 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007080 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007081#ifdef FEAT_MBYTE
7082 if (has_mbyte)
7083 l = mb_char2bytes(c, wcopy);
7084 else
7085#endif
7086 {
7087 l = 1;
7088 wcopy[0] = c;
7089 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007090 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007091}
7092
7093/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007094 * Make a copy of "word" with all the letters upper cased into
7095 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007096 */
7097 static void
7098allcap_copy(word, wcopy)
7099 char_u *word;
7100 char_u *wcopy;
7101{
7102 char_u *s;
7103 char_u *d;
7104 int c;
7105
7106 d = wcopy;
7107 for (s = word; *s != NUL; )
7108 {
7109#ifdef FEAT_MBYTE
7110 if (has_mbyte)
7111 c = mb_ptr2char_adv(&s);
7112 else
7113#endif
7114 c = *s++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007115 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007116
7117#ifdef FEAT_MBYTE
7118 if (has_mbyte)
7119 {
7120 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
7121 break;
7122 d += mb_char2bytes(c, d);
7123 }
7124 else
7125#endif
7126 {
7127 if (d - wcopy >= MAXWLEN - 1)
7128 break;
7129 *d++ = c;
7130 }
7131 }
7132 *d = NUL;
7133}
7134
7135/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00007136 * Try finding suggestions by recognizing specific situations.
7137 */
7138 static void
7139suggest_try_special(su)
7140 suginfo_T *su;
7141{
7142 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007143 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007144 int c;
7145 char_u word[MAXWLEN];
7146
7147 /*
7148 * Recognize a word that is repeated: "the the".
7149 */
7150 p = skiptowhite(su->su_fbadword);
7151 len = p - su->su_fbadword;
7152 p = skipwhite(p);
7153 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
7154 {
7155 /* Include badflags: if the badword is onecap or allcap
7156 * use that for the goodword too: "The the" -> "The". */
7157 c = su->su_fbadword[len];
7158 su->su_fbadword[len] = NUL;
7159 make_case_word(su->su_fbadword, word, su->su_badflags);
7160 su->su_fbadword[len] = c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007161 add_suggestion(su, &su->su_ga, word, su->su_badlen, SCORE_DEL, 0, TRUE);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007162 }
7163}
7164
7165/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007166 * Try finding suggestions by adding/removing/swapping letters.
Bram Moolenaarea424162005-06-16 21:51:00 +00007167 *
7168 * This uses a state machine. At each node in the tree we try various
7169 * operations. When trying if an operation work "depth" is increased and the
7170 * stack[] is used to store info. This allows combinations, thus insert one
7171 * character, replace one and delete another. The number of changes is
7172 * limited by su->su_maxscore, checked in try_deeper().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007173 */
7174 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00007175suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007176 suginfo_T *su;
7177{
7178 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
7179 char_u tword[MAXWLEN]; /* good word collected so far */
7180 trystate_T stack[MAXWLEN];
7181 char_u preword[MAXWLEN * 3]; /* word found with proper case (appended
7182 * to for word split) */
7183 char_u prewordlen = 0; /* length of word in "preword" */
7184 int splitoff = 0; /* index in tword after last split */
7185 trystate_T *sp;
7186 int newscore;
7187 langp_T *lp;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007188 char_u *byts, *fbyts, *pbyts;
7189 idx_T *idxs, *fidxs, *pidxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007190 int depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007191 int c, c2, c3;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007192 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007193 int flags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007194 garray_T *gap;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007195 idx_T arridx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007196 int len;
7197 char_u *p;
7198 fromto_T *ftp;
Bram Moolenaarea424162005-06-16 21:51:00 +00007199 int fl = 0, tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007200 int repextra = 0; /* extra bytes in fword[] from REP item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007201
7202 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +00007203 * to find matches (esp. REP items). Append some more text, changing
7204 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007205 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007206 n = STRLEN(fword);
7207 p = su->su_badptr + su->su_badlen;
7208 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007209
7210 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
7211 lp->lp_slang != NULL; ++lp)
7212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007213 /*
7214 * Go through the whole case-fold tree, try changes at each node.
7215 * "tword[]" contains the word collected from nodes in the tree.
7216 * "fword[]" the word we are trying to match with (initially the bad
7217 * word).
7218 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007219 depth = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007220 sp = &stack[0];
7221 sp->ts_state = STATE_START;
7222 sp->ts_score = 0;
7223 sp->ts_curi = 1;
7224 sp->ts_fidx = 0;
7225 sp->ts_fidxtry = 0;
7226 sp->ts_twordlen = 0;
7227 sp->ts_arridx = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00007228#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007229 sp->ts_tcharlen = 0;
Bram Moolenaarea424162005-06-16 21:51:00 +00007230#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007231
Bram Moolenaarea424162005-06-16 21:51:00 +00007232 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007233 * When there are postponed prefixes we need to use these first. At
7234 * the end of the prefix we continue in the case-fold tree.
7235 */
7236 fbyts = lp->lp_slang->sl_fbyts;
7237 fidxs = lp->lp_slang->sl_fidxs;
7238 pbyts = lp->lp_slang->sl_pbyts;
7239 pidxs = lp->lp_slang->sl_pidxs;
7240 if (pbyts != NULL)
7241 {
7242 byts = pbyts;
7243 idxs = pidxs;
7244 sp->ts_prefixdepth = PREFIXTREE;
7245 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
7246 }
7247 else
7248 {
7249 byts = fbyts;
7250 idxs = fidxs;
7251 sp->ts_prefixdepth = NOPREFIX;
7252 }
7253
7254 /*
Bram Moolenaarea424162005-06-16 21:51:00 +00007255 * Loop to find all suggestions. At each round we either:
7256 * - For the current state try one operation, advance "ts_curi",
7257 * increase "depth".
7258 * - When a state is done go to the next, set "ts_state".
7259 * - When all states are tried decrease "depth".
7260 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007261 while (depth >= 0 && !got_int)
7262 {
7263 sp = &stack[depth];
7264 switch (sp->ts_state)
7265 {
7266 case STATE_START:
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007267 case STATE_NOPREFIX:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007268 /*
7269 * Start of node: Deal with NUL bytes, which means
7270 * tword[] may end here.
7271 */
7272 arridx = sp->ts_arridx; /* current node in the tree */
7273 len = byts[arridx]; /* bytes in this node */
7274 arridx += sp->ts_curi; /* index of current byte */
7275
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007276 if (sp->ts_prefixdepth == PREFIXTREE)
7277 {
7278 /* Skip over the NUL bytes, we use them later. */
7279 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
7280 ;
7281 sp->ts_curi += n;
7282
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007283 /* Always past NUL bytes now. */
7284 n = (int)sp->ts_state;
7285 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00007286 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007287
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007288 /* At end of a prefix or at start of prefixtree: check for
7289 * following word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007290 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007291 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00007292 /* Set su->su_badflags to the caps type at this
7293 * position. Use the caps type until here for the
7294 * prefix itself. */
7295#ifdef FEAT_MBYTE
7296 if (has_mbyte)
7297 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
7298 else
7299#endif
7300 n = sp->ts_fidx;
7301 flags = captype(su->su_badptr, su->su_badptr + n);
7302 su->su_badflags = captype(su->su_badptr + n,
7303 su->su_badptr + su->su_badlen);
7304
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007305 ++depth;
7306 stack[depth] = stack[depth - 1];
7307 sp = &stack[depth];
7308 sp->ts_prefixdepth = depth - 1;
7309 byts = fbyts;
7310 idxs = fidxs;
7311 sp->ts_state = STATE_START;
7312 sp->ts_curi = 1; /* start just after length byte */
7313 sp->ts_arridx = 0;
7314
Bram Moolenaar53805d12005-08-01 07:08:33 +00007315 /* Move the prefix to preword[] with the right case
7316 * and make find_keepcap_word() works. */
7317 splitoff = sp->ts_twordlen;
7318 tword[splitoff] = NUL;
7319 make_case_word(tword, preword, flags);
7320 prewordlen = STRLEN(preword);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007321 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007322 break;
7323 }
7324
Bram Moolenaar0c405862005-06-22 22:26:26 +00007325 if (sp->ts_curi > len || byts[arridx] != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007326 {
7327 /* Past bytes in node and/or past NUL bytes. */
7328 sp->ts_state = STATE_ENDNUL;
Bram Moolenaar53805d12005-08-01 07:08:33 +00007329 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007330 break;
7331 }
7332
7333 /*
7334 * End of word in tree.
7335 */
7336 ++sp->ts_curi; /* eat one NUL byte */
7337
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007338 flags = (int)idxs[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007339
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007340 if (sp->ts_prefixdepth < MAXWLEN)
7341 {
7342 /* There was a prefix before the word. Check that the
7343 * prefix can be used with this word. */
7344 /* Count the length of the NULs in the prefix. If there
7345 * are none this must be the first try without a prefix.
7346 */
7347 n = stack[sp->ts_prefixdepth].ts_arridx;
7348 len = pbyts[n++];
7349 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
7350 ;
7351 if (c > 0)
7352 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007353 /* The prefix ID is stored three bytes above the
7354 * flags. */
7355 c = valid_word_prefix(c, n, flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00007356 tword + splitoff, lp->lp_slang, FALSE);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007357 if (c == 0)
7358 break;
7359
7360 /* Use the WF_RARE flag for a rare prefix. */
7361 if (c & WF_RAREPFX)
7362 flags |= WF_RARE;
7363 }
7364 }
7365
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007366 /*
7367 * Form the word with proper case in preword.
7368 * If there is a word from a previous split, append.
7369 */
7370 tword[sp->ts_twordlen] = NUL;
7371 if (flags & WF_KEEPCAP)
7372 /* Must find the word in the keep-case tree. */
7373 find_keepcap_word(lp->lp_slang, tword + splitoff,
7374 preword + prewordlen);
7375 else
Bram Moolenaar0c405862005-06-22 22:26:26 +00007376 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007377 /* Include badflags: if the badword is onecap or allcap
Bram Moolenaar0c405862005-06-22 22:26:26 +00007378 * use that for the goodword too. But if the badword is
7379 * allcap and it's only one char long use onecap. */
7380 c = su->su_badflags;
7381 if ((c & WF_ALLCAP)
7382#ifdef FEAT_MBYTE
7383 && su->su_badlen == mb_ptr2len_check(su->su_badptr)
7384#else
7385 && su->su_badlen == 1
7386#endif
7387 )
7388 c = WF_ONECAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007389 make_case_word(tword + splitoff,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007390 preword + prewordlen, flags | c);
7391 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007392
7393 /* Don't use a banned word. It may appear again as a good
7394 * word, thus remember it. */
7395 if (flags & WF_BANNED)
7396 {
7397 add_banned(su, preword + prewordlen);
7398 break;
7399 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00007400 if (was_banned(su, preword + prewordlen)
7401 || was_banned(su, preword))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007402 break;
7403
7404 newscore = 0;
7405 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007406 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007407 newscore += SCORE_REGION;
7408 if (flags & WF_RARE)
7409 newscore += SCORE_RARE;
7410
Bram Moolenaar0c405862005-06-22 22:26:26 +00007411 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007412 captype(preword + prewordlen, NULL)))
7413 newscore += SCORE_ICASE;
7414
Bram Moolenaar0c405862005-06-22 22:26:26 +00007415 if ((fword[sp->ts_fidx] == NUL
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007416 || !spell_iswordp(fword + sp->ts_fidx, curbuf))
Bram Moolenaar0c405862005-06-22 22:26:26 +00007417 && sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007418 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007419 /* The badword also ends: add suggestions. Give a penalty
7420 * when changing non-word char to word char, e.g., "thes,"
7421 * -> "these". */
7422 p = fword + sp->ts_fidx;
7423#ifdef FEAT_MBYTE
7424 if (has_mbyte)
7425 mb_ptr_back(fword, p);
7426 else
7427#endif
7428 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007429 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007430 {
7431 p = preword + STRLEN(preword);
7432#ifdef FEAT_MBYTE
7433 if (has_mbyte)
7434 mb_ptr_back(preword, p);
7435 else
7436#endif
7437 --p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007438 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007439 newscore += SCORE_NONWORD;
7440 }
7441
Bram Moolenaard857f0e2005-06-21 22:37:39 +00007442 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar0c405862005-06-22 22:26:26 +00007443 sp->ts_fidx - repextra,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00007444 sp->ts_score + newscore, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007445 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007446 else if (sp->ts_fidx >= sp->ts_fidxtry
7447#ifdef FEAT_MBYTE
7448 /* Don't split halfway a character. */
7449 && (!has_mbyte || sp->ts_tcharlen == 0)
7450#endif
7451 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007452 {
7453 /* The word in the tree ends but the badword
7454 * continues: try inserting a space and check that a valid
7455 * words starts at fword[sp->ts_fidx]. */
7456 if (try_deeper(su, stack, depth, newscore + SCORE_SPLIT))
7457 {
7458 /* Save things to be restored at STATE_SPLITUNDO. */
7459 sp->ts_save_prewordlen = prewordlen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007460 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007461 sp->ts_save_splitoff = splitoff;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007462 sp->ts_state = STATE_SPLITUNDO;
7463
7464 ++depth;
7465 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007466
7467 /* Append a space to preword. */
7468 STRCAT(preword, " ");
7469 prewordlen = STRLEN(preword);
7470 splitoff = sp->ts_twordlen;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007471
7472 /* If the badword has a non-word character at this
7473 * position skip it. That means replacing the
7474 * non-word character with a space. */
7475 if (!spell_iswordp_nmw(fword + sp->ts_fidx))
7476 {
7477 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
7478#ifdef FEAT_MBYTE
7479 if (has_mbyte)
7480 sp->ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
7481 else
7482#endif
7483 ++sp->ts_fidx;
7484 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00007485
7486 /* set su->su_badflags to the caps type at this
7487 * position */
7488
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007489#ifdef FEAT_MBYTE
7490 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +00007491 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007492 else
7493#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00007494 n = sp->ts_fidx;
7495 su->su_badflags = captype(su->su_badptr + n,
7496 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007497
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007498 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00007499 sp->ts_arridx = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007500 }
7501 }
7502 break;
7503
7504 case STATE_SPLITUNDO:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007505 /* Undo the changes done for word split. */
7506 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007507 splitoff = sp->ts_save_splitoff;
7508 prewordlen = sp->ts_save_prewordlen;
7509
7510 /* Continue looking for NUL bytes. */
7511 sp->ts_state = STATE_START;
7512 break;
7513
7514 case STATE_ENDNUL:
7515 /* Past the NUL bytes in the node. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00007516 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007517 if (fword[sp->ts_fidx] == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007518 {
7519 /* The badword ends, can't use the bytes in this node. */
7520 sp->ts_state = STATE_DEL;
7521 break;
7522 }
7523 sp->ts_state = STATE_PLAIN;
7524 /*FALLTHROUGH*/
7525
7526 case STATE_PLAIN:
7527 /*
7528 * Go over all possible bytes at this node, add each to
7529 * tword[] and use child node. "ts_curi" is the index.
7530 */
7531 arridx = sp->ts_arridx;
7532 if (sp->ts_curi > byts[arridx])
7533 {
7534 /* Done all bytes at this node, do next state. When still
7535 * at already changed bytes skip the other tricks. */
7536 if (sp->ts_fidx >= sp->ts_fidxtry)
7537 sp->ts_state = STATE_DEL;
7538 else
7539 sp->ts_state = STATE_FINAL;
7540 }
7541 else
7542 {
7543 arridx += sp->ts_curi++;
7544 c = byts[arridx];
7545
7546 /* Normal byte, go one level deeper. If it's not equal to
7547 * the byte in the bad word adjust the score. But don't
7548 * even try when the byte was already changed. */
Bram Moolenaarea424162005-06-16 21:51:00 +00007549 if (c == fword[sp->ts_fidx]
7550#ifdef FEAT_MBYTE
7551 || (sp->ts_tcharlen > 0
7552 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00007553#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00007554 )
7555 newscore = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007556 else
7557 newscore = SCORE_SUBST;
7558 if ((newscore == 0 || sp->ts_fidx >= sp->ts_fidxtry)
7559 && try_deeper(su, stack, depth, newscore))
7560 {
7561 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007562 sp = &stack[depth];
7563 ++sp->ts_fidx;
7564 tword[sp->ts_twordlen++] = c;
7565 sp->ts_arridx = idxs[arridx];
7566#ifdef FEAT_MBYTE
7567 if (newscore == SCORE_SUBST)
7568 sp->ts_isdiff = DIFF_YES;
7569 if (has_mbyte)
7570 {
7571 /* Multi-byte characters are a bit complicated to
7572 * handle: They differ when any of the bytes
7573 * differ and then their length may also differ. */
7574 if (sp->ts_tcharlen == 0)
7575 {
7576 /* First byte. */
7577 sp->ts_tcharidx = 0;
7578 sp->ts_tcharlen = MB_BYTE2LEN(c);
7579 sp->ts_fcharstart = sp->ts_fidx - 1;
7580 sp->ts_isdiff = (newscore != 0)
7581 ? DIFF_YES : DIFF_NONE;
7582 }
7583 else if (sp->ts_isdiff == DIFF_INSERT)
7584 /* When inserting trail bytes don't advance in
7585 * the bad word. */
7586 --sp->ts_fidx;
7587 if (++sp->ts_tcharidx == sp->ts_tcharlen)
7588 {
7589 /* Last byte of character. */
7590 if (sp->ts_isdiff == DIFF_YES)
7591 {
7592 /* Correct ts_fidx for the byte length of
7593 * the character (we didn't check that
7594 * before). */
7595 sp->ts_fidx = sp->ts_fcharstart
7596 + MB_BYTE2LEN(
7597 fword[sp->ts_fcharstart]);
7598
7599 /* For a similar character adjust score
7600 * from SCORE_SUBST to SCORE_SIMILAR. */
7601 if (lp->lp_slang->sl_has_map
7602 && similar_chars(lp->lp_slang,
7603 mb_ptr2char(tword
7604 + sp->ts_twordlen
7605 - sp->ts_tcharlen),
7606 mb_ptr2char(fword
7607 + sp->ts_fcharstart)))
7608 sp->ts_score -=
7609 SCORE_SUBST - SCORE_SIMILAR;
7610 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007611 else if (sp->ts_isdiff == DIFF_INSERT
7612 && sp->ts_twordlen > sp->ts_tcharlen)
7613 {
7614 /* If the previous character was the same,
7615 * thus doubling a character, give a bonus
7616 * to the score. */
7617 p = tword + sp->ts_twordlen
7618 - sp->ts_tcharlen;
7619 c = mb_ptr2char(p);
7620 mb_ptr_back(tword, p);
7621 if (c == mb_ptr2char(p))
7622 sp->ts_score -= SCORE_INS
7623 - SCORE_INSDUP;
7624 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007625
7626 /* Starting a new char, reset the length. */
7627 sp->ts_tcharlen = 0;
7628 }
7629 }
7630 else
7631#endif
7632 {
7633 /* If we found a similar char adjust the score.
7634 * We do this after calling try_deeper() because
7635 * it's slow. */
7636 if (newscore != 0
7637 && lp->lp_slang->sl_has_map
7638 && similar_chars(lp->lp_slang,
7639 c, fword[sp->ts_fidx - 1]))
7640 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
7641 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007642 }
7643 }
7644 break;
7645
7646 case STATE_DEL:
Bram Moolenaarea424162005-06-16 21:51:00 +00007647#ifdef FEAT_MBYTE
7648 /* When past the first byte of a multi-byte char don't try
7649 * delete/insert/swap a character. */
7650 if (has_mbyte && sp->ts_tcharlen > 0)
7651 {
7652 sp->ts_state = STATE_FINAL;
7653 break;
7654 }
7655#endif
7656 /*
7657 * Try skipping one character in the bad word (delete it).
7658 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007659 sp->ts_state = STATE_INS;
7660 sp->ts_curi = 1;
7661 if (fword[sp->ts_fidx] != NUL
7662 && try_deeper(su, stack, depth, SCORE_DEL))
7663 {
7664 ++depth;
Bram Moolenaarea408852005-06-25 22:49:46 +00007665
7666 /* Advance over the character in fword[]. Give a bonus to
7667 * the score if the same character is following "nn" ->
7668 * "n". */
Bram Moolenaarea424162005-06-16 21:51:00 +00007669#ifdef FEAT_MBYTE
7670 if (has_mbyte)
Bram Moolenaarea408852005-06-25 22:49:46 +00007671 {
7672 c = mb_ptr2char(fword + sp->ts_fidx);
Bram Moolenaarea424162005-06-16 21:51:00 +00007673 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaarea408852005-06-25 22:49:46 +00007674 if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
7675 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
7676 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007677 else
7678#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007679 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007680 ++stack[depth].ts_fidx;
Bram Moolenaarea408852005-06-25 22:49:46 +00007681 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
7682 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
7683 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007684 break;
7685 }
7686 /*FALLTHROUGH*/
7687
7688 case STATE_INS:
Bram Moolenaarea424162005-06-16 21:51:00 +00007689 /* Insert one byte. Do this for each possible byte at this
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007690 * node. */
7691 n = sp->ts_arridx;
7692 if (sp->ts_curi > byts[n])
7693 {
7694 /* Done all bytes at this node, do next state. */
7695 sp->ts_state = STATE_SWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007696 }
7697 else
7698 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007699 /* Do one more byte at this node. Skip NUL bytes. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007700 n += sp->ts_curi++;
7701 c = byts[n];
7702 if (c != 0 && try_deeper(su, stack, depth, SCORE_INS))
7703 {
7704 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007705 sp = &stack[depth];
7706 tword[sp->ts_twordlen++] = c;
7707 sp->ts_arridx = idxs[n];
7708#ifdef FEAT_MBYTE
7709 if (has_mbyte)
7710 {
7711 fl = MB_BYTE2LEN(c);
7712 if (fl > 1)
7713 {
7714 /* There are following bytes for the same
7715 * character. We must find all bytes before
7716 * trying delete/insert/swap/etc. */
7717 sp->ts_tcharlen = fl;
7718 sp->ts_tcharidx = 1;
7719 sp->ts_isdiff = DIFF_INSERT;
7720 }
7721 }
Bram Moolenaarea408852005-06-25 22:49:46 +00007722 else
7723 fl = 1;
7724 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +00007725#endif
Bram Moolenaarea408852005-06-25 22:49:46 +00007726 {
7727 /* If the previous character was the same, thus
7728 * doubling a character, give a bonus to the
7729 * score. */
7730 if (sp->ts_twordlen >= 2
7731 && tword[sp->ts_twordlen - 2] == c)
7732 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
7733 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007734 }
7735 }
7736 break;
7737
7738 case STATE_SWAP:
Bram Moolenaarea424162005-06-16 21:51:00 +00007739 /*
7740 * Swap two bytes in the bad word: "12" -> "21".
7741 * We change "fword" here, it's changed back afterwards.
7742 */
7743 p = fword + sp->ts_fidx;
7744 c = *p;
7745 if (c == NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007746 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007747 /* End of word, can't swap or replace. */
7748 sp->ts_state = STATE_FINAL;
7749 break;
7750 }
7751#ifdef FEAT_MBYTE
7752 if (has_mbyte)
7753 {
7754 n = mb_ptr2len_check(p);
7755 c = mb_ptr2char(p);
7756 c2 = mb_ptr2char(p + n);
7757 }
7758 else
7759#endif
7760 c2 = p[1];
7761 if (c == c2)
7762 {
7763 /* Characters are identical, swap won't do anything. */
7764 sp->ts_state = STATE_SWAP3;
7765 break;
7766 }
7767 if (c2 != NUL && try_deeper(su, stack, depth, SCORE_SWAP))
7768 {
7769 sp->ts_state = STATE_UNSWAP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007770 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007771#ifdef FEAT_MBYTE
7772 if (has_mbyte)
7773 {
7774 fl = mb_char2len(c2);
7775 mch_memmove(p, p + n, fl);
7776 mb_char2bytes(c, p + fl);
7777 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7778 }
7779 else
7780#endif
7781 {
7782 p[0] = c2;
7783 p[1] = c;
7784 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
7785 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007786 }
7787 else
7788 /* If this swap doesn't work then SWAP3 won't either. */
7789 sp->ts_state = STATE_REP_INI;
7790 break;
7791
Bram Moolenaarea424162005-06-16 21:51:00 +00007792 case STATE_UNSWAP:
7793 /* Undo the STATE_SWAP swap: "21" -> "12". */
7794 p = fword + sp->ts_fidx;
7795#ifdef FEAT_MBYTE
7796 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007797 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007798 n = MB_BYTE2LEN(*p);
7799 c = mb_ptr2char(p + n);
7800 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
7801 mb_char2bytes(c, p);
7802 }
7803 else
7804#endif
7805 {
7806 c = *p;
7807 *p = p[1];
7808 p[1] = c;
7809 }
7810 /*FALLTHROUGH*/
7811
7812 case STATE_SWAP3:
7813 /* Swap two bytes, skipping one: "123" -> "321". We change
7814 * "fword" here, it's changed back afterwards. */
7815 p = fword + sp->ts_fidx;
7816#ifdef FEAT_MBYTE
7817 if (has_mbyte)
7818 {
7819 n = mb_ptr2len_check(p);
7820 c = mb_ptr2char(p);
7821 fl = mb_ptr2len_check(p + n);
7822 c2 = mb_ptr2char(p + n);
7823 c3 = mb_ptr2char(p + n + fl);
7824 }
7825 else
7826#endif
7827 {
7828 c = *p;
7829 c2 = p[1];
7830 c3 = p[2];
7831 }
7832
7833 /* When characters are identical: "121" then SWAP3 result is
7834 * identical, ROT3L result is same as SWAP: "211", ROT3L
7835 * result is same as SWAP on next char: "112". Thus skip all
7836 * swapping. Also skip when c3 is NUL. */
7837 if (c == c3 || c3 == NUL)
7838 {
7839 sp->ts_state = STATE_REP_INI;
7840 break;
7841 }
7842 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7843 {
7844 sp->ts_state = STATE_UNSWAP3;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007845 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007846#ifdef FEAT_MBYTE
7847 if (has_mbyte)
7848 {
7849 tl = mb_char2len(c3);
7850 mch_memmove(p, p + n + fl, tl);
7851 mb_char2bytes(c2, p + tl);
7852 mb_char2bytes(c, p + fl + tl);
7853 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
7854 }
7855 else
7856#endif
7857 {
7858 p[0] = p[2];
7859 p[2] = c;
7860 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7861 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007862 }
7863 else
7864 sp->ts_state = STATE_REP_INI;
7865 break;
7866
Bram Moolenaarea424162005-06-16 21:51:00 +00007867 case STATE_UNSWAP3:
7868 /* Undo STATE_SWAP3: "321" -> "123" */
7869 p = fword + sp->ts_fidx;
7870#ifdef FEAT_MBYTE
7871 if (has_mbyte)
7872 {
7873 n = MB_BYTE2LEN(*p);
7874 c2 = mb_ptr2char(p + n);
7875 fl = MB_BYTE2LEN(p[n]);
7876 c = mb_ptr2char(p + n + fl);
7877 tl = MB_BYTE2LEN(p[n + fl]);
7878 mch_memmove(p + fl + tl, p, n);
7879 mb_char2bytes(c, p);
7880 mb_char2bytes(c2, p + tl);
7881 }
7882 else
7883#endif
7884 {
7885 c = *p;
7886 *p = p[2];
7887 p[2] = c;
7888 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007889
Bram Moolenaarea424162005-06-16 21:51:00 +00007890 /* Rotate three characters left: "123" -> "231". We change
7891 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007892 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7893 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007894 sp->ts_state = STATE_UNROT3L;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007895 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007896 p = fword + sp->ts_fidx;
7897#ifdef FEAT_MBYTE
7898 if (has_mbyte)
7899 {
7900 n = mb_ptr2len_check(p);
7901 c = mb_ptr2char(p);
7902 fl = mb_ptr2len_check(p + n);
7903 fl += mb_ptr2len_check(p + n + fl);
7904 mch_memmove(p, p + n, fl);
7905 mb_char2bytes(c, p + fl);
7906 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
7907 }
7908 else
7909#endif
7910 {
7911 c = *p;
7912 *p = p[1];
7913 p[1] = p[2];
7914 p[2] = c;
7915 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7916 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007917 }
7918 else
7919 sp->ts_state = STATE_REP_INI;
7920 break;
7921
Bram Moolenaarea424162005-06-16 21:51:00 +00007922 case STATE_UNROT3L:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007923 /* Undo ROT3L: "231" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00007924 p = fword + sp->ts_fidx;
7925#ifdef FEAT_MBYTE
7926 if (has_mbyte)
7927 {
7928 n = MB_BYTE2LEN(*p);
7929 n += MB_BYTE2LEN(p[n]);
7930 c = mb_ptr2char(p + n);
7931 tl = MB_BYTE2LEN(p[n]);
7932 mch_memmove(p + tl, p, n);
7933 mb_char2bytes(c, p);
7934 }
7935 else
7936#endif
7937 {
7938 c = p[2];
7939 p[2] = p[1];
7940 p[1] = *p;
7941 *p = c;
7942 }
Bram Moolenaarea424162005-06-16 21:51:00 +00007943
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007944 /* Rotate three bytes right: "123" -> "312". We change
Bram Moolenaarea424162005-06-16 21:51:00 +00007945 * "fword" here, it's changed back afterwards. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007946 if (try_deeper(su, stack, depth, SCORE_SWAP3))
7947 {
Bram Moolenaarea424162005-06-16 21:51:00 +00007948 sp->ts_state = STATE_UNROT3R;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007949 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +00007950 p = fword + sp->ts_fidx;
7951#ifdef FEAT_MBYTE
7952 if (has_mbyte)
7953 {
7954 n = mb_ptr2len_check(p);
7955 n += mb_ptr2len_check(p + n);
7956 c = mb_ptr2char(p + n);
7957 tl = mb_ptr2len_check(p + n);
7958 mch_memmove(p + tl, p, n);
7959 mb_char2bytes(c, p);
7960 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
7961 }
7962 else
7963#endif
7964 {
7965 c = p[2];
7966 p[2] = p[1];
7967 p[1] = *p;
7968 *p = c;
7969 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
7970 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007971 }
7972 else
7973 sp->ts_state = STATE_REP_INI;
7974 break;
7975
Bram Moolenaarea424162005-06-16 21:51:00 +00007976 case STATE_UNROT3R:
Bram Moolenaar0c405862005-06-22 22:26:26 +00007977 /* Undo ROT3R: "312" -> "123" */
Bram Moolenaarea424162005-06-16 21:51:00 +00007978 p = fword + sp->ts_fidx;
7979#ifdef FEAT_MBYTE
7980 if (has_mbyte)
7981 {
7982 c = mb_ptr2char(p);
7983 tl = MB_BYTE2LEN(*p);
7984 n = MB_BYTE2LEN(p[tl]);
7985 n += MB_BYTE2LEN(p[tl + n]);
7986 mch_memmove(p, p + tl, n);
7987 mb_char2bytes(c, p + n);
7988 }
7989 else
7990#endif
7991 {
7992 c = *p;
7993 *p = p[1];
7994 p[1] = p[2];
7995 p[2] = c;
7996 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007997 /*FALLTHROUGH*/
7998
7999 case STATE_REP_INI:
8000 /* Check if matching with REP items from the .aff file would
8001 * work. Quickly skip if there are no REP items or the score
8002 * is going to be too high anyway. */
8003 gap = &lp->lp_slang->sl_rep;
8004 if (gap->ga_len == 0
8005 || sp->ts_score + SCORE_REP >= su->su_maxscore)
8006 {
8007 sp->ts_state = STATE_FINAL;
8008 break;
8009 }
8010
8011 /* Use the first byte to quickly find the first entry that
Bram Moolenaarea424162005-06-16 21:51:00 +00008012 * may match. If the index is -1 there is none. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008013 sp->ts_curi = lp->lp_slang->sl_rep_first[fword[sp->ts_fidx]];
8014 if (sp->ts_curi < 0)
8015 {
8016 sp->ts_state = STATE_FINAL;
8017 break;
8018 }
8019
8020 sp->ts_state = STATE_REP;
8021 /*FALLTHROUGH*/
8022
8023 case STATE_REP:
8024 /* Try matching with REP items from the .aff file. For each
Bram Moolenaarea424162005-06-16 21:51:00 +00008025 * match replace the characters and check if the resulting
8026 * word is valid. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008027 p = fword + sp->ts_fidx;
8028
8029 gap = &lp->lp_slang->sl_rep;
8030 while (sp->ts_curi < gap->ga_len)
8031 {
8032 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
8033 if (*ftp->ft_from != *p)
8034 {
8035 /* past possible matching entries */
8036 sp->ts_curi = gap->ga_len;
8037 break;
8038 }
8039 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
8040 && try_deeper(su, stack, depth, SCORE_REP))
8041 {
8042 /* Need to undo this afterwards. */
8043 sp->ts_state = STATE_REP_UNDO;
8044
8045 /* Change the "from" to the "to" string. */
8046 ++depth;
8047 fl = STRLEN(ftp->ft_from);
8048 tl = STRLEN(ftp->ft_to);
8049 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008050 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008051 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008052 repextra += tl - fl;
8053 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008054 mch_memmove(p, ftp->ft_to, tl);
8055 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
Bram Moolenaarea424162005-06-16 21:51:00 +00008056#ifdef FEAT_MBYTE
8057 stack[depth].ts_tcharlen = 0;
8058#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008059 break;
8060 }
8061 }
8062
8063 if (sp->ts_curi >= gap->ga_len)
8064 /* No (more) matches. */
8065 sp->ts_state = STATE_FINAL;
8066
8067 break;
8068
8069 case STATE_REP_UNDO:
8070 /* Undo a REP replacement and continue with the next one. */
8071 ftp = (fromto_T *)lp->lp_slang->sl_rep.ga_data
8072 + sp->ts_curi - 1;
8073 fl = STRLEN(ftp->ft_from);
8074 tl = STRLEN(ftp->ft_to);
8075 p = fword + sp->ts_fidx;
8076 if (fl != tl)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008077 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008078 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
Bram Moolenaar0c405862005-06-22 22:26:26 +00008079 repextra -= tl - fl;
8080 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008081 mch_memmove(p, ftp->ft_from, fl);
8082 sp->ts_state = STATE_REP;
8083 break;
8084
8085 default:
8086 /* Did all possible states at this level, go up one level. */
8087 --depth;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008088
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008089 if (depth >= 0 && stack[depth].ts_prefixdepth == PREFIXTREE)
8090 {
8091 /* Continue in or go back to the prefix tree. */
8092 byts = pbyts;
8093 idxs = pidxs;
8094 splitoff = 0;
8095 }
8096
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008097 /* Don't check for CTRL-C too often, it takes time. */
8098 line_breakcheck();
8099 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008100 }
8101 }
8102}
8103
8104/*
8105 * Try going one level deeper in the tree.
8106 */
8107 static int
8108try_deeper(su, stack, depth, score_add)
8109 suginfo_T *su;
8110 trystate_T *stack;
8111 int depth;
8112 int score_add;
8113{
8114 int newscore;
8115
8116 /* Refuse to go deeper if the scrore is getting too big. */
8117 newscore = stack[depth].ts_score + score_add;
8118 if (newscore >= su->su_maxscore)
8119 return FALSE;
8120
Bram Moolenaarea424162005-06-16 21:51:00 +00008121 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008122 stack[depth + 1].ts_state = STATE_START;
8123 stack[depth + 1].ts_score = newscore;
8124 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008125 return TRUE;
8126}
8127
Bram Moolenaar53805d12005-08-01 07:08:33 +00008128#ifdef FEAT_MBYTE
8129/*
8130 * Case-folding may change the number of bytes: Count nr of chars in
8131 * fword[flen] and return the byte length of that many chars in "word".
8132 */
8133 static int
8134nofold_len(fword, flen, word)
8135 char_u *fword;
8136 int flen;
8137 char_u *word;
8138{
8139 char_u *p;
8140 int i = 0;
8141
8142 for (p = fword; p < fword + flen; mb_ptr_adv(p))
8143 ++i;
8144 for (p = word; i > 0; mb_ptr_adv(p))
8145 --i;
8146 return (int)(p - word);
8147}
8148#endif
8149
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008150/*
8151 * "fword" is a good word with case folded. Find the matching keep-case
8152 * words and put it in "kword".
8153 * Theoretically there could be several keep-case words that result in the
8154 * same case-folded word, but we only find one...
8155 */
8156 static void
8157find_keepcap_word(slang, fword, kword)
8158 slang_T *slang;
8159 char_u *fword;
8160 char_u *kword;
8161{
8162 char_u uword[MAXWLEN]; /* "fword" in upper-case */
8163 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008164 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008165
8166 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008167 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008168 int round[MAXWLEN];
8169 int fwordidx[MAXWLEN];
8170 int uwordidx[MAXWLEN];
8171 int kwordlen[MAXWLEN];
8172
8173 int flen, ulen;
8174 int l;
8175 int len;
8176 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008177 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008178 char_u *p;
8179 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008180 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008181
8182 if (byts == NULL)
8183 {
8184 /* array is empty: "cannot happen" */
8185 *kword = NUL;
8186 return;
8187 }
8188
8189 /* Make an all-cap version of "fword". */
8190 allcap_copy(fword, uword);
8191
8192 /*
8193 * Each character needs to be tried both case-folded and upper-case.
8194 * All this gets very complicated if we keep in mind that changing case
8195 * may change the byte length of a multi-byte character...
8196 */
8197 depth = 0;
8198 arridx[0] = 0;
8199 round[0] = 0;
8200 fwordidx[0] = 0;
8201 uwordidx[0] = 0;
8202 kwordlen[0] = 0;
8203 while (depth >= 0)
8204 {
8205 if (fword[fwordidx[depth]] == NUL)
8206 {
8207 /* We are at the end of "fword". If the tree allows a word to end
8208 * here we have found a match. */
8209 if (byts[arridx[depth] + 1] == 0)
8210 {
8211 kword[kwordlen[depth]] = NUL;
8212 return;
8213 }
8214
8215 /* kword is getting too long, continue one level up */
8216 --depth;
8217 }
8218 else if (++round[depth] > 2)
8219 {
8220 /* tried both fold-case and upper-case character, continue one
8221 * level up */
8222 --depth;
8223 }
8224 else
8225 {
8226 /*
8227 * round[depth] == 1: Try using the folded-case character.
8228 * round[depth] == 2: Try using the upper-case character.
8229 */
8230#ifdef FEAT_MBYTE
8231 if (has_mbyte)
8232 {
8233 flen = mb_ptr2len_check(fword + fwordidx[depth]);
8234 ulen = mb_ptr2len_check(uword + uwordidx[depth]);
8235 }
8236 else
8237#endif
8238 ulen = flen = 1;
8239 if (round[depth] == 1)
8240 {
8241 p = fword + fwordidx[depth];
8242 l = flen;
8243 }
8244 else
8245 {
8246 p = uword + uwordidx[depth];
8247 l = ulen;
8248 }
8249
8250 for (tryidx = arridx[depth]; l > 0; --l)
8251 {
8252 /* Perform a binary search in the list of accepted bytes. */
8253 len = byts[tryidx++];
8254 c = *p++;
8255 lo = tryidx;
8256 hi = tryidx + len - 1;
8257 while (lo < hi)
8258 {
8259 m = (lo + hi) / 2;
8260 if (byts[m] > c)
8261 hi = m - 1;
8262 else if (byts[m] < c)
8263 lo = m + 1;
8264 else
8265 {
8266 lo = hi = m;
8267 break;
8268 }
8269 }
8270
8271 /* Stop if there is no matching byte. */
8272 if (hi < lo || byts[lo] != c)
8273 break;
8274
8275 /* Continue at the child (if there is one). */
8276 tryidx = idxs[lo];
8277 }
8278
8279 if (l == 0)
8280 {
8281 /*
8282 * Found the matching char. Copy it to "kword" and go a
8283 * level deeper.
8284 */
8285 if (round[depth] == 1)
8286 {
8287 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
8288 flen);
8289 kwordlen[depth + 1] = kwordlen[depth] + flen;
8290 }
8291 else
8292 {
8293 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
8294 ulen);
8295 kwordlen[depth + 1] = kwordlen[depth] + ulen;
8296 }
8297 fwordidx[depth + 1] = fwordidx[depth] + flen;
8298 uwordidx[depth + 1] = uwordidx[depth] + ulen;
8299
8300 ++depth;
8301 arridx[depth] = tryidx;
8302 round[depth] = 0;
8303 }
8304 }
8305 }
8306
8307 /* Didn't find it: "cannot happen". */
8308 *kword = NUL;
8309}
8310
8311/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008312 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
8313 * su->su_sga.
8314 */
8315 static void
8316score_comp_sal(su)
8317 suginfo_T *su;
8318{
8319 langp_T *lp;
8320 char_u badsound[MAXWLEN];
8321 int i;
8322 suggest_T *stp;
8323 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008324 int score;
8325
8326 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
8327 return;
8328
8329 /* Use the sound-folding of the first language that supports it. */
8330 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8331 lp->lp_slang != NULL; ++lp)
8332 if (lp->lp_slang->sl_sal.ga_len > 0)
8333 {
8334 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008335 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008336
8337 for (i = 0; i < su->su_ga.ga_len; ++i)
8338 {
8339 stp = &SUG(su->su_ga, i);
8340
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008341 /* Case-fold the suggested word, sound-fold it and compute the
8342 * sound-a-like score. */
8343 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008344 if (score < SCORE_MAXMAX)
8345 {
8346 /* Add the suggestion. */
8347 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
8348 sstp->st_word = vim_strsave(stp->st_word);
8349 if (sstp->st_word != NULL)
8350 {
8351 sstp->st_score = score;
8352 sstp->st_altscore = 0;
8353 sstp->st_orglen = stp->st_orglen;
8354 ++su->su_sga.ga_len;
8355 }
8356 }
8357 }
8358 break;
8359 }
8360}
8361
8362/*
8363 * Combine the list of suggestions in su->su_ga and su->su_sga.
8364 * They are intwined.
8365 */
8366 static void
8367score_combine(su)
8368 suginfo_T *su;
8369{
8370 int i;
8371 int j;
8372 garray_T ga;
8373 garray_T *gap;
8374 langp_T *lp;
8375 suggest_T *stp;
8376 char_u *p;
8377 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008378 int round;
8379
8380 /* Add the alternate score to su_ga. */
8381 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8382 lp->lp_slang != NULL; ++lp)
8383 {
8384 if (lp->lp_slang->sl_sal.ga_len > 0)
8385 {
8386 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008387 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008388
8389 for (i = 0; i < su->su_ga.ga_len; ++i)
8390 {
8391 stp = &SUG(su->su_ga, i);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008392 stp->st_altscore = stp_sal_score(stp, su, lp->lp_slang,
8393 badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008394 if (stp->st_altscore == SCORE_MAXMAX)
8395 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
8396 else
8397 stp->st_score = (stp->st_score * 3
8398 + stp->st_altscore) / 4;
8399 stp->st_salscore = FALSE;
8400 }
8401 break;
8402 }
8403 }
8404
8405 /* Add the alternate score to su_sga. */
8406 for (i = 0; i < su->su_sga.ga_len; ++i)
8407 {
8408 stp = &SUG(su->su_sga, i);
8409 stp->st_altscore = spell_edit_score(su->su_badword, stp->st_word);
8410 if (stp->st_score == SCORE_MAXMAX)
8411 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
8412 else
8413 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
8414 stp->st_salscore = TRUE;
8415 }
8416
8417 /* Sort the suggestions and truncate at "maxcount" for both lists. */
8418 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
8419 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
8420
8421 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
8422 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
8423 return;
8424
8425 stp = &SUG(ga, 0);
8426 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
8427 {
8428 /* round 1: get a suggestion from su_ga
8429 * round 2: get a suggestion from su_sga */
8430 for (round = 1; round <= 2; ++round)
8431 {
8432 gap = round == 1 ? &su->su_ga : &su->su_sga;
8433 if (i < gap->ga_len)
8434 {
8435 /* Don't add a word if it's already there. */
8436 p = SUG(*gap, i).st_word;
8437 for (j = 0; j < ga.ga_len; ++j)
8438 if (STRCMP(stp[j].st_word, p) == 0)
8439 break;
8440 if (j == ga.ga_len)
8441 stp[ga.ga_len++] = SUG(*gap, i);
8442 else
8443 vim_free(p);
8444 }
8445 }
8446 }
8447
8448 ga_clear(&su->su_ga);
8449 ga_clear(&su->su_sga);
8450
8451 /* Truncate the list to the number of suggestions that will be displayed. */
8452 if (ga.ga_len > su->su_maxcount)
8453 {
8454 for (i = su->su_maxcount; i < ga.ga_len; ++i)
8455 vim_free(stp[i].st_word);
8456 ga.ga_len = su->su_maxcount;
8457 }
8458
8459 su->su_ga = ga;
8460}
8461
8462/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008463 * For the goodword in "stp" compute the soundalike score compared to the
8464 * badword.
8465 */
8466 static int
8467stp_sal_score(stp, su, slang, badsound)
8468 suggest_T *stp;
8469 suginfo_T *su;
8470 slang_T *slang;
8471 char_u *badsound; /* sound-folded badword */
8472{
8473 char_u *p;
8474 char_u badsound2[MAXWLEN];
8475 char_u fword[MAXWLEN];
8476 char_u goodsound[MAXWLEN];
8477
8478 if (stp->st_orglen <= su->su_badlen)
8479 p = badsound;
8480 else
8481 {
8482 /* soundfold the bad word with more characters following */
8483 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
8484
8485 /* When joining two words the sound often changes a lot. E.g., "t he"
8486 * sounds like "t h" while "the" sounds like "@". Avoid that by
8487 * removing the space. Don't do it when the good word also contains a
8488 * space. */
8489 if (vim_iswhite(su->su_badptr[su->su_badlen])
8490 && *skiptowhite(stp->st_word) == NUL)
8491 for (p = fword; *(p = skiptowhite(p)) != NUL; )
8492 mch_memmove(p, p + 1, STRLEN(p));
8493
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008494 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008495 p = badsound2;
8496 }
8497
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008498 /* Sound-fold the word and compute the score for the difference. */
8499 spell_soundfold(slang, stp->st_word, FALSE, goodsound);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008500
8501 return soundalike_score(goodsound, p);
8502}
8503
8504/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008505 * Find suggestions by comparing the word in a sound-a-like form.
8506 */
8507 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +00008508suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008509 suginfo_T *su;
8510{
8511 char_u salword[MAXWLEN];
8512 char_u tword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008513 char_u tsalword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008514 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008515 int curi[MAXWLEN];
8516 langp_T *lp;
8517 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008518 idx_T *idxs;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008519 int depth;
8520 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008521 idx_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008522 int round;
8523 int flags;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008524 int sound_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008525
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008526 /* Do this for all languages that support sound folding. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008527 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8528 lp->lp_slang != NULL; ++lp)
8529 {
8530 if (lp->lp_slang->sl_sal.ga_len > 0)
8531 {
8532 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008533 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008534
8535 /*
8536 * Go through the whole tree, soundfold each word and compare.
8537 * round 1: use the case-folded tree.
8538 * round 2: use the keep-case tree.
8539 */
8540 for (round = 1; round <= 2; ++round)
8541 {
8542 if (round == 1)
8543 {
8544 byts = lp->lp_slang->sl_fbyts;
8545 idxs = lp->lp_slang->sl_fidxs;
8546 }
8547 else
8548 {
8549 byts = lp->lp_slang->sl_kbyts;
8550 idxs = lp->lp_slang->sl_kidxs;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00008551 if (byts == NULL) /* no keep-case words */
8552 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008553 }
8554
8555 depth = 0;
8556 arridx[0] = 0;
8557 curi[0] = 1;
8558 while (depth >= 0 && !got_int)
8559 {
8560 if (curi[depth] > byts[arridx[depth]])
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008561 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008562 /* Done all bytes at this node, go up one level. */
8563 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008564 line_breakcheck();
8565 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008566 else
8567 {
8568 /* Do one more byte at this node. */
8569 n = arridx[depth] + curi[depth];
8570 ++curi[depth];
8571 c = byts[n];
8572 if (c == 0)
8573 {
8574 /* End of word, deal with the word. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008575 flags = (int)idxs[n];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008576 if (round == 2 || (flags & WF_KEEPCAP) == 0)
8577 {
8578 tword[depth] = NUL;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008579 /* Sound-fold. Only in keep-case tree need to
8580 * case-fold the word. */
8581 spell_soundfold(lp->lp_slang, tword,
8582 round == 1, tsalword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008583
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008584 /* Compute the edit distance between the
8585 * sound-a-like words. */
8586 sound_score = soundalike_score(salword,
8587 tsalword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008588 if (sound_score < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008589 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008590 char_u cword[MAXWLEN];
8591 char_u *p;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008592 int score;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008593
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00008594 flags |= su->su_badflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008595 if (round == 1 && (flags & WF_CAPMASK) != 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008596 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008597 /* Need to fix case according to
8598 * "flags". */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008599 make_case_word(tword, cword, flags);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008600 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008601 }
8602 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008603 p = tword;
8604
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008605 if (sps_flags & SPS_DOUBLE)
8606 add_suggestion(su, &su->su_sga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008607 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008608 sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008609 else
8610 {
8611 /* Compute the score. */
8612 score = spell_edit_score(
8613 su->su_badword, p);
8614 if (sps_flags & SPS_BEST)
8615 /* give a bonus for the good word
8616 * sounding the same as the bad
8617 * word */
8618 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008619 su->su_badlen,
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008620 RESCORE(score, sound_score),
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008621 sound_score, TRUE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008622 else
8623 add_suggestion(su, &su->su_ga, p,
Bram Moolenaar0c405862005-06-22 22:26:26 +00008624 su->su_badlen,
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008625 score + sound_score, 0, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008626 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008627 }
8628 }
8629
8630 /* Skip over other NUL bytes. */
8631 while (byts[n + 1] == 0)
8632 {
8633 ++n;
8634 ++curi[depth];
8635 }
8636 }
8637 else
8638 {
8639 /* Normal char, go one level deeper. */
8640 tword[depth++] = c;
8641 arridx[depth] = idxs[n];
8642 curi[depth] = 1;
8643 }
8644 }
8645 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008646 }
8647 }
8648 }
8649}
8650
8651/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008652 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008653 */
8654 static void
8655make_case_word(fword, cword, flags)
8656 char_u *fword;
8657 char_u *cword;
8658 int flags;
8659{
8660 if (flags & WF_ALLCAP)
8661 /* Make it all upper-case */
8662 allcap_copy(fword, cword);
8663 else if (flags & WF_ONECAP)
8664 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008665 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008666 else
8667 /* Use goodword as-is. */
8668 STRCPY(cword, fword);
8669}
8670
Bram Moolenaarea424162005-06-16 21:51:00 +00008671/*
8672 * Use map string "map" for languages "lp".
8673 */
8674 static void
8675set_map_str(lp, map)
8676 slang_T *lp;
8677 char_u *map;
8678{
8679 char_u *p;
8680 int headc = 0;
8681 int c;
8682 int i;
8683
8684 if (*map == NUL)
8685 {
8686 lp->sl_has_map = FALSE;
8687 return;
8688 }
8689 lp->sl_has_map = TRUE;
8690
8691 /* Init the array and hash table empty. */
8692 for (i = 0; i < 256; ++i)
8693 lp->sl_map_array[i] = 0;
8694#ifdef FEAT_MBYTE
8695 hash_init(&lp->sl_map_hash);
8696#endif
8697
8698 /*
8699 * The similar characters are stored separated with slashes:
8700 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
8701 * before the same slash. For characters above 255 sl_map_hash is used.
8702 */
8703 for (p = map; *p != NUL; )
8704 {
8705#ifdef FEAT_MBYTE
8706 c = mb_ptr2char_adv(&p);
8707#else
8708 c = *p++;
8709#endif
8710 if (c == '/')
8711 headc = 0;
8712 else
8713 {
8714 if (headc == 0)
8715 headc = c;
8716
8717#ifdef FEAT_MBYTE
8718 /* Characters above 255 don't fit in sl_map_array[], put them in
8719 * the hash table. Each entry is the char, a NUL the headchar and
8720 * a NUL. */
8721 if (c >= 256)
8722 {
8723 int cl = mb_char2len(c);
8724 int headcl = mb_char2len(headc);
8725 char_u *b;
8726 hash_T hash;
8727 hashitem_T *hi;
8728
8729 b = alloc((unsigned)(cl + headcl + 2));
8730 if (b == NULL)
8731 return;
8732 mb_char2bytes(c, b);
8733 b[cl] = NUL;
8734 mb_char2bytes(headc, b + cl + 1);
8735 b[cl + 1 + headcl] = NUL;
8736 hash = hash_hash(b);
8737 hi = hash_lookup(&lp->sl_map_hash, b, hash);
8738 if (HASHITEM_EMPTY(hi))
8739 hash_add_item(&lp->sl_map_hash, hi, b, hash);
8740 else
8741 {
8742 /* This should have been checked when generating the .spl
8743 * file. */
8744 EMSG(_("E999: duplicate char in MAP entry"));
8745 vim_free(b);
8746 }
8747 }
8748 else
8749#endif
8750 lp->sl_map_array[c] = headc;
8751 }
8752 }
8753}
8754
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008755/*
8756 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
8757 * lines in the .aff file.
8758 */
8759 static int
8760similar_chars(slang, c1, c2)
8761 slang_T *slang;
8762 int c1;
8763 int c2;
8764{
Bram Moolenaarea424162005-06-16 21:51:00 +00008765 int m1, m2;
8766#ifdef FEAT_MBYTE
8767 char_u buf[MB_MAXBYTES];
8768 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008769
Bram Moolenaarea424162005-06-16 21:51:00 +00008770 if (c1 >= 256)
8771 {
8772 buf[mb_char2bytes(c1, buf)] = 0;
8773 hi = hash_find(&slang->sl_map_hash, buf);
8774 if (HASHITEM_EMPTY(hi))
8775 m1 = 0;
8776 else
8777 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8778 }
8779 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008780#endif
Bram Moolenaarea424162005-06-16 21:51:00 +00008781 m1 = slang->sl_map_array[c1];
8782 if (m1 == 0)
8783 return FALSE;
8784
8785
8786#ifdef FEAT_MBYTE
8787 if (c2 >= 256)
8788 {
8789 buf[mb_char2bytes(c2, buf)] = 0;
8790 hi = hash_find(&slang->sl_map_hash, buf);
8791 if (HASHITEM_EMPTY(hi))
8792 m2 = 0;
8793 else
8794 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
8795 }
8796 else
8797#endif
8798 m2 = slang->sl_map_array[c2];
8799
8800 return m1 == m2;
8801}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008802
8803/*
8804 * Add a suggestion to the list of suggestions.
8805 * Do not add a duplicate suggestion or suggestions with a bad score.
8806 * When "use_score" is not zero it's used, otherwise the score is computed
8807 * with spell_edit_score().
8808 */
8809 static void
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008810add_suggestion(su, gap, goodword, badlen, score, altscore, had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008811 suginfo_T *su;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008812 garray_T *gap;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008813 char_u *goodword;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008814 int badlen; /* length of bad word used */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008815 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008816 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008817 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008818{
8819 suggest_T *stp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008820 int i;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008821 char_u *p = NULL;
8822 int c = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008823
8824 /* Check that the word wasn't banned. */
8825 if (was_banned(su, goodword))
8826 return;
8827
Bram Moolenaar0c405862005-06-22 22:26:26 +00008828 /* If past "su_badlen" and the rest is identical stop at "su_badlen".
8829 * Remove the common part from "goodword". */
8830 i = badlen - su->su_badlen;
8831 if (i > 0)
8832 {
8833 /* This assumes there was no case folding or it didn't change the
8834 * length... */
8835 p = goodword + STRLEN(goodword) - i;
8836 if (p > goodword && STRNICMP(su->su_badptr + su->su_badlen, p, i) == 0)
8837 {
8838 badlen = su->su_badlen;
8839 c = *p;
8840 *p = NUL;
8841 }
8842 else
8843 p = NULL;
8844 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008845 else if (i < 0)
8846 {
8847 /* When replacing part of the word check that we actually change
8848 * something. For "the the" a suggestion can be replacing the first
8849 * "the" with itself, since "the" wasn't banned. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008850 if (badlen == (int)STRLEN(goodword)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00008851 && STRNCMP(su->su_badword, goodword, badlen) == 0)
8852 return;
8853 }
8854
Bram Moolenaar0c405862005-06-22 22:26:26 +00008855
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008856 if (score <= su->su_maxscore)
8857 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008858 /* Check if the word is already there. Also check the length that is
8859 * being replaced "thes," -> "these" is a different suggestion from
8860 * "thes" -> "these". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008861 stp = &SUG(*gap, 0);
8862 for (i = gap->ga_len - 1; i >= 0; --i)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008863 if (STRCMP(stp[i].st_word, goodword) == 0
8864 && stp[i].st_orglen == badlen)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008865 {
8866 /* Found it. Remember the lowest score. */
8867 if (stp[i].st_score > score)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008868 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008869 stp[i].st_score = score;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00008870 stp[i].st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008871 stp[i].st_had_bonus = had_bonus;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008872 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008873 break;
8874 }
8875
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008876 if (i < 0 && ga_grow(gap, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008877 {
8878 /* Add a suggestion. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008879 stp = &SUG(*gap, gap->ga_len);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008880 stp->st_word = vim_strsave(goodword);
8881 if (stp->st_word != NULL)
8882 {
8883 stp->st_score = score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008884 stp->st_altscore = altscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008885 stp->st_had_bonus = had_bonus;
Bram Moolenaar0c405862005-06-22 22:26:26 +00008886 stp->st_orglen = badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008887 ++gap->ga_len;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008888
8889 /* If we have too many suggestions now, sort the list and keep
8890 * the best suggestions. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008891 if (gap->ga_len > SUG_MAX_COUNT(su))
8892 su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore,
8893 SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008894 }
8895 }
8896 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008897
8898 if (p != NULL)
8899 *p = c; /* restore "goodword" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008900}
8901
8902/*
8903 * Add a word to be banned.
8904 */
8905 static void
8906add_banned(su, word)
8907 suginfo_T *su;
8908 char_u *word;
8909{
8910 char_u *s = vim_strsave(word);
8911 hash_T hash;
8912 hashitem_T *hi;
8913
8914 if (s != NULL)
8915 {
8916 hash = hash_hash(s);
8917 hi = hash_lookup(&su->su_banned, s, hash);
8918 if (HASHITEM_EMPTY(hi))
8919 hash_add_item(&su->su_banned, hi, s, hash);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008920 else
8921 vim_free(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008922 }
8923}
8924
8925/*
8926 * Return TRUE if a word appears in the list of banned words.
8927 */
8928 static int
8929was_banned(su, word)
8930 suginfo_T *su;
8931 char_u *word;
8932{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008933 hashitem_T *hi = hash_find(&su->su_banned, word);
8934
8935 return !HASHITEM_EMPTY(hi);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008936}
8937
8938/*
8939 * Free the banned words in "su".
8940 */
8941 static void
8942free_banned(su)
8943 suginfo_T *su;
8944{
8945 int todo;
8946 hashitem_T *hi;
8947
8948 todo = su->su_banned.ht_used;
8949 for (hi = su->su_banned.ht_array; todo > 0; ++hi)
8950 {
8951 if (!HASHITEM_EMPTY(hi))
8952 {
8953 vim_free(hi->hi_key);
8954 --todo;
8955 }
8956 }
8957 hash_clear(&su->su_banned);
8958}
8959
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008960/*
8961 * Recompute the score if sound-folding is possible. This is slow,
8962 * thus only done for the final results.
8963 */
8964 static void
8965rescore_suggestions(su)
8966 suginfo_T *su;
8967{
8968 langp_T *lp;
8969 suggest_T *stp;
8970 char_u sal_badword[MAXWLEN];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008971 int i;
8972
8973 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
8974 lp->lp_slang != NULL; ++lp)
8975 {
8976 if (lp->lp_slang->sl_sal.ga_len > 0)
8977 {
8978 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008979 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008980
8981 for (i = 0; i < su->su_ga.ga_len; ++i)
8982 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00008983 stp = &SUG(su->su_ga, i);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008984 if (!stp->st_had_bonus)
8985 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008986 stp->st_altscore = stp_sal_score(stp, su,
8987 lp->lp_slang, sal_badword);
8988 if (stp->st_altscore == SCORE_MAXMAX)
8989 stp->st_altscore = SCORE_BIG;
8990 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008991 }
8992 }
8993 break;
8994 }
8995 }
8996}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00008997
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008998static int
8999#ifdef __BORLANDC__
9000_RTLENTRYF
9001#endif
9002sug_compare __ARGS((const void *s1, const void *s2));
9003
9004/*
9005 * Function given to qsort() to sort the suggestions on st_score.
9006 */
9007 static int
9008#ifdef __BORLANDC__
9009_RTLENTRYF
9010#endif
9011sug_compare(s1, s2)
9012 const void *s1;
9013 const void *s2;
9014{
9015 suggest_T *p1 = (suggest_T *)s1;
9016 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009017 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009018
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009019 if (n == 0)
9020 return p1->st_altscore - p2->st_altscore;
9021 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009022}
9023
9024/*
9025 * Cleanup the suggestions:
9026 * - Sort on score.
9027 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009028 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009029 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009030 static int
9031cleanup_suggestions(gap, maxscore, keep)
9032 garray_T *gap;
9033 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009034 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009035{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009036 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009037 int i;
9038
9039 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009040 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009041
9042 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009043 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009044 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009045 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009046 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009047 gap->ga_len = keep;
9048 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009049 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009050 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009051}
9052
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009053#if defined(FEAT_EVAL) || defined(PROTO)
9054/*
9055 * Soundfold a string, for soundfold().
9056 * Result is in allocated memory, NULL for an error.
9057 */
9058 char_u *
9059eval_soundfold(word)
9060 char_u *word;
9061{
9062 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009063 char_u sound[MAXWLEN];
9064
9065 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
9066 /* Use the sound-folding of the first language that supports it. */
9067 for (lp = LANGP_ENTRY(curwin->w_buffer->b_langp, 0);
9068 lp->lp_slang != NULL; ++lp)
9069 if (lp->lp_slang->sl_sal.ga_len > 0)
9070 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009071 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009072 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009073 return vim_strsave(sound);
9074 }
9075
9076 /* No language with sound folding, return word as-is. */
9077 return vim_strsave(word);
9078}
9079#endif
9080
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009081/*
9082 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
9083 */
9084 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009085spell_soundfold(slang, inword, folded, res)
9086 slang_T *slang;
9087 char_u *inword;
9088 int folded; /* "inword" is already case-folded */
9089 char_u *res;
9090{
9091 char_u fword[MAXWLEN];
9092 char_u *word;
9093
9094 if (slang->sl_sofo)
9095 /* SOFOFROM and SOFOTO used */
9096 spell_soundfold_sofo(slang, inword, res);
9097 else
9098 {
9099 /* SAL items used. Requires the word to be case-folded. */
9100 if (folded)
9101 word = inword;
9102 else
9103 {
9104 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
9105 word = fword;
9106 }
9107
9108#ifdef FEAT_MBYTE
9109 if (has_mbyte)
9110 spell_soundfold_wsal(slang, word, res);
9111 else
9112#endif
9113 spell_soundfold_sal(slang, word, res);
9114 }
9115}
9116
9117/*
9118 * Perform sound folding of "inword" into "res" according to SOFOFROM and
9119 * SOFOTO lines.
9120 */
9121 static void
9122spell_soundfold_sofo(slang, inword, res)
9123 slang_T *slang;
9124 char_u *inword;
9125 char_u *res;
9126{
9127 char_u *s;
9128 int ri = 0;
9129 int c;
9130
9131#ifdef FEAT_MBYTE
9132 if (has_mbyte)
9133 {
9134 int prevc = 0;
9135 int *ip;
9136
9137 /* The sl_sal_first[] table contains the translation for chars up to
9138 * 255, sl_sal the rest. */
9139 for (s = inword; *s != NUL; )
9140 {
9141 c = mb_ptr2char_adv(&s);
9142 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
9143 c = ' ';
9144 else if (c < 256)
9145 c = slang->sl_sal_first[c];
9146 else
9147 {
9148 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
9149 if (ip == NULL) /* empty list, can't match */
9150 c = NUL;
9151 else
9152 for (;;) /* find "c" in the list */
9153 {
9154 if (*ip == 0) /* not found */
9155 {
9156 c = NUL;
9157 break;
9158 }
9159 if (*ip == c) /* match! */
9160 {
9161 c = ip[1];
9162 break;
9163 }
9164 ip += 2;
9165 }
9166 }
9167
9168 if (c != NUL && c != prevc)
9169 {
9170 ri += mb_char2bytes(c, res + ri);
9171 if (ri + MB_MAXBYTES > MAXWLEN)
9172 break;
9173 prevc = c;
9174 }
9175 }
9176 }
9177 else
9178#endif
9179 {
9180 /* The sl_sal_first[] table contains the translation. */
9181 for (s = inword; (c = *s) != NUL; ++s)
9182 {
9183 if (vim_iswhite(c))
9184 c = ' ';
9185 else
9186 c = slang->sl_sal_first[c];
9187 if (c != NUL && (ri == 0 || res[ri - 1] != c))
9188 res[ri++] = c;
9189 }
9190 }
9191
9192 res[ri] = NUL;
9193}
9194
9195 static void
9196spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009197 slang_T *slang;
9198 char_u *inword;
9199 char_u *res;
9200{
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009201 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009202 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009203 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009204 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009205 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009206 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009207 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009208 int n, k = 0;
9209 int z0;
9210 int k0;
9211 int n0;
9212 int c;
9213 int pri;
9214 int p0 = -333;
9215 int c0;
9216
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009217 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009218 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009219 if (slang->sl_rem_accents)
9220 {
9221 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009222 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009223 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009224 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009225 {
9226 *t++ = ' ';
9227 s = skipwhite(s);
9228 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009229 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009230 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009231 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009232 *t++ = *s;
9233 ++s;
9234 }
9235 }
9236 *t = NUL;
9237 }
9238 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009239 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009240
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009241 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009242
9243 /*
9244 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009245 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009246 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009247 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009248 while ((c = word[i]) != NUL)
9249 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009250 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009251 n = slang->sl_sal_first[c];
9252 z0 = 0;
9253
9254 if (n >= 0)
9255 {
9256 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009257 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009258 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009259 /* Quickly skip entries that don't match the word. Most
9260 * entries are less then three chars, optimize for that. */
9261 k = smp[n].sm_leadlen;
9262 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009263 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009264 if (word[i + 1] != s[1])
9265 continue;
9266 if (k > 2)
9267 {
9268 for (j = 2; j < k; ++j)
9269 if (word[i + j] != s[j])
9270 break;
9271 if (j < k)
9272 continue;
9273 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009274 }
9275
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009276 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009277 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009278 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009279 while (*pf != NUL && *pf != word[i + k])
9280 ++pf;
9281 if (*pf == NUL)
9282 continue;
9283 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009284 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009285 s = smp[n].sm_rules;
9286 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009287
9288 p0 = *s;
9289 k0 = k;
9290 while (*s == '-' && k > 1)
9291 {
9292 k--;
9293 s++;
9294 }
9295 if (*s == '<')
9296 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009297 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009298 {
9299 /* determine priority */
9300 pri = *s - '0';
9301 s++;
9302 }
9303 if (*s == '^' && *(s + 1) == '^')
9304 s++;
9305
9306 if (*s == NUL
9307 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009308 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009309 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009310 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009311 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009312 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009313 && spell_iswordp(word + i - 1, curbuf)
9314 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009315 {
9316 /* search for followup rules, if: */
9317 /* followup and k > 1 and NO '-' in searchstring */
9318 c0 = word[i + k - 1];
9319 n0 = slang->sl_sal_first[c0];
9320
9321 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009322 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009323 {
9324 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009325 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009326 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009327 /* Quickly skip entries that don't match the word.
9328 * */
9329 k0 = smp[n0].sm_leadlen;
9330 if (k0 > 1)
9331 {
9332 if (word[i + k] != s[1])
9333 continue;
9334 if (k0 > 2)
9335 {
9336 pf = word + i + k + 1;
9337 for (j = 2; j < k0; ++j)
9338 if (*pf++ != s[j])
9339 break;
9340 if (j < k0)
9341 continue;
9342 }
9343 }
9344 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009345
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009346 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009347 {
9348 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009349 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009350 while (*pf != NUL && *pf != word[i + k0])
9351 ++pf;
9352 if (*pf == NUL)
9353 continue;
9354 ++k0;
9355 }
9356
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009357 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009358 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009359 while (*s == '-')
9360 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009361 /* "k0" gets NOT reduced because
9362 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009363 s++;
9364 }
9365 if (*s == '<')
9366 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009367 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009368 {
9369 p0 = *s - '0';
9370 s++;
9371 }
9372
9373 if (*s == NUL
9374 /* *s == '^' cuts */
9375 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009376 && !spell_iswordp(word + i + k0,
9377 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009378 {
9379 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009380 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009381 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009382
9383 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009384 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009385 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009386 /* rule fits; stop search */
9387 break;
9388 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009389 }
9390
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009391 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009392 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009393 }
9394
9395 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009396 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009397 if (s == NULL)
9398 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009399 pf = smp[n].sm_rules;
9400 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009401 if (p0 == 1 && z == 0)
9402 {
9403 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009404 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
9405 || res[reslen - 1] == *s))
9406 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009407 z0 = 1;
9408 z = 1;
9409 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009410 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009411 {
9412 word[i + k0] = *s;
9413 k0++;
9414 s++;
9415 }
9416 if (k > k0)
9417 mch_memmove(word + i + k0, word + i + k,
9418 STRLEN(word + i + k) + 1);
9419
9420 /* new "actual letter" */
9421 c = word[i];
9422 }
9423 else
9424 {
9425 /* no '<' rule used */
9426 i += k - 1;
9427 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009428 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009429 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009430 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009431 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009432 s++;
9433 }
9434 /* new "actual letter" */
9435 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009436 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009437 {
9438 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009439 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009440 mch_memmove(word, word + i + 1,
9441 STRLEN(word + i + 1) + 1);
9442 i = 0;
9443 z0 = 1;
9444 }
9445 }
9446 break;
9447 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009448 }
9449 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009450 else if (vim_iswhite(c))
9451 {
9452 c = ' ';
9453 k = 1;
9454 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009455
9456 if (z0 == 0)
9457 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009458 if (k && !p0 && reslen < MAXWLEN && c != NUL
9459 && (!slang->sl_collapse || reslen == 0
9460 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009461 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009462 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009463
9464 i++;
9465 z = 0;
9466 k = 0;
9467 }
9468 }
9469
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009470 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009471}
9472
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009473#ifdef FEAT_MBYTE
9474/*
9475 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
9476 * Multi-byte version of spell_soundfold().
9477 */
9478 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009479spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009480 slang_T *slang;
9481 char_u *inword;
9482 char_u *res;
9483{
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009484 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009485 int word[MAXWLEN];
9486 int wres[MAXWLEN];
9487 int l;
9488 char_u *s;
9489 int *ws;
9490 char_u *t;
9491 int *pf;
9492 int i, j, z;
9493 int reslen;
9494 int n, k = 0;
9495 int z0;
9496 int k0;
9497 int n0;
9498 int c;
9499 int pri;
9500 int p0 = -333;
9501 int c0;
9502 int did_white = FALSE;
9503
9504 /*
9505 * Convert the multi-byte string to a wide-character string.
9506 * Remove accents, if wanted. We actually remove all non-word characters.
9507 * But keep white space.
9508 */
9509 n = 0;
9510 for (s = inword; *s != NUL; )
9511 {
9512 t = s;
9513 c = mb_ptr2char_adv(&s);
9514 if (slang->sl_rem_accents)
9515 {
9516 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
9517 {
9518 if (did_white)
9519 continue;
9520 c = ' ';
9521 did_white = TRUE;
9522 }
9523 else
9524 {
9525 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009526 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009527 continue;
9528 }
9529 }
9530 word[n++] = c;
9531 }
9532 word[n] = NUL;
9533
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009534 /*
9535 * This comes from Aspell phonet.cpp.
9536 * Converted from C++ to C. Added support for multi-byte chars.
9537 * Changed to keep spaces.
9538 */
9539 i = reslen = z = 0;
9540 while ((c = word[i]) != NUL)
9541 {
9542 /* Start with the first rule that has the character in the word. */
9543 n = slang->sl_sal_first[c & 0xff];
9544 z0 = 0;
9545
9546 if (n >= 0)
9547 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009548 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009549 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
9550 {
9551 /* Quickly skip entries that don't match the word. Most
9552 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009553 if (c != ws[0])
9554 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009555 k = smp[n].sm_leadlen;
9556 if (k > 1)
9557 {
9558 if (word[i + 1] != ws[1])
9559 continue;
9560 if (k > 2)
9561 {
9562 for (j = 2; j < k; ++j)
9563 if (word[i + j] != ws[j])
9564 break;
9565 if (j < k)
9566 continue;
9567 }
9568 }
9569
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009570 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009571 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009572 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009573 while (*pf != NUL && *pf != word[i + k])
9574 ++pf;
9575 if (*pf == NUL)
9576 continue;
9577 ++k;
9578 }
9579 s = smp[n].sm_rules;
9580 pri = 5; /* default priority */
9581
9582 p0 = *s;
9583 k0 = k;
9584 while (*s == '-' && k > 1)
9585 {
9586 k--;
9587 s++;
9588 }
9589 if (*s == '<')
9590 s++;
9591 if (VIM_ISDIGIT(*s))
9592 {
9593 /* determine priority */
9594 pri = *s - '0';
9595 s++;
9596 }
9597 if (*s == '^' && *(s + 1) == '^')
9598 s++;
9599
9600 if (*s == NUL
9601 || (*s == '^'
9602 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009603 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009604 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009605 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009606 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009607 && spell_iswordp_w(word + i - 1, curbuf)
9608 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009609 {
9610 /* search for followup rules, if: */
9611 /* followup and k > 1 and NO '-' in searchstring */
9612 c0 = word[i + k - 1];
9613 n0 = slang->sl_sal_first[c0 & 0xff];
9614
9615 if (slang->sl_followup && k > 1 && n0 >= 0
9616 && p0 != '-' && word[i + k] != NUL)
9617 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009618 /* Test follow-up rule for "word[i + k]"; loop over
9619 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009620 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
9621 == (c0 & 0xff); ++n0)
9622 {
9623 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009624 */
9625 if (c0 != ws[0])
9626 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009627 k0 = smp[n0].sm_leadlen;
9628 if (k0 > 1)
9629 {
9630 if (word[i + k] != ws[1])
9631 continue;
9632 if (k0 > 2)
9633 {
9634 pf = word + i + k + 1;
9635 for (j = 2; j < k0; ++j)
9636 if (*pf++ != ws[j])
9637 break;
9638 if (j < k0)
9639 continue;
9640 }
9641 }
9642 k0 += k - 1;
9643
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009644 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009645 {
9646 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +00009647 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009648 while (*pf != NUL && *pf != word[i + k0])
9649 ++pf;
9650 if (*pf == NUL)
9651 continue;
9652 ++k0;
9653 }
9654
9655 p0 = 5;
9656 s = smp[n0].sm_rules;
9657 while (*s == '-')
9658 {
9659 /* "k0" gets NOT reduced because
9660 * "if (k0 == k)" */
9661 s++;
9662 }
9663 if (*s == '<')
9664 s++;
9665 if (VIM_ISDIGIT(*s))
9666 {
9667 p0 = *s - '0';
9668 s++;
9669 }
9670
9671 if (*s == NUL
9672 /* *s == '^' cuts */
9673 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009674 && !spell_iswordp_w(word + i + k0,
9675 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009676 {
9677 if (k0 == k)
9678 /* this is just a piece of the string */
9679 continue;
9680
9681 if (p0 < pri)
9682 /* priority too low */
9683 continue;
9684 /* rule fits; stop search */
9685 break;
9686 }
9687 }
9688
9689 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
9690 == (c0 & 0xff))
9691 continue;
9692 }
9693
9694 /* replace string */
9695 ws = smp[n].sm_to_w;
9696 s = smp[n].sm_rules;
9697 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
9698 if (p0 == 1 && z == 0)
9699 {
9700 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009701 if (reslen > 0 && ws != NULL && *ws != NUL
9702 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009703 || wres[reslen - 1] == *ws))
9704 reslen--;
9705 z0 = 1;
9706 z = 1;
9707 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009708 if (ws != NULL)
9709 while (*ws != NUL && word[i + k0] != NUL)
9710 {
9711 word[i + k0] = *ws;
9712 k0++;
9713 ws++;
9714 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009715 if (k > k0)
9716 mch_memmove(word + i + k0, word + i + k,
9717 sizeof(int) * (STRLEN(word + i + k) + 1));
9718
9719 /* new "actual letter" */
9720 c = word[i];
9721 }
9722 else
9723 {
9724 /* no '<' rule used */
9725 i += k - 1;
9726 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009727 if (ws != NULL)
9728 while (*ws != NUL && ws[1] != NUL
9729 && reslen < MAXWLEN)
9730 {
9731 if (reslen == 0 || wres[reslen - 1] != *ws)
9732 wres[reslen++] = *ws;
9733 ws++;
9734 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009735 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009736 if (ws == NULL)
9737 c = NUL;
9738 else
9739 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009740 if (strstr((char *)s, "^^") != NULL)
9741 {
9742 if (c != NUL)
9743 wres[reslen++] = c;
9744 mch_memmove(word, word + i + 1,
9745 sizeof(int) * (STRLEN(word + i + 1) + 1));
9746 i = 0;
9747 z0 = 1;
9748 }
9749 }
9750 break;
9751 }
9752 }
9753 }
9754 else if (vim_iswhite(c))
9755 {
9756 c = ' ';
9757 k = 1;
9758 }
9759
9760 if (z0 == 0)
9761 {
9762 if (k && !p0 && reslen < MAXWLEN && c != NUL
9763 && (!slang->sl_collapse || reslen == 0
9764 || wres[reslen - 1] != c))
9765 /* condense only double letters */
9766 wres[reslen++] = c;
9767
9768 i++;
9769 z = 0;
9770 k = 0;
9771 }
9772 }
9773
9774 /* Convert wide characters in "wres" to a multi-byte string in "res". */
9775 l = 0;
9776 for (n = 0; n < reslen; ++n)
9777 {
9778 l += mb_char2bytes(wres[n], res + l);
9779 if (l + MB_MAXBYTES > MAXWLEN)
9780 break;
9781 }
9782 res[l] = NUL;
9783}
9784#endif
9785
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009786/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009787 * Compute a score for two sound-a-like words.
9788 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
9789 * Instead of a generic loop we write out the code. That keeps it fast by
9790 * avoiding checks that will not be possible.
9791 */
9792 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009793soundalike_score(goodstart, badstart)
9794 char_u *goodstart; /* sound-folded good word */
9795 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009796{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009797 char_u *goodsound = goodstart;
9798 char_u *badsound = badstart;
9799 int goodlen;
9800 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009801 int n;
9802 char_u *pl, *ps;
9803 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009804 int score = 0;
9805
9806 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
9807 * counted so much, vowels halfway the word aren't counted at all. */
9808 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
9809 {
9810 score = SCORE_DEL / 2;
9811 if (*badsound == '*')
9812 ++badsound;
9813 else
9814 ++goodsound;
9815 }
9816
9817 goodlen = STRLEN(goodsound);
9818 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009819
9820 /* Return quickly if the lenghts are too different to be fixed by two
9821 * changes. */
9822 n = goodlen - badlen;
9823 if (n < -2 || n > 2)
9824 return SCORE_MAXMAX;
9825
9826 if (n > 0)
9827 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009828 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009829 ps = badsound;
9830 }
9831 else
9832 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009833 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009834 ps = goodsound;
9835 }
9836
9837 /* Skip over the identical part. */
9838 while (*pl == *ps && *pl != NUL)
9839 {
9840 ++pl;
9841 ++ps;
9842 }
9843
9844 switch (n)
9845 {
9846 case -2:
9847 case 2:
9848 /*
9849 * Must delete two characters from "pl".
9850 */
9851 ++pl; /* first delete */
9852 while (*pl == *ps)
9853 {
9854 ++pl;
9855 ++ps;
9856 }
9857 /* strings must be equal after second delete */
9858 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009859 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009860
9861 /* Failed to compare. */
9862 break;
9863
9864 case -1:
9865 case 1:
9866 /*
9867 * Minimal one delete from "pl" required.
9868 */
9869
9870 /* 1: delete */
9871 pl2 = pl + 1;
9872 ps2 = ps;
9873 while (*pl2 == *ps2)
9874 {
9875 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009876 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009877 ++pl2;
9878 ++ps2;
9879 }
9880
9881 /* 2: delete then swap, then rest must be equal */
9882 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9883 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009884 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009885
9886 /* 3: delete then substitute, then the rest must be equal */
9887 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009888 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009889
9890 /* 4: first swap then delete */
9891 if (pl[0] == ps[1] && pl[1] == ps[0])
9892 {
9893 pl2 = pl + 2; /* swap, skip two chars */
9894 ps2 = ps + 2;
9895 while (*pl2 == *ps2)
9896 {
9897 ++pl2;
9898 ++ps2;
9899 }
9900 /* delete a char and then strings must be equal */
9901 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009902 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009903 }
9904
9905 /* 5: first substitute then delete */
9906 pl2 = pl + 1; /* substitute, skip one char */
9907 ps2 = ps + 1;
9908 while (*pl2 == *ps2)
9909 {
9910 ++pl2;
9911 ++ps2;
9912 }
9913 /* delete a char and then strings must be equal */
9914 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009915 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009916
9917 /* Failed to compare. */
9918 break;
9919
9920 case 0:
9921 /*
9922 * Lenghts are equal, thus changes must result in same length: An
9923 * insert is only possible in combination with a delete.
9924 * 1: check if for identical strings
9925 */
9926 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009927 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009928
9929 /* 2: swap */
9930 if (pl[0] == ps[1] && pl[1] == ps[0])
9931 {
9932 pl2 = pl + 2; /* swap, skip two chars */
9933 ps2 = ps + 2;
9934 while (*pl2 == *ps2)
9935 {
9936 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009937 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009938 ++pl2;
9939 ++ps2;
9940 }
9941 /* 3: swap and swap again */
9942 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9943 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009944 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009945
9946 /* 4: swap and substitute */
9947 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009948 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009949 }
9950
9951 /* 5: substitute */
9952 pl2 = pl + 1;
9953 ps2 = ps + 1;
9954 while (*pl2 == *ps2)
9955 {
9956 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009957 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009958 ++pl2;
9959 ++ps2;
9960 }
9961
9962 /* 6: substitute and swap */
9963 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
9964 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009965 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009966
9967 /* 7: substitute and substitute */
9968 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009969 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009970
9971 /* 8: insert then delete */
9972 pl2 = pl;
9973 ps2 = ps + 1;
9974 while (*pl2 == *ps2)
9975 {
9976 ++pl2;
9977 ++ps2;
9978 }
9979 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009980 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009981
9982 /* 9: delete then insert */
9983 pl2 = pl + 1;
9984 ps2 = ps;
9985 while (*pl2 == *ps2)
9986 {
9987 ++pl2;
9988 ++ps2;
9989 }
9990 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009991 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009992
9993 /* Failed to compare. */
9994 break;
9995 }
9996
9997 return SCORE_MAXMAX;
9998}
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009999
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010000/*
10001 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010002 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010003 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010004 * The algorithm comes from Aspell editdist.cpp, edit_distance().
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010005 * It has been converted from C++ to C and modified to support multi-byte
10006 * characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010007 */
10008 static int
10009spell_edit_score(badword, goodword)
10010 char_u *badword;
10011 char_u *goodword;
10012{
10013 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010014 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010015 int j, i;
10016 int t;
10017 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010018 int pbc, pgc;
10019#ifdef FEAT_MBYTE
10020 char_u *p;
10021 int wbadword[MAXWLEN];
10022 int wgoodword[MAXWLEN];
10023
10024 if (has_mbyte)
10025 {
10026 /* Get the characters from the multi-byte strings and put them in an
10027 * int array for easy access. */
10028 for (p = badword, badlen = 0; *p != NUL; )
10029 wbadword[badlen++] = mb_ptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000010030 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010031 for (p = goodword, goodlen = 0; *p != NUL; )
10032 wgoodword[goodlen++] = mb_ptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000010033 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010034 }
10035 else
10036#endif
10037 {
10038 badlen = STRLEN(badword) + 1;
10039 goodlen = STRLEN(goodword) + 1;
10040 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010041
10042 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
10043#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010044 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
10045 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010046 if (cnt == NULL)
10047 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010048
10049 CNT(0, 0) = 0;
10050 for (j = 1; j <= goodlen; ++j)
10051 CNT(0, j) = CNT(0, j - 1) + SCORE_DEL;
10052
10053 for (i = 1; i <= badlen; ++i)
10054 {
10055 CNT(i, 0) = CNT(i - 1, 0) + SCORE_INS;
10056 for (j = 1; j <= goodlen; ++j)
10057 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010058#ifdef FEAT_MBYTE
10059 if (has_mbyte)
10060 {
10061 bc = wbadword[i - 1];
10062 gc = wgoodword[j - 1];
10063 }
10064 else
10065#endif
10066 {
10067 bc = badword[i - 1];
10068 gc = goodword[j - 1];
10069 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010070 if (bc == gc)
10071 CNT(i, j) = CNT(i - 1, j - 1);
10072 else
10073 {
10074 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010075 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010076 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
10077 else
10078 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
10079
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010080 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010081 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010082#ifdef FEAT_MBYTE
10083 if (has_mbyte)
10084 {
10085 pbc = wbadword[i - 2];
10086 pgc = wgoodword[j - 2];
10087 }
10088 else
10089#endif
10090 {
10091 pbc = badword[i - 2];
10092 pgc = goodword[j - 2];
10093 }
10094 if (bc == pgc && pbc == gc)
10095 {
10096 t = SCORE_SWAP + CNT(i - 2, j - 2);
10097 if (t < CNT(i, j))
10098 CNT(i, j) = t;
10099 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010100 }
10101 t = SCORE_DEL + CNT(i - 1, j);
10102 if (t < CNT(i, j))
10103 CNT(i, j) = t;
10104 t = SCORE_INS + CNT(i, j - 1);
10105 if (t < CNT(i, j))
10106 CNT(i, j) = t;
10107 }
10108 }
10109 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010110
10111 i = CNT(badlen - 1, goodlen - 1);
10112 vim_free(cnt);
10113 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010114}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010115
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010116/*
10117 * ":spelldump"
10118 */
10119/*ARGSUSED*/
10120 void
10121ex_spelldump(eap)
10122 exarg_T *eap;
10123{
10124 buf_T *buf = curbuf;
10125 langp_T *lp;
10126 slang_T *slang;
10127 idx_T arridx[MAXWLEN];
10128 int curi[MAXWLEN];
10129 char_u word[MAXWLEN];
10130 int c;
10131 char_u *byts;
10132 idx_T *idxs;
10133 linenr_T lnum = 0;
10134 int round;
10135 int depth;
10136 int n;
10137 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000010138 char_u *region_names = NULL; /* region names being used */
10139 int do_region = TRUE; /* dump region names and numbers */
10140 char_u *p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010141
10142 if (no_spell_checking())
10143 return;
10144
10145 /* Create a new empty buffer by splitting the window. */
10146 do_cmdline_cmd((char_u *)"new");
10147 if (!bufempty() || !buf_valid(buf))
10148 return;
10149
Bram Moolenaar7887d882005-07-01 22:33:52 +000010150 /* Find out if we can support regions: All languages must support the same
10151 * regions or none at all. */
10152 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
10153 {
10154 p = lp->lp_slang->sl_regions;
10155 if (p[0] != 0)
10156 {
10157 if (region_names == NULL) /* first language with regions */
10158 region_names = p;
10159 else if (STRCMP(region_names, p) != 0)
10160 {
10161 do_region = FALSE; /* region names are different */
10162 break;
10163 }
10164 }
10165 }
10166
10167 if (do_region && region_names != NULL)
10168 {
10169 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
10170 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
10171 }
10172 else
10173 do_region = FALSE;
10174
10175 /*
10176 * Loop over all files loaded for the entries in 'spelllang'.
10177 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010178 for (lp = LANGP_ENTRY(buf->b_langp, 0); lp->lp_slang != NULL; ++lp)
10179 {
10180 slang = lp->lp_slang;
10181
10182 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
10183 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
10184
10185 /* round 1: case-folded tree
10186 * round 2: keep-case tree */
10187 for (round = 1; round <= 2; ++round)
10188 {
10189 if (round == 1)
10190 {
10191 byts = slang->sl_fbyts;
10192 idxs = slang->sl_fidxs;
10193 }
10194 else
10195 {
10196 byts = slang->sl_kbyts;
10197 idxs = slang->sl_kidxs;
10198 }
10199 if (byts == NULL)
10200 continue; /* array is empty */
10201
10202 depth = 0;
10203 arridx[0] = 0;
10204 curi[0] = 1;
10205 while (depth >= 0 && !got_int)
10206 {
10207 if (curi[depth] > byts[arridx[depth]])
10208 {
10209 /* Done all bytes at this node, go up one level. */
10210 --depth;
10211 line_breakcheck();
10212 }
10213 else
10214 {
10215 /* Do one more byte at this node. */
10216 n = arridx[depth] + curi[depth];
10217 ++curi[depth];
10218 c = byts[n];
10219 if (c == 0)
10220 {
10221 /* End of word, deal with the word.
10222 * Don't use keep-case words in the fold-case tree,
10223 * they will appear in the keep-case tree.
10224 * Only use the word when the region matches. */
10225 flags = (int)idxs[n];
10226 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +000010227 && (do_region
10228 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010229 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010230 & lp->lp_region) != 0))
10231 {
10232 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000010233 if (!do_region)
10234 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010235
10236 /* Dump the basic word if there is no prefix or
10237 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010238 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010239 if (c == 0 || curi[depth] == 2)
10240 dump_word(word, round, flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010241
10242 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010243 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010244 lnum = apply_prefixes(slang, word, round,
10245 flags, lnum);
10246 }
10247 }
10248 else
10249 {
10250 /* Normal char, go one level deeper. */
10251 word[depth++] = c;
10252 arridx[depth] = idxs[n];
10253 curi[depth] = 1;
10254 }
10255 }
10256 }
10257 }
10258 }
10259
10260 /* Delete the empty line that we started with. */
10261 if (curbuf->b_ml.ml_line_count > 1)
10262 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
10263
10264 redraw_later(NOT_VALID);
10265}
10266
10267/*
10268 * Dump one word: apply case modifications and append a line to the buffer.
10269 */
10270 static void
10271dump_word(word, round, flags, lnum)
10272 char_u *word;
10273 int round;
10274 int flags;
10275 linenr_T lnum;
10276{
10277 int keepcap = FALSE;
10278 char_u *p;
10279 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000010280 char_u badword[MAXWLEN + 10];
10281 int i;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010282
10283 if (round == 1 && (flags & WF_CAPMASK) != 0)
10284 {
10285 /* Need to fix case according to "flags". */
10286 make_case_word(word, cword, flags);
10287 p = cword;
10288 }
10289 else
10290 {
10291 p = word;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000010292 if (round == 2 && ((captype(word, NULL) & WF_KEEPCAP) == 0
10293 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010294 keepcap = TRUE;
10295 }
10296
Bram Moolenaar7887d882005-07-01 22:33:52 +000010297 /* Add flags and regions after a slash. */
10298 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010299 {
Bram Moolenaar7887d882005-07-01 22:33:52 +000010300 STRCPY(badword, p);
10301 STRCAT(badword, "/");
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010302 if (keepcap)
10303 STRCAT(badword, "=");
10304 if (flags & WF_BANNED)
10305 STRCAT(badword, "!");
10306 else if (flags & WF_RARE)
10307 STRCAT(badword, "?");
Bram Moolenaar7887d882005-07-01 22:33:52 +000010308 if (flags & WF_REGION)
10309 for (i = 0; i < 7; ++i)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010310 if (flags & (0x10000 << i))
Bram Moolenaar7887d882005-07-01 22:33:52 +000010311 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010312 p = badword;
10313 }
10314
10315 ml_append(lnum, p, (colnr_T)0, FALSE);
10316}
10317
10318/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010319 * For ":spelldump": Find matching prefixes for "word". Prepend each to
10320 * "word" and append a line to the buffer.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010321 * Return the updated line number.
10322 */
10323 static linenr_T
10324apply_prefixes(slang, word, round, flags, startlnum)
10325 slang_T *slang;
10326 char_u *word; /* case-folded word */
10327 int round;
10328 int flags; /* flags with prefix ID */
10329 linenr_T startlnum;
10330{
10331 idx_T arridx[MAXWLEN];
10332 int curi[MAXWLEN];
10333 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000010334 char_u word_up[MAXWLEN];
10335 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010336 int c;
10337 char_u *byts;
10338 idx_T *idxs;
10339 linenr_T lnum = startlnum;
10340 int depth;
10341 int n;
10342 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010343 int i;
10344
Bram Moolenaar53805d12005-08-01 07:08:33 +000010345 /* if the word starts with a lower-case letter make the word with an
10346 * upper-case letter in word_up[]. */
10347 c = PTR2CHAR(word);
10348 if (SPELL_TOUPPER(c) != c)
10349 {
10350 onecap_copy(word, word_up, TRUE);
10351 has_word_up = TRUE;
10352 }
10353
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010354 byts = slang->sl_pbyts;
10355 idxs = slang->sl_pidxs;
10356 if (byts != NULL) /* array not is empty */
10357 {
10358 /*
10359 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010360 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010361 */
10362 depth = 0;
10363 arridx[0] = 0;
10364 curi[0] = 1;
10365 while (depth >= 0 && !got_int)
10366 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010367 n = arridx[depth];
10368 len = byts[n];
10369 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010370 {
10371 /* Done all bytes at this node, go up one level. */
10372 --depth;
10373 line_breakcheck();
10374 }
10375 else
10376 {
10377 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010378 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010379 ++curi[depth];
10380 c = byts[n];
10381 if (c == 0)
10382 {
10383 /* End of prefix, find out how many IDs there are. */
10384 for (i = 1; i < len; ++i)
10385 if (byts[n + i] != 0)
10386 break;
10387 curi[depth] += i - 1;
10388
Bram Moolenaar53805d12005-08-01 07:08:33 +000010389 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
10390 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010391 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010392 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010393 dump_word(prefix, round,
Bram Moolenaar53805d12005-08-01 07:08:33 +000010394 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000010395 : flags, lnum++);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010396 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000010397
10398 /* Check for prefix that matches the word when the
10399 * first letter is upper-case, but only if the prefix has
10400 * a condition. */
10401 if (has_word_up)
10402 {
10403 c = valid_word_prefix(i, n, flags, word_up, slang,
10404 TRUE);
10405 if (c != 0)
10406 {
10407 vim_strncpy(prefix + depth, word_up,
10408 MAXWLEN - depth - 1);
10409 dump_word(prefix, round,
10410 (c & WF_RAREPFX) ? (flags | WF_RARE)
10411 : flags, lnum++);
10412 }
10413 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010414 }
10415 else
10416 {
10417 /* Normal char, go one level deeper. */
10418 prefix[depth++] = c;
10419 arridx[depth] = idxs[n];
10420 curi[depth] = 1;
10421 }
10422 }
10423 }
10424 }
10425
10426 return lnum;
10427}
10428
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000010429#endif /* FEAT_SYN_HL */