blob: f747570028edad3bb1b78c3580f129bb42dc5590 [file] [log] [blame]
Bram Moolenaare19defe2005-03-21 08:23:33 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * spell.c: code for spell checking
Bram Moolenaarfc735152005-03-22 22:54:12 +000012 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000013 * The spell checking mechanism uses a tree (aka trie). Each node in the tree
14 * has a list of bytes that can appear (siblings). For each byte there is a
15 * pointer to the node with the byte that follows in the word (child).
Bram Moolenaar9f30f502005-06-14 22:01:04 +000016 *
17 * A NUL byte is used where the word may end. The bytes are sorted, so that
18 * binary searching can be used and the NUL bytes are at the start. The
19 * number of possible bytes is stored before the list of bytes.
20 *
21 * The tree uses two arrays: "byts" stores the characters, "idxs" stores
22 * either the next index or flags. The tree starts at index 0. For example,
23 * to lookup "vi" this sequence is followed:
24 * i = 0
25 * len = byts[i]
26 * n = where "v" appears in byts[i + 1] to byts[i + len]
27 * i = idxs[n]
28 * len = byts[i]
29 * n = where "i" appears in byts[i + 1] to byts[i + len]
30 * i = idxs[n]
31 * len = byts[i]
32 * find that byts[i + 1] is 0, idxs[i + 1] has flags for "vi".
Bram Moolenaar51485f02005-06-04 21:55:20 +000033 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +000034 * There are two word trees: one with case-folded words and one with words in
Bram Moolenaar51485f02005-06-04 21:55:20 +000035 * original case. The second one is only used for keep-case words and is
36 * usually small.
37 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +000038 * There is one additional tree for when not all prefixes are applied when
Bram Moolenaar1d73c882005-06-19 22:48:47 +000039 * generating the .spl file. This tree stores all the possible prefixes, as
40 * if they were words. At each word (prefix) end the prefix nr is stored, the
41 * following word must support this prefix nr. And the condition nr is
42 * stored, used to lookup the condition that the word must match with.
43 *
Bram Moolenaar51485f02005-06-04 21:55:20 +000044 * Thanks to Olaf Seibert for providing an example implementation of this tree
45 * and the compression mechanism.
Bram Moolenaar4770d092006-01-12 23:22:24 +000046 * LZ trie ideas:
47 * http://www.irb.hr/hr/home/ristov/papers/RistovLZtrieRevision1.pdf
48 * More papers: http://www-igm.univ-mlv.fr/~laporte/publi_en.html
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000049 *
50 * Matching involves checking the caps type: Onecap ALLCAP KeepCap.
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +000051 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000052 * Why doesn't Vim use aspell/ispell/myspell/etc.?
53 * See ":help develop-spell".
54 */
55
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000056/* Use SPELL_PRINTTREE for debugging: dump the word tree after adding a word.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000057 * Only use it for small word lists! */
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000058#if 0
59# define SPELL_PRINTTREE
Bram Moolenaar329cc7e2005-08-10 07:51:35 +000060#endif
61
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000062/* Use DEBUG_TRIEWALK to print the changes made in suggest_trie_walk() for a
63 * specific word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000064#if 0
65# define DEBUG_TRIEWALK
66#endif
67
Bram Moolenaar51485f02005-06-04 21:55:20 +000068/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000069 * Use this to adjust the score after finding suggestions, based on the
70 * suggested word sounding like the bad word. This is much faster than doing
71 * it for every possible suggestion.
Bram Moolenaar4770d092006-01-12 23:22:24 +000072 * Disadvantage: When "the" is typed as "hte" it sounds quite different ("@"
73 * vs "ht") and goes down in the list.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000074 * Used when 'spellsuggest' is set to "best".
75 */
76#define RESCORE(word_score, sound_score) ((3 * word_score + sound_score) / 4)
77
78/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000079 * Do the opposite: based on a maximum end score and a known sound score,
80 * compute the the maximum word score that can be used.
81 */
82#define MAXSCORE(word_score, sound_score) ((4 * word_score - sound_score) / 3)
83
84/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +000085 * Vim spell file format: <HEADER>
Bram Moolenaar5195e452005-08-19 20:32:47 +000086 * <SECTIONS>
Bram Moolenaar1d73c882005-06-19 22:48:47 +000087 * <LWORDTREE>
88 * <KWORDTREE>
89 * <PREFIXTREE>
Bram Moolenaar51485f02005-06-04 21:55:20 +000090 *
Bram Moolenaar5195e452005-08-19 20:32:47 +000091 * <HEADER>: <fileID> <versionnr>
Bram Moolenaar51485f02005-06-04 21:55:20 +000092 *
Bram Moolenaar5195e452005-08-19 20:32:47 +000093 * <fileID> 8 bytes "VIMspell"
94 * <versionnr> 1 byte VIMSPELLVERSION
95 *
96 *
97 * Sections make it possible to add information to the .spl file without
98 * making it incompatible with previous versions. There are two kinds of
99 * sections:
100 * 1. Not essential for correct spell checking. E.g. for making suggestions.
101 * These are skipped when not supported.
102 * 2. Optional information, but essential for spell checking when present.
103 * E.g. conditions for affixes. When this section is present but not
104 * supported an error message is given.
105 *
106 * <SECTIONS>: <section> ... <sectionend>
107 *
108 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
109 *
110 * <sectionID> 1 byte number from 0 to 254 identifying the section
111 *
112 * <sectionflags> 1 byte SNF_REQUIRED: this section is required for correct
113 * spell checking
114 *
115 * <sectionlen> 4 bytes length of section contents, MSB first
116 *
117 * <sectionend> 1 byte SN_END
118 *
119 *
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000120 * sectionID == SN_INFO: <infotext>
121 * <infotext> N bytes free format text with spell file info (version,
122 * website, etc)
123 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000124 * sectionID == SN_REGION: <regionname> ...
125 * <regionname> 2 bytes Up to 8 region names: ca, au, etc. Lower case.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000126 * First <regionname> is region 1.
127 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000128 * sectionID == SN_CHARFLAGS: <charflagslen> <charflags>
129 * <folcharslen> <folchars>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000130 * <charflagslen> 1 byte Number of bytes in <charflags> (should be 128).
131 * <charflags> N bytes List of flags (first one is for character 128):
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000132 * 0x01 word character CF_WORD
133 * 0x02 upper-case character CF_UPPER
Bram Moolenaar5195e452005-08-19 20:32:47 +0000134 * <folcharslen> 2 bytes Number of bytes in <folchars>.
135 * <folchars> N bytes Folded characters, first one is for character 128.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000136 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000137 * sectionID == SN_MIDWORD: <midword>
138 * <midword> N bytes Characters that are word characters only when used
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000139 * in the middle of a word.
140 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000141 * sectionID == SN_PREFCOND: <prefcondcnt> <prefcond> ...
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000142 * <prefcondcnt> 2 bytes Number of <prefcond> items following.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000143 * <prefcond> : <condlen> <condstr>
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000144 * <condlen> 1 byte Length of <condstr>.
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000145 * <condstr> N bytes Condition for the prefix.
146 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000147 * sectionID == SN_REP: <repcount> <rep> ...
148 * <repcount> 2 bytes number of <rep> items, MSB first.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000149 * <rep> : <repfromlen> <repfrom> <reptolen> <repto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000150 * <repfromlen> 1 byte length of <repfrom>
151 * <repfrom> N bytes "from" part of replacement
152 * <reptolen> 1 byte length of <repto>
153 * <repto> N bytes "to" part of replacement
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000154 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000155 * sectionID == SN_REPSAL: <repcount> <rep> ...
156 * just like SN_REP but for soundfolded words
157 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000158 * sectionID == SN_SAL: <salflags> <salcount> <sal> ...
159 * <salflags> 1 byte flags for soundsalike conversion:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000160 * SAL_F0LLOWUP
161 * SAL_COLLAPSE
162 * SAL_REM_ACCENTS
Bram Moolenaar5195e452005-08-19 20:32:47 +0000163 * <salcount> 2 bytes number of <sal> items following
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000164 * <sal> : <salfromlen> <salfrom> <saltolen> <salto>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000165 * <salfromlen> 1 byte length of <salfrom>
166 * <salfrom> N bytes "from" part of soundsalike
167 * <saltolen> 1 byte length of <salto>
168 * <salto> N bytes "to" part of soundsalike
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000169 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000170 * sectionID == SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
171 * <sofofromlen> 2 bytes length of <sofofrom>
172 * <sofofrom> N bytes "from" part of soundfold
173 * <sofotolen> 2 bytes length of <sofoto>
174 * <sofoto> N bytes "to" part of soundfold
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000175 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000176 * sectionID == SN_SUGFILE: <timestamp>
177 * <timestamp> 8 bytes time in seconds that must match with .sug file
178 *
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000179 * sectionID == SN_NOSPLITSUGS: nothing
180 *
Bram Moolenaar4770d092006-01-12 23:22:24 +0000181 * sectionID == SN_WORDS: <word> ...
182 * <word> N bytes NUL terminated common word
183 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000184 * sectionID == SN_MAP: <mapstr>
185 * <mapstr> N bytes String with sequences of similar characters,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000186 * separated by slashes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000187 *
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000188 * sectionID == SN_COMPOUND: <compmax> <compminlen> <compsylmax> <compoptions>
189 * <comppatcount> <comppattern> ... <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +0000190 * <compmax> 1 byte Maximum nr of words in compound word.
191 * <compminlen> 1 byte Minimal word length for compounding.
192 * <compsylmax> 1 byte Maximum nr of syllables in compound word.
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000193 * <compoptions> 2 bytes COMP_ flags.
194 * <comppatcount> 2 bytes number of <comppattern> following
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000195 * <compflags> N bytes Flags from COMPOUNDRULE items, separated by
Bram Moolenaar5195e452005-08-19 20:32:47 +0000196 * slashes.
197 *
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000198 * <comppattern>: <comppatlen> <comppattext>
199 * <comppatlen> 1 byte length of <comppattext>
200 * <comppattext> N bytes end or begin chars from CHECKCOMPOUNDPATTERN
201 *
202 * sectionID == SN_NOBREAK: (empty, its presence is what matters)
Bram Moolenaar78622822005-08-23 21:00:13 +0000203 *
Bram Moolenaar5195e452005-08-19 20:32:47 +0000204 * sectionID == SN_SYLLABLE: <syllable>
205 * <syllable> N bytes String from SYLLABLE item.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000206 *
207 * <LWORDTREE>: <wordtree>
208 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000209 * <KWORDTREE>: <wordtree>
210 *
211 * <PREFIXTREE>: <wordtree>
212 *
213 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000214 * <wordtree>: <nodecount> <nodedata> ...
215 *
216 * <nodecount> 4 bytes Number of nodes following. MSB first.
217 *
218 * <nodedata>: <siblingcount> <sibling> ...
219 *
220 * <siblingcount> 1 byte Number of siblings in this node. The siblings
221 * follow in sorted order.
222 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000223 * <sibling>: <byte> [ <nodeidx> <xbyte>
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000224 * | <flags> [<flags2>] [<region>] [<affixID>]
225 * | [<pflags>] <affixID> <prefcondnr> ]
Bram Moolenaar51485f02005-06-04 21:55:20 +0000226 *
227 * <byte> 1 byte Byte value of the sibling. Special cases:
228 * BY_NOFLAGS: End of word without flags and for all
229 * regions.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000230 * For PREFIXTREE <affixID> and
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000231 * <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000232 * BY_FLAGS: End of word, <flags> follow.
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000233 * For PREFIXTREE <pflags>, <affixID>
Bram Moolenaar53805d12005-08-01 07:08:33 +0000234 * and <prefcondnr> follow.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000235 * BY_FLAGS2: End of word, <flags> and <flags2>
236 * follow. Not used in PREFIXTREE.
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000237 * BY_INDEX: Child of sibling is shared, <nodeidx>
Bram Moolenaar51485f02005-06-04 21:55:20 +0000238 * and <xbyte> follow.
239 *
240 * <nodeidx> 3 bytes Index of child for this sibling, MSB first.
241 *
242 * <xbyte> 1 byte byte value of the sibling.
243 *
244 * <flags> 1 byte bitmask of:
245 * WF_ALLCAP word must have only capitals
246 * WF_ONECAP first char of word must be capital
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000247 * WF_KEEPCAP keep-case word
248 * WF_FIXCAP keep-case word, all caps not allowed
Bram Moolenaar51485f02005-06-04 21:55:20 +0000249 * WF_RARE rare word
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000250 * WF_BANNED bad word
Bram Moolenaar51485f02005-06-04 21:55:20 +0000251 * WF_REGION <region> follows
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000252 * WF_AFX <affixID> follows
Bram Moolenaar51485f02005-06-04 21:55:20 +0000253 *
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000254 * <flags2> 1 byte Bitmask of:
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000255 * WF_HAS_AFF >> 8 word includes affix
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000256 * WF_NEEDCOMP >> 8 word only valid in compound
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000257 * WF_NOSUGGEST >> 8 word not used for suggestions
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000258 * WF_COMPROOT >> 8 word already a compound
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000259 * WF_NOCOMPBEF >> 8 no compounding before this word
260 * WF_NOCOMPAFT >> 8 no compounding after this word
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000261 *
Bram Moolenaar53805d12005-08-01 07:08:33 +0000262 * <pflags> 1 byte bitmask of:
263 * WFP_RARE rare prefix
264 * WFP_NC non-combining prefix
265 * WFP_UP letter after prefix made upper case
266 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000267 * <region> 1 byte Bitmask for regions in which word is valid. When
268 * omitted it's valid in all regions.
269 * Lowest bit is for region 1.
270 *
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000271 * <affixID> 1 byte ID of affix that can be used with this word. In
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000272 * PREFIXTREE used for the required prefix ID.
273 *
274 * <prefcondnr> 2 bytes Prefix condition number, index in <prefcond> list
275 * from HEADER.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000276 *
Bram Moolenaar51485f02005-06-04 21:55:20 +0000277 * All text characters are in 'encoding', but stored as single bytes.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000278 */
279
Bram Moolenaar4770d092006-01-12 23:22:24 +0000280/*
281 * Vim .sug file format: <SUGHEADER>
282 * <SUGWORDTREE>
283 * <SUGTABLE>
284 *
285 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
286 *
287 * <fileID> 6 bytes "VIMsug"
288 * <versionnr> 1 byte VIMSUGVERSION
289 * <timestamp> 8 bytes timestamp that must match with .spl file
290 *
291 *
292 * <SUGWORDTREE>: <wordtree> (see above, no flags or region used)
293 *
294 *
295 * <SUGTABLE>: <sugwcount> <sugline> ...
296 *
297 * <sugwcount> 4 bytes number of <sugline> following
298 *
299 * <sugline>: <sugnr> ... NUL
300 *
301 * <sugnr>: X bytes word number that results in this soundfolded word,
302 * stored as an offset to the previous number in as
303 * few bytes as possible, see offset2bytes())
304 */
305
Bram Moolenaare19defe2005-03-21 08:23:33 +0000306#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000307# include "vimio.h" /* for lseek(), must be before vim.h */
Bram Moolenaare19defe2005-03-21 08:23:33 +0000308#endif
309
310#include "vim.h"
311
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000312#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaare19defe2005-03-21 08:23:33 +0000313
314#ifdef HAVE_FCNTL_H
315# include <fcntl.h>
316#endif
317
Bram Moolenaar4770d092006-01-12 23:22:24 +0000318#ifndef UNIX /* it's in os_unix.h for Unix */
319# include <time.h> /* for time_t */
320#endif
321
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000322#define MAXWLEN 250 /* Assume max. word len is this many bytes.
323 Some places assume a word length fits in a
324 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000325
Bram Moolenaare52325c2005-08-22 22:54:29 +0000326/* Type used for indexes in the word tree need to be at least 4 bytes. If int
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000327 * is 8 bytes we could use something smaller, but what? */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000328#if SIZEOF_INT > 3
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000329typedef int idx_T;
330#else
331typedef long idx_T;
332#endif
333
334/* Flags used for a word. Only the lowest byte can be used, the region byte
335 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000336#define WF_REGION 0x01 /* region byte follows */
337#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
338#define WF_ALLCAP 0x04 /* word must be all capitals */
339#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000340#define WF_BANNED 0x10 /* bad word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000341#define WF_AFX 0x20 /* affix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000342#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000343#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000344
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000345/* for <flags2>, shifted up one byte to be used in wn_flags */
346#define WF_HAS_AFF 0x0100 /* word includes affix */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000347#define WF_NEEDCOMP 0x0200 /* word only valid in compound */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000348#define WF_NOSUGGEST 0x0400 /* word not to be suggested */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000349#define WF_COMPROOT 0x0800 /* already compounded word, COMPOUNDROOT */
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000350#define WF_NOCOMPBEF 0x1000 /* no compounding before this word */
351#define WF_NOCOMPAFT 0x2000 /* no compounding after this word */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000352
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000353/* only used for su_badflags */
354#define WF_MIXCAP 0x20 /* mix of upper and lower case: macaRONI */
355
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000356#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000357
Bram Moolenaar53805d12005-08-01 07:08:33 +0000358/* flags for <pflags> */
359#define WFP_RARE 0x01 /* rare prefix */
360#define WFP_NC 0x02 /* prefix is not combining */
361#define WFP_UP 0x04 /* to-upper prefix */
362
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000363/* Flags for postponed prefixes. Must be above affixID (one byte)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000364 * and prefcondnr (two bytes). */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000365#define WF_RAREPFX (WFP_RARE << 24) /* in sl_pidxs: flag for rare
366 * postponed prefix */
367#define WF_PFX_NC (WFP_NC << 24) /* in sl_pidxs: flag for non-combining
368 * postponed prefix */
369#define WF_PFX_UP (WFP_UP << 24) /* in sl_pidxs: flag for to-upper
370 * postponed prefix */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000371
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000372/* flags for <compoptions> */
373#define COMP_CHECKDUP 1 /* CHECKCOMPOUNDDUP */
374#define COMP_CHECKREP 2 /* CHECKCOMPOUNDREP */
375#define COMP_CHECKCASE 4 /* CHECKCOMPOUNDCASE */
376#define COMP_CHECKTRIPLE 8 /* CHECKCOMPOUNDTRIPLE */
377
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000378/* Special byte values for <byte>. Some are only used in the tree for
379 * postponed prefixes, some only in the other trees. This is a bit messy... */
380#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000381 * postponed prefix: no <pflags> */
382#define BY_INDEX 1 /* child is shared, index follows */
383#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
384 * postponed prefix: <pflags> follows */
385#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
386 * follow; never used in prefix tree */
387#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000388
Bram Moolenaar4770d092006-01-12 23:22:24 +0000389/* Info from "REP", "REPSAL" and "SAL" entries in ".aff" file used in si_rep,
390 * si_repsal, sl_rep, and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000391 * One replacement: from "ft_from" to "ft_to". */
392typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000393{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000394 char_u *ft_from;
395 char_u *ft_to;
396} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000397
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000398/* Info from "SAL" entries in ".aff" file used in sl_sal.
399 * The info is split for quick processing by spell_soundfold().
400 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
401typedef struct salitem_S
402{
403 char_u *sm_lead; /* leading letters */
404 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000405 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000406 char_u *sm_rules; /* rules like ^, $, priority */
407 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000408#ifdef FEAT_MBYTE
409 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000410 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000411 int *sm_to_w; /* wide character copy of "sm_to" */
412#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000413} salitem_T;
414
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000415#ifdef FEAT_MBYTE
416typedef int salfirst_T;
417#else
418typedef short salfirst_T;
419#endif
420
Bram Moolenaar5195e452005-08-19 20:32:47 +0000421/* Values for SP_*ERROR are negative, positive values are used by
422 * read_cnt_string(). */
423#define SP_TRUNCERROR -1 /* spell file truncated error */
424#define SP_FORMERROR -2 /* format error in spell file */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000425#define SP_OTHERERROR -3 /* other error while reading spell file */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000426
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000427/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000428 * Structure used to store words and other info for one language, loaded from
429 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000430 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
431 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
432 *
433 * The "byts" array stores the possible bytes in each tree node, preceded by
434 * the number of possible bytes, sorted on byte value:
435 * <len> <byte1> <byte2> ...
436 * The "idxs" array stores the index of the child node corresponding to the
437 * byte in "byts".
438 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000439 * the flags, region mask and affixID for the word. There may be several
440 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000441 */
442typedef struct slang_S slang_T;
443struct slang_S
444{
445 slang_T *sl_next; /* next language */
446 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000447 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000448 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000449
Bram Moolenaar51485f02005-06-04 21:55:20 +0000450 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000451 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000452 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000453 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000454 char_u *sl_pbyts; /* prefix tree word bytes */
455 idx_T *sl_pidxs; /* prefix tree word indexes */
456
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000457 char_u *sl_info; /* infotext string or NULL */
458
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000459 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000460
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000461 char_u *sl_midword; /* MIDWORD string or NULL */
462
Bram Moolenaar4770d092006-01-12 23:22:24 +0000463 hashtab_T sl_wordcount; /* hashtable with word count, wordcount_T */
464
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000465 int sl_compmax; /* COMPOUNDWORDMAX (default: MAXWLEN) */
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000466 int sl_compminlen; /* COMPOUNDMIN (default: 0) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000467 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000468 int sl_compoptions; /* COMP_* flags */
469 garray_T sl_comppat; /* CHECKCOMPOUNDPATTERN items */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000470 regprog_T *sl_compprog; /* COMPOUNDRULE turned into a regexp progrm
Bram Moolenaar5195e452005-08-19 20:32:47 +0000471 * (NULL when no compounding) */
472 char_u *sl_compstartflags; /* flags for first compound word */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000473 char_u *sl_compallflags; /* all flags for compound words */
Bram Moolenaar78622822005-08-23 21:00:13 +0000474 char_u sl_nobreak; /* When TRUE: no spaces between words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000475 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
476 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000477
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000478 int sl_prefixcnt; /* number of items in "sl_prefprog" */
479 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
480
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000481 garray_T sl_rep; /* list of fromto_T entries from REP lines */
482 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
483 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000484 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000485 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000486 there is none */
487 int sl_followup; /* SAL followup */
488 int sl_collapse; /* SAL collapse_result */
489 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000490 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
491 * "sl_sal_first" maps chars, when has_mbyte
492 * "sl_sal" is a list of wide char lists. */
493 garray_T sl_repsal; /* list of fromto_T entries from REPSAL lines */
494 short sl_repsal_first[256]; /* sl_rep_first for REPSAL lines */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000495 int sl_nosplitsugs; /* don't suggest splitting a word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000496
497 /* Info from the .sug file. Loaded on demand. */
498 time_t sl_sugtime; /* timestamp for .sug file */
499 char_u *sl_sbyts; /* soundfolded word bytes */
500 idx_T *sl_sidxs; /* soundfolded word indexes */
501 buf_T *sl_sugbuf; /* buffer with word number table */
502 int sl_sugloaded; /* TRUE when .sug file was loaded or failed to
503 load */
504
Bram Moolenaarea424162005-06-16 21:51:00 +0000505 int sl_has_map; /* TRUE if there is a MAP line */
506#ifdef FEAT_MBYTE
507 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
508 int sl_map_array[256]; /* MAP for first 256 chars */
509#else
510 char_u sl_map_array[256]; /* MAP for first 256 chars */
511#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000512 hashtab_T sl_sounddone; /* table with soundfolded words that have
513 handled, see add_sound_suggest() */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000514};
515
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000516/* First language that is loaded, start of the linked list of loaded
517 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000518static slang_T *first_lang = NULL;
519
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000520/* Flags used in .spl file for soundsalike flags. */
521#define SAL_F0LLOWUP 1
522#define SAL_COLLAPSE 2
523#define SAL_REM_ACCENTS 4
524
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000525/*
526 * Structure used in "b_langp", filled from 'spelllang'.
527 */
528typedef struct langp_S
529{
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000530 slang_T *lp_slang; /* info for this language */
531 slang_T *lp_sallang; /* language used for sound folding or NULL */
532 slang_T *lp_replang; /* language used for REP items or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000533 int lp_region; /* bitmask for region or REGION_ALL */
534} langp_T;
535
536#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
537
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000538#define REGION_ALL 0xff /* word valid in all regions */
539
Bram Moolenaar5195e452005-08-19 20:32:47 +0000540#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
541#define VIMSPELLMAGICL 8
542#define VIMSPELLVERSION 50
543
Bram Moolenaar4770d092006-01-12 23:22:24 +0000544#define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */
545#define VIMSUGMAGICL 6
546#define VIMSUGVERSION 1
547
Bram Moolenaar5195e452005-08-19 20:32:47 +0000548/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
549#define SN_REGION 0 /* <regionname> section */
550#define SN_CHARFLAGS 1 /* charflags section */
551#define SN_MIDWORD 2 /* <midword> section */
552#define SN_PREFCOND 3 /* <prefcond> section */
553#define SN_REP 4 /* REP items section */
554#define SN_SAL 5 /* SAL items section */
555#define SN_SOFO 6 /* soundfolding section */
556#define SN_MAP 7 /* MAP items section */
557#define SN_COMPOUND 8 /* compound words section */
558#define SN_SYLLABLE 9 /* syllable section */
Bram Moolenaar78622822005-08-23 21:00:13 +0000559#define SN_NOBREAK 10 /* NOBREAK section */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000560#define SN_SUGFILE 11 /* timestamp for .sug file */
561#define SN_REPSAL 12 /* REPSAL items section */
562#define SN_WORDS 13 /* common words */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000563#define SN_NOSPLITSUGS 14 /* don't split word for suggestions */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000564#define SN_INFO 15 /* info section */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000565#define SN_END 255 /* end of sections */
566
567#define SNF_REQUIRED 1 /* <sectionflags>: required section */
568
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000569/* Result values. Lower number is accepted over higher one. */
570#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000571#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000572#define SP_RARE 1
573#define SP_LOCAL 2
574#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000575
Bram Moolenaar7887d882005-07-01 22:33:52 +0000576/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000577static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000578
Bram Moolenaar4770d092006-01-12 23:22:24 +0000579typedef struct wordcount_S
580{
581 short_u wc_count; /* nr of times word was seen */
582 char_u wc_word[1]; /* word, actually longer */
583} wordcount_T;
584
585static wordcount_T dumwc;
586#define WC_KEY_OFF (dumwc.wc_word - (char_u *)&dumwc)
587#define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF))
588#define MAXWORDCOUNT 0xffff
589
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000590/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000591 * Information used when looking for suggestions.
592 */
593typedef struct suginfo_S
594{
595 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000596 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000597 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000598 int su_sfmaxscore; /* idem, for when doing soundfold words */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000599 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000600 char_u *su_badptr; /* start of bad word in line */
601 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000602 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000603 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
604 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000605 char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000606 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000607 slang_T *su_sallang; /* default language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000608} suginfo_T;
609
610/* One word suggestion. Used in "si_ga". */
611typedef struct suggest_S
612{
613 char_u *st_word; /* suggested word, allocated string */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000614 int st_wordlen; /* STRLEN(st_word) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000615 int st_orglen; /* length of replaced text */
616 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000617 int st_altscore; /* used when st_score compares equal */
618 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000619 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000620 slang_T *st_slang; /* language used for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000621} suggest_T;
622
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000623#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000624
Bram Moolenaar4770d092006-01-12 23:22:24 +0000625/* TRUE if a word appears in the list of banned words. */
626#define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word)))
627
628/* Number of suggestions kept when cleaning up. we need to keep more than
629 * what is displayed, because when rescore_suggestions() is called the score
630 * may change and wrong suggestions may be removed later. */
631#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000632
633/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
634 * of suggestions that are not going to be displayed. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000635#define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000636
637/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000638#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000639#define SCORE_SPLIT_NO 249 /* split bad word with NOSPLITSUGS */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000640#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000641#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000642#define SCORE_RARE 180 /* rare word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000643#define SCORE_SWAP 75 /* swap two characters */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000644#define SCORE_SWAP3 110 /* swap two characters in three */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000645#define SCORE_REP 65 /* REP replacement */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000646#define SCORE_SUBST 93 /* substitute a character */
647#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000648#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000649#define SCORE_DEL 94 /* delete a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000650#define SCORE_DELDUP 66 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000651#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000652#define SCORE_INS 96 /* insert a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000653#define SCORE_INSDUP 67 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000654#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000655#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000656
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000657#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000658#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
659 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000660
Bram Moolenaar4770d092006-01-12 23:22:24 +0000661#define SCORE_COMMON1 30 /* subtracted for words seen before */
662#define SCORE_COMMON2 40 /* subtracted for words often seen */
663#define SCORE_COMMON3 50 /* subtracted for words very often seen */
664#define SCORE_THRES2 10 /* word count threshold for COMMON2 */
665#define SCORE_THRES3 100 /* word count threshold for COMMON3 */
666
667/* When trying changed soundfold words it becomes slow when trying more than
668 * two changes. With less then two changes it's slightly faster but we miss a
669 * few good suggestions. In rare cases we need to try three of four changes.
670 */
671#define SCORE_SFMAX1 200 /* maximum score for first try */
672#define SCORE_SFMAX2 300 /* maximum score for second try */
673#define SCORE_SFMAX3 400 /* maximum score for third try */
674
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000675#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000676#define SCORE_MAXMAX 999999 /* accept any score */
677#define SCORE_LIMITMAX 350 /* for spell_edit_score_limit() */
678
679/* for spell_edit_score_limit() we need to know the minimum value of
680 * SCORE_ICASE, SCORE_SWAP, SCORE_DEL, SCORE_SIMILAR and SCORE_INS */
681#define SCORE_EDIT_MIN SCORE_SIMILAR
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000682
683/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000684 * Structure to store info for word matching.
685 */
686typedef struct matchinf_S
687{
688 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000689
690 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000691 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000692 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000693 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000694 char_u *mi_cend; /* char after what was used for
695 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000696
697 /* case-folded text */
698 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000699 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000700
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000701 /* for when checking word after a prefix */
702 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000703 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000704 int mi_prefcnt; /* number of entries at mi_prefarridx */
705 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000706#ifdef FEAT_MBYTE
707 int mi_cprefixlen; /* byte length of prefix in original
708 case */
709#else
710# define mi_cprefixlen mi_prefixlen /* it's the same value */
711#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000712
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000713 /* for when checking a compound word */
714 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000715 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
716 int mi_complen; /* nr of compound words used */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000717 int mi_compextra; /* nr of COMPOUNDROOT words */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000718
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000719 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000720 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000721 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000722 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000723
724 /* for NOBREAK */
725 int mi_result2; /* "mi_resul" without following word */
726 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000727} matchinf_T;
728
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000729/*
730 * The tables used for recognizing word characters according to spelling.
731 * These are only used for the first 256 characters of 'encoding'.
732 */
733typedef struct spelltab_S
734{
735 char_u st_isw[256]; /* flags: is word char */
736 char_u st_isu[256]; /* flags: is uppercase char */
737 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000738 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000739} spelltab_T;
740
741static spelltab_T spelltab;
742static int did_set_spelltab;
743
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000744#define CF_WORD 0x01
745#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000746
747static void clear_spell_chartab __ARGS((spelltab_T *sp));
748static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000749static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
750static int spell_iswordp_nmw __ARGS((char_u *p));
751#ifdef FEAT_MBYTE
752static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
753#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000754static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000755
756/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000757 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000758 */
759typedef enum
760{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000761 STATE_START = 0, /* At start of node check for NUL bytes (goodword
762 * ends); if badword ends there is a match, otherwise
763 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000764 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000765 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000766 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
767 STATE_PLAIN, /* Use each byte of the node. */
768 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000769 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000770 STATE_INS, /* Insert a byte in the bad word. */
771 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000772 STATE_UNSWAP, /* Undo swap two characters. */
773 STATE_SWAP3, /* Swap two characters over three. */
774 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000775 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000776 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000777 STATE_REP_INI, /* Prepare for using REP items. */
778 STATE_REP, /* Use matching REP items from the .aff file. */
779 STATE_REP_UNDO, /* Undo a REP item replacement. */
780 STATE_FINAL /* End of this node. */
781} state_T;
782
783/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000784 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000785 */
786typedef struct trystate_S
787{
Bram Moolenaarea424162005-06-16 21:51:00 +0000788 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000789 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000790 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000791 short ts_curi; /* index in list of child nodes */
792 char_u ts_fidx; /* index in fword[], case-folded bad word */
793 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
794 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000795 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000796 * PFD_PREFIXTREE or PFD_NOPREFIX */
797 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000798#ifdef FEAT_MBYTE
799 char_u ts_tcharlen; /* number of bytes in tword character */
800 char_u ts_tcharidx; /* current byte index in tword character */
801 char_u ts_isdiff; /* DIFF_ values */
802 char_u ts_fcharstart; /* index in fword where badword char started */
803#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000804 char_u ts_prewordlen; /* length of word in "preword[]" */
805 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000806 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000807 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000808 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000809 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000810 char_u ts_delidx; /* index in fword for char that was deleted,
811 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000812} trystate_T;
813
Bram Moolenaarea424162005-06-16 21:51:00 +0000814/* values for ts_isdiff */
815#define DIFF_NONE 0 /* no different byte (yet) */
816#define DIFF_YES 1 /* different byte found */
817#define DIFF_INSERT 2 /* inserting character */
818
Bram Moolenaard12a1322005-08-21 22:08:24 +0000819/* values for ts_flags */
820#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
821#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000822#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000823
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000824/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000825#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000826#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000827#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000828
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000829/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000830#define FIND_FOLDWORD 0 /* find word case-folded */
831#define FIND_KEEPWORD 1 /* find keep-case word */
832#define FIND_PREFIX 2 /* find word after prefix */
833#define FIND_COMPOUND 3 /* find case-folded compound word */
834#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000835
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000836static slang_T *slang_alloc __ARGS((char_u *lang));
837static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000838static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000839static void slang_clear_sug __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000840static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000841static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000842static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req));
Bram Moolenaard12a1322005-08-21 22:08:24 +0000843static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000844static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000845static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000846static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000847static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000848static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000849static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000850static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000851static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent));
Bram Moolenaarb388adb2006-02-28 23:50:17 +0000852static int get2c __ARGS((FILE *fd));
853static int get3c __ARGS((FILE *fd));
854static int get4c __ARGS((FILE *fd));
855static time_t get8c __ARGS((FILE *fd));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000856static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000857static char_u *read_string __ARGS((FILE *fd, int cnt));
858static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
859static int read_charflags_section __ARGS((FILE *fd));
860static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000861static int read_rep_section __ARGS((FILE *fd, garray_T *gap, short *first));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000862static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000863static int read_words_section __ARGS((FILE *fd, slang_T *lp, int len));
864static void count_common_word __ARGS((slang_T *lp, char_u *word, int len, int count));
865static int score_wordcount_adj __ARGS((slang_T *slang, int score, char_u *word, int split));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000866static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
867static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000868static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000869static int init_syl_tab __ARGS((slang_T *slang));
870static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000871static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
872static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000873#ifdef FEAT_MBYTE
874static int *mb_str2wide __ARGS((char_u *s));
875#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000876static int spell_read_tree __ARGS((FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt));
877static idx_T read_tree_node __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, int startidx, int prefixtree, int maxprefcondnr));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000878static void clear_midword __ARGS((buf_T *buf));
879static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000880static int find_region __ARGS((char_u *rp, char_u *region));
881static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000882static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000883static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000884static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000885static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000886static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000887static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar66fa2712006-01-22 23:22:22 +0000888static void spell_find_suggest __ARGS((char_u *badptr, int badlen, suginfo_T *su, int maxcount, int banbadword, int need_cap, int interactive));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000889#ifdef FEAT_EVAL
890static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
891#endif
892static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000893static void spell_suggest_intern __ARGS((suginfo_T *su, int interactive));
894static void suggest_load_files __ARGS((void));
895static void tree_count_words __ARGS((char_u *byts, idx_T *idxs));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000896static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000897static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000898static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000899static void suggest_try_special __ARGS((suginfo_T *su));
900static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000901static void suggest_trie_walk __ARGS((suginfo_T *su, langp_T *lp, char_u *fword, int soundfold));
902static void go_deeper __ARGS((trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000903#ifdef FEAT_MBYTE
904static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
905#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000906static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000907static void score_comp_sal __ARGS((suginfo_T *su));
908static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000909static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000910static void suggest_try_soundalike_prep __ARGS((void));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000911static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000912static void suggest_try_soundalike_finish __ARGS((void));
913static void add_sound_suggest __ARGS((suginfo_T *su, char_u *goodword, int score, langp_T *lp));
914static int soundfold_find __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000915static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000916static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000917static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000918static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus, slang_T *slang, int maxsf));
919static void check_suggestions __ARGS((suginfo_T *su, garray_T *gap));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000920static void add_banned __ARGS((suginfo_T *su, char_u *word));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000921static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000922static void rescore_one __ARGS((suginfo_T *su, suggest_T *stp));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000923static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000924static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
925static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
926static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000927#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000928static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000929#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000930static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000931static int spell_edit_score __ARGS((slang_T *slang, char_u *badword, char_u *goodword));
932static int spell_edit_score_limit __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
933#ifdef FEAT_MBYTE
934static int spell_edit_score_limit_w __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
935#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +0000936static void dump_word __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum));
937static linenr_T dump_prefixes __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T startlnum));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000938static buf_T *open_spellbuf __ARGS((void));
939static void close_spellbuf __ARGS((buf_T *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000940
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000941/*
942 * Use our own character-case definitions, because the current locale may
943 * differ from what the .spl file uses.
944 * These must not be called with negative number!
945 */
946#ifndef FEAT_MBYTE
947/* Non-multi-byte implementation. */
948# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
949# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
950# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
951#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000952# if defined(HAVE_WCHAR_H)
953# include <wchar.h> /* for towupper() and towlower() */
954# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000955/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
956 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
957 * the "w" library function for characters above 255 if available. */
958# ifdef HAVE_TOWLOWER
959# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
960 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
961# else
962# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
963 : (c) < 256 ? spelltab.st_fold[c] : (c))
964# endif
965
966# ifdef HAVE_TOWUPPER
967# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
968 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
969# else
970# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
971 : (c) < 256 ? spelltab.st_upper[c] : (c))
972# endif
973
974# ifdef HAVE_ISWUPPER
975# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
976 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
977# else
978# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000979 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000980# endif
981#endif
982
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000983
984static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000985static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000986static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000987static char *e_affname = N_("Affix name too long in %s line %d: %s");
988static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
989static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000990static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000991
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000992/* Remember what "z?" replaced. */
993static char_u *repl_from = NULL;
994static char_u *repl_to = NULL;
995
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000996/*
997 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000998 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000999 * "*attrp" is set to the highlight index for a badly spelled word. For a
1000 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001001 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001002 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001003 * "capcol" is used to check for a Capitalised word after the end of a
1004 * sentence. If it's zero then perform the check. Return the column where to
1005 * check next, or -1 when no sentence end was found. If it's NULL then don't
1006 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001007 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001008 * Returns the length of the word in bytes, also when it's OK, so that the
1009 * caller can skip over the word.
1010 */
1011 int
Bram Moolenaar4770d092006-01-12 23:22:24 +00001012spell_check(wp, ptr, attrp, capcol, docount)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001013 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001014 char_u *ptr;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001015 hlf_T *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001016 int *capcol; /* column to check for Capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001017 int docount; /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001018{
1019 matchinf_T mi; /* Most things are put in "mi" so that it can
1020 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001021 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001022 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001023 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001024 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001025 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001026
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001027 /* A word never starts at a space or a control character. Return quickly
1028 * then, skipping over the character. */
1029 if (*ptr <= ' ')
1030 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001031
1032 /* Return here when loading language files failed. */
1033 if (wp->w_buffer->b_langp.ga_len == 0)
1034 return 1;
1035
Bram Moolenaar5195e452005-08-19 20:32:47 +00001036 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001037
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001038 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +00001039 * 0X99FF. But always do check spelling to find "3GPP" and "11
1040 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001041 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +00001042 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001043 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
1044 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001045 else
1046 mi.mi_end = skipdigits(ptr);
Bram Moolenaar43abc522005-12-10 20:15:02 +00001047 nrlen = mi.mi_end - ptr;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001048 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001049
Bram Moolenaar0c405862005-06-22 22:26:26 +00001050 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001051 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +00001052 mi.mi_fend = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001053 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001054 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001055 do
Bram Moolenaar51485f02005-06-04 21:55:20 +00001056 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001057 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001058 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001059
1060 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
1061 {
1062 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00001063 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001064 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001065 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001066 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001067 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001068 if (capcol != NULL)
1069 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001070
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001071 /* We always use the characters up to the next non-word character,
1072 * also for bad words. */
1073 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001074
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001075 /* Check caps type later. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001076 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001077
Bram Moolenaar5195e452005-08-19 20:32:47 +00001078 /* case-fold the word with one non-word character, so that we can check
1079 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001080 if (*mi.mi_fend != NUL)
1081 mb_ptr_adv(mi.mi_fend);
1082
1083 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
1084 MAXWLEN + 1);
1085 mi.mi_fwordlen = STRLEN(mi.mi_fword);
1086
1087 /* The word is bad unless we recognize it. */
1088 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +00001089 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001090
1091 /*
1092 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001093 * We check them all, because a word may be matched longer in another
1094 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001095 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001096 for (lpi = 0; lpi < wp->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001097 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001098 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, lpi);
1099
1100 /* If reloading fails the language is still in the list but everything
1101 * has been cleared. */
1102 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
1103 continue;
1104
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001105 /* Check for a matching word in case-folded words. */
1106 find_word(&mi, FIND_FOLDWORD);
1107
1108 /* Check for a matching word in keep-case words. */
1109 find_word(&mi, FIND_KEEPWORD);
1110
1111 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001112 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +00001113
1114 /* For a NOBREAK language, may want to use a word without a following
1115 * word as a backup. */
1116 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
1117 && mi.mi_result2 != SP_BAD)
1118 {
1119 mi.mi_result = mi.mi_result2;
1120 mi.mi_end = mi.mi_end2;
1121 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001122
1123 /* Count the word in the first language where it's found to be OK. */
1124 if (count_word && mi.mi_result == SP_OK)
1125 {
1126 count_common_word(mi.mi_lp->lp_slang, ptr,
1127 (int)(mi.mi_end - ptr), 1);
1128 count_word = FALSE;
1129 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001130 }
1131
1132 if (mi.mi_result != SP_OK)
1133 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001134 /* If we found a number skip over it. Allows for "42nd". Do flag
1135 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001136 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001137 {
1138 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1139 return nrlen;
1140 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001141
1142 /* When we are at a non-word character there is no error, just
1143 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001144 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001145 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001146 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
1147 {
1148 regmatch_T regmatch;
1149
1150 /* Check for end of sentence. */
1151 regmatch.regprog = wp->w_buffer->b_cap_prog;
1152 regmatch.rm_ic = FALSE;
1153 if (vim_regexec(&regmatch, ptr, 0))
1154 *capcol = (int)(regmatch.endp[0] - ptr);
1155 }
1156
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001157#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001158 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001159 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001160#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001161 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001162 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001163 else if (mi.mi_end == ptr)
1164 /* Always include at least one character. Required for when there
1165 * is a mixup in "midword". */
1166 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +00001167 else if (mi.mi_result == SP_BAD
1168 && LANGP_ENTRY(wp->w_buffer->b_langp, 0)->lp_slang->sl_nobreak)
1169 {
1170 char_u *p, *fp;
1171 int save_result = mi.mi_result;
1172
1173 /* First language in 'spelllang' is NOBREAK. Find first position
1174 * at which any word would be valid. */
1175 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001176 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +00001177 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001178 p = mi.mi_word;
1179 fp = mi.mi_fword;
1180 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +00001181 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001182 mb_ptr_adv(p);
1183 mb_ptr_adv(fp);
1184 if (p >= mi.mi_end)
1185 break;
1186 mi.mi_compoff = fp - mi.mi_fword;
1187 find_word(&mi, FIND_COMPOUND);
1188 if (mi.mi_result != SP_BAD)
1189 {
1190 mi.mi_end = p;
1191 break;
1192 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001193 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001194 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001195 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001196 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001197
1198 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001199 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001200 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001201 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001202 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001203 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001204 }
1205
Bram Moolenaar5195e452005-08-19 20:32:47 +00001206 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1207 {
1208 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001209 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001210 return wrongcaplen;
1211 }
1212
Bram Moolenaar51485f02005-06-04 21:55:20 +00001213 return (int)(mi.mi_end - ptr);
1214}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001215
Bram Moolenaar51485f02005-06-04 21:55:20 +00001216/*
1217 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001218 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1219 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1220 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1221 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001222 *
1223 * For a match mip->mi_result is updated.
1224 */
1225 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001226find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001227 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001228 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001229{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001230 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001231 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001232 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001233 int endidxcnt = 0;
1234 int len;
1235 int wlen = 0;
1236 int flen;
1237 int c;
1238 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001239 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001240#ifdef FEAT_MBYTE
1241 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001242#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001243 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001244 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001245 slang_T *slang = mip->mi_lp->lp_slang;
1246 unsigned flags;
1247 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001248 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001249 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001250 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001251 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001252
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001253 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001254 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001255 /* Check for word with matching case in keep-case tree. */
1256 ptr = mip->mi_word;
1257 flen = 9999; /* no case folding, always enough bytes */
1258 byts = slang->sl_kbyts;
1259 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001260
1261 if (mode == FIND_KEEPCOMPOUND)
1262 /* Skip over the previously found word(s). */
1263 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001264 }
1265 else
1266 {
1267 /* Check for case-folded in case-folded tree. */
1268 ptr = mip->mi_fword;
1269 flen = mip->mi_fwordlen; /* available case-folded bytes */
1270 byts = slang->sl_fbyts;
1271 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001272
1273 if (mode == FIND_PREFIX)
1274 {
1275 /* Skip over the prefix. */
1276 wlen = mip->mi_prefixlen;
1277 flen -= mip->mi_prefixlen;
1278 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001279 else if (mode == FIND_COMPOUND)
1280 {
1281 /* Skip over the previously found word(s). */
1282 wlen = mip->mi_compoff;
1283 flen -= mip->mi_compoff;
1284 }
1285
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001286 }
1287
Bram Moolenaar51485f02005-06-04 21:55:20 +00001288 if (byts == NULL)
1289 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001290
Bram Moolenaar51485f02005-06-04 21:55:20 +00001291 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001292 * Repeat advancing in the tree until:
1293 * - there is a byte that doesn't match,
1294 * - we reach the end of the tree,
1295 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001296 */
1297 for (;;)
1298 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001299 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001300 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001301
1302 len = byts[arridx++];
1303
1304 /* If the first possible byte is a zero the word could end here.
1305 * Remember this index, we first check for the longest word. */
1306 if (byts[arridx] == 0)
1307 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001308 if (endidxcnt == MAXWLEN)
1309 {
1310 /* Must be a corrupted spell file. */
1311 EMSG(_(e_format));
1312 return;
1313 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001314 endlen[endidxcnt] = wlen;
1315 endidx[endidxcnt++] = arridx++;
1316 --len;
1317
1318 /* Skip over the zeros, there can be several flag/region
1319 * combinations. */
1320 while (len > 0 && byts[arridx] == 0)
1321 {
1322 ++arridx;
1323 --len;
1324 }
1325 if (len == 0)
1326 break; /* no children, word must end here */
1327 }
1328
1329 /* Stop looking at end of the line. */
1330 if (ptr[wlen] == NUL)
1331 break;
1332
1333 /* Perform a binary search in the list of accepted bytes. */
1334 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001335 if (c == TAB) /* <Tab> is handled like <Space> */
1336 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001337 lo = arridx;
1338 hi = arridx + len - 1;
1339 while (lo < hi)
1340 {
1341 m = (lo + hi) / 2;
1342 if (byts[m] > c)
1343 hi = m - 1;
1344 else if (byts[m] < c)
1345 lo = m + 1;
1346 else
1347 {
1348 lo = hi = m;
1349 break;
1350 }
1351 }
1352
1353 /* Stop if there is no matching byte. */
1354 if (hi < lo || byts[lo] != c)
1355 break;
1356
1357 /* Continue at the child (if there is one). */
1358 arridx = idxs[lo];
1359 ++wlen;
1360 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001361
1362 /* One space in the good word may stand for several spaces in the
1363 * checked word. */
1364 if (c == ' ')
1365 {
1366 for (;;)
1367 {
1368 if (flen <= 0 && *mip->mi_fend != NUL)
1369 flen = fold_more(mip);
1370 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1371 break;
1372 ++wlen;
1373 --flen;
1374 }
1375 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001376 }
1377
1378 /*
1379 * Verify that one of the possible endings is valid. Try the longest
1380 * first.
1381 */
1382 while (endidxcnt > 0)
1383 {
1384 --endidxcnt;
1385 arridx = endidx[endidxcnt];
1386 wlen = endlen[endidxcnt];
1387
1388#ifdef FEAT_MBYTE
1389 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1390 continue; /* not at first byte of character */
1391#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001392 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001393 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001394 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001395 continue; /* next char is a word character */
1396 word_ends = FALSE;
1397 }
1398 else
1399 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001400 /* The prefix flag is before compound flags. Once a valid prefix flag
1401 * has been found we try compound flags. */
1402 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001403
1404#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001405 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001406 {
1407 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001408 * when folding case. This can be slow, take a shortcut when the
1409 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001410 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001411 if (STRNCMP(ptr, p, wlen) != 0)
1412 {
1413 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1414 mb_ptr_adv(p);
1415 wlen = p - mip->mi_word;
1416 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001417 }
1418#endif
1419
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001420 /* Check flags and region. For FIND_PREFIX check the condition and
1421 * prefix ID.
1422 * Repeat this if there are more flags/region alternatives until there
1423 * is a match. */
1424 res = SP_BAD;
1425 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1426 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001427 {
1428 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001429
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001430 /* For the fold-case tree check that the case of the checked word
1431 * matches with what the word in the tree requires.
1432 * For keep-case tree the case is always right. For prefixes we
1433 * don't bother to check. */
1434 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001435 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001436 if (mip->mi_cend != mip->mi_word + wlen)
1437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001438 /* mi_capflags was set for a different word length, need
1439 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001440 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001441 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001442 }
1443
Bram Moolenaar0c405862005-06-22 22:26:26 +00001444 if (mip->mi_capflags == WF_KEEPCAP
1445 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001446 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001447 }
1448
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001449 /* When mode is FIND_PREFIX the word must support the prefix:
1450 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001451 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001452 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001453 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001454 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001455 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001456 mip->mi_word + mip->mi_cprefixlen, slang,
1457 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001458 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001459 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001460
1461 /* Use the WF_RARE flag for a rare prefix. */
1462 if (c & WF_RAREPFX)
1463 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001464 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001465 }
1466
Bram Moolenaar78622822005-08-23 21:00:13 +00001467 if (slang->sl_nobreak)
1468 {
1469 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1470 && (flags & WF_BANNED) == 0)
1471 {
1472 /* NOBREAK: found a valid following word. That's all we
1473 * need to know, so return. */
1474 mip->mi_result = SP_OK;
1475 break;
1476 }
1477 }
1478
1479 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1480 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001481 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00001482 /* If there is no flag or the word is shorter than
1483 * COMPOUNDMIN reject it quickly.
1484 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001485 * that's too short... Myspell compatibility requires this
1486 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001487 if (((unsigned)flags >> 24) == 0
1488 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001489 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001490#ifdef FEAT_MBYTE
1491 /* For multi-byte chars check character length against
1492 * COMPOUNDMIN. */
1493 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001494 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001495 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1496 wlen - mip->mi_compoff) < slang->sl_compminlen)
1497 continue;
1498#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001499
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001500 /* Limit the number of compound words to COMPOUNDWORDMAX if no
Bram Moolenaare52325c2005-08-22 22:54:29 +00001501 * maximum for syllables is specified. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001502 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
1503 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +00001504 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001505 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001506
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001507 /* Don't allow compounding on a side where an affix was added,
1508 * unless COMPOUNDPERMITFLAG was used. */
1509 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
1510 continue;
1511 if (!word_ends && (flags & WF_NOCOMPAFT))
1512 continue;
1513
Bram Moolenaard12a1322005-08-21 22:08:24 +00001514 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001515 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001516 ? slang->sl_compstartflags
1517 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001518 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001519 continue;
1520
Bram Moolenaare52325c2005-08-22 22:54:29 +00001521 if (mode == FIND_COMPOUND)
1522 {
1523 int capflags;
1524
1525 /* Need to check the caps type of the appended compound
1526 * word. */
1527#ifdef FEAT_MBYTE
1528 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1529 mip->mi_compoff) != 0)
1530 {
1531 /* case folding may have changed the length */
1532 p = mip->mi_word;
1533 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1534 mb_ptr_adv(p);
1535 }
1536 else
1537#endif
1538 p = mip->mi_word + mip->mi_compoff;
1539 capflags = captype(p, mip->mi_word + wlen);
1540 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1541 && (flags & WF_FIXCAP) != 0))
1542 continue;
1543
1544 if (capflags != WF_ALLCAP)
1545 {
1546 /* When the character before the word is a word
1547 * character we do not accept a Onecap word. We do
1548 * accept a no-caps word, even when the dictionary
1549 * word specifies ONECAP. */
1550 mb_ptr_back(mip->mi_word, p);
1551 if (spell_iswordp_nmw(p)
1552 ? capflags == WF_ONECAP
1553 : (flags & WF_ONECAP) != 0
1554 && capflags != WF_ONECAP)
1555 continue;
1556 }
1557 }
1558
Bram Moolenaar5195e452005-08-19 20:32:47 +00001559 /* If the word ends the sequence of compound flags of the
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001560 * words must match with one of the COMPOUNDRULE items and
Bram Moolenaar5195e452005-08-19 20:32:47 +00001561 * the number of syllables must not be too large. */
1562 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1563 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1564 if (word_ends)
1565 {
1566 char_u fword[MAXWLEN];
1567
1568 if (slang->sl_compsylmax < MAXWLEN)
1569 {
1570 /* "fword" is only needed for checking syllables. */
1571 if (ptr == mip->mi_word)
1572 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1573 else
1574 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1575 }
1576 if (!can_compound(slang, fword, mip->mi_compflags))
1577 continue;
1578 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001579 }
1580
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001581 /* Check NEEDCOMPOUND: can't use word without compounding. */
1582 else if (flags & WF_NEEDCOMP)
1583 continue;
1584
Bram Moolenaar78622822005-08-23 21:00:13 +00001585 nobreak_result = SP_OK;
1586
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001587 if (!word_ends)
1588 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001589 int save_result = mip->mi_result;
1590 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001591 langp_T *save_lp = mip->mi_lp;
1592 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001593
1594 /* Check that a valid word follows. If there is one and we
1595 * are compounding, it will set "mi_result", thus we are
1596 * always finished here. For NOBREAK we only check that a
1597 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001598 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001599 if (slang->sl_nobreak)
1600 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001601
1602 /* Find following word in case-folded tree. */
1603 mip->mi_compoff = endlen[endidxcnt];
1604#ifdef FEAT_MBYTE
1605 if (has_mbyte && mode == FIND_KEEPWORD)
1606 {
1607 /* Compute byte length in case-folded word from "wlen":
1608 * byte length in keep-case word. Length may change when
1609 * folding case. This can be slow, take a shortcut when
1610 * the case-folded word is equal to the keep-case word. */
1611 p = mip->mi_fword;
1612 if (STRNCMP(ptr, p, wlen) != 0)
1613 {
1614 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1615 mb_ptr_adv(p);
1616 mip->mi_compoff = p - mip->mi_fword;
1617 }
1618 }
1619#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001620 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001621 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001622 if (flags & WF_COMPROOT)
1623 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001624
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001625 /* For NOBREAK we need to try all NOBREAK languages, at least
1626 * to find the ".add" file(s). */
1627 for (lpi = 0; lpi < mip->mi_buf->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001628 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001629 if (slang->sl_nobreak)
1630 {
1631 mip->mi_lp = LANGP_ENTRY(mip->mi_buf->b_langp, lpi);
1632 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1633 || !mip->mi_lp->lp_slang->sl_nobreak)
1634 continue;
1635 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001636
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001637 find_word(mip, FIND_COMPOUND);
1638
1639 /* When NOBREAK any word that matches is OK. Otherwise we
1640 * need to find the longest match, thus try with keep-case
1641 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001642 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1643 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001644 /* Find following word in keep-case tree. */
1645 mip->mi_compoff = wlen;
1646 find_word(mip, FIND_KEEPCOMPOUND);
1647
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001648#if 0 /* Disabled, a prefix must not appear halfway a compound word,
1649 unless the COMPOUNDPERMITFLAG is used and then it can't be a
1650 postponed prefix. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001651 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1652 {
1653 /* Check for following word with prefix. */
1654 mip->mi_compoff = c;
1655 find_prefix(mip, FIND_COMPOUND);
1656 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001657#endif
Bram Moolenaar78622822005-08-23 21:00:13 +00001658 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001659
1660 if (!slang->sl_nobreak)
1661 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001662 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001663 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001664 if (flags & WF_COMPROOT)
1665 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001666 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001667
Bram Moolenaar78622822005-08-23 21:00:13 +00001668 if (slang->sl_nobreak)
1669 {
1670 nobreak_result = mip->mi_result;
1671 mip->mi_result = save_result;
1672 mip->mi_end = save_end;
1673 }
1674 else
1675 {
1676 if (mip->mi_result == SP_OK)
1677 break;
1678 continue;
1679 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001680 }
1681
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001682 if (flags & WF_BANNED)
1683 res = SP_BANNED;
1684 else if (flags & WF_REGION)
1685 {
1686 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001687 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001688 res = SP_OK;
1689 else
1690 res = SP_LOCAL;
1691 }
1692 else if (flags & WF_RARE)
1693 res = SP_RARE;
1694 else
1695 res = SP_OK;
1696
Bram Moolenaar78622822005-08-23 21:00:13 +00001697 /* Always use the longest match and the best result. For NOBREAK
1698 * we separately keep the longest match without a following good
1699 * word as a fall-back. */
1700 if (nobreak_result == SP_BAD)
1701 {
1702 if (mip->mi_result2 > res)
1703 {
1704 mip->mi_result2 = res;
1705 mip->mi_end2 = mip->mi_word + wlen;
1706 }
1707 else if (mip->mi_result2 == res
1708 && mip->mi_end2 < mip->mi_word + wlen)
1709 mip->mi_end2 = mip->mi_word + wlen;
1710 }
1711 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001712 {
1713 mip->mi_result = res;
1714 mip->mi_end = mip->mi_word + wlen;
1715 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001716 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001717 mip->mi_end = mip->mi_word + wlen;
1718
Bram Moolenaar78622822005-08-23 21:00:13 +00001719 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001720 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001721 }
1722
Bram Moolenaar78622822005-08-23 21:00:13 +00001723 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001724 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001725 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001726}
1727
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001728/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001729 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1730 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001731 */
1732 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001733can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001734 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001735 char_u *word;
1736 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001737{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001738 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001739#ifdef FEAT_MBYTE
1740 char_u uflags[MAXWLEN * 2];
1741 int i;
1742#endif
1743 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001744
1745 if (slang->sl_compprog == NULL)
1746 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001747#ifdef FEAT_MBYTE
1748 if (enc_utf8)
1749 {
1750 /* Need to convert the single byte flags to utf8 characters. */
1751 p = uflags;
1752 for (i = 0; flags[i] != NUL; ++i)
1753 p += mb_char2bytes(flags[i], p);
1754 *p = NUL;
1755 p = uflags;
1756 }
1757 else
1758#endif
1759 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001760 regmatch.regprog = slang->sl_compprog;
1761 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001762 if (!vim_regexec(&regmatch, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001763 return FALSE;
1764
Bram Moolenaare52325c2005-08-22 22:54:29 +00001765 /* Count the number of syllables. This may be slow, do it last. If there
1766 * are too many syllables AND the number of compound words is above
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001767 * COMPOUNDWORDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001768 if (slang->sl_compsylmax < MAXWLEN
1769 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001770 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001771 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001772}
1773
1774/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001775 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1776 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001777 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001778 */
1779 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001780valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001781 int totprefcnt; /* nr of prefix IDs */
1782 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001783 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001784 char_u *word;
1785 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001786 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001787{
1788 int prefcnt;
1789 int pidx;
1790 regprog_T *rp;
1791 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001792 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001793
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001794 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001795 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1796 {
1797 pidx = slang->sl_pidxs[arridx + prefcnt];
1798
1799 /* Check the prefix ID. */
1800 if (prefid != (pidx & 0xff))
1801 continue;
1802
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001803 /* Check if the prefix doesn't combine and the word already has a
1804 * suffix. */
1805 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1806 continue;
1807
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001808 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001809 * stored in the two bytes above the prefix ID byte. */
1810 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001811 if (rp != NULL)
1812 {
1813 regmatch.regprog = rp;
1814 regmatch.rm_ic = FALSE;
1815 if (!vim_regexec(&regmatch, word, 0))
1816 continue;
1817 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001818 else if (cond_req)
1819 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001820
Bram Moolenaar53805d12005-08-01 07:08:33 +00001821 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001822 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001823 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001824 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001825}
1826
1827/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001828 * Check if the word at "mip->mi_word" has a matching prefix.
1829 * If it does, then check the following word.
1830 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001831 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1832 * prefix in a compound word.
1833 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001834 * For a match mip->mi_result is updated.
1835 */
1836 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001837find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001838 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001839 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001840{
1841 idx_T arridx = 0;
1842 int len;
1843 int wlen = 0;
1844 int flen;
1845 int c;
1846 char_u *ptr;
1847 idx_T lo, hi, m;
1848 slang_T *slang = mip->mi_lp->lp_slang;
1849 char_u *byts;
1850 idx_T *idxs;
1851
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001852 byts = slang->sl_pbyts;
1853 if (byts == NULL)
1854 return; /* array is empty */
1855
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001856 /* We use the case-folded word here, since prefixes are always
1857 * case-folded. */
1858 ptr = mip->mi_fword;
1859 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001860 if (mode == FIND_COMPOUND)
1861 {
1862 /* Skip over the previously found word(s). */
1863 ptr += mip->mi_compoff;
1864 flen -= mip->mi_compoff;
1865 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001866 idxs = slang->sl_pidxs;
1867
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001868 /*
1869 * Repeat advancing in the tree until:
1870 * - there is a byte that doesn't match,
1871 * - we reach the end of the tree,
1872 * - or we reach the end of the line.
1873 */
1874 for (;;)
1875 {
1876 if (flen == 0 && *mip->mi_fend != NUL)
1877 flen = fold_more(mip);
1878
1879 len = byts[arridx++];
1880
1881 /* If the first possible byte is a zero the prefix could end here.
1882 * Check if the following word matches and supports the prefix. */
1883 if (byts[arridx] == 0)
1884 {
1885 /* There can be several prefixes with different conditions. We
1886 * try them all, since we don't know which one will give the
1887 * longest match. The word is the same each time, pass the list
1888 * of possible prefixes to find_word(). */
1889 mip->mi_prefarridx = arridx;
1890 mip->mi_prefcnt = len;
1891 while (len > 0 && byts[arridx] == 0)
1892 {
1893 ++arridx;
1894 --len;
1895 }
1896 mip->mi_prefcnt -= len;
1897
1898 /* Find the word that comes after the prefix. */
1899 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001900 if (mode == FIND_COMPOUND)
1901 /* Skip over the previously found word(s). */
1902 mip->mi_prefixlen += mip->mi_compoff;
1903
Bram Moolenaar53805d12005-08-01 07:08:33 +00001904#ifdef FEAT_MBYTE
1905 if (has_mbyte)
1906 {
1907 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001908 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1909 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001910 }
1911 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001912 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001913#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001914 find_word(mip, FIND_PREFIX);
1915
1916
1917 if (len == 0)
1918 break; /* no children, word must end here */
1919 }
1920
1921 /* Stop looking at end of the line. */
1922 if (ptr[wlen] == NUL)
1923 break;
1924
1925 /* Perform a binary search in the list of accepted bytes. */
1926 c = ptr[wlen];
1927 lo = arridx;
1928 hi = arridx + len - 1;
1929 while (lo < hi)
1930 {
1931 m = (lo + hi) / 2;
1932 if (byts[m] > c)
1933 hi = m - 1;
1934 else if (byts[m] < c)
1935 lo = m + 1;
1936 else
1937 {
1938 lo = hi = m;
1939 break;
1940 }
1941 }
1942
1943 /* Stop if there is no matching byte. */
1944 if (hi < lo || byts[lo] != c)
1945 break;
1946
1947 /* Continue at the child (if there is one). */
1948 arridx = idxs[lo];
1949 ++wlen;
1950 --flen;
1951 }
1952}
1953
1954/*
1955 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001956 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001957 * Return the length of the folded chars in bytes.
1958 */
1959 static int
1960fold_more(mip)
1961 matchinf_T *mip;
1962{
1963 int flen;
1964 char_u *p;
1965
1966 p = mip->mi_fend;
1967 do
1968 {
1969 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001970 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001971
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001972 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001973 if (*mip->mi_fend != NUL)
1974 mb_ptr_adv(mip->mi_fend);
1975
1976 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1977 mip->mi_fword + mip->mi_fwordlen,
1978 MAXWLEN - mip->mi_fwordlen);
1979 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1980 mip->mi_fwordlen += flen;
1981 return flen;
1982}
1983
1984/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001985 * Check case flags for a word. Return TRUE if the word has the requested
1986 * case.
1987 */
1988 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001989spell_valid_case(wordflags, treeflags)
1990 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001991 int treeflags; /* flags for the word in the spell tree */
1992{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001993 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001994 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001995 && ((treeflags & WF_ONECAP) == 0
1996 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001997}
1998
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001999/*
2000 * Return TRUE if spell checking is not enabled.
2001 */
2002 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00002003no_spell_checking(wp)
2004 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002005{
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002006 if (!wp->w_p_spell || *wp->w_buffer->b_p_spl == NUL
2007 || wp->w_buffer->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002008 {
2009 EMSG(_("E756: Spell checking is not enabled"));
2010 return TRUE;
2011 }
2012 return FALSE;
2013}
Bram Moolenaar51485f02005-06-04 21:55:20 +00002014
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002015/*
2016 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002017 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
2018 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002019 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
2020 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00002021 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002022 */
2023 int
Bram Moolenaar95529562005-08-25 21:21:38 +00002024spell_move_to(wp, dir, allwords, curline, attrp)
2025 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002026 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002027 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002028 int curline;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002029 hlf_T *attrp; /* return: attributes of bad word or NULL
2030 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002031{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002032 linenr_T lnum;
2033 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002034 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002035 char_u *line;
2036 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002037 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002038 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002039 int len;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002040# ifdef FEAT_SYN_HL
Bram Moolenaar95529562005-08-25 21:21:38 +00002041 int has_syntax = syntax_present(wp->w_buffer);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002042 int col;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002043# endif
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002044 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002045 char_u *buf = NULL;
2046 int buflen = 0;
2047 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002048 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002049 int found_one = FALSE;
2050 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002051
Bram Moolenaar95529562005-08-25 21:21:38 +00002052 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00002053 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002054
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002055 /*
2056 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00002057 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002058 *
2059 * When searching backwards, we continue in the line to find the last
2060 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00002061 *
2062 * We concatenate the start of the next line, so that wrapped words work
2063 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
2064 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002065 */
Bram Moolenaar95529562005-08-25 21:21:38 +00002066 lnum = wp->w_cursor.lnum;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002067 clearpos(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002068
2069 while (!got_int)
2070 {
Bram Moolenaar95529562005-08-25 21:21:38 +00002071 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002072
Bram Moolenaar0c405862005-06-22 22:26:26 +00002073 len = STRLEN(line);
2074 if (buflen < len + MAXWLEN + 2)
2075 {
2076 vim_free(buf);
2077 buflen = len + MAXWLEN + 2;
2078 buf = alloc(buflen);
2079 if (buf == NULL)
2080 break;
2081 }
2082
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002083 /* In first line check first word for Capital. */
2084 if (lnum == 1)
2085 capcol = 0;
2086
2087 /* For checking first word with a capital skip white space. */
2088 if (capcol == 0)
2089 capcol = skipwhite(line) - line;
2090
Bram Moolenaar0c405862005-06-22 22:26:26 +00002091 /* Copy the line into "buf" and append the start of the next line if
2092 * possible. */
2093 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00002094 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002095 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
2096
2097 p = buf + skip;
2098 endp = buf + len;
2099 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002100 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002101 /* When searching backward don't search after the cursor. Unless
2102 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002103 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00002104 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002105 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00002106 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002107 break;
2108
2109 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002110 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002111 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002112
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002113 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002114 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002115 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002116 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002117 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002118 found_one = TRUE;
2119
Bram Moolenaar51485f02005-06-04 21:55:20 +00002120 /* When searching forward only accept a bad word after
2121 * the cursor. */
2122 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002123 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00002124 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002125 && (wrapped
2126 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00002127 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002128 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002129 {
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002130# ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00002131 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002132 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00002133 col = p - buf;
Bram Moolenaar95529562005-08-25 21:21:38 +00002134 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar51485f02005-06-04 21:55:20 +00002135 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002136 }
2137 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002138#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002139 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002140
Bram Moolenaar51485f02005-06-04 21:55:20 +00002141 if (can_spell)
2142 {
2143 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002144 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002145#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00002146 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002147#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002148 if (dir == FORWARD)
2149 {
2150 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00002151 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002152 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002153 if (attrp != NULL)
2154 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002155 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002156 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002157 else if (curline)
2158 /* Insert mode completion: put cursor after
2159 * the bad word. */
2160 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002161 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002162 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002163 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002164 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002165 }
2166
Bram Moolenaar51485f02005-06-04 21:55:20 +00002167 /* advance to character after the word */
2168 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002169 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002170 }
2171
Bram Moolenaar5195e452005-08-19 20:32:47 +00002172 if (dir == BACKWARD && found_pos.lnum != 0)
2173 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002174 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00002175 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002176 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002177 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002178 }
2179
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002180 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002181 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002182
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002183 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002184 if (dir == BACKWARD)
2185 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002186 /* If we are back at the starting line and searched it again there
2187 * is no match, give up. */
2188 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002189 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002190
2191 if (lnum > 1)
2192 --lnum;
2193 else if (!p_ws)
2194 break; /* at first line and 'nowrapscan' */
2195 else
2196 {
2197 /* Wrap around to the end of the buffer. May search the
2198 * starting line again and accept the last match. */
2199 lnum = wp->w_buffer->b_ml.ml_line_count;
2200 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002201 if (!shortmess(SHM_SEARCH))
2202 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002203 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002204 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002205 }
2206 else
2207 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002208 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2209 ++lnum;
2210 else if (!p_ws)
2211 break; /* at first line and 'nowrapscan' */
2212 else
2213 {
2214 /* Wrap around to the start of the buffer. May search the
2215 * starting line again and accept the first match. */
2216 lnum = 1;
2217 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002218 if (!shortmess(SHM_SEARCH))
2219 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002220 }
2221
2222 /* If we are back at the starting line and there is no match then
2223 * give up. */
2224 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002225 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002226
2227 /* Skip the characters at the start of the next line that were
2228 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002229 if (attr == HLF_COUNT)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002230 skip = p - endp;
2231 else
2232 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002233
2234 /* Capscol skips over the inserted space. */
2235 --capcol;
2236
2237 /* But after empty line check first word in next line */
2238 if (*skipwhite(line) == NUL)
2239 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002240 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002241
2242 line_breakcheck();
2243 }
2244
Bram Moolenaar0c405862005-06-22 22:26:26 +00002245 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002246 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002247}
2248
2249/*
2250 * For spell checking: concatenate the start of the following line "line" into
2251 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
2252 */
2253 void
2254spell_cat_line(buf, line, maxlen)
2255 char_u *buf;
2256 char_u *line;
2257 int maxlen;
2258{
2259 char_u *p;
2260 int n;
2261
2262 p = skipwhite(line);
2263 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2264 p = skipwhite(p + 1);
2265
2266 if (*p != NUL)
2267 {
2268 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002269 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002270 n = p - line;
2271 if (n >= maxlen)
2272 n = maxlen - 1;
2273 vim_memset(buf + 1, ' ', n);
2274 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002275}
2276
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002277/*
2278 * Structure used for the cookie argument of do_in_runtimepath().
2279 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002280typedef struct spelload_S
2281{
2282 char_u sl_lang[MAXWLEN + 1]; /* language name */
2283 slang_T *sl_slang; /* resulting slang_T struct */
2284 int sl_nobreak; /* NOBREAK language found */
2285} spelload_T;
2286
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002287/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002288 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002289 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002290 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002291 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002292spell_load_lang(lang)
2293 char_u *lang;
2294{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002295 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002296 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002297 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002298#ifdef FEAT_AUTOCMD
2299 int round;
2300#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002301
Bram Moolenaarb765d632005-06-07 21:00:02 +00002302 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002303 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002304 STRCPY(sl.sl_lang, lang);
2305 sl.sl_slang = NULL;
2306 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002307
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002308#ifdef FEAT_AUTOCMD
2309 /* We may retry when no spell file is found for the language, an
2310 * autocommand may load it then. */
2311 for (round = 1; round <= 2; ++round)
2312#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002313 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002314 /*
2315 * Find the first spell file for "lang" in 'runtimepath' and load it.
2316 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002317 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002318 "spell/%s.%s.spl", lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002319 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002320
2321 if (r == FAIL && *sl.sl_lang != NUL)
2322 {
2323 /* Try loading the ASCII version. */
2324 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2325 "spell/%s.ascii.spl", lang);
2326 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
2327
2328#ifdef FEAT_AUTOCMD
2329 if (r == FAIL && *sl.sl_lang != NUL && round == 1
2330 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
2331 curbuf->b_fname, FALSE, curbuf))
2332 continue;
2333 break;
2334#endif
2335 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002336#ifdef FEAT_AUTOCMD
2337 break;
2338#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002339 }
2340
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002341 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002342 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002343 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2344 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002345 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002346 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002347 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002348 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002349 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002350 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002351 }
2352}
2353
2354/*
2355 * Return the encoding used for spell checking: Use 'encoding', except that we
2356 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2357 */
2358 static char_u *
2359spell_enc()
2360{
2361
2362#ifdef FEAT_MBYTE
2363 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2364 return p_enc;
2365#endif
2366 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002367}
2368
2369/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002370 * Get the name of the .spl file for the internal wordlist into
2371 * "fname[MAXPATHL]".
2372 */
2373 static void
2374int_wordlist_spl(fname)
2375 char_u *fname;
2376{
2377 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2378 int_wordlist, spell_enc());
2379}
2380
2381/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002382 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002383 * Caller must fill "sl_next".
2384 */
2385 static slang_T *
2386slang_alloc(lang)
2387 char_u *lang;
2388{
2389 slang_T *lp;
2390
Bram Moolenaar51485f02005-06-04 21:55:20 +00002391 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002392 if (lp != NULL)
2393 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002394 if (lang != NULL)
2395 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002396 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002397 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002398 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002399 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002400 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002401 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002402
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002403 return lp;
2404}
2405
2406/*
2407 * Free the contents of an slang_T and the structure itself.
2408 */
2409 static void
2410slang_free(lp)
2411 slang_T *lp;
2412{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002413 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002414 vim_free(lp->sl_fname);
2415 slang_clear(lp);
2416 vim_free(lp);
2417}
2418
2419/*
2420 * Clear an slang_T so that the file can be reloaded.
2421 */
2422 static void
2423slang_clear(lp)
2424 slang_T *lp;
2425{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002426 garray_T *gap;
2427 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002428 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002429 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002430 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002431
Bram Moolenaar51485f02005-06-04 21:55:20 +00002432 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002433 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002434 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002435 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002436 vim_free(lp->sl_pbyts);
2437 lp->sl_pbyts = NULL;
2438
Bram Moolenaar51485f02005-06-04 21:55:20 +00002439 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002440 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002441 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002442 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002443 vim_free(lp->sl_pidxs);
2444 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002445
Bram Moolenaar4770d092006-01-12 23:22:24 +00002446 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002447 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002448 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2449 while (gap->ga_len > 0)
2450 {
2451 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2452 vim_free(ftp->ft_from);
2453 vim_free(ftp->ft_to);
2454 }
2455 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002456 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002457
2458 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002459 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002460 {
2461 /* "ga_len" is set to 1 without adding an item for latin1 */
2462 if (gap->ga_data != NULL)
2463 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2464 for (i = 0; i < gap->ga_len; ++i)
2465 vim_free(((int **)gap->ga_data)[i]);
2466 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002467 else
2468 /* SAL items: free salitem_T items */
2469 while (gap->ga_len > 0)
2470 {
2471 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2472 vim_free(smp->sm_lead);
2473 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2474 vim_free(smp->sm_to);
2475#ifdef FEAT_MBYTE
2476 vim_free(smp->sm_lead_w);
2477 vim_free(smp->sm_oneof_w);
2478 vim_free(smp->sm_to_w);
2479#endif
2480 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002481 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002482
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002483 for (i = 0; i < lp->sl_prefixcnt; ++i)
2484 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002485 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002486 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002487 lp->sl_prefprog = NULL;
2488
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002489 vim_free(lp->sl_info);
2490 lp->sl_info = NULL;
2491
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002492 vim_free(lp->sl_midword);
2493 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002494
Bram Moolenaar5195e452005-08-19 20:32:47 +00002495 vim_free(lp->sl_compprog);
2496 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002497 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002498 lp->sl_compprog = NULL;
2499 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002500 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002501
2502 vim_free(lp->sl_syllable);
2503 lp->sl_syllable = NULL;
2504 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002505
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002506 ga_clear_strings(&lp->sl_comppat);
2507
Bram Moolenaar4770d092006-01-12 23:22:24 +00002508 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2509 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002510
Bram Moolenaar4770d092006-01-12 23:22:24 +00002511#ifdef FEAT_MBYTE
2512 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002513#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002514
Bram Moolenaar4770d092006-01-12 23:22:24 +00002515 /* Clear info from .sug file. */
2516 slang_clear_sug(lp);
2517
Bram Moolenaar5195e452005-08-19 20:32:47 +00002518 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002519 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002520 lp->sl_compsylmax = MAXWLEN;
2521 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002522}
2523
2524/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002525 * Clear the info from the .sug file in "lp".
2526 */
2527 static void
2528slang_clear_sug(lp)
2529 slang_T *lp;
2530{
2531 vim_free(lp->sl_sbyts);
2532 lp->sl_sbyts = NULL;
2533 vim_free(lp->sl_sidxs);
2534 lp->sl_sidxs = NULL;
2535 close_spellbuf(lp->sl_sugbuf);
2536 lp->sl_sugbuf = NULL;
2537 lp->sl_sugloaded = FALSE;
2538 lp->sl_sugtime = 0;
2539}
2540
2541/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002542 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002543 * Invoked through do_in_runtimepath().
2544 */
2545 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002546spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002547 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002548 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002549{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002550 spelload_T *slp = (spelload_T *)cookie;
2551 slang_T *slang;
2552
2553 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2554 if (slang != NULL)
2555 {
2556 /* When a previously loaded file has NOBREAK also use it for the
2557 * ".add" files. */
2558 if (slp->sl_nobreak && slang->sl_add)
2559 slang->sl_nobreak = TRUE;
2560 else if (slang->sl_nobreak)
2561 slp->sl_nobreak = TRUE;
2562
2563 slp->sl_slang = slang;
2564 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002565}
2566
2567/*
2568 * Load one spell file and store the info into a slang_T.
2569 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00002570 * This is invoked in three ways:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002571 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2572 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2573 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2574 * points to the existing slang_T.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002575 * - Just after writing a .spl file; it's read back to produce the .sug file.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002576 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
2577 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002578 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002579 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002580 static slang_T *
2581spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002582 char_u *fname;
2583 char_u *lang;
2584 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002585 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002586{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002587 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002588 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002589 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002590 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002591 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002592 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002593 char_u *save_sourcing_name = sourcing_name;
2594 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002595 slang_T *lp = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002596 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002597 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002598
Bram Moolenaarb765d632005-06-07 21:00:02 +00002599 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002600 if (fd == NULL)
2601 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002602 if (!silent)
2603 EMSG2(_(e_notopen), fname);
2604 else if (p_verbose > 2)
2605 {
2606 verbose_enter();
2607 smsg((char_u *)e_notopen, fname);
2608 verbose_leave();
2609 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002610 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002611 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002612 if (p_verbose > 2)
2613 {
2614 verbose_enter();
2615 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2616 verbose_leave();
2617 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002618
Bram Moolenaarb765d632005-06-07 21:00:02 +00002619 if (old_lp == NULL)
2620 {
2621 lp = slang_alloc(lang);
2622 if (lp == NULL)
2623 goto endFAIL;
2624
2625 /* Remember the file name, used to reload the file when it's updated. */
2626 lp->sl_fname = vim_strsave(fname);
2627 if (lp->sl_fname == NULL)
2628 goto endFAIL;
2629
2630 /* Check for .add.spl. */
2631 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2632 }
2633 else
2634 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002635
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002636 /* Set sourcing_name, so that error messages mention the file name. */
2637 sourcing_name = fname;
2638 sourcing_lnum = 0;
2639
Bram Moolenaar4770d092006-01-12 23:22:24 +00002640 /*
2641 * <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002642 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002643 for (i = 0; i < VIMSPELLMAGICL; ++i)
2644 buf[i] = getc(fd); /* <fileID> */
2645 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2646 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002647 EMSG(_("E757: This does not look like a spell file"));
2648 goto endFAIL;
2649 }
2650 c = getc(fd); /* <versionnr> */
2651 if (c < VIMSPELLVERSION)
2652 {
2653 EMSG(_("E771: Old spell file, needs to be updated"));
2654 goto endFAIL;
2655 }
2656 else if (c > VIMSPELLVERSION)
2657 {
2658 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002659 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002660 }
2661
Bram Moolenaar5195e452005-08-19 20:32:47 +00002662
2663 /*
2664 * <SECTIONS>: <section> ... <sectionend>
2665 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2666 */
2667 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002668 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002669 n = getc(fd); /* <sectionID> or <sectionend> */
2670 if (n == SN_END)
2671 break;
2672 c = getc(fd); /* <sectionflags> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002673 len = get4c(fd); /* <sectionlen> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002674 if (len < 0)
2675 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002676
Bram Moolenaar5195e452005-08-19 20:32:47 +00002677 res = 0;
2678 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002679 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002680 case SN_INFO:
2681 lp->sl_info = read_string(fd, len); /* <infotext> */
2682 if (lp->sl_info == NULL)
2683 goto endFAIL;
2684 break;
2685
Bram Moolenaar5195e452005-08-19 20:32:47 +00002686 case SN_REGION:
2687 res = read_region_section(fd, lp, len);
2688 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002689
Bram Moolenaar5195e452005-08-19 20:32:47 +00002690 case SN_CHARFLAGS:
2691 res = read_charflags_section(fd);
2692 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002693
Bram Moolenaar5195e452005-08-19 20:32:47 +00002694 case SN_MIDWORD:
2695 lp->sl_midword = read_string(fd, len); /* <midword> */
2696 if (lp->sl_midword == NULL)
2697 goto endFAIL;
2698 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002699
Bram Moolenaar5195e452005-08-19 20:32:47 +00002700 case SN_PREFCOND:
2701 res = read_prefcond_section(fd, lp);
2702 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002703
Bram Moolenaar5195e452005-08-19 20:32:47 +00002704 case SN_REP:
Bram Moolenaar4770d092006-01-12 23:22:24 +00002705 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
2706 break;
2707
2708 case SN_REPSAL:
2709 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002710 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002711
Bram Moolenaar5195e452005-08-19 20:32:47 +00002712 case SN_SAL:
2713 res = read_sal_section(fd, lp);
2714 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002715
Bram Moolenaar5195e452005-08-19 20:32:47 +00002716 case SN_SOFO:
2717 res = read_sofo_section(fd, lp);
2718 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002719
Bram Moolenaar5195e452005-08-19 20:32:47 +00002720 case SN_MAP:
2721 p = read_string(fd, len); /* <mapstr> */
2722 if (p == NULL)
2723 goto endFAIL;
2724 set_map_str(lp, p);
2725 vim_free(p);
2726 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002727
Bram Moolenaar4770d092006-01-12 23:22:24 +00002728 case SN_WORDS:
2729 res = read_words_section(fd, lp, len);
2730 break;
2731
2732 case SN_SUGFILE:
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002733 lp->sl_sugtime = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002734 break;
2735
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002736 case SN_NOSPLITSUGS:
2737 lp->sl_nosplitsugs = TRUE; /* <timestamp> */
2738 break;
2739
Bram Moolenaar5195e452005-08-19 20:32:47 +00002740 case SN_COMPOUND:
2741 res = read_compound(fd, lp, len);
2742 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002743
Bram Moolenaar78622822005-08-23 21:00:13 +00002744 case SN_NOBREAK:
2745 lp->sl_nobreak = TRUE;
2746 break;
2747
Bram Moolenaar5195e452005-08-19 20:32:47 +00002748 case SN_SYLLABLE:
2749 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2750 if (lp->sl_syllable == NULL)
2751 goto endFAIL;
2752 if (init_syl_tab(lp) == FAIL)
2753 goto endFAIL;
2754 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002755
Bram Moolenaar5195e452005-08-19 20:32:47 +00002756 default:
2757 /* Unsupported section. When it's required give an error
2758 * message. When it's not required skip the contents. */
2759 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002760 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002761 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002762 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002763 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002764 while (--len >= 0)
2765 if (getc(fd) < 0)
2766 goto truncerr;
2767 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002768 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002769someerror:
Bram Moolenaar5195e452005-08-19 20:32:47 +00002770 if (res == SP_FORMERROR)
2771 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002772 EMSG(_(e_format));
2773 goto endFAIL;
2774 }
2775 if (res == SP_TRUNCERROR)
2776 {
2777truncerr:
2778 EMSG(_(e_spell_trunc));
2779 goto endFAIL;
2780 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002781 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002782 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002783 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002784
Bram Moolenaar4770d092006-01-12 23:22:24 +00002785 /* <LWORDTREE> */
2786 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
2787 if (res != 0)
2788 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002789
Bram Moolenaar4770d092006-01-12 23:22:24 +00002790 /* <KWORDTREE> */
2791 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
2792 if (res != 0)
2793 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002794
Bram Moolenaar4770d092006-01-12 23:22:24 +00002795 /* <PREFIXTREE> */
2796 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
2797 lp->sl_prefixcnt);
2798 if (res != 0)
2799 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002800
Bram Moolenaarb765d632005-06-07 21:00:02 +00002801 /* For a new file link it in the list of spell files. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002802 if (old_lp == NULL && lang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002803 {
2804 lp->sl_next = first_lang;
2805 first_lang = lp;
2806 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002807
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002808 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002809
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002810endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002811 if (lang != NULL)
2812 /* truncating the name signals the error to spell_load_lang() */
2813 *lang = NUL;
2814 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002815 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002816 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002817
2818endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002819 if (fd != NULL)
2820 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002821 sourcing_name = save_sourcing_name;
2822 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002823
2824 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002825}
2826
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002827/*
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002828 * Read 2 bytes from "fd" and turn them into an int, MSB first.
2829 */
2830 static int
2831get2c(fd)
2832 FILE *fd;
2833{
2834 long n;
2835
2836 n = getc(fd);
2837 n = (n << 8) + getc(fd);
2838 return n;
2839}
2840
2841/*
2842 * Read 3 bytes from "fd" and turn them into an int, MSB first.
2843 */
2844 static int
2845get3c(fd)
2846 FILE *fd;
2847{
2848 long n;
2849
2850 n = getc(fd);
2851 n = (n << 8) + getc(fd);
2852 n = (n << 8) + getc(fd);
2853 return n;
2854}
2855
2856/*
2857 * Read 4 bytes from "fd" and turn them into an int, MSB first.
2858 */
2859 static int
2860get4c(fd)
2861 FILE *fd;
2862{
2863 long n;
2864
2865 n = getc(fd);
2866 n = (n << 8) + getc(fd);
2867 n = (n << 8) + getc(fd);
2868 n = (n << 8) + getc(fd);
2869 return n;
2870}
2871
2872/*
2873 * Read 8 bytes from "fd" and turn them into a time_t, MSB first.
2874 */
2875 static time_t
2876get8c(fd)
2877 FILE *fd;
2878{
2879 time_t n = 0;
2880 int i;
2881
2882 for (i = 0; i < 8; ++i)
2883 n = (n << 8) + getc(fd);
2884 return n;
2885}
2886
2887/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002888 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002889 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002890 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002891 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2892 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002893 */
2894 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002895read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002896 FILE *fd;
2897 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002898 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002899{
2900 int cnt = 0;
2901 int i;
2902 char_u *str;
2903
2904 /* read the length bytes, MSB first */
2905 for (i = 0; i < cnt_bytes; ++i)
2906 cnt = (cnt << 8) + getc(fd);
2907 if (cnt < 0)
2908 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002909 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002910 return NULL;
2911 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002912 *cntp = cnt;
2913 if (cnt == 0)
2914 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002915
Bram Moolenaar5195e452005-08-19 20:32:47 +00002916 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002917 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002918 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002919 return str;
2920}
2921
Bram Moolenaar7887d882005-07-01 22:33:52 +00002922/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002923 * Read a string of length "cnt" from "fd" into allocated memory.
2924 * Returns NULL when out of memory.
2925 */
2926 static char_u *
2927read_string(fd, cnt)
2928 FILE *fd;
2929 int cnt;
2930{
2931 char_u *str;
2932 int i;
2933
2934 /* allocate memory */
2935 str = alloc((unsigned)cnt + 1);
2936 if (str != NULL)
2937 {
2938 /* Read the string. Doesn't check for truncated file. */
2939 for (i = 0; i < cnt; ++i)
2940 str[i] = getc(fd);
2941 str[i] = NUL;
2942 }
2943 return str;
2944}
2945
2946/*
2947 * Read SN_REGION: <regionname> ...
2948 * Return SP_*ERROR flags.
2949 */
2950 static int
2951read_region_section(fd, lp, len)
2952 FILE *fd;
2953 slang_T *lp;
2954 int len;
2955{
2956 int i;
2957
2958 if (len > 16)
2959 return SP_FORMERROR;
2960 for (i = 0; i < len; ++i)
2961 lp->sl_regions[i] = getc(fd); /* <regionname> */
2962 lp->sl_regions[len] = NUL;
2963 return 0;
2964}
2965
2966/*
2967 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2968 * <folcharslen> <folchars>
2969 * Return SP_*ERROR flags.
2970 */
2971 static int
2972read_charflags_section(fd)
2973 FILE *fd;
2974{
2975 char_u *flags;
2976 char_u *fol;
2977 int flagslen, follen;
2978
2979 /* <charflagslen> <charflags> */
2980 flags = read_cnt_string(fd, 1, &flagslen);
2981 if (flagslen < 0)
2982 return flagslen;
2983
2984 /* <folcharslen> <folchars> */
2985 fol = read_cnt_string(fd, 2, &follen);
2986 if (follen < 0)
2987 {
2988 vim_free(flags);
2989 return follen;
2990 }
2991
2992 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
2993 if (flags != NULL && fol != NULL)
2994 set_spell_charflags(flags, flagslen, fol);
2995
2996 vim_free(flags);
2997 vim_free(fol);
2998
2999 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
3000 if ((flags == NULL) != (fol == NULL))
3001 return SP_FORMERROR;
3002 return 0;
3003}
3004
3005/*
3006 * Read SN_PREFCOND section.
3007 * Return SP_*ERROR flags.
3008 */
3009 static int
3010read_prefcond_section(fd, lp)
3011 FILE *fd;
3012 slang_T *lp;
3013{
3014 int cnt;
3015 int i;
3016 int n;
3017 char_u *p;
3018 char_u buf[MAXWLEN + 1];
3019
3020 /* <prefcondcnt> <prefcond> ... */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003021 cnt = get2c(fd); /* <prefcondcnt> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003022 if (cnt <= 0)
3023 return SP_FORMERROR;
3024
3025 lp->sl_prefprog = (regprog_T **)alloc_clear(
3026 (unsigned)sizeof(regprog_T *) * cnt);
3027 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003028 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003029 lp->sl_prefixcnt = cnt;
3030
3031 for (i = 0; i < cnt; ++i)
3032 {
3033 /* <prefcond> : <condlen> <condstr> */
3034 n = getc(fd); /* <condlen> */
3035 if (n < 0 || n >= MAXWLEN)
3036 return SP_FORMERROR;
3037
3038 /* When <condlen> is zero we have an empty condition. Otherwise
3039 * compile the regexp program used to check for the condition. */
3040 if (n > 0)
3041 {
3042 buf[0] = '^'; /* always match at one position only */
3043 p = buf + 1;
3044 while (n-- > 0)
3045 *p++ = getc(fd); /* <condstr> */
3046 *p = NUL;
3047 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
3048 }
3049 }
3050 return 0;
3051}
3052
3053/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003054 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
Bram Moolenaar5195e452005-08-19 20:32:47 +00003055 * Return SP_*ERROR flags.
3056 */
3057 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00003058read_rep_section(fd, gap, first)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003059 FILE *fd;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003060 garray_T *gap;
3061 short *first;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003062{
3063 int cnt;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003064 fromto_T *ftp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003065 int i;
3066
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003067 cnt = get2c(fd); /* <repcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003068 if (cnt < 0)
3069 return SP_TRUNCERROR;
3070
Bram Moolenaar5195e452005-08-19 20:32:47 +00003071 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003072 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003073
3074 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3075 for (; gap->ga_len < cnt; ++gap->ga_len)
3076 {
3077 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
3078 ftp->ft_from = read_cnt_string(fd, 1, &i);
3079 if (i < 0)
3080 return i;
3081 if (i == 0)
3082 return SP_FORMERROR;
3083 ftp->ft_to = read_cnt_string(fd, 1, &i);
3084 if (i <= 0)
3085 {
3086 vim_free(ftp->ft_from);
3087 if (i < 0)
3088 return i;
3089 return SP_FORMERROR;
3090 }
3091 }
3092
3093 /* Fill the first-index table. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003094 for (i = 0; i < 256; ++i)
3095 first[i] = -1;
3096 for (i = 0; i < gap->ga_len; ++i)
3097 {
3098 ftp = &((fromto_T *)gap->ga_data)[i];
3099 if (first[*ftp->ft_from] == -1)
3100 first[*ftp->ft_from] = i;
3101 }
3102 return 0;
3103}
3104
3105/*
3106 * Read SN_SAL section: <salflags> <salcount> <sal> ...
3107 * Return SP_*ERROR flags.
3108 */
3109 static int
3110read_sal_section(fd, slang)
3111 FILE *fd;
3112 slang_T *slang;
3113{
3114 int i;
3115 int cnt;
3116 garray_T *gap;
3117 salitem_T *smp;
3118 int ccnt;
3119 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003120 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003121
3122 slang->sl_sofo = FALSE;
3123
3124 i = getc(fd); /* <salflags> */
3125 if (i & SAL_F0LLOWUP)
3126 slang->sl_followup = TRUE;
3127 if (i & SAL_COLLAPSE)
3128 slang->sl_collapse = TRUE;
3129 if (i & SAL_REM_ACCENTS)
3130 slang->sl_rem_accents = TRUE;
3131
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003132 cnt = get2c(fd); /* <salcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003133 if (cnt < 0)
3134 return SP_TRUNCERROR;
3135
3136 gap = &slang->sl_sal;
3137 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003138 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003139 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003140
3141 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3142 for (; gap->ga_len < cnt; ++gap->ga_len)
3143 {
3144 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3145 ccnt = getc(fd); /* <salfromlen> */
3146 if (ccnt < 0)
3147 return SP_TRUNCERROR;
3148 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003149 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003150 smp->sm_lead = p;
3151
3152 /* Read up to the first special char into sm_lead. */
3153 for (i = 0; i < ccnt; ++i)
3154 {
3155 c = getc(fd); /* <salfrom> */
3156 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
3157 break;
3158 *p++ = c;
3159 }
3160 smp->sm_leadlen = p - smp->sm_lead;
3161 *p++ = NUL;
3162
3163 /* Put (abc) chars in sm_oneof, if any. */
3164 if (c == '(')
3165 {
3166 smp->sm_oneof = p;
3167 for (++i; i < ccnt; ++i)
3168 {
3169 c = getc(fd); /* <salfrom> */
3170 if (c == ')')
3171 break;
3172 *p++ = c;
3173 }
3174 *p++ = NUL;
3175 if (++i < ccnt)
3176 c = getc(fd);
3177 }
3178 else
3179 smp->sm_oneof = NULL;
3180
3181 /* Any following chars go in sm_rules. */
3182 smp->sm_rules = p;
3183 if (i < ccnt)
3184 /* store the char we got while checking for end of sm_lead */
3185 *p++ = c;
3186 for (++i; i < ccnt; ++i)
3187 *p++ = getc(fd); /* <salfrom> */
3188 *p++ = NUL;
3189
3190 /* <saltolen> <salto> */
3191 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
3192 if (ccnt < 0)
3193 {
3194 vim_free(smp->sm_lead);
3195 return ccnt;
3196 }
3197
3198#ifdef FEAT_MBYTE
3199 if (has_mbyte)
3200 {
3201 /* convert the multi-byte strings to wide char strings */
3202 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3203 smp->sm_leadlen = mb_charlen(smp->sm_lead);
3204 if (smp->sm_oneof == NULL)
3205 smp->sm_oneof_w = NULL;
3206 else
3207 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
3208 if (smp->sm_to == NULL)
3209 smp->sm_to_w = NULL;
3210 else
3211 smp->sm_to_w = mb_str2wide(smp->sm_to);
3212 if (smp->sm_lead_w == NULL
3213 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
3214 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
3215 {
3216 vim_free(smp->sm_lead);
3217 vim_free(smp->sm_to);
3218 vim_free(smp->sm_lead_w);
3219 vim_free(smp->sm_oneof_w);
3220 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003221 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003222 }
3223 }
3224#endif
3225 }
3226
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003227 if (gap->ga_len > 0)
3228 {
3229 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
3230 * that we need to check the index every time. */
3231 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3232 if ((p = alloc(1)) == NULL)
3233 return SP_OTHERERROR;
3234 p[0] = NUL;
3235 smp->sm_lead = p;
3236 smp->sm_leadlen = 0;
3237 smp->sm_oneof = NULL;
3238 smp->sm_rules = p;
3239 smp->sm_to = NULL;
3240#ifdef FEAT_MBYTE
3241 if (has_mbyte)
3242 {
3243 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3244 smp->sm_leadlen = 0;
3245 smp->sm_oneof_w = NULL;
3246 smp->sm_to_w = NULL;
3247 }
3248#endif
3249 ++gap->ga_len;
3250 }
3251
Bram Moolenaar5195e452005-08-19 20:32:47 +00003252 /* Fill the first-index table. */
3253 set_sal_first(slang);
3254
3255 return 0;
3256}
3257
3258/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003259 * Read SN_WORDS: <word> ...
3260 * Return SP_*ERROR flags.
3261 */
3262 static int
3263read_words_section(fd, lp, len)
3264 FILE *fd;
3265 slang_T *lp;
3266 int len;
3267{
3268 int done = 0;
3269 int i;
3270 char_u word[MAXWLEN];
3271
3272 while (done < len)
3273 {
3274 /* Read one word at a time. */
3275 for (i = 0; ; ++i)
3276 {
3277 word[i] = getc(fd);
3278 if (word[i] == NUL)
3279 break;
3280 if (i == MAXWLEN - 1)
3281 return SP_FORMERROR;
3282 }
3283
3284 /* Init the count to 10. */
3285 count_common_word(lp, word, -1, 10);
3286 done += i + 1;
3287 }
3288 return 0;
3289}
3290
3291/*
3292 * Add a word to the hashtable of common words.
3293 * If it's already there then the counter is increased.
3294 */
3295 static void
3296count_common_word(lp, word, len, count)
3297 slang_T *lp;
3298 char_u *word;
3299 int len; /* word length, -1 for upto NUL */
3300 int count; /* 1 to count once, 10 to init */
3301{
3302 hash_T hash;
3303 hashitem_T *hi;
3304 wordcount_T *wc;
3305 char_u buf[MAXWLEN];
3306 char_u *p;
3307
3308 if (len == -1)
3309 p = word;
3310 else
3311 {
3312 vim_strncpy(buf, word, len);
3313 p = buf;
3314 }
3315
3316 hash = hash_hash(p);
3317 hi = hash_lookup(&lp->sl_wordcount, p, hash);
3318 if (HASHITEM_EMPTY(hi))
3319 {
3320 wc = (wordcount_T *)alloc(sizeof(wordcount_T) + STRLEN(p));
3321 if (wc == NULL)
3322 return;
3323 STRCPY(wc->wc_word, p);
3324 wc->wc_count = count;
3325 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
3326 }
3327 else
3328 {
3329 wc = HI2WC(hi);
3330 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
3331 wc->wc_count = MAXWORDCOUNT;
3332 }
3333}
3334
3335/*
3336 * Adjust the score of common words.
3337 */
3338 static int
3339score_wordcount_adj(slang, score, word, split)
3340 slang_T *slang;
3341 int score;
3342 char_u *word;
3343 int split; /* word was split, less bonus */
3344{
3345 hashitem_T *hi;
3346 wordcount_T *wc;
3347 int bonus;
3348 int newscore;
3349
3350 hi = hash_find(&slang->sl_wordcount, word);
3351 if (!HASHITEM_EMPTY(hi))
3352 {
3353 wc = HI2WC(hi);
3354 if (wc->wc_count < SCORE_THRES2)
3355 bonus = SCORE_COMMON1;
3356 else if (wc->wc_count < SCORE_THRES3)
3357 bonus = SCORE_COMMON2;
3358 else
3359 bonus = SCORE_COMMON3;
3360 if (split)
3361 newscore = score - bonus / 2;
3362 else
3363 newscore = score - bonus;
3364 if (newscore < 0)
3365 return 0;
3366 return newscore;
3367 }
3368 return score;
3369}
3370
3371/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003372 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
3373 * Return SP_*ERROR flags.
3374 */
3375 static int
3376read_sofo_section(fd, slang)
3377 FILE *fd;
3378 slang_T *slang;
3379{
3380 int cnt;
3381 char_u *from, *to;
3382 int res;
3383
3384 slang->sl_sofo = TRUE;
3385
3386 /* <sofofromlen> <sofofrom> */
3387 from = read_cnt_string(fd, 2, &cnt);
3388 if (cnt < 0)
3389 return cnt;
3390
3391 /* <sofotolen> <sofoto> */
3392 to = read_cnt_string(fd, 2, &cnt);
3393 if (cnt < 0)
3394 {
3395 vim_free(from);
3396 return cnt;
3397 }
3398
3399 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
3400 if (from != NULL && to != NULL)
3401 res = set_sofo(slang, from, to);
3402 else if (from != NULL || to != NULL)
3403 res = SP_FORMERROR; /* only one of two strings is an error */
3404 else
3405 res = 0;
3406
3407 vim_free(from);
3408 vim_free(to);
3409 return res;
3410}
3411
3412/*
3413 * Read the compound section from the .spl file:
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003414 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +00003415 * Returns SP_*ERROR flags.
3416 */
3417 static int
3418read_compound(fd, slang, len)
3419 FILE *fd;
3420 slang_T *slang;
3421 int len;
3422{
3423 int todo = len;
3424 int c;
3425 int atstart;
3426 char_u *pat;
3427 char_u *pp;
3428 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003429 char_u *ap;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003430 int cnt;
3431 garray_T *gap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003432
3433 if (todo < 2)
3434 return SP_FORMERROR; /* need at least two bytes */
3435
3436 --todo;
3437 c = getc(fd); /* <compmax> */
3438 if (c < 2)
3439 c = MAXWLEN;
3440 slang->sl_compmax = c;
3441
3442 --todo;
3443 c = getc(fd); /* <compminlen> */
3444 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003445 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003446 slang->sl_compminlen = c;
3447
3448 --todo;
3449 c = getc(fd); /* <compsylmax> */
3450 if (c < 1)
3451 c = MAXWLEN;
3452 slang->sl_compsylmax = c;
3453
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003454 c = getc(fd); /* <compoptions> */
3455 if (c != 0)
3456 ungetc(c, fd); /* be backwards compatible with Vim 7.0b */
3457 else
3458 {
3459 --todo;
3460 c = getc(fd); /* only use the lower byte for now */
3461 --todo;
3462 slang->sl_compoptions = c;
3463
3464 gap = &slang->sl_comppat;
3465 c = get2c(fd); /* <comppatcount> */
3466 todo -= 2;
3467 ga_init2(gap, sizeof(char_u *), c);
3468 if (ga_grow(gap, c) == OK)
3469 while (--c >= 0)
3470 {
3471 ((char_u **)(gap->ga_data))[gap->ga_len++] =
3472 read_cnt_string(fd, 1, &cnt);
3473 /* <comppatlen> <comppattext> */
3474 if (cnt < 0)
3475 return cnt;
3476 todo -= cnt + 2;
3477 }
3478 }
3479
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003480 /* Turn the COMPOUNDRULE items into a regexp pattern:
Bram Moolenaar5195e452005-08-19 20:32:47 +00003481 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003482 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3483 * Conversion to utf-8 may double the size. */
3484 c = todo * 2 + 7;
3485#ifdef FEAT_MBYTE
3486 if (enc_utf8)
3487 c += todo * 2;
3488#endif
3489 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003490 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003491 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003492
Bram Moolenaard12a1322005-08-21 22:08:24 +00003493 /* We also need a list of all flags that can appear at the start and one
3494 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003495 cp = alloc(todo + 1);
3496 if (cp == NULL)
3497 {
3498 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003499 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003500 }
3501 slang->sl_compstartflags = cp;
3502 *cp = NUL;
3503
Bram Moolenaard12a1322005-08-21 22:08:24 +00003504 ap = alloc(todo + 1);
3505 if (ap == NULL)
3506 {
3507 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003508 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003509 }
3510 slang->sl_compallflags = ap;
3511 *ap = NUL;
3512
Bram Moolenaar5195e452005-08-19 20:32:47 +00003513 pp = pat;
3514 *pp++ = '^';
3515 *pp++ = '\\';
3516 *pp++ = '(';
3517
3518 atstart = 1;
3519 while (todo-- > 0)
3520 {
3521 c = getc(fd); /* <compflags> */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003522
3523 /* Add all flags to "sl_compallflags". */
3524 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003525 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003526 {
3527 *ap++ = c;
3528 *ap = NUL;
3529 }
3530
Bram Moolenaar5195e452005-08-19 20:32:47 +00003531 if (atstart != 0)
3532 {
3533 /* At start of item: copy flags to "sl_compstartflags". For a
3534 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3535 if (c == '[')
3536 atstart = 2;
3537 else if (c == ']')
3538 atstart = 0;
3539 else
3540 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003541 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003542 {
3543 *cp++ = c;
3544 *cp = NUL;
3545 }
3546 if (atstart == 1)
3547 atstart = 0;
3548 }
3549 }
3550 if (c == '/') /* slash separates two items */
3551 {
3552 *pp++ = '\\';
3553 *pp++ = '|';
3554 atstart = 1;
3555 }
3556 else /* normal char, "[abc]" and '*' are copied as-is */
3557 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003558 if (c == '+' || c == '~')
Bram Moolenaar5195e452005-08-19 20:32:47 +00003559 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003560#ifdef FEAT_MBYTE
3561 if (enc_utf8)
3562 pp += mb_char2bytes(c, pp);
3563 else
3564#endif
3565 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003566 }
3567 }
3568
3569 *pp++ = '\\';
3570 *pp++ = ')';
3571 *pp++ = '$';
3572 *pp = NUL;
3573
3574 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3575 vim_free(pat);
3576 if (slang->sl_compprog == NULL)
3577 return SP_FORMERROR;
3578
3579 return 0;
3580}
3581
Bram Moolenaar6de68532005-08-24 22:08:48 +00003582/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003583 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003584 * Like strchr() but independent of locale.
3585 */
3586 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003587byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003588 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003589 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003590{
3591 char_u *p;
3592
3593 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003594 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003595 return TRUE;
3596 return FALSE;
3597}
3598
Bram Moolenaar5195e452005-08-19 20:32:47 +00003599#define SY_MAXLEN 30
3600typedef struct syl_item_S
3601{
3602 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3603 int sy_len;
3604} syl_item_T;
3605
3606/*
3607 * Truncate "slang->sl_syllable" at the first slash and put the following items
3608 * in "slang->sl_syl_items".
3609 */
3610 static int
3611init_syl_tab(slang)
3612 slang_T *slang;
3613{
3614 char_u *p;
3615 char_u *s;
3616 int l;
3617 syl_item_T *syl;
3618
3619 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3620 p = vim_strchr(slang->sl_syllable, '/');
3621 while (p != NULL)
3622 {
3623 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003624 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003625 break;
3626 s = p;
3627 p = vim_strchr(p, '/');
3628 if (p == NULL)
3629 l = STRLEN(s);
3630 else
3631 l = p - s;
3632 if (l >= SY_MAXLEN)
3633 return SP_FORMERROR;
3634 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003635 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003636 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3637 + slang->sl_syl_items.ga_len++;
3638 vim_strncpy(syl->sy_chars, s, l);
3639 syl->sy_len = l;
3640 }
3641 return OK;
3642}
3643
3644/*
3645 * Count the number of syllables in "word".
3646 * When "word" contains spaces the syllables after the last space are counted.
3647 * Returns zero if syllables are not defines.
3648 */
3649 static int
3650count_syllables(slang, word)
3651 slang_T *slang;
3652 char_u *word;
3653{
3654 int cnt = 0;
3655 int skip = FALSE;
3656 char_u *p;
3657 int len;
3658 int i;
3659 syl_item_T *syl;
3660 int c;
3661
3662 if (slang->sl_syllable == NULL)
3663 return 0;
3664
3665 for (p = word; *p != NUL; p += len)
3666 {
3667 /* When running into a space reset counter. */
3668 if (*p == ' ')
3669 {
3670 len = 1;
3671 cnt = 0;
3672 continue;
3673 }
3674
3675 /* Find longest match of syllable items. */
3676 len = 0;
3677 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3678 {
3679 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3680 if (syl->sy_len > len
3681 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3682 len = syl->sy_len;
3683 }
3684 if (len != 0) /* found a match, count syllable */
3685 {
3686 ++cnt;
3687 skip = FALSE;
3688 }
3689 else
3690 {
3691 /* No recognized syllable item, at least a syllable char then? */
3692#ifdef FEAT_MBYTE
3693 c = mb_ptr2char(p);
3694 len = (*mb_ptr2len)(p);
3695#else
3696 c = *p;
3697 len = 1;
3698#endif
3699 if (vim_strchr(slang->sl_syllable, c) == NULL)
3700 skip = FALSE; /* No, search for next syllable */
3701 else if (!skip)
3702 {
3703 ++cnt; /* Yes, count it */
3704 skip = TRUE; /* don't count following syllable chars */
3705 }
3706 }
3707 }
3708 return cnt;
3709}
3710
3711/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003712 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003713 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003714 */
3715 static int
3716set_sofo(lp, from, to)
3717 slang_T *lp;
3718 char_u *from;
3719 char_u *to;
3720{
3721 int i;
3722
3723#ifdef FEAT_MBYTE
3724 garray_T *gap;
3725 char_u *s;
3726 char_u *p;
3727 int c;
3728 int *inp;
3729
3730 if (has_mbyte)
3731 {
3732 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3733 * characters. The index is the low byte of the character.
3734 * The list contains from-to pairs with a terminating NUL.
3735 * sl_sal_first[] is used for latin1 "from" characters. */
3736 gap = &lp->sl_sal;
3737 ga_init2(gap, sizeof(int *), 1);
3738 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003739 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003740 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3741 gap->ga_len = 256;
3742
3743 /* First count the number of items for each list. Temporarily use
3744 * sl_sal_first[] for this. */
3745 for (p = from, s = to; *p != NUL && *s != NUL; )
3746 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003747 c = mb_cptr2char_adv(&p);
3748 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003749 if (c >= 256)
3750 ++lp->sl_sal_first[c & 0xff];
3751 }
3752 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003753 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003754
3755 /* Allocate the lists. */
3756 for (i = 0; i < 256; ++i)
3757 if (lp->sl_sal_first[i] > 0)
3758 {
3759 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3760 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003761 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003762 ((int **)gap->ga_data)[i] = (int *)p;
3763 *(int *)p = 0;
3764 }
3765
3766 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3767 * list. */
3768 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3769 for (p = from, s = to; *p != NUL && *s != NUL; )
3770 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003771 c = mb_cptr2char_adv(&p);
3772 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003773 if (c >= 256)
3774 {
3775 /* Append the from-to chars at the end of the list with
3776 * the low byte. */
3777 inp = ((int **)gap->ga_data)[c & 0xff];
3778 while (*inp != 0)
3779 ++inp;
3780 *inp++ = c; /* from char */
3781 *inp++ = i; /* to char */
3782 *inp++ = NUL; /* NUL at the end */
3783 }
3784 else
3785 /* mapping byte to char is done in sl_sal_first[] */
3786 lp->sl_sal_first[c] = i;
3787 }
3788 }
3789 else
3790#endif
3791 {
3792 /* mapping bytes to bytes is done in sl_sal_first[] */
3793 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003794 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003795
3796 for (i = 0; to[i] != NUL; ++i)
3797 lp->sl_sal_first[from[i]] = to[i];
3798 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3799 }
3800
Bram Moolenaar5195e452005-08-19 20:32:47 +00003801 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003802}
3803
3804/*
3805 * Fill the first-index table for "lp".
3806 */
3807 static void
3808set_sal_first(lp)
3809 slang_T *lp;
3810{
3811 salfirst_T *sfirst;
3812 int i;
3813 salitem_T *smp;
3814 int c;
3815 garray_T *gap = &lp->sl_sal;
3816
3817 sfirst = lp->sl_sal_first;
3818 for (i = 0; i < 256; ++i)
3819 sfirst[i] = -1;
3820 smp = (salitem_T *)gap->ga_data;
3821 for (i = 0; i < gap->ga_len; ++i)
3822 {
3823#ifdef FEAT_MBYTE
3824 if (has_mbyte)
3825 /* Use the lowest byte of the first character. For latin1 it's
3826 * the character, for other encodings it should differ for most
3827 * characters. */
3828 c = *smp[i].sm_lead_w & 0xff;
3829 else
3830#endif
3831 c = *smp[i].sm_lead;
3832 if (sfirst[c] == -1)
3833 {
3834 sfirst[c] = i;
3835#ifdef FEAT_MBYTE
3836 if (has_mbyte)
3837 {
3838 int n;
3839
3840 /* Make sure all entries with this byte are following each
3841 * other. Move the ones that are in the wrong position. Do
3842 * keep the same ordering! */
3843 while (i + 1 < gap->ga_len
3844 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3845 /* Skip over entry with same index byte. */
3846 ++i;
3847
3848 for (n = 1; i + n < gap->ga_len; ++n)
3849 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3850 {
3851 salitem_T tsal;
3852
3853 /* Move entry with same index byte after the entries
3854 * we already found. */
3855 ++i;
3856 --n;
3857 tsal = smp[i + n];
3858 mch_memmove(smp + i + 1, smp + i,
3859 sizeof(salitem_T) * n);
3860 smp[i] = tsal;
3861 }
3862 }
3863#endif
3864 }
3865 }
3866}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003867
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003868#ifdef FEAT_MBYTE
3869/*
3870 * Turn a multi-byte string into a wide character string.
3871 * Return it in allocated memory (NULL for out-of-memory)
3872 */
3873 static int *
3874mb_str2wide(s)
3875 char_u *s;
3876{
3877 int *res;
3878 char_u *p;
3879 int i = 0;
3880
3881 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3882 if (res != NULL)
3883 {
3884 for (p = s; *p != NUL; )
3885 res[i++] = mb_ptr2char_adv(&p);
3886 res[i] = NUL;
3887 }
3888 return res;
3889}
3890#endif
3891
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003892/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003893 * Read a tree from the .spl or .sug file.
3894 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
3895 * This is skipped when the tree has zero length.
3896 * Returns zero when OK, SP_ value for an error.
3897 */
3898 static int
3899spell_read_tree(fd, bytsp, idxsp, prefixtree, prefixcnt)
3900 FILE *fd;
3901 char_u **bytsp;
3902 idx_T **idxsp;
3903 int prefixtree; /* TRUE for the prefix tree */
3904 int prefixcnt; /* when "prefixtree" is TRUE: prefix count */
3905{
3906 int len;
3907 int idx;
3908 char_u *bp;
3909 idx_T *ip;
3910
3911 /* The tree size was computed when writing the file, so that we can
3912 * allocate it as one long block. <nodecount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003913 len = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003914 if (len < 0)
3915 return SP_TRUNCERROR;
3916 if (len > 0)
3917 {
3918 /* Allocate the byte array. */
3919 bp = lalloc((long_u)len, TRUE);
3920 if (bp == NULL)
3921 return SP_OTHERERROR;
3922 *bytsp = bp;
3923
3924 /* Allocate the index array. */
3925 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
3926 if (ip == NULL)
3927 return SP_OTHERERROR;
3928 *idxsp = ip;
3929
3930 /* Recursively read the tree and store it in the array. */
3931 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
3932 if (idx < 0)
3933 return idx;
3934 }
3935 return 0;
3936}
3937
3938/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003939 * Read one row of siblings from the spell file and store it in the byte array
3940 * "byts" and index array "idxs". Recursively read the children.
3941 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003942 * NOTE: The code here must match put_node()!
Bram Moolenaar51485f02005-06-04 21:55:20 +00003943 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003944 * Returns the index (>= 0) following the siblings.
3945 * Returns SP_TRUNCERROR if the file is shorter than expected.
3946 * Returns SP_FORMERROR if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003947 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003948 static idx_T
Bram Moolenaar4770d092006-01-12 23:22:24 +00003949read_tree_node(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003950 FILE *fd;
3951 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003952 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003953 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003954 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003955 int prefixtree; /* TRUE for reading PREFIXTREE */
3956 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003957{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003958 int len;
3959 int i;
3960 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003961 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003962 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003963 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003964#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003965
Bram Moolenaar51485f02005-06-04 21:55:20 +00003966 len = getc(fd); /* <siblingcount> */
3967 if (len <= 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003968 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003969
3970 if (startidx + len >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003971 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003972 byts[idx++] = len;
3973
3974 /* Read the byte values, flag/region bytes and shared indexes. */
3975 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003976 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003977 c = getc(fd); /* <byte> */
3978 if (c < 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003979 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003980 if (c <= BY_SPECIAL)
3981 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003982 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003983 {
3984 /* No flags, all regions. */
3985 idxs[idx] = 0;
3986 c = 0;
3987 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003988 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003989 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003990 if (prefixtree)
3991 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003992 /* Read the optional pflags byte, the prefix ID and the
3993 * condition nr. In idxs[] store the prefix ID in the low
3994 * byte, the condition index shifted up 8 bits, the flags
3995 * shifted up 24 bits. */
3996 if (c == BY_FLAGS)
3997 c = getc(fd) << 24; /* <pflags> */
3998 else
3999 c = 0;
4000
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004001 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004002
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004003 n = get2c(fd); /* <prefcondnr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004004 if (n >= maxprefcondnr)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004005 return SP_FORMERROR;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004006 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004007 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004008 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004009 {
4010 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004011 * idxs[] the flags go in the low two bytes, region above
4012 * that and prefix ID above the region. */
4013 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004014 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004015 if (c2 == BY_FLAGS2)
4016 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004017 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004018 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004019 if (c & WF_AFX)
4020 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004021 }
4022
Bram Moolenaar51485f02005-06-04 21:55:20 +00004023 idxs[idx] = c;
4024 c = 0;
4025 }
4026 else /* c == BY_INDEX */
4027 {
4028 /* <nodeidx> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004029 n = get3c(fd);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004030 if (n < 0 || n >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004031 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004032 idxs[idx] = n + SHARED_MASK;
4033 c = getc(fd); /* <xbyte> */
4034 }
4035 }
4036 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004037 }
4038
Bram Moolenaar51485f02005-06-04 21:55:20 +00004039 /* Recursively read the children for non-shared siblings.
4040 * Skip the end-of-word ones (zero byte value) and the shared ones (and
4041 * remove SHARED_MASK) */
4042 for (i = 1; i <= len; ++i)
4043 if (byts[startidx + i] != 0)
4044 {
4045 if (idxs[startidx + i] & SHARED_MASK)
4046 idxs[startidx + i] &= ~SHARED_MASK;
4047 else
4048 {
4049 idxs[startidx + i] = idx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004050 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004051 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004052 if (idx < 0)
4053 break;
4054 }
4055 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004056
Bram Moolenaar51485f02005-06-04 21:55:20 +00004057 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004058}
4059
4060/*
4061 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004062 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004063 */
4064 char_u *
4065did_set_spelllang(buf)
4066 buf_T *buf;
4067{
4068 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004069 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004070 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00004071 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004072 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004073 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004074 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004075 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004076 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004077 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004078 int len;
4079 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004080 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004081 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004082 char_u *use_region = NULL;
4083 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004084 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004085 int i, j;
4086 langp_T *lp, *lp2;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004087
4088 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004089 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004090
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004091 /* loop over comma separated language names. */
4092 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004093 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004094 /* Get one language name. */
4095 copy_option_part(&splp, lang, MAXWLEN, ",");
4096
Bram Moolenaar5482f332005-04-17 20:18:43 +00004097 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004098 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004099
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004100 /* If the name ends in ".spl" use it as the name of the spell file.
4101 * If there is a region name let "region" point to it and remove it
4102 * from the name. */
4103 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
4104 {
4105 filename = TRUE;
4106
Bram Moolenaarb6356332005-07-18 21:40:44 +00004107 /* Locate a region and remove it from the file name. */
4108 p = vim_strchr(gettail(lang), '_');
4109 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
4110 && !ASCII_ISALPHA(p[3]))
4111 {
4112 vim_strncpy(region_cp, p + 1, 2);
4113 mch_memmove(p, p + 3, len - (p - lang) - 2);
4114 len -= 3;
4115 region = region_cp;
4116 }
4117 else
4118 dont_use_region = TRUE;
4119
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004120 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004121 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4122 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004123 break;
4124 }
4125 else
4126 {
4127 filename = FALSE;
4128 if (len > 3 && lang[len - 3] == '_')
4129 {
4130 region = lang + len - 2;
4131 len -= 3;
4132 lang[len] = NUL;
4133 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004134 else
4135 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004136
4137 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004138 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4139 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004140 break;
4141 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004142
Bram Moolenaarb6356332005-07-18 21:40:44 +00004143 if (region != NULL)
4144 {
4145 /* If the region differs from what was used before then don't
4146 * use it for 'spellfile'. */
4147 if (use_region != NULL && STRCMP(region, use_region) != 0)
4148 dont_use_region = TRUE;
4149 use_region = region;
4150 }
4151
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004152 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004153 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004154 {
4155 if (filename)
4156 (void)spell_load_file(lang, lang, NULL, FALSE);
4157 else
4158 spell_load_lang(lang);
4159 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004160
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004161 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004162 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004163 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004164 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4165 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4166 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004167 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004168 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004169 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004170 {
4171 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004172 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004173 if (c == REGION_ALL)
4174 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004175 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004176 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004177 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004178 /* This addition file is for other regions. */
4179 region_mask = 0;
4180 }
4181 else
4182 /* This is probably an error. Give a warning and
4183 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004184 smsg((char_u *)
4185 _("Warning: region %s not supported"),
4186 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004187 }
4188 else
4189 region_mask = 1 << c;
4190 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004191
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004192 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004193 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004194 if (ga_grow(&ga, 1) == FAIL)
4195 {
4196 ga_clear(&ga);
4197 return e_outofmem;
4198 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004199 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004200 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4201 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004202 use_midword(slang, buf);
4203 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004204 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004205 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004206 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004207 }
4208
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004209 /* round 0: load int_wordlist, if possible.
4210 * round 1: load first name in 'spellfile'.
4211 * round 2: load second name in 'spellfile.
4212 * etc. */
4213 spf = curbuf->b_p_spf;
4214 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004215 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004216 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004217 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004218 /* Internal wordlist, if there is one. */
4219 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004220 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004221 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004222 }
4223 else
4224 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004225 /* One entry in 'spellfile'. */
4226 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4227 STRCAT(spf_name, ".spl");
4228
4229 /* If it was already found above then skip it. */
4230 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004231 {
4232 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4233 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004234 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004235 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004236 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004237 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004238 }
4239
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004240 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004241 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4242 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004244 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004245 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004246 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004247 * region name, the region is ignored otherwise. for int_wordlist
4248 * use an arbitrary name. */
4249 if (round == 0)
4250 STRCPY(lang, "internal wordlist");
4251 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00004252 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004253 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004254 p = vim_strchr(lang, '.');
4255 if (p != NULL)
4256 *p = NUL; /* truncate at ".encoding.add" */
4257 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004258 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004259
4260 /* If one of the languages has NOBREAK we assume the addition
4261 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004262 if (slang != NULL && nobreak)
4263 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004264 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004265 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004266 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004267 region_mask = REGION_ALL;
4268 if (use_region != NULL && !dont_use_region)
4269 {
4270 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004271 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004272 if (c != REGION_ALL)
4273 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004274 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004275 /* This spell file is for other regions. */
4276 region_mask = 0;
4277 }
4278
4279 if (region_mask != 0)
4280 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004281 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4282 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4283 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004284 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4285 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004286 use_midword(slang, buf);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004287 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004288 }
4289 }
4290
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004291 /* Everything is fine, store the new b_langp value. */
4292 ga_clear(&buf->b_langp);
4293 buf->b_langp = ga;
4294
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004295 /* For each language figure out what language to use for sound folding and
4296 * REP items. If the language doesn't support it itself use another one
4297 * with the same name. E.g. for "en-math" use "en". */
4298 for (i = 0; i < ga.ga_len; ++i)
4299 {
4300 lp = LANGP_ENTRY(ga, i);
4301
4302 /* sound folding */
4303 if (lp->lp_slang->sl_sal.ga_len > 0)
4304 /* language does sound folding itself */
4305 lp->lp_sallang = lp->lp_slang;
4306 else
4307 /* find first similar language that does sound folding */
4308 for (j = 0; j < ga.ga_len; ++j)
4309 {
4310 lp2 = LANGP_ENTRY(ga, j);
4311 if (lp2->lp_slang->sl_sal.ga_len > 0
4312 && STRNCMP(lp->lp_slang->sl_name,
4313 lp2->lp_slang->sl_name, 2) == 0)
4314 {
4315 lp->lp_sallang = lp2->lp_slang;
4316 break;
4317 }
4318 }
4319
4320 /* REP items */
4321 if (lp->lp_slang->sl_rep.ga_len > 0)
4322 /* language has REP items itself */
4323 lp->lp_replang = lp->lp_slang;
4324 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004325 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004326 for (j = 0; j < ga.ga_len; ++j)
4327 {
4328 lp2 = LANGP_ENTRY(ga, j);
4329 if (lp2->lp_slang->sl_rep.ga_len > 0
4330 && STRNCMP(lp->lp_slang->sl_name,
4331 lp2->lp_slang->sl_name, 2) == 0)
4332 {
4333 lp->lp_replang = lp2->lp_slang;
4334 break;
4335 }
4336 }
4337 }
4338
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004339 return NULL;
4340}
4341
4342/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004343 * Clear the midword characters for buffer "buf".
4344 */
4345 static void
4346clear_midword(buf)
4347 buf_T *buf;
4348{
4349 vim_memset(buf->b_spell_ismw, 0, 256);
4350#ifdef FEAT_MBYTE
4351 vim_free(buf->b_spell_ismw_mb);
4352 buf->b_spell_ismw_mb = NULL;
4353#endif
4354}
4355
4356/*
4357 * Use the "sl_midword" field of language "lp" for buffer "buf".
4358 * They add up to any currently used midword characters.
4359 */
4360 static void
4361use_midword(lp, buf)
4362 slang_T *lp;
4363 buf_T *buf;
4364{
4365 char_u *p;
4366
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004367 if (lp->sl_midword == NULL) /* there aren't any */
4368 return;
4369
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004370 for (p = lp->sl_midword; *p != NUL; )
4371#ifdef FEAT_MBYTE
4372 if (has_mbyte)
4373 {
4374 int c, l, n;
4375 char_u *bp;
4376
4377 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004378 l = (*mb_ptr2len)(p);
4379 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004380 buf->b_spell_ismw[c] = TRUE;
4381 else if (buf->b_spell_ismw_mb == NULL)
4382 /* First multi-byte char in "b_spell_ismw_mb". */
4383 buf->b_spell_ismw_mb = vim_strnsave(p, l);
4384 else
4385 {
4386 /* Append multi-byte chars to "b_spell_ismw_mb". */
4387 n = STRLEN(buf->b_spell_ismw_mb);
4388 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
4389 if (bp != NULL)
4390 {
4391 vim_free(buf->b_spell_ismw_mb);
4392 buf->b_spell_ismw_mb = bp;
4393 vim_strncpy(bp + n, p, l);
4394 }
4395 }
4396 p += l;
4397 }
4398 else
4399#endif
4400 buf->b_spell_ismw[*p++] = TRUE;
4401}
4402
4403/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004404 * Find the region "region[2]" in "rp" (points to "sl_regions").
4405 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00004406 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004407 */
4408 static int
4409find_region(rp, region)
4410 char_u *rp;
4411 char_u *region;
4412{
4413 int i;
4414
4415 for (i = 0; ; i += 2)
4416 {
4417 if (rp[i] == NUL)
4418 return REGION_ALL;
4419 if (rp[i] == region[0] && rp[i + 1] == region[1])
4420 break;
4421 }
4422 return i / 2;
4423}
4424
4425/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004426 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004427 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00004428 * Word WF_ONECAP
4429 * W WORD WF_ALLCAP
4430 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004431 */
4432 static int
4433captype(word, end)
4434 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004435 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004436{
4437 char_u *p;
4438 int c;
4439 int firstcap;
4440 int allcap;
4441 int past_second = FALSE; /* past second word char */
4442
4443 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004444 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004445 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004446 return 0; /* only non-word characters, illegal word */
4447#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004448 if (has_mbyte)
4449 c = mb_ptr2char_adv(&p);
4450 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004451#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004452 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004453 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004454
4455 /*
4456 * Need to check all letters to find a word with mixed upper/lower.
4457 * But a word with an upper char only at start is a ONECAP.
4458 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004459 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004460 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004461 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004462 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004463 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004464 {
4465 /* UUl -> KEEPCAP */
4466 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004467 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004468 allcap = FALSE;
4469 }
4470 else if (!allcap)
4471 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004472 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004473 past_second = TRUE;
4474 }
4475
4476 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004477 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004478 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004479 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004480 return 0;
4481}
4482
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004483/*
4484 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4485 * capital. So that make_case_word() can turn WOrd into Word.
4486 * Add ALLCAP for "WOrD".
4487 */
4488 static int
4489badword_captype(word, end)
4490 char_u *word;
4491 char_u *end;
4492{
4493 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004494 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004495 int l, u;
4496 int first;
4497 char_u *p;
4498
4499 if (flags & WF_KEEPCAP)
4500 {
4501 /* Count the number of UPPER and lower case letters. */
4502 l = u = 0;
4503 first = FALSE;
4504 for (p = word; p < end; mb_ptr_adv(p))
4505 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004506 c = PTR2CHAR(p);
4507 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004508 {
4509 ++u;
4510 if (p == word)
4511 first = TRUE;
4512 }
4513 else
4514 ++l;
4515 }
4516
4517 /* If there are more UPPER than lower case letters suggest an
4518 * ALLCAP word. Otherwise, if the first letter is UPPER then
4519 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4520 * require three upper case letters. */
4521 if (u > l && u > 2)
4522 flags |= WF_ALLCAP;
4523 else if (first)
4524 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004525
4526 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4527 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004528 }
4529 return flags;
4530}
4531
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004532# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4533/*
4534 * Free all languages.
4535 */
4536 void
4537spell_free_all()
4538{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004539 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004540 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004541 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004542
4543 /* Go through all buffers and handle 'spelllang'. */
4544 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4545 ga_clear(&buf->b_langp);
4546
4547 while (first_lang != NULL)
4548 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004549 slang = first_lang;
4550 first_lang = slang->sl_next;
4551 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004552 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004553
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004554 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004555 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004556 /* Delete the internal wordlist and its .spl file */
4557 mch_remove(int_wordlist);
4558 int_wordlist_spl(fname);
4559 mch_remove(fname);
4560 vim_free(int_wordlist);
4561 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004562 }
4563
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004564 init_spell_chartab();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004565
4566 vim_free(repl_to);
4567 repl_to = NULL;
4568 vim_free(repl_from);
4569 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004570}
4571# endif
4572
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004573# if defined(FEAT_MBYTE) || defined(PROTO)
4574/*
4575 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004576 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004577 */
4578 void
4579spell_reload()
4580{
4581 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004582 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004583
Bram Moolenaarea408852005-06-25 22:49:46 +00004584 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004585 init_spell_chartab();
4586
4587 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004588 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004589
4590 /* Go through all buffers and handle 'spelllang'. */
4591 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4592 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004593 /* Only load the wordlists when 'spelllang' is set and there is a
4594 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004595 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004596 {
4597 FOR_ALL_WINDOWS(wp)
4598 if (wp->w_buffer == buf && wp->w_p_spell)
4599 {
4600 (void)did_set_spelllang(buf);
4601# ifdef FEAT_WINDOWS
4602 break;
4603# endif
4604 }
4605 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004606 }
4607}
4608# endif
4609
Bram Moolenaarb765d632005-06-07 21:00:02 +00004610/*
4611 * Reload the spell file "fname" if it's loaded.
4612 */
4613 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004614spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004615 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004616 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004617{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004618 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004619 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004620
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004621 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004622 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004623 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004624 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004625 slang_clear(slang);
4626 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004627 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004628 slang_clear(slang);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004629 redraw_all_later(SOME_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004630 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004631 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004632 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004633
4634 /* When "zg" was used and the file wasn't loaded yet, should redo
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004635 * 'spelllang' to load it now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004636 if (added_word && !didit)
4637 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004638}
4639
4640
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004641/*
4642 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004643 */
4644
Bram Moolenaar51485f02005-06-04 21:55:20 +00004645#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004646 and .dic file. */
4647/*
4648 * Main structure to store the contents of a ".aff" file.
4649 */
4650typedef struct afffile_S
4651{
4652 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004653 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004654 unsigned af_rare; /* RARE ID for rare word */
4655 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004656 unsigned af_bad; /* BAD ID for banned word */
4657 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004658 unsigned af_circumfix; /* CIRCUMFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004659 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004660 unsigned af_comproot; /* COMPOUNDROOT ID */
4661 unsigned af_compforbid; /* COMPOUNDFORBIDFLAG ID */
4662 unsigned af_comppermit; /* COMPOUNDPERMITFLAG ID */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004663 unsigned af_nosuggest; /* NOSUGGEST ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004664 int af_pfxpostpone; /* postpone prefixes without chop string and
4665 without flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004666 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4667 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004668 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004669} afffile_T;
4670
Bram Moolenaar6de68532005-08-24 22:08:48 +00004671#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004672#define AFT_LONG 1 /* flags are two characters */
4673#define AFT_CAPLONG 2 /* flags are one or two characters */
4674#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004675
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004676typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004677/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4678struct affentry_S
4679{
4680 affentry_T *ae_next; /* next affix with same name/number */
4681 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4682 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004683 char_u *ae_flags; /* flags on the affix (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004684 char_u *ae_cond; /* condition (NULL for ".") */
4685 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004686};
4687
Bram Moolenaar6de68532005-08-24 22:08:48 +00004688#ifdef FEAT_MBYTE
4689# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4690#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004691# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004692#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004693
Bram Moolenaar51485f02005-06-04 21:55:20 +00004694/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4695typedef struct affheader_S
4696{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004697 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4698 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4699 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004700 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004701 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004702 affentry_T *ah_first; /* first affix entry */
4703} affheader_T;
4704
4705#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4706
Bram Moolenaar6de68532005-08-24 22:08:48 +00004707/* Flag used in compound items. */
4708typedef struct compitem_S
4709{
4710 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4711 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4712 int ci_newID; /* affix ID after renumbering. */
4713} compitem_T;
4714
4715#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4716
Bram Moolenaar51485f02005-06-04 21:55:20 +00004717/*
4718 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004719 * the need to keep track of each allocated thing, everything is freed all at
4720 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004721 */
4722#define SBLOCKSIZE 16000 /* size of sb_data */
4723typedef struct sblock_S sblock_T;
4724struct sblock_S
4725{
4726 sblock_T *sb_next; /* next block in list */
4727 int sb_used; /* nr of bytes already in use */
4728 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004729};
4730
4731/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004732 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004733 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004734typedef struct wordnode_S wordnode_T;
4735struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004736{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004737 union /* shared to save space */
4738 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004739 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004740 int index; /* index in written nodes (valid after first
4741 round) */
4742 } wn_u1;
4743 union /* shared to save space */
4744 {
4745 wordnode_T *next; /* next node with same hash key */
4746 wordnode_T *wnode; /* parent node that will write this node */
4747 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004748 wordnode_T *wn_child; /* child (next byte in word) */
4749 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4750 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004751 int wn_refs; /* Nr. of references to this node. Only
4752 relevant for first node in a list of
4753 siblings, in following siblings it is
4754 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004755 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004756
4757 /* Info for when "wn_byte" is NUL.
4758 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4759 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4760 * "wn_region" the LSW of the wordnr. */
4761 char_u wn_affixID; /* supported/required prefix ID or 0 */
4762 short_u wn_flags; /* WF_ flags */
4763 short wn_region; /* region mask */
4764
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004765#ifdef SPELL_PRINTTREE
4766 int wn_nr; /* sequence nr for printing */
4767#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004768};
4769
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004770#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4771
Bram Moolenaar51485f02005-06-04 21:55:20 +00004772#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004773
Bram Moolenaar51485f02005-06-04 21:55:20 +00004774/*
4775 * Info used while reading the spell files.
4776 */
4777typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004778{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004779 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004780 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004781
Bram Moolenaar51485f02005-06-04 21:55:20 +00004782 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004783 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004784
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004785 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004786
Bram Moolenaar4770d092006-01-12 23:22:24 +00004787 long si_sugtree; /* creating the soundfolding trie */
4788
Bram Moolenaar51485f02005-06-04 21:55:20 +00004789 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004790 long si_blocks_cnt; /* memory blocks allocated */
4791 long si_compress_cnt; /* words to add before lowering
4792 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004793 wordnode_T *si_first_free; /* List of nodes that have been freed during
4794 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004795 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004796#ifdef SPELL_PRINTTREE
4797 int si_wordnode_nr; /* sequence nr for nodes */
4798#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004799 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004800
Bram Moolenaar51485f02005-06-04 21:55:20 +00004801 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004802 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004803 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004804 int si_region; /* region mask */
4805 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004806 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004807 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004808 int si_msg_count; /* number of words added since last message */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004809 char_u *si_info; /* info text chars or NULL */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004810 int si_region_count; /* number of regions supported (1 when there
4811 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004812 char_u si_region_name[16]; /* region names; used only if
4813 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004814
4815 garray_T si_rep; /* list of fromto_T entries from REP lines */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004816 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004817 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004818 char_u *si_sofofr; /* SOFOFROM text */
4819 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004820 int si_nosugfile; /* NOSUGFILE item found */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004821 int si_nosplitsugs; /* NOSPLITSUGS item found */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004822 int si_followup; /* soundsalike: ? */
4823 int si_collapse; /* soundsalike: ? */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004824 hashtab_T si_commonwords; /* hashtable for common words */
4825 time_t si_sugtime; /* timestamp for .sug file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004826 int si_rem_accents; /* soundsalike: remove accents */
4827 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004828 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004829 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004830 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004831 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004832 int si_compoptions; /* COMP_ flags */
4833 garray_T si_comppat; /* CHECKCOMPOUNDPATTERN items, each stored as
4834 a string */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004835 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004836 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004837 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004838 garray_T si_prefcond; /* table with conditions for postponed
4839 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004840 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004841 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004842} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004843
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004844static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004845static int spell_info_item __ARGS((char_u *s));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004846static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
4847static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
4848static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004849static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004850static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
4851static void aff_check_number __ARGS((int spinval, int affval, char *name));
4852static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004853static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004854static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4855static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00004856static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004857static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004858static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004859static int get_affix_flags __ARGS((afffile_T *affile, char_u *afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004860static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004861static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004862static int store_aff_word __ARGS((spellinfo_T *spin, char_u *word, char_u *afflist, afffile_T *affile, hashtab_T *ht, hashtab_T *xht, int condit, int flags, char_u *pfxlist, int pfxlen));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004863static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
4864static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
4865static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004866static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004867static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004868static int store_word __ARGS((spellinfo_T *spin, char_u *word, int flags, int region, char_u *pfxlist, int need_affix));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004869static int tree_add_word __ARGS((spellinfo_T *spin, char_u *word, wordnode_T *tree, int flags, int region, int affixID));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004870static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004871static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004872static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
4873static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
4874static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004875static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004876static void put_sugtime __ARGS((spellinfo_T *spin, FILE *fd));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004877static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00004878static void clear_node __ARGS((wordnode_T *node));
4879static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004880static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname));
4881static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang));
4882static int sug_maketable __ARGS((spellinfo_T *spin));
4883static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap));
4884static int offset2bytes __ARGS((int nr, char_u *buf));
4885static int bytes2offset __ARGS((char_u **pp));
4886static void sug_write __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004887static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004888static void spell_message __ARGS((spellinfo_T *spin, char_u *str));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004889static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004890
Bram Moolenaar53805d12005-08-01 07:08:33 +00004891/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
4892 * but it must be negative to indicate the prefix tree to tree_add_word().
4893 * Use a negative number with the lower 8 bits zero. */
4894#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004895
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004896/* flags for "condit" argument of store_aff_word() */
4897#define CONDIT_COMB 1 /* affix must combine */
4898#define CONDIT_CFIX 2 /* affix must have CIRCUMFIX flag */
4899#define CONDIT_SUF 4 /* add a suffix for matching flags */
4900#define CONDIT_AFF 8 /* word already has an affix */
4901
Bram Moolenaar5195e452005-08-19 20:32:47 +00004902/*
4903 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
4904 */
4905static long compress_start = 30000; /* memory / SBLOCKSIZE */
4906static long compress_inc = 100; /* memory / SBLOCKSIZE */
4907static long compress_added = 500000; /* word count */
4908
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004909#ifdef SPELL_PRINTTREE
4910/*
4911 * For debugging the tree code: print the current tree in a (more or less)
4912 * readable format, so that we can see what happens when adding a word and/or
4913 * compressing the tree.
4914 * Based on code from Olaf Seibert.
4915 */
4916#define PRINTLINESIZE 1000
4917#define PRINTWIDTH 6
4918
4919#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4920 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4921
4922static char line1[PRINTLINESIZE];
4923static char line2[PRINTLINESIZE];
4924static char line3[PRINTLINESIZE];
4925
4926 static void
4927spell_clear_flags(wordnode_T *node)
4928{
4929 wordnode_T *np;
4930
4931 for (np = node; np != NULL; np = np->wn_sibling)
4932 {
4933 np->wn_u1.index = FALSE;
4934 spell_clear_flags(np->wn_child);
4935 }
4936}
4937
4938 static void
4939spell_print_node(wordnode_T *node, int depth)
4940{
4941 if (node->wn_u1.index)
4942 {
4943 /* Done this node before, print the reference. */
4944 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
4945 PRINTSOME(line2, depth, " ", 0, 0);
4946 PRINTSOME(line3, depth, " ", 0, 0);
4947 msg(line1);
4948 msg(line2);
4949 msg(line3);
4950 }
4951 else
4952 {
4953 node->wn_u1.index = TRUE;
4954
4955 if (node->wn_byte != NUL)
4956 {
4957 if (node->wn_child != NULL)
4958 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
4959 else
4960 /* Cannot happen? */
4961 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
4962 }
4963 else
4964 PRINTSOME(line1, depth, " $ ", 0, 0);
4965
4966 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
4967
4968 if (node->wn_sibling != NULL)
4969 PRINTSOME(line3, depth, " | ", 0, 0);
4970 else
4971 PRINTSOME(line3, depth, " ", 0, 0);
4972
4973 if (node->wn_byte == NUL)
4974 {
4975 msg(line1);
4976 msg(line2);
4977 msg(line3);
4978 }
4979
4980 /* do the children */
4981 if (node->wn_byte != NUL && node->wn_child != NULL)
4982 spell_print_node(node->wn_child, depth + 1);
4983
4984 /* do the siblings */
4985 if (node->wn_sibling != NULL)
4986 {
4987 /* get rid of all parent details except | */
4988 STRCPY(line1, line3);
4989 STRCPY(line2, line3);
4990 spell_print_node(node->wn_sibling, depth);
4991 }
4992 }
4993}
4994
4995 static void
4996spell_print_tree(wordnode_T *root)
4997{
4998 if (root != NULL)
4999 {
5000 /* Clear the "wn_u1.index" fields, used to remember what has been
5001 * done. */
5002 spell_clear_flags(root);
5003
5004 /* Recursively print the tree. */
5005 spell_print_node(root, 0);
5006 }
5007}
5008#endif /* SPELL_PRINTTREE */
5009
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005010/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005011 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00005012 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005013 */
5014 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005015spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005016 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005017 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005018{
5019 FILE *fd;
5020 afffile_T *aff;
5021 char_u rline[MAXLINELEN];
5022 char_u *line;
5023 char_u *pc = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005024#define MAXITEMCNT 30
Bram Moolenaar8db73182005-06-17 21:51:16 +00005025 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005026 int itemcnt;
5027 char_u *p;
5028 int lnum = 0;
5029 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005030 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005031 int aff_todo = 0;
5032 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005033 char_u *low = NULL;
5034 char_u *fol = NULL;
5035 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005036 int do_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005037 int do_repsal;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005038 int do_sal;
5039 int do_map;
5040 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005041 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005042 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005043 int compminlen = 0; /* COMPOUNDMIN value */
5044 int compsylmax = 0; /* COMPOUNDSYLMAX value */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005045 int compoptions = 0; /* COMP_ flags */
5046 int compmax = 0; /* COMPOUNDWORDMAX value */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005047 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDRULE
Bram Moolenaar6de68532005-08-24 22:08:48 +00005048 concatenated */
5049 char_u *midword = NULL; /* MIDWORD value */
5050 char_u *syllable = NULL; /* SYLLABLE value */
5051 char_u *sofofrom = NULL; /* SOFOFROM value */
5052 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005053
Bram Moolenaar51485f02005-06-04 21:55:20 +00005054 /*
5055 * Open the file.
5056 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005057 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005058 if (fd == NULL)
5059 {
5060 EMSG2(_(e_notopen), fname);
5061 return NULL;
5062 }
5063
Bram Moolenaar4770d092006-01-12 23:22:24 +00005064 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
5065 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005066
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005067 /* Only do REP lines when not done in another .aff file already. */
5068 do_rep = spin->si_rep.ga_len == 0;
5069
Bram Moolenaar4770d092006-01-12 23:22:24 +00005070 /* Only do REPSAL lines when not done in another .aff file already. */
5071 do_repsal = spin->si_repsal.ga_len == 0;
5072
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005073 /* Only do SAL lines when not done in another .aff file already. */
5074 do_sal = spin->si_sal.ga_len == 0;
5075
5076 /* Only do MAP lines when not done in another .aff file already. */
5077 do_map = spin->si_map.ga_len == 0;
5078
Bram Moolenaar51485f02005-06-04 21:55:20 +00005079 /*
5080 * Allocate and init the afffile_T structure.
5081 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005082 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005083 if (aff == NULL)
5084 return NULL;
5085 hash_init(&aff->af_pref);
5086 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005087 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005088
5089 /*
5090 * Read all the lines in the file one by one.
5091 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005092 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005093 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005094 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005095 ++lnum;
5096
5097 /* Skip comment lines. */
5098 if (*rline == '#')
5099 continue;
5100
5101 /* Convert from "SET" to 'encoding' when needed. */
5102 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005103#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005104 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005105 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005106 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005107 if (pc == NULL)
5108 {
5109 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5110 fname, lnum, rline);
5111 continue;
5112 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005113 line = pc;
5114 }
5115 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005116#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005117 {
5118 pc = NULL;
5119 line = rline;
5120 }
5121
5122 /* Split the line up in white separated items. Put a NUL after each
5123 * item. */
5124 itemcnt = 0;
5125 for (p = line; ; )
5126 {
5127 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
5128 ++p;
5129 if (*p == NUL)
5130 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00005131 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005132 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005133 items[itemcnt++] = p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005134 /* A few items have arbitrary text argument, don't split them. */
5135 if (itemcnt == 2 && spell_info_item(items[0]))
5136 while (*p >= ' ' || *p == TAB) /* skip until CR/NL */
5137 ++p;
5138 else
5139 while (*p > ' ') /* skip until white space or CR/NL */
5140 ++p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005141 if (*p == NUL)
5142 break;
5143 *p++ = NUL;
5144 }
5145
5146 /* Handle non-empty lines. */
5147 if (itemcnt > 0)
5148 {
5149 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
5150 && aff->af_enc == NULL)
5151 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005152#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005153 /* Setup for conversion from "ENC" to 'encoding'. */
5154 aff->af_enc = enc_canonize(items[1]);
5155 if (aff->af_enc != NULL && !spin->si_ascii
5156 && convert_setup(&spin->si_conv, aff->af_enc,
5157 p_enc) == FAIL)
5158 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
5159 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005160 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005161#else
5162 smsg((char_u *)_("Conversion in %s not supported"), fname);
5163#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005164 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005165 else if (STRCMP(items[0], "FLAG") == 0 && itemcnt == 2
5166 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005167 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005168 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005169 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005170 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005171 aff->af_flagtype = AFT_NUM;
5172 else if (STRCMP(items[1], "caplong") == 0)
5173 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005174 else
5175 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
5176 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00005177 if (aff->af_rare != 0
5178 || aff->af_keepcase != 0
5179 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005180 || aff->af_needaffix != 0
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005181 || aff->af_circumfix != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005182 || aff->af_needcomp != 0
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005183 || aff->af_comproot != 0
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005184 || aff->af_nosuggest != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005185 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00005186 || aff->af_suff.ht_used > 0
5187 || aff->af_pref.ht_used > 0)
5188 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
5189 fname, lnum, items[1]);
5190 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005191 else if (spell_info_item(items[0]))
5192 {
5193 p = (char_u *)getroom(spin,
5194 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
5195 + STRLEN(items[0])
5196 + STRLEN(items[1]) + 3, FALSE);
5197 if (p != NULL)
5198 {
5199 if (spin->si_info != NULL)
5200 {
5201 STRCPY(p, spin->si_info);
5202 STRCAT(p, "\n");
5203 }
5204 STRCAT(p, items[0]);
5205 STRCAT(p, " ");
5206 STRCAT(p, items[1]);
5207 spin->si_info = p;
5208 }
5209 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005210 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2
5211 && midword == NULL)
5212 {
5213 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005214 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005215 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005216 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005217 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005218 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005219 /* TODO: remove "RAR" later */
5220 else if ((STRCMP(items[0], "RAR") == 0
5221 || STRCMP(items[0], "RARE") == 0) && itemcnt == 2
5222 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005223 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005224 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005225 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005226 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005227 /* TODO: remove "KEP" later */
5228 else if ((STRCMP(items[0], "KEP") == 0
5229 || STRCMP(items[0], "KEEPCASE") == 0) && itemcnt == 2
5230 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005231 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005232 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005233 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005234 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005235 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
5236 && aff->af_bad == 0)
5237 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005238 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5239 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005240 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005241 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
5242 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005243 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005244 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5245 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005246 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005247 else if (STRCMP(items[0], "CIRCUMFIX") == 0 && itemcnt == 2
5248 && aff->af_circumfix == 0)
5249 {
5250 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
5251 fname, lnum);
5252 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005253 else if (STRCMP(items[0], "NOSUGGEST") == 0 && itemcnt == 2
5254 && aff->af_nosuggest == 0)
5255 {
5256 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
5257 fname, lnum);
5258 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005259 else if (STRCMP(items[0], "NEEDCOMPOUND") == 0 && itemcnt == 2
5260 && aff->af_needcomp == 0)
5261 {
5262 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5263 fname, lnum);
5264 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005265 else if (STRCMP(items[0], "COMPOUNDROOT") == 0 && itemcnt == 2
5266 && aff->af_comproot == 0)
5267 {
5268 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
5269 fname, lnum);
5270 }
5271 else if (STRCMP(items[0], "COMPOUNDFORBIDFLAG") == 0
5272 && itemcnt == 2 && aff->af_compforbid == 0)
5273 {
5274 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
5275 fname, lnum);
5276 }
5277 else if (STRCMP(items[0], "COMPOUNDPERMITFLAG") == 0
5278 && itemcnt == 2 && aff->af_comppermit == 0)
5279 {
5280 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
5281 fname, lnum);
5282 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005283 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005284 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005285 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005286 /* Turn flag "c" into COMPOUNDRULE compatible string "c+",
Bram Moolenaar6de68532005-08-24 22:08:48 +00005287 * "Na" into "Na+", "1234" into "1234+". */
5288 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005289 if (p != NULL)
5290 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005291 STRCPY(p, items[1]);
5292 STRCAT(p, "+");
5293 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005294 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005295 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005296 else if (STRCMP(items[0], "COMPOUNDRULE") == 0 && itemcnt == 2)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005297 {
5298 /* Concatenate this string to previously defined ones, using a
5299 * slash to separate them. */
5300 l = STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005301 if (compflags != NULL)
5302 l += STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005303 p = getroom(spin, l, FALSE);
5304 if (p != NULL)
5305 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005306 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005307 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005308 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005309 STRCAT(p, "/");
5310 }
5311 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005312 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005313 }
5314 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005315 else if (STRCMP(items[0], "COMPOUNDWORDMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005316 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005317 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005318 compmax = atoi((char *)items[1]);
5319 if (compmax == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005320 smsg((char_u *)_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar5195e452005-08-19 20:32:47 +00005321 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005322 }
5323 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005324 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005325 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005326 compminlen = atoi((char *)items[1]);
5327 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005328 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5329 fname, lnum, items[1]);
5330 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005331 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005332 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005333 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005334 compsylmax = atoi((char *)items[1]);
5335 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005336 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5337 fname, lnum, items[1]);
5338 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005339 else if (STRCMP(items[0], "CHECKCOMPOUNDDUP") == 0 && itemcnt == 1)
5340 {
5341 compoptions |= COMP_CHECKDUP;
5342 }
5343 else if (STRCMP(items[0], "CHECKCOMPOUNDREP") == 0 && itemcnt == 1)
5344 {
5345 compoptions |= COMP_CHECKREP;
5346 }
5347 else if (STRCMP(items[0], "CHECKCOMPOUNDCASE") == 0 && itemcnt == 1)
5348 {
5349 compoptions |= COMP_CHECKCASE;
5350 }
5351 else if (STRCMP(items[0], "CHECKCOMPOUNDTRIPLE") == 0
5352 && itemcnt == 1)
5353 {
5354 compoptions |= COMP_CHECKTRIPLE;
5355 }
5356 else if (STRCMP(items[0], "CHECKCOMPOUNDPATTERN") == 0
5357 && itemcnt == 2)
5358 {
5359 if (atoi((char *)items[1]) == 0)
5360 smsg((char_u *)_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
5361 fname, lnum, items[1]);
5362 }
5363 else if (STRCMP(items[0], "CHECKCOMPOUNDPATTERN") == 0
5364 && itemcnt == 3)
5365 {
5366 garray_T *gap = &spin->si_comppat;
5367 int i;
5368
5369 /* Only add the couple if it isn't already there. */
5370 for (i = 0; i < gap->ga_len - 1; i += 2)
5371 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
5372 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
5373 items[2]) == 0)
5374 break;
5375 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
5376 {
5377 ((char_u **)(gap->ga_data))[gap->ga_len++]
5378 = getroom_save(spin, items[1]);
5379 ((char_u **)(gap->ga_data))[gap->ga_len++]
5380 = getroom_save(spin, items[2]);
5381 }
5382 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005383 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005384 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005385 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005386 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005387 }
Bram Moolenaar78622822005-08-23 21:00:13 +00005388 else if (STRCMP(items[0], "NOBREAK") == 0 && itemcnt == 1)
5389 {
5390 spin->si_nobreak = TRUE;
5391 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005392 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
5393 {
5394 spin->si_nosplitsugs = TRUE;
5395 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005396 else if (STRCMP(items[0], "NOSUGFILE") == 0 && itemcnt == 1)
5397 {
5398 spin->si_nosugfile = TRUE;
5399 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005400 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
5401 {
5402 aff->af_pfxpostpone = TRUE;
5403 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005404 else if ((STRCMP(items[0], "PFX") == 0
5405 || STRCMP(items[0], "SFX") == 0)
5406 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005407 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005408 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005409 int lasti = 4;
5410 char_u key[AH_KEY_LEN];
5411
5412 if (*items[0] == 'P')
5413 tp = &aff->af_pref;
5414 else
5415 tp = &aff->af_suff;
5416
5417 /* Myspell allows the same affix name to be used multiple
5418 * times. The affix files that do this have an undocumented
5419 * "S" flag on all but the last block, thus we check for that
5420 * and store it in ah_follows. */
5421 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5422 hi = hash_find(tp, key);
5423 if (!HASHITEM_EMPTY(hi))
5424 {
5425 cur_aff = HI2AH(hi);
5426 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5427 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5428 fname, lnum, items[1]);
5429 if (!cur_aff->ah_follows)
5430 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5431 fname, lnum, items[1]);
5432 }
5433 else
5434 {
5435 /* New affix letter. */
5436 cur_aff = (affheader_T *)getroom(spin,
5437 sizeof(affheader_T), TRUE);
5438 if (cur_aff == NULL)
5439 break;
5440 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5441 fname, lnum);
5442 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5443 break;
5444 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00005445 || cur_aff->ah_flag == aff->af_rare
5446 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005447 || cur_aff->ah_flag == aff->af_needaffix
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005448 || cur_aff->ah_flag == aff->af_circumfix
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005449 || cur_aff->ah_flag == aff->af_nosuggest
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005450 || cur_aff->ah_flag == aff->af_needcomp
5451 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005452 smsg((char_u *)_("Affix also used for BAD/RARE/KEEPCASE/NEEDAFFIX/NEEDCOMPOUND/NOSUGGEST in %s line %d: %s"),
Bram Moolenaar95529562005-08-25 21:21:38 +00005453 fname, lnum, items[1]);
5454 STRCPY(cur_aff->ah_key, items[1]);
5455 hash_add(tp, cur_aff->ah_key);
5456
5457 cur_aff->ah_combine = (*items[2] == 'Y');
5458 }
5459
5460 /* Check for the "S" flag, which apparently means that another
5461 * block with the same affix name is following. */
5462 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5463 {
5464 ++lasti;
5465 cur_aff->ah_follows = TRUE;
5466 }
5467 else
5468 cur_aff->ah_follows = FALSE;
5469
Bram Moolenaar8db73182005-06-17 21:51:16 +00005470 /* Myspell allows extra text after the item, but that might
5471 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00005472 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005473 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005474
Bram Moolenaar95529562005-08-25 21:21:38 +00005475 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005476 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5477 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005478
Bram Moolenaar95529562005-08-25 21:21:38 +00005479 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005480 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005481 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005482 {
5483 /* Use a new number in the .spl file later, to be able
5484 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005485 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005486 cur_aff->ah_newID = ++spin->si_newprefID;
5487
5488 /* We only really use ah_newID if the prefix is
5489 * postponed. We know that only after handling all
5490 * the items. */
5491 did_postpone_prefix = FALSE;
5492 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005493 else
5494 /* Did use the ID in a previous block. */
5495 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005496 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005497
Bram Moolenaar51485f02005-06-04 21:55:20 +00005498 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005499 }
5500 else if ((STRCMP(items[0], "PFX") == 0
5501 || STRCMP(items[0], "SFX") == 0)
5502 && aff_todo > 0
5503 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005504 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005505 {
5506 affentry_T *aff_entry;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005507 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005508 int lasti = 5;
5509
Bram Moolenaar8db73182005-06-17 21:51:16 +00005510 /* Myspell allows extra text after the item, but that might
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005511 * mean mistakes go unnoticed. Require a comment-starter.
5512 * Hunspell uses a "-" item. */
5513 if (itemcnt > lasti && *items[lasti] != '#'
5514 && (STRCMP(items[lasti], "-") != 0
5515 || itemcnt != lasti + 1))
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005516 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005517
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005518 /* New item for an affix letter. */
5519 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005520 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005521 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005522 if (aff_entry == NULL)
5523 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005524
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005525 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005526 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005527 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005528 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005529 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005530
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005531 /* Recognize flags on the affix: abcd/1234 */
5532 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
5533 if (aff_entry->ae_flags != NULL)
5534 *aff_entry->ae_flags++ = NUL;
5535 }
5536
Bram Moolenaar51485f02005-06-04 21:55:20 +00005537 /* Don't use an affix entry with non-ASCII characters when
5538 * "spin->si_ascii" is TRUE. */
5539 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005540 || has_non_ascii(aff_entry->ae_add)))
5541 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00005542 aff_entry->ae_next = cur_aff->ah_first;
5543 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005544
5545 if (STRCMP(items[4], ".") != 0)
5546 {
5547 char_u buf[MAXLINELEN];
5548
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005549 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005550 if (*items[0] == 'P')
5551 sprintf((char *)buf, "^%s", items[4]);
5552 else
5553 sprintf((char *)buf, "%s$", items[4]);
5554 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005555 RE_MAGIC + RE_STRING + RE_STRICT);
5556 if (aff_entry->ae_prog == NULL)
5557 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5558 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005559 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005560
5561 /* For postponed prefixes we need an entry in si_prefcond
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005562 * for the condition. Use an existing one if possible.
5563 * Can't be done for an affix with flags. */
5564 if (*items[0] == 'P' && aff->af_pfxpostpone
5565 && aff_entry->ae_flags == NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005566 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005567 /* When the chop string is one lower-case letter and
5568 * the add string ends in the upper-case letter we set
5569 * the "upper" flag, clear "ae_chop" and remove the
5570 * letters from "ae_add". The condition must either
5571 * be empty or start with the same letter. */
5572 if (aff_entry->ae_chop != NULL
5573 && aff_entry->ae_add != NULL
5574#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005575 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00005576 aff_entry->ae_chop)] == NUL
5577#else
5578 && aff_entry->ae_chop[1] == NUL
5579#endif
5580 )
5581 {
5582 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005583
Bram Moolenaar53805d12005-08-01 07:08:33 +00005584 c = PTR2CHAR(aff_entry->ae_chop);
5585 c_up = SPELL_TOUPPER(c);
5586 if (c_up != c
5587 && (aff_entry->ae_cond == NULL
5588 || PTR2CHAR(aff_entry->ae_cond) == c))
5589 {
5590 p = aff_entry->ae_add
5591 + STRLEN(aff_entry->ae_add);
5592 mb_ptr_back(aff_entry->ae_add, p);
5593 if (PTR2CHAR(p) == c_up)
5594 {
5595 upper = TRUE;
5596 aff_entry->ae_chop = NULL;
5597 *p = NUL;
5598
5599 /* The condition is matched with the
5600 * actual word, thus must check for the
5601 * upper-case letter. */
5602 if (aff_entry->ae_cond != NULL)
5603 {
5604 char_u buf[MAXLINELEN];
5605#ifdef FEAT_MBYTE
5606 if (has_mbyte)
5607 {
5608 onecap_copy(items[4], buf, TRUE);
5609 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005610 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005611 }
5612 else
5613#endif
5614 *aff_entry->ae_cond = c_up;
5615 if (aff_entry->ae_cond != NULL)
5616 {
5617 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005618 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005619 vim_free(aff_entry->ae_prog);
5620 aff_entry->ae_prog = vim_regcomp(
5621 buf, RE_MAGIC + RE_STRING);
5622 }
5623 }
5624 }
5625 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005626 }
5627
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005628 if (aff_entry->ae_chop == NULL
5629 && aff_entry->ae_flags == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005630 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005631 int idx;
5632 char_u **pp;
5633 int n;
5634
Bram Moolenaar6de68532005-08-24 22:08:48 +00005635 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005636 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5637 --idx)
5638 {
5639 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5640 if (str_equal(p, aff_entry->ae_cond))
5641 break;
5642 }
5643 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5644 {
5645 /* Not found, add a new condition. */
5646 idx = spin->si_prefcond.ga_len++;
5647 pp = ((char_u **)spin->si_prefcond.ga_data)
5648 + idx;
5649 if (aff_entry->ae_cond == NULL)
5650 *pp = NULL;
5651 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005652 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005653 aff_entry->ae_cond);
5654 }
5655
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005656 if (aff_entry->ae_flags != NULL)
5657 smsg((char_u *)_("Affix flags ignored when PFXPOSTPONE used in %s line %d: %s"),
5658 fname, lnum, items[4]);
5659
Bram Moolenaar53805d12005-08-01 07:08:33 +00005660 /* Add the prefix to the prefix tree. */
5661 if (aff_entry->ae_add == NULL)
5662 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005663 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005664 p = aff_entry->ae_add;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005665
Bram Moolenaar53805d12005-08-01 07:08:33 +00005666 /* PFX_FLAGS is a negative number, so that
5667 * tree_add_word() knows this is the prefix tree. */
5668 n = PFX_FLAGS;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005669 if (!cur_aff->ah_combine)
5670 n |= WFP_NC;
5671 if (upper)
5672 n |= WFP_UP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005673 tree_add_word(spin, p, spin->si_prefroot, n,
5674 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005675 did_postpone_prefix = TRUE;
5676 }
5677
5678 /* Didn't actually use ah_newID, backup si_newprefID. */
5679 if (aff_todo == 0 && !did_postpone_prefix)
5680 {
5681 --spin->si_newprefID;
5682 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005683 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005684 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005685 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005686 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005687 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2
5688 && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005689 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005690 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005691 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005692 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2
5693 && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005694 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005695 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005696 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005697 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2
5698 && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005699 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005700 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005701 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005702 else if ((STRCMP(items[0], "REP") == 0
5703 || STRCMP(items[0], "REPSAL") == 0)
5704 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005705 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005706 /* Ignore REP/REPSAL count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005707 if (!isdigit(*items[1]))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005708 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005709 fname, lnum);
5710 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005711 else if ((STRCMP(items[0], "REP") == 0
5712 || STRCMP(items[0], "REPSAL") == 0)
5713 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005714 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005715 /* REP/REPSAL item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005716 /* Myspell ignores extra arguments, we require it starts with
5717 * # to detect mistakes. */
5718 if (itemcnt > 3 && items[3][0] != '#')
5719 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005720 if (items[0][3] == 'S' ? do_repsal : do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005721 {
5722 /* Replace underscore with space (can't include a space
5723 * directly). */
5724 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5725 if (*p == '_')
5726 *p = ' ';
5727 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5728 if (*p == '_')
5729 *p = ' ';
Bram Moolenaar4770d092006-01-12 23:22:24 +00005730 add_fromto(spin, items[0][3] == 'S'
5731 ? &spin->si_repsal
5732 : &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005733 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005734 }
5735 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
5736 {
5737 /* MAP item or count */
5738 if (!found_map)
5739 {
5740 /* First line contains the count. */
5741 found_map = TRUE;
5742 if (!isdigit(*items[1]))
5743 smsg((char_u *)_("Expected MAP count in %s line %d"),
5744 fname, lnum);
5745 }
5746 else if (do_map)
5747 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005748 int c;
5749
5750 /* Check that every character appears only once. */
5751 for (p = items[1]; *p != NUL; )
5752 {
5753#ifdef FEAT_MBYTE
5754 c = mb_ptr2char_adv(&p);
5755#else
5756 c = *p++;
5757#endif
5758 if ((spin->si_map.ga_len > 0
5759 && vim_strchr(spin->si_map.ga_data, c)
5760 != NULL)
5761 || vim_strchr(p, c) != NULL)
5762 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5763 fname, lnum);
5764 }
5765
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005766 /* We simply concatenate all the MAP strings, separated by
5767 * slashes. */
5768 ga_concat(&spin->si_map, items[1]);
5769 ga_append(&spin->si_map, '/');
5770 }
5771 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005772 /* Accept "SAL from to" and "SAL from to # comment". */
5773 else if (STRCMP(items[0], "SAL") == 0
5774 && (itemcnt == 3 || (itemcnt > 3 && items[3][0] == '#')))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005775 {
5776 if (do_sal)
5777 {
5778 /* SAL item (sounds-a-like)
5779 * Either one of the known keys or a from-to pair. */
5780 if (STRCMP(items[1], "followup") == 0)
5781 spin->si_followup = sal_to_bool(items[2]);
5782 else if (STRCMP(items[1], "collapse_result") == 0)
5783 spin->si_collapse = sal_to_bool(items[2]);
5784 else if (STRCMP(items[1], "remove_accents") == 0)
5785 spin->si_rem_accents = sal_to_bool(items[2]);
5786 else
5787 /* when "to" is "_" it means empty */
5788 add_fromto(spin, &spin->si_sal, items[1],
5789 STRCMP(items[2], "_") == 0 ? (char_u *)""
5790 : items[2]);
5791 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005792 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005793 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005794 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005795 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005796 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005797 }
5798 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005799 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005800 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005801 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005802 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005803 else if (STRCMP(items[0], "COMMON") == 0)
5804 {
5805 int i;
5806
5807 for (i = 1; i < itemcnt; ++i)
5808 {
5809 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
5810 items[i])))
5811 {
5812 p = vim_strsave(items[i]);
5813 if (p == NULL)
5814 break;
5815 hash_add(&spin->si_commonwords, p);
5816 }
5817 }
5818 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005819 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005820 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005821 fname, lnum, items[0]);
5822 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005823 }
5824
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005825 if (fol != NULL || low != NULL || upp != NULL)
5826 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005827 if (spin->si_clear_chartab)
5828 {
5829 /* Clear the char type tables, don't want to use any of the
5830 * currently used spell properties. */
5831 init_spell_chartab();
5832 spin->si_clear_chartab = FALSE;
5833 }
5834
Bram Moolenaar3982c542005-06-08 21:56:31 +00005835 /*
5836 * Don't write a word table for an ASCII file, so that we don't check
5837 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005838 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00005839 * mb_get_class(), the list of chars in the file will be incomplete.
5840 */
5841 if (!spin->si_ascii
5842#ifdef FEAT_MBYTE
5843 && !enc_utf8
5844#endif
5845 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005846 {
5847 if (fol == NULL || low == NULL || upp == NULL)
5848 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
5849 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00005850 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005851 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005852
5853 vim_free(fol);
5854 vim_free(low);
5855 vim_free(upp);
5856 }
5857
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005858 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005859 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005860 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005861 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
Bram Moolenaar6de68532005-08-24 22:08:48 +00005862 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005863 }
5864
Bram Moolenaar6de68532005-08-24 22:08:48 +00005865 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005866 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005867 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
5868 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005869 }
5870
Bram Moolenaar6de68532005-08-24 22:08:48 +00005871 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005872 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005873 if (syllable == NULL)
5874 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
5875 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
5876 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005877 }
5878
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005879 if (compoptions != 0)
5880 {
5881 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
5882 spin->si_compoptions |= compoptions;
5883 }
5884
Bram Moolenaar6de68532005-08-24 22:08:48 +00005885 if (compflags != NULL)
5886 process_compflags(spin, aff, compflags);
5887
5888 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005889 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005890 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005891 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005892 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005893 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005894 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005895 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005896 MSG(_("Too many posponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005897 }
5898
Bram Moolenaar6de68532005-08-24 22:08:48 +00005899 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005900 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005901 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
5902 spin->si_syllable = syllable;
5903 }
5904
5905 if (sofofrom != NULL || sofoto != NULL)
5906 {
5907 if (sofofrom == NULL || sofoto == NULL)
5908 smsg((char_u *)_("Missing SOFO%s line in %s"),
5909 sofofrom == NULL ? "FROM" : "TO", fname);
5910 else if (spin->si_sal.ga_len > 0)
5911 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005912 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005913 {
5914 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
5915 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
5916 spin->si_sofofr = sofofrom;
5917 spin->si_sofoto = sofoto;
5918 }
5919 }
5920
5921 if (midword != NULL)
5922 {
5923 aff_check_string(spin->si_midword, midword, "MIDWORD");
5924 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005925 }
5926
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005927 vim_free(pc);
5928 fclose(fd);
5929 return aff;
5930}
5931
5932/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005933 * Return TRUE if "s" is the name of an info item in the affix file.
5934 */
5935 static int
5936spell_info_item(s)
5937 char_u *s;
5938{
5939 return STRCMP(s, "NAME") == 0
5940 || STRCMP(s, "HOME") == 0
5941 || STRCMP(s, "VERSION") == 0
5942 || STRCMP(s, "AUTHOR") == 0
5943 || STRCMP(s, "EMAIL") == 0
5944 || STRCMP(s, "COPYRIGHT") == 0;
5945}
5946
5947/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00005948 * Turn an affix flag name into a number, according to the FLAG type.
5949 * returns zero for failure.
5950 */
5951 static unsigned
5952affitem2flag(flagtype, item, fname, lnum)
5953 int flagtype;
5954 char_u *item;
5955 char_u *fname;
5956 int lnum;
5957{
5958 unsigned res;
5959 char_u *p = item;
5960
5961 res = get_affitem(flagtype, &p);
5962 if (res == 0)
5963 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005964 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005965 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
5966 fname, lnum, item);
5967 else
5968 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
5969 fname, lnum, item);
5970 }
5971 if (*p != NUL)
5972 {
5973 smsg((char_u *)_(e_affname), fname, lnum, item);
5974 return 0;
5975 }
5976
5977 return res;
5978}
5979
5980/*
5981 * Get one affix name from "*pp" and advance the pointer.
5982 * Returns zero for an error, still advances the pointer then.
5983 */
5984 static unsigned
5985get_affitem(flagtype, pp)
5986 int flagtype;
5987 char_u **pp;
5988{
5989 int res;
5990
Bram Moolenaar95529562005-08-25 21:21:38 +00005991 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005992 {
5993 if (!VIM_ISDIGIT(**pp))
5994 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005995 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005996 return 0;
5997 }
5998 res = getdigits(pp);
5999 }
6000 else
6001 {
6002#ifdef FEAT_MBYTE
6003 res = mb_ptr2char_adv(pp);
6004#else
6005 res = *(*pp)++;
6006#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006007 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00006008 && res >= 'A' && res <= 'Z'))
6009 {
6010 if (**pp == NUL)
6011 return 0;
6012#ifdef FEAT_MBYTE
6013 res = mb_ptr2char_adv(pp) + (res << 16);
6014#else
6015 res = *(*pp)++ + (res << 16);
6016#endif
6017 }
6018 }
6019 return res;
6020}
6021
6022/*
6023 * Process the "compflags" string used in an affix file and append it to
6024 * spin->si_compflags.
6025 * The processing involves changing the affix names to ID numbers, so that
6026 * they fit in one byte.
6027 */
6028 static void
6029process_compflags(spin, aff, compflags)
6030 spellinfo_T *spin;
6031 afffile_T *aff;
6032 char_u *compflags;
6033{
6034 char_u *p;
6035 char_u *prevp;
6036 unsigned flag;
6037 compitem_T *ci;
6038 int id;
6039 int len;
6040 char_u *tp;
6041 char_u key[AH_KEY_LEN];
6042 hashitem_T *hi;
6043
6044 /* Make room for the old and the new compflags, concatenated with a / in
6045 * between. Processing it makes it shorter, but we don't know by how
6046 * much, thus allocate the maximum. */
6047 len = STRLEN(compflags) + 1;
6048 if (spin->si_compflags != NULL)
6049 len += STRLEN(spin->si_compflags) + 1;
6050 p = getroom(spin, len, FALSE);
6051 if (p == NULL)
6052 return;
6053 if (spin->si_compflags != NULL)
6054 {
6055 STRCPY(p, spin->si_compflags);
6056 STRCAT(p, "/");
6057 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00006058 spin->si_compflags = p;
6059 tp = p + STRLEN(p);
6060
6061 for (p = compflags; *p != NUL; )
6062 {
6063 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
6064 /* Copy non-flag characters directly. */
6065 *tp++ = *p++;
6066 else
6067 {
6068 /* First get the flag number, also checks validity. */
6069 prevp = p;
6070 flag = get_affitem(aff->af_flagtype, &p);
6071 if (flag != 0)
6072 {
6073 /* Find the flag in the hashtable. If it was used before, use
6074 * the existing ID. Otherwise add a new entry. */
6075 vim_strncpy(key, prevp, p - prevp);
6076 hi = hash_find(&aff->af_comp, key);
6077 if (!HASHITEM_EMPTY(hi))
6078 id = HI2CI(hi)->ci_newID;
6079 else
6080 {
6081 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
6082 if (ci == NULL)
6083 break;
6084 STRCPY(ci->ci_key, key);
6085 ci->ci_flag = flag;
6086 /* Avoid using a flag ID that has a special meaning in a
6087 * regexp (also inside []). */
6088 do
6089 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006090 check_renumber(spin);
6091 id = spin->si_newcompID--;
6092 } while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006093 ci->ci_newID = id;
6094 hash_add(&aff->af_comp, ci->ci_key);
6095 }
6096 *tp++ = id;
6097 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006098 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006099 ++p;
6100 }
6101 }
6102
6103 *tp = NUL;
6104}
6105
6106/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006107 * Check that the new IDs for postponed affixes and compounding don't overrun
6108 * each other. We have almost 255 available, but start at 0-127 to avoid
6109 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
6110 * When that is used up an error message is given.
6111 */
6112 static void
6113check_renumber(spin)
6114 spellinfo_T *spin;
6115{
6116 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
6117 {
6118 spin->si_newprefID = 127;
6119 spin->si_newcompID = 255;
6120 }
6121}
6122
6123/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006124 * Return TRUE if flag "flag" appears in affix list "afflist".
6125 */
6126 static int
6127flag_in_afflist(flagtype, afflist, flag)
6128 int flagtype;
6129 char_u *afflist;
6130 unsigned flag;
6131{
6132 char_u *p;
6133 unsigned n;
6134
6135 switch (flagtype)
6136 {
6137 case AFT_CHAR:
6138 return vim_strchr(afflist, flag) != NULL;
6139
Bram Moolenaar95529562005-08-25 21:21:38 +00006140 case AFT_CAPLONG:
6141 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006142 for (p = afflist; *p != NUL; )
6143 {
6144#ifdef FEAT_MBYTE
6145 n = mb_ptr2char_adv(&p);
6146#else
6147 n = *p++;
6148#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006149 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00006150 && *p != NUL)
6151#ifdef FEAT_MBYTE
6152 n = mb_ptr2char_adv(&p) + (n << 16);
6153#else
6154 n = *p++ + (n << 16);
6155#endif
6156 if (n == flag)
6157 return TRUE;
6158 }
6159 break;
6160
Bram Moolenaar95529562005-08-25 21:21:38 +00006161 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006162 for (p = afflist; *p != NUL; )
6163 {
6164 n = getdigits(&p);
6165 if (n == flag)
6166 return TRUE;
6167 if (*p != NUL) /* skip over comma */
6168 ++p;
6169 }
6170 break;
6171 }
6172 return FALSE;
6173}
6174
6175/*
6176 * Give a warning when "spinval" and "affval" numbers are set and not the same.
6177 */
6178 static void
6179aff_check_number(spinval, affval, name)
6180 int spinval;
6181 int affval;
6182 char *name;
6183{
6184 if (spinval != 0 && spinval != affval)
6185 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6186}
6187
6188/*
6189 * Give a warning when "spinval" and "affval" strings are set and not the same.
6190 */
6191 static void
6192aff_check_string(spinval, affval, name)
6193 char_u *spinval;
6194 char_u *affval;
6195 char *name;
6196{
6197 if (spinval != NULL && STRCMP(spinval, affval) != 0)
6198 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6199}
6200
6201/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006202 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
6203 * NULL as equal.
6204 */
6205 static int
6206str_equal(s1, s2)
6207 char_u *s1;
6208 char_u *s2;
6209{
6210 if (s1 == NULL || s2 == NULL)
6211 return s1 == s2;
6212 return STRCMP(s1, s2) == 0;
6213}
6214
6215/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006216 * Add a from-to item to "gap". Used for REP and SAL items.
6217 * They are stored case-folded.
6218 */
6219 static void
6220add_fromto(spin, gap, from, to)
6221 spellinfo_T *spin;
6222 garray_T *gap;
6223 char_u *from;
6224 char_u *to;
6225{
6226 fromto_T *ftp;
6227 char_u word[MAXWLEN];
6228
6229 if (ga_grow(gap, 1) == OK)
6230 {
6231 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
6232 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006233 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006234 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006235 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006236 ++gap->ga_len;
6237 }
6238}
6239
6240/*
6241 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6242 */
6243 static int
6244sal_to_bool(s)
6245 char_u *s;
6246{
6247 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
6248}
6249
6250/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00006251 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6252 * When "s" is NULL FALSE is returned.
6253 */
6254 static int
6255has_non_ascii(s)
6256 char_u *s;
6257{
6258 char_u *p;
6259
6260 if (s != NULL)
6261 for (p = s; *p != NUL; ++p)
6262 if (*p >= 128)
6263 return TRUE;
6264 return FALSE;
6265}
6266
6267/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006268 * Free the structure filled by spell_read_aff().
6269 */
6270 static void
6271spell_free_aff(aff)
6272 afffile_T *aff;
6273{
6274 hashtab_T *ht;
6275 hashitem_T *hi;
6276 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006277 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006278 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006279
6280 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006281
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006282 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006283 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
6284 {
6285 todo = ht->ht_used;
6286 for (hi = ht->ht_array; todo > 0; ++hi)
6287 {
6288 if (!HASHITEM_EMPTY(hi))
6289 {
6290 --todo;
6291 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006292 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
6293 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006294 }
6295 }
6296 if (ht == &aff->af_suff)
6297 break;
6298 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006299
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006300 hash_clear(&aff->af_pref);
6301 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006302 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006303}
6304
6305/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006306 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006307 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006308 */
6309 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006310spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006311 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006312 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006313 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006314{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006315 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006316 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006317 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006318 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006319 char_u store_afflist[MAXWLEN];
6320 int pfxlen;
6321 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006322 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006323 char_u *pc;
6324 char_u *w;
6325 int l;
6326 hash_T hash;
6327 hashitem_T *hi;
6328 FILE *fd;
6329 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006330 int non_ascii = 0;
6331 int retval = OK;
6332 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006333 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006334 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006335
Bram Moolenaar51485f02005-06-04 21:55:20 +00006336 /*
6337 * Open the file.
6338 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006339 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006340 if (fd == NULL)
6341 {
6342 EMSG2(_(e_notopen), fname);
6343 return FAIL;
6344 }
6345
Bram Moolenaar51485f02005-06-04 21:55:20 +00006346 /* The hashtable is only used to detect duplicated words. */
6347 hash_init(&ht);
6348
Bram Moolenaar4770d092006-01-12 23:22:24 +00006349 vim_snprintf((char *)IObuff, IOSIZE,
6350 _("Reading dictionary file %s ..."), fname);
6351 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006352
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006353 /* start with a message for the first line */
6354 spin->si_msg_count = 999999;
6355
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006356 /* Read and ignore the first line: word count. */
6357 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006358 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006359 EMSG2(_("E760: No word count in %s"), fname);
6360
6361 /*
6362 * Read all the lines in the file one by one.
6363 * The words are converted to 'encoding' here, before being added to
6364 * the hashtable.
6365 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006366 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006367 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006368 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006369 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00006370 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006371 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006372
Bram Moolenaar51485f02005-06-04 21:55:20 +00006373 /* Remove CR, LF and white space from the end. White space halfway
6374 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006375 l = STRLEN(line);
6376 while (l > 0 && line[l - 1] <= ' ')
6377 --l;
6378 if (l == 0)
6379 continue; /* empty line */
6380 line[l] = NUL;
6381
Bram Moolenaar66fa2712006-01-22 23:22:22 +00006382 /* Truncate the word at the "/", set "afflist" to what follows.
6383 * Replace "\/" by "/" and "\\" by "\". */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006384 afflist = NULL;
6385 for (p = line; *p != NUL; mb_ptr_adv(p))
6386 {
Bram Moolenaar66fa2712006-01-22 23:22:22 +00006387 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
6388 mch_memmove(p, p + 1, STRLEN(p));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006389 else if (*p == '/')
6390 {
6391 *p = NUL;
6392 afflist = p + 1;
6393 break;
6394 }
6395 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006396
6397 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6398 if (spin->si_ascii && has_non_ascii(line))
6399 {
6400 ++non_ascii;
Bram Moolenaar5482f332005-04-17 20:18:43 +00006401 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006402 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00006403
Bram Moolenaarb765d632005-06-07 21:00:02 +00006404#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006405 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006406 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006407 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006408 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006409 if (pc == NULL)
6410 {
6411 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6412 fname, lnum, line);
6413 continue;
6414 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006415 w = pc;
6416 }
6417 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006418#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006419 {
6420 pc = NULL;
6421 w = line;
6422 }
6423
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006424 /* This takes time, print a message every 10000 words. */
6425 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006426 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006427 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006428 vim_snprintf((char *)message, sizeof(message),
6429 _("line %6d, word %6d - %s"),
6430 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6431 msg_start();
6432 msg_puts_long_attr(message, 0);
6433 msg_clr_eos();
6434 msg_didout = FALSE;
6435 msg_col = 0;
6436 out_flush();
6437 }
6438
Bram Moolenaar51485f02005-06-04 21:55:20 +00006439 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006440 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006441 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006442 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006443 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006444 if (retval == FAIL)
6445 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006446
Bram Moolenaar51485f02005-06-04 21:55:20 +00006447 hash = hash_hash(dw);
6448 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006449 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006450 {
6451 if (p_verbose > 0)
6452 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006453 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006454 else if (duplicate == 0)
6455 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6456 fname, lnum, dw);
6457 ++duplicate;
6458 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006459 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006460 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006461
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006462 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006463 store_afflist[0] = NUL;
6464 pfxlen = 0;
6465 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006466 if (afflist != NULL)
6467 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006468 /* Extract flags from the affix list. */
6469 flags |= get_affix_flags(affile, afflist);
6470
Bram Moolenaar6de68532005-08-24 22:08:48 +00006471 if (affile->af_needaffix != 0 && flag_in_afflist(
6472 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006473 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006474
6475 if (affile->af_pfxpostpone)
6476 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006477 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006478
Bram Moolenaar5195e452005-08-19 20:32:47 +00006479 if (spin->si_compflags != NULL)
6480 /* Need to store the list of compound flags with the word.
6481 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006482 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006483 }
6484
Bram Moolenaar51485f02005-06-04 21:55:20 +00006485 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006486 if (store_word(spin, dw, flags, spin->si_region,
6487 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006488 retval = FAIL;
6489
6490 if (afflist != NULL)
6491 {
6492 /* Find all matching suffixes and add the resulting words.
6493 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006494 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006495 &affile->af_suff, &affile->af_pref,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006496 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006497 retval = FAIL;
6498
6499 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006500 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006501 &affile->af_pref, NULL,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006502 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006503 retval = FAIL;
6504 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006505 }
6506
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006507 if (duplicate > 0)
6508 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006509 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006510 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6511 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006512 hash_clear(&ht);
6513
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006514 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006515 return retval;
6516}
6517
6518/*
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006519 * Check for affix flags in "afflist" that are turned into word flags.
6520 * Return WF_ flags.
6521 */
6522 static int
6523get_affix_flags(affile, afflist)
6524 afffile_T *affile;
6525 char_u *afflist;
6526{
6527 int flags = 0;
6528
6529 if (affile->af_keepcase != 0 && flag_in_afflist(
6530 affile->af_flagtype, afflist, affile->af_keepcase))
6531 flags |= WF_KEEPCAP | WF_FIXCAP;
6532 if (affile->af_rare != 0 && flag_in_afflist(
6533 affile->af_flagtype, afflist, affile->af_rare))
6534 flags |= WF_RARE;
6535 if (affile->af_bad != 0 && flag_in_afflist(
6536 affile->af_flagtype, afflist, affile->af_bad))
6537 flags |= WF_BANNED;
6538 if (affile->af_needcomp != 0 && flag_in_afflist(
6539 affile->af_flagtype, afflist, affile->af_needcomp))
6540 flags |= WF_NEEDCOMP;
6541 if (affile->af_comproot != 0 && flag_in_afflist(
6542 affile->af_flagtype, afflist, affile->af_comproot))
6543 flags |= WF_COMPROOT;
6544 if (affile->af_nosuggest != 0 && flag_in_afflist(
6545 affile->af_flagtype, afflist, affile->af_nosuggest))
6546 flags |= WF_NOSUGGEST;
6547 return flags;
6548}
6549
6550/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006551 * Get the list of prefix IDs from the affix list "afflist".
6552 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006553 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6554 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006555 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006556 static int
6557get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006558 afffile_T *affile;
6559 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006560 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006561{
6562 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006563 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006564 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006565 int id;
6566 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006567 hashitem_T *hi;
6568
Bram Moolenaar6de68532005-08-24 22:08:48 +00006569 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006570 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006571 prevp = p;
6572 if (get_affitem(affile->af_flagtype, &p) != 0)
6573 {
6574 /* A flag is a postponed prefix flag if it appears in "af_pref"
6575 * and it's ID is not zero. */
6576 vim_strncpy(key, prevp, p - prevp);
6577 hi = hash_find(&affile->af_pref, key);
6578 if (!HASHITEM_EMPTY(hi))
6579 {
6580 id = HI2AH(hi)->ah_newID;
6581 if (id != 0)
6582 store_afflist[cnt++] = id;
6583 }
6584 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006585 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006586 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006587 }
6588
Bram Moolenaar5195e452005-08-19 20:32:47 +00006589 store_afflist[cnt] = NUL;
6590 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006591}
6592
6593/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006594 * Get the list of compound IDs from the affix list "afflist" that are used
6595 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006596 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006597 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006598 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00006599get_compflags(affile, afflist, store_afflist)
6600 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006601 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006602 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006603{
6604 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006605 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006606 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006607 char_u key[AH_KEY_LEN];
6608 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006609
Bram Moolenaar6de68532005-08-24 22:08:48 +00006610 for (p = afflist; *p != NUL; )
6611 {
6612 prevp = p;
6613 if (get_affitem(affile->af_flagtype, &p) != 0)
6614 {
6615 /* A flag is a compound flag if it appears in "af_comp". */
6616 vim_strncpy(key, prevp, p - prevp);
6617 hi = hash_find(&affile->af_comp, key);
6618 if (!HASHITEM_EMPTY(hi))
6619 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6620 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006621 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006622 ++p;
6623 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006624
Bram Moolenaar5195e452005-08-19 20:32:47 +00006625 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006626}
6627
6628/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006629 * Apply affixes to a word and store the resulting words.
6630 * "ht" is the hashtable with affentry_T that need to be applied, either
6631 * prefixes or suffixes.
6632 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6633 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006634 *
6635 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006636 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006637 static int
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006638store_aff_word(spin, word, afflist, affile, ht, xht, condit, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006639 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006640 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006641 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006642 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006643 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006644 hashtab_T *ht;
6645 hashtab_T *xht;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006646 int condit; /* CONDIT_SUF et al. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006647 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006648 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006649 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
6650 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006651{
6652 int todo;
6653 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006654 affheader_T *ah;
6655 affentry_T *ae;
6656 regmatch_T regmatch;
6657 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006658 int retval = OK;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006659 int i, j;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006660 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006661 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006662 char_u *use_pfxlist;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006663 int use_pfxlen;
6664 int need_affix;
6665 char_u store_afflist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006666 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006667 size_t wordlen = STRLEN(word);
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006668 int use_condit;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006669
Bram Moolenaar51485f02005-06-04 21:55:20 +00006670 todo = ht->ht_used;
6671 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006672 {
6673 if (!HASHITEM_EMPTY(hi))
6674 {
6675 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006676 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006677
Bram Moolenaar51485f02005-06-04 21:55:20 +00006678 /* Check that the affix combines, if required, and that the word
6679 * supports this affix. */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006680 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
6681 && flag_in_afflist(affile->af_flagtype, afflist,
6682 ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006683 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006684 /* Loop over all affix entries with this name. */
6685 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006686 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006687 /* Check the condition. It's not logical to match case
6688 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006689 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006690 * Another requirement from Myspell is that the chop
6691 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006692 * For prefixes, when "PFXPOSTPONE" was used, only do
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006693 * prefixes with a chop string and/or flags.
6694 * When a previously added affix had CIRCUMFIX this one
6695 * must have it too, if it had not then this one must not
6696 * have one either. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006697 regmatch.regprog = ae->ae_prog;
6698 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006699 if ((xht != NULL || !affile->af_pfxpostpone
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006700 || ae->ae_chop != NULL
6701 || ae->ae_flags != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006702 && (ae->ae_chop == NULL
6703 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006704 && (ae->ae_prog == NULL
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006705 || vim_regexec(&regmatch, word, (colnr_T)0))
6706 && (((condit & CONDIT_CFIX) == 0)
6707 == ((condit & CONDIT_AFF) == 0
6708 || ae->ae_flags == NULL
6709 || !flag_in_afflist(affile->af_flagtype,
6710 ae->ae_flags, affile->af_circumfix))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006711 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006712 /* Match. Remove the chop and add the affix. */
6713 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006714 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006715 /* prefix: chop/add at the start of the word */
6716 if (ae->ae_add == NULL)
6717 *newword = NUL;
6718 else
6719 STRCPY(newword, ae->ae_add);
6720 p = word;
6721 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006722 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006723 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006724#ifdef FEAT_MBYTE
6725 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006726 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006727 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006728 for ( ; i > 0; --i)
6729 mb_ptr_adv(p);
6730 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006731 else
6732#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006733 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006734 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006735 STRCAT(newword, p);
6736 }
6737 else
6738 {
6739 /* suffix: chop/add at the end of the word */
6740 STRCPY(newword, word);
6741 if (ae->ae_chop != NULL)
6742 {
6743 /* Remove chop string. */
6744 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006745 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006746 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006747 mb_ptr_back(newword, p);
6748 *p = NUL;
6749 }
6750 if (ae->ae_add != NULL)
6751 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006752 }
6753
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006754 use_flags = flags;
6755 use_pfxlist = pfxlist;
6756 use_pfxlen = pfxlen;
6757 need_affix = FALSE;
6758 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
6759 if (ae->ae_flags != NULL)
6760 {
6761 /* Extract flags from the affix list. */
6762 use_flags |= get_affix_flags(affile, ae->ae_flags);
6763
6764 if (affile->af_needaffix != 0 && flag_in_afflist(
6765 affile->af_flagtype, ae->ae_flags,
6766 affile->af_needaffix))
6767 need_affix = TRUE;
6768
6769 /* When there is a CIRCUMFIX flag the other affix
6770 * must also have it and we don't add the word
6771 * with one affix. */
6772 if (affile->af_circumfix != 0 && flag_in_afflist(
6773 affile->af_flagtype, ae->ae_flags,
6774 affile->af_circumfix))
6775 {
6776 use_condit |= CONDIT_CFIX;
6777 if ((condit & CONDIT_CFIX) == 0)
6778 need_affix = TRUE;
6779 }
6780
6781 if (affile->af_pfxpostpone
6782 || spin->si_compflags != NULL)
6783 {
6784 if (affile->af_pfxpostpone)
6785 /* Get prefix IDS from the affix list. */
6786 use_pfxlen = get_pfxlist(affile,
6787 ae->ae_flags, store_afflist);
6788 else
6789 use_pfxlen = 0;
6790 use_pfxlist = store_afflist;
6791
6792 /* Combine the prefix IDs. Avoid adding the
6793 * same ID twice. */
6794 for (i = 0; i < pfxlen; ++i)
6795 {
6796 for (j = 0; j < use_pfxlen; ++j)
6797 if (pfxlist[i] == use_pfxlist[j])
6798 break;
6799 if (j == use_pfxlen)
6800 use_pfxlist[use_pfxlen++] = pfxlist[i];
6801 }
6802
6803 if (spin->si_compflags != NULL)
6804 /* Get compound IDS from the affix list. */
6805 get_compflags(affile, ae->ae_flags,
6806 use_pfxlist + use_pfxlen);
6807
6808 /* Combine the list of compound flags.
6809 * Concatenate them to the prefix IDs list.
6810 * Avoid adding the same ID twice. */
6811 for (i = pfxlen; pfxlist[i] != NUL; ++i)
6812 {
6813 for (j = use_pfxlen;
6814 use_pfxlist[j] != NUL; ++j)
6815 if (pfxlist[i] == use_pfxlist[j])
6816 break;
6817 if (use_pfxlist[j] == NUL)
6818 {
6819 use_pfxlist[j++] = pfxlist[i];
6820 use_pfxlist[j] = NUL;
6821 }
6822 }
6823 }
6824 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00006825
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006826 /* Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006827 * use the compound flags. */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006828 if (use_pfxlist != NULL
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006829 && affile->af_compforbid != 0
6830 && ae->ae_flags != NULL
6831 && flag_in_afflist(
6832 affile->af_flagtype, ae->ae_flags,
6833 affile->af_compforbid))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006834 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006835 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006836 use_pfxlist = pfx_pfxlist;
6837 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006838
6839 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00006840 if (spin->si_prefroot != NULL
6841 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006842 {
6843 /* ... add a flag to indicate an affix was used. */
6844 use_flags |= WF_HAS_AFF;
6845
6846 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00006847 * affixes is not allowed. But do use the
6848 * compound flags after them. */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006849 if ((!ah->ah_combine || (condit & CONDIT_COMB))
6850 && use_pfxlist != NULL)
6851 use_pfxlist += use_pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006852 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006853
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006854 /* When compounding is supported and there is no
6855 * "COMPOUNDPERMITFLAG" then forbid compounding on the
6856 * side where the affix is applied. */
6857 if (spin->si_compflags != NULL
6858 && (affile->af_comppermit == 0
6859 || ae->ae_flags == NULL
6860 || !flag_in_afflist(
6861 affile->af_flagtype, ae->ae_flags,
6862 affile->af_comppermit)))
6863 {
6864 if (xht != NULL)
6865 use_flags |= WF_NOCOMPAFT;
6866 else
6867 use_flags |= WF_NOCOMPBEF;
6868 }
6869
Bram Moolenaar51485f02005-06-04 21:55:20 +00006870 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006871 if (store_word(spin, newword, use_flags,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006872 spin->si_region, use_pfxlist,
6873 need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006874 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006875
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006876 /* When added a prefix or a first suffix and the affix
6877 * has flags may add a(nother) suffix. RECURSIVE! */
6878 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
6879 if (store_aff_word(spin, newword, ae->ae_flags,
6880 affile, &affile->af_suff, xht,
6881 use_condit & (xht == NULL
6882 ? ~0 : ~CONDIT_SUF),
Bram Moolenaar5195e452005-08-19 20:32:47 +00006883 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006884 retval = FAIL;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006885
6886 /* When added a suffix and combining is allowed also
6887 * try adding a prefix additionally. Both for the
6888 * word flags and for the affix flags. RECURSIVE! */
6889 if (xht != NULL && ah->ah_combine)
6890 {
6891 if (store_aff_word(spin, newword,
6892 afflist, affile,
6893 xht, NULL, use_condit,
6894 use_flags, use_pfxlist,
6895 pfxlen) == FAIL
6896 || (ae->ae_flags != NULL
6897 && store_aff_word(spin, newword,
6898 ae->ae_flags, affile,
6899 xht, NULL, use_condit,
6900 use_flags, use_pfxlist,
6901 pfxlen) == FAIL))
6902 retval = FAIL;
6903 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006904 }
6905 }
6906 }
6907 }
6908 }
6909
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006910 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006911}
6912
6913/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006914 * Read a file with a list of words.
6915 */
6916 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006917spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006918 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006919 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006920{
6921 FILE *fd;
6922 long lnum = 0;
6923 char_u rline[MAXLINELEN];
6924 char_u *line;
6925 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006926 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006927 int l;
6928 int retval = OK;
6929 int did_word = FALSE;
6930 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006931 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006932 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006933
6934 /*
6935 * Open the file.
6936 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006937 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006938 if (fd == NULL)
6939 {
6940 EMSG2(_(e_notopen), fname);
6941 return FAIL;
6942 }
6943
Bram Moolenaar4770d092006-01-12 23:22:24 +00006944 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
6945 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006946
6947 /*
6948 * Read all the lines in the file one by one.
6949 */
6950 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
6951 {
6952 line_breakcheck();
6953 ++lnum;
6954
6955 /* Skip comment lines. */
6956 if (*rline == '#')
6957 continue;
6958
6959 /* Remove CR, LF and white space from the end. */
6960 l = STRLEN(rline);
6961 while (l > 0 && rline[l - 1] <= ' ')
6962 --l;
6963 if (l == 0)
6964 continue; /* empty or blank line */
6965 rline[l] = NUL;
6966
6967 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
6968 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006969#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00006970 if (spin->si_conv.vc_type != CONV_NONE)
6971 {
6972 pc = string_convert(&spin->si_conv, rline, NULL);
6973 if (pc == NULL)
6974 {
6975 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6976 fname, lnum, rline);
6977 continue;
6978 }
6979 line = pc;
6980 }
6981 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006982#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00006983 {
6984 pc = NULL;
6985 line = rline;
6986 }
6987
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006988 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00006989 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006990 ++line;
6991 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006992 {
6993 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006994 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
6995 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006996 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00006997 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
6998 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006999 else
7000 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007001#ifdef FEAT_MBYTE
7002 char_u *enc;
7003
Bram Moolenaar51485f02005-06-04 21:55:20 +00007004 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007005 line += 10;
7006 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007007 if (enc != NULL && !spin->si_ascii
7008 && convert_setup(&spin->si_conv, enc,
7009 p_enc) == FAIL)
7010 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00007011 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007012 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007013 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007014#else
7015 smsg((char_u *)_("Conversion in %s not supported"), fname);
7016#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007017 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007018 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007019 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007020
Bram Moolenaar3982c542005-06-08 21:56:31 +00007021 if (STRNCMP(line, "regions=", 8) == 0)
7022 {
7023 if (spin->si_region_count > 1)
7024 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
7025 fname, lnum, line);
7026 else
7027 {
7028 line += 8;
7029 if (STRLEN(line) > 16)
7030 smsg((char_u *)_("Too many regions in %s line %d: %s"),
7031 fname, lnum, line);
7032 else
7033 {
7034 spin->si_region_count = STRLEN(line) / 2;
7035 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007036
7037 /* Adjust the mask for a word valid in all regions. */
7038 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007039 }
7040 }
7041 continue;
7042 }
7043
Bram Moolenaar7887d882005-07-01 22:33:52 +00007044 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
7045 fname, lnum, line - 1);
7046 continue;
7047 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007048
Bram Moolenaar7887d882005-07-01 22:33:52 +00007049 flags = 0;
7050 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007051
Bram Moolenaar7887d882005-07-01 22:33:52 +00007052 /* Check for flags and region after a slash. */
7053 p = vim_strchr(line, '/');
7054 if (p != NULL)
7055 {
7056 *p++ = NUL;
7057 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007058 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007059 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007060 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007061 else if (*p == '!') /* Bad, bad, wicked word. */
7062 flags |= WF_BANNED;
7063 else if (*p == '?') /* Rare word. */
7064 flags |= WF_RARE;
7065 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007066 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007067 if ((flags & WF_REGION) == 0) /* first one */
7068 regionmask = 0;
7069 flags |= WF_REGION;
7070
7071 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00007072 if (l > spin->si_region_count)
7073 {
7074 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00007075 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007076 break;
7077 }
7078 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007079 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007080 else
7081 {
7082 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
7083 fname, lnum, p);
7084 break;
7085 }
7086 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007087 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007088 }
7089
7090 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
7091 if (spin->si_ascii && has_non_ascii(line))
7092 {
7093 ++non_ascii;
7094 continue;
7095 }
7096
7097 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007098 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007099 {
7100 retval = FAIL;
7101 break;
7102 }
7103 did_word = TRUE;
7104 }
7105
7106 vim_free(pc);
7107 fclose(fd);
7108
Bram Moolenaar4770d092006-01-12 23:22:24 +00007109 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007110 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007111 vim_snprintf((char *)IObuff, IOSIZE,
7112 _("Ignored %d words with non-ASCII characters"), non_ascii);
7113 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007114 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007115
Bram Moolenaar51485f02005-06-04 21:55:20 +00007116 return retval;
7117}
7118
7119/*
7120 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007121 * This avoids calling free() for every little struct we use (and keeping
7122 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00007123 * The memory is cleared to all zeros.
7124 * Returns NULL when out of memory.
7125 */
7126 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007127getroom(spin, len, align)
7128 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007129 size_t len; /* length needed */
7130 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007131{
7132 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007133 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007134
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007135 if (align && bl != NULL)
7136 /* Round size up for alignment. On some systems structures need to be
7137 * aligned to the size of a pointer (e.g., SPARC). */
7138 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
7139 & ~(sizeof(char *) - 1);
7140
Bram Moolenaar51485f02005-06-04 21:55:20 +00007141 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
7142 {
7143 /* Allocate a block of memory. This is not freed until much later. */
7144 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
7145 if (bl == NULL)
7146 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007147 bl->sb_next = spin->si_blocks;
7148 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007149 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007150 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007151 }
7152
7153 p = bl->sb_data + bl->sb_used;
7154 bl->sb_used += len;
7155
7156 return p;
7157}
7158
7159/*
7160 * Make a copy of a string into memory allocated with getroom().
7161 */
7162 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007163getroom_save(spin, s)
7164 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007165 char_u *s;
7166{
7167 char_u *sc;
7168
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007169 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007170 if (sc != NULL)
7171 STRCPY(sc, s);
7172 return sc;
7173}
7174
7175
7176/*
7177 * Free the list of allocated sblock_T.
7178 */
7179 static void
7180free_blocks(bl)
7181 sblock_T *bl;
7182{
7183 sblock_T *next;
7184
7185 while (bl != NULL)
7186 {
7187 next = bl->sb_next;
7188 vim_free(bl);
7189 bl = next;
7190 }
7191}
7192
7193/*
7194 * Allocate the root of a word tree.
7195 */
7196 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007197wordtree_alloc(spin)
7198 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007199{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007200 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007201}
7202
7203/*
7204 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007205 * Always store it in the case-folded tree. For a keep-case word this is
7206 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
7207 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007208 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007209 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
7210 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007211 */
7212 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00007213store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007214 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007215 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007216 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007217 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007218 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007219 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007220{
7221 int len = STRLEN(word);
7222 int ct = captype(word, word + len);
7223 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007224 int res = OK;
7225 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007226
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007227 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007228 for (p = pfxlist; res == OK; ++p)
7229 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007230 if (!need_affix || (p != NULL && *p != NUL))
7231 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007232 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007233 if (p == NULL || *p == NUL)
7234 break;
7235 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007236 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007237
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007238 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00007239 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007240 for (p = pfxlist; res == OK; ++p)
7241 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007242 if (!need_affix || (p != NULL && *p != NUL))
7243 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007244 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007245 if (p == NULL || *p == NUL)
7246 break;
7247 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007248 ++spin->si_keepwcount;
7249 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007250 return res;
7251}
7252
7253/*
7254 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00007255 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007256 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007257 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007258 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007259 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007260tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007261 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007262 char_u *word;
7263 wordnode_T *root;
7264 int flags;
7265 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007266 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007267{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007268 wordnode_T *node = root;
7269 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007270 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007271 wordnode_T **prev = NULL;
7272 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007273
Bram Moolenaar51485f02005-06-04 21:55:20 +00007274 /* Add each byte of the word to the tree, including the NUL at the end. */
7275 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007276 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007277 /* When there is more than one reference to this node we need to make
7278 * a copy, so that we can modify it. Copy the whole list of siblings
7279 * (we don't optimize for a partly shared list of siblings). */
7280 if (node != NULL && node->wn_refs > 1)
7281 {
7282 --node->wn_refs;
7283 copyprev = prev;
7284 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
7285 {
7286 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007287 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007288 if (np == NULL)
7289 return FAIL;
7290 np->wn_child = copyp->wn_child;
7291 if (np->wn_child != NULL)
7292 ++np->wn_child->wn_refs; /* child gets extra ref */
7293 np->wn_byte = copyp->wn_byte;
7294 if (np->wn_byte == NUL)
7295 {
7296 np->wn_flags = copyp->wn_flags;
7297 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007298 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007299 }
7300
7301 /* Link the new node in the list, there will be one ref. */
7302 np->wn_refs = 1;
7303 *copyprev = np;
7304 copyprev = &np->wn_sibling;
7305
7306 /* Let "node" point to the head of the copied list. */
7307 if (copyp == node)
7308 node = np;
7309 }
7310 }
7311
Bram Moolenaar51485f02005-06-04 21:55:20 +00007312 /* Look for the sibling that has the same character. They are sorted
7313 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007314 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007315 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007316 while (node != NULL
7317 && (node->wn_byte < word[i]
7318 || (node->wn_byte == NUL
7319 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007320 ? node->wn_affixID < (unsigned)affixID
7321 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007322 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007323 && (spin->si_sugtree
7324 ? (node->wn_region & 0xffff) < region
7325 : node->wn_affixID
7326 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007327 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007328 prev = &node->wn_sibling;
7329 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007330 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007331 if (node == NULL
7332 || node->wn_byte != word[i]
7333 || (word[i] == NUL
7334 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007335 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007336 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007337 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007338 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007339 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007340 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007341 if (np == NULL)
7342 return FAIL;
7343 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007344
7345 /* If "node" is NULL this is a new child or the end of the sibling
7346 * list: ref count is one. Otherwise use ref count of sibling and
7347 * make ref count of sibling one (matters when inserting in front
7348 * of the list of siblings). */
7349 if (node == NULL)
7350 np->wn_refs = 1;
7351 else
7352 {
7353 np->wn_refs = node->wn_refs;
7354 node->wn_refs = 1;
7355 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007356 *prev = np;
7357 np->wn_sibling = node;
7358 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007359 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007360
Bram Moolenaar51485f02005-06-04 21:55:20 +00007361 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007362 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007363 node->wn_flags = flags;
7364 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007365 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007366 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00007367 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007368 prev = &node->wn_child;
7369 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007370 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007371#ifdef SPELL_PRINTTREE
7372 smsg("Added \"%s\"", word);
7373 spell_print_tree(root->wn_sibling);
7374#endif
7375
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007376 /* count nr of words added since last message */
7377 ++spin->si_msg_count;
7378
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007379 if (spin->si_compress_cnt > 1)
7380 {
7381 if (--spin->si_compress_cnt == 1)
7382 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007383 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007384 }
7385
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007386 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007387 * When we have allocated lots of memory we need to compress the word tree
7388 * to free up some room. But compression is slow, and we might actually
7389 * need that room, thus only compress in the following situations:
7390 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00007391 * "compress_start" blocks.
7392 * 2. When compressed before and used "compress_inc" blocks before
7393 * adding "compress_added" words (si_compress_cnt > 1).
7394 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007395 * (si_compress_cnt == 1) and the number of free nodes drops below the
7396 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007397 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007398#ifndef SPELL_PRINTTREE
7399 if (spin->si_compress_cnt == 1
7400 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00007401 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007402#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007403 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007404 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00007405 * when the freed up room has been used and another "compress_inc"
7406 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007407 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007408 spin->si_blocks_cnt -= compress_inc;
7409 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007410
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007411 if (spin->si_verbose)
7412 {
7413 msg_start();
7414 msg_puts((char_u *)_(msg_compressing));
7415 msg_clr_eos();
7416 msg_didout = FALSE;
7417 msg_col = 0;
7418 out_flush();
7419 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007420
7421 /* Compress both trees. Either they both have many nodes, which makes
7422 * compression useful, or one of them is small, which means
Bram Moolenaar4770d092006-01-12 23:22:24 +00007423 * compression goes fast. But when filling the souldfold word tree
7424 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007425 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007426 if (affixID >= 0)
7427 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007428 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007429
7430 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007431}
7432
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007433/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007434 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7435 * Sets "sps_flags".
7436 */
7437 int
7438spell_check_msm()
7439{
7440 char_u *p = p_msm;
7441 long start = 0;
7442 long inc = 0;
7443 long added = 0;
7444
7445 if (!VIM_ISDIGIT(*p))
7446 return FAIL;
7447 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7448 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7449 if (*p != ',')
7450 return FAIL;
7451 ++p;
7452 if (!VIM_ISDIGIT(*p))
7453 return FAIL;
7454 inc = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
7455 if (*p != ',')
7456 return FAIL;
7457 ++p;
7458 if (!VIM_ISDIGIT(*p))
7459 return FAIL;
7460 added = getdigits(&p) * 1024;
7461 if (*p != NUL)
7462 return FAIL;
7463
7464 if (start == 0 || inc == 0 || added == 0 || inc > start)
7465 return FAIL;
7466
7467 compress_start = start;
7468 compress_inc = inc;
7469 compress_added = added;
7470 return OK;
7471}
7472
7473
7474/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007475 * Get a wordnode_T, either from the list of previously freed nodes or
7476 * allocate a new one.
7477 */
7478 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007479get_wordnode(spin)
7480 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007481{
7482 wordnode_T *n;
7483
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007484 if (spin->si_first_free == NULL)
7485 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007486 else
7487 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007488 n = spin->si_first_free;
7489 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007490 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007491 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007492 }
7493#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007494 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007495#endif
7496 return n;
7497}
7498
7499/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007500 * Decrement the reference count on a node (which is the head of a list of
7501 * siblings). If the reference count becomes zero free the node and its
7502 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007503 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007504 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007505 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007506deref_wordnode(spin, node)
7507 spellinfo_T *spin;
7508 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007509{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007510 wordnode_T *np;
7511 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007512
7513 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007514 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007515 for (np = node; np != NULL; np = np->wn_sibling)
7516 {
7517 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007518 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007519 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007520 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007521 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007522 ++cnt; /* length field */
7523 }
7524 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007525}
7526
7527/*
7528 * Free a wordnode_T for re-use later.
7529 * Only the "wn_child" field becomes invalid.
7530 */
7531 static void
7532free_wordnode(spin, n)
7533 spellinfo_T *spin;
7534 wordnode_T *n;
7535{
7536 n->wn_child = spin->si_first_free;
7537 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007538 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007539}
7540
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007541/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007542 * Compress a tree: find tails that are identical and can be shared.
7543 */
7544 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007545wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007546 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007547 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007548{
7549 hashtab_T ht;
7550 int n;
7551 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007552 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007553
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007554 /* Skip the root itself, it's not actually used. The first sibling is the
7555 * start of the tree. */
7556 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007557 {
7558 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007559 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007560
7561#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007562 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007563#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007564 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007565 if (tot > 1000000)
7566 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007567 else if (tot == 0)
7568 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007569 else
7570 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007571 vim_snprintf((char *)IObuff, IOSIZE,
7572 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7573 n, tot, tot - n, perc);
7574 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007575 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007576#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007577 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007578#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007579 hash_clear(&ht);
7580 }
7581}
7582
7583/*
7584 * Compress a node, its siblings and its children, depth first.
7585 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007586 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007587 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007588node_compress(spin, node, ht, tot)
7589 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007590 wordnode_T *node;
7591 hashtab_T *ht;
7592 int *tot; /* total count of nodes before compressing,
7593 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007594{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007595 wordnode_T *np;
7596 wordnode_T *tp;
7597 wordnode_T *child;
7598 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007599 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007600 int len = 0;
7601 unsigned nr, n;
7602 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007603
Bram Moolenaar51485f02005-06-04 21:55:20 +00007604 /*
7605 * Go through the list of siblings. Compress each child and then try
7606 * finding an identical child to replace it.
7607 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007608 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007609 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007610 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007611 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007612 ++len;
7613 if ((child = np->wn_child) != NULL)
7614 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007615 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007616 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007617
7618 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007619 hash = hash_hash(child->wn_u1.hashkey);
7620 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007621 if (!HASHITEM_EMPTY(hi))
7622 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007623 /* There are children we encountered before with a hash value
7624 * identical to the current child. Now check if there is one
7625 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007626 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007627 if (node_equal(child, tp))
7628 {
7629 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007630 * current one. This means the current child and all
7631 * its siblings is unlinked from the tree. */
7632 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007633 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007634 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007635 break;
7636 }
7637 if (tp == NULL)
7638 {
7639 /* No other child with this hash value equals the child of
7640 * the node, add it to the linked list after the first
7641 * item. */
7642 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007643 child->wn_u2.next = tp->wn_u2.next;
7644 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007645 }
7646 }
7647 else
7648 /* No other child has this hash value, add it to the
7649 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007650 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007651 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007652 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007653 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007654
7655 /*
7656 * Make a hash key for the node and its siblings, so that we can quickly
7657 * find a lookalike node. This must be done after compressing the sibling
7658 * list, otherwise the hash key would become invalid by the compression.
7659 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007660 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007661 nr = 0;
7662 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007663 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007664 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007665 /* end node: use wn_flags, wn_region and wn_affixID */
7666 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007667 else
7668 /* byte node: use the byte value and the child pointer */
7669 n = np->wn_byte + ((long_u)np->wn_child << 8);
7670 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007671 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007672
7673 /* Avoid NUL bytes, it terminates the hash key. */
7674 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007675 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007676 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007677 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007678 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007679 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007680 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007681 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7682 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007683
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007684 /* Check for CTRL-C pressed now and then. */
7685 fast_breakcheck();
7686
Bram Moolenaar51485f02005-06-04 21:55:20 +00007687 return compressed;
7688}
7689
7690/*
7691 * Return TRUE when two nodes have identical siblings and children.
7692 */
7693 static int
7694node_equal(n1, n2)
7695 wordnode_T *n1;
7696 wordnode_T *n2;
7697{
7698 wordnode_T *p1;
7699 wordnode_T *p2;
7700
7701 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7702 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7703 if (p1->wn_byte != p2->wn_byte
7704 || (p1->wn_byte == NUL
7705 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007706 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007707 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007708 : (p1->wn_child != p2->wn_child)))
7709 break;
7710
7711 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007712}
7713
7714/*
7715 * Write a number to file "fd", MSB first, in "len" bytes.
7716 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007717 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007718put_bytes(fd, nr, len)
7719 FILE *fd;
7720 long_u nr;
7721 int len;
7722{
7723 int i;
7724
7725 for (i = len - 1; i >= 0; --i)
7726 putc((int)(nr >> (i * 8)), fd);
7727}
7728
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007729#ifdef _MSC_VER
7730# if (_MSC_VER <= 1200)
7731/* This line is required for VC6 without the service pack. Also see the
7732 * matching #pragma below. */
7733/* # pragma optimize("", off) */
7734# endif
7735#endif
7736
Bram Moolenaar4770d092006-01-12 23:22:24 +00007737/*
7738 * Write spin->si_sugtime to file "fd".
7739 */
7740 static void
7741put_sugtime(spin, fd)
7742 spellinfo_T *spin;
7743 FILE *fd;
7744{
7745 int c;
7746 int i;
7747
7748 /* time_t can be up to 8 bytes in size, more than long_u, thus we
7749 * can't use put_bytes() here. */
7750 for (i = 7; i >= 0; --i)
7751 if (i + 1 > sizeof(time_t))
7752 /* ">>" doesn't work well when shifting more bits than avail */
7753 putc(0, fd);
7754 else
7755 {
7756 c = (unsigned)spin->si_sugtime >> (i * 8);
7757 putc(c, fd);
7758 }
7759}
7760
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007761#ifdef _MSC_VER
7762# if (_MSC_VER <= 1200)
7763/* # pragma optimize("", on) */
7764# endif
7765#endif
7766
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007767static int
7768#ifdef __BORLANDC__
7769_RTLENTRYF
7770#endif
7771rep_compare __ARGS((const void *s1, const void *s2));
7772
7773/*
7774 * Function given to qsort() to sort the REP items on "from" string.
7775 */
7776 static int
7777#ifdef __BORLANDC__
7778_RTLENTRYF
7779#endif
7780rep_compare(s1, s2)
7781 const void *s1;
7782 const void *s2;
7783{
7784 fromto_T *p1 = (fromto_T *)s1;
7785 fromto_T *p2 = (fromto_T *)s2;
7786
7787 return STRCMP(p1->ft_from, p2->ft_from);
7788}
7789
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007790/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007791 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007792 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007793 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007794 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007795write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007796 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007797 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007798{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007799 FILE *fd;
7800 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007801 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007802 wordnode_T *tree;
7803 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007804 int i;
7805 int l;
7806 garray_T *gap;
7807 fromto_T *ftp;
7808 char_u *p;
7809 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007810 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007811
Bram Moolenaarb765d632005-06-07 21:00:02 +00007812 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007813 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007814 {
7815 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007816 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007817 }
7818
Bram Moolenaar5195e452005-08-19 20:32:47 +00007819 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007820 /* <fileID> */
7821 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007822 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007823 EMSG(_(e_write));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007824 retval = FAIL;
7825 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007826 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007827
Bram Moolenaar5195e452005-08-19 20:32:47 +00007828 /*
7829 * <SECTIONS>: <section> ... <sectionend>
7830 */
7831
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007832 /* SN_INFO: <infotext> */
7833 if (spin->si_info != NULL)
7834 {
7835 putc(SN_INFO, fd); /* <sectionID> */
7836 putc(0, fd); /* <sectionflags> */
7837
7838 i = STRLEN(spin->si_info);
7839 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
7840 fwrite(spin->si_info, (size_t)i, (size_t)1, fd); /* <infotext> */
7841 }
7842
Bram Moolenaar5195e452005-08-19 20:32:47 +00007843 /* SN_REGION: <regionname> ...
7844 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007845 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007846 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007847 putc(SN_REGION, fd); /* <sectionID> */
7848 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7849 l = spin->si_region_count * 2;
7850 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7851 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
7852 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007853 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007854 }
7855 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00007856 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007857
Bram Moolenaar5195e452005-08-19 20:32:47 +00007858 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
7859 *
7860 * The table with character flags and the table for case folding.
7861 * This makes sure the same characters are recognized as word characters
7862 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007863 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007864 * 'encoding'.
7865 * Also skip this for an .add.spl file, the main spell file must contain
7866 * the table (avoids that it conflicts). File is shorter too.
7867 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007868 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007869 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007870 char_u folchars[128 * 8];
7871 int flags;
7872
Bram Moolenaard12a1322005-08-21 22:08:24 +00007873 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007874 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7875
7876 /* Form the <folchars> string first, we need to know its length. */
7877 l = 0;
7878 for (i = 128; i < 256; ++i)
7879 {
7880#ifdef FEAT_MBYTE
7881 if (has_mbyte)
7882 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
7883 else
7884#endif
7885 folchars[l++] = spelltab.st_fold[i];
7886 }
7887 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
7888
7889 fputc(128, fd); /* <charflagslen> */
7890 for (i = 128; i < 256; ++i)
7891 {
7892 flags = 0;
7893 if (spelltab.st_isw[i])
7894 flags |= CF_WORD;
7895 if (spelltab.st_isu[i])
7896 flags |= CF_UPPER;
7897 fputc(flags, fd); /* <charflags> */
7898 }
7899
7900 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
7901 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007902 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007903
Bram Moolenaar5195e452005-08-19 20:32:47 +00007904 /* SN_MIDWORD: <midword> */
7905 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007906 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007907 putc(SN_MIDWORD, fd); /* <sectionID> */
7908 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7909
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007910 i = STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007911 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007912 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
7913 }
7914
Bram Moolenaar5195e452005-08-19 20:32:47 +00007915 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
7916 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007917 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007918 putc(SN_PREFCOND, fd); /* <sectionID> */
7919 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7920
7921 l = write_spell_prefcond(NULL, &spin->si_prefcond);
7922 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7923
7924 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007925 }
7926
Bram Moolenaar5195e452005-08-19 20:32:47 +00007927 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00007928 * SN_SAL: <salflags> <salcount> <sal> ...
7929 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007930
Bram Moolenaar5195e452005-08-19 20:32:47 +00007931 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00007932 * round 2: SN_SAL section (unless SN_SOFO is used)
7933 * round 3: SN_REPSAL section */
7934 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007935 {
7936 if (round == 1)
7937 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007938 else if (round == 2)
7939 {
7940 /* Don't write SN_SAL when using a SN_SOFO section */
7941 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
7942 continue;
7943 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007944 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007945 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00007946 gap = &spin->si_repsal;
7947
7948 /* Don't write the section if there are no items. */
7949 if (gap->ga_len == 0)
7950 continue;
7951
7952 /* Sort the REP/REPSAL items. */
7953 if (round != 2)
7954 qsort(gap->ga_data, (size_t)gap->ga_len,
7955 sizeof(fromto_T), rep_compare);
7956
7957 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
7958 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007959
Bram Moolenaar5195e452005-08-19 20:32:47 +00007960 /* This is for making suggestions, section is not required. */
7961 putc(0, fd); /* <sectionflags> */
7962
7963 /* Compute the length of what follows. */
7964 l = 2; /* count <repcount> or <salcount> */
7965 for (i = 0; i < gap->ga_len; ++i)
7966 {
7967 ftp = &((fromto_T *)gap->ga_data)[i];
7968 l += 1 + STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
7969 l += 1 + STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
7970 }
7971 if (round == 2)
7972 ++l; /* count <salflags> */
7973 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7974
7975 if (round == 2)
7976 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007977 i = 0;
7978 if (spin->si_followup)
7979 i |= SAL_F0LLOWUP;
7980 if (spin->si_collapse)
7981 i |= SAL_COLLAPSE;
7982 if (spin->si_rem_accents)
7983 i |= SAL_REM_ACCENTS;
7984 putc(i, fd); /* <salflags> */
7985 }
7986
7987 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
7988 for (i = 0; i < gap->ga_len; ++i)
7989 {
7990 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
7991 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
7992 ftp = &((fromto_T *)gap->ga_data)[i];
7993 for (rr = 1; rr <= 2; ++rr)
7994 {
7995 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
7996 l = STRLEN(p);
7997 putc(l, fd);
7998 fwrite(p, l, (size_t)1, fd);
7999 }
8000 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008001
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008002 }
8003
Bram Moolenaar5195e452005-08-19 20:32:47 +00008004 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
8005 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008006 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8007 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008008 putc(SN_SOFO, fd); /* <sectionID> */
8009 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008010
8011 l = STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008012 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
8013 /* <sectionlen> */
8014
8015 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
8016 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008017
8018 l = STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008019 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
8020 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008021 }
8022
Bram Moolenaar4770d092006-01-12 23:22:24 +00008023 /* SN_WORDS: <word> ...
8024 * This is for making suggestions, section is not required. */
8025 if (spin->si_commonwords.ht_used > 0)
8026 {
8027 putc(SN_WORDS, fd); /* <sectionID> */
8028 putc(0, fd); /* <sectionflags> */
8029
8030 /* round 1: count the bytes
8031 * round 2: write the bytes */
8032 for (round = 1; round <= 2; ++round)
8033 {
8034 int todo;
8035 int len = 0;
8036 hashitem_T *hi;
8037
8038 todo = spin->si_commonwords.ht_used;
8039 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
8040 if (!HASHITEM_EMPTY(hi))
8041 {
8042 l = STRLEN(hi->hi_key) + 1;
8043 len += l;
8044 if (round == 2) /* <word> */
8045 fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
8046 --todo;
8047 }
8048 if (round == 1)
8049 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
8050 }
8051 }
8052
Bram Moolenaar5195e452005-08-19 20:32:47 +00008053 /* SN_MAP: <mapstr>
8054 * This is for making suggestions, section is not required. */
8055 if (spin->si_map.ga_len > 0)
8056 {
8057 putc(SN_MAP, fd); /* <sectionID> */
8058 putc(0, fd); /* <sectionflags> */
8059 l = spin->si_map.ga_len;
8060 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8061 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
8062 /* <mapstr> */
8063 }
8064
Bram Moolenaar4770d092006-01-12 23:22:24 +00008065 /* SN_SUGFILE: <timestamp>
8066 * This is used to notify that a .sug file may be available and at the
8067 * same time allows for checking that a .sug file that is found matches
8068 * with this .spl file. That's because the word numbers must be exactly
8069 * right. */
8070 if (!spin->si_nosugfile
8071 && (spin->si_sal.ga_len > 0
8072 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
8073 {
8074 putc(SN_SUGFILE, fd); /* <sectionID> */
8075 putc(0, fd); /* <sectionflags> */
8076 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
8077
8078 /* Set si_sugtime and write it to the file. */
8079 spin->si_sugtime = time(NULL);
8080 put_sugtime(spin, fd); /* <timestamp> */
8081 }
8082
Bram Moolenaare1438bb2006-03-01 22:01:55 +00008083 /* SN_NOSPLITSUGS: nothing
8084 * This is used to notify that no suggestions with word splits are to be
8085 * made. */
8086 if (spin->si_nosplitsugs)
8087 {
8088 putc(SN_NOSPLITSUGS, fd); /* <sectionID> */
8089 putc(0, fd); /* <sectionflags> */
8090 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8091 }
8092
Bram Moolenaar5195e452005-08-19 20:32:47 +00008093 /* SN_COMPOUND: compound info.
8094 * We don't mark it required, when not supported all compound words will
8095 * be bad words. */
8096 if (spin->si_compflags != NULL)
8097 {
8098 putc(SN_COMPOUND, fd); /* <sectionID> */
8099 putc(0, fd); /* <sectionflags> */
8100
8101 l = STRLEN(spin->si_compflags);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008102 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8103 l += STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
8104 put_bytes(fd, (long_u)(l + 7), 4); /* <sectionlen> */
8105
Bram Moolenaar5195e452005-08-19 20:32:47 +00008106 putc(spin->si_compmax, fd); /* <compmax> */
8107 putc(spin->si_compminlen, fd); /* <compminlen> */
8108 putc(spin->si_compsylmax, fd); /* <compsylmax> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008109 putc(0, fd); /* for Vim 7.0b compatibility */
8110 putc(spin->si_compoptions, fd); /* <compoptions> */
8111 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
8112 /* <comppatcount> */
8113 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8114 {
8115 p = ((char_u **)(spin->si_comppat.ga_data))[i];
8116 putc(STRLEN(p), fd); /* <comppatlen> */
8117 fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);/* <comppattext> */
8118 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008119 /* <compflags> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008120 fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
8121 (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008122 }
8123
Bram Moolenaar78622822005-08-23 21:00:13 +00008124 /* SN_NOBREAK: NOBREAK flag */
8125 if (spin->si_nobreak)
8126 {
8127 putc(SN_NOBREAK, fd); /* <sectionID> */
8128 putc(0, fd); /* <sectionflags> */
8129
8130 /* It's empty, the precense of the section flags the feature. */
8131 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8132 }
8133
Bram Moolenaar5195e452005-08-19 20:32:47 +00008134 /* SN_SYLLABLE: syllable info.
8135 * We don't mark it required, when not supported syllables will not be
8136 * counted. */
8137 if (spin->si_syllable != NULL)
8138 {
8139 putc(SN_SYLLABLE, fd); /* <sectionID> */
8140 putc(0, fd); /* <sectionflags> */
8141
8142 l = STRLEN(spin->si_syllable);
8143 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8144 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
8145 }
8146
8147 /* end of <SECTIONS> */
8148 putc(SN_END, fd); /* <sectionend> */
8149
Bram Moolenaar50cde822005-06-05 21:54:54 +00008150
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008151 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008152 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008153 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008154 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008155 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008156 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008157 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008158 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008159 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008160 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008161 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008162 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008163
Bram Moolenaar0c405862005-06-22 22:26:26 +00008164 /* Clear the index and wnode fields in the tree. */
8165 clear_node(tree);
8166
Bram Moolenaar51485f02005-06-04 21:55:20 +00008167 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00008168 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00008169 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008170 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008171
Bram Moolenaar51485f02005-06-04 21:55:20 +00008172 /* number of nodes in 4 bytes */
8173 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00008174 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008175
Bram Moolenaar51485f02005-06-04 21:55:20 +00008176 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008177 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008178 }
8179
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008180 /* Write another byte to check for errors. */
8181 if (putc(0, fd) == EOF)
8182 retval = FAIL;
8183
8184 if (fclose(fd) == EOF)
8185 retval = FAIL;
8186
8187 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008188}
8189
8190/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008191 * Clear the index and wnode fields of "node", it siblings and its
8192 * children. This is needed because they are a union with other items to save
8193 * space.
8194 */
8195 static void
8196clear_node(node)
8197 wordnode_T *node;
8198{
8199 wordnode_T *np;
8200
8201 if (node != NULL)
8202 for (np = node; np != NULL; np = np->wn_sibling)
8203 {
8204 np->wn_u1.index = 0;
8205 np->wn_u2.wnode = NULL;
8206
8207 if (np->wn_byte != NUL)
8208 clear_node(np->wn_child);
8209 }
8210}
8211
8212
8213/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008214 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008215 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00008216 * This first writes the list of possible bytes (siblings). Then for each
8217 * byte recursively write the children.
8218 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00008219 * NOTE: The code here must match the code in read_tree_node(), since
8220 * assumptions are made about the indexes (so that we don't have to write them
8221 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00008222 *
8223 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008224 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008225 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00008226put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008227 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008228 wordnode_T *node;
8229 int index;
8230 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008231 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008232{
Bram Moolenaar51485f02005-06-04 21:55:20 +00008233 int newindex = index;
8234 int siblingcount = 0;
8235 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008236 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008237
Bram Moolenaar51485f02005-06-04 21:55:20 +00008238 /* If "node" is zero the tree is empty. */
8239 if (node == NULL)
8240 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008241
Bram Moolenaar51485f02005-06-04 21:55:20 +00008242 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008243 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008244
8245 /* Count the number of siblings. */
8246 for (np = node; np != NULL; np = np->wn_sibling)
8247 ++siblingcount;
8248
8249 /* Write the sibling count. */
8250 if (fd != NULL)
8251 putc(siblingcount, fd); /* <siblingcount> */
8252
8253 /* Write each sibling byte and optionally extra info. */
8254 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008255 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008256 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008257 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008258 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008259 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008260 /* For a NUL byte (end of word) write the flags etc. */
8261 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008262 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008263 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008264 * associated condition nr (stored in wn_region). The
8265 * byte value is misused to store the "rare" and "not
8266 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008267 if (np->wn_flags == (short_u)PFX_FLAGS)
8268 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008269 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00008270 {
8271 putc(BY_FLAGS, fd); /* <byte> */
8272 putc(np->wn_flags, fd); /* <pflags> */
8273 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008274 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008275 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008276 }
8277 else
8278 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008279 /* For word trees we write the flag/region items. */
8280 flags = np->wn_flags;
8281 if (regionmask != 0 && np->wn_region != regionmask)
8282 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008283 if (np->wn_affixID != 0)
8284 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008285 if (flags == 0)
8286 {
8287 /* word without flags or region */
8288 putc(BY_NOFLAGS, fd); /* <byte> */
8289 }
8290 else
8291 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008292 if (np->wn_flags >= 0x100)
8293 {
8294 putc(BY_FLAGS2, fd); /* <byte> */
8295 putc(flags, fd); /* <flags> */
8296 putc((unsigned)flags >> 8, fd); /* <flags2> */
8297 }
8298 else
8299 {
8300 putc(BY_FLAGS, fd); /* <byte> */
8301 putc(flags, fd); /* <flags> */
8302 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008303 if (flags & WF_REGION)
8304 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008305 if (flags & WF_AFX)
8306 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008307 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008308 }
8309 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008310 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008311 else
8312 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008313 if (np->wn_child->wn_u1.index != 0
8314 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008315 {
8316 /* The child is written elsewhere, write the reference. */
8317 if (fd != NULL)
8318 {
8319 putc(BY_INDEX, fd); /* <byte> */
8320 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008321 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008322 }
8323 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008324 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008325 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008326 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008327
Bram Moolenaar51485f02005-06-04 21:55:20 +00008328 if (fd != NULL)
8329 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
8330 {
8331 EMSG(_(e_write));
8332 return 0;
8333 }
8334 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008335 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008336
8337 /* Space used in the array when reading: one for each sibling and one for
8338 * the count. */
8339 newindex += siblingcount + 1;
8340
8341 /* Recursively dump the children of each sibling. */
8342 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008343 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
8344 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008345 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008346
8347 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008348}
8349
8350
8351/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008352 * ":mkspell [-ascii] outfile infile ..."
8353 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008354 */
8355 void
8356ex_mkspell(eap)
8357 exarg_T *eap;
8358{
8359 int fcount;
8360 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008361 char_u *arg = eap->arg;
8362 int ascii = FALSE;
8363
8364 if (STRNCMP(arg, "-ascii", 6) == 0)
8365 {
8366 ascii = TRUE;
8367 arg = skipwhite(arg + 6);
8368 }
8369
8370 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
8371 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
8372 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008373 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008374 FreeWild(fcount, fnames);
8375 }
8376}
8377
8378/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00008379 * Create the .sug file.
8380 * Uses the soundfold info in "spin".
8381 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
8382 */
8383 static void
8384spell_make_sugfile(spin, wfname)
8385 spellinfo_T *spin;
8386 char_u *wfname;
8387{
8388 char_u fname[MAXPATHL];
8389 int len;
8390 slang_T *slang;
8391 int free_slang = FALSE;
8392
8393 /*
8394 * Read back the .spl file that was written. This fills the required
8395 * info for soundfolding. This also uses less memory than the
8396 * pointer-linked version of the trie. And it avoids having two versions
8397 * of the code for the soundfolding stuff.
8398 * It might have been done already by spell_reload_one().
8399 */
8400 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
8401 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
8402 break;
8403 if (slang == NULL)
8404 {
8405 spell_message(spin, (char_u *)_("Reading back spell file..."));
8406 slang = spell_load_file(wfname, NULL, NULL, FALSE);
8407 if (slang == NULL)
8408 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008409 free_slang = TRUE;
8410 }
8411
8412 /*
8413 * Clear the info in "spin" that is used.
8414 */
8415 spin->si_blocks = NULL;
8416 spin->si_blocks_cnt = 0;
8417 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
8418 spin->si_free_count = 0;
8419 spin->si_first_free = NULL;
8420 spin->si_foldwcount = 0;
8421
8422 /*
8423 * Go through the trie of good words, soundfold each word and add it to
8424 * the soundfold trie.
8425 */
8426 spell_message(spin, (char_u *)_("Performing soundfolding..."));
8427 if (sug_filltree(spin, slang) == FAIL)
8428 goto theend;
8429
8430 /*
8431 * Create the table which links each soundfold word with a list of the
8432 * good words it may come from. Creates buffer "spin->si_spellbuf".
8433 * This also removes the wordnr from the NUL byte entries to make
8434 * compression possible.
8435 */
8436 if (sug_maketable(spin) == FAIL)
8437 goto theend;
8438
8439 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8440 (long)spin->si_spellbuf->b_ml.ml_line_count);
8441
8442 /*
8443 * Compress the soundfold trie.
8444 */
8445 spell_message(spin, (char_u *)_(msg_compressing));
8446 wordtree_compress(spin, spin->si_foldroot);
8447
8448 /*
8449 * Write the .sug file.
8450 * Make the file name by changing ".spl" to ".sug".
8451 */
8452 STRCPY(fname, wfname);
8453 len = STRLEN(fname);
8454 fname[len - 2] = 'u';
8455 fname[len - 1] = 'g';
8456 sug_write(spin, fname);
8457
8458theend:
8459 if (free_slang)
8460 slang_free(slang);
8461 free_blocks(spin->si_blocks);
8462 close_spellbuf(spin->si_spellbuf);
8463}
8464
8465/*
8466 * Build the soundfold trie for language "slang".
8467 */
8468 static int
8469sug_filltree(spin, slang)
8470 spellinfo_T *spin;
8471 slang_T *slang;
8472{
8473 char_u *byts;
8474 idx_T *idxs;
8475 int depth;
8476 idx_T arridx[MAXWLEN];
8477 int curi[MAXWLEN];
8478 char_u tword[MAXWLEN];
8479 char_u tsalword[MAXWLEN];
8480 int c;
8481 idx_T n;
8482 unsigned words_done = 0;
8483 int wordcount[MAXWLEN];
8484
8485 /* We use si_foldroot for the souldfolded trie. */
8486 spin->si_foldroot = wordtree_alloc(spin);
8487 if (spin->si_foldroot == NULL)
8488 return FAIL;
8489
8490 /* let tree_add_word() know we're adding to the soundfolded tree */
8491 spin->si_sugtree = TRUE;
8492
8493 /*
8494 * Go through the whole case-folded tree, soundfold each word and put it
8495 * in the trie.
8496 */
8497 byts = slang->sl_fbyts;
8498 idxs = slang->sl_fidxs;
8499
8500 arridx[0] = 0;
8501 curi[0] = 1;
8502 wordcount[0] = 0;
8503
8504 depth = 0;
8505 while (depth >= 0 && !got_int)
8506 {
8507 if (curi[depth] > byts[arridx[depth]])
8508 {
8509 /* Done all bytes at this node, go up one level. */
8510 idxs[arridx[depth]] = wordcount[depth];
8511 if (depth > 0)
8512 wordcount[depth - 1] += wordcount[depth];
8513
8514 --depth;
8515 line_breakcheck();
8516 }
8517 else
8518 {
8519
8520 /* Do one more byte at this node. */
8521 n = arridx[depth] + curi[depth];
8522 ++curi[depth];
8523
8524 c = byts[n];
8525 if (c == 0)
8526 {
8527 /* Sound-fold the word. */
8528 tword[depth] = NUL;
8529 spell_soundfold(slang, tword, TRUE, tsalword);
8530
8531 /* We use the "flags" field for the MSB of the wordnr,
8532 * "region" for the LSB of the wordnr. */
8533 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8534 words_done >> 16, words_done & 0xffff,
8535 0) == FAIL)
8536 return FAIL;
8537
8538 ++words_done;
8539 ++wordcount[depth];
8540
8541 /* Reset the block count each time to avoid compression
8542 * kicking in. */
8543 spin->si_blocks_cnt = 0;
8544
8545 /* Skip over any other NUL bytes (same word with different
8546 * flags). */
8547 while (byts[n + 1] == 0)
8548 {
8549 ++n;
8550 ++curi[depth];
8551 }
8552 }
8553 else
8554 {
8555 /* Normal char, go one level deeper. */
8556 tword[depth++] = c;
8557 arridx[depth] = idxs[n];
8558 curi[depth] = 1;
8559 wordcount[depth] = 0;
8560 }
8561 }
8562 }
8563
8564 smsg((char_u *)_("Total number of words: %d"), words_done);
8565
8566 return OK;
8567}
8568
8569/*
8570 * Make the table that links each word in the soundfold trie to the words it
8571 * can be produced from.
8572 * This is not unlike lines in a file, thus use a memfile to be able to access
8573 * the table efficiently.
8574 * Returns FAIL when out of memory.
8575 */
8576 static int
8577sug_maketable(spin)
8578 spellinfo_T *spin;
8579{
8580 garray_T ga;
8581 int res = OK;
8582
8583 /* Allocate a buffer, open a memline for it and create the swap file
8584 * (uses a temp file, not a .swp file). */
8585 spin->si_spellbuf = open_spellbuf();
8586 if (spin->si_spellbuf == NULL)
8587 return FAIL;
8588
8589 /* Use a buffer to store the line info, avoids allocating many small
8590 * pieces of memory. */
8591 ga_init2(&ga, 1, 100);
8592
8593 /* recursively go through the tree */
8594 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8595 res = FAIL;
8596
8597 ga_clear(&ga);
8598 return res;
8599}
8600
8601/*
8602 * Fill the table for one node and its children.
8603 * Returns the wordnr at the start of the node.
8604 * Returns -1 when out of memory.
8605 */
8606 static int
8607sug_filltable(spin, node, startwordnr, gap)
8608 spellinfo_T *spin;
8609 wordnode_T *node;
8610 int startwordnr;
8611 garray_T *gap; /* place to store line of numbers */
8612{
8613 wordnode_T *p, *np;
8614 int wordnr = startwordnr;
8615 int nr;
8616 int prev_nr;
8617
8618 for (p = node; p != NULL; p = p->wn_sibling)
8619 {
8620 if (p->wn_byte == NUL)
8621 {
8622 gap->ga_len = 0;
8623 prev_nr = 0;
8624 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8625 {
8626 if (ga_grow(gap, 10) == FAIL)
8627 return -1;
8628
8629 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8630 /* Compute the offset from the previous nr and store the
8631 * offset in a way that it takes a minimum number of bytes.
8632 * It's a bit like utf-8, but without the need to mark
8633 * following bytes. */
8634 nr -= prev_nr;
8635 prev_nr += nr;
8636 gap->ga_len += offset2bytes(nr,
8637 (char_u *)gap->ga_data + gap->ga_len);
8638 }
8639
8640 /* add the NUL byte */
8641 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8642
8643 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8644 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8645 return -1;
8646 ++wordnr;
8647
8648 /* Remove extra NUL entries, we no longer need them. We don't
8649 * bother freeing the nodes, the won't be reused anyway. */
8650 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8651 p->wn_sibling = p->wn_sibling->wn_sibling;
8652
8653 /* Clear the flags on the remaining NUL node, so that compression
8654 * works a lot better. */
8655 p->wn_flags = 0;
8656 p->wn_region = 0;
8657 }
8658 else
8659 {
8660 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8661 if (wordnr == -1)
8662 return -1;
8663 }
8664 }
8665 return wordnr;
8666}
8667
8668/*
8669 * Convert an offset into a minimal number of bytes.
8670 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8671 * bytes.
8672 */
8673 static int
8674offset2bytes(nr, buf)
8675 int nr;
8676 char_u *buf;
8677{
8678 int rem;
8679 int b1, b2, b3, b4;
8680
8681 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8682 b1 = nr % 255 + 1;
8683 rem = nr / 255;
8684 b2 = rem % 255 + 1;
8685 rem = rem / 255;
8686 b3 = rem % 255 + 1;
8687 b4 = rem / 255 + 1;
8688
8689 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8690 {
8691 buf[0] = 0xe0 + b4;
8692 buf[1] = b3;
8693 buf[2] = b2;
8694 buf[3] = b1;
8695 return 4;
8696 }
8697 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8698 {
8699 buf[0] = 0xc0 + b3;
8700 buf[1] = b2;
8701 buf[2] = b1;
8702 return 3;
8703 }
8704 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8705 {
8706 buf[0] = 0x80 + b2;
8707 buf[1] = b1;
8708 return 2;
8709 }
8710 /* 1 byte */
8711 buf[0] = b1;
8712 return 1;
8713}
8714
8715/*
8716 * Opposite of offset2bytes().
8717 * "pp" points to the bytes and is advanced over it.
8718 * Returns the offset.
8719 */
8720 static int
8721bytes2offset(pp)
8722 char_u **pp;
8723{
8724 char_u *p = *pp;
8725 int nr;
8726 int c;
8727
8728 c = *p++;
8729 if ((c & 0x80) == 0x00) /* 1 byte */
8730 {
8731 nr = c - 1;
8732 }
8733 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8734 {
8735 nr = (c & 0x3f) - 1;
8736 nr = nr * 255 + (*p++ - 1);
8737 }
8738 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8739 {
8740 nr = (c & 0x1f) - 1;
8741 nr = nr * 255 + (*p++ - 1);
8742 nr = nr * 255 + (*p++ - 1);
8743 }
8744 else /* 4 bytes */
8745 {
8746 nr = (c & 0x0f) - 1;
8747 nr = nr * 255 + (*p++ - 1);
8748 nr = nr * 255 + (*p++ - 1);
8749 nr = nr * 255 + (*p++ - 1);
8750 }
8751
8752 *pp = p;
8753 return nr;
8754}
8755
8756/*
8757 * Write the .sug file in "fname".
8758 */
8759 static void
8760sug_write(spin, fname)
8761 spellinfo_T *spin;
8762 char_u *fname;
8763{
8764 FILE *fd;
8765 wordnode_T *tree;
8766 int nodecount;
8767 int wcount;
8768 char_u *line;
8769 linenr_T lnum;
8770 int len;
8771
8772 /* Create the file. Note that an existing file is silently overwritten! */
8773 fd = mch_fopen((char *)fname, "w");
8774 if (fd == NULL)
8775 {
8776 EMSG2(_(e_notopen), fname);
8777 return;
8778 }
8779
8780 vim_snprintf((char *)IObuff, IOSIZE,
8781 _("Writing suggestion file %s ..."), fname);
8782 spell_message(spin, IObuff);
8783
8784 /*
8785 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8786 */
8787 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
8788 {
8789 EMSG(_(e_write));
8790 goto theend;
8791 }
8792 putc(VIMSUGVERSION, fd); /* <versionnr> */
8793
8794 /* Write si_sugtime to the file. */
8795 put_sugtime(spin, fd); /* <timestamp> */
8796
8797 /*
8798 * <SUGWORDTREE>
8799 */
8800 spin->si_memtot = 0;
8801 tree = spin->si_foldroot->wn_sibling;
8802
8803 /* Clear the index and wnode fields in the tree. */
8804 clear_node(tree);
8805
8806 /* Count the number of nodes. Needed to be able to allocate the
8807 * memory when reading the nodes. Also fills in index for shared
8808 * nodes. */
8809 nodecount = put_node(NULL, tree, 0, 0, FALSE);
8810
8811 /* number of nodes in 4 bytes */
8812 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
8813 spin->si_memtot += nodecount + nodecount * sizeof(int);
8814
8815 /* Write the nodes. */
8816 (void)put_node(fd, tree, 0, 0, FALSE);
8817
8818 /*
8819 * <SUGTABLE>: <sugwcount> <sugline> ...
8820 */
8821 wcount = spin->si_spellbuf->b_ml.ml_line_count;
8822 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
8823
8824 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
8825 {
8826 /* <sugline>: <sugnr> ... NUL */
8827 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
8828 len = STRLEN(line) + 1;
8829 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
8830 {
8831 EMSG(_(e_write));
8832 goto theend;
8833 }
8834 spin->si_memtot += len;
8835 }
8836
8837 /* Write another byte to check for errors. */
8838 if (putc(0, fd) == EOF)
8839 EMSG(_(e_write));
8840
8841 vim_snprintf((char *)IObuff, IOSIZE,
8842 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
8843 spell_message(spin, IObuff);
8844
8845theend:
8846 /* close the file */
8847 fclose(fd);
8848}
8849
8850/*
8851 * Open a spell buffer. This is a nameless buffer that is not in the buffer
8852 * list and only contains text lines. Can use a swapfile to reduce memory
8853 * use.
8854 * Most other fields are invalid! Esp. watch out for string options being
8855 * NULL and there is no undo info.
8856 * Returns NULL when out of memory.
8857 */
8858 static buf_T *
8859open_spellbuf()
8860{
8861 buf_T *buf;
8862
8863 buf = (buf_T *)alloc_clear(sizeof(buf_T));
8864 if (buf != NULL)
8865 {
8866 buf->b_spell = TRUE;
8867 buf->b_p_swf = TRUE; /* may create a swap file */
8868 ml_open(buf);
8869 ml_open_file(buf); /* create swap file now */
8870 }
8871 return buf;
8872}
8873
8874/*
8875 * Close the buffer used for spell info.
8876 */
8877 static void
8878close_spellbuf(buf)
8879 buf_T *buf;
8880{
8881 if (buf != NULL)
8882 {
8883 ml_close(buf, TRUE);
8884 vim_free(buf);
8885 }
8886}
8887
8888
8889/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008890 * Create a Vim spell file from one or more word lists.
8891 * "fnames[0]" is the output file name.
8892 * "fnames[fcount - 1]" is the last input file name.
8893 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
8894 * and ".spl" is appended to make the output file name.
8895 */
8896 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008897mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00008898 int fcount;
8899 char_u **fnames;
8900 int ascii; /* -ascii argument given */
8901 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008902 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008903{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008904 char_u fname[MAXPATHL];
8905 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00008906 char_u **innames;
8907 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008908 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008909 int i;
8910 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008911 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008912 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008913 spellinfo_T spin;
8914
8915 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008916 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008917 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008918 spin.si_followup = TRUE;
8919 spin.si_rem_accents = TRUE;
8920 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008921 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008922 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
8923 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008924 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008925 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008926 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008927 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008928
Bram Moolenaarb765d632005-06-07 21:00:02 +00008929 /* default: fnames[0] is output file, following are input files */
8930 innames = &fnames[1];
8931 incount = fcount - 1;
8932
8933 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00008934 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008935 len = STRLEN(fnames[0]);
8936 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
8937 {
8938 /* For ":mkspell path/en.latin1.add" output file is
8939 * "path/en.latin1.add.spl". */
8940 innames = &fnames[0];
8941 incount = 1;
8942 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
8943 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008944 else if (fcount == 1)
8945 {
8946 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
8947 innames = &fnames[0];
8948 incount = 1;
8949 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
8950 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
8951 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00008952 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
8953 {
8954 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008955 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008956 }
8957 else
8958 /* Name should be language, make the file name from it. */
8959 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
8960 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
8961
8962 /* Check for .ascii.spl. */
8963 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
8964 spin.si_ascii = TRUE;
8965
8966 /* Check for .add.spl. */
8967 if (strstr((char *)gettail(wfname), ".add.") != NULL)
8968 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00008969 }
8970
Bram Moolenaarb765d632005-06-07 21:00:02 +00008971 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008972 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00008973 else if (vim_strchr(gettail(wfname), '_') != NULL)
8974 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00008975 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008976 EMSG(_("E754: Only up to 8 regions supported"));
8977 else
8978 {
8979 /* Check for overwriting before doing things that may take a lot of
8980 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008981 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008982 {
8983 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00008984 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008985 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00008986 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008987 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008988 EMSG2(_(e_isadir2), wfname);
8989 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008990 }
8991
8992 /*
8993 * Init the aff and dic pointers.
8994 * Get the region names if there are more than 2 arguments.
8995 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008996 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008997 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008998 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008999
Bram Moolenaar3982c542005-06-08 21:56:31 +00009000 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009001 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009002 len = STRLEN(innames[i]);
9003 if (STRLEN(gettail(innames[i])) < 5
9004 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009005 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009006 EMSG2(_("E755: Invalid region in %s"), innames[i]);
9007 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009008 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009009 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
9010 spin.si_region_name[i * 2 + 1] =
9011 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009012 }
9013 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009014 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009015
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009016 spin.si_foldroot = wordtree_alloc(&spin);
9017 spin.si_keeproot = wordtree_alloc(&spin);
9018 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009019 if (spin.si_foldroot == NULL
9020 || spin.si_keeproot == NULL
9021 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009022 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00009023 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009024 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009025 }
9026
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009027 /* When not producing a .add.spl file clear the character table when
9028 * we encounter one in the .aff file. This means we dump the current
9029 * one in the .spl file if the .aff file doesn't define one. That's
9030 * better than guessing the contents, the table will match a
9031 * previously loaded spell file. */
9032 if (!spin.si_add)
9033 spin.si_clear_chartab = TRUE;
9034
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009035 /*
9036 * Read all the .aff and .dic files.
9037 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00009038 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009039 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009040 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009041 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009042 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009043 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009044
Bram Moolenaarb765d632005-06-07 21:00:02 +00009045 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009046 if (mch_stat((char *)fname, &st) >= 0)
9047 {
9048 /* Read the .aff file. Will init "spin->si_conv" based on the
9049 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009050 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009051 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009052 error = TRUE;
9053 else
9054 {
9055 /* Read the .dic file and store the words in the trees. */
9056 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00009057 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009058 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009059 error = TRUE;
9060 }
9061 }
9062 else
9063 {
9064 /* No .aff file, try reading the file as a word list. Store
9065 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009066 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009067 error = TRUE;
9068 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009069
Bram Moolenaarb765d632005-06-07 21:00:02 +00009070#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009071 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00009072 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009073#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009074 }
9075
Bram Moolenaar78622822005-08-23 21:00:13 +00009076 if (spin.si_compflags != NULL && spin.si_nobreak)
9077 MSG(_("Warning: both compounding and NOBREAK specified"));
9078
Bram Moolenaar4770d092006-01-12 23:22:24 +00009079 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009080 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009081 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00009082 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009083 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009084 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009085 wordtree_compress(&spin, spin.si_foldroot);
9086 wordtree_compress(&spin, spin.si_keeproot);
9087 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009088 }
9089
Bram Moolenaar4770d092006-01-12 23:22:24 +00009090 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009091 {
9092 /*
9093 * Write the info in the spell file.
9094 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009095 vim_snprintf((char *)IObuff, IOSIZE,
9096 _("Writing spell file %s ..."), wfname);
9097 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00009098
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009099 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009100
Bram Moolenaar4770d092006-01-12 23:22:24 +00009101 spell_message(&spin, (char_u *)_("Done!"));
9102 vim_snprintf((char *)IObuff, IOSIZE,
9103 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
9104 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009105
Bram Moolenaar4770d092006-01-12 23:22:24 +00009106 /*
9107 * If the file is loaded need to reload it.
9108 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009109 if (!error)
9110 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009111 }
9112
9113 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009114 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009115 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009116 ga_clear(&spin.si_sal);
9117 ga_clear(&spin.si_map);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009118 ga_clear(&spin.si_comppat);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009119 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009120 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009121
9122 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009123 for (i = 0; i < incount; ++i)
9124 if (afile[i] != NULL)
9125 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009126
9127 /* Free all the bits and pieces at once. */
9128 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009129
9130 /*
9131 * If there is soundfolding info and no NOSUGFILE item create the
9132 * .sug file with the soundfolded word trie.
9133 */
9134 if (spin.si_sugtime != 0 && !error && !got_int)
9135 spell_make_sugfile(&spin, wfname);
9136
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009137 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009138}
9139
Bram Moolenaar4770d092006-01-12 23:22:24 +00009140/*
9141 * Display a message for spell file processing when 'verbose' is set or using
9142 * ":mkspell". "str" can be IObuff.
9143 */
9144 static void
9145spell_message(spin, str)
9146 spellinfo_T *spin;
9147 char_u *str;
9148{
9149 if (spin->si_verbose || p_verbose > 2)
9150 {
9151 if (!spin->si_verbose)
9152 verbose_enter();
9153 MSG(str);
9154 out_flush();
9155 if (!spin->si_verbose)
9156 verbose_leave();
9157 }
9158}
Bram Moolenaarb765d632005-06-07 21:00:02 +00009159
9160/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009161 * ":[count]spellgood {word}"
9162 * ":[count]spellwrong {word}"
Bram Moolenaard0131a82006-03-04 21:46:13 +00009163 * ":[count]spellundo {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00009164 */
9165 void
9166ex_spell(eap)
9167 exarg_T *eap;
9168{
Bram Moolenaar7887d882005-07-01 22:33:52 +00009169 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaard0131a82006-03-04 21:46:13 +00009170 eap->forceit ? 0 : (int)eap->line2,
9171 eap->cmdidx == CMD_spellundo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009172}
9173
9174/*
9175 * Add "word[len]" to 'spellfile' as a good or bad word.
9176 */
9177 void
Bram Moolenaard0131a82006-03-04 21:46:13 +00009178spell_add_word(word, len, bad, index, undo)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009179 char_u *word;
9180 int len;
9181 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009182 int index; /* "zG" and "zW": zero, otherwise index in
9183 'spellfile' */
Bram Moolenaard0131a82006-03-04 21:46:13 +00009184 int undo; /* TRUE for "zug", "zuG", "zuw" and "zuW" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009185{
9186 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009187 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009188 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009189 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009190 char_u fnamebuf[MAXPATHL];
9191 char_u line[MAXWLEN * 2];
9192 long fpos, fpos_next = 0;
9193 int i;
9194 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009195
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009196 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009197 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009198 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009199 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009200 int_wordlist = vim_tempname('s');
9201 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009202 return;
9203 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009204 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009205 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009206 else
9207 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009208 /* If 'spellfile' isn't set figure out a good default value. */
9209 if (*curbuf->b_p_spf == NUL)
9210 {
9211 init_spellfile();
9212 new_spf = TRUE;
9213 }
9214
9215 if (*curbuf->b_p_spf == NUL)
9216 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00009217 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009218 return;
9219 }
9220
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009221 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
9222 {
9223 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
9224 if (i == index)
9225 break;
9226 if (*spf == NUL)
9227 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00009228 EMSGN(_("E765: 'spellfile' does not have %ld entries"), index);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009229 return;
9230 }
9231 }
9232
Bram Moolenaarb765d632005-06-07 21:00:02 +00009233 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009234 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009235 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
9236 buf = NULL;
9237 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00009238 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009239 EMSG(_(e_bufloaded));
9240 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009241 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009242
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009243 fname = fnamebuf;
9244 }
9245
Bram Moolenaard0131a82006-03-04 21:46:13 +00009246 if (bad || undo)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009247 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009248 /* When the word appears as good word we need to remove that one,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009249 * since its flags sort before the one with WF_BANNED. */
9250 fd = mch_fopen((char *)fname, "r");
9251 if (fd != NULL)
9252 {
9253 while (!vim_fgets(line, MAXWLEN * 2, fd))
9254 {
9255 fpos = fpos_next;
9256 fpos_next = ftell(fd);
9257 if (STRNCMP(word, line, len) == 0
9258 && (line[len] == '/' || line[len] < ' '))
9259 {
9260 /* Found duplicate word. Remove it by writing a '#' at
9261 * the start of the line. Mixing reading and writing
9262 * doesn't work for all systems, close the file first. */
9263 fclose(fd);
9264 fd = mch_fopen((char *)fname, "r+");
9265 if (fd == NULL)
9266 break;
9267 if (fseek(fd, fpos, SEEK_SET) == 0)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009268 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009269 fputc('#', fd);
Bram Moolenaard0131a82006-03-04 21:46:13 +00009270 if (undo)
9271 smsg((char_u *)_("Word removed from %s"), NameBuff);
9272 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009273 fseek(fd, fpos_next, SEEK_SET);
9274 }
9275 }
9276 fclose(fd);
9277 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009278 }
9279
Bram Moolenaard0131a82006-03-04 21:46:13 +00009280 if (!undo)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009281 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009282 fd = mch_fopen((char *)fname, "a");
9283 if (fd == NULL && new_spf)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009284 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009285 /* We just initialized the 'spellfile' option and can't open the
9286 * file. We may need to create the "spell" directory first. We
9287 * already checked the runtime directory is writable in
9288 * init_spellfile(). */
9289 if (!dir_of_file_exists(fname))
9290 {
9291 /* The directory doesn't exist. Try creating it and opening
9292 * the file again. */
9293 vim_mkdir(NameBuff, 0755);
9294 fd = mch_fopen((char *)fname, "a");
9295 }
9296 }
9297
9298 if (fd == NULL)
9299 EMSG2(_(e_notopen), fname);
9300 else
9301 {
9302 if (bad)
9303 fprintf(fd, "%.*s/!\n", len, word);
9304 else
9305 fprintf(fd, "%.*s\n", len, word);
9306 fclose(fd);
9307
9308 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
9309 smsg((char_u *)_("Word added to %s"), NameBuff);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009310 }
9311 }
9312
Bram Moolenaard0131a82006-03-04 21:46:13 +00009313 if (fd != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009314 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009315 /* Update the .add.spl file. */
9316 mkspell(1, &fname, FALSE, TRUE, TRUE);
9317
9318 /* If the .add file is edited somewhere, reload it. */
9319 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00009320 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009321
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009322 redraw_all_later(SOME_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009323 }
9324}
9325
9326/*
9327 * Initialize 'spellfile' for the current buffer.
9328 */
9329 static void
9330init_spellfile()
9331{
9332 char_u buf[MAXPATHL];
9333 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009334 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009335 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009336 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009337 int aspath = FALSE;
9338 char_u *lstart = curbuf->b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009339
9340 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
9341 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009342 /* Find the end of the language name. Exclude the region. If there
9343 * is a path separator remember the start of the tail. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009344 for (lend = curbuf->b_p_spl; *lend != NUL
9345 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009346 if (vim_ispathsep(*lend))
9347 {
9348 aspath = TRUE;
9349 lstart = lend + 1;
9350 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009351
9352 /* Loop over all entries in 'runtimepath'. Use the first one where we
9353 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009354 rtp = p_rtp;
9355 while (*rtp != NUL)
9356 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009357 if (aspath)
9358 /* Use directory of an entry with path, e.g., for
9359 * "/dir/lg.utf-8.spl" use "/dir". */
9360 vim_strncpy(buf, curbuf->b_p_spl, lstart - curbuf->b_p_spl - 1);
9361 else
9362 /* Copy the path from 'runtimepath' to buf[]. */
9363 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009364 if (filewritable(buf) == 2)
9365 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00009366 /* Use the first language name from 'spelllang' and the
9367 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009368 if (aspath)
9369 vim_strncpy(buf, curbuf->b_p_spl, lend - curbuf->b_p_spl);
9370 else
9371 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009372 /* Create the "spell" directory if it doesn't exist yet. */
9373 l = STRLEN(buf);
9374 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
9375 if (!filewritable(buf) != 2)
9376 vim_mkdir(buf, 0755);
9377
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009378 l = STRLEN(buf);
9379 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009380 "/%.*s", (int)(lend - lstart), lstart);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009381 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009382 l = STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009383 fname = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang->sl_fname;
9384 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
9385 fname != NULL
9386 && strstr((char *)gettail(fname), ".ascii.") != NULL
9387 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009388 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
9389 break;
9390 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009391 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009392 }
9393 }
9394}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009395
Bram Moolenaar51485f02005-06-04 21:55:20 +00009396
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009397/*
9398 * Init the chartab used for spelling for ASCII.
9399 * EBCDIC is not supported!
9400 */
9401 static void
9402clear_spell_chartab(sp)
9403 spelltab_T *sp;
9404{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009405 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009406
9407 /* Init everything to FALSE. */
9408 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
9409 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
9410 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009411 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009412 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009413 sp->st_upper[i] = i;
9414 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009415
9416 /* We include digits. A word shouldn't start with a digit, but handling
9417 * that is done separately. */
9418 for (i = '0'; i <= '9'; ++i)
9419 sp->st_isw[i] = TRUE;
9420 for (i = 'A'; i <= 'Z'; ++i)
9421 {
9422 sp->st_isw[i] = TRUE;
9423 sp->st_isu[i] = TRUE;
9424 sp->st_fold[i] = i + 0x20;
9425 }
9426 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009427 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009428 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009429 sp->st_upper[i] = i - 0x20;
9430 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009431}
9432
9433/*
9434 * Init the chartab used for spelling. Only depends on 'encoding'.
9435 * Called once while starting up and when 'encoding' changes.
9436 * The default is to use isalpha(), but the spell file should define the word
9437 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009438 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009439 */
9440 void
9441init_spell_chartab()
9442{
9443 int i;
9444
9445 did_set_spelltab = FALSE;
9446 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009447#ifdef FEAT_MBYTE
9448 if (enc_dbcs)
9449 {
9450 /* DBCS: assume double-wide characters are word characters. */
9451 for (i = 128; i <= 255; ++i)
9452 if (MB_BYTE2LEN(i) == 2)
9453 spelltab.st_isw[i] = TRUE;
9454 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009455 else if (enc_utf8)
9456 {
9457 for (i = 128; i < 256; ++i)
9458 {
9459 spelltab.st_isu[i] = utf_isupper(i);
9460 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
9461 spelltab.st_fold[i] = utf_fold(i);
9462 spelltab.st_upper[i] = utf_toupper(i);
9463 }
9464 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009465 else
9466#endif
9467 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009468 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009469 for (i = 128; i < 256; ++i)
9470 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009471 if (MB_ISUPPER(i))
9472 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009473 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009474 spelltab.st_isu[i] = TRUE;
9475 spelltab.st_fold[i] = MB_TOLOWER(i);
9476 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009477 else if (MB_ISLOWER(i))
9478 {
9479 spelltab.st_isw[i] = TRUE;
9480 spelltab.st_upper[i] = MB_TOUPPER(i);
9481 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009482 }
9483 }
9484}
9485
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009486/*
9487 * Set the spell character tables from strings in the affix file.
9488 */
9489 static int
9490set_spell_chartab(fol, low, upp)
9491 char_u *fol;
9492 char_u *low;
9493 char_u *upp;
9494{
9495 /* We build the new tables here first, so that we can compare with the
9496 * previous one. */
9497 spelltab_T new_st;
9498 char_u *pf = fol, *pl = low, *pu = upp;
9499 int f, l, u;
9500
9501 clear_spell_chartab(&new_st);
9502
9503 while (*pf != NUL)
9504 {
9505 if (*pl == NUL || *pu == NUL)
9506 {
9507 EMSG(_(e_affform));
9508 return FAIL;
9509 }
9510#ifdef FEAT_MBYTE
9511 f = mb_ptr2char_adv(&pf);
9512 l = mb_ptr2char_adv(&pl);
9513 u = mb_ptr2char_adv(&pu);
9514#else
9515 f = *pf++;
9516 l = *pl++;
9517 u = *pu++;
9518#endif
9519 /* Every character that appears is a word character. */
9520 if (f < 256)
9521 new_st.st_isw[f] = TRUE;
9522 if (l < 256)
9523 new_st.st_isw[l] = TRUE;
9524 if (u < 256)
9525 new_st.st_isw[u] = TRUE;
9526
9527 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9528 * case-folding */
9529 if (l < 256 && l != f)
9530 {
9531 if (f >= 256)
9532 {
9533 EMSG(_(e_affrange));
9534 return FAIL;
9535 }
9536 new_st.st_fold[l] = f;
9537 }
9538
9539 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009540 * case-folding, it's upper case and the "UPP" is the upper case of
9541 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009542 if (u < 256 && u != f)
9543 {
9544 if (f >= 256)
9545 {
9546 EMSG(_(e_affrange));
9547 return FAIL;
9548 }
9549 new_st.st_fold[u] = f;
9550 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009551 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009552 }
9553 }
9554
9555 if (*pl != NUL || *pu != NUL)
9556 {
9557 EMSG(_(e_affform));
9558 return FAIL;
9559 }
9560
9561 return set_spell_finish(&new_st);
9562}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009563
9564/*
9565 * Set the spell character tables from strings in the .spl file.
9566 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009567 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009568set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009569 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009570 int cnt; /* length of "flags" */
9571 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009572{
9573 /* We build the new tables here first, so that we can compare with the
9574 * previous one. */
9575 spelltab_T new_st;
9576 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009577 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009578 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009579
9580 clear_spell_chartab(&new_st);
9581
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009582 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009583 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009584 if (i < cnt)
9585 {
9586 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9587 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9588 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009589
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009590 if (*p != NUL)
9591 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009592#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009593 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009594#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009595 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009596#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009597 new_st.st_fold[i + 128] = c;
9598 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9599 new_st.st_upper[c] = i + 128;
9600 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009601 }
9602
Bram Moolenaar5195e452005-08-19 20:32:47 +00009603 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009604}
9605
9606 static int
9607set_spell_finish(new_st)
9608 spelltab_T *new_st;
9609{
9610 int i;
9611
9612 if (did_set_spelltab)
9613 {
9614 /* check that it's the same table */
9615 for (i = 0; i < 256; ++i)
9616 {
9617 if (spelltab.st_isw[i] != new_st->st_isw[i]
9618 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009619 || spelltab.st_fold[i] != new_st->st_fold[i]
9620 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009621 {
9622 EMSG(_("E763: Word characters differ between spell files"));
9623 return FAIL;
9624 }
9625 }
9626 }
9627 else
9628 {
9629 /* copy the new spelltab into the one being used */
9630 spelltab = *new_st;
9631 did_set_spelltab = TRUE;
9632 }
9633
9634 return OK;
9635}
9636
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009637/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009638 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009639 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009640 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009641 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009642 */
9643 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009644spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00009645 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009646 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009647{
Bram Moolenaarea408852005-06-25 22:49:46 +00009648#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009649 char_u *s;
9650 int l;
9651 int c;
9652
9653 if (has_mbyte)
9654 {
9655 l = MB_BYTE2LEN(*p);
9656 s = p;
9657 if (l == 1)
9658 {
9659 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009660 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009661 {
9662 s = p + 1; /* skip a mid-word character */
9663 l = MB_BYTE2LEN(*s);
9664 }
9665 }
9666 else
9667 {
9668 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009669 if (c < 256 ? buf->b_spell_ismw[c]
9670 : (buf->b_spell_ismw_mb != NULL
9671 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009672 {
9673 s = p + l;
9674 l = MB_BYTE2LEN(*s);
9675 }
9676 }
9677
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009678 c = mb_ptr2char(s);
9679 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009680 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009681 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009682 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009683#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009684
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009685 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
9686}
9687
9688/*
9689 * Return TRUE if "p" points to a word character.
9690 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9691 */
9692 static int
9693spell_iswordp_nmw(p)
9694 char_u *p;
9695{
9696#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009697 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009698
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009699 if (has_mbyte)
9700 {
9701 c = mb_ptr2char(p);
9702 if (c > 255)
9703 return mb_get_class(p) >= 2;
9704 return spelltab.st_isw[c];
9705 }
9706#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009707 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009708}
9709
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009710#ifdef FEAT_MBYTE
9711/*
9712 * Return TRUE if "p" points to a word character.
9713 * Wide version of spell_iswordp().
9714 */
9715 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009716spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009717 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009718 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009719{
9720 int *s;
9721
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009722 if (*p < 256 ? buf->b_spell_ismw[*p]
9723 : (buf->b_spell_ismw_mb != NULL
9724 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009725 s = p + 1;
9726 else
9727 s = p;
9728
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009729 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009730 {
9731 if (enc_utf8)
9732 return utf_class(*s) >= 2;
9733 if (enc_dbcs)
9734 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
9735 return 0;
9736 }
9737 return spelltab.st_isw[*s];
9738}
9739#endif
9740
Bram Moolenaarea408852005-06-25 22:49:46 +00009741/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009742 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009743 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009744 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009745 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009746write_spell_prefcond(fd, gap)
9747 FILE *fd;
9748 garray_T *gap;
9749{
9750 int i;
9751 char_u *p;
9752 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009753 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009754
Bram Moolenaar5195e452005-08-19 20:32:47 +00009755 if (fd != NULL)
9756 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
9757
9758 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009759
9760 for (i = 0; i < gap->ga_len; ++i)
9761 {
9762 /* <prefcond> : <condlen> <condstr> */
9763 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009764 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009765 {
9766 len = STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009767 if (fd != NULL)
9768 {
9769 fputc(len, fd);
9770 fwrite(p, (size_t)len, (size_t)1, fd);
9771 }
9772 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009773 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009774 else if (fd != NULL)
9775 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009776 }
9777
Bram Moolenaar5195e452005-08-19 20:32:47 +00009778 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009779}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009780
9781/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009782 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
9783 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009784 * When using a multi-byte 'encoding' the length may change!
9785 * Returns FAIL when something wrong.
9786 */
9787 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009788spell_casefold(str, len, buf, buflen)
9789 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009790 int len;
9791 char_u *buf;
9792 int buflen;
9793{
9794 int i;
9795
9796 if (len >= buflen)
9797 {
9798 buf[0] = NUL;
9799 return FAIL; /* result will not fit */
9800 }
9801
9802#ifdef FEAT_MBYTE
9803 if (has_mbyte)
9804 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009805 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009806 char_u *p;
9807 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009808
9809 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009810 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009811 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009812 if (outi + MB_MAXBYTES > buflen)
9813 {
9814 buf[outi] = NUL;
9815 return FAIL;
9816 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009817 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009818 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009819 }
9820 buf[outi] = NUL;
9821 }
9822 else
9823#endif
9824 {
9825 /* Be quick for non-multibyte encodings. */
9826 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009827 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009828 buf[i] = NUL;
9829 }
9830
9831 return OK;
9832}
9833
Bram Moolenaar4770d092006-01-12 23:22:24 +00009834/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009835#define SPS_BEST 1
9836#define SPS_FAST 2
9837#define SPS_DOUBLE 4
9838
Bram Moolenaar4770d092006-01-12 23:22:24 +00009839static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
9840static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009841
9842/*
9843 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009844 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009845 */
9846 int
9847spell_check_sps()
9848{
9849 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009850 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009851 char_u buf[MAXPATHL];
9852 int f;
9853
9854 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009855 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009856
9857 for (p = p_sps; *p != NUL; )
9858 {
9859 copy_option_part(&p, buf, MAXPATHL, ",");
9860
9861 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009862 if (VIM_ISDIGIT(*buf))
9863 {
9864 s = buf;
9865 sps_limit = getdigits(&s);
9866 if (*s != NUL && !VIM_ISDIGIT(*s))
9867 f = -1;
9868 }
9869 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009870 f = SPS_BEST;
9871 else if (STRCMP(buf, "fast") == 0)
9872 f = SPS_FAST;
9873 else if (STRCMP(buf, "double") == 0)
9874 f = SPS_DOUBLE;
9875 else if (STRNCMP(buf, "expr:", 5) != 0
9876 && STRNCMP(buf, "file:", 5) != 0)
9877 f = -1;
9878
9879 if (f == -1 || (sps_flags != 0 && f != 0))
9880 {
9881 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009882 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009883 return FAIL;
9884 }
9885 if (f != 0)
9886 sps_flags = f;
9887 }
9888
9889 if (sps_flags == 0)
9890 sps_flags = SPS_BEST;
9891
9892 return OK;
9893}
9894
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009895/*
9896 * "z?": Find badly spelled word under or after the cursor.
9897 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009898 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00009899 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009900 */
9901 void
Bram Moolenaard12a1322005-08-21 22:08:24 +00009902spell_suggest(count)
9903 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009904{
9905 char_u *line;
9906 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009907 char_u wcopy[MAXWLEN + 2];
9908 char_u *p;
9909 int i;
9910 int c;
9911 suginfo_T sug;
9912 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009913 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009914 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009915 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009916 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009917 int badlen = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009918
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009919 if (no_spell_checking(curwin))
9920 return;
9921
9922#ifdef FEAT_VISUAL
9923 if (VIsual_active)
9924 {
9925 /* Use the Visually selected text as the bad word. But reject
9926 * a multi-line selection. */
9927 if (curwin->w_cursor.lnum != VIsual.lnum)
9928 {
9929 vim_beep();
9930 return;
9931 }
9932 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
9933 if (badlen < 0)
9934 badlen = -badlen;
9935 else
9936 curwin->w_cursor.col = VIsual.col;
9937 ++badlen;
9938 end_visual_mode();
9939 }
9940 else
9941#endif
9942 /* Find the start of the badly spelled word. */
9943 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00009944 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009945 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00009946 /* No bad word or it starts after the cursor: use the word under the
9947 * cursor. */
9948 curwin->w_cursor = prev_cursor;
9949 line = ml_get_curline();
9950 p = line + curwin->w_cursor.col;
9951 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009952 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00009953 mb_ptr_back(line, p);
9954 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009955 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00009956 mb_ptr_adv(p);
9957
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009958 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00009959 {
9960 beep_flush();
9961 return;
9962 }
9963 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009964 }
9965
Bram Moolenaard857f0e2005-06-21 22:37:39 +00009966 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009967
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009968 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009969 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009970
Bram Moolenaar8b59de92005-08-11 19:59:29 +00009971 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009972
Bram Moolenaar5195e452005-08-19 20:32:47 +00009973 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
9974 * 'spellsuggest', whatever is smaller. */
9975 if (sps_limit > (int)Rows - 2)
9976 limit = (int)Rows - 2;
9977 else
9978 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009979 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +00009980 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009981
9982 if (sug.su_ga.ga_len == 0)
9983 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +00009984 else if (count > 0)
9985 {
9986 if (count > sug.su_ga.ga_len)
9987 smsg((char_u *)_("Sorry, only %ld suggestions"),
9988 (long)sug.su_ga.ga_len);
9989 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009990 else
9991 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009992 vim_free(repl_from);
9993 repl_from = NULL;
9994 vim_free(repl_to);
9995 repl_to = NULL;
9996
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009997#ifdef FEAT_RIGHTLEFT
9998 /* When 'rightleft' is set the list is drawn right-left. */
9999 cmdmsg_rl = curwin->w_p_rl;
10000 if (cmdmsg_rl)
10001 msg_col = Columns - 1;
10002#endif
10003
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010004 /* List the suggestions. */
10005 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010006 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010007 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
10008 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010009#ifdef FEAT_RIGHTLEFT
10010 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
10011 {
10012 /* And now the rabbit from the high hat: Avoid showing the
10013 * untranslated message rightleft. */
10014 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
10015 sug.su_badlen, sug.su_badptr);
10016 }
10017#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010018 msg_puts(IObuff);
10019 msg_clr_eos();
10020 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +000010021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010022 msg_scroll = TRUE;
10023 for (i = 0; i < sug.su_ga.ga_len; ++i)
10024 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010025 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010026
10027 /* The suggested word may replace only part of the bad word, add
10028 * the not replaced part. */
10029 STRCPY(wcopy, stp->st_word);
10030 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010031 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010032 sug.su_badptr + stp->st_orglen,
10033 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010034 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
10035#ifdef FEAT_RIGHTLEFT
10036 if (cmdmsg_rl)
10037 rl_mirror(IObuff);
10038#endif
10039 msg_puts(IObuff);
10040
10041 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010042 msg_puts(IObuff);
10043
10044 /* The word may replace more than "su_badlen". */
10045 if (sug.su_badlen < stp->st_orglen)
10046 {
10047 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
10048 stp->st_orglen, sug.su_badptr);
10049 msg_puts(IObuff);
10050 }
10051
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010052 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010053 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010054 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010055 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010056 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010057 stp->st_salscore ? "s " : "",
10058 stp->st_score, stp->st_altscore);
10059 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010060 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +000010061 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010062#ifdef FEAT_RIGHTLEFT
10063 if (cmdmsg_rl)
10064 /* Mirror the numbers, but keep the leading space. */
10065 rl_mirror(IObuff + 1);
10066#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +000010067 msg_advance(30);
10068 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010069 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010070 msg_putchar('\n');
10071 }
10072
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010073#ifdef FEAT_RIGHTLEFT
10074 cmdmsg_rl = FALSE;
10075 msg_col = 0;
10076#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010077 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010078 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010079 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010080 selected -= lines_left;
Bram Moolenaar0fd92892006-03-09 22:27:48 +000010081 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010082 }
10083
Bram Moolenaard12a1322005-08-21 22:08:24 +000010084 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
10085 {
10086 /* Save the from and to text for :spellrepall. */
10087 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000010088 if (sug.su_badlen > stp->st_orglen)
10089 {
10090 /* Replacing less than "su_badlen", append the remainder to
10091 * repl_to. */
10092 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
10093 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
10094 sug.su_badlen - stp->st_orglen,
10095 sug.su_badptr + stp->st_orglen);
10096 repl_to = vim_strsave(IObuff);
10097 }
10098 else
10099 {
10100 /* Replacing su_badlen or more, use the whole word. */
10101 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
10102 repl_to = vim_strsave(stp->st_word);
10103 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010104
10105 /* Replace the word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010106 p = alloc(STRLEN(line) - stp->st_orglen + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010107 if (p != NULL)
10108 {
10109 c = sug.su_badptr - line;
10110 mch_memmove(p, line, c);
10111 STRCPY(p + c, stp->st_word);
10112 STRCAT(p, sug.su_badptr + stp->st_orglen);
10113 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10114 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010115
10116 /* For redo we use a change-word command. */
10117 ResetRedobuff();
10118 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +000010119 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010120 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010121 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010122
10123 /* After this "p" may be invalid. */
10124 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010125 }
10126 }
10127 else
10128 curwin->w_cursor = prev_cursor;
10129
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010130 spell_find_cleanup(&sug);
10131}
10132
10133/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010134 * Check if the word at line "lnum" column "col" is required to start with a
10135 * capital. This uses 'spellcapcheck' of the current buffer.
10136 */
10137 static int
10138check_need_cap(lnum, col)
10139 linenr_T lnum;
10140 colnr_T col;
10141{
10142 int need_cap = FALSE;
10143 char_u *line;
10144 char_u *line_copy = NULL;
10145 char_u *p;
10146 colnr_T endcol;
10147 regmatch_T regmatch;
10148
10149 if (curbuf->b_cap_prog == NULL)
10150 return FALSE;
10151
10152 line = ml_get_curline();
10153 endcol = 0;
10154 if ((int)(skipwhite(line) - line) >= (int)col)
10155 {
10156 /* At start of line, check if previous line is empty or sentence
10157 * ends there. */
10158 if (lnum == 1)
10159 need_cap = TRUE;
10160 else
10161 {
10162 line = ml_get(lnum - 1);
10163 if (*skipwhite(line) == NUL)
10164 need_cap = TRUE;
10165 else
10166 {
10167 /* Append a space in place of the line break. */
10168 line_copy = concat_str(line, (char_u *)" ");
10169 line = line_copy;
10170 endcol = STRLEN(line);
10171 }
10172 }
10173 }
10174 else
10175 endcol = col;
10176
10177 if (endcol > 0)
10178 {
10179 /* Check if sentence ends before the bad word. */
10180 regmatch.regprog = curbuf->b_cap_prog;
10181 regmatch.rm_ic = FALSE;
10182 p = line + endcol;
10183 for (;;)
10184 {
10185 mb_ptr_back(line, p);
10186 if (p == line || spell_iswordp_nmw(p))
10187 break;
10188 if (vim_regexec(&regmatch, p, 0)
10189 && regmatch.endp[0] == line + endcol)
10190 {
10191 need_cap = TRUE;
10192 break;
10193 }
10194 }
10195 }
10196
10197 vim_free(line_copy);
10198
10199 return need_cap;
10200}
10201
10202
10203/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010204 * ":spellrepall"
10205 */
10206/*ARGSUSED*/
10207 void
10208ex_spellrepall(eap)
10209 exarg_T *eap;
10210{
10211 pos_T pos = curwin->w_cursor;
10212 char_u *frompat;
10213 int addlen;
10214 char_u *line;
10215 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010216 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010217 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010218
10219 if (repl_from == NULL || repl_to == NULL)
10220 {
10221 EMSG(_("E752: No previous spell replacement"));
10222 return;
10223 }
10224 addlen = STRLEN(repl_to) - STRLEN(repl_from);
10225
10226 frompat = alloc(STRLEN(repl_from) + 7);
10227 if (frompat == NULL)
10228 return;
10229 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
10230 p_ws = FALSE;
10231
Bram Moolenaar5195e452005-08-19 20:32:47 +000010232 sub_nsubs = 0;
10233 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010234 curwin->w_cursor.lnum = 0;
10235 while (!got_int)
10236 {
10237 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
10238 || u_save_cursor() == FAIL)
10239 break;
10240
10241 /* Only replace when the right word isn't there yet. This happens
10242 * when changing "etc" to "etc.". */
10243 line = ml_get_curline();
10244 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
10245 repl_to, STRLEN(repl_to)) != 0)
10246 {
10247 p = alloc(STRLEN(line) + addlen + 1);
10248 if (p == NULL)
10249 break;
10250 mch_memmove(p, line, curwin->w_cursor.col);
10251 STRCPY(p + curwin->w_cursor.col, repl_to);
10252 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
10253 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10254 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010255
10256 if (curwin->w_cursor.lnum != prev_lnum)
10257 {
10258 ++sub_nlines;
10259 prev_lnum = curwin->w_cursor.lnum;
10260 }
10261 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010262 }
10263 curwin->w_cursor.col += STRLEN(repl_to);
10264 }
10265
10266 p_ws = save_ws;
10267 curwin->w_cursor = pos;
10268 vim_free(frompat);
10269
Bram Moolenaar5195e452005-08-19 20:32:47 +000010270 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010271 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010272 else
10273 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010274}
10275
10276/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010277 * Find spell suggestions for "word". Return them in the growarray "*gap" as
10278 * a list of allocated strings.
10279 */
10280 void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010281spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010282 garray_T *gap;
10283 char_u *word;
10284 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010285 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010286 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010287{
10288 suginfo_T sug;
10289 int i;
10290 suggest_T *stp;
10291 char_u *wcopy;
10292
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010293 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010294
10295 /* Make room in "gap". */
10296 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010297 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010298 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010299 for (i = 0; i < sug.su_ga.ga_len; ++i)
10300 {
10301 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010302
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010303 /* The suggested word may replace only part of "word", add the not
10304 * replaced part. */
10305 wcopy = alloc(stp->st_wordlen
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010306 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010307 if (wcopy == NULL)
10308 break;
10309 STRCPY(wcopy, stp->st_word);
10310 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
10311 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
10312 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010313 }
10314
10315 spell_find_cleanup(&sug);
10316}
10317
10318/*
10319 * Find spell suggestions for the word at the start of "badptr".
10320 * Return the suggestions in "su->su_ga".
10321 * The maximum number of suggestions is "maxcount".
10322 * Note: does use info for the current window.
10323 * This is based on the mechanisms of Aspell, but completely reimplemented.
10324 */
10325 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010326spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010327 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010328 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010329 suginfo_T *su;
10330 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +000010331 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010332 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010333 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010334{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010335 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010336 char_u buf[MAXPATHL];
10337 char_u *p;
10338 int do_combine = FALSE;
10339 char_u *sps_copy;
10340#ifdef FEAT_EVAL
10341 static int expr_busy = FALSE;
10342#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010343 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010344 int i;
10345 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010346
10347 /*
10348 * Set the info in "*su".
10349 */
10350 vim_memset(su, 0, sizeof(suginfo_T));
10351 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10352 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010353 if (*badptr == NUL)
10354 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010355 hash_init(&su->su_banned);
10356
10357 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010358 if (badlen != 0)
10359 su->su_badlen = badlen;
10360 else
10361 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010362 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010363 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010364
10365 if (su->su_badlen >= MAXWLEN)
10366 su->su_badlen = MAXWLEN - 1; /* just in case */
10367 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10368 (void)spell_casefold(su->su_badptr, su->su_badlen,
10369 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010370 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010371 su->su_badflags = badword_captype(su->su_badptr,
10372 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010373 if (need_cap)
10374 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010375
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010376 /* Find the default language for sound folding. We simply use the first
10377 * one in 'spelllang' that supports sound folding. That's good for when
10378 * using multiple files for one language, it's not that bad when mixing
10379 * languages (e.g., "pl,en"). */
10380 for (i = 0; i < curbuf->b_langp.ga_len; ++i)
10381 {
10382 lp = LANGP_ENTRY(curbuf->b_langp, i);
10383 if (lp->lp_sallang != NULL)
10384 {
10385 su->su_sallang = lp->lp_sallang;
10386 break;
10387 }
10388 }
10389
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010390 /* Soundfold the bad word with the default sound folding, so that we don't
10391 * have to do this many times. */
10392 if (su->su_sallang != NULL)
10393 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10394 su->su_sal_badword);
10395
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010396 /* If the word is not capitalised and spell_check() doesn't consider the
10397 * word to be bad then it might need to be capitalised. Add a suggestion
10398 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010399 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010400 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010401 {
10402 make_case_word(su->su_badword, buf, WF_ONECAP);
10403 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010404 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010405 }
10406
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010407 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +000010408 if (banbadword)
10409 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010410
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010411 /* Make a copy of 'spellsuggest', because the expression may change it. */
10412 sps_copy = vim_strsave(p_sps);
10413 if (sps_copy == NULL)
10414 return;
10415
10416 /* Loop over the items in 'spellsuggest'. */
10417 for (p = sps_copy; *p != NUL; )
10418 {
10419 copy_option_part(&p, buf, MAXPATHL, ",");
10420
10421 if (STRNCMP(buf, "expr:", 5) == 0)
10422 {
10423#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010424 /* Evaluate an expression. Skip this when called recursively,
10425 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010426 if (!expr_busy)
10427 {
10428 expr_busy = TRUE;
10429 spell_suggest_expr(su, buf + 5);
10430 expr_busy = FALSE;
10431 }
10432#endif
10433 }
10434 else if (STRNCMP(buf, "file:", 5) == 0)
10435 /* Use list of suggestions in a file. */
10436 spell_suggest_file(su, buf + 5);
10437 else
10438 {
10439 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010440 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010441 if (sps_flags & SPS_DOUBLE)
10442 do_combine = TRUE;
10443 }
10444 }
10445
10446 vim_free(sps_copy);
10447
10448 if (do_combine)
10449 /* Combine the two list of suggestions. This must be done last,
10450 * because sorting changes the order again. */
10451 score_combine(su);
10452}
10453
10454#ifdef FEAT_EVAL
10455/*
10456 * Find suggestions by evaluating expression "expr".
10457 */
10458 static void
10459spell_suggest_expr(su, expr)
10460 suginfo_T *su;
10461 char_u *expr;
10462{
10463 list_T *list;
10464 listitem_T *li;
10465 int score;
10466 char_u *p;
10467
10468 /* The work is split up in a few parts to avoid having to export
10469 * suginfo_T.
10470 * First evaluate the expression and get the resulting list. */
10471 list = eval_spell_expr(su->su_badword, expr);
10472 if (list != NULL)
10473 {
10474 /* Loop over the items in the list. */
10475 for (li = list->lv_first; li != NULL; li = li->li_next)
10476 if (li->li_tv.v_type == VAR_LIST)
10477 {
10478 /* Get the word and the score from the items. */
10479 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010480 if (score >= 0 && score <= su->su_maxscore)
10481 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10482 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010483 }
10484 list_unref(list);
10485 }
10486
Bram Moolenaar4770d092006-01-12 23:22:24 +000010487 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10488 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010489 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10490}
10491#endif
10492
10493/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010494 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010495 */
10496 static void
10497spell_suggest_file(su, fname)
10498 suginfo_T *su;
10499 char_u *fname;
10500{
10501 FILE *fd;
10502 char_u line[MAXWLEN * 2];
10503 char_u *p;
10504 int len;
10505 char_u cword[MAXWLEN];
10506
10507 /* Open the file. */
10508 fd = mch_fopen((char *)fname, "r");
10509 if (fd == NULL)
10510 {
10511 EMSG2(_(e_notopen), fname);
10512 return;
10513 }
10514
10515 /* Read it line by line. */
10516 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10517 {
10518 line_breakcheck();
10519
10520 p = vim_strchr(line, '/');
10521 if (p == NULL)
10522 continue; /* No Tab found, just skip the line. */
10523 *p++ = NUL;
10524 if (STRICMP(su->su_badword, line) == 0)
10525 {
10526 /* Match! Isolate the good word, until CR or NL. */
10527 for (len = 0; p[len] >= ' '; ++len)
10528 ;
10529 p[len] = NUL;
10530
10531 /* If the suggestion doesn't have specific case duplicate the case
10532 * of the bad word. */
10533 if (captype(p, NULL) == 0)
10534 {
10535 make_case_word(p, cword, su->su_badflags);
10536 p = cword;
10537 }
10538
10539 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010540 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010541 }
10542 }
10543
10544 fclose(fd);
10545
Bram Moolenaar4770d092006-01-12 23:22:24 +000010546 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10547 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010548 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10549}
10550
10551/*
10552 * Find suggestions for the internal method indicated by "sps_flags".
10553 */
10554 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010555spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010556 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010557 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010558{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010559 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010560 * Load the .sug file(s) that are available and not done yet.
10561 */
10562 suggest_load_files();
10563
10564 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010565 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010566 *
10567 * Set a maximum score to limit the combination of operations that is
10568 * tried.
10569 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010570 suggest_try_special(su);
10571
10572 /*
10573 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10574 * from the .aff file and inserting a space (split the word).
10575 */
10576 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010577
10578 /* For the resulting top-scorers compute the sound-a-like score. */
10579 if (sps_flags & SPS_DOUBLE)
10580 score_comp_sal(su);
10581
10582 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010583 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010584 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010585 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010586 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010587 if (sps_flags & SPS_BEST)
10588 /* Adjust the word score for the suggestions found so far for how
10589 * they sounds like. */
10590 rescore_suggestions(su);
10591
10592 /*
10593 * While going throught the soundfold tree "su_maxscore" is the score
10594 * for the soundfold word, limits the changes that are being tried,
10595 * and "su_sfmaxscore" the rescored score, which is set by
10596 * cleanup_suggestions().
10597 * First find words with a small edit distance, because this is much
10598 * faster and often already finds the top-N suggestions. If we didn't
10599 * find many suggestions try again with a higher edit distance.
10600 * "sl_sounddone" is used to avoid doing the same word twice.
10601 */
10602 suggest_try_soundalike_prep();
10603 su->su_maxscore = SCORE_SFMAX1;
10604 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010605 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010606 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10607 {
10608 /* We didn't find enough matches, try again, allowing more
10609 * changes to the soundfold word. */
10610 su->su_maxscore = SCORE_SFMAX2;
10611 suggest_try_soundalike(su);
10612 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10613 {
10614 /* Still didn't find enough matches, try again, allowing even
10615 * more changes to the soundfold word. */
10616 su->su_maxscore = SCORE_SFMAX3;
10617 suggest_try_soundalike(su);
10618 }
10619 }
10620 su->su_maxscore = su->su_sfmaxscore;
10621 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010622 }
10623
Bram Moolenaar4770d092006-01-12 23:22:24 +000010624 /* When CTRL-C was hit while searching do show the results. Only clear
10625 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010626 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010627 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010628 {
10629 (void)vgetc();
10630 got_int = FALSE;
10631 }
10632
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010633 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010634 {
10635 if (sps_flags & SPS_BEST)
10636 /* Adjust the word score for how it sounds like. */
10637 rescore_suggestions(su);
10638
Bram Moolenaar4770d092006-01-12 23:22:24 +000010639 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10640 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010641 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010642 }
10643}
10644
10645/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010646 * Load the .sug files for languages that have one and weren't loaded yet.
10647 */
10648 static void
10649suggest_load_files()
10650{
10651 langp_T *lp;
10652 int lpi;
10653 slang_T *slang;
10654 char_u *dotp;
10655 FILE *fd;
10656 char_u buf[MAXWLEN];
10657 int i;
10658 time_t timestamp;
10659 int wcount;
10660 int wordnr;
10661 garray_T ga;
10662 int c;
10663
10664 /* Do this for all languages that support sound folding. */
10665 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
10666 {
10667 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
10668 slang = lp->lp_slang;
10669 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10670 {
10671 /* Change ".spl" to ".sug" and open the file. When the file isn't
10672 * found silently skip it. Do set "sl_sugloaded" so that we
10673 * don't try again and again. */
10674 slang->sl_sugloaded = TRUE;
10675
10676 dotp = vim_strrchr(slang->sl_fname, '.');
10677 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10678 continue;
10679 STRCPY(dotp, ".sug");
10680 fd = fopen((char *)slang->sl_fname, "r");
10681 if (fd == NULL)
10682 goto nextone;
10683
10684 /*
10685 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10686 */
10687 for (i = 0; i < VIMSUGMAGICL; ++i)
10688 buf[i] = getc(fd); /* <fileID> */
10689 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10690 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010691 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010692 slang->sl_fname);
10693 goto nextone;
10694 }
10695 c = getc(fd); /* <versionnr> */
10696 if (c < VIMSUGVERSION)
10697 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010698 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010699 slang->sl_fname);
10700 goto nextone;
10701 }
10702 else if (c > VIMSUGVERSION)
10703 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010704 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010705 slang->sl_fname);
10706 goto nextone;
10707 }
10708
10709 /* Check the timestamp, it must be exactly the same as the one in
10710 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010711 timestamp = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010712 if (timestamp != slang->sl_sugtime)
10713 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010714 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010715 slang->sl_fname);
10716 goto nextone;
10717 }
10718
10719 /*
10720 * <SUGWORDTREE>: <wordtree>
10721 * Read the trie with the soundfolded words.
10722 */
10723 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10724 FALSE, 0) != 0)
10725 {
10726someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010727 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010728 slang->sl_fname);
10729 slang_clear_sug(slang);
10730 goto nextone;
10731 }
10732
10733 /*
10734 * <SUGTABLE>: <sugwcount> <sugline> ...
10735 *
10736 * Read the table with word numbers. We use a file buffer for
10737 * this, because it's so much like a file with lines. Makes it
10738 * possible to swap the info and save on memory use.
10739 */
10740 slang->sl_sugbuf = open_spellbuf();
10741 if (slang->sl_sugbuf == NULL)
10742 goto someerror;
10743 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010744 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010745 if (wcount < 0)
10746 goto someerror;
10747
10748 /* Read all the wordnr lists into the buffer, one NUL terminated
10749 * list per line. */
10750 ga_init2(&ga, 1, 100);
10751 for (wordnr = 0; wordnr < wcount; ++wordnr)
10752 {
10753 ga.ga_len = 0;
10754 for (;;)
10755 {
10756 c = getc(fd); /* <sugline> */
10757 if (c < 0 || ga_grow(&ga, 1) == FAIL)
10758 goto someerror;
10759 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
10760 if (c == NUL)
10761 break;
10762 }
10763 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
10764 ga.ga_data, ga.ga_len, TRUE) == FAIL)
10765 goto someerror;
10766 }
10767 ga_clear(&ga);
10768
10769 /*
10770 * Need to put word counts in the word tries, so that we can find
10771 * a word by its number.
10772 */
10773 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
10774 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
10775
10776nextone:
10777 if (fd != NULL)
10778 fclose(fd);
10779 STRCPY(dotp, ".spl");
10780 }
10781 }
10782}
10783
10784
10785/*
10786 * Fill in the wordcount fields for a trie.
10787 * Returns the total number of words.
10788 */
10789 static void
10790tree_count_words(byts, idxs)
10791 char_u *byts;
10792 idx_T *idxs;
10793{
10794 int depth;
10795 idx_T arridx[MAXWLEN];
10796 int curi[MAXWLEN];
10797 int c;
10798 idx_T n;
10799 int wordcount[MAXWLEN];
10800
10801 arridx[0] = 0;
10802 curi[0] = 1;
10803 wordcount[0] = 0;
10804 depth = 0;
10805 while (depth >= 0 && !got_int)
10806 {
10807 if (curi[depth] > byts[arridx[depth]])
10808 {
10809 /* Done all bytes at this node, go up one level. */
10810 idxs[arridx[depth]] = wordcount[depth];
10811 if (depth > 0)
10812 wordcount[depth - 1] += wordcount[depth];
10813
10814 --depth;
10815 fast_breakcheck();
10816 }
10817 else
10818 {
10819 /* Do one more byte at this node. */
10820 n = arridx[depth] + curi[depth];
10821 ++curi[depth];
10822
10823 c = byts[n];
10824 if (c == 0)
10825 {
10826 /* End of word, count it. */
10827 ++wordcount[depth];
10828
10829 /* Skip over any other NUL bytes (same word with different
10830 * flags). */
10831 while (byts[n + 1] == 0)
10832 {
10833 ++n;
10834 ++curi[depth];
10835 }
10836 }
10837 else
10838 {
10839 /* Normal char, go one level deeper to count the words. */
10840 ++depth;
10841 arridx[depth] = idxs[n];
10842 curi[depth] = 1;
10843 wordcount[depth] = 0;
10844 }
10845 }
10846 }
10847}
10848
10849/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010850 * Free the info put in "*su" by spell_find_suggest().
10851 */
10852 static void
10853spell_find_cleanup(su)
10854 suginfo_T *su;
10855{
10856 int i;
10857
10858 /* Free the suggestions. */
10859 for (i = 0; i < su->su_ga.ga_len; ++i)
10860 vim_free(SUG(su->su_ga, i).st_word);
10861 ga_clear(&su->su_ga);
10862 for (i = 0; i < su->su_sga.ga_len; ++i)
10863 vim_free(SUG(su->su_sga, i).st_word);
10864 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010865
10866 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010867 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010868}
10869
10870/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010871 * Make a copy of "word", with the first letter upper or lower cased, to
10872 * "wcopy[MAXWLEN]". "word" must not be empty.
10873 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010874 */
10875 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010876onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010877 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010878 char_u *wcopy;
10879 int upper; /* TRUE: first letter made upper case */
10880{
10881 char_u *p;
10882 int c;
10883 int l;
10884
10885 p = word;
10886#ifdef FEAT_MBYTE
10887 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010888 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010889 else
10890#endif
10891 c = *p++;
10892 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010893 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010894 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010895 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010896#ifdef FEAT_MBYTE
10897 if (has_mbyte)
10898 l = mb_char2bytes(c, wcopy);
10899 else
10900#endif
10901 {
10902 l = 1;
10903 wcopy[0] = c;
10904 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010905 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010906}
10907
10908/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010909 * Make a copy of "word" with all the letters upper cased into
10910 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010911 */
10912 static void
10913allcap_copy(word, wcopy)
10914 char_u *word;
10915 char_u *wcopy;
10916{
10917 char_u *s;
10918 char_u *d;
10919 int c;
10920
10921 d = wcopy;
10922 for (s = word; *s != NUL; )
10923 {
10924#ifdef FEAT_MBYTE
10925 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010926 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010927 else
10928#endif
10929 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000010930
10931#ifdef FEAT_MBYTE
10932 /* We only change ß to SS when we are certain latin1 is used. It
10933 * would cause weird errors in other 8-bit encodings. */
10934 if (enc_latin1like && c == 0xdf)
10935 {
10936 c = 'S';
10937 if (d - wcopy >= MAXWLEN - 1)
10938 break;
10939 *d++ = c;
10940 }
10941 else
10942#endif
10943 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010944
10945#ifdef FEAT_MBYTE
10946 if (has_mbyte)
10947 {
10948 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
10949 break;
10950 d += mb_char2bytes(c, d);
10951 }
10952 else
10953#endif
10954 {
10955 if (d - wcopy >= MAXWLEN - 1)
10956 break;
10957 *d++ = c;
10958 }
10959 }
10960 *d = NUL;
10961}
10962
10963/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010964 * Try finding suggestions by recognizing specific situations.
10965 */
10966 static void
10967suggest_try_special(su)
10968 suginfo_T *su;
10969{
10970 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010971 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010972 int c;
10973 char_u word[MAXWLEN];
10974
10975 /*
10976 * Recognize a word that is repeated: "the the".
10977 */
10978 p = skiptowhite(su->su_fbadword);
10979 len = p - su->su_fbadword;
10980 p = skipwhite(p);
10981 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
10982 {
10983 /* Include badflags: if the badword is onecap or allcap
10984 * use that for the goodword too: "The the" -> "The". */
10985 c = su->su_fbadword[len];
10986 su->su_fbadword[len] = NUL;
10987 make_case_word(su->su_fbadword, word, su->su_badflags);
10988 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010989
10990 /* Give a soundalike score of 0, compute the score as if deleting one
10991 * character. */
10992 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010993 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010994 }
10995}
10996
10997/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010998 * Try finding suggestions by adding/removing/swapping letters.
10999 */
11000 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000011001suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011002 suginfo_T *su;
11003{
11004 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011005 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011006 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011007 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011008 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011009
11010 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000011011 * to find matches (esp. REP items). Append some more text, changing
11012 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011013 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011014 n = STRLEN(fword);
11015 p = su->su_badptr + su->su_badlen;
11016 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011017
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011018 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011019 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011020 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011021
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011022 /* If reloading a spell file fails it's still in the list but
11023 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011024 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011025 continue;
11026
Bram Moolenaar4770d092006-01-12 23:22:24 +000011027 /* Try it for this language. Will add possible suggestions. */
11028 suggest_trie_walk(su, lp, fword, FALSE);
11029 }
11030}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011031
Bram Moolenaar4770d092006-01-12 23:22:24 +000011032/* Check the maximum score, if we go over it we won't try this change. */
11033#define TRY_DEEPER(su, stack, depth, add) \
11034 (stack[depth].ts_score + (add) < su->su_maxscore)
11035
11036/*
11037 * Try finding suggestions by adding/removing/swapping letters.
11038 *
11039 * This uses a state machine. At each node in the tree we try various
11040 * operations. When trying if an operation works "depth" is increased and the
11041 * stack[] is used to store info. This allows combinations, thus insert one
11042 * character, replace one and delete another. The number of changes is
11043 * limited by su->su_maxscore.
11044 *
11045 * After implementing this I noticed an article by Kemal Oflazer that
11046 * describes something similar: "Error-tolerant Finite State Recognition with
11047 * Applications to Morphological Analysis and Spelling Correction" (1996).
11048 * The implementation in the article is simplified and requires a stack of
11049 * unknown depth. The implementation here only needs a stack depth equal to
11050 * the length of the word.
11051 *
11052 * This is also used for the sound-folded word, "soundfold" is TRUE then.
11053 * The mechanism is the same, but we find a match with a sound-folded word
11054 * that comes from one or more original words. Each of these words may be
11055 * added, this is done by add_sound_suggest().
11056 * Don't use:
11057 * the prefix tree or the keep-case tree
11058 * "su->su_badlen"
11059 * anything to do with upper and lower case
11060 * anything to do with word or non-word characters ("spell_iswordp()")
11061 * banned words
11062 * word flags (rare, region, compounding)
11063 * word splitting for now
11064 * "similar_chars()"
11065 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
11066 */
11067 static void
11068suggest_trie_walk(su, lp, fword, soundfold)
11069 suginfo_T *su;
11070 langp_T *lp;
11071 char_u *fword;
11072 int soundfold;
11073{
11074 char_u tword[MAXWLEN]; /* good word collected so far */
11075 trystate_T stack[MAXWLEN];
11076 char_u preword[MAXWLEN * 3]; /* word found with proper case;
11077 * concatanation of prefix compound
11078 * words and split word. NUL terminated
11079 * when going deeper but not when coming
11080 * back. */
11081 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
11082 trystate_T *sp;
11083 int newscore;
11084 int score;
11085 char_u *byts, *fbyts, *pbyts;
11086 idx_T *idxs, *fidxs, *pidxs;
11087 int depth;
11088 int c, c2, c3;
11089 int n = 0;
11090 int flags;
11091 garray_T *gap;
11092 idx_T arridx;
11093 int len;
11094 char_u *p;
11095 fromto_T *ftp;
11096 int fl = 0, tl;
11097 int repextra = 0; /* extra bytes in fword[] from REP item */
11098 slang_T *slang = lp->lp_slang;
11099 int fword_ends;
11100 int goodword_ends;
11101#ifdef DEBUG_TRIEWALK
11102 /* Stores the name of the change made at each level. */
11103 char_u changename[MAXWLEN][80];
11104#endif
11105 int breakcheckcount = 1000;
11106 int compound_ok;
11107
11108 /*
11109 * Go through the whole case-fold tree, try changes at each node.
11110 * "tword[]" contains the word collected from nodes in the tree.
11111 * "fword[]" the word we are trying to match with (initially the bad
11112 * word).
11113 */
11114 depth = 0;
11115 sp = &stack[0];
11116 vim_memset(sp, 0, sizeof(trystate_T));
11117 sp->ts_curi = 1;
11118
11119 if (soundfold)
11120 {
11121 /* Going through the soundfold tree. */
11122 byts = fbyts = slang->sl_sbyts;
11123 idxs = fidxs = slang->sl_sidxs;
11124 pbyts = NULL;
11125 pidxs = NULL;
11126 sp->ts_prefixdepth = PFD_NOPREFIX;
11127 sp->ts_state = STATE_START;
11128 }
11129 else
11130 {
Bram Moolenaarea424162005-06-16 21:51:00 +000011131 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011132 * When there are postponed prefixes we need to use these first. At
11133 * the end of the prefix we continue in the case-fold tree.
11134 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011135 fbyts = slang->sl_fbyts;
11136 fidxs = slang->sl_fidxs;
11137 pbyts = slang->sl_pbyts;
11138 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011139 if (pbyts != NULL)
11140 {
11141 byts = pbyts;
11142 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011143 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011144 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
11145 }
11146 else
11147 {
11148 byts = fbyts;
11149 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011150 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011151 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011152 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011153 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011154
Bram Moolenaar4770d092006-01-12 23:22:24 +000011155 /*
11156 * Loop to find all suggestions. At each round we either:
11157 * - For the current state try one operation, advance "ts_curi",
11158 * increase "depth".
11159 * - When a state is done go to the next, set "ts_state".
11160 * - When all states are tried decrease "depth".
11161 */
11162 while (depth >= 0 && !got_int)
11163 {
11164 sp = &stack[depth];
11165 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011166 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011167 case STATE_START:
11168 case STATE_NOPREFIX:
11169 /*
11170 * Start of node: Deal with NUL bytes, which means
11171 * tword[] may end here.
11172 */
11173 arridx = sp->ts_arridx; /* current node in the tree */
11174 len = byts[arridx]; /* bytes in this node */
11175 arridx += sp->ts_curi; /* index of current byte */
11176
11177 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011178 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011179 /* Skip over the NUL bytes, we use them later. */
11180 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
11181 ;
11182 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011183
Bram Moolenaar4770d092006-01-12 23:22:24 +000011184 /* Always past NUL bytes now. */
11185 n = (int)sp->ts_state;
11186 sp->ts_state = STATE_ENDNUL;
11187 sp->ts_save_badflags = su->su_badflags;
11188
11189 /* At end of a prefix or at start of prefixtree: check for
11190 * following word. */
11191 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011192 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011193 /* Set su->su_badflags to the caps type at this position.
11194 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000011195#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011196 if (has_mbyte)
11197 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
11198 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000011199#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011200 n = sp->ts_fidx;
11201 flags = badword_captype(su->su_badptr, su->su_badptr + n);
11202 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011203 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011204#ifdef DEBUG_TRIEWALK
11205 sprintf(changename[depth], "prefix");
11206#endif
11207 go_deeper(stack, depth, 0);
11208 ++depth;
11209 sp = &stack[depth];
11210 sp->ts_prefixdepth = depth - 1;
11211 byts = fbyts;
11212 idxs = fidxs;
11213 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011214
Bram Moolenaar4770d092006-01-12 23:22:24 +000011215 /* Move the prefix to preword[] with the right case
11216 * and make find_keepcap_word() works. */
11217 tword[sp->ts_twordlen] = NUL;
11218 make_case_word(tword + sp->ts_splitoff,
11219 preword + sp->ts_prewordlen, flags);
11220 sp->ts_prewordlen = STRLEN(preword);
11221 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011222 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011223 break;
11224 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011225
Bram Moolenaar4770d092006-01-12 23:22:24 +000011226 if (sp->ts_curi > len || byts[arridx] != 0)
11227 {
11228 /* Past bytes in node and/or past NUL bytes. */
11229 sp->ts_state = STATE_ENDNUL;
11230 sp->ts_save_badflags = su->su_badflags;
11231 break;
11232 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011233
Bram Moolenaar4770d092006-01-12 23:22:24 +000011234 /*
11235 * End of word in tree.
11236 */
11237 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011238
Bram Moolenaar4770d092006-01-12 23:22:24 +000011239 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011240
11241 /* Skip words with the NOSUGGEST flag. */
11242 if (flags & WF_NOSUGGEST)
11243 break;
11244
Bram Moolenaar4770d092006-01-12 23:22:24 +000011245 fword_ends = (fword[sp->ts_fidx] == NUL
11246 || (soundfold
11247 ? vim_iswhite(fword[sp->ts_fidx])
11248 : !spell_iswordp(fword + sp->ts_fidx, curbuf)));
11249 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011250
Bram Moolenaar4770d092006-01-12 23:22:24 +000011251 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000011252 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011253 {
11254 /* There was a prefix before the word. Check that the prefix
11255 * can be used with this word. */
11256 /* Count the length of the NULs in the prefix. If there are
11257 * none this must be the first try without a prefix. */
11258 n = stack[sp->ts_prefixdepth].ts_arridx;
11259 len = pbyts[n++];
11260 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
11261 ;
11262 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011263 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011264 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011265 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011266 if (c == 0)
11267 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011268
Bram Moolenaar4770d092006-01-12 23:22:24 +000011269 /* Use the WF_RARE flag for a rare prefix. */
11270 if (c & WF_RAREPFX)
11271 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011272
Bram Moolenaar4770d092006-01-12 23:22:24 +000011273 /* Tricky: when checking for both prefix and compounding
11274 * we run into the prefix flag first.
11275 * Remember that it's OK, so that we accept the prefix
11276 * when arriving at a compound flag. */
11277 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011278 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011279 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011280
Bram Moolenaar4770d092006-01-12 23:22:24 +000011281 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
11282 * appending another compound word below. */
11283 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011284 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011285 goodword_ends = FALSE;
11286 else
11287 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011288
Bram Moolenaar4770d092006-01-12 23:22:24 +000011289 p = NULL;
11290 compound_ok = TRUE;
11291 if (sp->ts_complen > sp->ts_compsplit)
11292 {
11293 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000011294 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011295 /* There was a word before this word. When there was no
11296 * change in this word (it was correct) add the first word
11297 * as a suggestion. If this word was corrected too, we
11298 * need to check if a correct word follows. */
11299 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000011300 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000011301 && STRNCMP(fword + sp->ts_splitfidx,
11302 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000011303 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011304 {
11305 preword[sp->ts_prewordlen] = NUL;
11306 newscore = score_wordcount_adj(slang, sp->ts_score,
11307 preword + sp->ts_prewordlen,
11308 sp->ts_prewordlen > 0);
11309 /* Add the suggestion if the score isn't too bad. */
11310 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000011311 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011312 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011313 newscore, 0, FALSE,
11314 lp->lp_sallang, FALSE);
11315 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000011316 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011317 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000011318 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000011319 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011320 /* There was a compound word before this word. If this
11321 * word does not support compounding then give up
11322 * (splitting is tried for the word without compound
11323 * flag). */
11324 if (((unsigned)flags >> 24) == 0
11325 || sp->ts_twordlen - sp->ts_splitoff
11326 < slang->sl_compminlen)
11327 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011328#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011329 /* For multi-byte chars check character length against
11330 * COMPOUNDMIN. */
11331 if (has_mbyte
11332 && slang->sl_compminlen > 0
11333 && mb_charlen(tword + sp->ts_splitoff)
11334 < slang->sl_compminlen)
11335 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011336#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000011337
Bram Moolenaar4770d092006-01-12 23:22:24 +000011338 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11339 compflags[sp->ts_complen + 1] = NUL;
11340 vim_strncpy(preword + sp->ts_prewordlen,
11341 tword + sp->ts_splitoff,
11342 sp->ts_twordlen - sp->ts_splitoff);
11343 p = preword;
11344 while (*skiptowhite(p) != NUL)
11345 p = skipwhite(skiptowhite(p));
11346 if (fword_ends && !can_compound(slang, p,
11347 compflags + sp->ts_compsplit))
11348 /* Compound is not allowed. But it may still be
11349 * possible if we add another (short) word. */
11350 compound_ok = FALSE;
11351
11352 /* Get pointer to last char of previous word. */
11353 p = preword + sp->ts_prewordlen;
11354 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011355 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011356 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011357
Bram Moolenaar4770d092006-01-12 23:22:24 +000011358 /*
11359 * Form the word with proper case in preword.
11360 * If there is a word from a previous split, append.
11361 * For the soundfold tree don't change the case, simply append.
11362 */
11363 if (soundfold)
11364 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11365 else if (flags & WF_KEEPCAP)
11366 /* Must find the word in the keep-case tree. */
11367 find_keepcap_word(slang, tword + sp->ts_splitoff,
11368 preword + sp->ts_prewordlen);
11369 else
11370 {
11371 /* Include badflags: If the badword is onecap or allcap
11372 * use that for the goodword too. But if the badword is
11373 * allcap and it's only one char long use onecap. */
11374 c = su->su_badflags;
11375 if ((c & WF_ALLCAP)
11376#ifdef FEAT_MBYTE
11377 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11378#else
11379 && su->su_badlen == 1
11380#endif
11381 )
11382 c = WF_ONECAP;
11383 c |= flags;
11384
11385 /* When appending a compound word after a word character don't
11386 * use Onecap. */
11387 if (p != NULL && spell_iswordp_nmw(p))
11388 c &= ~WF_ONECAP;
11389 make_case_word(tword + sp->ts_splitoff,
11390 preword + sp->ts_prewordlen, c);
11391 }
11392
11393 if (!soundfold)
11394 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011395 /* Don't use a banned word. It may appear again as a good
11396 * word, thus remember it. */
11397 if (flags & WF_BANNED)
11398 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000011399 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011400 break;
11401 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011402 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000011403 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11404 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011405 {
11406 if (slang->sl_compprog == NULL)
11407 break;
11408 /* the word so far was banned but we may try compounding */
11409 goodword_ends = FALSE;
11410 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011411 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011412
Bram Moolenaar4770d092006-01-12 23:22:24 +000011413 newscore = 0;
11414 if (!soundfold) /* soundfold words don't have flags */
11415 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011416 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011417 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011418 newscore += SCORE_REGION;
11419 if (flags & WF_RARE)
11420 newscore += SCORE_RARE;
11421
Bram Moolenaar0c405862005-06-22 22:26:26 +000011422 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011423 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011424 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011425 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011426
Bram Moolenaar4770d092006-01-12 23:22:24 +000011427 /* TODO: how about splitting in the soundfold tree? */
11428 if (fword_ends
11429 && goodword_ends
11430 && sp->ts_fidx >= sp->ts_fidxtry
11431 && compound_ok)
11432 {
11433 /* The badword also ends: add suggestions. */
11434#ifdef DEBUG_TRIEWALK
11435 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011436 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011437 int j;
11438
11439 /* print the stack of changes that brought us here */
11440 smsg("------ %s -------", fword);
11441 for (j = 0; j < depth; ++j)
11442 smsg("%s", changename[j]);
11443 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011444#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011445 if (soundfold)
11446 {
11447 /* For soundfolded words we need to find the original
11448 * words, the edit distrance and then add them. */
11449 add_sound_suggest(su, preword, sp->ts_score, lp);
11450 }
11451 else
11452 {
11453 /* Give a penalty when changing non-word char to word
11454 * char, e.g., "thes," -> "these". */
11455 p = fword + sp->ts_fidx;
11456 mb_ptr_back(fword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011457 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011458 {
11459 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011460 mb_ptr_back(preword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011461 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011462 newscore += SCORE_NONWORD;
11463 }
11464
Bram Moolenaar4770d092006-01-12 23:22:24 +000011465 /* Give a bonus to words seen before. */
11466 score = score_wordcount_adj(slang,
11467 sp->ts_score + newscore,
11468 preword + sp->ts_prewordlen,
11469 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011470
Bram Moolenaar4770d092006-01-12 23:22:24 +000011471 /* Add the suggestion if the score isn't too bad. */
11472 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011473 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011474 add_suggestion(su, &su->su_ga, preword,
11475 sp->ts_fidx - repextra,
11476 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011477
11478 if (su->su_badflags & WF_MIXCAP)
11479 {
11480 /* We really don't know if the word should be
11481 * upper or lower case, add both. */
11482 c = captype(preword, NULL);
11483 if (c == 0 || c == WF_ALLCAP)
11484 {
11485 make_case_word(tword + sp->ts_splitoff,
11486 preword + sp->ts_prewordlen,
11487 c == 0 ? WF_ALLCAP : 0);
11488
11489 add_suggestion(su, &su->su_ga, preword,
11490 sp->ts_fidx - repextra,
11491 score + SCORE_ICASE, 0, FALSE,
11492 lp->lp_sallang, FALSE);
11493 }
11494 }
11495 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011496 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011497 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011498
Bram Moolenaar4770d092006-01-12 23:22:24 +000011499 /*
11500 * Try word split and/or compounding.
11501 */
11502 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011503#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011504 /* Don't split halfway a character. */
11505 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011506#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011507 )
11508 {
11509 int try_compound;
11510 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011511
Bram Moolenaar4770d092006-01-12 23:22:24 +000011512 /* If past the end of the bad word don't try a split.
11513 * Otherwise try changing the next word. E.g., find
11514 * suggestions for "the the" where the second "the" is
11515 * different. It's done like a split.
11516 * TODO: word split for soundfold words */
11517 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11518 && !soundfold;
11519
11520 /* Get here in several situations:
11521 * 1. The word in the tree ends:
11522 * If the word allows compounding try that. Otherwise try
11523 * a split by inserting a space. For both check that a
11524 * valid words starts at fword[sp->ts_fidx].
11525 * For NOBREAK do like compounding to be able to check if
11526 * the next word is valid.
11527 * 2. The badword does end, but it was due to a change (e.g.,
11528 * a swap). No need to split, but do check that the
11529 * following word is valid.
11530 * 3. The badword and the word in the tree end. It may still
11531 * be possible to compound another (short) word.
11532 */
11533 try_compound = FALSE;
11534 if (!soundfold
11535 && slang->sl_compprog != NULL
11536 && ((unsigned)flags >> 24) != 0
11537 && sp->ts_twordlen - sp->ts_splitoff
11538 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011539#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011540 && (!has_mbyte
11541 || slang->sl_compminlen == 0
11542 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011543 >= slang->sl_compminlen)
11544#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011545 && (slang->sl_compsylmax < MAXWLEN
11546 || sp->ts_complen + 1 - sp->ts_compsplit
11547 < slang->sl_compmax)
11548 && (byte_in_str(sp->ts_complen == sp->ts_compsplit
11549 ? slang->sl_compstartflags
11550 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +000011551 ((unsigned)flags >> 24))))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011552 {
11553 try_compound = TRUE;
11554 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11555 compflags[sp->ts_complen + 1] = NUL;
11556 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011557
Bram Moolenaar4770d092006-01-12 23:22:24 +000011558 /* For NOBREAK we never try splitting, it won't make any word
11559 * valid. */
11560 if (slang->sl_nobreak)
11561 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011562
Bram Moolenaar4770d092006-01-12 23:22:24 +000011563 /* If we could add a compound word, and it's also possible to
11564 * split at this point, do the split first and set
11565 * TSF_DIDSPLIT to avoid doing it again. */
11566 else if (!fword_ends
11567 && try_compound
11568 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11569 {
11570 try_compound = FALSE;
11571 sp->ts_flags |= TSF_DIDSPLIT;
11572 --sp->ts_curi; /* do the same NUL again */
11573 compflags[sp->ts_complen] = NUL;
11574 }
11575 else
11576 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011577
Bram Moolenaar4770d092006-01-12 23:22:24 +000011578 if (try_split || try_compound)
11579 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011580 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011581 {
11582 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011583 * words so far are valid for compounding. If there
11584 * is only one word it must not have the NEEDCOMPOUND
11585 * flag. */
11586 if (sp->ts_complen == sp->ts_compsplit
11587 && (flags & WF_NEEDCOMP))
11588 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011589 p = preword;
11590 while (*skiptowhite(p) != NUL)
11591 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011592 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011593 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011594 compflags + sp->ts_compsplit))
11595 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011596
11597 if (slang->sl_nosplitsugs)
11598 newscore += SCORE_SPLIT_NO;
11599 else
11600 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011601
11602 /* Give a bonus to words seen before. */
11603 newscore = score_wordcount_adj(slang, newscore,
11604 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011605 }
11606
Bram Moolenaar4770d092006-01-12 23:22:24 +000011607 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011608 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011609 go_deeper(stack, depth, newscore);
11610#ifdef DEBUG_TRIEWALK
11611 if (!try_compound && !fword_ends)
11612 sprintf(changename[depth], "%.*s-%s: split",
11613 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11614 else
11615 sprintf(changename[depth], "%.*s-%s: compound",
11616 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11617#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011618 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011619 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011620 sp->ts_state = STATE_SPLITUNDO;
11621
11622 ++depth;
11623 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011624
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011625 /* Append a space to preword when splitting. */
11626 if (!try_compound && !fword_ends)
11627 STRCAT(preword, " ");
Bram Moolenaar5195e452005-08-19 20:32:47 +000011628 sp->ts_prewordlen = STRLEN(preword);
11629 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011630 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011631
11632 /* If the badword has a non-word character at this
11633 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011634 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011635 * character when the word ends. But only when the
11636 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011637 if (((!try_compound && !spell_iswordp_nmw(fword
11638 + sp->ts_fidx))
11639 || fword_ends)
11640 && fword[sp->ts_fidx] != NUL
11641 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011642 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011643 int l;
11644
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011645#ifdef FEAT_MBYTE
11646 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011647 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011648 else
11649#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011650 l = 1;
11651 if (fword_ends)
11652 {
11653 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011654 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011655 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011656 sp->ts_prewordlen += l;
11657 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011658 }
11659 else
11660 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11661 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011662 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011663
Bram Moolenaard12a1322005-08-21 22:08:24 +000011664 /* When compounding include compound flag in
11665 * compflags[] (already set above). When splitting we
11666 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011667 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011668 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011669 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011670 sp->ts_compsplit = sp->ts_complen;
11671 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011672
Bram Moolenaar53805d12005-08-01 07:08:33 +000011673 /* set su->su_badflags to the caps type at this
11674 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011675#ifdef FEAT_MBYTE
11676 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011677 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011678 else
11679#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011680 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011681 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011682 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011683
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011684 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011685 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011686
11687 /* If there are postponed prefixes, try these too. */
11688 if (pbyts != NULL)
11689 {
11690 byts = pbyts;
11691 idxs = pidxs;
11692 sp->ts_prefixdepth = PFD_PREFIXTREE;
11693 sp->ts_state = STATE_NOPREFIX;
11694 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011695 }
11696 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011697 }
11698 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011699
Bram Moolenaar4770d092006-01-12 23:22:24 +000011700 case STATE_SPLITUNDO:
11701 /* Undo the changes done for word split or compound word. */
11702 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011703
Bram Moolenaar4770d092006-01-12 23:22:24 +000011704 /* Continue looking for NUL bytes. */
11705 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011706
Bram Moolenaar4770d092006-01-12 23:22:24 +000011707 /* In case we went into the prefix tree. */
11708 byts = fbyts;
11709 idxs = fidxs;
11710 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011711
Bram Moolenaar4770d092006-01-12 23:22:24 +000011712 case STATE_ENDNUL:
11713 /* Past the NUL bytes in the node. */
11714 su->su_badflags = sp->ts_save_badflags;
11715 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011716#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011717 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011718#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011719 )
11720 {
11721 /* The badword ends, can't use STATE_PLAIN. */
11722 sp->ts_state = STATE_DEL;
11723 break;
11724 }
11725 sp->ts_state = STATE_PLAIN;
11726 /*FALLTHROUGH*/
11727
11728 case STATE_PLAIN:
11729 /*
11730 * Go over all possible bytes at this node, add each to tword[]
11731 * and use child node. "ts_curi" is the index.
11732 */
11733 arridx = sp->ts_arridx;
11734 if (sp->ts_curi > byts[arridx])
11735 {
11736 /* Done all bytes at this node, do next state. When still at
11737 * already changed bytes skip the other tricks. */
11738 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011739 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011740 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011741 sp->ts_state = STATE_FINAL;
11742 }
11743 else
11744 {
11745 arridx += sp->ts_curi++;
11746 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011747
Bram Moolenaar4770d092006-01-12 23:22:24 +000011748 /* Normal byte, go one level deeper. If it's not equal to the
11749 * byte in the bad word adjust the score. But don't even try
11750 * when the byte was already changed. And don't try when we
11751 * just deleted this byte, accepting it is always cheaper then
11752 * delete + substitute. */
11753 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000011754#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011755 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011756#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011757 )
11758 newscore = 0;
11759 else
11760 newscore = SCORE_SUBST;
11761 if ((newscore == 0
11762 || (sp->ts_fidx >= sp->ts_fidxtry
11763 && ((sp->ts_flags & TSF_DIDDEL) == 0
11764 || c != fword[sp->ts_delidx])))
11765 && TRY_DEEPER(su, stack, depth, newscore))
11766 {
11767 go_deeper(stack, depth, newscore);
11768#ifdef DEBUG_TRIEWALK
11769 if (newscore > 0)
11770 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
11771 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11772 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011773 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011774 sprintf(changename[depth], "%.*s-%s: accept %c",
11775 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11776 fword[sp->ts_fidx]);
11777#endif
11778 ++depth;
11779 sp = &stack[depth];
11780 ++sp->ts_fidx;
11781 tword[sp->ts_twordlen++] = c;
11782 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000011783#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011784 if (newscore == SCORE_SUBST)
11785 sp->ts_isdiff = DIFF_YES;
11786 if (has_mbyte)
11787 {
11788 /* Multi-byte characters are a bit complicated to
11789 * handle: They differ when any of the bytes differ
11790 * and then their length may also differ. */
11791 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011792 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011793 /* First byte. */
11794 sp->ts_tcharidx = 0;
11795 sp->ts_tcharlen = MB_BYTE2LEN(c);
11796 sp->ts_fcharstart = sp->ts_fidx - 1;
11797 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011798 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011799 }
11800 else if (sp->ts_isdiff == DIFF_INSERT)
11801 /* When inserting trail bytes don't advance in the
11802 * bad word. */
11803 --sp->ts_fidx;
11804 if (++sp->ts_tcharidx == sp->ts_tcharlen)
11805 {
11806 /* Last byte of character. */
11807 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000011808 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011809 /* Correct ts_fidx for the byte length of the
11810 * character (we didn't check that before). */
11811 sp->ts_fidx = sp->ts_fcharstart
11812 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000011813 fword[sp->ts_fcharstart]);
11814
Bram Moolenaar4770d092006-01-12 23:22:24 +000011815 /* For changing a composing character adjust
11816 * the score from SCORE_SUBST to
11817 * SCORE_SUBCOMP. */
11818 if (enc_utf8
11819 && utf_iscomposing(
11820 mb_ptr2char(tword
11821 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011822 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011823 && utf_iscomposing(
11824 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011825 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011826 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011827 SCORE_SUBST - SCORE_SUBCOMP;
11828
Bram Moolenaar4770d092006-01-12 23:22:24 +000011829 /* For a similar character adjust score from
11830 * SCORE_SUBST to SCORE_SIMILAR. */
11831 else if (!soundfold
11832 && slang->sl_has_map
11833 && similar_chars(slang,
11834 mb_ptr2char(tword
11835 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000011836 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000011837 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000011838 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011839 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000011840 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000011841 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011842 else if (sp->ts_isdiff == DIFF_INSERT
11843 && sp->ts_twordlen > sp->ts_tcharlen)
11844 {
11845 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
11846 c = mb_ptr2char(p);
11847 if (enc_utf8 && utf_iscomposing(c))
11848 {
11849 /* Inserting a composing char doesn't
11850 * count that much. */
11851 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
11852 }
11853 else
11854 {
11855 /* If the previous character was the same,
11856 * thus doubling a character, give a bonus
11857 * to the score. Also for the soundfold
11858 * tree (might seem illogical but does
11859 * give better scores). */
11860 mb_ptr_back(tword, p);
11861 if (c == mb_ptr2char(p))
11862 sp->ts_score -= SCORE_INS
11863 - SCORE_INSDUP;
11864 }
11865 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011866
Bram Moolenaar4770d092006-01-12 23:22:24 +000011867 /* Starting a new char, reset the length. */
11868 sp->ts_tcharlen = 0;
11869 }
Bram Moolenaarea408852005-06-25 22:49:46 +000011870 }
Bram Moolenaarea424162005-06-16 21:51:00 +000011871 else
11872#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000011873 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011874 /* If we found a similar char adjust the score.
11875 * We do this after calling go_deeper() because
11876 * it's slow. */
11877 if (newscore != 0
11878 && !soundfold
11879 && slang->sl_has_map
11880 && similar_chars(slang,
11881 c, fword[sp->ts_fidx - 1]))
11882 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000011883 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011884 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011885 }
11886 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011887
Bram Moolenaar4770d092006-01-12 23:22:24 +000011888 case STATE_DEL:
11889#ifdef FEAT_MBYTE
11890 /* When past the first byte of a multi-byte char don't try
11891 * delete/insert/swap a character. */
11892 if (has_mbyte && sp->ts_tcharlen > 0)
11893 {
11894 sp->ts_state = STATE_FINAL;
11895 break;
11896 }
11897#endif
11898 /*
11899 * Try skipping one character in the bad word (delete it).
11900 */
11901 sp->ts_state = STATE_INS_PREP;
11902 sp->ts_curi = 1;
11903 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
11904 /* Deleting a vowel at the start of a word counts less, see
11905 * soundalike_score(). */
11906 newscore = 2 * SCORE_DEL / 3;
11907 else
11908 newscore = SCORE_DEL;
11909 if (fword[sp->ts_fidx] != NUL
11910 && TRY_DEEPER(su, stack, depth, newscore))
11911 {
11912 go_deeper(stack, depth, newscore);
11913#ifdef DEBUG_TRIEWALK
11914 sprintf(changename[depth], "%.*s-%s: delete %c",
11915 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11916 fword[sp->ts_fidx]);
11917#endif
11918 ++depth;
11919
11920 /* Remember what character we deleted, so that we can avoid
11921 * inserting it again. */
11922 stack[depth].ts_flags |= TSF_DIDDEL;
11923 stack[depth].ts_delidx = sp->ts_fidx;
11924
11925 /* Advance over the character in fword[]. Give a bonus to the
11926 * score if the same character is following "nn" -> "n". It's
11927 * a bit illogical for soundfold tree but it does give better
11928 * results. */
11929#ifdef FEAT_MBYTE
11930 if (has_mbyte)
11931 {
11932 c = mb_ptr2char(fword + sp->ts_fidx);
11933 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
11934 if (enc_utf8 && utf_iscomposing(c))
11935 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
11936 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
11937 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
11938 }
11939 else
11940#endif
11941 {
11942 ++stack[depth].ts_fidx;
11943 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
11944 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
11945 }
11946 break;
11947 }
11948 /*FALLTHROUGH*/
11949
11950 case STATE_INS_PREP:
11951 if (sp->ts_flags & TSF_DIDDEL)
11952 {
11953 /* If we just deleted a byte then inserting won't make sense,
11954 * a substitute is always cheaper. */
11955 sp->ts_state = STATE_SWAP;
11956 break;
11957 }
11958
11959 /* skip over NUL bytes */
11960 n = sp->ts_arridx;
11961 for (;;)
11962 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011963 if (sp->ts_curi > byts[n])
11964 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011965 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011966 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011967 break;
11968 }
11969 if (byts[n + sp->ts_curi] != NUL)
11970 {
11971 /* Found a byte to insert. */
11972 sp->ts_state = STATE_INS;
11973 break;
11974 }
11975 ++sp->ts_curi;
11976 }
11977 break;
11978
11979 /*FALLTHROUGH*/
11980
11981 case STATE_INS:
11982 /* Insert one byte. Repeat this for each possible byte at this
11983 * node. */
11984 n = sp->ts_arridx;
11985 if (sp->ts_curi > byts[n])
11986 {
11987 /* Done all bytes at this node, go to next state. */
11988 sp->ts_state = STATE_SWAP;
11989 break;
11990 }
11991
11992 /* Do one more byte at this node, but:
11993 * - Skip NUL bytes.
11994 * - Skip the byte if it's equal to the byte in the word,
11995 * accepting that byte is always better.
11996 */
11997 n += sp->ts_curi++;
11998 c = byts[n];
11999 if (soundfold && sp->ts_twordlen == 0 && c == '*')
12000 /* Inserting a vowel at the start of a word counts less,
12001 * see soundalike_score(). */
12002 newscore = 2 * SCORE_INS / 3;
12003 else
12004 newscore = SCORE_INS;
12005 if (c != fword[sp->ts_fidx]
12006 && TRY_DEEPER(su, stack, depth, newscore))
12007 {
12008 go_deeper(stack, depth, newscore);
12009#ifdef DEBUG_TRIEWALK
12010 sprintf(changename[depth], "%.*s-%s: insert %c",
12011 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12012 c);
12013#endif
12014 ++depth;
12015 sp = &stack[depth];
12016 tword[sp->ts_twordlen++] = c;
12017 sp->ts_arridx = idxs[n];
12018#ifdef FEAT_MBYTE
12019 if (has_mbyte)
12020 {
12021 fl = MB_BYTE2LEN(c);
12022 if (fl > 1)
12023 {
12024 /* There are following bytes for the same character.
12025 * We must find all bytes before trying
12026 * delete/insert/swap/etc. */
12027 sp->ts_tcharlen = fl;
12028 sp->ts_tcharidx = 1;
12029 sp->ts_isdiff = DIFF_INSERT;
12030 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012031 }
12032 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012033 fl = 1;
12034 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000012035#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012036 {
12037 /* If the previous character was the same, thus doubling a
12038 * character, give a bonus to the score. Also for
12039 * soundfold words (illogical but does give a better
12040 * score). */
12041 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000012042 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012043 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012044 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012045 }
12046 break;
12047
12048 case STATE_SWAP:
12049 /*
12050 * Swap two bytes in the bad word: "12" -> "21".
12051 * We change "fword" here, it's changed back afterwards at
12052 * STATE_UNSWAP.
12053 */
12054 p = fword + sp->ts_fidx;
12055 c = *p;
12056 if (c == NUL)
12057 {
12058 /* End of word, can't swap or replace. */
12059 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012060 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012061 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012062
Bram Moolenaar4770d092006-01-12 23:22:24 +000012063 /* Don't swap if the first character is not a word character.
12064 * SWAP3 etc. also don't make sense then. */
12065 if (!soundfold && !spell_iswordp(p, curbuf))
12066 {
12067 sp->ts_state = STATE_REP_INI;
12068 break;
12069 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012070
Bram Moolenaar4770d092006-01-12 23:22:24 +000012071#ifdef FEAT_MBYTE
12072 if (has_mbyte)
12073 {
12074 n = mb_cptr2len(p);
12075 c = mb_ptr2char(p);
12076 if (!soundfold && !spell_iswordp(p + n, curbuf))
12077 c2 = c; /* don't swap non-word char */
12078 else
12079 c2 = mb_ptr2char(p + n);
12080 }
12081 else
12082#endif
12083 {
12084 if (!soundfold && !spell_iswordp(p + 1, curbuf))
12085 c2 = c; /* don't swap non-word char */
12086 else
12087 c2 = p[1];
12088 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012089
Bram Moolenaar4770d092006-01-12 23:22:24 +000012090 /* When characters are identical, swap won't do anything.
12091 * Also get here if the second char is not a word character. */
12092 if (c == c2)
12093 {
12094 sp->ts_state = STATE_SWAP3;
12095 break;
12096 }
12097 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
12098 {
12099 go_deeper(stack, depth, SCORE_SWAP);
12100#ifdef DEBUG_TRIEWALK
12101 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
12102 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12103 c, c2);
12104#endif
12105 sp->ts_state = STATE_UNSWAP;
12106 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012107#ifdef FEAT_MBYTE
12108 if (has_mbyte)
12109 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012110 fl = mb_char2len(c2);
12111 mch_memmove(p, p + n, fl);
12112 mb_char2bytes(c, p + fl);
12113 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012114 }
12115 else
12116#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012117 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012118 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012119 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012120 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012121 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012122 }
12123 else
12124 /* If this swap doesn't work then SWAP3 won't either. */
12125 sp->ts_state = STATE_REP_INI;
12126 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000012127
Bram Moolenaar4770d092006-01-12 23:22:24 +000012128 case STATE_UNSWAP:
12129 /* Undo the STATE_SWAP swap: "21" -> "12". */
12130 p = fword + sp->ts_fidx;
12131#ifdef FEAT_MBYTE
12132 if (has_mbyte)
12133 {
12134 n = MB_BYTE2LEN(*p);
12135 c = mb_ptr2char(p + n);
12136 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
12137 mb_char2bytes(c, p);
12138 }
12139 else
12140#endif
12141 {
12142 c = *p;
12143 *p = p[1];
12144 p[1] = c;
12145 }
12146 /*FALLTHROUGH*/
12147
12148 case STATE_SWAP3:
12149 /* Swap two bytes, skipping one: "123" -> "321". We change
12150 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
12151 p = fword + sp->ts_fidx;
12152#ifdef FEAT_MBYTE
12153 if (has_mbyte)
12154 {
12155 n = mb_cptr2len(p);
12156 c = mb_ptr2char(p);
12157 fl = mb_cptr2len(p + n);
12158 c2 = mb_ptr2char(p + n);
12159 if (!soundfold && !spell_iswordp(p + n + fl, curbuf))
12160 c3 = c; /* don't swap non-word char */
12161 else
12162 c3 = mb_ptr2char(p + n + fl);
12163 }
12164 else
12165#endif
12166 {
12167 c = *p;
12168 c2 = p[1];
12169 if (!soundfold && !spell_iswordp(p + 2, curbuf))
12170 c3 = c; /* don't swap non-word char */
12171 else
12172 c3 = p[2];
12173 }
12174
12175 /* When characters are identical: "121" then SWAP3 result is
12176 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
12177 * same as SWAP on next char: "112". Thus skip all swapping.
12178 * Also skip when c3 is NUL.
12179 * Also get here when the third character is not a word character.
12180 * Second character may any char: "a.b" -> "b.a" */
12181 if (c == c3 || c3 == NUL)
12182 {
12183 sp->ts_state = STATE_REP_INI;
12184 break;
12185 }
12186 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12187 {
12188 go_deeper(stack, depth, SCORE_SWAP3);
12189#ifdef DEBUG_TRIEWALK
12190 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
12191 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12192 c, c3);
12193#endif
12194 sp->ts_state = STATE_UNSWAP3;
12195 ++depth;
12196#ifdef FEAT_MBYTE
12197 if (has_mbyte)
12198 {
12199 tl = mb_char2len(c3);
12200 mch_memmove(p, p + n + fl, tl);
12201 mb_char2bytes(c2, p + tl);
12202 mb_char2bytes(c, p + fl + tl);
12203 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
12204 }
12205 else
12206#endif
12207 {
12208 p[0] = p[2];
12209 p[2] = c;
12210 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12211 }
12212 }
12213 else
12214 sp->ts_state = STATE_REP_INI;
12215 break;
12216
12217 case STATE_UNSWAP3:
12218 /* Undo STATE_SWAP3: "321" -> "123" */
12219 p = fword + sp->ts_fidx;
12220#ifdef FEAT_MBYTE
12221 if (has_mbyte)
12222 {
12223 n = MB_BYTE2LEN(*p);
12224 c2 = mb_ptr2char(p + n);
12225 fl = MB_BYTE2LEN(p[n]);
12226 c = mb_ptr2char(p + n + fl);
12227 tl = MB_BYTE2LEN(p[n + fl]);
12228 mch_memmove(p + fl + tl, p, n);
12229 mb_char2bytes(c, p);
12230 mb_char2bytes(c2, p + tl);
12231 p = p + tl;
12232 }
12233 else
12234#endif
12235 {
12236 c = *p;
12237 *p = p[2];
12238 p[2] = c;
12239 ++p;
12240 }
12241
12242 if (!soundfold && !spell_iswordp(p, curbuf))
12243 {
12244 /* Middle char is not a word char, skip the rotate. First and
12245 * third char were already checked at swap and swap3. */
12246 sp->ts_state = STATE_REP_INI;
12247 break;
12248 }
12249
12250 /* Rotate three characters left: "123" -> "231". We change
12251 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
12252 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12253 {
12254 go_deeper(stack, depth, SCORE_SWAP3);
12255#ifdef DEBUG_TRIEWALK
12256 p = fword + sp->ts_fidx;
12257 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
12258 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12259 p[0], p[1], p[2]);
12260#endif
12261 sp->ts_state = STATE_UNROT3L;
12262 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012263 p = fword + sp->ts_fidx;
12264#ifdef FEAT_MBYTE
12265 if (has_mbyte)
12266 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012267 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012268 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012269 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012270 fl += mb_cptr2len(p + n + fl);
12271 mch_memmove(p, p + n, fl);
12272 mb_char2bytes(c, p + fl);
12273 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012274 }
12275 else
12276#endif
12277 {
12278 c = *p;
12279 *p = p[1];
12280 p[1] = p[2];
12281 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012282 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000012283 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012284 }
12285 else
12286 sp->ts_state = STATE_REP_INI;
12287 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012288
Bram Moolenaar4770d092006-01-12 23:22:24 +000012289 case STATE_UNROT3L:
12290 /* Undo ROT3L: "231" -> "123" */
12291 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000012292#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012293 if (has_mbyte)
12294 {
12295 n = MB_BYTE2LEN(*p);
12296 n += MB_BYTE2LEN(p[n]);
12297 c = mb_ptr2char(p + n);
12298 tl = MB_BYTE2LEN(p[n]);
12299 mch_memmove(p + tl, p, n);
12300 mb_char2bytes(c, p);
12301 }
12302 else
Bram Moolenaarea424162005-06-16 21:51:00 +000012303#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012304 {
12305 c = p[2];
12306 p[2] = p[1];
12307 p[1] = *p;
12308 *p = c;
12309 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012310
Bram Moolenaar4770d092006-01-12 23:22:24 +000012311 /* Rotate three bytes right: "123" -> "312". We change "fword"
12312 * here, it's changed back afterwards at STATE_UNROT3R. */
12313 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12314 {
12315 go_deeper(stack, depth, SCORE_SWAP3);
12316#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012317 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012318 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
12319 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12320 p[0], p[1], p[2]);
12321#endif
12322 sp->ts_state = STATE_UNROT3R;
12323 ++depth;
12324 p = fword + sp->ts_fidx;
12325#ifdef FEAT_MBYTE
12326 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012327 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012328 n = mb_cptr2len(p);
12329 n += mb_cptr2len(p + n);
12330 c = mb_ptr2char(p + n);
12331 tl = mb_cptr2len(p + n);
12332 mch_memmove(p + tl, p, n);
12333 mb_char2bytes(c, p);
12334 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012335 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012336 else
12337#endif
12338 {
12339 c = p[2];
12340 p[2] = p[1];
12341 p[1] = *p;
12342 *p = c;
12343 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12344 }
12345 }
12346 else
12347 sp->ts_state = STATE_REP_INI;
12348 break;
12349
12350 case STATE_UNROT3R:
12351 /* Undo ROT3R: "312" -> "123" */
12352 p = fword + sp->ts_fidx;
12353#ifdef FEAT_MBYTE
12354 if (has_mbyte)
12355 {
12356 c = mb_ptr2char(p);
12357 tl = MB_BYTE2LEN(*p);
12358 n = MB_BYTE2LEN(p[tl]);
12359 n += MB_BYTE2LEN(p[tl + n]);
12360 mch_memmove(p, p + tl, n);
12361 mb_char2bytes(c, p + n);
12362 }
12363 else
12364#endif
12365 {
12366 c = *p;
12367 *p = p[1];
12368 p[1] = p[2];
12369 p[2] = c;
12370 }
12371 /*FALLTHROUGH*/
12372
12373 case STATE_REP_INI:
12374 /* Check if matching with REP items from the .aff file would work.
12375 * Quickly skip if:
12376 * - there are no REP items and we are not in the soundfold trie
12377 * - the score is going to be too high anyway
12378 * - already applied a REP item or swapped here */
12379 if ((lp->lp_replang == NULL && !soundfold)
12380 || sp->ts_score + SCORE_REP >= su->su_maxscore
12381 || sp->ts_fidx < sp->ts_fidxtry)
12382 {
12383 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012384 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012385 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012386
Bram Moolenaar4770d092006-01-12 23:22:24 +000012387 /* Use the first byte to quickly find the first entry that may
12388 * match. If the index is -1 there is none. */
12389 if (soundfold)
12390 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12391 else
12392 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012393
Bram Moolenaar4770d092006-01-12 23:22:24 +000012394 if (sp->ts_curi < 0)
12395 {
12396 sp->ts_state = STATE_FINAL;
12397 break;
12398 }
12399
12400 sp->ts_state = STATE_REP;
12401 /*FALLTHROUGH*/
12402
12403 case STATE_REP:
12404 /* Try matching with REP items from the .aff file. For each match
12405 * replace the characters and check if the resulting word is
12406 * valid. */
12407 p = fword + sp->ts_fidx;
12408
12409 if (soundfold)
12410 gap = &slang->sl_repsal;
12411 else
12412 gap = &lp->lp_replang->sl_rep;
12413 while (sp->ts_curi < gap->ga_len)
12414 {
12415 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12416 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012417 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012418 /* past possible matching entries */
12419 sp->ts_curi = gap->ga_len;
12420 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012421 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012422 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12423 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12424 {
12425 go_deeper(stack, depth, SCORE_REP);
12426#ifdef DEBUG_TRIEWALK
12427 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12428 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12429 ftp->ft_from, ftp->ft_to);
12430#endif
12431 /* Need to undo this afterwards. */
12432 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012433
Bram Moolenaar4770d092006-01-12 23:22:24 +000012434 /* Change the "from" to the "to" string. */
12435 ++depth;
12436 fl = STRLEN(ftp->ft_from);
12437 tl = STRLEN(ftp->ft_to);
12438 if (fl != tl)
12439 {
12440 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
12441 repextra += tl - fl;
12442 }
12443 mch_memmove(p, ftp->ft_to, tl);
12444 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12445#ifdef FEAT_MBYTE
12446 stack[depth].ts_tcharlen = 0;
12447#endif
12448 break;
12449 }
12450 }
12451
12452 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
12453 /* No (more) matches. */
12454 sp->ts_state = STATE_FINAL;
12455
12456 break;
12457
12458 case STATE_REP_UNDO:
12459 /* Undo a REP replacement and continue with the next one. */
12460 if (soundfold)
12461 gap = &slang->sl_repsal;
12462 else
12463 gap = &lp->lp_replang->sl_rep;
12464 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
12465 fl = STRLEN(ftp->ft_from);
12466 tl = STRLEN(ftp->ft_to);
12467 p = fword + sp->ts_fidx;
12468 if (fl != tl)
12469 {
12470 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
12471 repextra -= tl - fl;
12472 }
12473 mch_memmove(p, ftp->ft_from, fl);
12474 sp->ts_state = STATE_REP;
12475 break;
12476
12477 default:
12478 /* Did all possible states at this level, go up one level. */
12479 --depth;
12480
12481 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12482 {
12483 /* Continue in or go back to the prefix tree. */
12484 byts = pbyts;
12485 idxs = pidxs;
12486 }
12487
12488 /* Don't check for CTRL-C too often, it takes time. */
12489 if (--breakcheckcount == 0)
12490 {
12491 ui_breakcheck();
12492 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012493 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012494 }
12495 }
12496}
12497
Bram Moolenaar4770d092006-01-12 23:22:24 +000012498
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012499/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012500 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012501 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012502 static void
12503go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012504 trystate_T *stack;
12505 int depth;
12506 int score_add;
12507{
Bram Moolenaarea424162005-06-16 21:51:00 +000012508 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012509 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012510 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012511 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012512 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012513}
12514
Bram Moolenaar53805d12005-08-01 07:08:33 +000012515#ifdef FEAT_MBYTE
12516/*
12517 * Case-folding may change the number of bytes: Count nr of chars in
12518 * fword[flen] and return the byte length of that many chars in "word".
12519 */
12520 static int
12521nofold_len(fword, flen, word)
12522 char_u *fword;
12523 int flen;
12524 char_u *word;
12525{
12526 char_u *p;
12527 int i = 0;
12528
12529 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12530 ++i;
12531 for (p = word; i > 0; mb_ptr_adv(p))
12532 --i;
12533 return (int)(p - word);
12534}
12535#endif
12536
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012537/*
12538 * "fword" is a good word with case folded. Find the matching keep-case
12539 * words and put it in "kword".
12540 * Theoretically there could be several keep-case words that result in the
12541 * same case-folded word, but we only find one...
12542 */
12543 static void
12544find_keepcap_word(slang, fword, kword)
12545 slang_T *slang;
12546 char_u *fword;
12547 char_u *kword;
12548{
12549 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12550 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012551 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012552
12553 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012554 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012555 int round[MAXWLEN];
12556 int fwordidx[MAXWLEN];
12557 int uwordidx[MAXWLEN];
12558 int kwordlen[MAXWLEN];
12559
12560 int flen, ulen;
12561 int l;
12562 int len;
12563 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012564 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012565 char_u *p;
12566 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012567 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012568
12569 if (byts == NULL)
12570 {
12571 /* array is empty: "cannot happen" */
12572 *kword = NUL;
12573 return;
12574 }
12575
12576 /* Make an all-cap version of "fword". */
12577 allcap_copy(fword, uword);
12578
12579 /*
12580 * Each character needs to be tried both case-folded and upper-case.
12581 * All this gets very complicated if we keep in mind that changing case
12582 * may change the byte length of a multi-byte character...
12583 */
12584 depth = 0;
12585 arridx[0] = 0;
12586 round[0] = 0;
12587 fwordidx[0] = 0;
12588 uwordidx[0] = 0;
12589 kwordlen[0] = 0;
12590 while (depth >= 0)
12591 {
12592 if (fword[fwordidx[depth]] == NUL)
12593 {
12594 /* We are at the end of "fword". If the tree allows a word to end
12595 * here we have found a match. */
12596 if (byts[arridx[depth] + 1] == 0)
12597 {
12598 kword[kwordlen[depth]] = NUL;
12599 return;
12600 }
12601
12602 /* kword is getting too long, continue one level up */
12603 --depth;
12604 }
12605 else if (++round[depth] > 2)
12606 {
12607 /* tried both fold-case and upper-case character, continue one
12608 * level up */
12609 --depth;
12610 }
12611 else
12612 {
12613 /*
12614 * round[depth] == 1: Try using the folded-case character.
12615 * round[depth] == 2: Try using the upper-case character.
12616 */
12617#ifdef FEAT_MBYTE
12618 if (has_mbyte)
12619 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012620 flen = mb_cptr2len(fword + fwordidx[depth]);
12621 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012622 }
12623 else
12624#endif
12625 ulen = flen = 1;
12626 if (round[depth] == 1)
12627 {
12628 p = fword + fwordidx[depth];
12629 l = flen;
12630 }
12631 else
12632 {
12633 p = uword + uwordidx[depth];
12634 l = ulen;
12635 }
12636
12637 for (tryidx = arridx[depth]; l > 0; --l)
12638 {
12639 /* Perform a binary search in the list of accepted bytes. */
12640 len = byts[tryidx++];
12641 c = *p++;
12642 lo = tryidx;
12643 hi = tryidx + len - 1;
12644 while (lo < hi)
12645 {
12646 m = (lo + hi) / 2;
12647 if (byts[m] > c)
12648 hi = m - 1;
12649 else if (byts[m] < c)
12650 lo = m + 1;
12651 else
12652 {
12653 lo = hi = m;
12654 break;
12655 }
12656 }
12657
12658 /* Stop if there is no matching byte. */
12659 if (hi < lo || byts[lo] != c)
12660 break;
12661
12662 /* Continue at the child (if there is one). */
12663 tryidx = idxs[lo];
12664 }
12665
12666 if (l == 0)
12667 {
12668 /*
12669 * Found the matching char. Copy it to "kword" and go a
12670 * level deeper.
12671 */
12672 if (round[depth] == 1)
12673 {
12674 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12675 flen);
12676 kwordlen[depth + 1] = kwordlen[depth] + flen;
12677 }
12678 else
12679 {
12680 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12681 ulen);
12682 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12683 }
12684 fwordidx[depth + 1] = fwordidx[depth] + flen;
12685 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12686
12687 ++depth;
12688 arridx[depth] = tryidx;
12689 round[depth] = 0;
12690 }
12691 }
12692 }
12693
12694 /* Didn't find it: "cannot happen". */
12695 *kword = NUL;
12696}
12697
12698/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012699 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12700 * su->su_sga.
12701 */
12702 static void
12703score_comp_sal(su)
12704 suginfo_T *su;
12705{
12706 langp_T *lp;
12707 char_u badsound[MAXWLEN];
12708 int i;
12709 suggest_T *stp;
12710 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012711 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012712 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012713
12714 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
12715 return;
12716
12717 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012718 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012719 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012720 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012721 if (lp->lp_slang->sl_sal.ga_len > 0)
12722 {
12723 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012724 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012725
12726 for (i = 0; i < su->su_ga.ga_len; ++i)
12727 {
12728 stp = &SUG(su->su_ga, i);
12729
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012730 /* Case-fold the suggested word, sound-fold it and compute the
12731 * sound-a-like score. */
12732 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012733 if (score < SCORE_MAXMAX)
12734 {
12735 /* Add the suggestion. */
12736 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
12737 sstp->st_word = vim_strsave(stp->st_word);
12738 if (sstp->st_word != NULL)
12739 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012740 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012741 sstp->st_score = score;
12742 sstp->st_altscore = 0;
12743 sstp->st_orglen = stp->st_orglen;
12744 ++su->su_sga.ga_len;
12745 }
12746 }
12747 }
12748 break;
12749 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012750 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012751}
12752
12753/*
12754 * Combine the list of suggestions in su->su_ga and su->su_sga.
12755 * They are intwined.
12756 */
12757 static void
12758score_combine(su)
12759 suginfo_T *su;
12760{
12761 int i;
12762 int j;
12763 garray_T ga;
12764 garray_T *gap;
12765 langp_T *lp;
12766 suggest_T *stp;
12767 char_u *p;
12768 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012769 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012770 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012771 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012772
12773 /* Add the alternate score to su_ga. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012774 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012775 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012776 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012777 if (lp->lp_slang->sl_sal.ga_len > 0)
12778 {
12779 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012780 slang = lp->lp_slang;
12781 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012782
12783 for (i = 0; i < su->su_ga.ga_len; ++i)
12784 {
12785 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012786 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012787 if (stp->st_altscore == SCORE_MAXMAX)
12788 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
12789 else
12790 stp->st_score = (stp->st_score * 3
12791 + stp->st_altscore) / 4;
12792 stp->st_salscore = FALSE;
12793 }
12794 break;
12795 }
12796 }
12797
Bram Moolenaar4770d092006-01-12 23:22:24 +000012798 if (slang == NULL) /* just in case */
12799 return;
12800
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012801 /* Add the alternate score to su_sga. */
12802 for (i = 0; i < su->su_sga.ga_len; ++i)
12803 {
12804 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012805 stp->st_altscore = spell_edit_score(slang,
12806 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012807 if (stp->st_score == SCORE_MAXMAX)
12808 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
12809 else
12810 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
12811 stp->st_salscore = TRUE;
12812 }
12813
Bram Moolenaar4770d092006-01-12 23:22:24 +000012814 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
12815 * for both lists. */
12816 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012817 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012818 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012819 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
12820
12821 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
12822 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
12823 return;
12824
12825 stp = &SUG(ga, 0);
12826 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
12827 {
12828 /* round 1: get a suggestion from su_ga
12829 * round 2: get a suggestion from su_sga */
12830 for (round = 1; round <= 2; ++round)
12831 {
12832 gap = round == 1 ? &su->su_ga : &su->su_sga;
12833 if (i < gap->ga_len)
12834 {
12835 /* Don't add a word if it's already there. */
12836 p = SUG(*gap, i).st_word;
12837 for (j = 0; j < ga.ga_len; ++j)
12838 if (STRCMP(stp[j].st_word, p) == 0)
12839 break;
12840 if (j == ga.ga_len)
12841 stp[ga.ga_len++] = SUG(*gap, i);
12842 else
12843 vim_free(p);
12844 }
12845 }
12846 }
12847
12848 ga_clear(&su->su_ga);
12849 ga_clear(&su->su_sga);
12850
12851 /* Truncate the list to the number of suggestions that will be displayed. */
12852 if (ga.ga_len > su->su_maxcount)
12853 {
12854 for (i = su->su_maxcount; i < ga.ga_len; ++i)
12855 vim_free(stp[i].st_word);
12856 ga.ga_len = su->su_maxcount;
12857 }
12858
12859 su->su_ga = ga;
12860}
12861
12862/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012863 * For the goodword in "stp" compute the soundalike score compared to the
12864 * badword.
12865 */
12866 static int
12867stp_sal_score(stp, su, slang, badsound)
12868 suggest_T *stp;
12869 suginfo_T *su;
12870 slang_T *slang;
12871 char_u *badsound; /* sound-folded badword */
12872{
12873 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012874 char_u *pbad;
12875 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012876 char_u badsound2[MAXWLEN];
12877 char_u fword[MAXWLEN];
12878 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012879 char_u goodword[MAXWLEN];
12880 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012881
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012882 lendiff = (int)(su->su_badlen - stp->st_orglen);
12883 if (lendiff >= 0)
12884 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012885 else
12886 {
12887 /* soundfold the bad word with more characters following */
12888 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
12889
12890 /* When joining two words the sound often changes a lot. E.g., "t he"
12891 * sounds like "t h" while "the" sounds like "@". Avoid that by
12892 * removing the space. Don't do it when the good word also contains a
12893 * space. */
12894 if (vim_iswhite(su->su_badptr[su->su_badlen])
12895 && *skiptowhite(stp->st_word) == NUL)
12896 for (p = fword; *(p = skiptowhite(p)) != NUL; )
12897 mch_memmove(p, p + 1, STRLEN(p));
12898
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012899 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012900 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012901 }
12902
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012903 if (lendiff > 0)
12904 {
12905 /* Add part of the bad word to the good word, so that we soundfold
12906 * what replaces the bad word. */
12907 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012908 vim_strncpy(goodword + stp->st_wordlen,
12909 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012910 pgood = goodword;
12911 }
12912 else
12913 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012914
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012915 /* Sound-fold the word and compute the score for the difference. */
12916 spell_soundfold(slang, pgood, FALSE, goodsound);
12917
12918 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012919}
12920
Bram Moolenaar4770d092006-01-12 23:22:24 +000012921/* structure used to store soundfolded words that add_sound_suggest() has
12922 * handled already. */
12923typedef struct
12924{
12925 short sft_score; /* lowest score used */
12926 char_u sft_word[1]; /* soundfolded word, actually longer */
12927} sftword_T;
12928
12929static sftword_T dumsft;
12930#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
12931#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
12932
12933/*
12934 * Prepare for calling suggest_try_soundalike().
12935 */
12936 static void
12937suggest_try_soundalike_prep()
12938{
12939 langp_T *lp;
12940 int lpi;
12941 slang_T *slang;
12942
12943 /* Do this for all languages that support sound folding and for which a
12944 * .sug file has been loaded. */
12945 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
12946 {
12947 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
12948 slang = lp->lp_slang;
12949 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
12950 /* prepare the hashtable used by add_sound_suggest() */
12951 hash_init(&slang->sl_sounddone);
12952 }
12953}
12954
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012955/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012956 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012957 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012958 */
12959 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000012960suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012961 suginfo_T *su;
12962{
12963 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012964 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012965 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012966 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012967
Bram Moolenaar4770d092006-01-12 23:22:24 +000012968 /* Do this for all languages that support sound folding and for which a
12969 * .sug file has been loaded. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012970 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012971 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012972 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
12973 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012974 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012975 {
12976 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012977 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012978
Bram Moolenaar4770d092006-01-12 23:22:24 +000012979 /* try all kinds of inserts/deletes/swaps/etc. */
12980 /* TODO: also soundfold the next words, so that we can try joining
12981 * and splitting */
12982 suggest_trie_walk(su, lp, salword, TRUE);
12983 }
12984 }
12985}
12986
12987/*
12988 * Finish up after calling suggest_try_soundalike().
12989 */
12990 static void
12991suggest_try_soundalike_finish()
12992{
12993 langp_T *lp;
12994 int lpi;
12995 slang_T *slang;
12996 int todo;
12997 hashitem_T *hi;
12998
12999 /* Do this for all languages that support sound folding and for which a
13000 * .sug file has been loaded. */
13001 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
13002 {
13003 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13004 slang = lp->lp_slang;
13005 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13006 {
13007 /* Free the info about handled words. */
13008 todo = slang->sl_sounddone.ht_used;
13009 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
13010 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013011 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013012 vim_free(HI2SFT(hi));
13013 --todo;
13014 }
13015 hash_clear(&slang->sl_sounddone);
13016 }
13017 }
13018}
13019
13020/*
13021 * A match with a soundfolded word is found. Add the good word(s) that
13022 * produce this soundfolded word.
13023 */
13024 static void
13025add_sound_suggest(su, goodword, score, lp)
13026 suginfo_T *su;
13027 char_u *goodword;
13028 int score; /* soundfold score */
13029 langp_T *lp;
13030{
13031 slang_T *slang = lp->lp_slang; /* language for sound folding */
13032 int sfwordnr;
13033 char_u *nrline;
13034 int orgnr;
13035 char_u theword[MAXWLEN];
13036 int i;
13037 int wlen;
13038 char_u *byts;
13039 idx_T *idxs;
13040 int n;
13041 int wordcount;
13042 int wc;
13043 int goodscore;
13044 hash_T hash;
13045 hashitem_T *hi;
13046 sftword_T *sft;
13047 int bc, gc;
13048 int limit;
13049
13050 /*
13051 * It's very well possible that the same soundfold word is found several
13052 * times with different scores. Since the following is quite slow only do
13053 * the words that have a better score than before. Use a hashtable to
13054 * remember the words that have been done.
13055 */
13056 hash = hash_hash(goodword);
13057 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
13058 if (HASHITEM_EMPTY(hi))
13059 {
13060 sft = (sftword_T *)alloc(sizeof(sftword_T) + STRLEN(goodword));
13061 if (sft != NULL)
13062 {
13063 sft->sft_score = score;
13064 STRCPY(sft->sft_word, goodword);
13065 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
13066 }
13067 }
13068 else
13069 {
13070 sft = HI2SFT(hi);
13071 if (score >= sft->sft_score)
13072 return;
13073 sft->sft_score = score;
13074 }
13075
13076 /*
13077 * Find the word nr in the soundfold tree.
13078 */
13079 sfwordnr = soundfold_find(slang, goodword);
13080 if (sfwordnr < 0)
13081 {
13082 EMSG2(_(e_intern2), "add_sound_suggest()");
13083 return;
13084 }
13085
13086 /*
13087 * go over the list of good words that produce this soundfold word
13088 */
13089 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
13090 orgnr = 0;
13091 while (*nrline != NUL)
13092 {
13093 /* The wordnr was stored in a minimal nr of bytes as an offset to the
13094 * previous wordnr. */
13095 orgnr += bytes2offset(&nrline);
13096
13097 byts = slang->sl_fbyts;
13098 idxs = slang->sl_fidxs;
13099
13100 /* Lookup the word "orgnr" one of the two tries. */
13101 n = 0;
13102 wlen = 0;
13103 wordcount = 0;
13104 for (;;)
13105 {
13106 i = 1;
13107 if (wordcount == orgnr && byts[n + 1] == NUL)
13108 break; /* found end of word */
13109
13110 if (byts[n + 1] == NUL)
13111 ++wordcount;
13112
13113 /* skip over the NUL bytes */
13114 for ( ; byts[n + i] == NUL; ++i)
13115 if (i > byts[n]) /* safety check */
13116 {
13117 STRCPY(theword + wlen, "BAD");
13118 goto badword;
13119 }
13120
13121 /* One of the siblings must have the word. */
13122 for ( ; i < byts[n]; ++i)
13123 {
13124 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
13125 if (wordcount + wc > orgnr)
13126 break;
13127 wordcount += wc;
13128 }
13129
13130 theword[wlen++] = byts[n + i];
13131 n = idxs[n + i];
13132 }
13133badword:
13134 theword[wlen] = NUL;
13135
13136 /* Go over the possible flags and regions. */
13137 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
13138 {
13139 char_u cword[MAXWLEN];
13140 char_u *p;
13141 int flags = (int)idxs[n + i];
13142
Bram Moolenaare1438bb2006-03-01 22:01:55 +000013143 /* Skip words with the NOSUGGEST flag */
13144 if (flags & WF_NOSUGGEST)
13145 continue;
13146
Bram Moolenaar4770d092006-01-12 23:22:24 +000013147 if (flags & WF_KEEPCAP)
13148 {
13149 /* Must find the word in the keep-case tree. */
13150 find_keepcap_word(slang, theword, cword);
13151 p = cword;
13152 }
13153 else
13154 {
13155 flags |= su->su_badflags;
13156 if ((flags & WF_CAPMASK) != 0)
13157 {
13158 /* Need to fix case according to "flags". */
13159 make_case_word(theword, cword, flags);
13160 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013161 }
13162 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013163 p = theword;
13164 }
13165
13166 /* Add the suggestion. */
13167 if (sps_flags & SPS_DOUBLE)
13168 {
13169 /* Add the suggestion if the score isn't too bad. */
13170 if (score <= su->su_maxscore)
13171 add_suggestion(su, &su->su_sga, p, su->su_badlen,
13172 score, 0, FALSE, slang, FALSE);
13173 }
13174 else
13175 {
13176 /* Add a penalty for words in another region. */
13177 if ((flags & WF_REGION)
13178 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
13179 goodscore = SCORE_REGION;
13180 else
13181 goodscore = 0;
13182
13183 /* Add a small penalty for changing the first letter from
13184 * lower to upper case. Helps for "tath" -> "Kath", which is
13185 * less common thatn "tath" -> "path". Don't do it when the
13186 * letter is the same, that has already been counted. */
13187 gc = PTR2CHAR(p);
13188 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013189 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013190 bc = PTR2CHAR(su->su_badword);
13191 if (!SPELL_ISUPPER(bc)
13192 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
13193 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013194 }
13195
Bram Moolenaar4770d092006-01-12 23:22:24 +000013196 /* Compute the score for the good word. This only does letter
13197 * insert/delete/swap/replace. REP items are not considered,
13198 * which may make the score a bit higher.
13199 * Use a limit for the score to make it work faster. Use
13200 * MAXSCORE(), because RESCORE() will change the score.
13201 * If the limit is very high then the iterative method is
13202 * inefficient, using an array is quicker. */
13203 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
13204 if (limit > SCORE_LIMITMAX)
13205 goodscore += spell_edit_score(slang, su->su_badword, p);
13206 else
13207 goodscore += spell_edit_score_limit(slang, su->su_badword,
13208 p, limit);
13209
13210 /* When going over the limit don't bother to do the rest. */
13211 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013212 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013213 /* Give a bonus to words seen before. */
13214 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013215
Bram Moolenaar4770d092006-01-12 23:22:24 +000013216 /* Add the suggestion if the score isn't too bad. */
13217 goodscore = RESCORE(goodscore, score);
13218 if (goodscore <= su->su_sfmaxscore)
13219 add_suggestion(su, &su->su_ga, p, su->su_badlen,
13220 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013221 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013222 }
13223 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013224 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013225 }
13226}
13227
13228/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013229 * Find word "word" in fold-case tree for "slang" and return the word number.
13230 */
13231 static int
13232soundfold_find(slang, word)
13233 slang_T *slang;
13234 char_u *word;
13235{
13236 idx_T arridx = 0;
13237 int len;
13238 int wlen = 0;
13239 int c;
13240 char_u *ptr = word;
13241 char_u *byts;
13242 idx_T *idxs;
13243 int wordnr = 0;
13244
13245 byts = slang->sl_sbyts;
13246 idxs = slang->sl_sidxs;
13247
13248 for (;;)
13249 {
13250 /* First byte is the number of possible bytes. */
13251 len = byts[arridx++];
13252
13253 /* If the first possible byte is a zero the word could end here.
13254 * If the word ends we found the word. If not skip the NUL bytes. */
13255 c = ptr[wlen];
13256 if (byts[arridx] == NUL)
13257 {
13258 if (c == NUL)
13259 break;
13260
13261 /* Skip over the zeros, there can be several. */
13262 while (len > 0 && byts[arridx] == NUL)
13263 {
13264 ++arridx;
13265 --len;
13266 }
13267 if (len == 0)
13268 return -1; /* no children, word should have ended here */
13269 ++wordnr;
13270 }
13271
13272 /* If the word ends we didn't find it. */
13273 if (c == NUL)
13274 return -1;
13275
13276 /* Perform a binary search in the list of accepted bytes. */
13277 if (c == TAB) /* <Tab> is handled like <Space> */
13278 c = ' ';
13279 while (byts[arridx] < c)
13280 {
13281 /* The word count is in the first idxs[] entry of the child. */
13282 wordnr += idxs[idxs[arridx]];
13283 ++arridx;
13284 if (--len == 0) /* end of the bytes, didn't find it */
13285 return -1;
13286 }
13287 if (byts[arridx] != c) /* didn't find the byte */
13288 return -1;
13289
13290 /* Continue at the child (if there is one). */
13291 arridx = idxs[arridx];
13292 ++wlen;
13293
13294 /* One space in the good word may stand for several spaces in the
13295 * checked word. */
13296 if (c == ' ')
13297 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
13298 ++wlen;
13299 }
13300
13301 return wordnr;
13302}
13303
13304/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013305 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013306 */
13307 static void
13308make_case_word(fword, cword, flags)
13309 char_u *fword;
13310 char_u *cword;
13311 int flags;
13312{
13313 if (flags & WF_ALLCAP)
13314 /* Make it all upper-case */
13315 allcap_copy(fword, cword);
13316 else if (flags & WF_ONECAP)
13317 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013318 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013319 else
13320 /* Use goodword as-is. */
13321 STRCPY(cword, fword);
13322}
13323
Bram Moolenaarea424162005-06-16 21:51:00 +000013324/*
13325 * Use map string "map" for languages "lp".
13326 */
13327 static void
13328set_map_str(lp, map)
13329 slang_T *lp;
13330 char_u *map;
13331{
13332 char_u *p;
13333 int headc = 0;
13334 int c;
13335 int i;
13336
13337 if (*map == NUL)
13338 {
13339 lp->sl_has_map = FALSE;
13340 return;
13341 }
13342 lp->sl_has_map = TRUE;
13343
Bram Moolenaar4770d092006-01-12 23:22:24 +000013344 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000013345 for (i = 0; i < 256; ++i)
13346 lp->sl_map_array[i] = 0;
13347#ifdef FEAT_MBYTE
13348 hash_init(&lp->sl_map_hash);
13349#endif
13350
13351 /*
13352 * The similar characters are stored separated with slashes:
13353 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13354 * before the same slash. For characters above 255 sl_map_hash is used.
13355 */
13356 for (p = map; *p != NUL; )
13357 {
13358#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013359 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000013360#else
13361 c = *p++;
13362#endif
13363 if (c == '/')
13364 headc = 0;
13365 else
13366 {
13367 if (headc == 0)
13368 headc = c;
13369
13370#ifdef FEAT_MBYTE
13371 /* Characters above 255 don't fit in sl_map_array[], put them in
13372 * the hash table. Each entry is the char, a NUL the headchar and
13373 * a NUL. */
13374 if (c >= 256)
13375 {
13376 int cl = mb_char2len(c);
13377 int headcl = mb_char2len(headc);
13378 char_u *b;
13379 hash_T hash;
13380 hashitem_T *hi;
13381
13382 b = alloc((unsigned)(cl + headcl + 2));
13383 if (b == NULL)
13384 return;
13385 mb_char2bytes(c, b);
13386 b[cl] = NUL;
13387 mb_char2bytes(headc, b + cl + 1);
13388 b[cl + 1 + headcl] = NUL;
13389 hash = hash_hash(b);
13390 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13391 if (HASHITEM_EMPTY(hi))
13392 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13393 else
13394 {
13395 /* This should have been checked when generating the .spl
13396 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013397 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000013398 vim_free(b);
13399 }
13400 }
13401 else
13402#endif
13403 lp->sl_map_array[c] = headc;
13404 }
13405 }
13406}
13407
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013408/*
13409 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13410 * lines in the .aff file.
13411 */
13412 static int
13413similar_chars(slang, c1, c2)
13414 slang_T *slang;
13415 int c1;
13416 int c2;
13417{
Bram Moolenaarea424162005-06-16 21:51:00 +000013418 int m1, m2;
13419#ifdef FEAT_MBYTE
13420 char_u buf[MB_MAXBYTES];
13421 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013422
Bram Moolenaarea424162005-06-16 21:51:00 +000013423 if (c1 >= 256)
13424 {
13425 buf[mb_char2bytes(c1, buf)] = 0;
13426 hi = hash_find(&slang->sl_map_hash, buf);
13427 if (HASHITEM_EMPTY(hi))
13428 m1 = 0;
13429 else
13430 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13431 }
13432 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013433#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013434 m1 = slang->sl_map_array[c1];
13435 if (m1 == 0)
13436 return FALSE;
13437
13438
13439#ifdef FEAT_MBYTE
13440 if (c2 >= 256)
13441 {
13442 buf[mb_char2bytes(c2, buf)] = 0;
13443 hi = hash_find(&slang->sl_map_hash, buf);
13444 if (HASHITEM_EMPTY(hi))
13445 m2 = 0;
13446 else
13447 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13448 }
13449 else
13450#endif
13451 m2 = slang->sl_map_array[c2];
13452
13453 return m1 == m2;
13454}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013455
13456/*
13457 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013458 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013459 */
13460 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000013461add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
13462 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013463 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013464 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013465 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013466 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013467 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013468 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013469 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013470 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013471 int maxsf; /* su_maxscore applies to soundfold score,
13472 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013473{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013474 int goodlen; /* len of goodword changed */
13475 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013476 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013477 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013478 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013479 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013480
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013481 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13482 * "thee the" is added next to changing the first "the" the "thee". */
13483 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013484 pbad = su->su_badptr + badlenarg;
13485 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013486 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013487 goodlen = pgood - goodword;
13488 badlen = pbad - su->su_badptr;
13489 if (goodlen <= 0 || badlen <= 0)
13490 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013491 mb_ptr_back(goodword, pgood);
13492 mb_ptr_back(su->su_badptr, pbad);
13493#ifdef FEAT_MBYTE
13494 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013495 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013496 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13497 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013498 }
13499 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013500#endif
13501 if (*pgood != *pbad)
13502 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013503 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013504
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013505 if (badlen == 0 && goodlen == 0)
13506 /* goodword doesn't change anything; may happen for "the the" changing
13507 * the first "the" to itself. */
13508 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013509
Bram Moolenaar4770d092006-01-12 23:22:24 +000013510 /* Check if the word is already there. Also check the length that is
13511 * being replaced "thes," -> "these" is a different suggestion from
13512 * "thes" -> "these". */
13513 stp = &SUG(*gap, 0);
13514 for (i = gap->ga_len; --i >= 0; ++stp)
13515 if (stp->st_wordlen == goodlen
13516 && stp->st_orglen == badlen
13517 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
13518 {
13519 /*
13520 * Found it. Remember the word with the lowest score.
13521 */
13522 if (stp->st_slang == NULL)
13523 stp->st_slang = slang;
13524
13525 new_sug.st_score = score;
13526 new_sug.st_altscore = altscore;
13527 new_sug.st_had_bonus = had_bonus;
13528
13529 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013530 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013531 /* Only one of the two had the soundalike score computed.
13532 * Need to do that for the other one now, otherwise the
13533 * scores can't be compared. This happens because
13534 * suggest_try_change() doesn't compute the soundalike
13535 * word to keep it fast, while some special methods set
13536 * the soundalike score to zero. */
13537 if (had_bonus)
13538 rescore_one(su, stp);
13539 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013540 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013541 new_sug.st_word = stp->st_word;
13542 new_sug.st_wordlen = stp->st_wordlen;
13543 new_sug.st_slang = stp->st_slang;
13544 new_sug.st_orglen = badlen;
13545 rescore_one(su, &new_sug);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013546 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013547 }
13548
Bram Moolenaar4770d092006-01-12 23:22:24 +000013549 if (stp->st_score > new_sug.st_score)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013550 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013551 stp->st_score = new_sug.st_score;
13552 stp->st_altscore = new_sug.st_altscore;
13553 stp->st_had_bonus = new_sug.st_had_bonus;
13554 }
13555 break;
13556 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013557
Bram Moolenaar4770d092006-01-12 23:22:24 +000013558 if (i < 0 && ga_grow(gap, 1) == OK)
13559 {
13560 /* Add a suggestion. */
13561 stp = &SUG(*gap, gap->ga_len);
13562 stp->st_word = vim_strnsave(goodword, goodlen);
13563 if (stp->st_word != NULL)
13564 {
13565 stp->st_wordlen = goodlen;
13566 stp->st_score = score;
13567 stp->st_altscore = altscore;
13568 stp->st_had_bonus = had_bonus;
13569 stp->st_orglen = badlen;
13570 stp->st_slang = slang;
13571 ++gap->ga_len;
13572
13573 /* If we have too many suggestions now, sort the list and keep
13574 * the best suggestions. */
13575 if (gap->ga_len > SUG_MAX_COUNT(su))
13576 {
13577 if (maxsf)
13578 su->su_sfmaxscore = cleanup_suggestions(gap,
13579 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13580 else
13581 {
13582 i = su->su_maxscore;
13583 su->su_maxscore = cleanup_suggestions(gap,
13584 su->su_maxscore, SUG_CLEAN_COUNT(su));
13585 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013586 }
13587 }
13588 }
13589}
13590
13591/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013592 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13593 * for split words, such as "the the". Remove these from the list here.
13594 */
13595 static void
13596check_suggestions(su, gap)
13597 suginfo_T *su;
13598 garray_T *gap; /* either su_ga or su_sga */
13599{
13600 suggest_T *stp;
13601 int i;
13602 char_u longword[MAXWLEN + 1];
13603 int len;
13604 hlf_T attr;
13605
13606 stp = &SUG(*gap, 0);
13607 for (i = gap->ga_len - 1; i >= 0; --i)
13608 {
13609 /* Need to append what follows to check for "the the". */
13610 STRCPY(longword, stp[i].st_word);
13611 len = stp[i].st_wordlen;
13612 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13613 MAXWLEN - len);
13614 attr = HLF_COUNT;
13615 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13616 if (attr != HLF_COUNT)
13617 {
13618 /* Remove this entry. */
13619 vim_free(stp[i].st_word);
13620 --gap->ga_len;
13621 if (i < gap->ga_len)
13622 mch_memmove(stp + i, stp + i + 1,
13623 sizeof(suggest_T) * (gap->ga_len - i));
13624 }
13625 }
13626}
13627
13628
13629/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013630 * Add a word to be banned.
13631 */
13632 static void
13633add_banned(su, word)
13634 suginfo_T *su;
13635 char_u *word;
13636{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013637 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013638 hash_T hash;
13639 hashitem_T *hi;
13640
Bram Moolenaar4770d092006-01-12 23:22:24 +000013641 hash = hash_hash(word);
13642 hi = hash_lookup(&su->su_banned, word, hash);
13643 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013644 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013645 s = vim_strsave(word);
13646 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013647 hash_add_item(&su->su_banned, hi, s, hash);
13648 }
13649}
13650
13651/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013652 * Recompute the score for all suggestions if sound-folding is possible. This
13653 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013654 */
13655 static void
13656rescore_suggestions(su)
13657 suginfo_T *su;
13658{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013659 int i;
13660
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013661 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013662 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013663 rescore_one(su, &SUG(su->su_ga, i));
13664}
13665
13666/*
13667 * Recompute the score for one suggestion if sound-folding is possible.
13668 */
13669 static void
13670rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013671 suginfo_T *su;
13672 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013673{
13674 slang_T *slang = stp->st_slang;
13675 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013676 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013677
13678 /* Only rescore suggestions that have no sal score yet and do have a
13679 * language. */
13680 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13681 {
13682 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013683 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013684 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013685 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013686 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013687 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013688 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013689
13690 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013691 if (stp->st_altscore == SCORE_MAXMAX)
13692 stp->st_altscore = SCORE_BIG;
13693 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13694 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013695 }
13696}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013697
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013698static int
13699#ifdef __BORLANDC__
13700_RTLENTRYF
13701#endif
13702sug_compare __ARGS((const void *s1, const void *s2));
13703
13704/*
13705 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013706 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013707 */
13708 static int
13709#ifdef __BORLANDC__
13710_RTLENTRYF
13711#endif
13712sug_compare(s1, s2)
13713 const void *s1;
13714 const void *s2;
13715{
13716 suggest_T *p1 = (suggest_T *)s1;
13717 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013718 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013719
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013720 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013721 {
13722 n = p1->st_altscore - p2->st_altscore;
13723 if (n == 0)
13724 n = STRICMP(p1->st_word, p2->st_word);
13725 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013726 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013727}
13728
13729/*
13730 * Cleanup the suggestions:
13731 * - Sort on score.
13732 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013733 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013734 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013735 static int
13736cleanup_suggestions(gap, maxscore, keep)
13737 garray_T *gap;
13738 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013739 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013740{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013741 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013742 int i;
13743
13744 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013745 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013746
13747 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013748 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013749 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013750 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013751 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013752 gap->ga_len = keep;
13753 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013754 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013755 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013756}
13757
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013758#if defined(FEAT_EVAL) || defined(PROTO)
13759/*
13760 * Soundfold a string, for soundfold().
13761 * Result is in allocated memory, NULL for an error.
13762 */
13763 char_u *
13764eval_soundfold(word)
13765 char_u *word;
13766{
13767 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013768 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013769 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013770
13771 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13772 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013773 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013774 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013775 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013776 if (lp->lp_slang->sl_sal.ga_len > 0)
13777 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013778 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013779 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013780 return vim_strsave(sound);
13781 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013782 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013783
13784 /* No language with sound folding, return word as-is. */
13785 return vim_strsave(word);
13786}
13787#endif
13788
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013789/*
13790 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000013791 *
13792 * There are many ways to turn a word into a sound-a-like representation. The
13793 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
13794 * swedish name matching - survey and test of different algorithms" by Klas
13795 * Erikson.
13796 *
13797 * We support two methods:
13798 * 1. SOFOFROM/SOFOTO do a simple character mapping.
13799 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013800 */
13801 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013802spell_soundfold(slang, inword, folded, res)
13803 slang_T *slang;
13804 char_u *inword;
13805 int folded; /* "inword" is already case-folded */
13806 char_u *res;
13807{
13808 char_u fword[MAXWLEN];
13809 char_u *word;
13810
13811 if (slang->sl_sofo)
13812 /* SOFOFROM and SOFOTO used */
13813 spell_soundfold_sofo(slang, inword, res);
13814 else
13815 {
13816 /* SAL items used. Requires the word to be case-folded. */
13817 if (folded)
13818 word = inword;
13819 else
13820 {
13821 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
13822 word = fword;
13823 }
13824
13825#ifdef FEAT_MBYTE
13826 if (has_mbyte)
13827 spell_soundfold_wsal(slang, word, res);
13828 else
13829#endif
13830 spell_soundfold_sal(slang, word, res);
13831 }
13832}
13833
13834/*
13835 * Perform sound folding of "inword" into "res" according to SOFOFROM and
13836 * SOFOTO lines.
13837 */
13838 static void
13839spell_soundfold_sofo(slang, inword, res)
13840 slang_T *slang;
13841 char_u *inword;
13842 char_u *res;
13843{
13844 char_u *s;
13845 int ri = 0;
13846 int c;
13847
13848#ifdef FEAT_MBYTE
13849 if (has_mbyte)
13850 {
13851 int prevc = 0;
13852 int *ip;
13853
13854 /* The sl_sal_first[] table contains the translation for chars up to
13855 * 255, sl_sal the rest. */
13856 for (s = inword; *s != NUL; )
13857 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013858 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013859 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
13860 c = ' ';
13861 else if (c < 256)
13862 c = slang->sl_sal_first[c];
13863 else
13864 {
13865 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
13866 if (ip == NULL) /* empty list, can't match */
13867 c = NUL;
13868 else
13869 for (;;) /* find "c" in the list */
13870 {
13871 if (*ip == 0) /* not found */
13872 {
13873 c = NUL;
13874 break;
13875 }
13876 if (*ip == c) /* match! */
13877 {
13878 c = ip[1];
13879 break;
13880 }
13881 ip += 2;
13882 }
13883 }
13884
13885 if (c != NUL && c != prevc)
13886 {
13887 ri += mb_char2bytes(c, res + ri);
13888 if (ri + MB_MAXBYTES > MAXWLEN)
13889 break;
13890 prevc = c;
13891 }
13892 }
13893 }
13894 else
13895#endif
13896 {
13897 /* The sl_sal_first[] table contains the translation. */
13898 for (s = inword; (c = *s) != NUL; ++s)
13899 {
13900 if (vim_iswhite(c))
13901 c = ' ';
13902 else
13903 c = slang->sl_sal_first[c];
13904 if (c != NUL && (ri == 0 || res[ri - 1] != c))
13905 res[ri++] = c;
13906 }
13907 }
13908
13909 res[ri] = NUL;
13910}
13911
13912 static void
13913spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013914 slang_T *slang;
13915 char_u *inword;
13916 char_u *res;
13917{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013918 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013919 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013920 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013921 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013922 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013923 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013924 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013925 int n, k = 0;
13926 int z0;
13927 int k0;
13928 int n0;
13929 int c;
13930 int pri;
13931 int p0 = -333;
13932 int c0;
13933
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013934 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013935 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013936 if (slang->sl_rem_accents)
13937 {
13938 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013939 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013940 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013941 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013942 {
13943 *t++ = ' ';
13944 s = skipwhite(s);
13945 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013946 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013947 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013948 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013949 *t++ = *s;
13950 ++s;
13951 }
13952 }
13953 *t = NUL;
13954 }
13955 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013956 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013957
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013958 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013959
13960 /*
13961 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013962 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013963 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013964 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013965 while ((c = word[i]) != NUL)
13966 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013967 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013968 n = slang->sl_sal_first[c];
13969 z0 = 0;
13970
13971 if (n >= 0)
13972 {
13973 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013974 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013975 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013976 /* Quickly skip entries that don't match the word. Most
13977 * entries are less then three chars, optimize for that. */
13978 k = smp[n].sm_leadlen;
13979 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013980 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013981 if (word[i + 1] != s[1])
13982 continue;
13983 if (k > 2)
13984 {
13985 for (j = 2; j < k; ++j)
13986 if (word[i + j] != s[j])
13987 break;
13988 if (j < k)
13989 continue;
13990 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013991 }
13992
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013993 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013994 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013995 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013996 while (*pf != NUL && *pf != word[i + k])
13997 ++pf;
13998 if (*pf == NUL)
13999 continue;
14000 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014001 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014002 s = smp[n].sm_rules;
14003 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014004
14005 p0 = *s;
14006 k0 = k;
14007 while (*s == '-' && k > 1)
14008 {
14009 k--;
14010 s++;
14011 }
14012 if (*s == '<')
14013 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014014 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014015 {
14016 /* determine priority */
14017 pri = *s - '0';
14018 s++;
14019 }
14020 if (*s == '^' && *(s + 1) == '^')
14021 s++;
14022
14023 if (*s == NUL
14024 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014025 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014026 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014027 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014028 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014029 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014030 && spell_iswordp(word + i - 1, curbuf)
14031 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014032 {
14033 /* search for followup rules, if: */
14034 /* followup and k > 1 and NO '-' in searchstring */
14035 c0 = word[i + k - 1];
14036 n0 = slang->sl_sal_first[c0];
14037
14038 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014039 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014040 {
14041 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014042 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014043 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014044 /* Quickly skip entries that don't match the word.
14045 * */
14046 k0 = smp[n0].sm_leadlen;
14047 if (k0 > 1)
14048 {
14049 if (word[i + k] != s[1])
14050 continue;
14051 if (k0 > 2)
14052 {
14053 pf = word + i + k + 1;
14054 for (j = 2; j < k0; ++j)
14055 if (*pf++ != s[j])
14056 break;
14057 if (j < k0)
14058 continue;
14059 }
14060 }
14061 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014062
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014063 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014064 {
14065 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014066 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014067 while (*pf != NUL && *pf != word[i + k0])
14068 ++pf;
14069 if (*pf == NUL)
14070 continue;
14071 ++k0;
14072 }
14073
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014074 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014075 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014076 while (*s == '-')
14077 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014078 /* "k0" gets NOT reduced because
14079 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014080 s++;
14081 }
14082 if (*s == '<')
14083 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014084 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014085 {
14086 p0 = *s - '0';
14087 s++;
14088 }
14089
14090 if (*s == NUL
14091 /* *s == '^' cuts */
14092 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014093 && !spell_iswordp(word + i + k0,
14094 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014095 {
14096 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014097 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014098 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014099
14100 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014101 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014102 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014103 /* rule fits; stop search */
14104 break;
14105 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014106 }
14107
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014108 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014109 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014110 }
14111
14112 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014113 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014114 if (s == NULL)
14115 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014116 pf = smp[n].sm_rules;
14117 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014118 if (p0 == 1 && z == 0)
14119 {
14120 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014121 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
14122 || res[reslen - 1] == *s))
14123 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014124 z0 = 1;
14125 z = 1;
14126 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014127 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014128 {
14129 word[i + k0] = *s;
14130 k0++;
14131 s++;
14132 }
14133 if (k > k0)
14134 mch_memmove(word + i + k0, word + i + k,
14135 STRLEN(word + i + k) + 1);
14136
14137 /* new "actual letter" */
14138 c = word[i];
14139 }
14140 else
14141 {
14142 /* no '<' rule used */
14143 i += k - 1;
14144 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014145 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014146 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014147 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014148 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014149 s++;
14150 }
14151 /* new "actual letter" */
14152 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014153 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014154 {
14155 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014156 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014157 mch_memmove(word, word + i + 1,
14158 STRLEN(word + i + 1) + 1);
14159 i = 0;
14160 z0 = 1;
14161 }
14162 }
14163 break;
14164 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014165 }
14166 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014167 else if (vim_iswhite(c))
14168 {
14169 c = ' ';
14170 k = 1;
14171 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014172
14173 if (z0 == 0)
14174 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014175 if (k && !p0 && reslen < MAXWLEN && c != NUL
14176 && (!slang->sl_collapse || reslen == 0
14177 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014178 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014179 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014180
14181 i++;
14182 z = 0;
14183 k = 0;
14184 }
14185 }
14186
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014187 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014188}
14189
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014190#ifdef FEAT_MBYTE
14191/*
14192 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14193 * Multi-byte version of spell_soundfold().
14194 */
14195 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014196spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014197 slang_T *slang;
14198 char_u *inword;
14199 char_u *res;
14200{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014201 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014202 int word[MAXWLEN];
14203 int wres[MAXWLEN];
14204 int l;
14205 char_u *s;
14206 int *ws;
14207 char_u *t;
14208 int *pf;
14209 int i, j, z;
14210 int reslen;
14211 int n, k = 0;
14212 int z0;
14213 int k0;
14214 int n0;
14215 int c;
14216 int pri;
14217 int p0 = -333;
14218 int c0;
14219 int did_white = FALSE;
14220
14221 /*
14222 * Convert the multi-byte string to a wide-character string.
14223 * Remove accents, if wanted. We actually remove all non-word characters.
14224 * But keep white space.
14225 */
14226 n = 0;
14227 for (s = inword; *s != NUL; )
14228 {
14229 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014230 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014231 if (slang->sl_rem_accents)
14232 {
14233 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14234 {
14235 if (did_white)
14236 continue;
14237 c = ' ';
14238 did_white = TRUE;
14239 }
14240 else
14241 {
14242 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014243 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014244 continue;
14245 }
14246 }
14247 word[n++] = c;
14248 }
14249 word[n] = NUL;
14250
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014251 /*
14252 * This comes from Aspell phonet.cpp.
14253 * Converted from C++ to C. Added support for multi-byte chars.
14254 * Changed to keep spaces.
14255 */
14256 i = reslen = z = 0;
14257 while ((c = word[i]) != NUL)
14258 {
14259 /* Start with the first rule that has the character in the word. */
14260 n = slang->sl_sal_first[c & 0xff];
14261 z0 = 0;
14262
14263 if (n >= 0)
14264 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014265 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014266 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
14267 {
14268 /* Quickly skip entries that don't match the word. Most
14269 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014270 if (c != ws[0])
14271 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014272 k = smp[n].sm_leadlen;
14273 if (k > 1)
14274 {
14275 if (word[i + 1] != ws[1])
14276 continue;
14277 if (k > 2)
14278 {
14279 for (j = 2; j < k; ++j)
14280 if (word[i + j] != ws[j])
14281 break;
14282 if (j < k)
14283 continue;
14284 }
14285 }
14286
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014287 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014288 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014289 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014290 while (*pf != NUL && *pf != word[i + k])
14291 ++pf;
14292 if (*pf == NUL)
14293 continue;
14294 ++k;
14295 }
14296 s = smp[n].sm_rules;
14297 pri = 5; /* default priority */
14298
14299 p0 = *s;
14300 k0 = k;
14301 while (*s == '-' && k > 1)
14302 {
14303 k--;
14304 s++;
14305 }
14306 if (*s == '<')
14307 s++;
14308 if (VIM_ISDIGIT(*s))
14309 {
14310 /* determine priority */
14311 pri = *s - '0';
14312 s++;
14313 }
14314 if (*s == '^' && *(s + 1) == '^')
14315 s++;
14316
14317 if (*s == NUL
14318 || (*s == '^'
14319 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014320 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014321 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014322 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014323 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014324 && spell_iswordp_w(word + i - 1, curbuf)
14325 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014326 {
14327 /* search for followup rules, if: */
14328 /* followup and k > 1 and NO '-' in searchstring */
14329 c0 = word[i + k - 1];
14330 n0 = slang->sl_sal_first[c0 & 0xff];
14331
14332 if (slang->sl_followup && k > 1 && n0 >= 0
14333 && p0 != '-' && word[i + k] != NUL)
14334 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014335 /* Test follow-up rule for "word[i + k]"; loop over
14336 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014337 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14338 == (c0 & 0xff); ++n0)
14339 {
14340 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014341 */
14342 if (c0 != ws[0])
14343 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014344 k0 = smp[n0].sm_leadlen;
14345 if (k0 > 1)
14346 {
14347 if (word[i + k] != ws[1])
14348 continue;
14349 if (k0 > 2)
14350 {
14351 pf = word + i + k + 1;
14352 for (j = 2; j < k0; ++j)
14353 if (*pf++ != ws[j])
14354 break;
14355 if (j < k0)
14356 continue;
14357 }
14358 }
14359 k0 += k - 1;
14360
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014361 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014362 {
14363 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014364 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014365 while (*pf != NUL && *pf != word[i + k0])
14366 ++pf;
14367 if (*pf == NUL)
14368 continue;
14369 ++k0;
14370 }
14371
14372 p0 = 5;
14373 s = smp[n0].sm_rules;
14374 while (*s == '-')
14375 {
14376 /* "k0" gets NOT reduced because
14377 * "if (k0 == k)" */
14378 s++;
14379 }
14380 if (*s == '<')
14381 s++;
14382 if (VIM_ISDIGIT(*s))
14383 {
14384 p0 = *s - '0';
14385 s++;
14386 }
14387
14388 if (*s == NUL
14389 /* *s == '^' cuts */
14390 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014391 && !spell_iswordp_w(word + i + k0,
14392 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014393 {
14394 if (k0 == k)
14395 /* this is just a piece of the string */
14396 continue;
14397
14398 if (p0 < pri)
14399 /* priority too low */
14400 continue;
14401 /* rule fits; stop search */
14402 break;
14403 }
14404 }
14405
14406 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14407 == (c0 & 0xff))
14408 continue;
14409 }
14410
14411 /* replace string */
14412 ws = smp[n].sm_to_w;
14413 s = smp[n].sm_rules;
14414 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14415 if (p0 == 1 && z == 0)
14416 {
14417 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014418 if (reslen > 0 && ws != NULL && *ws != NUL
14419 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014420 || wres[reslen - 1] == *ws))
14421 reslen--;
14422 z0 = 1;
14423 z = 1;
14424 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014425 if (ws != NULL)
14426 while (*ws != NUL && word[i + k0] != NUL)
14427 {
14428 word[i + k0] = *ws;
14429 k0++;
14430 ws++;
14431 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014432 if (k > k0)
14433 mch_memmove(word + i + k0, word + i + k,
14434 sizeof(int) * (STRLEN(word + i + k) + 1));
14435
14436 /* new "actual letter" */
14437 c = word[i];
14438 }
14439 else
14440 {
14441 /* no '<' rule used */
14442 i += k - 1;
14443 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014444 if (ws != NULL)
14445 while (*ws != NUL && ws[1] != NUL
14446 && reslen < MAXWLEN)
14447 {
14448 if (reslen == 0 || wres[reslen - 1] != *ws)
14449 wres[reslen++] = *ws;
14450 ws++;
14451 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014452 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014453 if (ws == NULL)
14454 c = NUL;
14455 else
14456 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014457 if (strstr((char *)s, "^^") != NULL)
14458 {
14459 if (c != NUL)
14460 wres[reslen++] = c;
14461 mch_memmove(word, word + i + 1,
14462 sizeof(int) * (STRLEN(word + i + 1) + 1));
14463 i = 0;
14464 z0 = 1;
14465 }
14466 }
14467 break;
14468 }
14469 }
14470 }
14471 else if (vim_iswhite(c))
14472 {
14473 c = ' ';
14474 k = 1;
14475 }
14476
14477 if (z0 == 0)
14478 {
14479 if (k && !p0 && reslen < MAXWLEN && c != NUL
14480 && (!slang->sl_collapse || reslen == 0
14481 || wres[reslen - 1] != c))
14482 /* condense only double letters */
14483 wres[reslen++] = c;
14484
14485 i++;
14486 z = 0;
14487 k = 0;
14488 }
14489 }
14490
14491 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14492 l = 0;
14493 for (n = 0; n < reslen; ++n)
14494 {
14495 l += mb_char2bytes(wres[n], res + l);
14496 if (l + MB_MAXBYTES > MAXWLEN)
14497 break;
14498 }
14499 res[l] = NUL;
14500}
14501#endif
14502
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014503/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014504 * Compute a score for two sound-a-like words.
14505 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14506 * Instead of a generic loop we write out the code. That keeps it fast by
14507 * avoiding checks that will not be possible.
14508 */
14509 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014510soundalike_score(goodstart, badstart)
14511 char_u *goodstart; /* sound-folded good word */
14512 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014513{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014514 char_u *goodsound = goodstart;
14515 char_u *badsound = badstart;
14516 int goodlen;
14517 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014518 int n;
14519 char_u *pl, *ps;
14520 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014521 int score = 0;
14522
14523 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
14524 * counted so much, vowels halfway the word aren't counted at all. */
14525 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14526 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014527 if (badsound[1] == goodsound[1]
14528 || (badsound[1] != NUL
14529 && goodsound[1] != NUL
14530 && badsound[2] == goodsound[2]))
14531 {
14532 /* handle like a substitute */
14533 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014534 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014535 {
14536 score = 2 * SCORE_DEL / 3;
14537 if (*badsound == '*')
14538 ++badsound;
14539 else
14540 ++goodsound;
14541 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014542 }
14543
14544 goodlen = STRLEN(goodsound);
14545 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014546
14547 /* Return quickly if the lenghts are too different to be fixed by two
14548 * changes. */
14549 n = goodlen - badlen;
14550 if (n < -2 || n > 2)
14551 return SCORE_MAXMAX;
14552
14553 if (n > 0)
14554 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014555 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014556 ps = badsound;
14557 }
14558 else
14559 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014560 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014561 ps = goodsound;
14562 }
14563
14564 /* Skip over the identical part. */
14565 while (*pl == *ps && *pl != NUL)
14566 {
14567 ++pl;
14568 ++ps;
14569 }
14570
14571 switch (n)
14572 {
14573 case -2:
14574 case 2:
14575 /*
14576 * Must delete two characters from "pl".
14577 */
14578 ++pl; /* first delete */
14579 while (*pl == *ps)
14580 {
14581 ++pl;
14582 ++ps;
14583 }
14584 /* strings must be equal after second delete */
14585 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014586 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014587
14588 /* Failed to compare. */
14589 break;
14590
14591 case -1:
14592 case 1:
14593 /*
14594 * Minimal one delete from "pl" required.
14595 */
14596
14597 /* 1: delete */
14598 pl2 = pl + 1;
14599 ps2 = ps;
14600 while (*pl2 == *ps2)
14601 {
14602 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014603 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014604 ++pl2;
14605 ++ps2;
14606 }
14607
14608 /* 2: delete then swap, then rest must be equal */
14609 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14610 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014611 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014612
14613 /* 3: delete then substitute, then the rest must be equal */
14614 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014615 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014616
14617 /* 4: first swap then delete */
14618 if (pl[0] == ps[1] && pl[1] == ps[0])
14619 {
14620 pl2 = pl + 2; /* swap, skip two chars */
14621 ps2 = ps + 2;
14622 while (*pl2 == *ps2)
14623 {
14624 ++pl2;
14625 ++ps2;
14626 }
14627 /* delete a char and then strings must be equal */
14628 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014629 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014630 }
14631
14632 /* 5: first substitute then delete */
14633 pl2 = pl + 1; /* substitute, skip one char */
14634 ps2 = ps + 1;
14635 while (*pl2 == *ps2)
14636 {
14637 ++pl2;
14638 ++ps2;
14639 }
14640 /* delete a char and then strings must be equal */
14641 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014642 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014643
14644 /* Failed to compare. */
14645 break;
14646
14647 case 0:
14648 /*
14649 * Lenghts are equal, thus changes must result in same length: An
14650 * insert is only possible in combination with a delete.
14651 * 1: check if for identical strings
14652 */
14653 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014654 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014655
14656 /* 2: swap */
14657 if (pl[0] == ps[1] && pl[1] == ps[0])
14658 {
14659 pl2 = pl + 2; /* swap, skip two chars */
14660 ps2 = ps + 2;
14661 while (*pl2 == *ps2)
14662 {
14663 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014664 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014665 ++pl2;
14666 ++ps2;
14667 }
14668 /* 3: swap and swap again */
14669 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14670 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014671 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014672
14673 /* 4: swap and substitute */
14674 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014675 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014676 }
14677
14678 /* 5: substitute */
14679 pl2 = pl + 1;
14680 ps2 = ps + 1;
14681 while (*pl2 == *ps2)
14682 {
14683 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014684 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014685 ++pl2;
14686 ++ps2;
14687 }
14688
14689 /* 6: substitute and swap */
14690 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14691 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014692 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014693
14694 /* 7: substitute and substitute */
14695 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014696 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014697
14698 /* 8: insert then delete */
14699 pl2 = pl;
14700 ps2 = ps + 1;
14701 while (*pl2 == *ps2)
14702 {
14703 ++pl2;
14704 ++ps2;
14705 }
14706 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014707 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014708
14709 /* 9: delete then insert */
14710 pl2 = pl + 1;
14711 ps2 = ps;
14712 while (*pl2 == *ps2)
14713 {
14714 ++pl2;
14715 ++ps2;
14716 }
14717 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014718 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014719
14720 /* Failed to compare. */
14721 break;
14722 }
14723
14724 return SCORE_MAXMAX;
14725}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014726
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014727/*
14728 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014729 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014730 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000014731 * The algorithm is described by Du and Chang, 1992.
14732 * The implementation of the algorithm comes from Aspell editdist.cpp,
14733 * edit_distance(). It has been converted from C++ to C and modified to
14734 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014735 */
14736 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000014737spell_edit_score(slang, badword, goodword)
14738 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014739 char_u *badword;
14740 char_u *goodword;
14741{
14742 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014743 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014744 int j, i;
14745 int t;
14746 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014747 int pbc, pgc;
14748#ifdef FEAT_MBYTE
14749 char_u *p;
14750 int wbadword[MAXWLEN];
14751 int wgoodword[MAXWLEN];
14752
14753 if (has_mbyte)
14754 {
14755 /* Get the characters from the multi-byte strings and put them in an
14756 * int array for easy access. */
14757 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014758 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014759 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014760 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014761 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014762 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014763 }
14764 else
14765#endif
14766 {
14767 badlen = STRLEN(badword) + 1;
14768 goodlen = STRLEN(goodword) + 1;
14769 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014770
14771 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
14772#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014773 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
14774 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014775 if (cnt == NULL)
14776 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014777
14778 CNT(0, 0) = 0;
14779 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000014780 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014781
14782 for (i = 1; i <= badlen; ++i)
14783 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014784 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014785 for (j = 1; j <= goodlen; ++j)
14786 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014787#ifdef FEAT_MBYTE
14788 if (has_mbyte)
14789 {
14790 bc = wbadword[i - 1];
14791 gc = wgoodword[j - 1];
14792 }
14793 else
14794#endif
14795 {
14796 bc = badword[i - 1];
14797 gc = goodword[j - 1];
14798 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014799 if (bc == gc)
14800 CNT(i, j) = CNT(i - 1, j - 1);
14801 else
14802 {
14803 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014804 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014805 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
14806 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014807 {
14808 /* For a similar character use SCORE_SIMILAR. */
14809 if (slang != NULL
14810 && slang->sl_has_map
14811 && similar_chars(slang, gc, bc))
14812 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
14813 else
14814 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
14815 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014816
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014817 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014818 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014819#ifdef FEAT_MBYTE
14820 if (has_mbyte)
14821 {
14822 pbc = wbadword[i - 2];
14823 pgc = wgoodword[j - 2];
14824 }
14825 else
14826#endif
14827 {
14828 pbc = badword[i - 2];
14829 pgc = goodword[j - 2];
14830 }
14831 if (bc == pgc && pbc == gc)
14832 {
14833 t = SCORE_SWAP + CNT(i - 2, j - 2);
14834 if (t < CNT(i, j))
14835 CNT(i, j) = t;
14836 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014837 }
14838 t = SCORE_DEL + CNT(i - 1, j);
14839 if (t < CNT(i, j))
14840 CNT(i, j) = t;
14841 t = SCORE_INS + CNT(i, j - 1);
14842 if (t < CNT(i, j))
14843 CNT(i, j) = t;
14844 }
14845 }
14846 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014847
14848 i = CNT(badlen - 1, goodlen - 1);
14849 vim_free(cnt);
14850 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014851}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000014852
Bram Moolenaar4770d092006-01-12 23:22:24 +000014853typedef struct
14854{
14855 int badi;
14856 int goodi;
14857 int score;
14858} limitscore_T;
14859
14860/*
14861 * Like spell_edit_score(), but with a limit on the score to make it faster.
14862 * May return SCORE_MAXMAX when the score is higher than "limit".
14863 *
14864 * This uses a stack for the edits still to be tried.
14865 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
14866 * for multi-byte characters.
14867 */
14868 static int
14869spell_edit_score_limit(slang, badword, goodword, limit)
14870 slang_T *slang;
14871 char_u *badword;
14872 char_u *goodword;
14873 int limit;
14874{
14875 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
14876 int stackidx;
14877 int bi, gi;
14878 int bi2, gi2;
14879 int bc, gc;
14880 int score;
14881 int score_off;
14882 int minscore;
14883 int round;
14884
14885#ifdef FEAT_MBYTE
14886 /* Multi-byte characters require a bit more work, use a different function
14887 * to avoid testing "has_mbyte" quite often. */
14888 if (has_mbyte)
14889 return spell_edit_score_limit_w(slang, badword, goodword, limit);
14890#endif
14891
14892 /*
14893 * The idea is to go from start to end over the words. So long as
14894 * characters are equal just continue, this always gives the lowest score.
14895 * When there is a difference try several alternatives. Each alternative
14896 * increases "score" for the edit distance. Some of the alternatives are
14897 * pushed unto a stack and tried later, some are tried right away. At the
14898 * end of the word the score for one alternative is known. The lowest
14899 * possible score is stored in "minscore".
14900 */
14901 stackidx = 0;
14902 bi = 0;
14903 gi = 0;
14904 score = 0;
14905 minscore = limit + 1;
14906
14907 for (;;)
14908 {
14909 /* Skip over an equal part, score remains the same. */
14910 for (;;)
14911 {
14912 bc = badword[bi];
14913 gc = goodword[gi];
14914 if (bc != gc) /* stop at a char that's different */
14915 break;
14916 if (bc == NUL) /* both words end */
14917 {
14918 if (score < minscore)
14919 minscore = score;
14920 goto pop; /* do next alternative */
14921 }
14922 ++bi;
14923 ++gi;
14924 }
14925
14926 if (gc == NUL) /* goodword ends, delete badword chars */
14927 {
14928 do
14929 {
14930 if ((score += SCORE_DEL) >= minscore)
14931 goto pop; /* do next alternative */
14932 } while (badword[++bi] != NUL);
14933 minscore = score;
14934 }
14935 else if (bc == NUL) /* badword ends, insert badword chars */
14936 {
14937 do
14938 {
14939 if ((score += SCORE_INS) >= minscore)
14940 goto pop; /* do next alternative */
14941 } while (goodword[++gi] != NUL);
14942 minscore = score;
14943 }
14944 else /* both words continue */
14945 {
14946 /* If not close to the limit, perform a change. Only try changes
14947 * that may lead to a lower score than "minscore".
14948 * round 0: try deleting a char from badword
14949 * round 1: try inserting a char in badword */
14950 for (round = 0; round <= 1; ++round)
14951 {
14952 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
14953 if (score_off < minscore)
14954 {
14955 if (score_off + SCORE_EDIT_MIN >= minscore)
14956 {
14957 /* Near the limit, rest of the words must match. We
14958 * can check that right now, no need to push an item
14959 * onto the stack. */
14960 bi2 = bi + 1 - round;
14961 gi2 = gi + round;
14962 while (goodword[gi2] == badword[bi2])
14963 {
14964 if (goodword[gi2] == NUL)
14965 {
14966 minscore = score_off;
14967 break;
14968 }
14969 ++bi2;
14970 ++gi2;
14971 }
14972 }
14973 else
14974 {
14975 /* try deleting/inserting a character later */
14976 stack[stackidx].badi = bi + 1 - round;
14977 stack[stackidx].goodi = gi + round;
14978 stack[stackidx].score = score_off;
14979 ++stackidx;
14980 }
14981 }
14982 }
14983
14984 if (score + SCORE_SWAP < minscore)
14985 {
14986 /* If swapping two characters makes a match then the
14987 * substitution is more expensive, thus there is no need to
14988 * try both. */
14989 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
14990 {
14991 /* Swap two characters, that is: skip them. */
14992 gi += 2;
14993 bi += 2;
14994 score += SCORE_SWAP;
14995 continue;
14996 }
14997 }
14998
14999 /* Substitute one character for another which is the same
15000 * thing as deleting a character from both goodword and badword.
15001 * Use a better score when there is only a case difference. */
15002 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15003 score += SCORE_ICASE;
15004 else
15005 {
15006 /* For a similar character use SCORE_SIMILAR. */
15007 if (slang != NULL
15008 && slang->sl_has_map
15009 && similar_chars(slang, gc, bc))
15010 score += SCORE_SIMILAR;
15011 else
15012 score += SCORE_SUBST;
15013 }
15014
15015 if (score < minscore)
15016 {
15017 /* Do the substitution. */
15018 ++gi;
15019 ++bi;
15020 continue;
15021 }
15022 }
15023pop:
15024 /*
15025 * Get here to try the next alternative, pop it from the stack.
15026 */
15027 if (stackidx == 0) /* stack is empty, finished */
15028 break;
15029
15030 /* pop an item from the stack */
15031 --stackidx;
15032 gi = stack[stackidx].goodi;
15033 bi = stack[stackidx].badi;
15034 score = stack[stackidx].score;
15035 }
15036
15037 /* When the score goes over "limit" it may actually be much higher.
15038 * Return a very large number to avoid going below the limit when giving a
15039 * bonus. */
15040 if (minscore > limit)
15041 return SCORE_MAXMAX;
15042 return minscore;
15043}
15044
15045#ifdef FEAT_MBYTE
15046/*
15047 * Multi-byte version of spell_edit_score_limit().
15048 * Keep it in sync with the above!
15049 */
15050 static int
15051spell_edit_score_limit_w(slang, badword, goodword, limit)
15052 slang_T *slang;
15053 char_u *badword;
15054 char_u *goodword;
15055 int limit;
15056{
15057 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15058 int stackidx;
15059 int bi, gi;
15060 int bi2, gi2;
15061 int bc, gc;
15062 int score;
15063 int score_off;
15064 int minscore;
15065 int round;
15066 char_u *p;
15067 int wbadword[MAXWLEN];
15068 int wgoodword[MAXWLEN];
15069
15070 /* Get the characters from the multi-byte strings and put them in an
15071 * int array for easy access. */
15072 bi = 0;
15073 for (p = badword; *p != NUL; )
15074 wbadword[bi++] = mb_cptr2char_adv(&p);
15075 wbadword[bi++] = 0;
15076 gi = 0;
15077 for (p = goodword; *p != NUL; )
15078 wgoodword[gi++] = mb_cptr2char_adv(&p);
15079 wgoodword[gi++] = 0;
15080
15081 /*
15082 * The idea is to go from start to end over the words. So long as
15083 * characters are equal just continue, this always gives the lowest score.
15084 * When there is a difference try several alternatives. Each alternative
15085 * increases "score" for the edit distance. Some of the alternatives are
15086 * pushed unto a stack and tried later, some are tried right away. At the
15087 * end of the word the score for one alternative is known. The lowest
15088 * possible score is stored in "minscore".
15089 */
15090 stackidx = 0;
15091 bi = 0;
15092 gi = 0;
15093 score = 0;
15094 minscore = limit + 1;
15095
15096 for (;;)
15097 {
15098 /* Skip over an equal part, score remains the same. */
15099 for (;;)
15100 {
15101 bc = wbadword[bi];
15102 gc = wgoodword[gi];
15103
15104 if (bc != gc) /* stop at a char that's different */
15105 break;
15106 if (bc == NUL) /* both words end */
15107 {
15108 if (score < minscore)
15109 minscore = score;
15110 goto pop; /* do next alternative */
15111 }
15112 ++bi;
15113 ++gi;
15114 }
15115
15116 if (gc == NUL) /* goodword ends, delete badword chars */
15117 {
15118 do
15119 {
15120 if ((score += SCORE_DEL) >= minscore)
15121 goto pop; /* do next alternative */
15122 } while (wbadword[++bi] != NUL);
15123 minscore = score;
15124 }
15125 else if (bc == NUL) /* badword ends, insert badword chars */
15126 {
15127 do
15128 {
15129 if ((score += SCORE_INS) >= minscore)
15130 goto pop; /* do next alternative */
15131 } while (wgoodword[++gi] != NUL);
15132 minscore = score;
15133 }
15134 else /* both words continue */
15135 {
15136 /* If not close to the limit, perform a change. Only try changes
15137 * that may lead to a lower score than "minscore".
15138 * round 0: try deleting a char from badword
15139 * round 1: try inserting a char in badword */
15140 for (round = 0; round <= 1; ++round)
15141 {
15142 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15143 if (score_off < minscore)
15144 {
15145 if (score_off + SCORE_EDIT_MIN >= minscore)
15146 {
15147 /* Near the limit, rest of the words must match. We
15148 * can check that right now, no need to push an item
15149 * onto the stack. */
15150 bi2 = bi + 1 - round;
15151 gi2 = gi + round;
15152 while (wgoodword[gi2] == wbadword[bi2])
15153 {
15154 if (wgoodword[gi2] == NUL)
15155 {
15156 minscore = score_off;
15157 break;
15158 }
15159 ++bi2;
15160 ++gi2;
15161 }
15162 }
15163 else
15164 {
15165 /* try deleting a character from badword later */
15166 stack[stackidx].badi = bi + 1 - round;
15167 stack[stackidx].goodi = gi + round;
15168 stack[stackidx].score = score_off;
15169 ++stackidx;
15170 }
15171 }
15172 }
15173
15174 if (score + SCORE_SWAP < minscore)
15175 {
15176 /* If swapping two characters makes a match then the
15177 * substitution is more expensive, thus there is no need to
15178 * try both. */
15179 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
15180 {
15181 /* Swap two characters, that is: skip them. */
15182 gi += 2;
15183 bi += 2;
15184 score += SCORE_SWAP;
15185 continue;
15186 }
15187 }
15188
15189 /* Substitute one character for another which is the same
15190 * thing as deleting a character from both goodword and badword.
15191 * Use a better score when there is only a case difference. */
15192 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15193 score += SCORE_ICASE;
15194 else
15195 {
15196 /* For a similar character use SCORE_SIMILAR. */
15197 if (slang != NULL
15198 && slang->sl_has_map
15199 && similar_chars(slang, gc, bc))
15200 score += SCORE_SIMILAR;
15201 else
15202 score += SCORE_SUBST;
15203 }
15204
15205 if (score < minscore)
15206 {
15207 /* Do the substitution. */
15208 ++gi;
15209 ++bi;
15210 continue;
15211 }
15212 }
15213pop:
15214 /*
15215 * Get here to try the next alternative, pop it from the stack.
15216 */
15217 if (stackidx == 0) /* stack is empty, finished */
15218 break;
15219
15220 /* pop an item from the stack */
15221 --stackidx;
15222 gi = stack[stackidx].goodi;
15223 bi = stack[stackidx].badi;
15224 score = stack[stackidx].score;
15225 }
15226
15227 /* When the score goes over "limit" it may actually be much higher.
15228 * Return a very large number to avoid going below the limit when giving a
15229 * bonus. */
15230 if (minscore > limit)
15231 return SCORE_MAXMAX;
15232 return minscore;
15233}
15234#endif
15235
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015236/*
15237 * ":spellinfo"
15238 */
15239/*ARGSUSED*/
15240 void
15241ex_spellinfo(eap)
15242 exarg_T *eap;
15243{
15244 int lpi;
15245 langp_T *lp;
15246 char_u *p;
15247
15248 if (no_spell_checking(curwin))
15249 return;
15250
15251 msg_start();
15252 for (lpi = 0; lpi < curbuf->b_langp.ga_len && !got_int; ++lpi)
15253 {
15254 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
15255 msg_puts((char_u *)"file: ");
15256 msg_puts(lp->lp_slang->sl_fname);
15257 msg_putchar('\n');
15258 p = lp->lp_slang->sl_info;
15259 if (p != NULL)
15260 {
15261 msg_puts(p);
15262 msg_putchar('\n');
15263 }
15264 }
15265 msg_end();
15266}
15267
Bram Moolenaar4770d092006-01-12 23:22:24 +000015268#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
15269#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015270#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +000015271#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
15272#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015273
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015274/*
15275 * ":spelldump"
15276 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015277 void
15278ex_spelldump(eap)
15279 exarg_T *eap;
15280{
15281 buf_T *buf = curbuf;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015282
15283 if (no_spell_checking(curwin))
15284 return;
15285
15286 /* Create a new empty buffer by splitting the window. */
15287 do_cmdline_cmd((char_u *)"new");
15288 if (!bufempty() || !buf_valid(buf))
15289 return;
15290
15291 spell_dump_compl(buf, NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
15292
15293 /* Delete the empty line that we started with. */
15294 if (curbuf->b_ml.ml_line_count > 1)
15295 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
15296
15297 redraw_later(NOT_VALID);
15298}
15299
15300/*
15301 * Go through all possible words and:
15302 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
15303 * "ic" and "dir" are not used.
15304 * 2. When "pat" is not NULL: add matching words to insert mode completion.
15305 */
15306 void
15307spell_dump_compl(buf, pat, ic, dir, dumpflags_arg)
15308 buf_T *buf; /* buffer with spell checking */
15309 char_u *pat; /* leading part of the word */
15310 int ic; /* ignore case */
15311 int *dir; /* direction for adding matches */
15312 int dumpflags_arg; /* DUMPFLAG_* */
15313{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015314 langp_T *lp;
15315 slang_T *slang;
15316 idx_T arridx[MAXWLEN];
15317 int curi[MAXWLEN];
15318 char_u word[MAXWLEN];
15319 int c;
15320 char_u *byts;
15321 idx_T *idxs;
15322 linenr_T lnum = 0;
15323 int round;
15324 int depth;
15325 int n;
15326 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015327 char_u *region_names = NULL; /* region names being used */
15328 int do_region = TRUE; /* dump region names and numbers */
15329 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015330 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015331 int dumpflags = dumpflags_arg;
15332 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015333
Bram Moolenaard0131a82006-03-04 21:46:13 +000015334 /* When ignoring case or when the pattern starts with capital pass this on
15335 * to dump_word(). */
15336 if (pat != NULL)
15337 {
15338 if (ic)
15339 dumpflags |= DUMPFLAG_ICASE;
15340 else
15341 {
15342 n = captype(pat, NULL);
15343 if (n == WF_ONECAP)
15344 dumpflags |= DUMPFLAG_ONECAP;
15345 else if (n == WF_ALLCAP
15346#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015347 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015348#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015349 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +000015350#endif
15351 )
15352 dumpflags |= DUMPFLAG_ALLCAP;
15353 }
15354 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015355
Bram Moolenaar7887d882005-07-01 22:33:52 +000015356 /* Find out if we can support regions: All languages must support the same
15357 * regions or none at all. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015358 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000015359 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015360 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000015361 p = lp->lp_slang->sl_regions;
15362 if (p[0] != 0)
15363 {
15364 if (region_names == NULL) /* first language with regions */
15365 region_names = p;
15366 else if (STRCMP(region_names, p) != 0)
15367 {
15368 do_region = FALSE; /* region names are different */
15369 break;
15370 }
15371 }
15372 }
15373
15374 if (do_region && region_names != NULL)
15375 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015376 if (pat == NULL)
15377 {
15378 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15379 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15380 }
Bram Moolenaar7887d882005-07-01 22:33:52 +000015381 }
15382 else
15383 do_region = FALSE;
15384
15385 /*
15386 * Loop over all files loaded for the entries in 'spelllang'.
15387 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015388 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015389 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015390 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015391 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015392 if (slang->sl_fbyts == NULL) /* reloading failed */
15393 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015394
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015395 if (pat == NULL)
15396 {
15397 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15398 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15399 }
15400
15401 /* When matching with a pattern and there are no prefixes only use
15402 * parts of the tree that match "pat". */
15403 if (pat != NULL && slang->sl_pbyts == NULL)
15404 patlen = STRLEN(pat);
15405 else
15406 patlen = 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015407
15408 /* round 1: case-folded tree
15409 * round 2: keep-case tree */
15410 for (round = 1; round <= 2; ++round)
15411 {
15412 if (round == 1)
15413 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015414 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015415 byts = slang->sl_fbyts;
15416 idxs = slang->sl_fidxs;
15417 }
15418 else
15419 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015420 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015421 byts = slang->sl_kbyts;
15422 idxs = slang->sl_kidxs;
15423 }
15424 if (byts == NULL)
15425 continue; /* array is empty */
15426
15427 depth = 0;
15428 arridx[0] = 0;
15429 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015430 while (depth >= 0 && !got_int
15431 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015432 {
15433 if (curi[depth] > byts[arridx[depth]])
15434 {
15435 /* Done all bytes at this node, go up one level. */
15436 --depth;
15437 line_breakcheck();
Bram Moolenaara2031822006-03-07 22:29:51 +000015438 ins_compl_check_keys(50);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015439 }
15440 else
15441 {
15442 /* Do one more byte at this node. */
15443 n = arridx[depth] + curi[depth];
15444 ++curi[depth];
15445 c = byts[n];
15446 if (c == 0)
15447 {
15448 /* End of word, deal with the word.
15449 * Don't use keep-case words in the fold-case tree,
15450 * they will appear in the keep-case tree.
15451 * Only use the word when the region matches. */
15452 flags = (int)idxs[n];
15453 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015454 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000015455 && (do_region
15456 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015457 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015458 & lp->lp_region) != 0))
15459 {
15460 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015461 if (!do_region)
15462 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015463
15464 /* Dump the basic word if there is no prefix or
15465 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015466 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015467 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015468 {
15469 dump_word(slang, word, pat, dir,
15470 dumpflags, flags, lnum);
15471 if (pat == NULL)
15472 ++lnum;
15473 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015474
15475 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015476 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015477 lnum = dump_prefixes(slang, word, pat, dir,
15478 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015479 }
15480 }
15481 else
15482 {
15483 /* Normal char, go one level deeper. */
15484 word[depth++] = c;
15485 arridx[depth] = idxs[n];
15486 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015487
15488 /* Check if this characters matches with the pattern.
15489 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +000015490 * Always ignore case here, dump_word() will check
15491 * proper case later. This isn't exactly right when
15492 * length changes for multi-byte characters with
15493 * ignore case... */
15494 if (depth <= patlen
15495 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015496 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015497 }
15498 }
15499 }
15500 }
15501 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015502}
15503
15504/*
15505 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015506 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015507 */
15508 static void
Bram Moolenaard0131a82006-03-04 21:46:13 +000015509dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015510 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015511 char_u *word;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015512 char_u *pat;
15513 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015514 int dumpflags;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015515 int wordflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015516 linenr_T lnum;
15517{
15518 int keepcap = FALSE;
15519 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015520 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015521 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000015522 char_u badword[MAXWLEN + 10];
15523 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015524 int flags = wordflags;
15525
15526 if (dumpflags & DUMPFLAG_ONECAP)
15527 flags |= WF_ONECAP;
15528 if (dumpflags & DUMPFLAG_ALLCAP)
15529 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015530
Bram Moolenaar4770d092006-01-12 23:22:24 +000015531 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015532 {
15533 /* Need to fix case according to "flags". */
15534 make_case_word(word, cword, flags);
15535 p = cword;
15536 }
15537 else
15538 {
15539 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015540 if ((dumpflags & DUMPFLAG_KEEPCASE)
15541 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015542 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015543 keepcap = TRUE;
15544 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015545 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015546
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015547 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015548 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015549 /* Add flags and regions after a slash. */
15550 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015551 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015552 STRCPY(badword, p);
15553 STRCAT(badword, "/");
15554 if (keepcap)
15555 STRCAT(badword, "=");
15556 if (flags & WF_BANNED)
15557 STRCAT(badword, "!");
15558 else if (flags & WF_RARE)
15559 STRCAT(badword, "?");
15560 if (flags & WF_REGION)
15561 for (i = 0; i < 7; ++i)
15562 if (flags & (0x10000 << i))
15563 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
15564 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015565 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015566
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015567 if (dumpflags & DUMPFLAG_COUNT)
15568 {
15569 hashitem_T *hi;
15570
15571 /* Include the word count for ":spelldump!". */
15572 hi = hash_find(&slang->sl_wordcount, tw);
15573 if (!HASHITEM_EMPTY(hi))
15574 {
15575 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15576 tw, HI2WC(hi)->wc_count);
15577 p = IObuff;
15578 }
15579 }
15580
15581 ml_append(lnum, p, (colnr_T)0, FALSE);
15582 }
Bram Moolenaard0131a82006-03-04 21:46:13 +000015583 else if (((dumpflags & DUMPFLAG_ICASE)
15584 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
15585 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015586 && ins_compl_add_infercase(p, (int)STRLEN(p),
15587 dumpflags & DUMPFLAG_ICASE,
15588 NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015589 /* if dir was BACKWARD then honor it just once */
15590 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015591}
15592
15593/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000015594 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15595 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015596 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015597 * Return the updated line number.
15598 */
15599 static linenr_T
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015600dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015601 slang_T *slang;
15602 char_u *word; /* case-folded word */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015603 char_u *pat;
15604 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015605 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015606 int flags; /* flags with prefix ID */
15607 linenr_T startlnum;
15608{
15609 idx_T arridx[MAXWLEN];
15610 int curi[MAXWLEN];
15611 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015612 char_u word_up[MAXWLEN];
15613 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015614 int c;
15615 char_u *byts;
15616 idx_T *idxs;
15617 linenr_T lnum = startlnum;
15618 int depth;
15619 int n;
15620 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015621 int i;
15622
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015623 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015624 * upper-case letter in word_up[]. */
15625 c = PTR2CHAR(word);
15626 if (SPELL_TOUPPER(c) != c)
15627 {
15628 onecap_copy(word, word_up, TRUE);
15629 has_word_up = TRUE;
15630 }
15631
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015632 byts = slang->sl_pbyts;
15633 idxs = slang->sl_pidxs;
15634 if (byts != NULL) /* array not is empty */
15635 {
15636 /*
15637 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015638 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015639 */
15640 depth = 0;
15641 arridx[0] = 0;
15642 curi[0] = 1;
15643 while (depth >= 0 && !got_int)
15644 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015645 n = arridx[depth];
15646 len = byts[n];
15647 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015648 {
15649 /* Done all bytes at this node, go up one level. */
15650 --depth;
15651 line_breakcheck();
15652 }
15653 else
15654 {
15655 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015656 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015657 ++curi[depth];
15658 c = byts[n];
15659 if (c == 0)
15660 {
15661 /* End of prefix, find out how many IDs there are. */
15662 for (i = 1; i < len; ++i)
15663 if (byts[n + i] != 0)
15664 break;
15665 curi[depth] += i - 1;
15666
Bram Moolenaar53805d12005-08-01 07:08:33 +000015667 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15668 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015669 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015670 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015671 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015672 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015673 : flags, lnum);
15674 if (lnum != 0)
15675 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015676 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000015677
15678 /* Check for prefix that matches the word when the
15679 * first letter is upper-case, but only if the prefix has
15680 * a condition. */
15681 if (has_word_up)
15682 {
15683 c = valid_word_prefix(i, n, flags, word_up, slang,
15684 TRUE);
15685 if (c != 0)
15686 {
15687 vim_strncpy(prefix + depth, word_up,
15688 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015689 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015690 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015691 : flags, lnum);
15692 if (lnum != 0)
15693 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +000015694 }
15695 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015696 }
15697 else
15698 {
15699 /* Normal char, go one level deeper. */
15700 prefix[depth++] = c;
15701 arridx[depth] = idxs[n];
15702 curi[depth] = 1;
15703 }
15704 }
15705 }
15706 }
15707
15708 return lnum;
15709}
15710
Bram Moolenaar95529562005-08-25 21:21:38 +000015711/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015712 * Move "p" to the end of word "start".
15713 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000015714 */
15715 char_u *
15716spell_to_word_end(start, buf)
15717 char_u *start;
15718 buf_T *buf;
15719{
15720 char_u *p = start;
15721
15722 while (*p != NUL && spell_iswordp(p, buf))
15723 mb_ptr_adv(p);
15724 return p;
15725}
15726
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015727#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015728/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015729 * For Insert mode completion CTRL-X s:
15730 * Find start of the word in front of column "startcol".
15731 * We don't check if it is badly spelled, with completion we can only change
15732 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015733 * Returns the column number of the word.
15734 */
15735 int
15736spell_word_start(startcol)
15737 int startcol;
15738{
15739 char_u *line;
15740 char_u *p;
15741 int col = 0;
15742
Bram Moolenaar95529562005-08-25 21:21:38 +000015743 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015744 return startcol;
15745
15746 /* Find a word character before "startcol". */
15747 line = ml_get_curline();
15748 for (p = line + startcol; p > line; )
15749 {
15750 mb_ptr_back(line, p);
15751 if (spell_iswordp_nmw(p))
15752 break;
15753 }
15754
15755 /* Go back to start of the word. */
15756 while (p > line)
15757 {
15758 col = p - line;
15759 mb_ptr_back(line, p);
15760 if (!spell_iswordp(p, curbuf))
15761 break;
15762 col = 0;
15763 }
15764
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015765 return col;
15766}
15767
15768/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000015769 * Need to check for 'spellcapcheck' now, the word is removed before
15770 * expand_spelling() is called. Therefore the ugly global variable.
15771 */
15772static int spell_expand_need_cap;
15773
15774 void
15775spell_expand_check_cap(col)
15776 colnr_T col;
15777{
15778 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
15779}
15780
15781/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015782 * Get list of spelling suggestions.
15783 * Used for Insert mode completion CTRL-X ?.
15784 * Returns the number of matches. The matches are in "matchp[]", array of
15785 * allocated strings.
15786 */
15787/*ARGSUSED*/
15788 int
15789expand_spelling(lnum, col, pat, matchp)
15790 linenr_T lnum;
15791 int col;
15792 char_u *pat;
15793 char_u ***matchp;
15794{
15795 garray_T ga;
15796
Bram Moolenaar4770d092006-01-12 23:22:24 +000015797 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015798 *matchp = ga.ga_data;
15799 return ga.ga_len;
15800}
15801#endif
15802
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000015803#endif /* FEAT_SPELL */