blob: 22630b5497a3bd748762417246265d4ee7146b9c [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> */
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000359#define WFP_RARE 0x01 /* rare prefix */
360#define WFP_NC 0x02 /* prefix is not combining */
361#define WFP_UP 0x04 /* to-upper prefix */
362#define WFP_COMPPERMIT 0x08 /* prefix with COMPOUNDPERMITFLAG */
363#define WFP_COMPFORBID 0x10 /* prefix with COMPOUNDFORBIDFLAG */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000364
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000365/* Flags for postponed prefixes in "sl_pidxs". Must be above affixID (one
366 * byte) and prefcondnr (two bytes). */
367#define WF_RAREPFX (WFP_RARE << 24) /* rare postponed prefix */
368#define WF_PFX_NC (WFP_NC << 24) /* non-combining postponed prefix */
369#define WF_PFX_UP (WFP_UP << 24) /* to-upper postponed prefix */
370#define WF_PFX_COMPPERMIT (WFP_COMPPERMIT << 24) /* postponed prefix with
371 * COMPOUNDPERMITFLAG */
372#define WF_PFX_COMPFORBID (WFP_COMPFORBID << 24) /* postponed prefix with
373 * COMPOUNDFORBIDFLAG */
374
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000375
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000376/* flags for <compoptions> */
377#define COMP_CHECKDUP 1 /* CHECKCOMPOUNDDUP */
378#define COMP_CHECKREP 2 /* CHECKCOMPOUNDREP */
379#define COMP_CHECKCASE 4 /* CHECKCOMPOUNDCASE */
380#define COMP_CHECKTRIPLE 8 /* CHECKCOMPOUNDTRIPLE */
381
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000382/* Special byte values for <byte>. Some are only used in the tree for
383 * postponed prefixes, some only in the other trees. This is a bit messy... */
384#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000385 * postponed prefix: no <pflags> */
386#define BY_INDEX 1 /* child is shared, index follows */
387#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
388 * postponed prefix: <pflags> follows */
389#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
390 * follow; never used in prefix tree */
391#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000392
Bram Moolenaar4770d092006-01-12 23:22:24 +0000393/* Info from "REP", "REPSAL" and "SAL" entries in ".aff" file used in si_rep,
394 * si_repsal, sl_rep, and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000395 * One replacement: from "ft_from" to "ft_to". */
396typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000397{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000398 char_u *ft_from;
399 char_u *ft_to;
400} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000401
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000402/* Info from "SAL" entries in ".aff" file used in sl_sal.
403 * The info is split for quick processing by spell_soundfold().
404 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
405typedef struct salitem_S
406{
407 char_u *sm_lead; /* leading letters */
408 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000409 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000410 char_u *sm_rules; /* rules like ^, $, priority */
411 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000412#ifdef FEAT_MBYTE
413 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000414 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000415 int *sm_to_w; /* wide character copy of "sm_to" */
416#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000417} salitem_T;
418
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000419#ifdef FEAT_MBYTE
420typedef int salfirst_T;
421#else
422typedef short salfirst_T;
423#endif
424
Bram Moolenaar5195e452005-08-19 20:32:47 +0000425/* Values for SP_*ERROR are negative, positive values are used by
426 * read_cnt_string(). */
427#define SP_TRUNCERROR -1 /* spell file truncated error */
428#define SP_FORMERROR -2 /* format error in spell file */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000429#define SP_OTHERERROR -3 /* other error while reading spell file */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000430
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000431/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000432 * Structure used to store words and other info for one language, loaded from
433 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000434 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
435 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
436 *
437 * The "byts" array stores the possible bytes in each tree node, preceded by
438 * the number of possible bytes, sorted on byte value:
439 * <len> <byte1> <byte2> ...
440 * The "idxs" array stores the index of the child node corresponding to the
441 * byte in "byts".
442 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000443 * the flags, region mask and affixID for the word. There may be several
444 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000445 */
446typedef struct slang_S slang_T;
447struct slang_S
448{
449 slang_T *sl_next; /* next language */
450 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000451 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000452 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000453
Bram Moolenaar51485f02005-06-04 21:55:20 +0000454 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000455 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000456 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000457 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000458 char_u *sl_pbyts; /* prefix tree word bytes */
459 idx_T *sl_pidxs; /* prefix tree word indexes */
460
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000461 char_u *sl_info; /* infotext string or NULL */
462
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000463 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000464
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000465 char_u *sl_midword; /* MIDWORD string or NULL */
466
Bram Moolenaar4770d092006-01-12 23:22:24 +0000467 hashtab_T sl_wordcount; /* hashtable with word count, wordcount_T */
468
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000469 int sl_compmax; /* COMPOUNDWORDMAX (default: MAXWLEN) */
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000470 int sl_compminlen; /* COMPOUNDMIN (default: 0) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000471 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000472 int sl_compoptions; /* COMP_* flags */
473 garray_T sl_comppat; /* CHECKCOMPOUNDPATTERN items */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000474 regprog_T *sl_compprog; /* COMPOUNDRULE turned into a regexp progrm
Bram Moolenaar5195e452005-08-19 20:32:47 +0000475 * (NULL when no compounding) */
476 char_u *sl_compstartflags; /* flags for first compound word */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000477 char_u *sl_compallflags; /* all flags for compound words */
Bram Moolenaar78622822005-08-23 21:00:13 +0000478 char_u sl_nobreak; /* When TRUE: no spaces between words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000479 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
480 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000481
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000482 int sl_prefixcnt; /* number of items in "sl_prefprog" */
483 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
484
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000485 garray_T sl_rep; /* list of fromto_T entries from REP lines */
486 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
487 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000488 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000489 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000490 there is none */
491 int sl_followup; /* SAL followup */
492 int sl_collapse; /* SAL collapse_result */
493 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000494 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
495 * "sl_sal_first" maps chars, when has_mbyte
496 * "sl_sal" is a list of wide char lists. */
497 garray_T sl_repsal; /* list of fromto_T entries from REPSAL lines */
498 short sl_repsal_first[256]; /* sl_rep_first for REPSAL lines */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000499 int sl_nosplitsugs; /* don't suggest splitting a word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000500
501 /* Info from the .sug file. Loaded on demand. */
502 time_t sl_sugtime; /* timestamp for .sug file */
503 char_u *sl_sbyts; /* soundfolded word bytes */
504 idx_T *sl_sidxs; /* soundfolded word indexes */
505 buf_T *sl_sugbuf; /* buffer with word number table */
506 int sl_sugloaded; /* TRUE when .sug file was loaded or failed to
507 load */
508
Bram Moolenaarea424162005-06-16 21:51:00 +0000509 int sl_has_map; /* TRUE if there is a MAP line */
510#ifdef FEAT_MBYTE
511 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
512 int sl_map_array[256]; /* MAP for first 256 chars */
513#else
514 char_u sl_map_array[256]; /* MAP for first 256 chars */
515#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000516 hashtab_T sl_sounddone; /* table with soundfolded words that have
517 handled, see add_sound_suggest() */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000518};
519
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000520/* First language that is loaded, start of the linked list of loaded
521 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000522static slang_T *first_lang = NULL;
523
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000524/* Flags used in .spl file for soundsalike flags. */
525#define SAL_F0LLOWUP 1
526#define SAL_COLLAPSE 2
527#define SAL_REM_ACCENTS 4
528
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000529/*
530 * Structure used in "b_langp", filled from 'spelllang'.
531 */
532typedef struct langp_S
533{
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000534 slang_T *lp_slang; /* info for this language */
535 slang_T *lp_sallang; /* language used for sound folding or NULL */
536 slang_T *lp_replang; /* language used for REP items or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000537 int lp_region; /* bitmask for region or REGION_ALL */
538} langp_T;
539
540#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
541
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000542#define REGION_ALL 0xff /* word valid in all regions */
543
Bram Moolenaar5195e452005-08-19 20:32:47 +0000544#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
545#define VIMSPELLMAGICL 8
546#define VIMSPELLVERSION 50
547
Bram Moolenaar4770d092006-01-12 23:22:24 +0000548#define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */
549#define VIMSUGMAGICL 6
550#define VIMSUGVERSION 1
551
Bram Moolenaar5195e452005-08-19 20:32:47 +0000552/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
553#define SN_REGION 0 /* <regionname> section */
554#define SN_CHARFLAGS 1 /* charflags section */
555#define SN_MIDWORD 2 /* <midword> section */
556#define SN_PREFCOND 3 /* <prefcond> section */
557#define SN_REP 4 /* REP items section */
558#define SN_SAL 5 /* SAL items section */
559#define SN_SOFO 6 /* soundfolding section */
560#define SN_MAP 7 /* MAP items section */
561#define SN_COMPOUND 8 /* compound words section */
562#define SN_SYLLABLE 9 /* syllable section */
Bram Moolenaar78622822005-08-23 21:00:13 +0000563#define SN_NOBREAK 10 /* NOBREAK section */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000564#define SN_SUGFILE 11 /* timestamp for .sug file */
565#define SN_REPSAL 12 /* REPSAL items section */
566#define SN_WORDS 13 /* common words */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000567#define SN_NOSPLITSUGS 14 /* don't split word for suggestions */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000568#define SN_INFO 15 /* info section */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000569#define SN_END 255 /* end of sections */
570
571#define SNF_REQUIRED 1 /* <sectionflags>: required section */
572
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000573/* Result values. Lower number is accepted over higher one. */
574#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000575#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000576#define SP_RARE 1
577#define SP_LOCAL 2
578#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000579
Bram Moolenaar7887d882005-07-01 22:33:52 +0000580/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000581static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000582
Bram Moolenaar4770d092006-01-12 23:22:24 +0000583typedef struct wordcount_S
584{
585 short_u wc_count; /* nr of times word was seen */
586 char_u wc_word[1]; /* word, actually longer */
587} wordcount_T;
588
589static wordcount_T dumwc;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000590#define WC_KEY_OFF (unsigned)(dumwc.wc_word - (char_u *)&dumwc)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000591#define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF))
592#define MAXWORDCOUNT 0xffff
593
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000594/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000595 * Information used when looking for suggestions.
596 */
597typedef struct suginfo_S
598{
599 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000600 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000601 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000602 int su_sfmaxscore; /* idem, for when doing soundfold words */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000603 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000604 char_u *su_badptr; /* start of bad word in line */
605 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000606 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000607 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
608 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000609 char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000610 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000611 slang_T *su_sallang; /* default language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000612} suginfo_T;
613
614/* One word suggestion. Used in "si_ga". */
615typedef struct suggest_S
616{
617 char_u *st_word; /* suggested word, allocated string */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000618 int st_wordlen; /* STRLEN(st_word) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000619 int st_orglen; /* length of replaced text */
620 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000621 int st_altscore; /* used when st_score compares equal */
622 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000623 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000624 slang_T *st_slang; /* language used for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000625} suggest_T;
626
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000627#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000628
Bram Moolenaar4770d092006-01-12 23:22:24 +0000629/* TRUE if a word appears in the list of banned words. */
630#define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word)))
631
632/* Number of suggestions kept when cleaning up. we need to keep more than
633 * what is displayed, because when rescore_suggestions() is called the score
634 * may change and wrong suggestions may be removed later. */
635#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000636
637/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
638 * of suggestions that are not going to be displayed. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000639#define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000640
641/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000642#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000643#define SCORE_SPLIT_NO 249 /* split bad word with NOSPLITSUGS */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000644#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000645#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000646#define SCORE_RARE 180 /* rare word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000647#define SCORE_SWAP 75 /* swap two characters */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000648#define SCORE_SWAP3 110 /* swap two characters in three */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000649#define SCORE_REP 65 /* REP replacement */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000650#define SCORE_SUBST 93 /* substitute a character */
651#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000652#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000653#define SCORE_DEL 94 /* delete a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000654#define SCORE_DELDUP 66 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000655#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000656#define SCORE_INS 96 /* insert a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000657#define SCORE_INSDUP 67 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000658#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000659#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000660
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000661#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000662#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
663 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000664
Bram Moolenaar4770d092006-01-12 23:22:24 +0000665#define SCORE_COMMON1 30 /* subtracted for words seen before */
666#define SCORE_COMMON2 40 /* subtracted for words often seen */
667#define SCORE_COMMON3 50 /* subtracted for words very often seen */
668#define SCORE_THRES2 10 /* word count threshold for COMMON2 */
669#define SCORE_THRES3 100 /* word count threshold for COMMON3 */
670
671/* When trying changed soundfold words it becomes slow when trying more than
672 * two changes. With less then two changes it's slightly faster but we miss a
673 * few good suggestions. In rare cases we need to try three of four changes.
674 */
675#define SCORE_SFMAX1 200 /* maximum score for first try */
676#define SCORE_SFMAX2 300 /* maximum score for second try */
677#define SCORE_SFMAX3 400 /* maximum score for third try */
678
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000679#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000680#define SCORE_MAXMAX 999999 /* accept any score */
681#define SCORE_LIMITMAX 350 /* for spell_edit_score_limit() */
682
683/* for spell_edit_score_limit() we need to know the minimum value of
684 * SCORE_ICASE, SCORE_SWAP, SCORE_DEL, SCORE_SIMILAR and SCORE_INS */
685#define SCORE_EDIT_MIN SCORE_SIMILAR
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000686
687/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000688 * Structure to store info for word matching.
689 */
690typedef struct matchinf_S
691{
692 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000693
694 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000695 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000696 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000697 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000698 char_u *mi_cend; /* char after what was used for
699 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000700
701 /* case-folded text */
702 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000703 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000704
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000705 /* for when checking word after a prefix */
706 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000707 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000708 int mi_prefcnt; /* number of entries at mi_prefarridx */
709 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000710#ifdef FEAT_MBYTE
711 int mi_cprefixlen; /* byte length of prefix in original
712 case */
713#else
714# define mi_cprefixlen mi_prefixlen /* it's the same value */
715#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000716
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000717 /* for when checking a compound word */
718 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000719 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
720 int mi_complen; /* nr of compound words used */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000721 int mi_compextra; /* nr of COMPOUNDROOT words */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000722
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000723 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000724 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000725 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000726 buf_T *mi_buf; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000727
728 /* for NOBREAK */
729 int mi_result2; /* "mi_resul" without following word */
730 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000731} matchinf_T;
732
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000733/*
734 * The tables used for recognizing word characters according to spelling.
735 * These are only used for the first 256 characters of 'encoding'.
736 */
737typedef struct spelltab_S
738{
739 char_u st_isw[256]; /* flags: is word char */
740 char_u st_isu[256]; /* flags: is uppercase char */
741 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000742 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000743} spelltab_T;
744
745static spelltab_T spelltab;
746static int did_set_spelltab;
747
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000748#define CF_WORD 0x01
749#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000750
751static void clear_spell_chartab __ARGS((spelltab_T *sp));
752static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000753static int spell_iswordp __ARGS((char_u *p, buf_T *buf));
754static int spell_iswordp_nmw __ARGS((char_u *p));
755#ifdef FEAT_MBYTE
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +0000756static int spell_mb_isword_class __ARGS((int cl));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000757static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
758#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000759static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000760
761/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000762 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000763 */
764typedef enum
765{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000766 STATE_START = 0, /* At start of node check for NUL bytes (goodword
767 * ends); if badword ends there is a match, otherwise
768 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000769 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000770 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000771 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
772 STATE_PLAIN, /* Use each byte of the node. */
773 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000774 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000775 STATE_INS, /* Insert a byte in the bad word. */
776 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000777 STATE_UNSWAP, /* Undo swap two characters. */
778 STATE_SWAP3, /* Swap two characters over three. */
779 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000780 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000781 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000782 STATE_REP_INI, /* Prepare for using REP items. */
783 STATE_REP, /* Use matching REP items from the .aff file. */
784 STATE_REP_UNDO, /* Undo a REP item replacement. */
785 STATE_FINAL /* End of this node. */
786} state_T;
787
788/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000789 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000790 */
791typedef struct trystate_S
792{
Bram Moolenaarea424162005-06-16 21:51:00 +0000793 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000794 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000795 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000796 short ts_curi; /* index in list of child nodes */
797 char_u ts_fidx; /* index in fword[], case-folded bad word */
798 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
799 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000800 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000801 * PFD_PREFIXTREE or PFD_NOPREFIX */
802 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000803#ifdef FEAT_MBYTE
804 char_u ts_tcharlen; /* number of bytes in tword character */
805 char_u ts_tcharidx; /* current byte index in tword character */
806 char_u ts_isdiff; /* DIFF_ values */
807 char_u ts_fcharstart; /* index in fword where badword char started */
808#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000809 char_u ts_prewordlen; /* length of word in "preword[]" */
810 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000811 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000812 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000813 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000814 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000815 char_u ts_delidx; /* index in fword for char that was deleted,
816 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000817} trystate_T;
818
Bram Moolenaarea424162005-06-16 21:51:00 +0000819/* values for ts_isdiff */
820#define DIFF_NONE 0 /* no different byte (yet) */
821#define DIFF_YES 1 /* different byte found */
822#define DIFF_INSERT 2 /* inserting character */
823
Bram Moolenaard12a1322005-08-21 22:08:24 +0000824/* values for ts_flags */
825#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
826#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000827#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000828
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000829/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000830#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000831#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000832#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000833
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000834/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000835#define FIND_FOLDWORD 0 /* find word case-folded */
836#define FIND_KEEPWORD 1 /* find keep-case word */
837#define FIND_PREFIX 2 /* find word after prefix */
838#define FIND_COMPOUND 3 /* find case-folded compound word */
839#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000840
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000841static slang_T *slang_alloc __ARGS((char_u *lang));
842static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000843static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000844static void slang_clear_sug __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000845static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000846static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000847static 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 +0000848static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000849static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000850static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000851static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000852static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000853static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000854static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000855static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000856static 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 +0000857static int get2c __ARGS((FILE *fd));
858static int get3c __ARGS((FILE *fd));
859static int get4c __ARGS((FILE *fd));
860static time_t get8c __ARGS((FILE *fd));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000861static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000862static char_u *read_string __ARGS((FILE *fd, int cnt));
863static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
864static int read_charflags_section __ARGS((FILE *fd));
865static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000866static int read_rep_section __ARGS((FILE *fd, garray_T *gap, short *first));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000867static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000868static int read_words_section __ARGS((FILE *fd, slang_T *lp, int len));
869static void count_common_word __ARGS((slang_T *lp, char_u *word, int len, int count));
870static int score_wordcount_adj __ARGS((slang_T *slang, int score, char_u *word, int split));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000871static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
872static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000873static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000874static int init_syl_tab __ARGS((slang_T *slang));
875static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000876static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
877static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000878#ifdef FEAT_MBYTE
879static int *mb_str2wide __ARGS((char_u *s));
880#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000881static int spell_read_tree __ARGS((FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt));
882static 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 +0000883static void clear_midword __ARGS((buf_T *buf));
884static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000885static int find_region __ARGS((char_u *rp, char_u *region));
886static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000887static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000888static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000889static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000890static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000891static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000892static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar66fa2712006-01-22 23:22:22 +0000893static 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 +0000894#ifdef FEAT_EVAL
895static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
896#endif
897static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000898static void spell_suggest_intern __ARGS((suginfo_T *su, int interactive));
899static void suggest_load_files __ARGS((void));
900static void tree_count_words __ARGS((char_u *byts, idx_T *idxs));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000901static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000902static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000903static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000904static void suggest_try_special __ARGS((suginfo_T *su));
905static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000906static void suggest_trie_walk __ARGS((suginfo_T *su, langp_T *lp, char_u *fword, int soundfold));
907static void go_deeper __ARGS((trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000908#ifdef FEAT_MBYTE
909static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
910#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000911static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000912static void score_comp_sal __ARGS((suginfo_T *su));
913static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000914static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000915static void suggest_try_soundalike_prep __ARGS((void));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000916static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000917static void suggest_try_soundalike_finish __ARGS((void));
918static void add_sound_suggest __ARGS((suginfo_T *su, char_u *goodword, int score, langp_T *lp));
919static int soundfold_find __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000920static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000921static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000922static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000923static 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));
924static void check_suggestions __ARGS((suginfo_T *su, garray_T *gap));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000925static void add_banned __ARGS((suginfo_T *su, char_u *word));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000926static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000927static void rescore_one __ARGS((suginfo_T *su, suggest_T *stp));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000928static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000929static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
930static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
931static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000932#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000933static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000934#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000935static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000936static int spell_edit_score __ARGS((slang_T *slang, char_u *badword, char_u *goodword));
937static int spell_edit_score_limit __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
938#ifdef FEAT_MBYTE
939static int spell_edit_score_limit_w __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
940#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +0000941static void dump_word __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum));
942static 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 +0000943static buf_T *open_spellbuf __ARGS((void));
944static void close_spellbuf __ARGS((buf_T *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000945
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000946/*
947 * Use our own character-case definitions, because the current locale may
948 * differ from what the .spl file uses.
949 * These must not be called with negative number!
950 */
951#ifndef FEAT_MBYTE
952/* Non-multi-byte implementation. */
953# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
954# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
955# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
956#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000957# if defined(HAVE_WCHAR_H)
958# include <wchar.h> /* for towupper() and towlower() */
959# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000960/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
961 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
962 * the "w" library function for characters above 255 if available. */
963# ifdef HAVE_TOWLOWER
964# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
965 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
966# else
967# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
968 : (c) < 256 ? spelltab.st_fold[c] : (c))
969# endif
970
971# ifdef HAVE_TOWUPPER
972# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
973 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
974# else
975# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
976 : (c) < 256 ? spelltab.st_upper[c] : (c))
977# endif
978
979# ifdef HAVE_ISWUPPER
980# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
981 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
982# else
983# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000984 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000985# endif
986#endif
987
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000988
989static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000990static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000991static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000992static char *e_affname = N_("Affix name too long in %s line %d: %s");
993static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
994static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000995static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000996
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000997/* Remember what "z?" replaced. */
998static char_u *repl_from = NULL;
999static char_u *repl_to = NULL;
1000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001001/*
1002 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001003 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001004 * "*attrp" is set to the highlight index for a badly spelled word. For a
1005 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001006 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001007 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001008 * "capcol" is used to check for a Capitalised word after the end of a
1009 * sentence. If it's zero then perform the check. Return the column where to
1010 * check next, or -1 when no sentence end was found. If it's NULL then don't
1011 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001012 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001013 * Returns the length of the word in bytes, also when it's OK, so that the
1014 * caller can skip over the word.
1015 */
1016 int
Bram Moolenaar4770d092006-01-12 23:22:24 +00001017spell_check(wp, ptr, attrp, capcol, docount)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001018 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001019 char_u *ptr;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001020 hlf_T *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001021 int *capcol; /* column to check for Capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001022 int docount; /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001023{
1024 matchinf_T mi; /* Most things are put in "mi" so that it can
1025 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001026 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001027 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001028 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001029 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001030 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001031
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001032 /* A word never starts at a space or a control character. Return quickly
1033 * then, skipping over the character. */
1034 if (*ptr <= ' ')
1035 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001036
1037 /* Return here when loading language files failed. */
1038 if (wp->w_buffer->b_langp.ga_len == 0)
1039 return 1;
1040
Bram Moolenaar5195e452005-08-19 20:32:47 +00001041 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001042
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001043 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +00001044 * 0X99FF. But always do check spelling to find "3GPP" and "11
1045 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001046 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +00001047 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001048 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
1049 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001050 else
1051 mi.mi_end = skipdigits(ptr);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001052 nrlen = (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001053 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001054
Bram Moolenaar0c405862005-06-22 22:26:26 +00001055 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001056 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +00001057 mi.mi_fend = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001058 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001059 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001060 do
Bram Moolenaar51485f02005-06-04 21:55:20 +00001061 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001062 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001063 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001064
1065 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
1066 {
1067 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00001068 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001069 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001070 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001071 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001072 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001073 if (capcol != NULL)
1074 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001075
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001076 /* We always use the characters up to the next non-word character,
1077 * also for bad words. */
1078 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001079
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001080 /* Check caps type later. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001081 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001082
Bram Moolenaar5195e452005-08-19 20:32:47 +00001083 /* case-fold the word with one non-word character, so that we can check
1084 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001085 if (*mi.mi_fend != NUL)
1086 mb_ptr_adv(mi.mi_fend);
1087
1088 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
1089 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001090 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001091
1092 /* The word is bad unless we recognize it. */
1093 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +00001094 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001095
1096 /*
1097 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001098 * We check them all, because a word may be matched longer in another
1099 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001100 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001101 for (lpi = 0; lpi < wp->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001102 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001103 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, lpi);
1104
1105 /* If reloading fails the language is still in the list but everything
1106 * has been cleared. */
1107 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
1108 continue;
1109
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001110 /* Check for a matching word in case-folded words. */
1111 find_word(&mi, FIND_FOLDWORD);
1112
1113 /* Check for a matching word in keep-case words. */
1114 find_word(&mi, FIND_KEEPWORD);
1115
1116 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001117 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +00001118
1119 /* For a NOBREAK language, may want to use a word without a following
1120 * word as a backup. */
1121 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
1122 && mi.mi_result2 != SP_BAD)
1123 {
1124 mi.mi_result = mi.mi_result2;
1125 mi.mi_end = mi.mi_end2;
1126 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001127
1128 /* Count the word in the first language where it's found to be OK. */
1129 if (count_word && mi.mi_result == SP_OK)
1130 {
1131 count_common_word(mi.mi_lp->lp_slang, ptr,
1132 (int)(mi.mi_end - ptr), 1);
1133 count_word = FALSE;
1134 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001135 }
1136
1137 if (mi.mi_result != SP_OK)
1138 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001139 /* If we found a number skip over it. Allows for "42nd". Do flag
1140 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001141 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001142 {
1143 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1144 return nrlen;
1145 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001146
1147 /* When we are at a non-word character there is no error, just
1148 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001149 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001150 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001151 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
1152 {
1153 regmatch_T regmatch;
1154
1155 /* Check for end of sentence. */
1156 regmatch.regprog = wp->w_buffer->b_cap_prog;
1157 regmatch.rm_ic = FALSE;
1158 if (vim_regexec(&regmatch, ptr, 0))
1159 *capcol = (int)(regmatch.endp[0] - ptr);
1160 }
1161
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001162#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001163 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001164 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001165#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001166 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001167 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001168 else if (mi.mi_end == ptr)
1169 /* Always include at least one character. Required for when there
1170 * is a mixup in "midword". */
1171 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +00001172 else if (mi.mi_result == SP_BAD
1173 && LANGP_ENTRY(wp->w_buffer->b_langp, 0)->lp_slang->sl_nobreak)
1174 {
1175 char_u *p, *fp;
1176 int save_result = mi.mi_result;
1177
1178 /* First language in 'spelllang' is NOBREAK. Find first position
1179 * at which any word would be valid. */
1180 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001181 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +00001182 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001183 p = mi.mi_word;
1184 fp = mi.mi_fword;
1185 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +00001186 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001187 mb_ptr_adv(p);
1188 mb_ptr_adv(fp);
1189 if (p >= mi.mi_end)
1190 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001191 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001192 find_word(&mi, FIND_COMPOUND);
1193 if (mi.mi_result != SP_BAD)
1194 {
1195 mi.mi_end = p;
1196 break;
1197 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001198 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001199 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001200 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001201 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001202
1203 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001204 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001205 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001206 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001207 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001208 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001209 }
1210
Bram Moolenaar5195e452005-08-19 20:32:47 +00001211 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1212 {
1213 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001214 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001215 return wrongcaplen;
1216 }
1217
Bram Moolenaar51485f02005-06-04 21:55:20 +00001218 return (int)(mi.mi_end - ptr);
1219}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001220
Bram Moolenaar51485f02005-06-04 21:55:20 +00001221/*
1222 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001223 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1224 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1225 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1226 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001227 *
1228 * For a match mip->mi_result is updated.
1229 */
1230 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001231find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001232 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001233 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001234{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001235 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001236 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001237 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001238 int endidxcnt = 0;
1239 int len;
1240 int wlen = 0;
1241 int flen;
1242 int c;
1243 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001244 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001245#ifdef FEAT_MBYTE
1246 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001247#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001248 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001249 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001250 slang_T *slang = mip->mi_lp->lp_slang;
1251 unsigned flags;
1252 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001253 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001254 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001255 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001256 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001257
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001258 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001259 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001260 /* Check for word with matching case in keep-case tree. */
1261 ptr = mip->mi_word;
1262 flen = 9999; /* no case folding, always enough bytes */
1263 byts = slang->sl_kbyts;
1264 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001265
1266 if (mode == FIND_KEEPCOMPOUND)
1267 /* Skip over the previously found word(s). */
1268 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001269 }
1270 else
1271 {
1272 /* Check for case-folded in case-folded tree. */
1273 ptr = mip->mi_fword;
1274 flen = mip->mi_fwordlen; /* available case-folded bytes */
1275 byts = slang->sl_fbyts;
1276 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001277
1278 if (mode == FIND_PREFIX)
1279 {
1280 /* Skip over the prefix. */
1281 wlen = mip->mi_prefixlen;
1282 flen -= mip->mi_prefixlen;
1283 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001284 else if (mode == FIND_COMPOUND)
1285 {
1286 /* Skip over the previously found word(s). */
1287 wlen = mip->mi_compoff;
1288 flen -= mip->mi_compoff;
1289 }
1290
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001291 }
1292
Bram Moolenaar51485f02005-06-04 21:55:20 +00001293 if (byts == NULL)
1294 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001295
Bram Moolenaar51485f02005-06-04 21:55:20 +00001296 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001297 * Repeat advancing in the tree until:
1298 * - there is a byte that doesn't match,
1299 * - we reach the end of the tree,
1300 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001301 */
1302 for (;;)
1303 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001304 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001305 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001306
1307 len = byts[arridx++];
1308
1309 /* If the first possible byte is a zero the word could end here.
1310 * Remember this index, we first check for the longest word. */
1311 if (byts[arridx] == 0)
1312 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001313 if (endidxcnt == MAXWLEN)
1314 {
1315 /* Must be a corrupted spell file. */
1316 EMSG(_(e_format));
1317 return;
1318 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001319 endlen[endidxcnt] = wlen;
1320 endidx[endidxcnt++] = arridx++;
1321 --len;
1322
1323 /* Skip over the zeros, there can be several flag/region
1324 * combinations. */
1325 while (len > 0 && byts[arridx] == 0)
1326 {
1327 ++arridx;
1328 --len;
1329 }
1330 if (len == 0)
1331 break; /* no children, word must end here */
1332 }
1333
1334 /* Stop looking at end of the line. */
1335 if (ptr[wlen] == NUL)
1336 break;
1337
1338 /* Perform a binary search in the list of accepted bytes. */
1339 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001340 if (c == TAB) /* <Tab> is handled like <Space> */
1341 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001342 lo = arridx;
1343 hi = arridx + len - 1;
1344 while (lo < hi)
1345 {
1346 m = (lo + hi) / 2;
1347 if (byts[m] > c)
1348 hi = m - 1;
1349 else if (byts[m] < c)
1350 lo = m + 1;
1351 else
1352 {
1353 lo = hi = m;
1354 break;
1355 }
1356 }
1357
1358 /* Stop if there is no matching byte. */
1359 if (hi < lo || byts[lo] != c)
1360 break;
1361
1362 /* Continue at the child (if there is one). */
1363 arridx = idxs[lo];
1364 ++wlen;
1365 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001366
1367 /* One space in the good word may stand for several spaces in the
1368 * checked word. */
1369 if (c == ' ')
1370 {
1371 for (;;)
1372 {
1373 if (flen <= 0 && *mip->mi_fend != NUL)
1374 flen = fold_more(mip);
1375 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1376 break;
1377 ++wlen;
1378 --flen;
1379 }
1380 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001381 }
1382
1383 /*
1384 * Verify that one of the possible endings is valid. Try the longest
1385 * first.
1386 */
1387 while (endidxcnt > 0)
1388 {
1389 --endidxcnt;
1390 arridx = endidx[endidxcnt];
1391 wlen = endlen[endidxcnt];
1392
1393#ifdef FEAT_MBYTE
1394 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1395 continue; /* not at first byte of character */
1396#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001397 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001398 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001399 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001400 continue; /* next char is a word character */
1401 word_ends = FALSE;
1402 }
1403 else
1404 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001405 /* The prefix flag is before compound flags. Once a valid prefix flag
1406 * has been found we try compound flags. */
1407 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001408
1409#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001410 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001411 {
1412 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001413 * when folding case. This can be slow, take a shortcut when the
1414 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001415 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001416 if (STRNCMP(ptr, p, wlen) != 0)
1417 {
1418 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1419 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001420 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001421 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001422 }
1423#endif
1424
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001425 /* Check flags and region. For FIND_PREFIX check the condition and
1426 * prefix ID.
1427 * Repeat this if there are more flags/region alternatives until there
1428 * is a match. */
1429 res = SP_BAD;
1430 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1431 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001432 {
1433 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001434
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001435 /* For the fold-case tree check that the case of the checked word
1436 * matches with what the word in the tree requires.
1437 * For keep-case tree the case is always right. For prefixes we
1438 * don't bother to check. */
1439 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001440 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001441 if (mip->mi_cend != mip->mi_word + wlen)
1442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001443 /* mi_capflags was set for a different word length, need
1444 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001445 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001446 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001447 }
1448
Bram Moolenaar0c405862005-06-22 22:26:26 +00001449 if (mip->mi_capflags == WF_KEEPCAP
1450 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001451 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001452 }
1453
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001454 /* When mode is FIND_PREFIX the word must support the prefix:
1455 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001456 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001457 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001458 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001459 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001460 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001461 mip->mi_word + mip->mi_cprefixlen, slang,
1462 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001463 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001464 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001465
1466 /* Use the WF_RARE flag for a rare prefix. */
1467 if (c & WF_RAREPFX)
1468 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001469 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001470 }
1471
Bram Moolenaar78622822005-08-23 21:00:13 +00001472 if (slang->sl_nobreak)
1473 {
1474 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1475 && (flags & WF_BANNED) == 0)
1476 {
1477 /* NOBREAK: found a valid following word. That's all we
1478 * need to know, so return. */
1479 mip->mi_result = SP_OK;
1480 break;
1481 }
1482 }
1483
1484 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1485 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001486 {
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00001487 /* If there is no compound flag or the word is shorter than
Bram Moolenaar5195e452005-08-19 20:32:47 +00001488 * COMPOUNDMIN reject it quickly.
1489 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001490 * that's too short... Myspell compatibility requires this
1491 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001492 if (((unsigned)flags >> 24) == 0
1493 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001494 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001495#ifdef FEAT_MBYTE
1496 /* For multi-byte chars check character length against
1497 * COMPOUNDMIN. */
1498 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001499 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001500 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1501 wlen - mip->mi_compoff) < slang->sl_compminlen)
1502 continue;
1503#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001504
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001505 /* Limit the number of compound words to COMPOUNDWORDMAX if no
Bram Moolenaare52325c2005-08-22 22:54:29 +00001506 * maximum for syllables is specified. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001507 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
1508 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +00001509 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001510 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001511
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001512 /* Don't allow compounding on a side where an affix was added,
1513 * unless COMPOUNDPERMITFLAG was used. */
1514 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
1515 continue;
1516 if (!word_ends && (flags & WF_NOCOMPAFT))
1517 continue;
1518
Bram Moolenaard12a1322005-08-21 22:08:24 +00001519 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001520 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001521 ? slang->sl_compstartflags
1522 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001523 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001524 continue;
1525
Bram Moolenaare52325c2005-08-22 22:54:29 +00001526 if (mode == FIND_COMPOUND)
1527 {
1528 int capflags;
1529
1530 /* Need to check the caps type of the appended compound
1531 * word. */
1532#ifdef FEAT_MBYTE
1533 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1534 mip->mi_compoff) != 0)
1535 {
1536 /* case folding may have changed the length */
1537 p = mip->mi_word;
1538 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1539 mb_ptr_adv(p);
1540 }
1541 else
1542#endif
1543 p = mip->mi_word + mip->mi_compoff;
1544 capflags = captype(p, mip->mi_word + wlen);
1545 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1546 && (flags & WF_FIXCAP) != 0))
1547 continue;
1548
1549 if (capflags != WF_ALLCAP)
1550 {
1551 /* When the character before the word is a word
1552 * character we do not accept a Onecap word. We do
1553 * accept a no-caps word, even when the dictionary
1554 * word specifies ONECAP. */
1555 mb_ptr_back(mip->mi_word, p);
1556 if (spell_iswordp_nmw(p)
1557 ? capflags == WF_ONECAP
1558 : (flags & WF_ONECAP) != 0
1559 && capflags != WF_ONECAP)
1560 continue;
1561 }
1562 }
1563
Bram Moolenaar5195e452005-08-19 20:32:47 +00001564 /* If the word ends the sequence of compound flags of the
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001565 * words must match with one of the COMPOUNDRULE items and
Bram Moolenaar5195e452005-08-19 20:32:47 +00001566 * the number of syllables must not be too large. */
1567 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1568 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1569 if (word_ends)
1570 {
1571 char_u fword[MAXWLEN];
1572
1573 if (slang->sl_compsylmax < MAXWLEN)
1574 {
1575 /* "fword" is only needed for checking syllables. */
1576 if (ptr == mip->mi_word)
1577 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1578 else
1579 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1580 }
1581 if (!can_compound(slang, fword, mip->mi_compflags))
1582 continue;
1583 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001584 }
1585
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001586 /* Check NEEDCOMPOUND: can't use word without compounding. */
1587 else if (flags & WF_NEEDCOMP)
1588 continue;
1589
Bram Moolenaar78622822005-08-23 21:00:13 +00001590 nobreak_result = SP_OK;
1591
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001592 if (!word_ends)
1593 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001594 int save_result = mip->mi_result;
1595 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001596 langp_T *save_lp = mip->mi_lp;
1597 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001598
1599 /* Check that a valid word follows. If there is one and we
1600 * are compounding, it will set "mi_result", thus we are
1601 * always finished here. For NOBREAK we only check that a
1602 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001603 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001604 if (slang->sl_nobreak)
1605 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001606
1607 /* Find following word in case-folded tree. */
1608 mip->mi_compoff = endlen[endidxcnt];
1609#ifdef FEAT_MBYTE
1610 if (has_mbyte && mode == FIND_KEEPWORD)
1611 {
1612 /* Compute byte length in case-folded word from "wlen":
1613 * byte length in keep-case word. Length may change when
1614 * folding case. This can be slow, take a shortcut when
1615 * the case-folded word is equal to the keep-case word. */
1616 p = mip->mi_fword;
1617 if (STRNCMP(ptr, p, wlen) != 0)
1618 {
1619 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1620 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001621 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001622 }
1623 }
1624#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001625 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001626 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001627 if (flags & WF_COMPROOT)
1628 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001629
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001630 /* For NOBREAK we need to try all NOBREAK languages, at least
1631 * to find the ".add" file(s). */
1632 for (lpi = 0; lpi < mip->mi_buf->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001633 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001634 if (slang->sl_nobreak)
1635 {
1636 mip->mi_lp = LANGP_ENTRY(mip->mi_buf->b_langp, lpi);
1637 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1638 || !mip->mi_lp->lp_slang->sl_nobreak)
1639 continue;
1640 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001641
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001642 find_word(mip, FIND_COMPOUND);
1643
1644 /* When NOBREAK any word that matches is OK. Otherwise we
1645 * need to find the longest match, thus try with keep-case
1646 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001647 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1648 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001649 /* Find following word in keep-case tree. */
1650 mip->mi_compoff = wlen;
1651 find_word(mip, FIND_KEEPCOMPOUND);
1652
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001653#if 0 /* Disabled, a prefix must not appear halfway a compound word,
1654 unless the COMPOUNDPERMITFLAG is used and then it can't be a
1655 postponed prefix. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001656 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1657 {
1658 /* Check for following word with prefix. */
1659 mip->mi_compoff = c;
1660 find_prefix(mip, FIND_COMPOUND);
1661 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001662#endif
Bram Moolenaar78622822005-08-23 21:00:13 +00001663 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001664
1665 if (!slang->sl_nobreak)
1666 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001667 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001668 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001669 if (flags & WF_COMPROOT)
1670 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001671 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001672
Bram Moolenaar78622822005-08-23 21:00:13 +00001673 if (slang->sl_nobreak)
1674 {
1675 nobreak_result = mip->mi_result;
1676 mip->mi_result = save_result;
1677 mip->mi_end = save_end;
1678 }
1679 else
1680 {
1681 if (mip->mi_result == SP_OK)
1682 break;
1683 continue;
1684 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001685 }
1686
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001687 if (flags & WF_BANNED)
1688 res = SP_BANNED;
1689 else if (flags & WF_REGION)
1690 {
1691 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001692 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001693 res = SP_OK;
1694 else
1695 res = SP_LOCAL;
1696 }
1697 else if (flags & WF_RARE)
1698 res = SP_RARE;
1699 else
1700 res = SP_OK;
1701
Bram Moolenaar78622822005-08-23 21:00:13 +00001702 /* Always use the longest match and the best result. For NOBREAK
1703 * we separately keep the longest match without a following good
1704 * word as a fall-back. */
1705 if (nobreak_result == SP_BAD)
1706 {
1707 if (mip->mi_result2 > res)
1708 {
1709 mip->mi_result2 = res;
1710 mip->mi_end2 = mip->mi_word + wlen;
1711 }
1712 else if (mip->mi_result2 == res
1713 && mip->mi_end2 < mip->mi_word + wlen)
1714 mip->mi_end2 = mip->mi_word + wlen;
1715 }
1716 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001717 {
1718 mip->mi_result = res;
1719 mip->mi_end = mip->mi_word + wlen;
1720 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001721 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001722 mip->mi_end = mip->mi_word + wlen;
1723
Bram Moolenaar78622822005-08-23 21:00:13 +00001724 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001725 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001726 }
1727
Bram Moolenaar78622822005-08-23 21:00:13 +00001728 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001729 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001730 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001731}
1732
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001733/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001734 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1735 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001736 */
1737 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001738can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001739 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001740 char_u *word;
1741 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001742{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001743 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001744#ifdef FEAT_MBYTE
1745 char_u uflags[MAXWLEN * 2];
1746 int i;
1747#endif
1748 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001749
1750 if (slang->sl_compprog == NULL)
1751 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001752#ifdef FEAT_MBYTE
1753 if (enc_utf8)
1754 {
1755 /* Need to convert the single byte flags to utf8 characters. */
1756 p = uflags;
1757 for (i = 0; flags[i] != NUL; ++i)
1758 p += mb_char2bytes(flags[i], p);
1759 *p = NUL;
1760 p = uflags;
1761 }
1762 else
1763#endif
1764 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001765 regmatch.regprog = slang->sl_compprog;
1766 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001767 if (!vim_regexec(&regmatch, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001768 return FALSE;
1769
Bram Moolenaare52325c2005-08-22 22:54:29 +00001770 /* Count the number of syllables. This may be slow, do it last. If there
1771 * are too many syllables AND the number of compound words is above
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001772 * COMPOUNDWORDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001773 if (slang->sl_compsylmax < MAXWLEN
1774 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001775 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001776 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001777}
1778
1779/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001780 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1781 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001782 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001783 */
1784 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001785valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001786 int totprefcnt; /* nr of prefix IDs */
1787 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001788 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001789 char_u *word;
1790 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001791 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001792{
1793 int prefcnt;
1794 int pidx;
1795 regprog_T *rp;
1796 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001797 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001798
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001799 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001800 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1801 {
1802 pidx = slang->sl_pidxs[arridx + prefcnt];
1803
1804 /* Check the prefix ID. */
1805 if (prefid != (pidx & 0xff))
1806 continue;
1807
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001808 /* Check if the prefix doesn't combine and the word already has a
1809 * suffix. */
1810 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1811 continue;
1812
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001813 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001814 * stored in the two bytes above the prefix ID byte. */
1815 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001816 if (rp != NULL)
1817 {
1818 regmatch.regprog = rp;
1819 regmatch.rm_ic = FALSE;
1820 if (!vim_regexec(&regmatch, word, 0))
1821 continue;
1822 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001823 else if (cond_req)
1824 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001825
Bram Moolenaar53805d12005-08-01 07:08:33 +00001826 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001827 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001828 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001829 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001830}
1831
1832/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001833 * Check if the word at "mip->mi_word" has a matching prefix.
1834 * If it does, then check the following word.
1835 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001836 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1837 * prefix in a compound word.
1838 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001839 * For a match mip->mi_result is updated.
1840 */
1841 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001842find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001843 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001844 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001845{
1846 idx_T arridx = 0;
1847 int len;
1848 int wlen = 0;
1849 int flen;
1850 int c;
1851 char_u *ptr;
1852 idx_T lo, hi, m;
1853 slang_T *slang = mip->mi_lp->lp_slang;
1854 char_u *byts;
1855 idx_T *idxs;
1856
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001857 byts = slang->sl_pbyts;
1858 if (byts == NULL)
1859 return; /* array is empty */
1860
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001861 /* We use the case-folded word here, since prefixes are always
1862 * case-folded. */
1863 ptr = mip->mi_fword;
1864 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001865 if (mode == FIND_COMPOUND)
1866 {
1867 /* Skip over the previously found word(s). */
1868 ptr += mip->mi_compoff;
1869 flen -= mip->mi_compoff;
1870 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001871 idxs = slang->sl_pidxs;
1872
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001873 /*
1874 * Repeat advancing in the tree until:
1875 * - there is a byte that doesn't match,
1876 * - we reach the end of the tree,
1877 * - or we reach the end of the line.
1878 */
1879 for (;;)
1880 {
1881 if (flen == 0 && *mip->mi_fend != NUL)
1882 flen = fold_more(mip);
1883
1884 len = byts[arridx++];
1885
1886 /* If the first possible byte is a zero the prefix could end here.
1887 * Check if the following word matches and supports the prefix. */
1888 if (byts[arridx] == 0)
1889 {
1890 /* There can be several prefixes with different conditions. We
1891 * try them all, since we don't know which one will give the
1892 * longest match. The word is the same each time, pass the list
1893 * of possible prefixes to find_word(). */
1894 mip->mi_prefarridx = arridx;
1895 mip->mi_prefcnt = len;
1896 while (len > 0 && byts[arridx] == 0)
1897 {
1898 ++arridx;
1899 --len;
1900 }
1901 mip->mi_prefcnt -= len;
1902
1903 /* Find the word that comes after the prefix. */
1904 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001905 if (mode == FIND_COMPOUND)
1906 /* Skip over the previously found word(s). */
1907 mip->mi_prefixlen += mip->mi_compoff;
1908
Bram Moolenaar53805d12005-08-01 07:08:33 +00001909#ifdef FEAT_MBYTE
1910 if (has_mbyte)
1911 {
1912 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001913 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1914 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001915 }
1916 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001917 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001918#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001919 find_word(mip, FIND_PREFIX);
1920
1921
1922 if (len == 0)
1923 break; /* no children, word must end here */
1924 }
1925
1926 /* Stop looking at end of the line. */
1927 if (ptr[wlen] == NUL)
1928 break;
1929
1930 /* Perform a binary search in the list of accepted bytes. */
1931 c = ptr[wlen];
1932 lo = arridx;
1933 hi = arridx + len - 1;
1934 while (lo < hi)
1935 {
1936 m = (lo + hi) / 2;
1937 if (byts[m] > c)
1938 hi = m - 1;
1939 else if (byts[m] < c)
1940 lo = m + 1;
1941 else
1942 {
1943 lo = hi = m;
1944 break;
1945 }
1946 }
1947
1948 /* Stop if there is no matching byte. */
1949 if (hi < lo || byts[lo] != c)
1950 break;
1951
1952 /* Continue at the child (if there is one). */
1953 arridx = idxs[lo];
1954 ++wlen;
1955 --flen;
1956 }
1957}
1958
1959/*
1960 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001961 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001962 * Return the length of the folded chars in bytes.
1963 */
1964 static int
1965fold_more(mip)
1966 matchinf_T *mip;
1967{
1968 int flen;
1969 char_u *p;
1970
1971 p = mip->mi_fend;
1972 do
1973 {
1974 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001975 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001976
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001977 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001978 if (*mip->mi_fend != NUL)
1979 mb_ptr_adv(mip->mi_fend);
1980
1981 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1982 mip->mi_fword + mip->mi_fwordlen,
1983 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001984 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001985 mip->mi_fwordlen += flen;
1986 return flen;
1987}
1988
1989/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001990 * Check case flags for a word. Return TRUE if the word has the requested
1991 * case.
1992 */
1993 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001994spell_valid_case(wordflags, treeflags)
1995 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001996 int treeflags; /* flags for the word in the spell tree */
1997{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001998 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001999 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002000 && ((treeflags & WF_ONECAP) == 0
2001 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002002}
2003
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002004/*
2005 * Return TRUE if spell checking is not enabled.
2006 */
2007 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00002008no_spell_checking(wp)
2009 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002010{
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002011 if (!wp->w_p_spell || *wp->w_buffer->b_p_spl == NUL
2012 || wp->w_buffer->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002013 {
2014 EMSG(_("E756: Spell checking is not enabled"));
2015 return TRUE;
2016 }
2017 return FALSE;
2018}
Bram Moolenaar51485f02005-06-04 21:55:20 +00002019
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002020/*
2021 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002022 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
2023 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002024 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
2025 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00002026 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002027 */
2028 int
Bram Moolenaar95529562005-08-25 21:21:38 +00002029spell_move_to(wp, dir, allwords, curline, attrp)
2030 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002031 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002032 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002033 int curline;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002034 hlf_T *attrp; /* return: attributes of bad word or NULL
2035 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002036{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002037 linenr_T lnum;
2038 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002039 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002040 char_u *line;
2041 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002042 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002043 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002044 int len;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002045# ifdef FEAT_SYN_HL
Bram Moolenaar95529562005-08-25 21:21:38 +00002046 int has_syntax = syntax_present(wp->w_buffer);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002047# endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00002048 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002049 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002050 char_u *buf = NULL;
2051 int buflen = 0;
2052 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002053 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002054 int found_one = FALSE;
2055 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002056
Bram Moolenaar95529562005-08-25 21:21:38 +00002057 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00002058 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002059
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002060 /*
2061 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00002062 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002063 *
2064 * When searching backwards, we continue in the line to find the last
2065 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00002066 *
2067 * We concatenate the start of the next line, so that wrapped words work
2068 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
2069 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002070 */
Bram Moolenaar95529562005-08-25 21:21:38 +00002071 lnum = wp->w_cursor.lnum;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002072 clearpos(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002073
2074 while (!got_int)
2075 {
Bram Moolenaar95529562005-08-25 21:21:38 +00002076 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002077
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002078 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002079 if (buflen < len + MAXWLEN + 2)
2080 {
2081 vim_free(buf);
2082 buflen = len + MAXWLEN + 2;
2083 buf = alloc(buflen);
2084 if (buf == NULL)
2085 break;
2086 }
2087
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002088 /* In first line check first word for Capital. */
2089 if (lnum == 1)
2090 capcol = 0;
2091
2092 /* For checking first word with a capital skip white space. */
2093 if (capcol == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002094 capcol = (int)(skipwhite(line) - line);
2095 else if (curline && wp == curwin)
2096 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002097 /* For spellbadword(): check if first word needs a capital. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002098 col = (int)(skipwhite(line) - line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002099 if (check_need_cap(lnum, col))
2100 capcol = col;
2101
2102 /* Need to get the line again, may have looked at the previous
2103 * one. */
2104 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2105 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002106
Bram Moolenaar0c405862005-06-22 22:26:26 +00002107 /* Copy the line into "buf" and append the start of the next line if
2108 * possible. */
2109 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00002110 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00002111 spell_cat_line(buf + STRLEN(buf),
2112 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002113
2114 p = buf + skip;
2115 endp = buf + len;
2116 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002117 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002118 /* When searching backward don't search after the cursor. Unless
2119 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002120 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00002121 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002122 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00002123 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002124 break;
2125
2126 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002127 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002128 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002129
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002130 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002131 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002132 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002133 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002134 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002135 /* When searching forward only accept a bad word after
2136 * the cursor. */
2137 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002138 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00002139 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002140 && (wrapped
2141 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00002142 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002143 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002144 {
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002145# ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00002146 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002147 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002148 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002149 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00002150 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00002151 if (!can_spell)
2152 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002153 }
2154 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002155#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002156 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002157
Bram Moolenaar51485f02005-06-04 21:55:20 +00002158 if (can_spell)
2159 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00002160 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002161 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002162 found_pos.col = (int)(p - buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002163#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00002164 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002165#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002166 if (dir == FORWARD)
2167 {
2168 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00002169 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002170 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002171 if (attrp != NULL)
2172 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002173 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002174 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002175 else if (curline)
2176 /* Insert mode completion: put cursor after
2177 * the bad word. */
2178 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002179 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002180 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002181 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00002182 else
2183 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002184 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002185 }
2186
Bram Moolenaar51485f02005-06-04 21:55:20 +00002187 /* advance to character after the word */
2188 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002189 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002190 }
2191
Bram Moolenaar5195e452005-08-19 20:32:47 +00002192 if (dir == BACKWARD && found_pos.lnum != 0)
2193 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002194 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00002195 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002196 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002197 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002198 }
2199
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002200 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002201 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002202
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002203 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002204 if (dir == BACKWARD)
2205 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002206 /* If we are back at the starting line and searched it again there
2207 * is no match, give up. */
2208 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002209 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002210
2211 if (lnum > 1)
2212 --lnum;
2213 else if (!p_ws)
2214 break; /* at first line and 'nowrapscan' */
2215 else
2216 {
2217 /* Wrap around to the end of the buffer. May search the
2218 * starting line again and accept the last match. */
2219 lnum = wp->w_buffer->b_ml.ml_line_count;
2220 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002221 if (!shortmess(SHM_SEARCH))
2222 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002223 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002224 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002225 }
2226 else
2227 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002228 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2229 ++lnum;
2230 else if (!p_ws)
2231 break; /* at first line and 'nowrapscan' */
2232 else
2233 {
2234 /* Wrap around to the start of the buffer. May search the
2235 * starting line again and accept the first match. */
2236 lnum = 1;
2237 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002238 if (!shortmess(SHM_SEARCH))
2239 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002240 }
2241
2242 /* If we are back at the starting line and there is no match then
2243 * give up. */
2244 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002245 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002246
2247 /* Skip the characters at the start of the next line that were
2248 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002249 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002250 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002251 else
2252 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002253
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002254 /* Capcol skips over the inserted space. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002255 --capcol;
2256
2257 /* But after empty line check first word in next line */
2258 if (*skipwhite(line) == NUL)
2259 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002260 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002261
2262 line_breakcheck();
2263 }
2264
Bram Moolenaar0c405862005-06-22 22:26:26 +00002265 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002266 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002267}
2268
2269/*
2270 * For spell checking: concatenate the start of the following line "line" into
2271 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00002272 * Keep the blanks at the start of the next line, this is used in win_line()
2273 * to skip those bytes if the word was OK.
Bram Moolenaar0c405862005-06-22 22:26:26 +00002274 */
2275 void
2276spell_cat_line(buf, line, maxlen)
2277 char_u *buf;
2278 char_u *line;
2279 int maxlen;
2280{
2281 char_u *p;
2282 int n;
2283
2284 p = skipwhite(line);
2285 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2286 p = skipwhite(p + 1);
2287
2288 if (*p != NUL)
2289 {
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00002290 /* Only worth concatenating if there is something else than spaces to
2291 * concatenate. */
2292 n = (int)(p - line) + 1;
2293 if (n < maxlen - 1)
2294 {
2295 vim_memset(buf, ' ', n);
2296 vim_strncpy(buf + n, p, maxlen - 1 - n);
2297 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00002298 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002299}
2300
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002301/*
2302 * Structure used for the cookie argument of do_in_runtimepath().
2303 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002304typedef struct spelload_S
2305{
2306 char_u sl_lang[MAXWLEN + 1]; /* language name */
2307 slang_T *sl_slang; /* resulting slang_T struct */
2308 int sl_nobreak; /* NOBREAK language found */
2309} spelload_T;
2310
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002311/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002312 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002313 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002314 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002315 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002316spell_load_lang(lang)
2317 char_u *lang;
2318{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002319 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002320 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002321 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002322#ifdef FEAT_AUTOCMD
2323 int round;
2324#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002325
Bram Moolenaarb765d632005-06-07 21:00:02 +00002326 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002327 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002328 STRCPY(sl.sl_lang, lang);
2329 sl.sl_slang = NULL;
2330 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002331
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002332#ifdef FEAT_AUTOCMD
2333 /* We may retry when no spell file is found for the language, an
2334 * autocommand may load it then. */
2335 for (round = 1; round <= 2; ++round)
2336#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002337 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002338 /*
2339 * Find the first spell file for "lang" in 'runtimepath' and load it.
2340 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002341 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002342 "spell/%s.%s.spl", lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002343 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002344
2345 if (r == FAIL && *sl.sl_lang != NUL)
2346 {
2347 /* Try loading the ASCII version. */
2348 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2349 "spell/%s.ascii.spl", lang);
2350 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
2351
2352#ifdef FEAT_AUTOCMD
2353 if (r == FAIL && *sl.sl_lang != NUL && round == 1
2354 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
2355 curbuf->b_fname, FALSE, curbuf))
2356 continue;
2357 break;
2358#endif
2359 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002360#ifdef FEAT_AUTOCMD
2361 break;
2362#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002363 }
2364
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002365 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002366 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002367 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2368 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002369 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002370 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002371 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002372 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002373 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002374 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002375 }
2376}
2377
2378/*
2379 * Return the encoding used for spell checking: Use 'encoding', except that we
2380 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2381 */
2382 static char_u *
2383spell_enc()
2384{
2385
2386#ifdef FEAT_MBYTE
2387 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2388 return p_enc;
2389#endif
2390 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002391}
2392
2393/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002394 * Get the name of the .spl file for the internal wordlist into
2395 * "fname[MAXPATHL]".
2396 */
2397 static void
2398int_wordlist_spl(fname)
2399 char_u *fname;
2400{
2401 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2402 int_wordlist, spell_enc());
2403}
2404
2405/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002406 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002407 * Caller must fill "sl_next".
2408 */
2409 static slang_T *
2410slang_alloc(lang)
2411 char_u *lang;
2412{
2413 slang_T *lp;
2414
Bram Moolenaar51485f02005-06-04 21:55:20 +00002415 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002416 if (lp != NULL)
2417 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002418 if (lang != NULL)
2419 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002420 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002421 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002422 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002423 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002424 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002425 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002426
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002427 return lp;
2428}
2429
2430/*
2431 * Free the contents of an slang_T and the structure itself.
2432 */
2433 static void
2434slang_free(lp)
2435 slang_T *lp;
2436{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002437 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002438 vim_free(lp->sl_fname);
2439 slang_clear(lp);
2440 vim_free(lp);
2441}
2442
2443/*
2444 * Clear an slang_T so that the file can be reloaded.
2445 */
2446 static void
2447slang_clear(lp)
2448 slang_T *lp;
2449{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002450 garray_T *gap;
2451 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002452 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002453 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002454 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002455
Bram Moolenaar51485f02005-06-04 21:55:20 +00002456 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002457 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002458 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002459 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002460 vim_free(lp->sl_pbyts);
2461 lp->sl_pbyts = NULL;
2462
Bram Moolenaar51485f02005-06-04 21:55:20 +00002463 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002464 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002465 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002466 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002467 vim_free(lp->sl_pidxs);
2468 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002469
Bram Moolenaar4770d092006-01-12 23:22:24 +00002470 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002471 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002472 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2473 while (gap->ga_len > 0)
2474 {
2475 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2476 vim_free(ftp->ft_from);
2477 vim_free(ftp->ft_to);
2478 }
2479 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002480 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002481
2482 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002483 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002484 {
2485 /* "ga_len" is set to 1 without adding an item for latin1 */
2486 if (gap->ga_data != NULL)
2487 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2488 for (i = 0; i < gap->ga_len; ++i)
2489 vim_free(((int **)gap->ga_data)[i]);
2490 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002491 else
2492 /* SAL items: free salitem_T items */
2493 while (gap->ga_len > 0)
2494 {
2495 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2496 vim_free(smp->sm_lead);
2497 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2498 vim_free(smp->sm_to);
2499#ifdef FEAT_MBYTE
2500 vim_free(smp->sm_lead_w);
2501 vim_free(smp->sm_oneof_w);
2502 vim_free(smp->sm_to_w);
2503#endif
2504 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002505 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002506
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002507 for (i = 0; i < lp->sl_prefixcnt; ++i)
2508 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002509 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002510 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002511 lp->sl_prefprog = NULL;
2512
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002513 vim_free(lp->sl_info);
2514 lp->sl_info = NULL;
2515
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002516 vim_free(lp->sl_midword);
2517 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002518
Bram Moolenaar5195e452005-08-19 20:32:47 +00002519 vim_free(lp->sl_compprog);
2520 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002521 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002522 lp->sl_compprog = NULL;
2523 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002524 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002525
2526 vim_free(lp->sl_syllable);
2527 lp->sl_syllable = NULL;
2528 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002529
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002530 ga_clear_strings(&lp->sl_comppat);
2531
Bram Moolenaar4770d092006-01-12 23:22:24 +00002532 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2533 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002534
Bram Moolenaar4770d092006-01-12 23:22:24 +00002535#ifdef FEAT_MBYTE
2536 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002537#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002538
Bram Moolenaar4770d092006-01-12 23:22:24 +00002539 /* Clear info from .sug file. */
2540 slang_clear_sug(lp);
2541
Bram Moolenaar5195e452005-08-19 20:32:47 +00002542 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002543 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002544 lp->sl_compsylmax = MAXWLEN;
2545 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002546}
2547
2548/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002549 * Clear the info from the .sug file in "lp".
2550 */
2551 static void
2552slang_clear_sug(lp)
2553 slang_T *lp;
2554{
2555 vim_free(lp->sl_sbyts);
2556 lp->sl_sbyts = NULL;
2557 vim_free(lp->sl_sidxs);
2558 lp->sl_sidxs = NULL;
2559 close_spellbuf(lp->sl_sugbuf);
2560 lp->sl_sugbuf = NULL;
2561 lp->sl_sugloaded = FALSE;
2562 lp->sl_sugtime = 0;
2563}
2564
2565/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002566 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002567 * Invoked through do_in_runtimepath().
2568 */
2569 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002570spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002571 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002572 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002573{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002574 spelload_T *slp = (spelload_T *)cookie;
2575 slang_T *slang;
2576
2577 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2578 if (slang != NULL)
2579 {
2580 /* When a previously loaded file has NOBREAK also use it for the
2581 * ".add" files. */
2582 if (slp->sl_nobreak && slang->sl_add)
2583 slang->sl_nobreak = TRUE;
2584 else if (slang->sl_nobreak)
2585 slp->sl_nobreak = TRUE;
2586
2587 slp->sl_slang = slang;
2588 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002589}
2590
2591/*
2592 * Load one spell file and store the info into a slang_T.
2593 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00002594 * This is invoked in three ways:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002595 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2596 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2597 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2598 * points to the existing slang_T.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002599 * - Just after writing a .spl file; it's read back to produce the .sug file.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002600 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
2601 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002602 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002603 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002604 static slang_T *
2605spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002606 char_u *fname;
2607 char_u *lang;
2608 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002609 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002610{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002611 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002612 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002613 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002614 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002615 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002616 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002617 char_u *save_sourcing_name = sourcing_name;
2618 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002619 slang_T *lp = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002620 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002621 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002622
Bram Moolenaarb765d632005-06-07 21:00:02 +00002623 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002624 if (fd == NULL)
2625 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002626 if (!silent)
2627 EMSG2(_(e_notopen), fname);
2628 else if (p_verbose > 2)
2629 {
2630 verbose_enter();
2631 smsg((char_u *)e_notopen, fname);
2632 verbose_leave();
2633 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002634 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002635 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002636 if (p_verbose > 2)
2637 {
2638 verbose_enter();
2639 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2640 verbose_leave();
2641 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002642
Bram Moolenaarb765d632005-06-07 21:00:02 +00002643 if (old_lp == NULL)
2644 {
2645 lp = slang_alloc(lang);
2646 if (lp == NULL)
2647 goto endFAIL;
2648
2649 /* Remember the file name, used to reload the file when it's updated. */
2650 lp->sl_fname = vim_strsave(fname);
2651 if (lp->sl_fname == NULL)
2652 goto endFAIL;
2653
2654 /* Check for .add.spl. */
2655 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2656 }
2657 else
2658 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002659
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002660 /* Set sourcing_name, so that error messages mention the file name. */
2661 sourcing_name = fname;
2662 sourcing_lnum = 0;
2663
Bram Moolenaar4770d092006-01-12 23:22:24 +00002664 /*
2665 * <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002666 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002667 for (i = 0; i < VIMSPELLMAGICL; ++i)
2668 buf[i] = getc(fd); /* <fileID> */
2669 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2670 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002671 EMSG(_("E757: This does not look like a spell file"));
2672 goto endFAIL;
2673 }
2674 c = getc(fd); /* <versionnr> */
2675 if (c < VIMSPELLVERSION)
2676 {
2677 EMSG(_("E771: Old spell file, needs to be updated"));
2678 goto endFAIL;
2679 }
2680 else if (c > VIMSPELLVERSION)
2681 {
2682 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002683 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002684 }
2685
Bram Moolenaar5195e452005-08-19 20:32:47 +00002686
2687 /*
2688 * <SECTIONS>: <section> ... <sectionend>
2689 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2690 */
2691 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002692 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002693 n = getc(fd); /* <sectionID> or <sectionend> */
2694 if (n == SN_END)
2695 break;
2696 c = getc(fd); /* <sectionflags> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002697 len = get4c(fd); /* <sectionlen> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002698 if (len < 0)
2699 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002700
Bram Moolenaar5195e452005-08-19 20:32:47 +00002701 res = 0;
2702 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002703 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002704 case SN_INFO:
2705 lp->sl_info = read_string(fd, len); /* <infotext> */
2706 if (lp->sl_info == NULL)
2707 goto endFAIL;
2708 break;
2709
Bram Moolenaar5195e452005-08-19 20:32:47 +00002710 case SN_REGION:
2711 res = read_region_section(fd, lp, len);
2712 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002713
Bram Moolenaar5195e452005-08-19 20:32:47 +00002714 case SN_CHARFLAGS:
2715 res = read_charflags_section(fd);
2716 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002717
Bram Moolenaar5195e452005-08-19 20:32:47 +00002718 case SN_MIDWORD:
2719 lp->sl_midword = read_string(fd, len); /* <midword> */
2720 if (lp->sl_midword == NULL)
2721 goto endFAIL;
2722 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002723
Bram Moolenaar5195e452005-08-19 20:32:47 +00002724 case SN_PREFCOND:
2725 res = read_prefcond_section(fd, lp);
2726 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002727
Bram Moolenaar5195e452005-08-19 20:32:47 +00002728 case SN_REP:
Bram Moolenaar4770d092006-01-12 23:22:24 +00002729 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
2730 break;
2731
2732 case SN_REPSAL:
2733 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002734 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002735
Bram Moolenaar5195e452005-08-19 20:32:47 +00002736 case SN_SAL:
2737 res = read_sal_section(fd, lp);
2738 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002739
Bram Moolenaar5195e452005-08-19 20:32:47 +00002740 case SN_SOFO:
2741 res = read_sofo_section(fd, lp);
2742 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002743
Bram Moolenaar5195e452005-08-19 20:32:47 +00002744 case SN_MAP:
2745 p = read_string(fd, len); /* <mapstr> */
2746 if (p == NULL)
2747 goto endFAIL;
2748 set_map_str(lp, p);
2749 vim_free(p);
2750 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002751
Bram Moolenaar4770d092006-01-12 23:22:24 +00002752 case SN_WORDS:
2753 res = read_words_section(fd, lp, len);
2754 break;
2755
2756 case SN_SUGFILE:
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002757 lp->sl_sugtime = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002758 break;
2759
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002760 case SN_NOSPLITSUGS:
2761 lp->sl_nosplitsugs = TRUE; /* <timestamp> */
2762 break;
2763
Bram Moolenaar5195e452005-08-19 20:32:47 +00002764 case SN_COMPOUND:
2765 res = read_compound(fd, lp, len);
2766 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002767
Bram Moolenaar78622822005-08-23 21:00:13 +00002768 case SN_NOBREAK:
2769 lp->sl_nobreak = TRUE;
2770 break;
2771
Bram Moolenaar5195e452005-08-19 20:32:47 +00002772 case SN_SYLLABLE:
2773 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2774 if (lp->sl_syllable == NULL)
2775 goto endFAIL;
2776 if (init_syl_tab(lp) == FAIL)
2777 goto endFAIL;
2778 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002779
Bram Moolenaar5195e452005-08-19 20:32:47 +00002780 default:
2781 /* Unsupported section. When it's required give an error
2782 * message. When it's not required skip the contents. */
2783 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002784 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002785 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002786 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002787 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002788 while (--len >= 0)
2789 if (getc(fd) < 0)
2790 goto truncerr;
2791 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002792 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002793someerror:
Bram Moolenaar5195e452005-08-19 20:32:47 +00002794 if (res == SP_FORMERROR)
2795 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002796 EMSG(_(e_format));
2797 goto endFAIL;
2798 }
2799 if (res == SP_TRUNCERROR)
2800 {
2801truncerr:
2802 EMSG(_(e_spell_trunc));
2803 goto endFAIL;
2804 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002805 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002806 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002807 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002808
Bram Moolenaar4770d092006-01-12 23:22:24 +00002809 /* <LWORDTREE> */
2810 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
2811 if (res != 0)
2812 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002813
Bram Moolenaar4770d092006-01-12 23:22:24 +00002814 /* <KWORDTREE> */
2815 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
2816 if (res != 0)
2817 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002818
Bram Moolenaar4770d092006-01-12 23:22:24 +00002819 /* <PREFIXTREE> */
2820 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
2821 lp->sl_prefixcnt);
2822 if (res != 0)
2823 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002824
Bram Moolenaarb765d632005-06-07 21:00:02 +00002825 /* For a new file link it in the list of spell files. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002826 if (old_lp == NULL && lang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002827 {
2828 lp->sl_next = first_lang;
2829 first_lang = lp;
2830 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002831
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002832 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002833
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002834endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002835 if (lang != NULL)
2836 /* truncating the name signals the error to spell_load_lang() */
2837 *lang = NUL;
2838 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002839 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002840 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002841
2842endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002843 if (fd != NULL)
2844 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002845 sourcing_name = save_sourcing_name;
2846 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002847
2848 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002849}
2850
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002851/*
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002852 * Read 2 bytes from "fd" and turn them into an int, MSB first.
2853 */
2854 static int
2855get2c(fd)
2856 FILE *fd;
2857{
2858 long n;
2859
2860 n = getc(fd);
2861 n = (n << 8) + getc(fd);
2862 return n;
2863}
2864
2865/*
2866 * Read 3 bytes from "fd" and turn them into an int, MSB first.
2867 */
2868 static int
2869get3c(fd)
2870 FILE *fd;
2871{
2872 long n;
2873
2874 n = getc(fd);
2875 n = (n << 8) + getc(fd);
2876 n = (n << 8) + getc(fd);
2877 return n;
2878}
2879
2880/*
2881 * Read 4 bytes from "fd" and turn them into an int, MSB first.
2882 */
2883 static int
2884get4c(fd)
2885 FILE *fd;
2886{
2887 long n;
2888
2889 n = getc(fd);
2890 n = (n << 8) + getc(fd);
2891 n = (n << 8) + getc(fd);
2892 n = (n << 8) + getc(fd);
2893 return n;
2894}
2895
2896/*
2897 * Read 8 bytes from "fd" and turn them into a time_t, MSB first.
2898 */
2899 static time_t
2900get8c(fd)
2901 FILE *fd;
2902{
2903 time_t n = 0;
2904 int i;
2905
2906 for (i = 0; i < 8; ++i)
2907 n = (n << 8) + getc(fd);
2908 return n;
2909}
2910
2911/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002912 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002913 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002914 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002915 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2916 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002917 */
2918 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002919read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002920 FILE *fd;
2921 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002922 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002923{
2924 int cnt = 0;
2925 int i;
2926 char_u *str;
2927
2928 /* read the length bytes, MSB first */
2929 for (i = 0; i < cnt_bytes; ++i)
2930 cnt = (cnt << 8) + getc(fd);
2931 if (cnt < 0)
2932 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002933 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002934 return NULL;
2935 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002936 *cntp = cnt;
2937 if (cnt == 0)
2938 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002939
Bram Moolenaar5195e452005-08-19 20:32:47 +00002940 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002941 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002942 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002943 return str;
2944}
2945
Bram Moolenaar7887d882005-07-01 22:33:52 +00002946/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002947 * Read a string of length "cnt" from "fd" into allocated memory.
2948 * Returns NULL when out of memory.
2949 */
2950 static char_u *
2951read_string(fd, cnt)
2952 FILE *fd;
2953 int cnt;
2954{
2955 char_u *str;
2956 int i;
2957
2958 /* allocate memory */
2959 str = alloc((unsigned)cnt + 1);
2960 if (str != NULL)
2961 {
2962 /* Read the string. Doesn't check for truncated file. */
2963 for (i = 0; i < cnt; ++i)
2964 str[i] = getc(fd);
2965 str[i] = NUL;
2966 }
2967 return str;
2968}
2969
2970/*
2971 * Read SN_REGION: <regionname> ...
2972 * Return SP_*ERROR flags.
2973 */
2974 static int
2975read_region_section(fd, lp, len)
2976 FILE *fd;
2977 slang_T *lp;
2978 int len;
2979{
2980 int i;
2981
2982 if (len > 16)
2983 return SP_FORMERROR;
2984 for (i = 0; i < len; ++i)
2985 lp->sl_regions[i] = getc(fd); /* <regionname> */
2986 lp->sl_regions[len] = NUL;
2987 return 0;
2988}
2989
2990/*
2991 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2992 * <folcharslen> <folchars>
2993 * Return SP_*ERROR flags.
2994 */
2995 static int
2996read_charflags_section(fd)
2997 FILE *fd;
2998{
2999 char_u *flags;
3000 char_u *fol;
3001 int flagslen, follen;
3002
3003 /* <charflagslen> <charflags> */
3004 flags = read_cnt_string(fd, 1, &flagslen);
3005 if (flagslen < 0)
3006 return flagslen;
3007
3008 /* <folcharslen> <folchars> */
3009 fol = read_cnt_string(fd, 2, &follen);
3010 if (follen < 0)
3011 {
3012 vim_free(flags);
3013 return follen;
3014 }
3015
3016 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
3017 if (flags != NULL && fol != NULL)
3018 set_spell_charflags(flags, flagslen, fol);
3019
3020 vim_free(flags);
3021 vim_free(fol);
3022
3023 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
3024 if ((flags == NULL) != (fol == NULL))
3025 return SP_FORMERROR;
3026 return 0;
3027}
3028
3029/*
3030 * Read SN_PREFCOND section.
3031 * Return SP_*ERROR flags.
3032 */
3033 static int
3034read_prefcond_section(fd, lp)
3035 FILE *fd;
3036 slang_T *lp;
3037{
3038 int cnt;
3039 int i;
3040 int n;
3041 char_u *p;
3042 char_u buf[MAXWLEN + 1];
3043
3044 /* <prefcondcnt> <prefcond> ... */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003045 cnt = get2c(fd); /* <prefcondcnt> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003046 if (cnt <= 0)
3047 return SP_FORMERROR;
3048
3049 lp->sl_prefprog = (regprog_T **)alloc_clear(
3050 (unsigned)sizeof(regprog_T *) * cnt);
3051 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003052 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003053 lp->sl_prefixcnt = cnt;
3054
3055 for (i = 0; i < cnt; ++i)
3056 {
3057 /* <prefcond> : <condlen> <condstr> */
3058 n = getc(fd); /* <condlen> */
3059 if (n < 0 || n >= MAXWLEN)
3060 return SP_FORMERROR;
3061
3062 /* When <condlen> is zero we have an empty condition. Otherwise
3063 * compile the regexp program used to check for the condition. */
3064 if (n > 0)
3065 {
3066 buf[0] = '^'; /* always match at one position only */
3067 p = buf + 1;
3068 while (n-- > 0)
3069 *p++ = getc(fd); /* <condstr> */
3070 *p = NUL;
3071 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
3072 }
3073 }
3074 return 0;
3075}
3076
3077/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003078 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
Bram Moolenaar5195e452005-08-19 20:32:47 +00003079 * Return SP_*ERROR flags.
3080 */
3081 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00003082read_rep_section(fd, gap, first)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003083 FILE *fd;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003084 garray_T *gap;
3085 short *first;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003086{
3087 int cnt;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003088 fromto_T *ftp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003089 int i;
3090
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003091 cnt = get2c(fd); /* <repcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003092 if (cnt < 0)
3093 return SP_TRUNCERROR;
3094
Bram Moolenaar5195e452005-08-19 20:32:47 +00003095 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003096 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003097
3098 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3099 for (; gap->ga_len < cnt; ++gap->ga_len)
3100 {
3101 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
3102 ftp->ft_from = read_cnt_string(fd, 1, &i);
3103 if (i < 0)
3104 return i;
3105 if (i == 0)
3106 return SP_FORMERROR;
3107 ftp->ft_to = read_cnt_string(fd, 1, &i);
3108 if (i <= 0)
3109 {
3110 vim_free(ftp->ft_from);
3111 if (i < 0)
3112 return i;
3113 return SP_FORMERROR;
3114 }
3115 }
3116
3117 /* Fill the first-index table. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003118 for (i = 0; i < 256; ++i)
3119 first[i] = -1;
3120 for (i = 0; i < gap->ga_len; ++i)
3121 {
3122 ftp = &((fromto_T *)gap->ga_data)[i];
3123 if (first[*ftp->ft_from] == -1)
3124 first[*ftp->ft_from] = i;
3125 }
3126 return 0;
3127}
3128
3129/*
3130 * Read SN_SAL section: <salflags> <salcount> <sal> ...
3131 * Return SP_*ERROR flags.
3132 */
3133 static int
3134read_sal_section(fd, slang)
3135 FILE *fd;
3136 slang_T *slang;
3137{
3138 int i;
3139 int cnt;
3140 garray_T *gap;
3141 salitem_T *smp;
3142 int ccnt;
3143 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003144 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003145
3146 slang->sl_sofo = FALSE;
3147
3148 i = getc(fd); /* <salflags> */
3149 if (i & SAL_F0LLOWUP)
3150 slang->sl_followup = TRUE;
3151 if (i & SAL_COLLAPSE)
3152 slang->sl_collapse = TRUE;
3153 if (i & SAL_REM_ACCENTS)
3154 slang->sl_rem_accents = TRUE;
3155
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003156 cnt = get2c(fd); /* <salcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003157 if (cnt < 0)
3158 return SP_TRUNCERROR;
3159
3160 gap = &slang->sl_sal;
3161 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003162 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003163 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003164
3165 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3166 for (; gap->ga_len < cnt; ++gap->ga_len)
3167 {
3168 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3169 ccnt = getc(fd); /* <salfromlen> */
3170 if (ccnt < 0)
3171 return SP_TRUNCERROR;
3172 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003173 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003174 smp->sm_lead = p;
3175
3176 /* Read up to the first special char into sm_lead. */
3177 for (i = 0; i < ccnt; ++i)
3178 {
3179 c = getc(fd); /* <salfrom> */
3180 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
3181 break;
3182 *p++ = c;
3183 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003184 smp->sm_leadlen = (int)(p - smp->sm_lead);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003185 *p++ = NUL;
3186
3187 /* Put (abc) chars in sm_oneof, if any. */
3188 if (c == '(')
3189 {
3190 smp->sm_oneof = p;
3191 for (++i; i < ccnt; ++i)
3192 {
3193 c = getc(fd); /* <salfrom> */
3194 if (c == ')')
3195 break;
3196 *p++ = c;
3197 }
3198 *p++ = NUL;
3199 if (++i < ccnt)
3200 c = getc(fd);
3201 }
3202 else
3203 smp->sm_oneof = NULL;
3204
3205 /* Any following chars go in sm_rules. */
3206 smp->sm_rules = p;
3207 if (i < ccnt)
3208 /* store the char we got while checking for end of sm_lead */
3209 *p++ = c;
3210 for (++i; i < ccnt; ++i)
3211 *p++ = getc(fd); /* <salfrom> */
3212 *p++ = NUL;
3213
3214 /* <saltolen> <salto> */
3215 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
3216 if (ccnt < 0)
3217 {
3218 vim_free(smp->sm_lead);
3219 return ccnt;
3220 }
3221
3222#ifdef FEAT_MBYTE
3223 if (has_mbyte)
3224 {
3225 /* convert the multi-byte strings to wide char strings */
3226 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3227 smp->sm_leadlen = mb_charlen(smp->sm_lead);
3228 if (smp->sm_oneof == NULL)
3229 smp->sm_oneof_w = NULL;
3230 else
3231 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
3232 if (smp->sm_to == NULL)
3233 smp->sm_to_w = NULL;
3234 else
3235 smp->sm_to_w = mb_str2wide(smp->sm_to);
3236 if (smp->sm_lead_w == NULL
3237 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
3238 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
3239 {
3240 vim_free(smp->sm_lead);
3241 vim_free(smp->sm_to);
3242 vim_free(smp->sm_lead_w);
3243 vim_free(smp->sm_oneof_w);
3244 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003245 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003246 }
3247 }
3248#endif
3249 }
3250
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003251 if (gap->ga_len > 0)
3252 {
3253 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
3254 * that we need to check the index every time. */
3255 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3256 if ((p = alloc(1)) == NULL)
3257 return SP_OTHERERROR;
3258 p[0] = NUL;
3259 smp->sm_lead = p;
3260 smp->sm_leadlen = 0;
3261 smp->sm_oneof = NULL;
3262 smp->sm_rules = p;
3263 smp->sm_to = NULL;
3264#ifdef FEAT_MBYTE
3265 if (has_mbyte)
3266 {
3267 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3268 smp->sm_leadlen = 0;
3269 smp->sm_oneof_w = NULL;
3270 smp->sm_to_w = NULL;
3271 }
3272#endif
3273 ++gap->ga_len;
3274 }
3275
Bram Moolenaar5195e452005-08-19 20:32:47 +00003276 /* Fill the first-index table. */
3277 set_sal_first(slang);
3278
3279 return 0;
3280}
3281
3282/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003283 * Read SN_WORDS: <word> ...
3284 * Return SP_*ERROR flags.
3285 */
3286 static int
3287read_words_section(fd, lp, len)
3288 FILE *fd;
3289 slang_T *lp;
3290 int len;
3291{
3292 int done = 0;
3293 int i;
3294 char_u word[MAXWLEN];
3295
3296 while (done < len)
3297 {
3298 /* Read one word at a time. */
3299 for (i = 0; ; ++i)
3300 {
3301 word[i] = getc(fd);
3302 if (word[i] == NUL)
3303 break;
3304 if (i == MAXWLEN - 1)
3305 return SP_FORMERROR;
3306 }
3307
3308 /* Init the count to 10. */
3309 count_common_word(lp, word, -1, 10);
3310 done += i + 1;
3311 }
3312 return 0;
3313}
3314
3315/*
3316 * Add a word to the hashtable of common words.
3317 * If it's already there then the counter is increased.
3318 */
3319 static void
3320count_common_word(lp, word, len, count)
3321 slang_T *lp;
3322 char_u *word;
3323 int len; /* word length, -1 for upto NUL */
3324 int count; /* 1 to count once, 10 to init */
3325{
3326 hash_T hash;
3327 hashitem_T *hi;
3328 wordcount_T *wc;
3329 char_u buf[MAXWLEN];
3330 char_u *p;
3331
3332 if (len == -1)
3333 p = word;
3334 else
3335 {
3336 vim_strncpy(buf, word, len);
3337 p = buf;
3338 }
3339
3340 hash = hash_hash(p);
3341 hi = hash_lookup(&lp->sl_wordcount, p, hash);
3342 if (HASHITEM_EMPTY(hi))
3343 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003344 wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003345 if (wc == NULL)
3346 return;
3347 STRCPY(wc->wc_word, p);
3348 wc->wc_count = count;
3349 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
3350 }
3351 else
3352 {
3353 wc = HI2WC(hi);
3354 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
3355 wc->wc_count = MAXWORDCOUNT;
3356 }
3357}
3358
3359/*
3360 * Adjust the score of common words.
3361 */
3362 static int
3363score_wordcount_adj(slang, score, word, split)
3364 slang_T *slang;
3365 int score;
3366 char_u *word;
3367 int split; /* word was split, less bonus */
3368{
3369 hashitem_T *hi;
3370 wordcount_T *wc;
3371 int bonus;
3372 int newscore;
3373
3374 hi = hash_find(&slang->sl_wordcount, word);
3375 if (!HASHITEM_EMPTY(hi))
3376 {
3377 wc = HI2WC(hi);
3378 if (wc->wc_count < SCORE_THRES2)
3379 bonus = SCORE_COMMON1;
3380 else if (wc->wc_count < SCORE_THRES3)
3381 bonus = SCORE_COMMON2;
3382 else
3383 bonus = SCORE_COMMON3;
3384 if (split)
3385 newscore = score - bonus / 2;
3386 else
3387 newscore = score - bonus;
3388 if (newscore < 0)
3389 return 0;
3390 return newscore;
3391 }
3392 return score;
3393}
3394
3395/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003396 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
3397 * Return SP_*ERROR flags.
3398 */
3399 static int
3400read_sofo_section(fd, slang)
3401 FILE *fd;
3402 slang_T *slang;
3403{
3404 int cnt;
3405 char_u *from, *to;
3406 int res;
3407
3408 slang->sl_sofo = TRUE;
3409
3410 /* <sofofromlen> <sofofrom> */
3411 from = read_cnt_string(fd, 2, &cnt);
3412 if (cnt < 0)
3413 return cnt;
3414
3415 /* <sofotolen> <sofoto> */
3416 to = read_cnt_string(fd, 2, &cnt);
3417 if (cnt < 0)
3418 {
3419 vim_free(from);
3420 return cnt;
3421 }
3422
3423 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
3424 if (from != NULL && to != NULL)
3425 res = set_sofo(slang, from, to);
3426 else if (from != NULL || to != NULL)
3427 res = SP_FORMERROR; /* only one of two strings is an error */
3428 else
3429 res = 0;
3430
3431 vim_free(from);
3432 vim_free(to);
3433 return res;
3434}
3435
3436/*
3437 * Read the compound section from the .spl file:
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003438 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +00003439 * Returns SP_*ERROR flags.
3440 */
3441 static int
3442read_compound(fd, slang, len)
3443 FILE *fd;
3444 slang_T *slang;
3445 int len;
3446{
3447 int todo = len;
3448 int c;
3449 int atstart;
3450 char_u *pat;
3451 char_u *pp;
3452 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003453 char_u *ap;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003454 int cnt;
3455 garray_T *gap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003456
3457 if (todo < 2)
3458 return SP_FORMERROR; /* need at least two bytes */
3459
3460 --todo;
3461 c = getc(fd); /* <compmax> */
3462 if (c < 2)
3463 c = MAXWLEN;
3464 slang->sl_compmax = c;
3465
3466 --todo;
3467 c = getc(fd); /* <compminlen> */
3468 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003469 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003470 slang->sl_compminlen = c;
3471
3472 --todo;
3473 c = getc(fd); /* <compsylmax> */
3474 if (c < 1)
3475 c = MAXWLEN;
3476 slang->sl_compsylmax = c;
3477
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003478 c = getc(fd); /* <compoptions> */
3479 if (c != 0)
3480 ungetc(c, fd); /* be backwards compatible with Vim 7.0b */
3481 else
3482 {
3483 --todo;
3484 c = getc(fd); /* only use the lower byte for now */
3485 --todo;
3486 slang->sl_compoptions = c;
3487
3488 gap = &slang->sl_comppat;
3489 c = get2c(fd); /* <comppatcount> */
3490 todo -= 2;
3491 ga_init2(gap, sizeof(char_u *), c);
3492 if (ga_grow(gap, c) == OK)
3493 while (--c >= 0)
3494 {
3495 ((char_u **)(gap->ga_data))[gap->ga_len++] =
3496 read_cnt_string(fd, 1, &cnt);
3497 /* <comppatlen> <comppattext> */
3498 if (cnt < 0)
3499 return cnt;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003500 todo -= cnt + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003501 }
3502 }
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003503 if (todo < 0)
3504 return SP_FORMERROR;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003505
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003506 /* Turn the COMPOUNDRULE items into a regexp pattern:
Bram Moolenaar5195e452005-08-19 20:32:47 +00003507 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003508 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3509 * Conversion to utf-8 may double the size. */
3510 c = todo * 2 + 7;
3511#ifdef FEAT_MBYTE
3512 if (enc_utf8)
3513 c += todo * 2;
3514#endif
3515 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003516 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003517 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003518
Bram Moolenaard12a1322005-08-21 22:08:24 +00003519 /* We also need a list of all flags that can appear at the start and one
3520 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003521 cp = alloc(todo + 1);
3522 if (cp == NULL)
3523 {
3524 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003525 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003526 }
3527 slang->sl_compstartflags = cp;
3528 *cp = NUL;
3529
Bram Moolenaard12a1322005-08-21 22:08:24 +00003530 ap = alloc(todo + 1);
3531 if (ap == NULL)
3532 {
3533 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003534 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003535 }
3536 slang->sl_compallflags = ap;
3537 *ap = NUL;
3538
Bram Moolenaar5195e452005-08-19 20:32:47 +00003539 pp = pat;
3540 *pp++ = '^';
3541 *pp++ = '\\';
3542 *pp++ = '(';
3543
3544 atstart = 1;
3545 while (todo-- > 0)
3546 {
3547 c = getc(fd); /* <compflags> */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003548
3549 /* Add all flags to "sl_compallflags". */
3550 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003551 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003552 {
3553 *ap++ = c;
3554 *ap = NUL;
3555 }
3556
Bram Moolenaar5195e452005-08-19 20:32:47 +00003557 if (atstart != 0)
3558 {
3559 /* At start of item: copy flags to "sl_compstartflags". For a
3560 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3561 if (c == '[')
3562 atstart = 2;
3563 else if (c == ']')
3564 atstart = 0;
3565 else
3566 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003567 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003568 {
3569 *cp++ = c;
3570 *cp = NUL;
3571 }
3572 if (atstart == 1)
3573 atstart = 0;
3574 }
3575 }
3576 if (c == '/') /* slash separates two items */
3577 {
3578 *pp++ = '\\';
3579 *pp++ = '|';
3580 atstart = 1;
3581 }
3582 else /* normal char, "[abc]" and '*' are copied as-is */
3583 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003584 if (c == '+' || c == '~')
Bram Moolenaar5195e452005-08-19 20:32:47 +00003585 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003586#ifdef FEAT_MBYTE
3587 if (enc_utf8)
3588 pp += mb_char2bytes(c, pp);
3589 else
3590#endif
3591 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003592 }
3593 }
3594
3595 *pp++ = '\\';
3596 *pp++ = ')';
3597 *pp++ = '$';
3598 *pp = NUL;
3599
3600 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3601 vim_free(pat);
3602 if (slang->sl_compprog == NULL)
3603 return SP_FORMERROR;
3604
3605 return 0;
3606}
3607
Bram Moolenaar6de68532005-08-24 22:08:48 +00003608/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003609 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003610 * Like strchr() but independent of locale.
3611 */
3612 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003613byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003614 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003615 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003616{
3617 char_u *p;
3618
3619 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003620 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003621 return TRUE;
3622 return FALSE;
3623}
3624
Bram Moolenaar5195e452005-08-19 20:32:47 +00003625#define SY_MAXLEN 30
3626typedef struct syl_item_S
3627{
3628 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3629 int sy_len;
3630} syl_item_T;
3631
3632/*
3633 * Truncate "slang->sl_syllable" at the first slash and put the following items
3634 * in "slang->sl_syl_items".
3635 */
3636 static int
3637init_syl_tab(slang)
3638 slang_T *slang;
3639{
3640 char_u *p;
3641 char_u *s;
3642 int l;
3643 syl_item_T *syl;
3644
3645 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3646 p = vim_strchr(slang->sl_syllable, '/');
3647 while (p != NULL)
3648 {
3649 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003650 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003651 break;
3652 s = p;
3653 p = vim_strchr(p, '/');
3654 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003655 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003656 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003657 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003658 if (l >= SY_MAXLEN)
3659 return SP_FORMERROR;
3660 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003661 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003662 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3663 + slang->sl_syl_items.ga_len++;
3664 vim_strncpy(syl->sy_chars, s, l);
3665 syl->sy_len = l;
3666 }
3667 return OK;
3668}
3669
3670/*
3671 * Count the number of syllables in "word".
3672 * When "word" contains spaces the syllables after the last space are counted.
3673 * Returns zero if syllables are not defines.
3674 */
3675 static int
3676count_syllables(slang, word)
3677 slang_T *slang;
3678 char_u *word;
3679{
3680 int cnt = 0;
3681 int skip = FALSE;
3682 char_u *p;
3683 int len;
3684 int i;
3685 syl_item_T *syl;
3686 int c;
3687
3688 if (slang->sl_syllable == NULL)
3689 return 0;
3690
3691 for (p = word; *p != NUL; p += len)
3692 {
3693 /* When running into a space reset counter. */
3694 if (*p == ' ')
3695 {
3696 len = 1;
3697 cnt = 0;
3698 continue;
3699 }
3700
3701 /* Find longest match of syllable items. */
3702 len = 0;
3703 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3704 {
3705 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3706 if (syl->sy_len > len
3707 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3708 len = syl->sy_len;
3709 }
3710 if (len != 0) /* found a match, count syllable */
3711 {
3712 ++cnt;
3713 skip = FALSE;
3714 }
3715 else
3716 {
3717 /* No recognized syllable item, at least a syllable char then? */
3718#ifdef FEAT_MBYTE
3719 c = mb_ptr2char(p);
3720 len = (*mb_ptr2len)(p);
3721#else
3722 c = *p;
3723 len = 1;
3724#endif
3725 if (vim_strchr(slang->sl_syllable, c) == NULL)
3726 skip = FALSE; /* No, search for next syllable */
3727 else if (!skip)
3728 {
3729 ++cnt; /* Yes, count it */
3730 skip = TRUE; /* don't count following syllable chars */
3731 }
3732 }
3733 }
3734 return cnt;
3735}
3736
3737/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003738 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003739 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003740 */
3741 static int
3742set_sofo(lp, from, to)
3743 slang_T *lp;
3744 char_u *from;
3745 char_u *to;
3746{
3747 int i;
3748
3749#ifdef FEAT_MBYTE
3750 garray_T *gap;
3751 char_u *s;
3752 char_u *p;
3753 int c;
3754 int *inp;
3755
3756 if (has_mbyte)
3757 {
3758 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3759 * characters. The index is the low byte of the character.
3760 * The list contains from-to pairs with a terminating NUL.
3761 * sl_sal_first[] is used for latin1 "from" characters. */
3762 gap = &lp->sl_sal;
3763 ga_init2(gap, sizeof(int *), 1);
3764 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003765 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003766 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3767 gap->ga_len = 256;
3768
3769 /* First count the number of items for each list. Temporarily use
3770 * sl_sal_first[] for this. */
3771 for (p = from, s = to; *p != NUL && *s != NUL; )
3772 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003773 c = mb_cptr2char_adv(&p);
3774 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003775 if (c >= 256)
3776 ++lp->sl_sal_first[c & 0xff];
3777 }
3778 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003779 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003780
3781 /* Allocate the lists. */
3782 for (i = 0; i < 256; ++i)
3783 if (lp->sl_sal_first[i] > 0)
3784 {
3785 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3786 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003787 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003788 ((int **)gap->ga_data)[i] = (int *)p;
3789 *(int *)p = 0;
3790 }
3791
3792 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3793 * list. */
3794 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3795 for (p = from, s = to; *p != NUL && *s != NUL; )
3796 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003797 c = mb_cptr2char_adv(&p);
3798 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003799 if (c >= 256)
3800 {
3801 /* Append the from-to chars at the end of the list with
3802 * the low byte. */
3803 inp = ((int **)gap->ga_data)[c & 0xff];
3804 while (*inp != 0)
3805 ++inp;
3806 *inp++ = c; /* from char */
3807 *inp++ = i; /* to char */
3808 *inp++ = NUL; /* NUL at the end */
3809 }
3810 else
3811 /* mapping byte to char is done in sl_sal_first[] */
3812 lp->sl_sal_first[c] = i;
3813 }
3814 }
3815 else
3816#endif
3817 {
3818 /* mapping bytes to bytes is done in sl_sal_first[] */
3819 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003820 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003821
3822 for (i = 0; to[i] != NUL; ++i)
3823 lp->sl_sal_first[from[i]] = to[i];
3824 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3825 }
3826
Bram Moolenaar5195e452005-08-19 20:32:47 +00003827 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003828}
3829
3830/*
3831 * Fill the first-index table for "lp".
3832 */
3833 static void
3834set_sal_first(lp)
3835 slang_T *lp;
3836{
3837 salfirst_T *sfirst;
3838 int i;
3839 salitem_T *smp;
3840 int c;
3841 garray_T *gap = &lp->sl_sal;
3842
3843 sfirst = lp->sl_sal_first;
3844 for (i = 0; i < 256; ++i)
3845 sfirst[i] = -1;
3846 smp = (salitem_T *)gap->ga_data;
3847 for (i = 0; i < gap->ga_len; ++i)
3848 {
3849#ifdef FEAT_MBYTE
3850 if (has_mbyte)
3851 /* Use the lowest byte of the first character. For latin1 it's
3852 * the character, for other encodings it should differ for most
3853 * characters. */
3854 c = *smp[i].sm_lead_w & 0xff;
3855 else
3856#endif
3857 c = *smp[i].sm_lead;
3858 if (sfirst[c] == -1)
3859 {
3860 sfirst[c] = i;
3861#ifdef FEAT_MBYTE
3862 if (has_mbyte)
3863 {
3864 int n;
3865
3866 /* Make sure all entries with this byte are following each
3867 * other. Move the ones that are in the wrong position. Do
3868 * keep the same ordering! */
3869 while (i + 1 < gap->ga_len
3870 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3871 /* Skip over entry with same index byte. */
3872 ++i;
3873
3874 for (n = 1; i + n < gap->ga_len; ++n)
3875 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3876 {
3877 salitem_T tsal;
3878
3879 /* Move entry with same index byte after the entries
3880 * we already found. */
3881 ++i;
3882 --n;
3883 tsal = smp[i + n];
3884 mch_memmove(smp + i + 1, smp + i,
3885 sizeof(salitem_T) * n);
3886 smp[i] = tsal;
3887 }
3888 }
3889#endif
3890 }
3891 }
3892}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003893
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003894#ifdef FEAT_MBYTE
3895/*
3896 * Turn a multi-byte string into a wide character string.
3897 * Return it in allocated memory (NULL for out-of-memory)
3898 */
3899 static int *
3900mb_str2wide(s)
3901 char_u *s;
3902{
3903 int *res;
3904 char_u *p;
3905 int i = 0;
3906
3907 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3908 if (res != NULL)
3909 {
3910 for (p = s; *p != NUL; )
3911 res[i++] = mb_ptr2char_adv(&p);
3912 res[i] = NUL;
3913 }
3914 return res;
3915}
3916#endif
3917
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003918/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003919 * Read a tree from the .spl or .sug file.
3920 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
3921 * This is skipped when the tree has zero length.
3922 * Returns zero when OK, SP_ value for an error.
3923 */
3924 static int
3925spell_read_tree(fd, bytsp, idxsp, prefixtree, prefixcnt)
3926 FILE *fd;
3927 char_u **bytsp;
3928 idx_T **idxsp;
3929 int prefixtree; /* TRUE for the prefix tree */
3930 int prefixcnt; /* when "prefixtree" is TRUE: prefix count */
3931{
3932 int len;
3933 int idx;
3934 char_u *bp;
3935 idx_T *ip;
3936
3937 /* The tree size was computed when writing the file, so that we can
3938 * allocate it as one long block. <nodecount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003939 len = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003940 if (len < 0)
3941 return SP_TRUNCERROR;
3942 if (len > 0)
3943 {
3944 /* Allocate the byte array. */
3945 bp = lalloc((long_u)len, TRUE);
3946 if (bp == NULL)
3947 return SP_OTHERERROR;
3948 *bytsp = bp;
3949
3950 /* Allocate the index array. */
3951 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
3952 if (ip == NULL)
3953 return SP_OTHERERROR;
3954 *idxsp = ip;
3955
3956 /* Recursively read the tree and store it in the array. */
3957 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
3958 if (idx < 0)
3959 return idx;
3960 }
3961 return 0;
3962}
3963
3964/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003965 * Read one row of siblings from the spell file and store it in the byte array
3966 * "byts" and index array "idxs". Recursively read the children.
3967 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003968 * NOTE: The code here must match put_node()!
Bram Moolenaar51485f02005-06-04 21:55:20 +00003969 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003970 * Returns the index (>= 0) following the siblings.
3971 * Returns SP_TRUNCERROR if the file is shorter than expected.
3972 * Returns SP_FORMERROR if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003973 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003974 static idx_T
Bram Moolenaar4770d092006-01-12 23:22:24 +00003975read_tree_node(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003976 FILE *fd;
3977 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003978 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003979 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003980 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003981 int prefixtree; /* TRUE for reading PREFIXTREE */
3982 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003983{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003984 int len;
3985 int i;
3986 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003987 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003988 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003989 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003990#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003991
Bram Moolenaar51485f02005-06-04 21:55:20 +00003992 len = getc(fd); /* <siblingcount> */
3993 if (len <= 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003994 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003995
3996 if (startidx + len >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003997 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003998 byts[idx++] = len;
3999
4000 /* Read the byte values, flag/region bytes and shared indexes. */
4001 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004002 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004003 c = getc(fd); /* <byte> */
4004 if (c < 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004005 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004006 if (c <= BY_SPECIAL)
4007 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004008 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004009 {
4010 /* No flags, all regions. */
4011 idxs[idx] = 0;
4012 c = 0;
4013 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004014 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004015 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004016 if (prefixtree)
4017 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004018 /* Read the optional pflags byte, the prefix ID and the
4019 * condition nr. In idxs[] store the prefix ID in the low
4020 * byte, the condition index shifted up 8 bits, the flags
4021 * shifted up 24 bits. */
4022 if (c == BY_FLAGS)
4023 c = getc(fd) << 24; /* <pflags> */
4024 else
4025 c = 0;
4026
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004027 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004028
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004029 n = get2c(fd); /* <prefcondnr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004030 if (n >= maxprefcondnr)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004031 return SP_FORMERROR;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004032 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004033 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004034 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004035 {
4036 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004037 * idxs[] the flags go in the low two bytes, region above
4038 * that and prefix ID above the region. */
4039 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004040 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004041 if (c2 == BY_FLAGS2)
4042 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004043 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004044 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004045 if (c & WF_AFX)
4046 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004047 }
4048
Bram Moolenaar51485f02005-06-04 21:55:20 +00004049 idxs[idx] = c;
4050 c = 0;
4051 }
4052 else /* c == BY_INDEX */
4053 {
4054 /* <nodeidx> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004055 n = get3c(fd);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004056 if (n < 0 || n >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004057 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004058 idxs[idx] = n + SHARED_MASK;
4059 c = getc(fd); /* <xbyte> */
4060 }
4061 }
4062 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004063 }
4064
Bram Moolenaar51485f02005-06-04 21:55:20 +00004065 /* Recursively read the children for non-shared siblings.
4066 * Skip the end-of-word ones (zero byte value) and the shared ones (and
4067 * remove SHARED_MASK) */
4068 for (i = 1; i <= len; ++i)
4069 if (byts[startidx + i] != 0)
4070 {
4071 if (idxs[startidx + i] & SHARED_MASK)
4072 idxs[startidx + i] &= ~SHARED_MASK;
4073 else
4074 {
4075 idxs[startidx + i] = idx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004076 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004077 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004078 if (idx < 0)
4079 break;
4080 }
4081 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004082
Bram Moolenaar51485f02005-06-04 21:55:20 +00004083 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004084}
4085
4086/*
4087 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004088 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004089 */
4090 char_u *
4091did_set_spelllang(buf)
4092 buf_T *buf;
4093{
4094 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004095 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004096 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00004097 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004098 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004099 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004100 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004101 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004102 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004103 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004104 int len;
4105 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004106 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004107 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004108 char_u *use_region = NULL;
4109 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004110 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004111 int i, j;
4112 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004113 static int recursive = FALSE;
4114 char_u *ret_msg = NULL;
4115 char_u *spl_copy;
4116
4117 /* We don't want to do this recursively. May happen when a language is
4118 * not available and the SpellFileMissing autocommand opens a new buffer
4119 * in which 'spell' is set. */
4120 if (recursive)
4121 return NULL;
4122 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004123
4124 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004125 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004126
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004127 /* Make a copy of 'spellang', the SpellFileMissing autocommands may change
4128 * it under our fingers. */
4129 spl_copy = vim_strsave(buf->b_p_spl);
4130 if (spl_copy == NULL)
4131 goto theend;
4132
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004133 /* loop over comma separated language names. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004134 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004135 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004136 /* Get one language name. */
4137 copy_option_part(&splp, lang, MAXWLEN, ",");
4138
Bram Moolenaar5482f332005-04-17 20:18:43 +00004139 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004140 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004141
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004142 /* If the name ends in ".spl" use it as the name of the spell file.
4143 * If there is a region name let "region" point to it and remove it
4144 * from the name. */
4145 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
4146 {
4147 filename = TRUE;
4148
Bram Moolenaarb6356332005-07-18 21:40:44 +00004149 /* Locate a region and remove it from the file name. */
4150 p = vim_strchr(gettail(lang), '_');
4151 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
4152 && !ASCII_ISALPHA(p[3]))
4153 {
4154 vim_strncpy(region_cp, p + 1, 2);
4155 mch_memmove(p, p + 3, len - (p - lang) - 2);
4156 len -= 3;
4157 region = region_cp;
4158 }
4159 else
4160 dont_use_region = TRUE;
4161
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004162 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004163 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4164 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004165 break;
4166 }
4167 else
4168 {
4169 filename = FALSE;
4170 if (len > 3 && lang[len - 3] == '_')
4171 {
4172 region = lang + len - 2;
4173 len -= 3;
4174 lang[len] = NUL;
4175 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004176 else
4177 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004178
4179 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004180 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4181 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004182 break;
4183 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004184
Bram Moolenaarb6356332005-07-18 21:40:44 +00004185 if (region != NULL)
4186 {
4187 /* If the region differs from what was used before then don't
4188 * use it for 'spellfile'. */
4189 if (use_region != NULL && STRCMP(region, use_region) != 0)
4190 dont_use_region = TRUE;
4191 use_region = region;
4192 }
4193
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004194 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004195 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004196 {
4197 if (filename)
4198 (void)spell_load_file(lang, lang, NULL, FALSE);
4199 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004200 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004201 spell_load_lang(lang);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004202#ifdef FEAT_AUTOCMD
4203 /* SpellFileMissing autocommands may do anything, including
4204 * destroying the buffer we are using... */
4205 if (!buf_valid(buf))
4206 {
4207 ret_msg = (char_u *)"E797: SpellFileMissing autocommand deleted buffer";
4208 goto theend;
4209 }
4210#endif
4211 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004212 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004213
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004214 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004215 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004216 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004217 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4218 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4219 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004220 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004221 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004222 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004223 {
4224 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004225 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004226 if (c == REGION_ALL)
4227 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004228 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004229 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004230 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004231 /* This addition file is for other regions. */
4232 region_mask = 0;
4233 }
4234 else
4235 /* This is probably an error. Give a warning and
4236 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004237 smsg((char_u *)
4238 _("Warning: region %s not supported"),
4239 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004240 }
4241 else
4242 region_mask = 1 << c;
4243 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004244
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004245 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004246 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004247 if (ga_grow(&ga, 1) == FAIL)
4248 {
4249 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004250 ret_msg = e_outofmem;
4251 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004252 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004253 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004254 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4255 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004256 use_midword(slang, buf);
4257 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004258 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004259 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004260 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004261 }
4262
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004263 /* round 0: load int_wordlist, if possible.
4264 * round 1: load first name in 'spellfile'.
4265 * round 2: load second name in 'spellfile.
4266 * etc. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004267 spf = buf->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004268 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004269 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004270 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004271 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004272 /* Internal wordlist, if there is one. */
4273 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004274 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004275 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004276 }
4277 else
4278 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004279 /* One entry in 'spellfile'. */
4280 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4281 STRCAT(spf_name, ".spl");
4282
4283 /* If it was already found above then skip it. */
4284 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004285 {
4286 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4287 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004288 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004289 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004290 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004291 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004292 }
4293
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004294 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004295 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4296 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004297 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004298 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004299 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004300 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004301 * region name, the region is ignored otherwise. for int_wordlist
4302 * use an arbitrary name. */
4303 if (round == 0)
4304 STRCPY(lang, "internal wordlist");
4305 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00004306 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004307 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004308 p = vim_strchr(lang, '.');
4309 if (p != NULL)
4310 *p = NUL; /* truncate at ".encoding.add" */
4311 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004312 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004313
4314 /* If one of the languages has NOBREAK we assume the addition
4315 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004316 if (slang != NULL && nobreak)
4317 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004318 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004319 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004320 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004321 region_mask = REGION_ALL;
4322 if (use_region != NULL && !dont_use_region)
4323 {
4324 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004325 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004326 if (c != REGION_ALL)
4327 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004328 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004329 /* This spell file is for other regions. */
4330 region_mask = 0;
4331 }
4332
4333 if (region_mask != 0)
4334 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004335 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4336 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4337 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004338 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4339 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004340 use_midword(slang, buf);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004341 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004342 }
4343 }
4344
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004345 /* Everything is fine, store the new b_langp value. */
4346 ga_clear(&buf->b_langp);
4347 buf->b_langp = ga;
4348
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004349 /* For each language figure out what language to use for sound folding and
4350 * REP items. If the language doesn't support it itself use another one
4351 * with the same name. E.g. for "en-math" use "en". */
4352 for (i = 0; i < ga.ga_len; ++i)
4353 {
4354 lp = LANGP_ENTRY(ga, i);
4355
4356 /* sound folding */
4357 if (lp->lp_slang->sl_sal.ga_len > 0)
4358 /* language does sound folding itself */
4359 lp->lp_sallang = lp->lp_slang;
4360 else
4361 /* find first similar language that does sound folding */
4362 for (j = 0; j < ga.ga_len; ++j)
4363 {
4364 lp2 = LANGP_ENTRY(ga, j);
4365 if (lp2->lp_slang->sl_sal.ga_len > 0
4366 && STRNCMP(lp->lp_slang->sl_name,
4367 lp2->lp_slang->sl_name, 2) == 0)
4368 {
4369 lp->lp_sallang = lp2->lp_slang;
4370 break;
4371 }
4372 }
4373
4374 /* REP items */
4375 if (lp->lp_slang->sl_rep.ga_len > 0)
4376 /* language has REP items itself */
4377 lp->lp_replang = lp->lp_slang;
4378 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004379 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004380 for (j = 0; j < ga.ga_len; ++j)
4381 {
4382 lp2 = LANGP_ENTRY(ga, j);
4383 if (lp2->lp_slang->sl_rep.ga_len > 0
4384 && STRNCMP(lp->lp_slang->sl_name,
4385 lp2->lp_slang->sl_name, 2) == 0)
4386 {
4387 lp->lp_replang = lp2->lp_slang;
4388 break;
4389 }
4390 }
4391 }
4392
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004393theend:
4394 vim_free(spl_copy);
4395 recursive = FALSE;
4396 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004397}
4398
4399/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004400 * Clear the midword characters for buffer "buf".
4401 */
4402 static void
4403clear_midword(buf)
4404 buf_T *buf;
4405{
4406 vim_memset(buf->b_spell_ismw, 0, 256);
4407#ifdef FEAT_MBYTE
4408 vim_free(buf->b_spell_ismw_mb);
4409 buf->b_spell_ismw_mb = NULL;
4410#endif
4411}
4412
4413/*
4414 * Use the "sl_midword" field of language "lp" for buffer "buf".
4415 * They add up to any currently used midword characters.
4416 */
4417 static void
4418use_midword(lp, buf)
4419 slang_T *lp;
4420 buf_T *buf;
4421{
4422 char_u *p;
4423
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004424 if (lp->sl_midword == NULL) /* there aren't any */
4425 return;
4426
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004427 for (p = lp->sl_midword; *p != NUL; )
4428#ifdef FEAT_MBYTE
4429 if (has_mbyte)
4430 {
4431 int c, l, n;
4432 char_u *bp;
4433
4434 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004435 l = (*mb_ptr2len)(p);
4436 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004437 buf->b_spell_ismw[c] = TRUE;
4438 else if (buf->b_spell_ismw_mb == NULL)
4439 /* First multi-byte char in "b_spell_ismw_mb". */
4440 buf->b_spell_ismw_mb = vim_strnsave(p, l);
4441 else
4442 {
4443 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004444 n = (int)STRLEN(buf->b_spell_ismw_mb);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004445 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
4446 if (bp != NULL)
4447 {
4448 vim_free(buf->b_spell_ismw_mb);
4449 buf->b_spell_ismw_mb = bp;
4450 vim_strncpy(bp + n, p, l);
4451 }
4452 }
4453 p += l;
4454 }
4455 else
4456#endif
4457 buf->b_spell_ismw[*p++] = TRUE;
4458}
4459
4460/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004461 * Find the region "region[2]" in "rp" (points to "sl_regions").
4462 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00004463 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004464 */
4465 static int
4466find_region(rp, region)
4467 char_u *rp;
4468 char_u *region;
4469{
4470 int i;
4471
4472 for (i = 0; ; i += 2)
4473 {
4474 if (rp[i] == NUL)
4475 return REGION_ALL;
4476 if (rp[i] == region[0] && rp[i + 1] == region[1])
4477 break;
4478 }
4479 return i / 2;
4480}
4481
4482/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004483 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004484 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00004485 * Word WF_ONECAP
4486 * W WORD WF_ALLCAP
4487 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004488 */
4489 static int
4490captype(word, end)
4491 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004492 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004493{
4494 char_u *p;
4495 int c;
4496 int firstcap;
4497 int allcap;
4498 int past_second = FALSE; /* past second word char */
4499
4500 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004501 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004502 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004503 return 0; /* only non-word characters, illegal word */
4504#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004505 if (has_mbyte)
4506 c = mb_ptr2char_adv(&p);
4507 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004508#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004509 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004510 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004511
4512 /*
4513 * Need to check all letters to find a word with mixed upper/lower.
4514 * But a word with an upper char only at start is a ONECAP.
4515 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004516 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004517 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004518 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004519 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004520 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004521 {
4522 /* UUl -> KEEPCAP */
4523 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004524 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004525 allcap = FALSE;
4526 }
4527 else if (!allcap)
4528 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004529 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004530 past_second = TRUE;
4531 }
4532
4533 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004534 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004535 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004536 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004537 return 0;
4538}
4539
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004540/*
4541 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4542 * capital. So that make_case_word() can turn WOrd into Word.
4543 * Add ALLCAP for "WOrD".
4544 */
4545 static int
4546badword_captype(word, end)
4547 char_u *word;
4548 char_u *end;
4549{
4550 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004551 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004552 int l, u;
4553 int first;
4554 char_u *p;
4555
4556 if (flags & WF_KEEPCAP)
4557 {
4558 /* Count the number of UPPER and lower case letters. */
4559 l = u = 0;
4560 first = FALSE;
4561 for (p = word; p < end; mb_ptr_adv(p))
4562 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004563 c = PTR2CHAR(p);
4564 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004565 {
4566 ++u;
4567 if (p == word)
4568 first = TRUE;
4569 }
4570 else
4571 ++l;
4572 }
4573
4574 /* If there are more UPPER than lower case letters suggest an
4575 * ALLCAP word. Otherwise, if the first letter is UPPER then
4576 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4577 * require three upper case letters. */
4578 if (u > l && u > 2)
4579 flags |= WF_ALLCAP;
4580 else if (first)
4581 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004582
4583 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4584 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004585 }
4586 return flags;
4587}
4588
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004589# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4590/*
4591 * Free all languages.
4592 */
4593 void
4594spell_free_all()
4595{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004596 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004597 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004598 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004599
4600 /* Go through all buffers and handle 'spelllang'. */
4601 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4602 ga_clear(&buf->b_langp);
4603
4604 while (first_lang != NULL)
4605 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004606 slang = first_lang;
4607 first_lang = slang->sl_next;
4608 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004609 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004610
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004611 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004612 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004613 /* Delete the internal wordlist and its .spl file */
4614 mch_remove(int_wordlist);
4615 int_wordlist_spl(fname);
4616 mch_remove(fname);
4617 vim_free(int_wordlist);
4618 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004619 }
4620
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004621 init_spell_chartab();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004622
4623 vim_free(repl_to);
4624 repl_to = NULL;
4625 vim_free(repl_from);
4626 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004627}
4628# endif
4629
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004630# if defined(FEAT_MBYTE) || defined(PROTO)
4631/*
4632 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004633 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004634 */
4635 void
4636spell_reload()
4637{
4638 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004639 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004640
Bram Moolenaarea408852005-06-25 22:49:46 +00004641 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004642 init_spell_chartab();
4643
4644 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004645 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004646
4647 /* Go through all buffers and handle 'spelllang'. */
4648 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4649 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004650 /* Only load the wordlists when 'spelllang' is set and there is a
4651 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004652 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004653 {
4654 FOR_ALL_WINDOWS(wp)
4655 if (wp->w_buffer == buf && wp->w_p_spell)
4656 {
4657 (void)did_set_spelllang(buf);
4658# ifdef FEAT_WINDOWS
4659 break;
4660# endif
4661 }
4662 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004663 }
4664}
4665# endif
4666
Bram Moolenaarb765d632005-06-07 21:00:02 +00004667/*
4668 * Reload the spell file "fname" if it's loaded.
4669 */
4670 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004671spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004672 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004673 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004674{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004675 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004676 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004677
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004678 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004679 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004680 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004681 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004682 slang_clear(slang);
4683 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004684 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004685 slang_clear(slang);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004686 redraw_all_later(SOME_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004687 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004688 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004689 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004690
4691 /* When "zg" was used and the file wasn't loaded yet, should redo
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004692 * 'spelllang' to load it now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004693 if (added_word && !didit)
4694 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004695}
4696
4697
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004698/*
4699 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004700 */
4701
Bram Moolenaar51485f02005-06-04 21:55:20 +00004702#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004703 and .dic file. */
4704/*
4705 * Main structure to store the contents of a ".aff" file.
4706 */
4707typedef struct afffile_S
4708{
4709 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004710 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004711 unsigned af_rare; /* RARE ID for rare word */
4712 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004713 unsigned af_bad; /* BAD ID for banned word */
4714 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004715 unsigned af_circumfix; /* CIRCUMFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004716 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004717 unsigned af_comproot; /* COMPOUNDROOT ID */
4718 unsigned af_compforbid; /* COMPOUNDFORBIDFLAG ID */
4719 unsigned af_comppermit; /* COMPOUNDPERMITFLAG ID */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004720 unsigned af_nosuggest; /* NOSUGGEST ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004721 int af_pfxpostpone; /* postpone prefixes without chop string and
4722 without flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004723 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4724 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004725 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004726} afffile_T;
4727
Bram Moolenaar6de68532005-08-24 22:08:48 +00004728#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004729#define AFT_LONG 1 /* flags are two characters */
4730#define AFT_CAPLONG 2 /* flags are one or two characters */
4731#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004732
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004733typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004734/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4735struct affentry_S
4736{
4737 affentry_T *ae_next; /* next affix with same name/number */
4738 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4739 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004740 char_u *ae_flags; /* flags on the affix (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004741 char_u *ae_cond; /* condition (NULL for ".") */
4742 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004743 char ae_compforbid; /* COMPOUNDFORBIDFLAG found */
4744 char ae_comppermit; /* COMPOUNDPERMITFLAG found */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004745};
4746
Bram Moolenaar6de68532005-08-24 22:08:48 +00004747#ifdef FEAT_MBYTE
4748# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4749#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004750# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004751#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004752
Bram Moolenaar51485f02005-06-04 21:55:20 +00004753/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4754typedef struct affheader_S
4755{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004756 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4757 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4758 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004759 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004760 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004761 affentry_T *ah_first; /* first affix entry */
4762} affheader_T;
4763
4764#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4765
Bram Moolenaar6de68532005-08-24 22:08:48 +00004766/* Flag used in compound items. */
4767typedef struct compitem_S
4768{
4769 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4770 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4771 int ci_newID; /* affix ID after renumbering. */
4772} compitem_T;
4773
4774#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4775
Bram Moolenaar51485f02005-06-04 21:55:20 +00004776/*
4777 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004778 * the need to keep track of each allocated thing, everything is freed all at
4779 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004780 */
4781#define SBLOCKSIZE 16000 /* size of sb_data */
4782typedef struct sblock_S sblock_T;
4783struct sblock_S
4784{
4785 sblock_T *sb_next; /* next block in list */
4786 int sb_used; /* nr of bytes already in use */
4787 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004788};
4789
4790/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004791 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004792 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004793typedef struct wordnode_S wordnode_T;
4794struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004795{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004796 union /* shared to save space */
4797 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004798 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004799 int index; /* index in written nodes (valid after first
4800 round) */
4801 } wn_u1;
4802 union /* shared to save space */
4803 {
4804 wordnode_T *next; /* next node with same hash key */
4805 wordnode_T *wnode; /* parent node that will write this node */
4806 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004807 wordnode_T *wn_child; /* child (next byte in word) */
4808 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4809 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004810 int wn_refs; /* Nr. of references to this node. Only
4811 relevant for first node in a list of
4812 siblings, in following siblings it is
4813 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004814 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004815
4816 /* Info for when "wn_byte" is NUL.
4817 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4818 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4819 * "wn_region" the LSW of the wordnr. */
4820 char_u wn_affixID; /* supported/required prefix ID or 0 */
4821 short_u wn_flags; /* WF_ flags */
4822 short wn_region; /* region mask */
4823
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004824#ifdef SPELL_PRINTTREE
4825 int wn_nr; /* sequence nr for printing */
4826#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004827};
4828
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004829#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4830
Bram Moolenaar51485f02005-06-04 21:55:20 +00004831#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004832
Bram Moolenaar51485f02005-06-04 21:55:20 +00004833/*
4834 * Info used while reading the spell files.
4835 */
4836typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004837{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004838 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004839 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004840
Bram Moolenaar51485f02005-06-04 21:55:20 +00004841 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004842 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004843
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004844 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004845
Bram Moolenaar4770d092006-01-12 23:22:24 +00004846 long si_sugtree; /* creating the soundfolding trie */
4847
Bram Moolenaar51485f02005-06-04 21:55:20 +00004848 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004849 long si_blocks_cnt; /* memory blocks allocated */
4850 long si_compress_cnt; /* words to add before lowering
4851 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004852 wordnode_T *si_first_free; /* List of nodes that have been freed during
4853 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004854 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004855#ifdef SPELL_PRINTTREE
4856 int si_wordnode_nr; /* sequence nr for nodes */
4857#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004858 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004859
Bram Moolenaar51485f02005-06-04 21:55:20 +00004860 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004861 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004862 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004863 int si_region; /* region mask */
4864 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004865 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004866 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004867 int si_msg_count; /* number of words added since last message */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004868 char_u *si_info; /* info text chars or NULL */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004869 int si_region_count; /* number of regions supported (1 when there
4870 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004871 char_u si_region_name[16]; /* region names; used only if
4872 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004873
4874 garray_T si_rep; /* list of fromto_T entries from REP lines */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004875 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004876 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004877 char_u *si_sofofr; /* SOFOFROM text */
4878 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004879 int si_nosugfile; /* NOSUGFILE item found */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004880 int si_nosplitsugs; /* NOSPLITSUGS item found */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004881 int si_followup; /* soundsalike: ? */
4882 int si_collapse; /* soundsalike: ? */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004883 hashtab_T si_commonwords; /* hashtable for common words */
4884 time_t si_sugtime; /* timestamp for .sug file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004885 int si_rem_accents; /* soundsalike: remove accents */
4886 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004887 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004888 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004889 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004890 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004891 int si_compoptions; /* COMP_ flags */
4892 garray_T si_comppat; /* CHECKCOMPOUNDPATTERN items, each stored as
4893 a string */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004894 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004895 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004896 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004897 garray_T si_prefcond; /* table with conditions for postponed
4898 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004899 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004900 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004901} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004902
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004903static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004904static void aff_process_flags __ARGS((afffile_T *affile, affentry_T *entry));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004905static int spell_info_item __ARGS((char_u *s));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004906static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
4907static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
4908static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004909static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004910static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
4911static void aff_check_number __ARGS((int spinval, int affval, char *name));
4912static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004913static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004914static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4915static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00004916static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004917static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004918static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004919static int get_affix_flags __ARGS((afffile_T *affile, char_u *afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004920static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004921static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004922static 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 +00004923static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
4924static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
4925static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004926static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004927static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004928static 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 +00004929static 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 +00004930static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004931static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004932static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
4933static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
4934static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004935static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004936static void put_sugtime __ARGS((spellinfo_T *spin, FILE *fd));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004937static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00004938static void clear_node __ARGS((wordnode_T *node));
4939static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004940static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname));
4941static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang));
4942static int sug_maketable __ARGS((spellinfo_T *spin));
4943static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap));
4944static int offset2bytes __ARGS((int nr, char_u *buf));
4945static int bytes2offset __ARGS((char_u **pp));
4946static void sug_write __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004947static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004948static void spell_message __ARGS((spellinfo_T *spin, char_u *str));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004949static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004950
Bram Moolenaar53805d12005-08-01 07:08:33 +00004951/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
4952 * but it must be negative to indicate the prefix tree to tree_add_word().
4953 * Use a negative number with the lower 8 bits zero. */
4954#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004955
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004956/* flags for "condit" argument of store_aff_word() */
4957#define CONDIT_COMB 1 /* affix must combine */
4958#define CONDIT_CFIX 2 /* affix must have CIRCUMFIX flag */
4959#define CONDIT_SUF 4 /* add a suffix for matching flags */
4960#define CONDIT_AFF 8 /* word already has an affix */
4961
Bram Moolenaar5195e452005-08-19 20:32:47 +00004962/*
4963 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
4964 */
4965static long compress_start = 30000; /* memory / SBLOCKSIZE */
4966static long compress_inc = 100; /* memory / SBLOCKSIZE */
4967static long compress_added = 500000; /* word count */
4968
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004969#ifdef SPELL_PRINTTREE
4970/*
4971 * For debugging the tree code: print the current tree in a (more or less)
4972 * readable format, so that we can see what happens when adding a word and/or
4973 * compressing the tree.
4974 * Based on code from Olaf Seibert.
4975 */
4976#define PRINTLINESIZE 1000
4977#define PRINTWIDTH 6
4978
4979#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4980 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4981
4982static char line1[PRINTLINESIZE];
4983static char line2[PRINTLINESIZE];
4984static char line3[PRINTLINESIZE];
4985
4986 static void
4987spell_clear_flags(wordnode_T *node)
4988{
4989 wordnode_T *np;
4990
4991 for (np = node; np != NULL; np = np->wn_sibling)
4992 {
4993 np->wn_u1.index = FALSE;
4994 spell_clear_flags(np->wn_child);
4995 }
4996}
4997
4998 static void
4999spell_print_node(wordnode_T *node, int depth)
5000{
5001 if (node->wn_u1.index)
5002 {
5003 /* Done this node before, print the reference. */
5004 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
5005 PRINTSOME(line2, depth, " ", 0, 0);
5006 PRINTSOME(line3, depth, " ", 0, 0);
5007 msg(line1);
5008 msg(line2);
5009 msg(line3);
5010 }
5011 else
5012 {
5013 node->wn_u1.index = TRUE;
5014
5015 if (node->wn_byte != NUL)
5016 {
5017 if (node->wn_child != NULL)
5018 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
5019 else
5020 /* Cannot happen? */
5021 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
5022 }
5023 else
5024 PRINTSOME(line1, depth, " $ ", 0, 0);
5025
5026 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
5027
5028 if (node->wn_sibling != NULL)
5029 PRINTSOME(line3, depth, " | ", 0, 0);
5030 else
5031 PRINTSOME(line3, depth, " ", 0, 0);
5032
5033 if (node->wn_byte == NUL)
5034 {
5035 msg(line1);
5036 msg(line2);
5037 msg(line3);
5038 }
5039
5040 /* do the children */
5041 if (node->wn_byte != NUL && node->wn_child != NULL)
5042 spell_print_node(node->wn_child, depth + 1);
5043
5044 /* do the siblings */
5045 if (node->wn_sibling != NULL)
5046 {
5047 /* get rid of all parent details except | */
5048 STRCPY(line1, line3);
5049 STRCPY(line2, line3);
5050 spell_print_node(node->wn_sibling, depth);
5051 }
5052 }
5053}
5054
5055 static void
5056spell_print_tree(wordnode_T *root)
5057{
5058 if (root != NULL)
5059 {
5060 /* Clear the "wn_u1.index" fields, used to remember what has been
5061 * done. */
5062 spell_clear_flags(root);
5063
5064 /* Recursively print the tree. */
5065 spell_print_node(root, 0);
5066 }
5067}
5068#endif /* SPELL_PRINTTREE */
5069
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005070/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005071 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00005072 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005073 */
5074 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005075spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005076 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005077 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005078{
5079 FILE *fd;
5080 afffile_T *aff;
5081 char_u rline[MAXLINELEN];
5082 char_u *line;
5083 char_u *pc = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005084#define MAXITEMCNT 30
Bram Moolenaar8db73182005-06-17 21:51:16 +00005085 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005086 int itemcnt;
5087 char_u *p;
5088 int lnum = 0;
5089 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005090 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005091 int aff_todo = 0;
5092 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005093 char_u *low = NULL;
5094 char_u *fol = NULL;
5095 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005096 int do_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005097 int do_repsal;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005098 int do_sal;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005099 int do_mapline;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005100 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005101 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005102 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005103 int compminlen = 0; /* COMPOUNDMIN value */
5104 int compsylmax = 0; /* COMPOUNDSYLMAX value */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005105 int compoptions = 0; /* COMP_ flags */
5106 int compmax = 0; /* COMPOUNDWORDMAX value */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005107 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDRULE
Bram Moolenaar6de68532005-08-24 22:08:48 +00005108 concatenated */
5109 char_u *midword = NULL; /* MIDWORD value */
5110 char_u *syllable = NULL; /* SYLLABLE value */
5111 char_u *sofofrom = NULL; /* SOFOFROM value */
5112 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005113
Bram Moolenaar51485f02005-06-04 21:55:20 +00005114 /*
5115 * Open the file.
5116 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005117 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005118 if (fd == NULL)
5119 {
5120 EMSG2(_(e_notopen), fname);
5121 return NULL;
5122 }
5123
Bram Moolenaar4770d092006-01-12 23:22:24 +00005124 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
5125 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005126
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005127 /* Only do REP lines when not done in another .aff file already. */
5128 do_rep = spin->si_rep.ga_len == 0;
5129
Bram Moolenaar4770d092006-01-12 23:22:24 +00005130 /* Only do REPSAL lines when not done in another .aff file already. */
5131 do_repsal = spin->si_repsal.ga_len == 0;
5132
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005133 /* Only do SAL lines when not done in another .aff file already. */
5134 do_sal = spin->si_sal.ga_len == 0;
5135
5136 /* Only do MAP lines when not done in another .aff file already. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00005137 do_mapline = spin->si_map.ga_len == 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005138
Bram Moolenaar51485f02005-06-04 21:55:20 +00005139 /*
5140 * Allocate and init the afffile_T structure.
5141 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005142 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005143 if (aff == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005144 {
5145 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005146 return NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005147 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005148 hash_init(&aff->af_pref);
5149 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005150 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005151
5152 /*
5153 * Read all the lines in the file one by one.
5154 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005155 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005156 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005157 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005158 ++lnum;
5159
5160 /* Skip comment lines. */
5161 if (*rline == '#')
5162 continue;
5163
5164 /* Convert from "SET" to 'encoding' when needed. */
5165 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005166#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005167 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005168 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005169 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005170 if (pc == NULL)
5171 {
5172 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5173 fname, lnum, rline);
5174 continue;
5175 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005176 line = pc;
5177 }
5178 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005179#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005180 {
5181 pc = NULL;
5182 line = rline;
5183 }
5184
5185 /* Split the line up in white separated items. Put a NUL after each
5186 * item. */
5187 itemcnt = 0;
5188 for (p = line; ; )
5189 {
5190 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
5191 ++p;
5192 if (*p == NUL)
5193 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00005194 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005195 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005196 items[itemcnt++] = p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005197 /* A few items have arbitrary text argument, don't split them. */
5198 if (itemcnt == 2 && spell_info_item(items[0]))
5199 while (*p >= ' ' || *p == TAB) /* skip until CR/NL */
5200 ++p;
5201 else
5202 while (*p > ' ') /* skip until white space or CR/NL */
5203 ++p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005204 if (*p == NUL)
5205 break;
5206 *p++ = NUL;
5207 }
5208
5209 /* Handle non-empty lines. */
5210 if (itemcnt > 0)
5211 {
5212 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
5213 && aff->af_enc == NULL)
5214 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005215#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005216 /* Setup for conversion from "ENC" to 'encoding'. */
5217 aff->af_enc = enc_canonize(items[1]);
5218 if (aff->af_enc != NULL && !spin->si_ascii
5219 && convert_setup(&spin->si_conv, aff->af_enc,
5220 p_enc) == FAIL)
5221 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
5222 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005223 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005224#else
5225 smsg((char_u *)_("Conversion in %s not supported"), fname);
5226#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005227 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005228 else if (STRCMP(items[0], "FLAG") == 0 && itemcnt == 2
5229 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005230 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005231 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005232 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005233 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005234 aff->af_flagtype = AFT_NUM;
5235 else if (STRCMP(items[1], "caplong") == 0)
5236 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005237 else
5238 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
5239 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00005240 if (aff->af_rare != 0
5241 || aff->af_keepcase != 0
5242 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005243 || aff->af_needaffix != 0
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005244 || aff->af_circumfix != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005245 || aff->af_needcomp != 0
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005246 || aff->af_comproot != 0
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005247 || aff->af_nosuggest != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005248 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00005249 || aff->af_suff.ht_used > 0
5250 || aff->af_pref.ht_used > 0)
5251 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
5252 fname, lnum, items[1]);
5253 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005254 else if (spell_info_item(items[0]))
5255 {
5256 p = (char_u *)getroom(spin,
5257 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
5258 + STRLEN(items[0])
5259 + STRLEN(items[1]) + 3, FALSE);
5260 if (p != NULL)
5261 {
5262 if (spin->si_info != NULL)
5263 {
5264 STRCPY(p, spin->si_info);
5265 STRCAT(p, "\n");
5266 }
5267 STRCAT(p, items[0]);
5268 STRCAT(p, " ");
5269 STRCAT(p, items[1]);
5270 spin->si_info = p;
5271 }
5272 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005273 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2
5274 && midword == NULL)
5275 {
5276 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005277 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005278 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005280 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005281 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005282 /* TODO: remove "RAR" later */
5283 else if ((STRCMP(items[0], "RAR") == 0
5284 || STRCMP(items[0], "RARE") == 0) && itemcnt == 2
5285 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005286 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005287 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005288 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005289 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005290 /* TODO: remove "KEP" later */
5291 else if ((STRCMP(items[0], "KEP") == 0
5292 || STRCMP(items[0], "KEEPCASE") == 0) && itemcnt == 2
5293 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005294 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005295 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005296 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005297 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005298 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
5299 && aff->af_bad == 0)
5300 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005301 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5302 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005303 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005304 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
5305 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005306 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005307 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5308 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005309 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005310 else if (STRCMP(items[0], "CIRCUMFIX") == 0 && itemcnt == 2
5311 && aff->af_circumfix == 0)
5312 {
5313 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
5314 fname, lnum);
5315 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005316 else if (STRCMP(items[0], "NOSUGGEST") == 0 && itemcnt == 2
5317 && aff->af_nosuggest == 0)
5318 {
5319 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
5320 fname, lnum);
5321 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005322 else if (STRCMP(items[0], "NEEDCOMPOUND") == 0 && itemcnt == 2
5323 && aff->af_needcomp == 0)
5324 {
5325 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5326 fname, lnum);
5327 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005328 else if (STRCMP(items[0], "COMPOUNDROOT") == 0 && itemcnt == 2
5329 && aff->af_comproot == 0)
5330 {
5331 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
5332 fname, lnum);
5333 }
5334 else if (STRCMP(items[0], "COMPOUNDFORBIDFLAG") == 0
5335 && itemcnt == 2 && aff->af_compforbid == 0)
5336 {
5337 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
5338 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005339 if (aff->af_pref.ht_used > 0)
5340 smsg((char_u *)_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
5341 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005342 }
5343 else if (STRCMP(items[0], "COMPOUNDPERMITFLAG") == 0
5344 && itemcnt == 2 && aff->af_comppermit == 0)
5345 {
5346 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
5347 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005348 if (aff->af_pref.ht_used > 0)
5349 smsg((char_u *)_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
5350 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005351 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005352 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005353 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005354 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005355 /* Turn flag "c" into COMPOUNDRULE compatible string "c+",
Bram Moolenaar6de68532005-08-24 22:08:48 +00005356 * "Na" into "Na+", "1234" into "1234+". */
5357 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005358 if (p != NULL)
5359 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005360 STRCPY(p, items[1]);
5361 STRCAT(p, "+");
5362 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005363 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005364 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005365 else if (STRCMP(items[0], "COMPOUNDRULE") == 0 && itemcnt == 2)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005366 {
5367 /* Concatenate this string to previously defined ones, using a
5368 * slash to separate them. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005369 l = (int)STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005370 if (compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005371 l += (int)STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005372 p = getroom(spin, l, FALSE);
5373 if (p != NULL)
5374 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005375 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005376 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005377 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005378 STRCAT(p, "/");
5379 }
5380 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005381 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005382 }
5383 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005384 else if (STRCMP(items[0], "COMPOUNDWORDMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005385 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005386 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005387 compmax = atoi((char *)items[1]);
5388 if (compmax == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005389 smsg((char_u *)_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar5195e452005-08-19 20:32:47 +00005390 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005391 }
5392 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005393 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005394 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005395 compminlen = atoi((char *)items[1]);
5396 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005397 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5398 fname, lnum, items[1]);
5399 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005400 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005401 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005402 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005403 compsylmax = atoi((char *)items[1]);
5404 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005405 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5406 fname, lnum, items[1]);
5407 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005408 else if (STRCMP(items[0], "CHECKCOMPOUNDDUP") == 0 && itemcnt == 1)
5409 {
5410 compoptions |= COMP_CHECKDUP;
5411 }
5412 else if (STRCMP(items[0], "CHECKCOMPOUNDREP") == 0 && itemcnt == 1)
5413 {
5414 compoptions |= COMP_CHECKREP;
5415 }
5416 else if (STRCMP(items[0], "CHECKCOMPOUNDCASE") == 0 && itemcnt == 1)
5417 {
5418 compoptions |= COMP_CHECKCASE;
5419 }
5420 else if (STRCMP(items[0], "CHECKCOMPOUNDTRIPLE") == 0
5421 && itemcnt == 1)
5422 {
5423 compoptions |= COMP_CHECKTRIPLE;
5424 }
5425 else if (STRCMP(items[0], "CHECKCOMPOUNDPATTERN") == 0
5426 && itemcnt == 2)
5427 {
5428 if (atoi((char *)items[1]) == 0)
5429 smsg((char_u *)_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
5430 fname, lnum, items[1]);
5431 }
5432 else if (STRCMP(items[0], "CHECKCOMPOUNDPATTERN") == 0
5433 && itemcnt == 3)
5434 {
5435 garray_T *gap = &spin->si_comppat;
5436 int i;
5437
5438 /* Only add the couple if it isn't already there. */
5439 for (i = 0; i < gap->ga_len - 1; i += 2)
5440 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
5441 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
5442 items[2]) == 0)
5443 break;
5444 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
5445 {
5446 ((char_u **)(gap->ga_data))[gap->ga_len++]
5447 = getroom_save(spin, items[1]);
5448 ((char_u **)(gap->ga_data))[gap->ga_len++]
5449 = getroom_save(spin, items[2]);
5450 }
5451 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005452 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005453 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005454 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005455 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005456 }
Bram Moolenaar78622822005-08-23 21:00:13 +00005457 else if (STRCMP(items[0], "NOBREAK") == 0 && itemcnt == 1)
5458 {
5459 spin->si_nobreak = TRUE;
5460 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005461 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
5462 {
5463 spin->si_nosplitsugs = TRUE;
5464 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005465 else if (STRCMP(items[0], "NOSUGFILE") == 0 && itemcnt == 1)
5466 {
5467 spin->si_nosugfile = TRUE;
5468 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005469 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
5470 {
5471 aff->af_pfxpostpone = TRUE;
5472 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005473 else if ((STRCMP(items[0], "PFX") == 0
5474 || STRCMP(items[0], "SFX") == 0)
5475 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005476 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005477 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005478 int lasti = 4;
5479 char_u key[AH_KEY_LEN];
5480
5481 if (*items[0] == 'P')
5482 tp = &aff->af_pref;
5483 else
5484 tp = &aff->af_suff;
5485
5486 /* Myspell allows the same affix name to be used multiple
5487 * times. The affix files that do this have an undocumented
5488 * "S" flag on all but the last block, thus we check for that
5489 * and store it in ah_follows. */
5490 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5491 hi = hash_find(tp, key);
5492 if (!HASHITEM_EMPTY(hi))
5493 {
5494 cur_aff = HI2AH(hi);
5495 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5496 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5497 fname, lnum, items[1]);
5498 if (!cur_aff->ah_follows)
5499 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5500 fname, lnum, items[1]);
5501 }
5502 else
5503 {
5504 /* New affix letter. */
5505 cur_aff = (affheader_T *)getroom(spin,
5506 sizeof(affheader_T), TRUE);
5507 if (cur_aff == NULL)
5508 break;
5509 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5510 fname, lnum);
5511 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5512 break;
5513 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00005514 || cur_aff->ah_flag == aff->af_rare
5515 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005516 || cur_aff->ah_flag == aff->af_needaffix
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005517 || cur_aff->ah_flag == aff->af_circumfix
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005518 || cur_aff->ah_flag == aff->af_nosuggest
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005519 || cur_aff->ah_flag == aff->af_needcomp
5520 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005521 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 +00005522 fname, lnum, items[1]);
5523 STRCPY(cur_aff->ah_key, items[1]);
5524 hash_add(tp, cur_aff->ah_key);
5525
5526 cur_aff->ah_combine = (*items[2] == 'Y');
5527 }
5528
5529 /* Check for the "S" flag, which apparently means that another
5530 * block with the same affix name is following. */
5531 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5532 {
5533 ++lasti;
5534 cur_aff->ah_follows = TRUE;
5535 }
5536 else
5537 cur_aff->ah_follows = FALSE;
5538
Bram Moolenaar8db73182005-06-17 21:51:16 +00005539 /* Myspell allows extra text after the item, but that might
5540 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00005541 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005542 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005543
Bram Moolenaar95529562005-08-25 21:21:38 +00005544 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005545 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5546 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005547
Bram Moolenaar95529562005-08-25 21:21:38 +00005548 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005549 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005550 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005551 {
5552 /* Use a new number in the .spl file later, to be able
5553 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005554 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005555 cur_aff->ah_newID = ++spin->si_newprefID;
5556
5557 /* We only really use ah_newID if the prefix is
5558 * postponed. We know that only after handling all
5559 * the items. */
5560 did_postpone_prefix = FALSE;
5561 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005562 else
5563 /* Did use the ID in a previous block. */
5564 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005565 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005566
Bram Moolenaar51485f02005-06-04 21:55:20 +00005567 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005568 }
5569 else if ((STRCMP(items[0], "PFX") == 0
5570 || STRCMP(items[0], "SFX") == 0)
5571 && aff_todo > 0
5572 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005573 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005574 {
5575 affentry_T *aff_entry;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005576 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005577 int lasti = 5;
5578
Bram Moolenaar8db73182005-06-17 21:51:16 +00005579 /* Myspell allows extra text after the item, but that might
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005580 * mean mistakes go unnoticed. Require a comment-starter.
5581 * Hunspell uses a "-" item. */
5582 if (itemcnt > lasti && *items[lasti] != '#'
5583 && (STRCMP(items[lasti], "-") != 0
5584 || itemcnt != lasti + 1))
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005585 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005586
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005587 /* New item for an affix letter. */
5588 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005589 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005590 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005591 if (aff_entry == NULL)
5592 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005593
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005594 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005595 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005596 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005597 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005598 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005599
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005600 /* Recognize flags on the affix: abcd/XYZ */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005601 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
5602 if (aff_entry->ae_flags != NULL)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005603 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005604 *aff_entry->ae_flags++ = NUL;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005605 aff_process_flags(aff, aff_entry);
5606 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005607 }
5608
Bram Moolenaar51485f02005-06-04 21:55:20 +00005609 /* Don't use an affix entry with non-ASCII characters when
5610 * "spin->si_ascii" is TRUE. */
5611 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005612 || has_non_ascii(aff_entry->ae_add)))
5613 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00005614 aff_entry->ae_next = cur_aff->ah_first;
5615 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005616
5617 if (STRCMP(items[4], ".") != 0)
5618 {
5619 char_u buf[MAXLINELEN];
5620
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005621 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005622 if (*items[0] == 'P')
5623 sprintf((char *)buf, "^%s", items[4]);
5624 else
5625 sprintf((char *)buf, "%s$", items[4]);
5626 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005627 RE_MAGIC + RE_STRING + RE_STRICT);
5628 if (aff_entry->ae_prog == NULL)
5629 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5630 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005631 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005632
5633 /* For postponed prefixes we need an entry in si_prefcond
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005634 * for the condition. Use an existing one if possible.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005635 * Can't be done for an affix with flags, ignoring
5636 * COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005637 if (*items[0] == 'P' && aff->af_pfxpostpone
5638 && aff_entry->ae_flags == NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005639 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005640 /* When the chop string is one lower-case letter and
5641 * the add string ends in the upper-case letter we set
5642 * the "upper" flag, clear "ae_chop" and remove the
5643 * letters from "ae_add". The condition must either
5644 * be empty or start with the same letter. */
5645 if (aff_entry->ae_chop != NULL
5646 && aff_entry->ae_add != NULL
5647#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005648 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00005649 aff_entry->ae_chop)] == NUL
5650#else
5651 && aff_entry->ae_chop[1] == NUL
5652#endif
5653 )
5654 {
5655 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005656
Bram Moolenaar53805d12005-08-01 07:08:33 +00005657 c = PTR2CHAR(aff_entry->ae_chop);
5658 c_up = SPELL_TOUPPER(c);
5659 if (c_up != c
5660 && (aff_entry->ae_cond == NULL
5661 || PTR2CHAR(aff_entry->ae_cond) == c))
5662 {
5663 p = aff_entry->ae_add
5664 + STRLEN(aff_entry->ae_add);
5665 mb_ptr_back(aff_entry->ae_add, p);
5666 if (PTR2CHAR(p) == c_up)
5667 {
5668 upper = TRUE;
5669 aff_entry->ae_chop = NULL;
5670 *p = NUL;
5671
5672 /* The condition is matched with the
5673 * actual word, thus must check for the
5674 * upper-case letter. */
5675 if (aff_entry->ae_cond != NULL)
5676 {
5677 char_u buf[MAXLINELEN];
5678#ifdef FEAT_MBYTE
5679 if (has_mbyte)
5680 {
5681 onecap_copy(items[4], buf, TRUE);
5682 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005683 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005684 }
5685 else
5686#endif
5687 *aff_entry->ae_cond = c_up;
5688 if (aff_entry->ae_cond != NULL)
5689 {
5690 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005691 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005692 vim_free(aff_entry->ae_prog);
5693 aff_entry->ae_prog = vim_regcomp(
5694 buf, RE_MAGIC + RE_STRING);
5695 }
5696 }
5697 }
5698 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005699 }
5700
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005701 if (aff_entry->ae_chop == NULL
5702 && aff_entry->ae_flags == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005703 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005704 int idx;
5705 char_u **pp;
5706 int n;
5707
Bram Moolenaar6de68532005-08-24 22:08:48 +00005708 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005709 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5710 --idx)
5711 {
5712 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5713 if (str_equal(p, aff_entry->ae_cond))
5714 break;
5715 }
5716 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5717 {
5718 /* Not found, add a new condition. */
5719 idx = spin->si_prefcond.ga_len++;
5720 pp = ((char_u **)spin->si_prefcond.ga_data)
5721 + idx;
5722 if (aff_entry->ae_cond == NULL)
5723 *pp = NULL;
5724 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005725 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005726 aff_entry->ae_cond);
5727 }
5728
5729 /* Add the prefix to the prefix tree. */
5730 if (aff_entry->ae_add == NULL)
5731 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005732 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005733 p = aff_entry->ae_add;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005734
Bram Moolenaar53805d12005-08-01 07:08:33 +00005735 /* PFX_FLAGS is a negative number, so that
5736 * tree_add_word() knows this is the prefix tree. */
5737 n = PFX_FLAGS;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005738 if (!cur_aff->ah_combine)
5739 n |= WFP_NC;
5740 if (upper)
5741 n |= WFP_UP;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005742 if (aff_entry->ae_comppermit)
5743 n |= WFP_COMPPERMIT;
5744 if (aff_entry->ae_compforbid)
5745 n |= WFP_COMPFORBID;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005746 tree_add_word(spin, p, spin->si_prefroot, n,
5747 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005748 did_postpone_prefix = TRUE;
5749 }
5750
5751 /* Didn't actually use ah_newID, backup si_newprefID. */
5752 if (aff_todo == 0 && !did_postpone_prefix)
5753 {
5754 --spin->si_newprefID;
5755 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005756 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005757 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005758 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005759 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005760 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2
5761 && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005762 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005763 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005764 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005765 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2
5766 && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005767 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005768 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005769 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005770 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2
5771 && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005772 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005773 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005774 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005775 else if ((STRCMP(items[0], "REP") == 0
5776 || STRCMP(items[0], "REPSAL") == 0)
5777 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005778 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005779 /* Ignore REP/REPSAL count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005780 if (!isdigit(*items[1]))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005781 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005782 fname, lnum);
5783 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005784 else if ((STRCMP(items[0], "REP") == 0
5785 || STRCMP(items[0], "REPSAL") == 0)
5786 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005787 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005788 /* REP/REPSAL item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005789 /* Myspell ignores extra arguments, we require it starts with
5790 * # to detect mistakes. */
5791 if (itemcnt > 3 && items[3][0] != '#')
5792 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005793 if (items[0][3] == 'S' ? do_repsal : do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005794 {
5795 /* Replace underscore with space (can't include a space
5796 * directly). */
5797 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5798 if (*p == '_')
5799 *p = ' ';
5800 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5801 if (*p == '_')
5802 *p = ' ';
Bram Moolenaar4770d092006-01-12 23:22:24 +00005803 add_fromto(spin, items[0][3] == 'S'
5804 ? &spin->si_repsal
5805 : &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005806 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005807 }
5808 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
5809 {
5810 /* MAP item or count */
5811 if (!found_map)
5812 {
5813 /* First line contains the count. */
5814 found_map = TRUE;
5815 if (!isdigit(*items[1]))
5816 smsg((char_u *)_("Expected MAP count in %s line %d"),
5817 fname, lnum);
5818 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005819 else if (do_mapline)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005820 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005821 int c;
5822
5823 /* Check that every character appears only once. */
5824 for (p = items[1]; *p != NUL; )
5825 {
5826#ifdef FEAT_MBYTE
5827 c = mb_ptr2char_adv(&p);
5828#else
5829 c = *p++;
5830#endif
5831 if ((spin->si_map.ga_len > 0
5832 && vim_strchr(spin->si_map.ga_data, c)
5833 != NULL)
5834 || vim_strchr(p, c) != NULL)
5835 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5836 fname, lnum);
5837 }
5838
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005839 /* We simply concatenate all the MAP strings, separated by
5840 * slashes. */
5841 ga_concat(&spin->si_map, items[1]);
5842 ga_append(&spin->si_map, '/');
5843 }
5844 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005845 /* Accept "SAL from to" and "SAL from to # comment". */
5846 else if (STRCMP(items[0], "SAL") == 0
5847 && (itemcnt == 3 || (itemcnt > 3 && items[3][0] == '#')))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005848 {
5849 if (do_sal)
5850 {
5851 /* SAL item (sounds-a-like)
5852 * Either one of the known keys or a from-to pair. */
5853 if (STRCMP(items[1], "followup") == 0)
5854 spin->si_followup = sal_to_bool(items[2]);
5855 else if (STRCMP(items[1], "collapse_result") == 0)
5856 spin->si_collapse = sal_to_bool(items[2]);
5857 else if (STRCMP(items[1], "remove_accents") == 0)
5858 spin->si_rem_accents = sal_to_bool(items[2]);
5859 else
5860 /* when "to" is "_" it means empty */
5861 add_fromto(spin, &spin->si_sal, items[1],
5862 STRCMP(items[2], "_") == 0 ? (char_u *)""
5863 : items[2]);
5864 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005865 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005866 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005867 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005868 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005869 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005870 }
5871 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005872 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005873 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005874 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005875 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005876 else if (STRCMP(items[0], "COMMON") == 0)
5877 {
5878 int i;
5879
5880 for (i = 1; i < itemcnt; ++i)
5881 {
5882 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
5883 items[i])))
5884 {
5885 p = vim_strsave(items[i]);
5886 if (p == NULL)
5887 break;
5888 hash_add(&spin->si_commonwords, p);
5889 }
5890 }
5891 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005892 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005893 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005894 fname, lnum, items[0]);
5895 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005896 }
5897
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005898 if (fol != NULL || low != NULL || upp != NULL)
5899 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005900 if (spin->si_clear_chartab)
5901 {
5902 /* Clear the char type tables, don't want to use any of the
5903 * currently used spell properties. */
5904 init_spell_chartab();
5905 spin->si_clear_chartab = FALSE;
5906 }
5907
Bram Moolenaar3982c542005-06-08 21:56:31 +00005908 /*
5909 * Don't write a word table for an ASCII file, so that we don't check
5910 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005911 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00005912 * mb_get_class(), the list of chars in the file will be incomplete.
5913 */
5914 if (!spin->si_ascii
5915#ifdef FEAT_MBYTE
5916 && !enc_utf8
5917#endif
5918 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005919 {
5920 if (fol == NULL || low == NULL || upp == NULL)
5921 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
5922 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00005923 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005924 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005925
5926 vim_free(fol);
5927 vim_free(low);
5928 vim_free(upp);
5929 }
5930
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005931 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005932 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005933 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005934 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
Bram Moolenaar6de68532005-08-24 22:08:48 +00005935 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005936 }
5937
Bram Moolenaar6de68532005-08-24 22:08:48 +00005938 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005939 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005940 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
5941 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005942 }
5943
Bram Moolenaar6de68532005-08-24 22:08:48 +00005944 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005945 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005946 if (syllable == NULL)
5947 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
5948 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
5949 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005950 }
5951
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005952 if (compoptions != 0)
5953 {
5954 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
5955 spin->si_compoptions |= compoptions;
5956 }
5957
Bram Moolenaar6de68532005-08-24 22:08:48 +00005958 if (compflags != NULL)
5959 process_compflags(spin, aff, compflags);
5960
5961 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005962 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005963 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005964 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005965 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005966 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005967 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005968 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005969 MSG(_("Too many posponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005970 }
5971
Bram Moolenaar6de68532005-08-24 22:08:48 +00005972 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005973 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005974 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
5975 spin->si_syllable = syllable;
5976 }
5977
5978 if (sofofrom != NULL || sofoto != NULL)
5979 {
5980 if (sofofrom == NULL || sofoto == NULL)
5981 smsg((char_u *)_("Missing SOFO%s line in %s"),
5982 sofofrom == NULL ? "FROM" : "TO", fname);
5983 else if (spin->si_sal.ga_len > 0)
5984 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005985 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005986 {
5987 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
5988 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
5989 spin->si_sofofr = sofofrom;
5990 spin->si_sofoto = sofoto;
5991 }
5992 }
5993
5994 if (midword != NULL)
5995 {
5996 aff_check_string(spin->si_midword, midword, "MIDWORD");
5997 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005998 }
5999
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006000 vim_free(pc);
6001 fclose(fd);
6002 return aff;
6003}
6004
6005/*
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006006 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
6007 * ae_flags to ae_comppermit and ae_compforbid.
6008 */
6009 static void
6010aff_process_flags(affile, entry)
6011 afffile_T *affile;
6012 affentry_T *entry;
6013{
6014 char_u *p;
6015 char_u *prevp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006016 unsigned flag;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006017
6018 if (entry->ae_flags != NULL
6019 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
6020 {
6021 for (p = entry->ae_flags; *p != NUL; )
6022 {
6023 prevp = p;
6024 flag = get_affitem(affile->af_flagtype, &p);
6025 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
6026 {
6027 mch_memmove(prevp, p, STRLEN(p) + 1);
6028 p = prevp;
6029 if (flag == affile->af_comppermit)
6030 entry->ae_comppermit = TRUE;
6031 else
6032 entry->ae_compforbid = TRUE;
6033 }
6034 if (affile->af_flagtype == AFT_NUM && *p == ',')
6035 ++p;
6036 }
6037 if (*entry->ae_flags == NUL)
6038 entry->ae_flags = NULL; /* nothing left */
6039 }
6040}
6041
6042/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006043 * Return TRUE if "s" is the name of an info item in the affix file.
6044 */
6045 static int
6046spell_info_item(s)
6047 char_u *s;
6048{
6049 return STRCMP(s, "NAME") == 0
6050 || STRCMP(s, "HOME") == 0
6051 || STRCMP(s, "VERSION") == 0
6052 || STRCMP(s, "AUTHOR") == 0
6053 || STRCMP(s, "EMAIL") == 0
6054 || STRCMP(s, "COPYRIGHT") == 0;
6055}
6056
6057/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006058 * Turn an affix flag name into a number, according to the FLAG type.
6059 * returns zero for failure.
6060 */
6061 static unsigned
6062affitem2flag(flagtype, item, fname, lnum)
6063 int flagtype;
6064 char_u *item;
6065 char_u *fname;
6066 int lnum;
6067{
6068 unsigned res;
6069 char_u *p = item;
6070
6071 res = get_affitem(flagtype, &p);
6072 if (res == 0)
6073 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006074 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006075 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
6076 fname, lnum, item);
6077 else
6078 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
6079 fname, lnum, item);
6080 }
6081 if (*p != NUL)
6082 {
6083 smsg((char_u *)_(e_affname), fname, lnum, item);
6084 return 0;
6085 }
6086
6087 return res;
6088}
6089
6090/*
6091 * Get one affix name from "*pp" and advance the pointer.
6092 * Returns zero for an error, still advances the pointer then.
6093 */
6094 static unsigned
6095get_affitem(flagtype, pp)
6096 int flagtype;
6097 char_u **pp;
6098{
6099 int res;
6100
Bram Moolenaar95529562005-08-25 21:21:38 +00006101 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006102 {
6103 if (!VIM_ISDIGIT(**pp))
6104 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006105 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006106 return 0;
6107 }
6108 res = getdigits(pp);
6109 }
6110 else
6111 {
6112#ifdef FEAT_MBYTE
6113 res = mb_ptr2char_adv(pp);
6114#else
6115 res = *(*pp)++;
6116#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006117 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00006118 && res >= 'A' && res <= 'Z'))
6119 {
6120 if (**pp == NUL)
6121 return 0;
6122#ifdef FEAT_MBYTE
6123 res = mb_ptr2char_adv(pp) + (res << 16);
6124#else
6125 res = *(*pp)++ + (res << 16);
6126#endif
6127 }
6128 }
6129 return res;
6130}
6131
6132/*
6133 * Process the "compflags" string used in an affix file and append it to
6134 * spin->si_compflags.
6135 * The processing involves changing the affix names to ID numbers, so that
6136 * they fit in one byte.
6137 */
6138 static void
6139process_compflags(spin, aff, compflags)
6140 spellinfo_T *spin;
6141 afffile_T *aff;
6142 char_u *compflags;
6143{
6144 char_u *p;
6145 char_u *prevp;
6146 unsigned flag;
6147 compitem_T *ci;
6148 int id;
6149 int len;
6150 char_u *tp;
6151 char_u key[AH_KEY_LEN];
6152 hashitem_T *hi;
6153
6154 /* Make room for the old and the new compflags, concatenated with a / in
6155 * between. Processing it makes it shorter, but we don't know by how
6156 * much, thus allocate the maximum. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006157 len = (int)STRLEN(compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006158 if (spin->si_compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006159 len += (int)STRLEN(spin->si_compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006160 p = getroom(spin, len, FALSE);
6161 if (p == NULL)
6162 return;
6163 if (spin->si_compflags != NULL)
6164 {
6165 STRCPY(p, spin->si_compflags);
6166 STRCAT(p, "/");
6167 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00006168 spin->si_compflags = p;
6169 tp = p + STRLEN(p);
6170
6171 for (p = compflags; *p != NUL; )
6172 {
6173 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
6174 /* Copy non-flag characters directly. */
6175 *tp++ = *p++;
6176 else
6177 {
6178 /* First get the flag number, also checks validity. */
6179 prevp = p;
6180 flag = get_affitem(aff->af_flagtype, &p);
6181 if (flag != 0)
6182 {
6183 /* Find the flag in the hashtable. If it was used before, use
6184 * the existing ID. Otherwise add a new entry. */
6185 vim_strncpy(key, prevp, p - prevp);
6186 hi = hash_find(&aff->af_comp, key);
6187 if (!HASHITEM_EMPTY(hi))
6188 id = HI2CI(hi)->ci_newID;
6189 else
6190 {
6191 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
6192 if (ci == NULL)
6193 break;
6194 STRCPY(ci->ci_key, key);
6195 ci->ci_flag = flag;
6196 /* Avoid using a flag ID that has a special meaning in a
6197 * regexp (also inside []). */
6198 do
6199 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006200 check_renumber(spin);
6201 id = spin->si_newcompID--;
6202 } while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006203 ci->ci_newID = id;
6204 hash_add(&aff->af_comp, ci->ci_key);
6205 }
6206 *tp++ = id;
6207 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006208 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006209 ++p;
6210 }
6211 }
6212
6213 *tp = NUL;
6214}
6215
6216/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006217 * Check that the new IDs for postponed affixes and compounding don't overrun
6218 * each other. We have almost 255 available, but start at 0-127 to avoid
6219 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
6220 * When that is used up an error message is given.
6221 */
6222 static void
6223check_renumber(spin)
6224 spellinfo_T *spin;
6225{
6226 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
6227 {
6228 spin->si_newprefID = 127;
6229 spin->si_newcompID = 255;
6230 }
6231}
6232
6233/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006234 * Return TRUE if flag "flag" appears in affix list "afflist".
6235 */
6236 static int
6237flag_in_afflist(flagtype, afflist, flag)
6238 int flagtype;
6239 char_u *afflist;
6240 unsigned flag;
6241{
6242 char_u *p;
6243 unsigned n;
6244
6245 switch (flagtype)
6246 {
6247 case AFT_CHAR:
6248 return vim_strchr(afflist, flag) != NULL;
6249
Bram Moolenaar95529562005-08-25 21:21:38 +00006250 case AFT_CAPLONG:
6251 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006252 for (p = afflist; *p != NUL; )
6253 {
6254#ifdef FEAT_MBYTE
6255 n = mb_ptr2char_adv(&p);
6256#else
6257 n = *p++;
6258#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006259 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00006260 && *p != NUL)
6261#ifdef FEAT_MBYTE
6262 n = mb_ptr2char_adv(&p) + (n << 16);
6263#else
6264 n = *p++ + (n << 16);
6265#endif
6266 if (n == flag)
6267 return TRUE;
6268 }
6269 break;
6270
Bram Moolenaar95529562005-08-25 21:21:38 +00006271 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006272 for (p = afflist; *p != NUL; )
6273 {
6274 n = getdigits(&p);
6275 if (n == flag)
6276 return TRUE;
6277 if (*p != NUL) /* skip over comma */
6278 ++p;
6279 }
6280 break;
6281 }
6282 return FALSE;
6283}
6284
6285/*
6286 * Give a warning when "spinval" and "affval" numbers are set and not the same.
6287 */
6288 static void
6289aff_check_number(spinval, affval, name)
6290 int spinval;
6291 int affval;
6292 char *name;
6293{
6294 if (spinval != 0 && spinval != affval)
6295 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6296}
6297
6298/*
6299 * Give a warning when "spinval" and "affval" strings are set and not the same.
6300 */
6301 static void
6302aff_check_string(spinval, affval, name)
6303 char_u *spinval;
6304 char_u *affval;
6305 char *name;
6306{
6307 if (spinval != NULL && STRCMP(spinval, affval) != 0)
6308 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6309}
6310
6311/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006312 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
6313 * NULL as equal.
6314 */
6315 static int
6316str_equal(s1, s2)
6317 char_u *s1;
6318 char_u *s2;
6319{
6320 if (s1 == NULL || s2 == NULL)
6321 return s1 == s2;
6322 return STRCMP(s1, s2) == 0;
6323}
6324
6325/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006326 * Add a from-to item to "gap". Used for REP and SAL items.
6327 * They are stored case-folded.
6328 */
6329 static void
6330add_fromto(spin, gap, from, to)
6331 spellinfo_T *spin;
6332 garray_T *gap;
6333 char_u *from;
6334 char_u *to;
6335{
6336 fromto_T *ftp;
6337 char_u word[MAXWLEN];
6338
6339 if (ga_grow(gap, 1) == OK)
6340 {
6341 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006342 (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006343 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006344 (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006345 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006346 ++gap->ga_len;
6347 }
6348}
6349
6350/*
6351 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6352 */
6353 static int
6354sal_to_bool(s)
6355 char_u *s;
6356{
6357 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
6358}
6359
6360/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00006361 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6362 * When "s" is NULL FALSE is returned.
6363 */
6364 static int
6365has_non_ascii(s)
6366 char_u *s;
6367{
6368 char_u *p;
6369
6370 if (s != NULL)
6371 for (p = s; *p != NUL; ++p)
6372 if (*p >= 128)
6373 return TRUE;
6374 return FALSE;
6375}
6376
6377/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006378 * Free the structure filled by spell_read_aff().
6379 */
6380 static void
6381spell_free_aff(aff)
6382 afffile_T *aff;
6383{
6384 hashtab_T *ht;
6385 hashitem_T *hi;
6386 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006387 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006388 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006389
6390 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006391
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006392 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006393 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
6394 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006395 todo = (int)ht->ht_used;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006396 for (hi = ht->ht_array; todo > 0; ++hi)
6397 {
6398 if (!HASHITEM_EMPTY(hi))
6399 {
6400 --todo;
6401 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006402 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
6403 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006404 }
6405 }
6406 if (ht == &aff->af_suff)
6407 break;
6408 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006409
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006410 hash_clear(&aff->af_pref);
6411 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006412 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006413}
6414
6415/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006416 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006417 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006418 */
6419 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006420spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006421 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006422 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006423 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006424{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006425 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006426 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006427 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006428 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006429 char_u store_afflist[MAXWLEN];
6430 int pfxlen;
6431 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006432 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006433 char_u *pc;
6434 char_u *w;
6435 int l;
6436 hash_T hash;
6437 hashitem_T *hi;
6438 FILE *fd;
6439 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006440 int non_ascii = 0;
6441 int retval = OK;
6442 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006443 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006444 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006445
Bram Moolenaar51485f02005-06-04 21:55:20 +00006446 /*
6447 * Open the file.
6448 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006449 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006450 if (fd == NULL)
6451 {
6452 EMSG2(_(e_notopen), fname);
6453 return FAIL;
6454 }
6455
Bram Moolenaar51485f02005-06-04 21:55:20 +00006456 /* The hashtable is only used to detect duplicated words. */
6457 hash_init(&ht);
6458
Bram Moolenaar4770d092006-01-12 23:22:24 +00006459 vim_snprintf((char *)IObuff, IOSIZE,
6460 _("Reading dictionary file %s ..."), fname);
6461 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006462
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006463 /* start with a message for the first line */
6464 spin->si_msg_count = 999999;
6465
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006466 /* Read and ignore the first line: word count. */
6467 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006468 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006469 EMSG2(_("E760: No word count in %s"), fname);
6470
6471 /*
6472 * Read all the lines in the file one by one.
6473 * The words are converted to 'encoding' here, before being added to
6474 * the hashtable.
6475 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006476 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006477 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006478 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006479 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00006480 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006481 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006482
Bram Moolenaar51485f02005-06-04 21:55:20 +00006483 /* Remove CR, LF and white space from the end. White space halfway
6484 * the word is kept to allow e.g., "et al.". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006485 l = (int)STRLEN(line);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006486 while (l > 0 && line[l - 1] <= ' ')
6487 --l;
6488 if (l == 0)
6489 continue; /* empty line */
6490 line[l] = NUL;
6491
Bram Moolenaarb765d632005-06-07 21:00:02 +00006492#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006493 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006494 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006495 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006496 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006497 if (pc == NULL)
6498 {
6499 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6500 fname, lnum, line);
6501 continue;
6502 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006503 w = pc;
6504 }
6505 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006506#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006507 {
6508 pc = NULL;
6509 w = line;
6510 }
6511
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006512 /* Truncate the word at the "/", set "afflist" to what follows.
6513 * Replace "\/" by "/" and "\\" by "\". */
6514 afflist = NULL;
6515 for (p = w; *p != NUL; mb_ptr_adv(p))
6516 {
6517 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
6518 mch_memmove(p, p + 1, STRLEN(p));
6519 else if (*p == '/')
6520 {
6521 *p = NUL;
6522 afflist = p + 1;
6523 break;
6524 }
6525 }
6526
6527 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6528 if (spin->si_ascii && has_non_ascii(w))
6529 {
6530 ++non_ascii;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006531 vim_free(pc);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006532 continue;
6533 }
6534
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006535 /* This takes time, print a message every 10000 words. */
6536 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006537 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006538 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006539 vim_snprintf((char *)message, sizeof(message),
6540 _("line %6d, word %6d - %s"),
6541 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6542 msg_start();
6543 msg_puts_long_attr(message, 0);
6544 msg_clr_eos();
6545 msg_didout = FALSE;
6546 msg_col = 0;
6547 out_flush();
6548 }
6549
Bram Moolenaar51485f02005-06-04 21:55:20 +00006550 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006551 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006552 if (dw == NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006553 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006554 retval = FAIL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006555 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006556 break;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006557 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006558
Bram Moolenaar51485f02005-06-04 21:55:20 +00006559 hash = hash_hash(dw);
6560 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006561 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006562 {
6563 if (p_verbose > 0)
6564 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006565 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006566 else if (duplicate == 0)
6567 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6568 fname, lnum, dw);
6569 ++duplicate;
6570 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006571 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006572 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006573
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006574 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006575 store_afflist[0] = NUL;
6576 pfxlen = 0;
6577 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006578 if (afflist != NULL)
6579 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006580 /* Extract flags from the affix list. */
6581 flags |= get_affix_flags(affile, afflist);
6582
Bram Moolenaar6de68532005-08-24 22:08:48 +00006583 if (affile->af_needaffix != 0 && flag_in_afflist(
6584 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006585 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006586
6587 if (affile->af_pfxpostpone)
6588 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006589 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006590
Bram Moolenaar5195e452005-08-19 20:32:47 +00006591 if (spin->si_compflags != NULL)
6592 /* Need to store the list of compound flags with the word.
6593 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006594 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006595 }
6596
Bram Moolenaar51485f02005-06-04 21:55:20 +00006597 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006598 if (store_word(spin, dw, flags, spin->si_region,
6599 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006600 retval = FAIL;
6601
6602 if (afflist != NULL)
6603 {
6604 /* Find all matching suffixes and add the resulting words.
6605 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006606 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006607 &affile->af_suff, &affile->af_pref,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006608 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006609 retval = FAIL;
6610
6611 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006612 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006613 &affile->af_pref, NULL,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006614 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006615 retval = FAIL;
6616 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006617
6618 vim_free(pc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006619 }
6620
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006621 if (duplicate > 0)
6622 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006623 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006624 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6625 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006626 hash_clear(&ht);
6627
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006628 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006629 return retval;
6630}
6631
6632/*
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006633 * Check for affix flags in "afflist" that are turned into word flags.
6634 * Return WF_ flags.
6635 */
6636 static int
6637get_affix_flags(affile, afflist)
6638 afffile_T *affile;
6639 char_u *afflist;
6640{
6641 int flags = 0;
6642
6643 if (affile->af_keepcase != 0 && flag_in_afflist(
6644 affile->af_flagtype, afflist, affile->af_keepcase))
6645 flags |= WF_KEEPCAP | WF_FIXCAP;
6646 if (affile->af_rare != 0 && flag_in_afflist(
6647 affile->af_flagtype, afflist, affile->af_rare))
6648 flags |= WF_RARE;
6649 if (affile->af_bad != 0 && flag_in_afflist(
6650 affile->af_flagtype, afflist, affile->af_bad))
6651 flags |= WF_BANNED;
6652 if (affile->af_needcomp != 0 && flag_in_afflist(
6653 affile->af_flagtype, afflist, affile->af_needcomp))
6654 flags |= WF_NEEDCOMP;
6655 if (affile->af_comproot != 0 && flag_in_afflist(
6656 affile->af_flagtype, afflist, affile->af_comproot))
6657 flags |= WF_COMPROOT;
6658 if (affile->af_nosuggest != 0 && flag_in_afflist(
6659 affile->af_flagtype, afflist, affile->af_nosuggest))
6660 flags |= WF_NOSUGGEST;
6661 return flags;
6662}
6663
6664/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006665 * Get the list of prefix IDs from the affix list "afflist".
6666 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006667 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6668 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006669 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006670 static int
6671get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006672 afffile_T *affile;
6673 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006674 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006675{
6676 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006677 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006678 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006679 int id;
6680 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006681 hashitem_T *hi;
6682
Bram Moolenaar6de68532005-08-24 22:08:48 +00006683 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006684 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006685 prevp = p;
6686 if (get_affitem(affile->af_flagtype, &p) != 0)
6687 {
6688 /* A flag is a postponed prefix flag if it appears in "af_pref"
6689 * and it's ID is not zero. */
6690 vim_strncpy(key, prevp, p - prevp);
6691 hi = hash_find(&affile->af_pref, key);
6692 if (!HASHITEM_EMPTY(hi))
6693 {
6694 id = HI2AH(hi)->ah_newID;
6695 if (id != 0)
6696 store_afflist[cnt++] = id;
6697 }
6698 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006699 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006700 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006701 }
6702
Bram Moolenaar5195e452005-08-19 20:32:47 +00006703 store_afflist[cnt] = NUL;
6704 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006705}
6706
6707/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006708 * Get the list of compound IDs from the affix list "afflist" that are used
6709 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006710 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006711 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006712 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00006713get_compflags(affile, afflist, store_afflist)
6714 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006715 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006716 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006717{
6718 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006719 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006720 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006721 char_u key[AH_KEY_LEN];
6722 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006723
Bram Moolenaar6de68532005-08-24 22:08:48 +00006724 for (p = afflist; *p != NUL; )
6725 {
6726 prevp = p;
6727 if (get_affitem(affile->af_flagtype, &p) != 0)
6728 {
6729 /* A flag is a compound flag if it appears in "af_comp". */
6730 vim_strncpy(key, prevp, p - prevp);
6731 hi = hash_find(&affile->af_comp, key);
6732 if (!HASHITEM_EMPTY(hi))
6733 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6734 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006735 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006736 ++p;
6737 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006738
Bram Moolenaar5195e452005-08-19 20:32:47 +00006739 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006740}
6741
6742/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006743 * Apply affixes to a word and store the resulting words.
6744 * "ht" is the hashtable with affentry_T that need to be applied, either
6745 * prefixes or suffixes.
6746 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6747 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006748 *
6749 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006750 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006751 static int
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006752store_aff_word(spin, word, afflist, affile, ht, xht, condit, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006753 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006754 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006755 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006756 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006757 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006758 hashtab_T *ht;
6759 hashtab_T *xht;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006760 int condit; /* CONDIT_SUF et al. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006761 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006762 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006763 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
6764 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006765{
6766 int todo;
6767 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006768 affheader_T *ah;
6769 affentry_T *ae;
6770 regmatch_T regmatch;
6771 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006772 int retval = OK;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006773 int i, j;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006774 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006775 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006776 char_u *use_pfxlist;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006777 int use_pfxlen;
6778 int need_affix;
6779 char_u store_afflist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006780 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006781 size_t wordlen = STRLEN(word);
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006782 int use_condit;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006783
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006784 todo = (int)ht->ht_used;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006785 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006786 {
6787 if (!HASHITEM_EMPTY(hi))
6788 {
6789 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006790 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006791
Bram Moolenaar51485f02005-06-04 21:55:20 +00006792 /* Check that the affix combines, if required, and that the word
6793 * supports this affix. */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006794 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
6795 && flag_in_afflist(affile->af_flagtype, afflist,
6796 ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006797 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006798 /* Loop over all affix entries with this name. */
6799 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006800 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006801 /* Check the condition. It's not logical to match case
6802 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006803 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006804 * Another requirement from Myspell is that the chop
6805 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006806 * For prefixes, when "PFXPOSTPONE" was used, only do
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006807 * prefixes with a chop string and/or flags.
6808 * When a previously added affix had CIRCUMFIX this one
6809 * must have it too, if it had not then this one must not
6810 * have one either. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006811 regmatch.regprog = ae->ae_prog;
6812 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006813 if ((xht != NULL || !affile->af_pfxpostpone
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006814 || ae->ae_chop != NULL
6815 || ae->ae_flags != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006816 && (ae->ae_chop == NULL
6817 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006818 && (ae->ae_prog == NULL
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006819 || vim_regexec(&regmatch, word, (colnr_T)0))
6820 && (((condit & CONDIT_CFIX) == 0)
6821 == ((condit & CONDIT_AFF) == 0
6822 || ae->ae_flags == NULL
6823 || !flag_in_afflist(affile->af_flagtype,
6824 ae->ae_flags, affile->af_circumfix))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006825 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006826 /* Match. Remove the chop and add the affix. */
6827 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006828 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006829 /* prefix: chop/add at the start of the word */
6830 if (ae->ae_add == NULL)
6831 *newword = NUL;
6832 else
6833 STRCPY(newword, ae->ae_add);
6834 p = word;
6835 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006836 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006837 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006838#ifdef FEAT_MBYTE
6839 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006840 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006841 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006842 for ( ; i > 0; --i)
6843 mb_ptr_adv(p);
6844 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006845 else
6846#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006847 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006848 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006849 STRCAT(newword, p);
6850 }
6851 else
6852 {
6853 /* suffix: chop/add at the end of the word */
6854 STRCPY(newword, word);
6855 if (ae->ae_chop != NULL)
6856 {
6857 /* Remove chop string. */
6858 p = newword + STRLEN(newword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006859 i = (int)MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006860 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006861 mb_ptr_back(newword, p);
6862 *p = NUL;
6863 }
6864 if (ae->ae_add != NULL)
6865 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006866 }
6867
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006868 use_flags = flags;
6869 use_pfxlist = pfxlist;
6870 use_pfxlen = pfxlen;
6871 need_affix = FALSE;
6872 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
6873 if (ae->ae_flags != NULL)
6874 {
6875 /* Extract flags from the affix list. */
6876 use_flags |= get_affix_flags(affile, ae->ae_flags);
6877
6878 if (affile->af_needaffix != 0 && flag_in_afflist(
6879 affile->af_flagtype, ae->ae_flags,
6880 affile->af_needaffix))
6881 need_affix = TRUE;
6882
6883 /* When there is a CIRCUMFIX flag the other affix
6884 * must also have it and we don't add the word
6885 * with one affix. */
6886 if (affile->af_circumfix != 0 && flag_in_afflist(
6887 affile->af_flagtype, ae->ae_flags,
6888 affile->af_circumfix))
6889 {
6890 use_condit |= CONDIT_CFIX;
6891 if ((condit & CONDIT_CFIX) == 0)
6892 need_affix = TRUE;
6893 }
6894
6895 if (affile->af_pfxpostpone
6896 || spin->si_compflags != NULL)
6897 {
6898 if (affile->af_pfxpostpone)
6899 /* Get prefix IDS from the affix list. */
6900 use_pfxlen = get_pfxlist(affile,
6901 ae->ae_flags, store_afflist);
6902 else
6903 use_pfxlen = 0;
6904 use_pfxlist = store_afflist;
6905
6906 /* Combine the prefix IDs. Avoid adding the
6907 * same ID twice. */
6908 for (i = 0; i < pfxlen; ++i)
6909 {
6910 for (j = 0; j < use_pfxlen; ++j)
6911 if (pfxlist[i] == use_pfxlist[j])
6912 break;
6913 if (j == use_pfxlen)
6914 use_pfxlist[use_pfxlen++] = pfxlist[i];
6915 }
6916
6917 if (spin->si_compflags != NULL)
6918 /* Get compound IDS from the affix list. */
6919 get_compflags(affile, ae->ae_flags,
6920 use_pfxlist + use_pfxlen);
6921
6922 /* Combine the list of compound flags.
6923 * Concatenate them to the prefix IDs list.
6924 * Avoid adding the same ID twice. */
6925 for (i = pfxlen; pfxlist[i] != NUL; ++i)
6926 {
6927 for (j = use_pfxlen;
6928 use_pfxlist[j] != NUL; ++j)
6929 if (pfxlist[i] == use_pfxlist[j])
6930 break;
6931 if (use_pfxlist[j] == NUL)
6932 {
6933 use_pfxlist[j++] = pfxlist[i];
6934 use_pfxlist[j] = NUL;
6935 }
6936 }
6937 }
6938 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00006939
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006940 /* Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006941 * use the compound flags. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006942 if (use_pfxlist != NULL && ae->ae_compforbid)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006943 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006944 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006945 use_pfxlist = pfx_pfxlist;
6946 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006947
6948 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00006949 if (spin->si_prefroot != NULL
6950 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006951 {
6952 /* ... add a flag to indicate an affix was used. */
6953 use_flags |= WF_HAS_AFF;
6954
6955 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00006956 * affixes is not allowed. But do use the
6957 * compound flags after them. */
Bram Moolenaar18144c82006-04-12 21:52:12 +00006958 if (!ah->ah_combine && use_pfxlist != NULL)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006959 use_pfxlist += use_pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006960 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006961
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006962 /* When compounding is supported and there is no
6963 * "COMPOUNDPERMITFLAG" then forbid compounding on the
6964 * side where the affix is applied. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006965 if (spin->si_compflags != NULL && !ae->ae_comppermit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006966 {
6967 if (xht != NULL)
6968 use_flags |= WF_NOCOMPAFT;
6969 else
6970 use_flags |= WF_NOCOMPBEF;
6971 }
6972
Bram Moolenaar51485f02005-06-04 21:55:20 +00006973 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006974 if (store_word(spin, newword, use_flags,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006975 spin->si_region, use_pfxlist,
6976 need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006977 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006978
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006979 /* When added a prefix or a first suffix and the affix
6980 * has flags may add a(nother) suffix. RECURSIVE! */
6981 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
6982 if (store_aff_word(spin, newword, ae->ae_flags,
6983 affile, &affile->af_suff, xht,
6984 use_condit & (xht == NULL
6985 ? ~0 : ~CONDIT_SUF),
Bram Moolenaar5195e452005-08-19 20:32:47 +00006986 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006987 retval = FAIL;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006988
6989 /* When added a suffix and combining is allowed also
6990 * try adding a prefix additionally. Both for the
6991 * word flags and for the affix flags. RECURSIVE! */
6992 if (xht != NULL && ah->ah_combine)
6993 {
6994 if (store_aff_word(spin, newword,
6995 afflist, affile,
6996 xht, NULL, use_condit,
6997 use_flags, use_pfxlist,
6998 pfxlen) == FAIL
6999 || (ae->ae_flags != NULL
7000 && store_aff_word(spin, newword,
7001 ae->ae_flags, affile,
7002 xht, NULL, use_condit,
7003 use_flags, use_pfxlist,
7004 pfxlen) == FAIL))
7005 retval = FAIL;
7006 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007007 }
7008 }
7009 }
7010 }
7011 }
7012
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007013 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007014}
7015
7016/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007017 * Read a file with a list of words.
7018 */
7019 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007020spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007021 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007022 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007023{
7024 FILE *fd;
7025 long lnum = 0;
7026 char_u rline[MAXLINELEN];
7027 char_u *line;
7028 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007029 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007030 int l;
7031 int retval = OK;
7032 int did_word = FALSE;
7033 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007034 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007035 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007036
7037 /*
7038 * Open the file.
7039 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007040 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007041 if (fd == NULL)
7042 {
7043 EMSG2(_(e_notopen), fname);
7044 return FAIL;
7045 }
7046
Bram Moolenaar4770d092006-01-12 23:22:24 +00007047 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
7048 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007049
7050 /*
7051 * Read all the lines in the file one by one.
7052 */
7053 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
7054 {
7055 line_breakcheck();
7056 ++lnum;
7057
7058 /* Skip comment lines. */
7059 if (*rline == '#')
7060 continue;
7061
7062 /* Remove CR, LF and white space from the end. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007063 l = (int)STRLEN(rline);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007064 while (l > 0 && rline[l - 1] <= ' ')
7065 --l;
7066 if (l == 0)
7067 continue; /* empty or blank line */
7068 rline[l] = NUL;
7069
Bram Moolenaar9c102382006-05-03 21:26:49 +00007070 /* Convert from "/encoding={encoding}" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007071 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007072#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00007073 if (spin->si_conv.vc_type != CONV_NONE)
7074 {
7075 pc = string_convert(&spin->si_conv, rline, NULL);
7076 if (pc == NULL)
7077 {
7078 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
7079 fname, lnum, rline);
7080 continue;
7081 }
7082 line = pc;
7083 }
7084 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00007085#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007086 {
7087 pc = NULL;
7088 line = rline;
7089 }
7090
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007091 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00007092 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007093 ++line;
7094 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007095 {
7096 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007097 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
7098 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007099 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007100 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
7101 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007102 else
7103 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007104#ifdef FEAT_MBYTE
7105 char_u *enc;
7106
Bram Moolenaar51485f02005-06-04 21:55:20 +00007107 /* Setup for conversion to 'encoding'. */
Bram Moolenaar9c102382006-05-03 21:26:49 +00007108 line += 9;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007109 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007110 if (enc != NULL && !spin->si_ascii
7111 && convert_setup(&spin->si_conv, enc,
7112 p_enc) == FAIL)
7113 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00007114 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007115 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007116 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007117#else
7118 smsg((char_u *)_("Conversion in %s not supported"), fname);
7119#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007120 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007121 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007122 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007123
Bram Moolenaar3982c542005-06-08 21:56:31 +00007124 if (STRNCMP(line, "regions=", 8) == 0)
7125 {
7126 if (spin->si_region_count > 1)
7127 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
7128 fname, lnum, line);
7129 else
7130 {
7131 line += 8;
7132 if (STRLEN(line) > 16)
7133 smsg((char_u *)_("Too many regions in %s line %d: %s"),
7134 fname, lnum, line);
7135 else
7136 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007137 spin->si_region_count = (int)STRLEN(line) / 2;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007138 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007139
7140 /* Adjust the mask for a word valid in all regions. */
7141 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007142 }
7143 }
7144 continue;
7145 }
7146
Bram Moolenaar7887d882005-07-01 22:33:52 +00007147 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
7148 fname, lnum, line - 1);
7149 continue;
7150 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007151
Bram Moolenaar7887d882005-07-01 22:33:52 +00007152 flags = 0;
7153 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007154
Bram Moolenaar7887d882005-07-01 22:33:52 +00007155 /* Check for flags and region after a slash. */
7156 p = vim_strchr(line, '/');
7157 if (p != NULL)
7158 {
7159 *p++ = NUL;
7160 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007161 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007162 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007163 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007164 else if (*p == '!') /* Bad, bad, wicked word. */
7165 flags |= WF_BANNED;
7166 else if (*p == '?') /* Rare word. */
7167 flags |= WF_RARE;
7168 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007169 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007170 if ((flags & WF_REGION) == 0) /* first one */
7171 regionmask = 0;
7172 flags |= WF_REGION;
7173
7174 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00007175 if (l > spin->si_region_count)
7176 {
7177 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00007178 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007179 break;
7180 }
7181 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007182 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007183 else
7184 {
7185 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
7186 fname, lnum, p);
7187 break;
7188 }
7189 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007190 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007191 }
7192
7193 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
7194 if (spin->si_ascii && has_non_ascii(line))
7195 {
7196 ++non_ascii;
7197 continue;
7198 }
7199
7200 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007201 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007202 {
7203 retval = FAIL;
7204 break;
7205 }
7206 did_word = TRUE;
7207 }
7208
7209 vim_free(pc);
7210 fclose(fd);
7211
Bram Moolenaar4770d092006-01-12 23:22:24 +00007212 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007213 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007214 vim_snprintf((char *)IObuff, IOSIZE,
7215 _("Ignored %d words with non-ASCII characters"), non_ascii);
7216 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007217 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007218
Bram Moolenaar51485f02005-06-04 21:55:20 +00007219 return retval;
7220}
7221
7222/*
7223 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007224 * This avoids calling free() for every little struct we use (and keeping
7225 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00007226 * The memory is cleared to all zeros.
7227 * Returns NULL when out of memory.
7228 */
7229 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007230getroom(spin, len, align)
7231 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007232 size_t len; /* length needed */
7233 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007234{
7235 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007236 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007237
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007238 if (align && bl != NULL)
7239 /* Round size up for alignment. On some systems structures need to be
7240 * aligned to the size of a pointer (e.g., SPARC). */
7241 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
7242 & ~(sizeof(char *) - 1);
7243
Bram Moolenaar51485f02005-06-04 21:55:20 +00007244 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
7245 {
7246 /* Allocate a block of memory. This is not freed until much later. */
7247 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
7248 if (bl == NULL)
7249 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007250 bl->sb_next = spin->si_blocks;
7251 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007252 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007253 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007254 }
7255
7256 p = bl->sb_data + bl->sb_used;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007257 bl->sb_used += (int)len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007258
7259 return p;
7260}
7261
7262/*
7263 * Make a copy of a string into memory allocated with getroom().
7264 */
7265 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007266getroom_save(spin, s)
7267 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007268 char_u *s;
7269{
7270 char_u *sc;
7271
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007272 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007273 if (sc != NULL)
7274 STRCPY(sc, s);
7275 return sc;
7276}
7277
7278
7279/*
7280 * Free the list of allocated sblock_T.
7281 */
7282 static void
7283free_blocks(bl)
7284 sblock_T *bl;
7285{
7286 sblock_T *next;
7287
7288 while (bl != NULL)
7289 {
7290 next = bl->sb_next;
7291 vim_free(bl);
7292 bl = next;
7293 }
7294}
7295
7296/*
7297 * Allocate the root of a word tree.
7298 */
7299 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007300wordtree_alloc(spin)
7301 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007302{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007303 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007304}
7305
7306/*
7307 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007308 * Always store it in the case-folded tree. For a keep-case word this is
7309 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
7310 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007311 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007312 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
7313 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007314 */
7315 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00007316store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007317 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007318 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007319 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007320 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007321 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007322 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007323{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007324 int len = (int)STRLEN(word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007325 int ct = captype(word, word + len);
7326 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007327 int res = OK;
7328 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007329
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007330 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007331 for (p = pfxlist; res == OK; ++p)
7332 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007333 if (!need_affix || (p != NULL && *p != NUL))
7334 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007335 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007336 if (p == NULL || *p == NUL)
7337 break;
7338 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007339 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007340
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007341 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00007342 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007343 for (p = pfxlist; res == OK; ++p)
7344 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007345 if (!need_affix || (p != NULL && *p != NUL))
7346 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007347 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007348 if (p == NULL || *p == NUL)
7349 break;
7350 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007351 ++spin->si_keepwcount;
7352 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007353 return res;
7354}
7355
7356/*
7357 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00007358 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007359 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007360 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007361 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007362 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007363tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007364 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007365 char_u *word;
7366 wordnode_T *root;
7367 int flags;
7368 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007369 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007370{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007371 wordnode_T *node = root;
7372 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007373 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007374 wordnode_T **prev = NULL;
7375 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007376
Bram Moolenaar51485f02005-06-04 21:55:20 +00007377 /* Add each byte of the word to the tree, including the NUL at the end. */
7378 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007379 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007380 /* When there is more than one reference to this node we need to make
7381 * a copy, so that we can modify it. Copy the whole list of siblings
7382 * (we don't optimize for a partly shared list of siblings). */
7383 if (node != NULL && node->wn_refs > 1)
7384 {
7385 --node->wn_refs;
7386 copyprev = prev;
7387 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
7388 {
7389 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007390 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007391 if (np == NULL)
7392 return FAIL;
7393 np->wn_child = copyp->wn_child;
7394 if (np->wn_child != NULL)
7395 ++np->wn_child->wn_refs; /* child gets extra ref */
7396 np->wn_byte = copyp->wn_byte;
7397 if (np->wn_byte == NUL)
7398 {
7399 np->wn_flags = copyp->wn_flags;
7400 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007401 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007402 }
7403
7404 /* Link the new node in the list, there will be one ref. */
7405 np->wn_refs = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007406 if (copyprev != NULL)
7407 *copyprev = np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007408 copyprev = &np->wn_sibling;
7409
7410 /* Let "node" point to the head of the copied list. */
7411 if (copyp == node)
7412 node = np;
7413 }
7414 }
7415
Bram Moolenaar51485f02005-06-04 21:55:20 +00007416 /* Look for the sibling that has the same character. They are sorted
7417 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007418 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007419 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007420 while (node != NULL
7421 && (node->wn_byte < word[i]
7422 || (node->wn_byte == NUL
7423 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007424 ? node->wn_affixID < (unsigned)affixID
7425 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007426 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007427 && (spin->si_sugtree
7428 ? (node->wn_region & 0xffff) < region
7429 : node->wn_affixID
7430 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007431 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007432 prev = &node->wn_sibling;
7433 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007434 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007435 if (node == NULL
7436 || node->wn_byte != word[i]
7437 || (word[i] == NUL
7438 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007439 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007440 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007441 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007442 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007443 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007444 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007445 if (np == NULL)
7446 return FAIL;
7447 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007448
7449 /* If "node" is NULL this is a new child or the end of the sibling
7450 * list: ref count is one. Otherwise use ref count of sibling and
7451 * make ref count of sibling one (matters when inserting in front
7452 * of the list of siblings). */
7453 if (node == NULL)
7454 np->wn_refs = 1;
7455 else
7456 {
7457 np->wn_refs = node->wn_refs;
7458 node->wn_refs = 1;
7459 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007460 *prev = np;
7461 np->wn_sibling = node;
7462 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007463 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007464
Bram Moolenaar51485f02005-06-04 21:55:20 +00007465 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007466 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007467 node->wn_flags = flags;
7468 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007469 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007470 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00007471 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007472 prev = &node->wn_child;
7473 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007474 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007475#ifdef SPELL_PRINTTREE
7476 smsg("Added \"%s\"", word);
7477 spell_print_tree(root->wn_sibling);
7478#endif
7479
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007480 /* count nr of words added since last message */
7481 ++spin->si_msg_count;
7482
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007483 if (spin->si_compress_cnt > 1)
7484 {
7485 if (--spin->si_compress_cnt == 1)
7486 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007487 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007488 }
7489
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007490 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007491 * When we have allocated lots of memory we need to compress the word tree
7492 * to free up some room. But compression is slow, and we might actually
7493 * need that room, thus only compress in the following situations:
7494 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00007495 * "compress_start" blocks.
7496 * 2. When compressed before and used "compress_inc" blocks before
7497 * adding "compress_added" words (si_compress_cnt > 1).
7498 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007499 * (si_compress_cnt == 1) and the number of free nodes drops below the
7500 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007501 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007502#ifndef SPELL_PRINTTREE
7503 if (spin->si_compress_cnt == 1
7504 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00007505 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007506#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007507 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007508 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00007509 * when the freed up room has been used and another "compress_inc"
7510 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007511 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007512 spin->si_blocks_cnt -= compress_inc;
7513 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007514
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007515 if (spin->si_verbose)
7516 {
7517 msg_start();
7518 msg_puts((char_u *)_(msg_compressing));
7519 msg_clr_eos();
7520 msg_didout = FALSE;
7521 msg_col = 0;
7522 out_flush();
7523 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007524
7525 /* Compress both trees. Either they both have many nodes, which makes
7526 * compression useful, or one of them is small, which means
Bram Moolenaar4770d092006-01-12 23:22:24 +00007527 * compression goes fast. But when filling the souldfold word tree
7528 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007529 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007530 if (affixID >= 0)
7531 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007532 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007533
7534 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007535}
7536
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007537/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007538 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7539 * Sets "sps_flags".
7540 */
7541 int
7542spell_check_msm()
7543{
7544 char_u *p = p_msm;
7545 long start = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007546 long incr = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007547 long added = 0;
7548
7549 if (!VIM_ISDIGIT(*p))
7550 return FAIL;
7551 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7552 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7553 if (*p != ',')
7554 return FAIL;
7555 ++p;
7556 if (!VIM_ISDIGIT(*p))
7557 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007558 incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007559 if (*p != ',')
7560 return FAIL;
7561 ++p;
7562 if (!VIM_ISDIGIT(*p))
7563 return FAIL;
7564 added = getdigits(&p) * 1024;
7565 if (*p != NUL)
7566 return FAIL;
7567
Bram Moolenaar89d40322006-08-29 15:30:07 +00007568 if (start == 0 || incr == 0 || added == 0 || incr > start)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007569 return FAIL;
7570
7571 compress_start = start;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007572 compress_inc = incr;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007573 compress_added = added;
7574 return OK;
7575}
7576
7577
7578/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007579 * Get a wordnode_T, either from the list of previously freed nodes or
7580 * allocate a new one.
7581 */
7582 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007583get_wordnode(spin)
7584 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007585{
7586 wordnode_T *n;
7587
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007588 if (spin->si_first_free == NULL)
7589 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007590 else
7591 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007592 n = spin->si_first_free;
7593 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007594 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007595 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007596 }
7597#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007598 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007599#endif
7600 return n;
7601}
7602
7603/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007604 * Decrement the reference count on a node (which is the head of a list of
7605 * siblings). If the reference count becomes zero free the node and its
7606 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007607 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007608 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007609 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007610deref_wordnode(spin, node)
7611 spellinfo_T *spin;
7612 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007613{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007614 wordnode_T *np;
7615 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007616
7617 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007618 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007619 for (np = node; np != NULL; np = np->wn_sibling)
7620 {
7621 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007622 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007623 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007624 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007625 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007626 ++cnt; /* length field */
7627 }
7628 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007629}
7630
7631/*
7632 * Free a wordnode_T for re-use later.
7633 * Only the "wn_child" field becomes invalid.
7634 */
7635 static void
7636free_wordnode(spin, n)
7637 spellinfo_T *spin;
7638 wordnode_T *n;
7639{
7640 n->wn_child = spin->si_first_free;
7641 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007642 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007643}
7644
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007645/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007646 * Compress a tree: find tails that are identical and can be shared.
7647 */
7648 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007649wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007650 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007651 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007652{
7653 hashtab_T ht;
7654 int n;
7655 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007656 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007657
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007658 /* Skip the root itself, it's not actually used. The first sibling is the
7659 * start of the tree. */
7660 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007661 {
7662 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007663 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007664
7665#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007666 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007667#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007668 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007669 if (tot > 1000000)
7670 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007671 else if (tot == 0)
7672 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007673 else
7674 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007675 vim_snprintf((char *)IObuff, IOSIZE,
7676 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7677 n, tot, tot - n, perc);
7678 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007679 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007680#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007681 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007682#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007683 hash_clear(&ht);
7684 }
7685}
7686
7687/*
7688 * Compress a node, its siblings and its children, depth first.
7689 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007690 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007691 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007692node_compress(spin, node, ht, tot)
7693 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007694 wordnode_T *node;
7695 hashtab_T *ht;
7696 int *tot; /* total count of nodes before compressing,
7697 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007698{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007699 wordnode_T *np;
7700 wordnode_T *tp;
7701 wordnode_T *child;
7702 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007703 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007704 int len = 0;
7705 unsigned nr, n;
7706 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007707
Bram Moolenaar51485f02005-06-04 21:55:20 +00007708 /*
7709 * Go through the list of siblings. Compress each child and then try
7710 * finding an identical child to replace it.
7711 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007712 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007713 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007714 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007715 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007716 ++len;
7717 if ((child = np->wn_child) != NULL)
7718 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007719 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007720 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007721
7722 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007723 hash = hash_hash(child->wn_u1.hashkey);
7724 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007725 if (!HASHITEM_EMPTY(hi))
7726 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007727 /* There are children we encountered before with a hash value
7728 * identical to the current child. Now check if there is one
7729 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007730 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007731 if (node_equal(child, tp))
7732 {
7733 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007734 * current one. This means the current child and all
7735 * its siblings is unlinked from the tree. */
7736 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007737 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007738 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007739 break;
7740 }
7741 if (tp == NULL)
7742 {
7743 /* No other child with this hash value equals the child of
7744 * the node, add it to the linked list after the first
7745 * item. */
7746 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007747 child->wn_u2.next = tp->wn_u2.next;
7748 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007749 }
7750 }
7751 else
7752 /* No other child has this hash value, add it to the
7753 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007754 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007755 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007756 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007757 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007758
7759 /*
7760 * Make a hash key for the node and its siblings, so that we can quickly
7761 * find a lookalike node. This must be done after compressing the sibling
7762 * list, otherwise the hash key would become invalid by the compression.
7763 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007764 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007765 nr = 0;
7766 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007767 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007768 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007769 /* end node: use wn_flags, wn_region and wn_affixID */
7770 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007771 else
7772 /* byte node: use the byte value and the child pointer */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007773 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
Bram Moolenaar51485f02005-06-04 21:55:20 +00007774 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007775 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007776
7777 /* Avoid NUL bytes, it terminates the hash key. */
7778 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007779 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007780 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007781 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007782 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007783 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007784 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007785 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7786 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007787
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007788 /* Check for CTRL-C pressed now and then. */
7789 fast_breakcheck();
7790
Bram Moolenaar51485f02005-06-04 21:55:20 +00007791 return compressed;
7792}
7793
7794/*
7795 * Return TRUE when two nodes have identical siblings and children.
7796 */
7797 static int
7798node_equal(n1, n2)
7799 wordnode_T *n1;
7800 wordnode_T *n2;
7801{
7802 wordnode_T *p1;
7803 wordnode_T *p2;
7804
7805 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7806 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7807 if (p1->wn_byte != p2->wn_byte
7808 || (p1->wn_byte == NUL
7809 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007810 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007811 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007812 : (p1->wn_child != p2->wn_child)))
7813 break;
7814
7815 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007816}
7817
7818/*
7819 * Write a number to file "fd", MSB first, in "len" bytes.
7820 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007821 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007822put_bytes(fd, nr, len)
7823 FILE *fd;
7824 long_u nr;
7825 int len;
7826{
7827 int i;
7828
7829 for (i = len - 1; i >= 0; --i)
7830 putc((int)(nr >> (i * 8)), fd);
7831}
7832
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007833#ifdef _MSC_VER
7834# if (_MSC_VER <= 1200)
7835/* This line is required for VC6 without the service pack. Also see the
7836 * matching #pragma below. */
Bram Moolenaar5fdec472007-07-24 08:45:13 +00007837 # pragma optimize("", off)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007838# endif
7839#endif
7840
Bram Moolenaar4770d092006-01-12 23:22:24 +00007841/*
7842 * Write spin->si_sugtime to file "fd".
7843 */
7844 static void
7845put_sugtime(spin, fd)
7846 spellinfo_T *spin;
7847 FILE *fd;
7848{
7849 int c;
7850 int i;
7851
7852 /* time_t can be up to 8 bytes in size, more than long_u, thus we
7853 * can't use put_bytes() here. */
7854 for (i = 7; i >= 0; --i)
7855 if (i + 1 > sizeof(time_t))
7856 /* ">>" doesn't work well when shifting more bits than avail */
7857 putc(0, fd);
7858 else
7859 {
7860 c = (unsigned)spin->si_sugtime >> (i * 8);
7861 putc(c, fd);
7862 }
7863}
7864
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007865#ifdef _MSC_VER
7866# if (_MSC_VER <= 1200)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00007867 # pragma optimize("", on)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007868# endif
7869#endif
7870
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007871static int
7872#ifdef __BORLANDC__
7873_RTLENTRYF
7874#endif
7875rep_compare __ARGS((const void *s1, const void *s2));
7876
7877/*
7878 * Function given to qsort() to sort the REP items on "from" string.
7879 */
7880 static int
7881#ifdef __BORLANDC__
7882_RTLENTRYF
7883#endif
7884rep_compare(s1, s2)
7885 const void *s1;
7886 const void *s2;
7887{
7888 fromto_T *p1 = (fromto_T *)s1;
7889 fromto_T *p2 = (fromto_T *)s2;
7890
7891 return STRCMP(p1->ft_from, p2->ft_from);
7892}
7893
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007894/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007895 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007896 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007897 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007898 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007899write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007900 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007901 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007902{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007903 FILE *fd;
7904 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007905 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007906 wordnode_T *tree;
7907 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007908 int i;
7909 int l;
7910 garray_T *gap;
7911 fromto_T *ftp;
7912 char_u *p;
7913 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007914 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007915
Bram Moolenaarb765d632005-06-07 21:00:02 +00007916 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007917 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007918 {
7919 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007920 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007921 }
7922
Bram Moolenaar5195e452005-08-19 20:32:47 +00007923 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007924 /* <fileID> */
7925 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007926 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007927 EMSG(_(e_write));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007928 retval = FAIL;
7929 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007930 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007931
Bram Moolenaar5195e452005-08-19 20:32:47 +00007932 /*
7933 * <SECTIONS>: <section> ... <sectionend>
7934 */
7935
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007936 /* SN_INFO: <infotext> */
7937 if (spin->si_info != NULL)
7938 {
7939 putc(SN_INFO, fd); /* <sectionID> */
7940 putc(0, fd); /* <sectionflags> */
7941
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007942 i = (int)STRLEN(spin->si_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007943 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
7944 fwrite(spin->si_info, (size_t)i, (size_t)1, fd); /* <infotext> */
7945 }
7946
Bram Moolenaar5195e452005-08-19 20:32:47 +00007947 /* SN_REGION: <regionname> ...
7948 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007949 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007950 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007951 putc(SN_REGION, fd); /* <sectionID> */
7952 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7953 l = spin->si_region_count * 2;
7954 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7955 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
7956 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007957 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007958 }
7959 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00007960 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007961
Bram Moolenaar5195e452005-08-19 20:32:47 +00007962 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
7963 *
7964 * The table with character flags and the table for case folding.
7965 * This makes sure the same characters are recognized as word characters
7966 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007967 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007968 * 'encoding'.
7969 * Also skip this for an .add.spl file, the main spell file must contain
7970 * the table (avoids that it conflicts). File is shorter too.
7971 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007972 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007973 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007974 char_u folchars[128 * 8];
7975 int flags;
7976
Bram Moolenaard12a1322005-08-21 22:08:24 +00007977 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007978 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7979
7980 /* Form the <folchars> string first, we need to know its length. */
7981 l = 0;
7982 for (i = 128; i < 256; ++i)
7983 {
7984#ifdef FEAT_MBYTE
7985 if (has_mbyte)
7986 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
7987 else
7988#endif
7989 folchars[l++] = spelltab.st_fold[i];
7990 }
7991 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
7992
7993 fputc(128, fd); /* <charflagslen> */
7994 for (i = 128; i < 256; ++i)
7995 {
7996 flags = 0;
7997 if (spelltab.st_isw[i])
7998 flags |= CF_WORD;
7999 if (spelltab.st_isu[i])
8000 flags |= CF_UPPER;
8001 fputc(flags, fd); /* <charflags> */
8002 }
8003
8004 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
8005 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008006 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008007
Bram Moolenaar5195e452005-08-19 20:32:47 +00008008 /* SN_MIDWORD: <midword> */
8009 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008010 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008011 putc(SN_MIDWORD, fd); /* <sectionID> */
8012 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8013
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008014 i = (int)STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008015 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008016 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
8017 }
8018
Bram Moolenaar5195e452005-08-19 20:32:47 +00008019 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
8020 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008021 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008022 putc(SN_PREFCOND, fd); /* <sectionID> */
8023 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8024
8025 l = write_spell_prefcond(NULL, &spin->si_prefcond);
8026 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8027
8028 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008029 }
8030
Bram Moolenaar5195e452005-08-19 20:32:47 +00008031 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00008032 * SN_SAL: <salflags> <salcount> <sal> ...
8033 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008034
Bram Moolenaar5195e452005-08-19 20:32:47 +00008035 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00008036 * round 2: SN_SAL section (unless SN_SOFO is used)
8037 * round 3: SN_REPSAL section */
8038 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008039 {
8040 if (round == 1)
8041 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008042 else if (round == 2)
8043 {
8044 /* Don't write SN_SAL when using a SN_SOFO section */
8045 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8046 continue;
8047 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008048 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008049 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00008050 gap = &spin->si_repsal;
8051
8052 /* Don't write the section if there are no items. */
8053 if (gap->ga_len == 0)
8054 continue;
8055
8056 /* Sort the REP/REPSAL items. */
8057 if (round != 2)
8058 qsort(gap->ga_data, (size_t)gap->ga_len,
8059 sizeof(fromto_T), rep_compare);
8060
8061 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
8062 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008063
Bram Moolenaar5195e452005-08-19 20:32:47 +00008064 /* This is for making suggestions, section is not required. */
8065 putc(0, fd); /* <sectionflags> */
8066
8067 /* Compute the length of what follows. */
8068 l = 2; /* count <repcount> or <salcount> */
8069 for (i = 0; i < gap->ga_len; ++i)
8070 {
8071 ftp = &((fromto_T *)gap->ga_data)[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008072 l += 1 + (int)STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
8073 l += 1 + (int)STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008074 }
8075 if (round == 2)
8076 ++l; /* count <salflags> */
8077 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8078
8079 if (round == 2)
8080 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008081 i = 0;
8082 if (spin->si_followup)
8083 i |= SAL_F0LLOWUP;
8084 if (spin->si_collapse)
8085 i |= SAL_COLLAPSE;
8086 if (spin->si_rem_accents)
8087 i |= SAL_REM_ACCENTS;
8088 putc(i, fd); /* <salflags> */
8089 }
8090
8091 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
8092 for (i = 0; i < gap->ga_len; ++i)
8093 {
8094 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
8095 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
8096 ftp = &((fromto_T *)gap->ga_data)[i];
8097 for (rr = 1; rr <= 2; ++rr)
8098 {
8099 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008100 l = (int)STRLEN(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008101 putc(l, fd);
8102 fwrite(p, l, (size_t)1, fd);
8103 }
8104 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008105
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008106 }
8107
Bram Moolenaar5195e452005-08-19 20:32:47 +00008108 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
8109 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008110 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8111 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008112 putc(SN_SOFO, fd); /* <sectionID> */
8113 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008114
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008115 l = (int)STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008116 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
8117 /* <sectionlen> */
8118
8119 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
8120 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008121
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008122 l = (int)STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008123 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
8124 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008125 }
8126
Bram Moolenaar4770d092006-01-12 23:22:24 +00008127 /* SN_WORDS: <word> ...
8128 * This is for making suggestions, section is not required. */
8129 if (spin->si_commonwords.ht_used > 0)
8130 {
8131 putc(SN_WORDS, fd); /* <sectionID> */
8132 putc(0, fd); /* <sectionflags> */
8133
8134 /* round 1: count the bytes
8135 * round 2: write the bytes */
8136 for (round = 1; round <= 2; ++round)
8137 {
8138 int todo;
8139 int len = 0;
8140 hashitem_T *hi;
8141
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008142 todo = (int)spin->si_commonwords.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008143 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
8144 if (!HASHITEM_EMPTY(hi))
8145 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008146 l = (int)STRLEN(hi->hi_key) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008147 len += l;
8148 if (round == 2) /* <word> */
8149 fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
8150 --todo;
8151 }
8152 if (round == 1)
8153 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
8154 }
8155 }
8156
Bram Moolenaar5195e452005-08-19 20:32:47 +00008157 /* SN_MAP: <mapstr>
8158 * This is for making suggestions, section is not required. */
8159 if (spin->si_map.ga_len > 0)
8160 {
8161 putc(SN_MAP, fd); /* <sectionID> */
8162 putc(0, fd); /* <sectionflags> */
8163 l = spin->si_map.ga_len;
8164 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8165 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
8166 /* <mapstr> */
8167 }
8168
Bram Moolenaar4770d092006-01-12 23:22:24 +00008169 /* SN_SUGFILE: <timestamp>
8170 * This is used to notify that a .sug file may be available and at the
8171 * same time allows for checking that a .sug file that is found matches
8172 * with this .spl file. That's because the word numbers must be exactly
8173 * right. */
8174 if (!spin->si_nosugfile
8175 && (spin->si_sal.ga_len > 0
8176 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
8177 {
8178 putc(SN_SUGFILE, fd); /* <sectionID> */
8179 putc(0, fd); /* <sectionflags> */
8180 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
8181
8182 /* Set si_sugtime and write it to the file. */
8183 spin->si_sugtime = time(NULL);
8184 put_sugtime(spin, fd); /* <timestamp> */
8185 }
8186
Bram Moolenaare1438bb2006-03-01 22:01:55 +00008187 /* SN_NOSPLITSUGS: nothing
8188 * This is used to notify that no suggestions with word splits are to be
8189 * made. */
8190 if (spin->si_nosplitsugs)
8191 {
8192 putc(SN_NOSPLITSUGS, fd); /* <sectionID> */
8193 putc(0, fd); /* <sectionflags> */
8194 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8195 }
8196
Bram Moolenaar5195e452005-08-19 20:32:47 +00008197 /* SN_COMPOUND: compound info.
8198 * We don't mark it required, when not supported all compound words will
8199 * be bad words. */
8200 if (spin->si_compflags != NULL)
8201 {
8202 putc(SN_COMPOUND, fd); /* <sectionID> */
8203 putc(0, fd); /* <sectionflags> */
8204
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008205 l = (int)STRLEN(spin->si_compflags);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008206 for (i = 0; i < spin->si_comppat.ga_len; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008207 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008208 put_bytes(fd, (long_u)(l + 7), 4); /* <sectionlen> */
8209
Bram Moolenaar5195e452005-08-19 20:32:47 +00008210 putc(spin->si_compmax, fd); /* <compmax> */
8211 putc(spin->si_compminlen, fd); /* <compminlen> */
8212 putc(spin->si_compsylmax, fd); /* <compsylmax> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008213 putc(0, fd); /* for Vim 7.0b compatibility */
8214 putc(spin->si_compoptions, fd); /* <compoptions> */
8215 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
8216 /* <comppatcount> */
8217 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8218 {
8219 p = ((char_u **)(spin->si_comppat.ga_data))[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008220 putc((int)STRLEN(p), fd); /* <comppatlen> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008221 fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);/* <comppattext> */
8222 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008223 /* <compflags> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008224 fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
8225 (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008226 }
8227
Bram Moolenaar78622822005-08-23 21:00:13 +00008228 /* SN_NOBREAK: NOBREAK flag */
8229 if (spin->si_nobreak)
8230 {
8231 putc(SN_NOBREAK, fd); /* <sectionID> */
8232 putc(0, fd); /* <sectionflags> */
8233
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008234 /* It's empty, the presence of the section flags the feature. */
Bram Moolenaar78622822005-08-23 21:00:13 +00008235 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8236 }
8237
Bram Moolenaar5195e452005-08-19 20:32:47 +00008238 /* SN_SYLLABLE: syllable info.
8239 * We don't mark it required, when not supported syllables will not be
8240 * counted. */
8241 if (spin->si_syllable != NULL)
8242 {
8243 putc(SN_SYLLABLE, fd); /* <sectionID> */
8244 putc(0, fd); /* <sectionflags> */
8245
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008246 l = (int)STRLEN(spin->si_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008247 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8248 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
8249 }
8250
8251 /* end of <SECTIONS> */
8252 putc(SN_END, fd); /* <sectionend> */
8253
Bram Moolenaar50cde822005-06-05 21:54:54 +00008254
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008255 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008256 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008257 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008258 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008259 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008260 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008261 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008262 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008263 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008264 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008265 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008266 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008267
Bram Moolenaar0c405862005-06-22 22:26:26 +00008268 /* Clear the index and wnode fields in the tree. */
8269 clear_node(tree);
8270
Bram Moolenaar51485f02005-06-04 21:55:20 +00008271 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00008272 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00008273 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008274 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008275
Bram Moolenaar51485f02005-06-04 21:55:20 +00008276 /* number of nodes in 4 bytes */
8277 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00008278 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008279
Bram Moolenaar51485f02005-06-04 21:55:20 +00008280 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008281 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008282 }
8283
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008284 /* Write another byte to check for errors. */
8285 if (putc(0, fd) == EOF)
8286 retval = FAIL;
8287
8288 if (fclose(fd) == EOF)
8289 retval = FAIL;
8290
8291 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008292}
8293
8294/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008295 * Clear the index and wnode fields of "node", it siblings and its
8296 * children. This is needed because they are a union with other items to save
8297 * space.
8298 */
8299 static void
8300clear_node(node)
8301 wordnode_T *node;
8302{
8303 wordnode_T *np;
8304
8305 if (node != NULL)
8306 for (np = node; np != NULL; np = np->wn_sibling)
8307 {
8308 np->wn_u1.index = 0;
8309 np->wn_u2.wnode = NULL;
8310
8311 if (np->wn_byte != NUL)
8312 clear_node(np->wn_child);
8313 }
8314}
8315
8316
8317/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008318 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008319 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00008320 * This first writes the list of possible bytes (siblings). Then for each
8321 * byte recursively write the children.
8322 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00008323 * NOTE: The code here must match the code in read_tree_node(), since
8324 * assumptions are made about the indexes (so that we don't have to write them
8325 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00008326 *
8327 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008328 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008329 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00008330put_node(fd, node, idx, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008331 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008332 wordnode_T *node;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008333 int idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008334 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008335 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008336{
Bram Moolenaar89d40322006-08-29 15:30:07 +00008337 int newindex = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008338 int siblingcount = 0;
8339 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008340 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008341
Bram Moolenaar51485f02005-06-04 21:55:20 +00008342 /* If "node" is zero the tree is empty. */
8343 if (node == NULL)
8344 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008345
Bram Moolenaar51485f02005-06-04 21:55:20 +00008346 /* Store the index where this node is written. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008347 node->wn_u1.index = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008348
8349 /* Count the number of siblings. */
8350 for (np = node; np != NULL; np = np->wn_sibling)
8351 ++siblingcount;
8352
8353 /* Write the sibling count. */
8354 if (fd != NULL)
8355 putc(siblingcount, fd); /* <siblingcount> */
8356
8357 /* Write each sibling byte and optionally extra info. */
8358 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008359 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008360 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008361 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008362 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008363 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008364 /* For a NUL byte (end of word) write the flags etc. */
8365 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008366 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008367 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008368 * associated condition nr (stored in wn_region). The
8369 * byte value is misused to store the "rare" and "not
8370 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008371 if (np->wn_flags == (short_u)PFX_FLAGS)
8372 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008373 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00008374 {
8375 putc(BY_FLAGS, fd); /* <byte> */
8376 putc(np->wn_flags, fd); /* <pflags> */
8377 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008378 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008379 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008380 }
8381 else
8382 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008383 /* For word trees we write the flag/region items. */
8384 flags = np->wn_flags;
8385 if (regionmask != 0 && np->wn_region != regionmask)
8386 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008387 if (np->wn_affixID != 0)
8388 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008389 if (flags == 0)
8390 {
8391 /* word without flags or region */
8392 putc(BY_NOFLAGS, fd); /* <byte> */
8393 }
8394 else
8395 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008396 if (np->wn_flags >= 0x100)
8397 {
8398 putc(BY_FLAGS2, fd); /* <byte> */
8399 putc(flags, fd); /* <flags> */
8400 putc((unsigned)flags >> 8, fd); /* <flags2> */
8401 }
8402 else
8403 {
8404 putc(BY_FLAGS, fd); /* <byte> */
8405 putc(flags, fd); /* <flags> */
8406 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008407 if (flags & WF_REGION)
8408 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008409 if (flags & WF_AFX)
8410 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008411 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008412 }
8413 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008414 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008415 else
8416 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008417 if (np->wn_child->wn_u1.index != 0
8418 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008419 {
8420 /* The child is written elsewhere, write the reference. */
8421 if (fd != NULL)
8422 {
8423 putc(BY_INDEX, fd); /* <byte> */
8424 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008425 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008426 }
8427 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008428 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008429 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008430 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008431
Bram Moolenaar51485f02005-06-04 21:55:20 +00008432 if (fd != NULL)
8433 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
8434 {
8435 EMSG(_(e_write));
8436 return 0;
8437 }
8438 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008439 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008440
8441 /* Space used in the array when reading: one for each sibling and one for
8442 * the count. */
8443 newindex += siblingcount + 1;
8444
8445 /* Recursively dump the children of each sibling. */
8446 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008447 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
8448 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008449 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008450
8451 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008452}
8453
8454
8455/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008456 * ":mkspell [-ascii] outfile infile ..."
8457 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008458 */
8459 void
8460ex_mkspell(eap)
8461 exarg_T *eap;
8462{
8463 int fcount;
8464 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008465 char_u *arg = eap->arg;
8466 int ascii = FALSE;
8467
8468 if (STRNCMP(arg, "-ascii", 6) == 0)
8469 {
8470 ascii = TRUE;
8471 arg = skipwhite(arg + 6);
8472 }
8473
8474 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
8475 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
8476 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008477 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008478 FreeWild(fcount, fnames);
8479 }
8480}
8481
8482/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00008483 * Create the .sug file.
8484 * Uses the soundfold info in "spin".
8485 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
8486 */
8487 static void
8488spell_make_sugfile(spin, wfname)
8489 spellinfo_T *spin;
8490 char_u *wfname;
8491{
8492 char_u fname[MAXPATHL];
8493 int len;
8494 slang_T *slang;
8495 int free_slang = FALSE;
8496
8497 /*
8498 * Read back the .spl file that was written. This fills the required
8499 * info for soundfolding. This also uses less memory than the
8500 * pointer-linked version of the trie. And it avoids having two versions
8501 * of the code for the soundfolding stuff.
8502 * It might have been done already by spell_reload_one().
8503 */
8504 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
8505 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
8506 break;
8507 if (slang == NULL)
8508 {
8509 spell_message(spin, (char_u *)_("Reading back spell file..."));
8510 slang = spell_load_file(wfname, NULL, NULL, FALSE);
8511 if (slang == NULL)
8512 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008513 free_slang = TRUE;
8514 }
8515
8516 /*
8517 * Clear the info in "spin" that is used.
8518 */
8519 spin->si_blocks = NULL;
8520 spin->si_blocks_cnt = 0;
8521 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
8522 spin->si_free_count = 0;
8523 spin->si_first_free = NULL;
8524 spin->si_foldwcount = 0;
8525
8526 /*
8527 * Go through the trie of good words, soundfold each word and add it to
8528 * the soundfold trie.
8529 */
8530 spell_message(spin, (char_u *)_("Performing soundfolding..."));
8531 if (sug_filltree(spin, slang) == FAIL)
8532 goto theend;
8533
8534 /*
8535 * Create the table which links each soundfold word with a list of the
8536 * good words it may come from. Creates buffer "spin->si_spellbuf".
8537 * This also removes the wordnr from the NUL byte entries to make
8538 * compression possible.
8539 */
8540 if (sug_maketable(spin) == FAIL)
8541 goto theend;
8542
8543 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8544 (long)spin->si_spellbuf->b_ml.ml_line_count);
8545
8546 /*
8547 * Compress the soundfold trie.
8548 */
8549 spell_message(spin, (char_u *)_(msg_compressing));
8550 wordtree_compress(spin, spin->si_foldroot);
8551
8552 /*
8553 * Write the .sug file.
8554 * Make the file name by changing ".spl" to ".sug".
8555 */
8556 STRCPY(fname, wfname);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008557 len = (int)STRLEN(fname);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008558 fname[len - 2] = 'u';
8559 fname[len - 1] = 'g';
8560 sug_write(spin, fname);
8561
8562theend:
8563 if (free_slang)
8564 slang_free(slang);
8565 free_blocks(spin->si_blocks);
8566 close_spellbuf(spin->si_spellbuf);
8567}
8568
8569/*
8570 * Build the soundfold trie for language "slang".
8571 */
8572 static int
8573sug_filltree(spin, slang)
8574 spellinfo_T *spin;
8575 slang_T *slang;
8576{
8577 char_u *byts;
8578 idx_T *idxs;
8579 int depth;
8580 idx_T arridx[MAXWLEN];
8581 int curi[MAXWLEN];
8582 char_u tword[MAXWLEN];
8583 char_u tsalword[MAXWLEN];
8584 int c;
8585 idx_T n;
8586 unsigned words_done = 0;
8587 int wordcount[MAXWLEN];
8588
8589 /* We use si_foldroot for the souldfolded trie. */
8590 spin->si_foldroot = wordtree_alloc(spin);
8591 if (spin->si_foldroot == NULL)
8592 return FAIL;
8593
8594 /* let tree_add_word() know we're adding to the soundfolded tree */
8595 spin->si_sugtree = TRUE;
8596
8597 /*
8598 * Go through the whole case-folded tree, soundfold each word and put it
8599 * in the trie.
8600 */
8601 byts = slang->sl_fbyts;
8602 idxs = slang->sl_fidxs;
8603
8604 arridx[0] = 0;
8605 curi[0] = 1;
8606 wordcount[0] = 0;
8607
8608 depth = 0;
8609 while (depth >= 0 && !got_int)
8610 {
8611 if (curi[depth] > byts[arridx[depth]])
8612 {
8613 /* Done all bytes at this node, go up one level. */
8614 idxs[arridx[depth]] = wordcount[depth];
8615 if (depth > 0)
8616 wordcount[depth - 1] += wordcount[depth];
8617
8618 --depth;
8619 line_breakcheck();
8620 }
8621 else
8622 {
8623
8624 /* Do one more byte at this node. */
8625 n = arridx[depth] + curi[depth];
8626 ++curi[depth];
8627
8628 c = byts[n];
8629 if (c == 0)
8630 {
8631 /* Sound-fold the word. */
8632 tword[depth] = NUL;
8633 spell_soundfold(slang, tword, TRUE, tsalword);
8634
8635 /* We use the "flags" field for the MSB of the wordnr,
8636 * "region" for the LSB of the wordnr. */
8637 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8638 words_done >> 16, words_done & 0xffff,
8639 0) == FAIL)
8640 return FAIL;
8641
8642 ++words_done;
8643 ++wordcount[depth];
8644
8645 /* Reset the block count each time to avoid compression
8646 * kicking in. */
8647 spin->si_blocks_cnt = 0;
8648
8649 /* Skip over any other NUL bytes (same word with different
8650 * flags). */
8651 while (byts[n + 1] == 0)
8652 {
8653 ++n;
8654 ++curi[depth];
8655 }
8656 }
8657 else
8658 {
8659 /* Normal char, go one level deeper. */
8660 tword[depth++] = c;
8661 arridx[depth] = idxs[n];
8662 curi[depth] = 1;
8663 wordcount[depth] = 0;
8664 }
8665 }
8666 }
8667
8668 smsg((char_u *)_("Total number of words: %d"), words_done);
8669
8670 return OK;
8671}
8672
8673/*
8674 * Make the table that links each word in the soundfold trie to the words it
8675 * can be produced from.
8676 * This is not unlike lines in a file, thus use a memfile to be able to access
8677 * the table efficiently.
8678 * Returns FAIL when out of memory.
8679 */
8680 static int
8681sug_maketable(spin)
8682 spellinfo_T *spin;
8683{
8684 garray_T ga;
8685 int res = OK;
8686
8687 /* Allocate a buffer, open a memline for it and create the swap file
8688 * (uses a temp file, not a .swp file). */
8689 spin->si_spellbuf = open_spellbuf();
8690 if (spin->si_spellbuf == NULL)
8691 return FAIL;
8692
8693 /* Use a buffer to store the line info, avoids allocating many small
8694 * pieces of memory. */
8695 ga_init2(&ga, 1, 100);
8696
8697 /* recursively go through the tree */
8698 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8699 res = FAIL;
8700
8701 ga_clear(&ga);
8702 return res;
8703}
8704
8705/*
8706 * Fill the table for one node and its children.
8707 * Returns the wordnr at the start of the node.
8708 * Returns -1 when out of memory.
8709 */
8710 static int
8711sug_filltable(spin, node, startwordnr, gap)
8712 spellinfo_T *spin;
8713 wordnode_T *node;
8714 int startwordnr;
8715 garray_T *gap; /* place to store line of numbers */
8716{
8717 wordnode_T *p, *np;
8718 int wordnr = startwordnr;
8719 int nr;
8720 int prev_nr;
8721
8722 for (p = node; p != NULL; p = p->wn_sibling)
8723 {
8724 if (p->wn_byte == NUL)
8725 {
8726 gap->ga_len = 0;
8727 prev_nr = 0;
8728 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8729 {
8730 if (ga_grow(gap, 10) == FAIL)
8731 return -1;
8732
8733 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8734 /* Compute the offset from the previous nr and store the
8735 * offset in a way that it takes a minimum number of bytes.
8736 * It's a bit like utf-8, but without the need to mark
8737 * following bytes. */
8738 nr -= prev_nr;
8739 prev_nr += nr;
8740 gap->ga_len += offset2bytes(nr,
8741 (char_u *)gap->ga_data + gap->ga_len);
8742 }
8743
8744 /* add the NUL byte */
8745 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8746
8747 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8748 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8749 return -1;
8750 ++wordnr;
8751
8752 /* Remove extra NUL entries, we no longer need them. We don't
8753 * bother freeing the nodes, the won't be reused anyway. */
8754 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8755 p->wn_sibling = p->wn_sibling->wn_sibling;
8756
8757 /* Clear the flags on the remaining NUL node, so that compression
8758 * works a lot better. */
8759 p->wn_flags = 0;
8760 p->wn_region = 0;
8761 }
8762 else
8763 {
8764 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8765 if (wordnr == -1)
8766 return -1;
8767 }
8768 }
8769 return wordnr;
8770}
8771
8772/*
8773 * Convert an offset into a minimal number of bytes.
8774 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8775 * bytes.
8776 */
8777 static int
8778offset2bytes(nr, buf)
8779 int nr;
8780 char_u *buf;
8781{
8782 int rem;
8783 int b1, b2, b3, b4;
8784
8785 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8786 b1 = nr % 255 + 1;
8787 rem = nr / 255;
8788 b2 = rem % 255 + 1;
8789 rem = rem / 255;
8790 b3 = rem % 255 + 1;
8791 b4 = rem / 255 + 1;
8792
8793 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8794 {
8795 buf[0] = 0xe0 + b4;
8796 buf[1] = b3;
8797 buf[2] = b2;
8798 buf[3] = b1;
8799 return 4;
8800 }
8801 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8802 {
8803 buf[0] = 0xc0 + b3;
8804 buf[1] = b2;
8805 buf[2] = b1;
8806 return 3;
8807 }
8808 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8809 {
8810 buf[0] = 0x80 + b2;
8811 buf[1] = b1;
8812 return 2;
8813 }
8814 /* 1 byte */
8815 buf[0] = b1;
8816 return 1;
8817}
8818
8819/*
8820 * Opposite of offset2bytes().
8821 * "pp" points to the bytes and is advanced over it.
8822 * Returns the offset.
8823 */
8824 static int
8825bytes2offset(pp)
8826 char_u **pp;
8827{
8828 char_u *p = *pp;
8829 int nr;
8830 int c;
8831
8832 c = *p++;
8833 if ((c & 0x80) == 0x00) /* 1 byte */
8834 {
8835 nr = c - 1;
8836 }
8837 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8838 {
8839 nr = (c & 0x3f) - 1;
8840 nr = nr * 255 + (*p++ - 1);
8841 }
8842 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8843 {
8844 nr = (c & 0x1f) - 1;
8845 nr = nr * 255 + (*p++ - 1);
8846 nr = nr * 255 + (*p++ - 1);
8847 }
8848 else /* 4 bytes */
8849 {
8850 nr = (c & 0x0f) - 1;
8851 nr = nr * 255 + (*p++ - 1);
8852 nr = nr * 255 + (*p++ - 1);
8853 nr = nr * 255 + (*p++ - 1);
8854 }
8855
8856 *pp = p;
8857 return nr;
8858}
8859
8860/*
8861 * Write the .sug file in "fname".
8862 */
8863 static void
8864sug_write(spin, fname)
8865 spellinfo_T *spin;
8866 char_u *fname;
8867{
8868 FILE *fd;
8869 wordnode_T *tree;
8870 int nodecount;
8871 int wcount;
8872 char_u *line;
8873 linenr_T lnum;
8874 int len;
8875
8876 /* Create the file. Note that an existing file is silently overwritten! */
8877 fd = mch_fopen((char *)fname, "w");
8878 if (fd == NULL)
8879 {
8880 EMSG2(_(e_notopen), fname);
8881 return;
8882 }
8883
8884 vim_snprintf((char *)IObuff, IOSIZE,
8885 _("Writing suggestion file %s ..."), fname);
8886 spell_message(spin, IObuff);
8887
8888 /*
8889 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8890 */
8891 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
8892 {
8893 EMSG(_(e_write));
8894 goto theend;
8895 }
8896 putc(VIMSUGVERSION, fd); /* <versionnr> */
8897
8898 /* Write si_sugtime to the file. */
8899 put_sugtime(spin, fd); /* <timestamp> */
8900
8901 /*
8902 * <SUGWORDTREE>
8903 */
8904 spin->si_memtot = 0;
8905 tree = spin->si_foldroot->wn_sibling;
8906
8907 /* Clear the index and wnode fields in the tree. */
8908 clear_node(tree);
8909
8910 /* Count the number of nodes. Needed to be able to allocate the
8911 * memory when reading the nodes. Also fills in index for shared
8912 * nodes. */
8913 nodecount = put_node(NULL, tree, 0, 0, FALSE);
8914
8915 /* number of nodes in 4 bytes */
8916 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
8917 spin->si_memtot += nodecount + nodecount * sizeof(int);
8918
8919 /* Write the nodes. */
8920 (void)put_node(fd, tree, 0, 0, FALSE);
8921
8922 /*
8923 * <SUGTABLE>: <sugwcount> <sugline> ...
8924 */
8925 wcount = spin->si_spellbuf->b_ml.ml_line_count;
8926 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
8927
8928 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
8929 {
8930 /* <sugline>: <sugnr> ... NUL */
8931 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008932 len = (int)STRLEN(line) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008933 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
8934 {
8935 EMSG(_(e_write));
8936 goto theend;
8937 }
8938 spin->si_memtot += len;
8939 }
8940
8941 /* Write another byte to check for errors. */
8942 if (putc(0, fd) == EOF)
8943 EMSG(_(e_write));
8944
8945 vim_snprintf((char *)IObuff, IOSIZE,
8946 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
8947 spell_message(spin, IObuff);
8948
8949theend:
8950 /* close the file */
8951 fclose(fd);
8952}
8953
8954/*
8955 * Open a spell buffer. This is a nameless buffer that is not in the buffer
8956 * list and only contains text lines. Can use a swapfile to reduce memory
8957 * use.
8958 * Most other fields are invalid! Esp. watch out for string options being
8959 * NULL and there is no undo info.
8960 * Returns NULL when out of memory.
8961 */
8962 static buf_T *
8963open_spellbuf()
8964{
8965 buf_T *buf;
8966
8967 buf = (buf_T *)alloc_clear(sizeof(buf_T));
8968 if (buf != NULL)
8969 {
8970 buf->b_spell = TRUE;
8971 buf->b_p_swf = TRUE; /* may create a swap file */
8972 ml_open(buf);
8973 ml_open_file(buf); /* create swap file now */
8974 }
8975 return buf;
8976}
8977
8978/*
8979 * Close the buffer used for spell info.
8980 */
8981 static void
8982close_spellbuf(buf)
8983 buf_T *buf;
8984{
8985 if (buf != NULL)
8986 {
8987 ml_close(buf, TRUE);
8988 vim_free(buf);
8989 }
8990}
8991
8992
8993/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008994 * Create a Vim spell file from one or more word lists.
8995 * "fnames[0]" is the output file name.
8996 * "fnames[fcount - 1]" is the last input file name.
8997 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
8998 * and ".spl" is appended to make the output file name.
8999 */
9000 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009001mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009002 int fcount;
9003 char_u **fnames;
9004 int ascii; /* -ascii argument given */
9005 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009006 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009007{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009008 char_u fname[MAXPATHL];
9009 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00009010 char_u **innames;
9011 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009012 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009013 int i;
9014 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009015 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00009016 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009017 spellinfo_T spin;
9018
9019 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009020 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009021 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009022 spin.si_followup = TRUE;
9023 spin.si_rem_accents = TRUE;
9024 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009025 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009026 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
9027 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009028 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009029 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009030 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009031 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009032
Bram Moolenaarb765d632005-06-07 21:00:02 +00009033 /* default: fnames[0] is output file, following are input files */
9034 innames = &fnames[1];
9035 incount = fcount - 1;
9036
9037 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00009038 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009039 len = (int)STRLEN(fnames[0]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009040 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
9041 {
9042 /* For ":mkspell path/en.latin1.add" output file is
9043 * "path/en.latin1.add.spl". */
9044 innames = &fnames[0];
9045 incount = 1;
9046 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
9047 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009048 else if (fcount == 1)
9049 {
9050 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
9051 innames = &fnames[0];
9052 incount = 1;
9053 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
9054 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
9055 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009056 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
9057 {
9058 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009059 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009060 }
9061 else
9062 /* Name should be language, make the file name from it. */
9063 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
9064 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
9065
9066 /* Check for .ascii.spl. */
9067 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
9068 spin.si_ascii = TRUE;
9069
9070 /* Check for .add.spl. */
9071 if (strstr((char *)gettail(wfname), ".add.") != NULL)
9072 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00009073 }
9074
Bram Moolenaarb765d632005-06-07 21:00:02 +00009075 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009076 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009077 else if (vim_strchr(gettail(wfname), '_') != NULL)
9078 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009079 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009080 EMSG(_("E754: Only up to 8 regions supported"));
9081 else
9082 {
9083 /* Check for overwriting before doing things that may take a lot of
9084 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009085 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009086 {
9087 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009088 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009089 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009090 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009091 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009092 EMSG2(_(e_isadir2), wfname);
9093 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009094 }
9095
9096 /*
9097 * Init the aff and dic pointers.
9098 * Get the region names if there are more than 2 arguments.
9099 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009100 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009101 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009102 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009103
Bram Moolenaar3982c542005-06-08 21:56:31 +00009104 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009105 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009106 len = (int)STRLEN(innames[i]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009107 if (STRLEN(gettail(innames[i])) < 5
9108 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009109 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009110 EMSG2(_("E755: Invalid region in %s"), innames[i]);
9111 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009112 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009113 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
9114 spin.si_region_name[i * 2 + 1] =
9115 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009116 }
9117 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009118 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009119
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009120 spin.si_foldroot = wordtree_alloc(&spin);
9121 spin.si_keeproot = wordtree_alloc(&spin);
9122 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009123 if (spin.si_foldroot == NULL
9124 || spin.si_keeproot == NULL
9125 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009126 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00009127 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009128 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009129 }
9130
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009131 /* When not producing a .add.spl file clear the character table when
9132 * we encounter one in the .aff file. This means we dump the current
9133 * one in the .spl file if the .aff file doesn't define one. That's
9134 * better than guessing the contents, the table will match a
9135 * previously loaded spell file. */
9136 if (!spin.si_add)
9137 spin.si_clear_chartab = TRUE;
9138
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009139 /*
9140 * Read all the .aff and .dic files.
9141 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00009142 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009143 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009144 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009145 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009146 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009147 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009148
Bram Moolenaarb765d632005-06-07 21:00:02 +00009149 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009150 if (mch_stat((char *)fname, &st) >= 0)
9151 {
9152 /* Read the .aff file. Will init "spin->si_conv" based on the
9153 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009154 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009155 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009156 error = TRUE;
9157 else
9158 {
9159 /* Read the .dic file and store the words in the trees. */
9160 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00009161 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009162 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009163 error = TRUE;
9164 }
9165 }
9166 else
9167 {
9168 /* No .aff file, try reading the file as a word list. Store
9169 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009170 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009171 error = TRUE;
9172 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009173
Bram Moolenaarb765d632005-06-07 21:00:02 +00009174#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009175 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00009176 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009177#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009178 }
9179
Bram Moolenaar78622822005-08-23 21:00:13 +00009180 if (spin.si_compflags != NULL && spin.si_nobreak)
9181 MSG(_("Warning: both compounding and NOBREAK specified"));
9182
Bram Moolenaar4770d092006-01-12 23:22:24 +00009183 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009184 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009185 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00009186 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009187 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009188 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009189 wordtree_compress(&spin, spin.si_foldroot);
9190 wordtree_compress(&spin, spin.si_keeproot);
9191 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009192 }
9193
Bram Moolenaar4770d092006-01-12 23:22:24 +00009194 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009195 {
9196 /*
9197 * Write the info in the spell file.
9198 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009199 vim_snprintf((char *)IObuff, IOSIZE,
9200 _("Writing spell file %s ..."), wfname);
9201 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00009202
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009203 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009204
Bram Moolenaar4770d092006-01-12 23:22:24 +00009205 spell_message(&spin, (char_u *)_("Done!"));
9206 vim_snprintf((char *)IObuff, IOSIZE,
9207 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
9208 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009209
Bram Moolenaar4770d092006-01-12 23:22:24 +00009210 /*
9211 * If the file is loaded need to reload it.
9212 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009213 if (!error)
9214 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009215 }
9216
9217 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009218 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009219 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009220 ga_clear(&spin.si_sal);
9221 ga_clear(&spin.si_map);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009222 ga_clear(&spin.si_comppat);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009223 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009224 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009225
9226 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009227 for (i = 0; i < incount; ++i)
9228 if (afile[i] != NULL)
9229 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009230
9231 /* Free all the bits and pieces at once. */
9232 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009233
9234 /*
9235 * If there is soundfolding info and no NOSUGFILE item create the
9236 * .sug file with the soundfolded word trie.
9237 */
9238 if (spin.si_sugtime != 0 && !error && !got_int)
9239 spell_make_sugfile(&spin, wfname);
9240
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009241 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009242}
9243
Bram Moolenaar4770d092006-01-12 23:22:24 +00009244/*
9245 * Display a message for spell file processing when 'verbose' is set or using
9246 * ":mkspell". "str" can be IObuff.
9247 */
9248 static void
9249spell_message(spin, str)
9250 spellinfo_T *spin;
9251 char_u *str;
9252{
9253 if (spin->si_verbose || p_verbose > 2)
9254 {
9255 if (!spin->si_verbose)
9256 verbose_enter();
9257 MSG(str);
9258 out_flush();
9259 if (!spin->si_verbose)
9260 verbose_leave();
9261 }
9262}
Bram Moolenaarb765d632005-06-07 21:00:02 +00009263
9264/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009265 * ":[count]spellgood {word}"
9266 * ":[count]spellwrong {word}"
Bram Moolenaard0131a82006-03-04 21:46:13 +00009267 * ":[count]spellundo {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00009268 */
9269 void
9270ex_spell(eap)
9271 exarg_T *eap;
9272{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009273 spell_add_word(eap->arg, (int)STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaard0131a82006-03-04 21:46:13 +00009274 eap->forceit ? 0 : (int)eap->line2,
9275 eap->cmdidx == CMD_spellundo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009276}
9277
9278/*
9279 * Add "word[len]" to 'spellfile' as a good or bad word.
9280 */
9281 void
Bram Moolenaar89d40322006-08-29 15:30:07 +00009282spell_add_word(word, len, bad, idx, undo)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009283 char_u *word;
9284 int len;
9285 int bad;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009286 int idx; /* "zG" and "zW": zero, otherwise index in
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009287 'spellfile' */
Bram Moolenaard0131a82006-03-04 21:46:13 +00009288 int undo; /* TRUE for "zug", "zuG", "zuw" and "zuW" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009289{
Bram Moolenaara3917072006-09-14 08:48:14 +00009290 FILE *fd = NULL;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009291 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009292 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009293 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009294 char_u fnamebuf[MAXPATHL];
9295 char_u line[MAXWLEN * 2];
9296 long fpos, fpos_next = 0;
9297 int i;
9298 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009299
Bram Moolenaar89d40322006-08-29 15:30:07 +00009300 if (idx == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009301 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009302 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009303 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009304 int_wordlist = vim_tempname('s');
9305 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009306 return;
9307 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009308 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009309 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009310 else
9311 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009312 /* If 'spellfile' isn't set figure out a good default value. */
9313 if (*curbuf->b_p_spf == NUL)
9314 {
9315 init_spellfile();
9316 new_spf = TRUE;
9317 }
9318
9319 if (*curbuf->b_p_spf == NUL)
9320 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00009321 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009322 return;
9323 }
9324
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009325 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
9326 {
9327 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
Bram Moolenaar89d40322006-08-29 15:30:07 +00009328 if (i == idx)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009329 break;
9330 if (*spf == NUL)
9331 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009332 EMSGN(_("E765: 'spellfile' does not have %ld entries"), idx);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009333 return;
9334 }
9335 }
9336
Bram Moolenaarb765d632005-06-07 21:00:02 +00009337 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009338 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009339 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
9340 buf = NULL;
9341 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00009342 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009343 EMSG(_(e_bufloaded));
9344 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009345 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009346
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009347 fname = fnamebuf;
9348 }
9349
Bram Moolenaard0131a82006-03-04 21:46:13 +00009350 if (bad || undo)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009351 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009352 /* When the word appears as good word we need to remove that one,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009353 * since its flags sort before the one with WF_BANNED. */
9354 fd = mch_fopen((char *)fname, "r");
9355 if (fd != NULL)
9356 {
9357 while (!vim_fgets(line, MAXWLEN * 2, fd))
9358 {
9359 fpos = fpos_next;
9360 fpos_next = ftell(fd);
9361 if (STRNCMP(word, line, len) == 0
9362 && (line[len] == '/' || line[len] < ' '))
9363 {
9364 /* Found duplicate word. Remove it by writing a '#' at
9365 * the start of the line. Mixing reading and writing
9366 * doesn't work for all systems, close the file first. */
9367 fclose(fd);
9368 fd = mch_fopen((char *)fname, "r+");
9369 if (fd == NULL)
9370 break;
9371 if (fseek(fd, fpos, SEEK_SET) == 0)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009372 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009373 fputc('#', fd);
Bram Moolenaard0131a82006-03-04 21:46:13 +00009374 if (undo)
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009375 {
9376 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009377 smsg((char_u *)_("Word removed from %s"), NameBuff);
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009378 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00009379 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009380 fseek(fd, fpos_next, SEEK_SET);
9381 }
9382 }
9383 fclose(fd);
9384 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009385 }
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009386
9387 if (!undo)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009388 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009389 fd = mch_fopen((char *)fname, "a");
9390 if (fd == NULL && new_spf)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009391 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009392 char_u *p;
9393
Bram Moolenaard0131a82006-03-04 21:46:13 +00009394 /* We just initialized the 'spellfile' option and can't open the
9395 * file. We may need to create the "spell" directory first. We
9396 * already checked the runtime directory is writable in
9397 * init_spellfile(). */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009398 if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009399 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009400 int c = *p;
9401
Bram Moolenaard0131a82006-03-04 21:46:13 +00009402 /* The directory doesn't exist. Try creating it and opening
9403 * the file again. */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009404 *p = NUL;
9405 vim_mkdir(fname, 0755);
9406 *p = c;
Bram Moolenaard0131a82006-03-04 21:46:13 +00009407 fd = mch_fopen((char *)fname, "a");
9408 }
9409 }
9410
9411 if (fd == NULL)
9412 EMSG2(_(e_notopen), fname);
9413 else
9414 {
9415 if (bad)
9416 fprintf(fd, "%.*s/!\n", len, word);
9417 else
9418 fprintf(fd, "%.*s\n", len, word);
9419 fclose(fd);
9420
9421 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
9422 smsg((char_u *)_("Word added to %s"), NameBuff);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009423 }
9424 }
9425
Bram Moolenaard0131a82006-03-04 21:46:13 +00009426 if (fd != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009427 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009428 /* Update the .add.spl file. */
9429 mkspell(1, &fname, FALSE, TRUE, TRUE);
9430
9431 /* If the .add file is edited somewhere, reload it. */
9432 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00009433 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009434
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009435 redraw_all_later(SOME_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009436 }
9437}
9438
9439/*
9440 * Initialize 'spellfile' for the current buffer.
9441 */
9442 static void
9443init_spellfile()
9444{
9445 char_u buf[MAXPATHL];
9446 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009447 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009448 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009449 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009450 int aspath = FALSE;
9451 char_u *lstart = curbuf->b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009452
9453 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
9454 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009455 /* Find the end of the language name. Exclude the region. If there
9456 * is a path separator remember the start of the tail. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009457 for (lend = curbuf->b_p_spl; *lend != NUL
9458 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009459 if (vim_ispathsep(*lend))
9460 {
9461 aspath = TRUE;
9462 lstart = lend + 1;
9463 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009464
9465 /* Loop over all entries in 'runtimepath'. Use the first one where we
9466 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009467 rtp = p_rtp;
9468 while (*rtp != NUL)
9469 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009470 if (aspath)
9471 /* Use directory of an entry with path, e.g., for
9472 * "/dir/lg.utf-8.spl" use "/dir". */
9473 vim_strncpy(buf, curbuf->b_p_spl, lstart - curbuf->b_p_spl - 1);
9474 else
9475 /* Copy the path from 'runtimepath' to buf[]. */
9476 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009477 if (filewritable(buf) == 2)
9478 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00009479 /* Use the first language name from 'spelllang' and the
9480 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009481 if (aspath)
9482 vim_strncpy(buf, curbuf->b_p_spl, lend - curbuf->b_p_spl);
9483 else
9484 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009485 /* Create the "spell" directory if it doesn't exist yet. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009486 l = (int)STRLEN(buf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009487 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
9488 if (!filewritable(buf) != 2)
9489 vim_mkdir(buf, 0755);
9490
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009491 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009492 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009493 "/%.*s", (int)(lend - lstart), lstart);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009494 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009495 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009496 fname = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang->sl_fname;
9497 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
9498 fname != NULL
9499 && strstr((char *)gettail(fname), ".ascii.") != NULL
9500 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009501 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
9502 break;
9503 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009504 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009505 }
9506 }
9507}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009508
Bram Moolenaar51485f02005-06-04 21:55:20 +00009509
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009510/*
9511 * Init the chartab used for spelling for ASCII.
9512 * EBCDIC is not supported!
9513 */
9514 static void
9515clear_spell_chartab(sp)
9516 spelltab_T *sp;
9517{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009518 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009519
9520 /* Init everything to FALSE. */
9521 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
9522 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
9523 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009524 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009525 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009526 sp->st_upper[i] = i;
9527 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009528
9529 /* We include digits. A word shouldn't start with a digit, but handling
9530 * that is done separately. */
9531 for (i = '0'; i <= '9'; ++i)
9532 sp->st_isw[i] = TRUE;
9533 for (i = 'A'; i <= 'Z'; ++i)
9534 {
9535 sp->st_isw[i] = TRUE;
9536 sp->st_isu[i] = TRUE;
9537 sp->st_fold[i] = i + 0x20;
9538 }
9539 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009540 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009541 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009542 sp->st_upper[i] = i - 0x20;
9543 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009544}
9545
9546/*
9547 * Init the chartab used for spelling. Only depends on 'encoding'.
9548 * Called once while starting up and when 'encoding' changes.
9549 * The default is to use isalpha(), but the spell file should define the word
9550 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009551 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009552 */
9553 void
9554init_spell_chartab()
9555{
9556 int i;
9557
9558 did_set_spelltab = FALSE;
9559 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009560#ifdef FEAT_MBYTE
9561 if (enc_dbcs)
9562 {
9563 /* DBCS: assume double-wide characters are word characters. */
9564 for (i = 128; i <= 255; ++i)
9565 if (MB_BYTE2LEN(i) == 2)
9566 spelltab.st_isw[i] = TRUE;
9567 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009568 else if (enc_utf8)
9569 {
9570 for (i = 128; i < 256; ++i)
9571 {
9572 spelltab.st_isu[i] = utf_isupper(i);
9573 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
9574 spelltab.st_fold[i] = utf_fold(i);
9575 spelltab.st_upper[i] = utf_toupper(i);
9576 }
9577 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009578 else
9579#endif
9580 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009581 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009582 for (i = 128; i < 256; ++i)
9583 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009584 if (MB_ISUPPER(i))
9585 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009586 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009587 spelltab.st_isu[i] = TRUE;
9588 spelltab.st_fold[i] = MB_TOLOWER(i);
9589 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009590 else if (MB_ISLOWER(i))
9591 {
9592 spelltab.st_isw[i] = TRUE;
9593 spelltab.st_upper[i] = MB_TOUPPER(i);
9594 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009595 }
9596 }
9597}
9598
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009599/*
9600 * Set the spell character tables from strings in the affix file.
9601 */
9602 static int
9603set_spell_chartab(fol, low, upp)
9604 char_u *fol;
9605 char_u *low;
9606 char_u *upp;
9607{
9608 /* We build the new tables here first, so that we can compare with the
9609 * previous one. */
9610 spelltab_T new_st;
9611 char_u *pf = fol, *pl = low, *pu = upp;
9612 int f, l, u;
9613
9614 clear_spell_chartab(&new_st);
9615
9616 while (*pf != NUL)
9617 {
9618 if (*pl == NUL || *pu == NUL)
9619 {
9620 EMSG(_(e_affform));
9621 return FAIL;
9622 }
9623#ifdef FEAT_MBYTE
9624 f = mb_ptr2char_adv(&pf);
9625 l = mb_ptr2char_adv(&pl);
9626 u = mb_ptr2char_adv(&pu);
9627#else
9628 f = *pf++;
9629 l = *pl++;
9630 u = *pu++;
9631#endif
9632 /* Every character that appears is a word character. */
9633 if (f < 256)
9634 new_st.st_isw[f] = TRUE;
9635 if (l < 256)
9636 new_st.st_isw[l] = TRUE;
9637 if (u < 256)
9638 new_st.st_isw[u] = TRUE;
9639
9640 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9641 * case-folding */
9642 if (l < 256 && l != f)
9643 {
9644 if (f >= 256)
9645 {
9646 EMSG(_(e_affrange));
9647 return FAIL;
9648 }
9649 new_st.st_fold[l] = f;
9650 }
9651
9652 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009653 * case-folding, it's upper case and the "UPP" is the upper case of
9654 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009655 if (u < 256 && u != f)
9656 {
9657 if (f >= 256)
9658 {
9659 EMSG(_(e_affrange));
9660 return FAIL;
9661 }
9662 new_st.st_fold[u] = f;
9663 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009664 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009665 }
9666 }
9667
9668 if (*pl != NUL || *pu != NUL)
9669 {
9670 EMSG(_(e_affform));
9671 return FAIL;
9672 }
9673
9674 return set_spell_finish(&new_st);
9675}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009676
9677/*
9678 * Set the spell character tables from strings in the .spl file.
9679 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009680 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009681set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009682 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009683 int cnt; /* length of "flags" */
9684 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009685{
9686 /* We build the new tables here first, so that we can compare with the
9687 * previous one. */
9688 spelltab_T new_st;
9689 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009690 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009691 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009692
9693 clear_spell_chartab(&new_st);
9694
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009695 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009696 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009697 if (i < cnt)
9698 {
9699 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9700 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9701 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009702
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009703 if (*p != NUL)
9704 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009705#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009706 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009707#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009708 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009709#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009710 new_st.st_fold[i + 128] = c;
9711 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9712 new_st.st_upper[c] = i + 128;
9713 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009714 }
9715
Bram Moolenaar5195e452005-08-19 20:32:47 +00009716 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009717}
9718
9719 static int
9720set_spell_finish(new_st)
9721 spelltab_T *new_st;
9722{
9723 int i;
9724
9725 if (did_set_spelltab)
9726 {
9727 /* check that it's the same table */
9728 for (i = 0; i < 256; ++i)
9729 {
9730 if (spelltab.st_isw[i] != new_st->st_isw[i]
9731 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009732 || spelltab.st_fold[i] != new_st->st_fold[i]
9733 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009734 {
9735 EMSG(_("E763: Word characters differ between spell files"));
9736 return FAIL;
9737 }
9738 }
9739 }
9740 else
9741 {
9742 /* copy the new spelltab into the one being used */
9743 spelltab = *new_st;
9744 did_set_spelltab = TRUE;
9745 }
9746
9747 return OK;
9748}
9749
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009750/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009751 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009752 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009753 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009754 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009755 */
9756 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009757spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00009758 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009759 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009760{
Bram Moolenaarea408852005-06-25 22:49:46 +00009761#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009762 char_u *s;
9763 int l;
9764 int c;
9765
9766 if (has_mbyte)
9767 {
9768 l = MB_BYTE2LEN(*p);
9769 s = p;
9770 if (l == 1)
9771 {
9772 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009773 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009774 {
9775 s = p + 1; /* skip a mid-word character */
9776 l = MB_BYTE2LEN(*s);
9777 }
9778 }
9779 else
9780 {
9781 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009782 if (c < 256 ? buf->b_spell_ismw[c]
9783 : (buf->b_spell_ismw_mb != NULL
9784 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009785 {
9786 s = p + l;
9787 l = MB_BYTE2LEN(*s);
9788 }
9789 }
9790
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009791 c = mb_ptr2char(s);
9792 if (c > 255)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009793 return spell_mb_isword_class(mb_get_class(s));
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009794 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009795 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009796#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009797
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009798 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
9799}
9800
9801/*
9802 * Return TRUE if "p" points to a word character.
9803 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9804 */
9805 static int
9806spell_iswordp_nmw(p)
9807 char_u *p;
9808{
9809#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009810 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009811
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009812 if (has_mbyte)
9813 {
9814 c = mb_ptr2char(p);
9815 if (c > 255)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009816 return spell_mb_isword_class(mb_get_class(p));
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009817 return spelltab.st_isw[c];
9818 }
9819#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009820 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009821}
9822
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009823#ifdef FEAT_MBYTE
9824/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009825 * Return TRUE if word class indicates a word character.
9826 * Only for characters above 255.
9827 * Unicode subscript and superscript are not considered word characters.
9828 */
9829 static int
9830spell_mb_isword_class(cl)
9831 int cl;
9832{
9833 return cl >= 2 && cl != 0x2070 && cl != 0x2080;
9834}
9835
9836/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009837 * Return TRUE if "p" points to a word character.
9838 * Wide version of spell_iswordp().
9839 */
9840 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009841spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009842 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009843 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009844{
9845 int *s;
9846
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009847 if (*p < 256 ? buf->b_spell_ismw[*p]
9848 : (buf->b_spell_ismw_mb != NULL
9849 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009850 s = p + 1;
9851 else
9852 s = p;
9853
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009854 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009855 {
9856 if (enc_utf8)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009857 return spell_mb_isword_class(utf_class(*s));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009858 if (enc_dbcs)
9859 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
9860 return 0;
9861 }
9862 return spelltab.st_isw[*s];
9863}
9864#endif
9865
Bram Moolenaarea408852005-06-25 22:49:46 +00009866/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009867 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009868 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009869 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009870 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009871write_spell_prefcond(fd, gap)
9872 FILE *fd;
9873 garray_T *gap;
9874{
9875 int i;
9876 char_u *p;
9877 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009878 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009879
Bram Moolenaar5195e452005-08-19 20:32:47 +00009880 if (fd != NULL)
9881 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
9882
9883 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009884
9885 for (i = 0; i < gap->ga_len; ++i)
9886 {
9887 /* <prefcond> : <condlen> <condstr> */
9888 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009889 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009890 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009891 len = (int)STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009892 if (fd != NULL)
9893 {
9894 fputc(len, fd);
9895 fwrite(p, (size_t)len, (size_t)1, fd);
9896 }
9897 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009898 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009899 else if (fd != NULL)
9900 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009901 }
9902
Bram Moolenaar5195e452005-08-19 20:32:47 +00009903 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009904}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009905
9906/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009907 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
9908 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009909 * When using a multi-byte 'encoding' the length may change!
9910 * Returns FAIL when something wrong.
9911 */
9912 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009913spell_casefold(str, len, buf, buflen)
9914 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009915 int len;
9916 char_u *buf;
9917 int buflen;
9918{
9919 int i;
9920
9921 if (len >= buflen)
9922 {
9923 buf[0] = NUL;
9924 return FAIL; /* result will not fit */
9925 }
9926
9927#ifdef FEAT_MBYTE
9928 if (has_mbyte)
9929 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009930 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009931 char_u *p;
9932 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009933
9934 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009935 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009936 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009937 if (outi + MB_MAXBYTES > buflen)
9938 {
9939 buf[outi] = NUL;
9940 return FAIL;
9941 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009942 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009943 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009944 }
9945 buf[outi] = NUL;
9946 }
9947 else
9948#endif
9949 {
9950 /* Be quick for non-multibyte encodings. */
9951 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009952 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009953 buf[i] = NUL;
9954 }
9955
9956 return OK;
9957}
9958
Bram Moolenaar4770d092006-01-12 23:22:24 +00009959/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009960#define SPS_BEST 1
9961#define SPS_FAST 2
9962#define SPS_DOUBLE 4
9963
Bram Moolenaar4770d092006-01-12 23:22:24 +00009964static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
9965static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009966
9967/*
9968 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009969 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009970 */
9971 int
9972spell_check_sps()
9973{
9974 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009975 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009976 char_u buf[MAXPATHL];
9977 int f;
9978
9979 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009980 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009981
9982 for (p = p_sps; *p != NUL; )
9983 {
9984 copy_option_part(&p, buf, MAXPATHL, ",");
9985
9986 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009987 if (VIM_ISDIGIT(*buf))
9988 {
9989 s = buf;
9990 sps_limit = getdigits(&s);
9991 if (*s != NUL && !VIM_ISDIGIT(*s))
9992 f = -1;
9993 }
9994 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009995 f = SPS_BEST;
9996 else if (STRCMP(buf, "fast") == 0)
9997 f = SPS_FAST;
9998 else if (STRCMP(buf, "double") == 0)
9999 f = SPS_DOUBLE;
10000 else if (STRNCMP(buf, "expr:", 5) != 0
10001 && STRNCMP(buf, "file:", 5) != 0)
10002 f = -1;
10003
10004 if (f == -1 || (sps_flags != 0 && f != 0))
10005 {
10006 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010007 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010008 return FAIL;
10009 }
10010 if (f != 0)
10011 sps_flags = f;
10012 }
10013
10014 if (sps_flags == 0)
10015 sps_flags = SPS_BEST;
10016
10017 return OK;
10018}
10019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010020/*
10021 * "z?": Find badly spelled word under or after the cursor.
10022 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010023 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +000010024 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010025 */
10026 void
Bram Moolenaard12a1322005-08-21 22:08:24 +000010027spell_suggest(count)
10028 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010029{
10030 char_u *line;
10031 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010032 char_u wcopy[MAXWLEN + 2];
10033 char_u *p;
10034 int i;
10035 int c;
10036 suginfo_T sug;
10037 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010038 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010039 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010040 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010041 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010042 int badlen = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010043
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010044 if (no_spell_checking(curwin))
10045 return;
10046
10047#ifdef FEAT_VISUAL
10048 if (VIsual_active)
10049 {
10050 /* Use the Visually selected text as the bad word. But reject
10051 * a multi-line selection. */
10052 if (curwin->w_cursor.lnum != VIsual.lnum)
10053 {
10054 vim_beep();
10055 return;
10056 }
10057 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
10058 if (badlen < 0)
10059 badlen = -badlen;
10060 else
10061 curwin->w_cursor.col = VIsual.col;
10062 ++badlen;
10063 end_visual_mode();
10064 }
10065 else
10066#endif
10067 /* Find the start of the badly spelled word. */
10068 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +000010069 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010070 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010071 /* No bad word or it starts after the cursor: use the word under the
10072 * cursor. */
10073 curwin->w_cursor = prev_cursor;
10074 line = ml_get_curline();
10075 p = line + curwin->w_cursor.col;
10076 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010077 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010078 mb_ptr_back(line, p);
10079 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010080 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010081 mb_ptr_adv(p);
10082
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010083 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010084 {
10085 beep_flush();
10086 return;
10087 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010088 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010089 }
10090
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010091 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010092
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010093 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010094 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010095
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010096 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010097
Bram Moolenaar5195e452005-08-19 20:32:47 +000010098 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
10099 * 'spellsuggest', whatever is smaller. */
10100 if (sps_limit > (int)Rows - 2)
10101 limit = (int)Rows - 2;
10102 else
10103 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010104 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010105 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010106
10107 if (sug.su_ga.ga_len == 0)
10108 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +000010109 else if (count > 0)
10110 {
10111 if (count > sug.su_ga.ga_len)
10112 smsg((char_u *)_("Sorry, only %ld suggestions"),
10113 (long)sug.su_ga.ga_len);
10114 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010115 else
10116 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010117 vim_free(repl_from);
10118 repl_from = NULL;
10119 vim_free(repl_to);
10120 repl_to = NULL;
10121
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010122#ifdef FEAT_RIGHTLEFT
10123 /* When 'rightleft' is set the list is drawn right-left. */
10124 cmdmsg_rl = curwin->w_p_rl;
10125 if (cmdmsg_rl)
10126 msg_col = Columns - 1;
10127#endif
10128
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010129 /* List the suggestions. */
10130 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000010131 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010132 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010133 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
10134 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010135#ifdef FEAT_RIGHTLEFT
10136 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
10137 {
10138 /* And now the rabbit from the high hat: Avoid showing the
10139 * untranslated message rightleft. */
10140 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
10141 sug.su_badlen, sug.su_badptr);
10142 }
10143#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010144 msg_puts(IObuff);
10145 msg_clr_eos();
10146 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +000010147
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010148 msg_scroll = TRUE;
10149 for (i = 0; i < sug.su_ga.ga_len; ++i)
10150 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010151 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010152
10153 /* The suggested word may replace only part of the bad word, add
10154 * the not replaced part. */
10155 STRCPY(wcopy, stp->st_word);
10156 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010157 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010158 sug.su_badptr + stp->st_orglen,
10159 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010160 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
10161#ifdef FEAT_RIGHTLEFT
10162 if (cmdmsg_rl)
10163 rl_mirror(IObuff);
10164#endif
10165 msg_puts(IObuff);
10166
10167 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010168 msg_puts(IObuff);
10169
10170 /* The word may replace more than "su_badlen". */
10171 if (sug.su_badlen < stp->st_orglen)
10172 {
10173 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
10174 stp->st_orglen, sug.su_badptr);
10175 msg_puts(IObuff);
10176 }
10177
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010178 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010179 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010180 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010181 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010182 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010183 stp->st_salscore ? "s " : "",
10184 stp->st_score, stp->st_altscore);
10185 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010186 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +000010187 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010188#ifdef FEAT_RIGHTLEFT
10189 if (cmdmsg_rl)
10190 /* Mirror the numbers, but keep the leading space. */
10191 rl_mirror(IObuff + 1);
10192#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +000010193 msg_advance(30);
10194 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010195 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010196 msg_putchar('\n');
10197 }
10198
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010199#ifdef FEAT_RIGHTLEFT
10200 cmdmsg_rl = FALSE;
10201 msg_col = 0;
10202#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010203 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010204 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010205 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010206 selected -= lines_left;
Bram Moolenaar0fd92892006-03-09 22:27:48 +000010207 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010208 }
10209
Bram Moolenaard12a1322005-08-21 22:08:24 +000010210 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
10211 {
10212 /* Save the from and to text for :spellrepall. */
10213 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000010214 if (sug.su_badlen > stp->st_orglen)
10215 {
10216 /* Replacing less than "su_badlen", append the remainder to
10217 * repl_to. */
10218 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
10219 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
10220 sug.su_badlen - stp->st_orglen,
10221 sug.su_badptr + stp->st_orglen);
10222 repl_to = vim_strsave(IObuff);
10223 }
10224 else
10225 {
10226 /* Replacing su_badlen or more, use the whole word. */
10227 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
10228 repl_to = vim_strsave(stp->st_word);
10229 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010230
10231 /* Replace the word. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010232 p = alloc((unsigned)STRLEN(line) - stp->st_orglen + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010233 if (p != NULL)
10234 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010235 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010236 mch_memmove(p, line, c);
10237 STRCPY(p + c, stp->st_word);
10238 STRCAT(p, sug.su_badptr + stp->st_orglen);
10239 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10240 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010241
10242 /* For redo we use a change-word command. */
10243 ResetRedobuff();
10244 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +000010245 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010246 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010247 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010248
10249 /* After this "p" may be invalid. */
10250 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010251 }
10252 }
10253 else
10254 curwin->w_cursor = prev_cursor;
10255
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010256 spell_find_cleanup(&sug);
10257}
10258
10259/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010260 * Check if the word at line "lnum" column "col" is required to start with a
10261 * capital. This uses 'spellcapcheck' of the current buffer.
10262 */
10263 static int
10264check_need_cap(lnum, col)
10265 linenr_T lnum;
10266 colnr_T col;
10267{
10268 int need_cap = FALSE;
10269 char_u *line;
10270 char_u *line_copy = NULL;
10271 char_u *p;
10272 colnr_T endcol;
10273 regmatch_T regmatch;
10274
10275 if (curbuf->b_cap_prog == NULL)
10276 return FALSE;
10277
10278 line = ml_get_curline();
10279 endcol = 0;
10280 if ((int)(skipwhite(line) - line) >= (int)col)
10281 {
10282 /* At start of line, check if previous line is empty or sentence
10283 * ends there. */
10284 if (lnum == 1)
10285 need_cap = TRUE;
10286 else
10287 {
10288 line = ml_get(lnum - 1);
10289 if (*skipwhite(line) == NUL)
10290 need_cap = TRUE;
10291 else
10292 {
10293 /* Append a space in place of the line break. */
10294 line_copy = concat_str(line, (char_u *)" ");
10295 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010296 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010297 }
10298 }
10299 }
10300 else
10301 endcol = col;
10302
10303 if (endcol > 0)
10304 {
10305 /* Check if sentence ends before the bad word. */
10306 regmatch.regprog = curbuf->b_cap_prog;
10307 regmatch.rm_ic = FALSE;
10308 p = line + endcol;
10309 for (;;)
10310 {
10311 mb_ptr_back(line, p);
10312 if (p == line || spell_iswordp_nmw(p))
10313 break;
10314 if (vim_regexec(&regmatch, p, 0)
10315 && regmatch.endp[0] == line + endcol)
10316 {
10317 need_cap = TRUE;
10318 break;
10319 }
10320 }
10321 }
10322
10323 vim_free(line_copy);
10324
10325 return need_cap;
10326}
10327
10328
10329/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010330 * ":spellrepall"
10331 */
10332/*ARGSUSED*/
10333 void
10334ex_spellrepall(eap)
10335 exarg_T *eap;
10336{
10337 pos_T pos = curwin->w_cursor;
10338 char_u *frompat;
10339 int addlen;
10340 char_u *line;
10341 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010342 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010343 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010344
10345 if (repl_from == NULL || repl_to == NULL)
10346 {
10347 EMSG(_("E752: No previous spell replacement"));
10348 return;
10349 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010350 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010351
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010352 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010353 if (frompat == NULL)
10354 return;
10355 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
10356 p_ws = FALSE;
10357
Bram Moolenaar5195e452005-08-19 20:32:47 +000010358 sub_nsubs = 0;
10359 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010360 curwin->w_cursor.lnum = 0;
10361 while (!got_int)
10362 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +000010363 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010364 || u_save_cursor() == FAIL)
10365 break;
10366
10367 /* Only replace when the right word isn't there yet. This happens
10368 * when changing "etc" to "etc.". */
10369 line = ml_get_curline();
10370 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
10371 repl_to, STRLEN(repl_to)) != 0)
10372 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010373 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010374 if (p == NULL)
10375 break;
10376 mch_memmove(p, line, curwin->w_cursor.col);
10377 STRCPY(p + curwin->w_cursor.col, repl_to);
10378 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
10379 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10380 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010381
10382 if (curwin->w_cursor.lnum != prev_lnum)
10383 {
10384 ++sub_nlines;
10385 prev_lnum = curwin->w_cursor.lnum;
10386 }
10387 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010388 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010389 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010390 }
10391
10392 p_ws = save_ws;
10393 curwin->w_cursor = pos;
10394 vim_free(frompat);
10395
Bram Moolenaar5195e452005-08-19 20:32:47 +000010396 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010397 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010398 else
10399 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010400}
10401
10402/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010403 * Find spell suggestions for "word". Return them in the growarray "*gap" as
10404 * a list of allocated strings.
10405 */
10406 void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010407spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010408 garray_T *gap;
10409 char_u *word;
10410 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010411 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010412 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010413{
10414 suginfo_T sug;
10415 int i;
10416 suggest_T *stp;
10417 char_u *wcopy;
10418
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010419 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010420
10421 /* Make room in "gap". */
10422 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010423 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010424 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010425 for (i = 0; i < sug.su_ga.ga_len; ++i)
10426 {
10427 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010428
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010429 /* The suggested word may replace only part of "word", add the not
10430 * replaced part. */
10431 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010432 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010433 if (wcopy == NULL)
10434 break;
10435 STRCPY(wcopy, stp->st_word);
10436 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
10437 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
10438 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010439 }
10440
10441 spell_find_cleanup(&sug);
10442}
10443
10444/*
10445 * Find spell suggestions for the word at the start of "badptr".
10446 * Return the suggestions in "su->su_ga".
10447 * The maximum number of suggestions is "maxcount".
10448 * Note: does use info for the current window.
10449 * This is based on the mechanisms of Aspell, but completely reimplemented.
10450 */
10451 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010452spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010453 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010454 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010455 suginfo_T *su;
10456 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +000010457 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010458 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010459 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010460{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010461 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010462 char_u buf[MAXPATHL];
10463 char_u *p;
10464 int do_combine = FALSE;
10465 char_u *sps_copy;
10466#ifdef FEAT_EVAL
10467 static int expr_busy = FALSE;
10468#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010469 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010470 int i;
10471 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010472
10473 /*
10474 * Set the info in "*su".
10475 */
10476 vim_memset(su, 0, sizeof(suginfo_T));
10477 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10478 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010479 if (*badptr == NUL)
10480 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010481 hash_init(&su->su_banned);
10482
10483 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010484 if (badlen != 0)
10485 su->su_badlen = badlen;
10486 else
10487 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010488 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010489 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010490
10491 if (su->su_badlen >= MAXWLEN)
10492 su->su_badlen = MAXWLEN - 1; /* just in case */
10493 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10494 (void)spell_casefold(su->su_badptr, su->su_badlen,
10495 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010496 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010497 su->su_badflags = badword_captype(su->su_badptr,
10498 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010499 if (need_cap)
10500 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010501
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010502 /* Find the default language for sound folding. We simply use the first
10503 * one in 'spelllang' that supports sound folding. That's good for when
10504 * using multiple files for one language, it's not that bad when mixing
10505 * languages (e.g., "pl,en"). */
10506 for (i = 0; i < curbuf->b_langp.ga_len; ++i)
10507 {
10508 lp = LANGP_ENTRY(curbuf->b_langp, i);
10509 if (lp->lp_sallang != NULL)
10510 {
10511 su->su_sallang = lp->lp_sallang;
10512 break;
10513 }
10514 }
10515
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010516 /* Soundfold the bad word with the default sound folding, so that we don't
10517 * have to do this many times. */
10518 if (su->su_sallang != NULL)
10519 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10520 su->su_sal_badword);
10521
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010522 /* If the word is not capitalised and spell_check() doesn't consider the
10523 * word to be bad then it might need to be capitalised. Add a suggestion
10524 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010525 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010526 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010527 {
10528 make_case_word(su->su_badword, buf, WF_ONECAP);
10529 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010530 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010531 }
10532
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010533 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +000010534 if (banbadword)
10535 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010536
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010537 /* Make a copy of 'spellsuggest', because the expression may change it. */
10538 sps_copy = vim_strsave(p_sps);
10539 if (sps_copy == NULL)
10540 return;
10541
10542 /* Loop over the items in 'spellsuggest'. */
10543 for (p = sps_copy; *p != NUL; )
10544 {
10545 copy_option_part(&p, buf, MAXPATHL, ",");
10546
10547 if (STRNCMP(buf, "expr:", 5) == 0)
10548 {
10549#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010550 /* Evaluate an expression. Skip this when called recursively,
10551 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010552 if (!expr_busy)
10553 {
10554 expr_busy = TRUE;
10555 spell_suggest_expr(su, buf + 5);
10556 expr_busy = FALSE;
10557 }
10558#endif
10559 }
10560 else if (STRNCMP(buf, "file:", 5) == 0)
10561 /* Use list of suggestions in a file. */
10562 spell_suggest_file(su, buf + 5);
10563 else
10564 {
10565 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010566 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010567 if (sps_flags & SPS_DOUBLE)
10568 do_combine = TRUE;
10569 }
10570 }
10571
10572 vim_free(sps_copy);
10573
10574 if (do_combine)
10575 /* Combine the two list of suggestions. This must be done last,
10576 * because sorting changes the order again. */
10577 score_combine(su);
10578}
10579
10580#ifdef FEAT_EVAL
10581/*
10582 * Find suggestions by evaluating expression "expr".
10583 */
10584 static void
10585spell_suggest_expr(su, expr)
10586 suginfo_T *su;
10587 char_u *expr;
10588{
10589 list_T *list;
10590 listitem_T *li;
10591 int score;
10592 char_u *p;
10593
10594 /* The work is split up in a few parts to avoid having to export
10595 * suginfo_T.
10596 * First evaluate the expression and get the resulting list. */
10597 list = eval_spell_expr(su->su_badword, expr);
10598 if (list != NULL)
10599 {
10600 /* Loop over the items in the list. */
10601 for (li = list->lv_first; li != NULL; li = li->li_next)
10602 if (li->li_tv.v_type == VAR_LIST)
10603 {
10604 /* Get the word and the score from the items. */
10605 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010606 if (score >= 0 && score <= su->su_maxscore)
10607 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10608 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010609 }
10610 list_unref(list);
10611 }
10612
Bram Moolenaar4770d092006-01-12 23:22:24 +000010613 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10614 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010615 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10616}
10617#endif
10618
10619/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010620 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010621 */
10622 static void
10623spell_suggest_file(su, fname)
10624 suginfo_T *su;
10625 char_u *fname;
10626{
10627 FILE *fd;
10628 char_u line[MAXWLEN * 2];
10629 char_u *p;
10630 int len;
10631 char_u cword[MAXWLEN];
10632
10633 /* Open the file. */
10634 fd = mch_fopen((char *)fname, "r");
10635 if (fd == NULL)
10636 {
10637 EMSG2(_(e_notopen), fname);
10638 return;
10639 }
10640
10641 /* Read it line by line. */
10642 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10643 {
10644 line_breakcheck();
10645
10646 p = vim_strchr(line, '/');
10647 if (p == NULL)
10648 continue; /* No Tab found, just skip the line. */
10649 *p++ = NUL;
10650 if (STRICMP(su->su_badword, line) == 0)
10651 {
10652 /* Match! Isolate the good word, until CR or NL. */
10653 for (len = 0; p[len] >= ' '; ++len)
10654 ;
10655 p[len] = NUL;
10656
10657 /* If the suggestion doesn't have specific case duplicate the case
10658 * of the bad word. */
10659 if (captype(p, NULL) == 0)
10660 {
10661 make_case_word(p, cword, su->su_badflags);
10662 p = cword;
10663 }
10664
10665 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010666 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010667 }
10668 }
10669
10670 fclose(fd);
10671
Bram Moolenaar4770d092006-01-12 23:22:24 +000010672 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10673 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010674 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10675}
10676
10677/*
10678 * Find suggestions for the internal method indicated by "sps_flags".
10679 */
10680 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010681spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010682 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010683 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010684{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010685 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010686 * Load the .sug file(s) that are available and not done yet.
10687 */
10688 suggest_load_files();
10689
10690 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010691 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010692 *
10693 * Set a maximum score to limit the combination of operations that is
10694 * tried.
10695 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010696 suggest_try_special(su);
10697
10698 /*
10699 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10700 * from the .aff file and inserting a space (split the word).
10701 */
10702 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010703
10704 /* For the resulting top-scorers compute the sound-a-like score. */
10705 if (sps_flags & SPS_DOUBLE)
10706 score_comp_sal(su);
10707
10708 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010709 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010710 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010711 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010712 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010713 if (sps_flags & SPS_BEST)
10714 /* Adjust the word score for the suggestions found so far for how
10715 * they sounds like. */
10716 rescore_suggestions(su);
10717
10718 /*
10719 * While going throught the soundfold tree "su_maxscore" is the score
10720 * for the soundfold word, limits the changes that are being tried,
10721 * and "su_sfmaxscore" the rescored score, which is set by
10722 * cleanup_suggestions().
10723 * First find words with a small edit distance, because this is much
10724 * faster and often already finds the top-N suggestions. If we didn't
10725 * find many suggestions try again with a higher edit distance.
10726 * "sl_sounddone" is used to avoid doing the same word twice.
10727 */
10728 suggest_try_soundalike_prep();
10729 su->su_maxscore = SCORE_SFMAX1;
10730 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010731 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010732 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10733 {
10734 /* We didn't find enough matches, try again, allowing more
10735 * changes to the soundfold word. */
10736 su->su_maxscore = SCORE_SFMAX2;
10737 suggest_try_soundalike(su);
10738 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10739 {
10740 /* Still didn't find enough matches, try again, allowing even
10741 * more changes to the soundfold word. */
10742 su->su_maxscore = SCORE_SFMAX3;
10743 suggest_try_soundalike(su);
10744 }
10745 }
10746 su->su_maxscore = su->su_sfmaxscore;
10747 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010748 }
10749
Bram Moolenaar4770d092006-01-12 23:22:24 +000010750 /* When CTRL-C was hit while searching do show the results. Only clear
10751 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010752 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010753 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010754 {
10755 (void)vgetc();
10756 got_int = FALSE;
10757 }
10758
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010759 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010760 {
10761 if (sps_flags & SPS_BEST)
10762 /* Adjust the word score for how it sounds like. */
10763 rescore_suggestions(su);
10764
Bram Moolenaar4770d092006-01-12 23:22:24 +000010765 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10766 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010767 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010768 }
10769}
10770
10771/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010772 * Load the .sug files for languages that have one and weren't loaded yet.
10773 */
10774 static void
10775suggest_load_files()
10776{
10777 langp_T *lp;
10778 int lpi;
10779 slang_T *slang;
10780 char_u *dotp;
10781 FILE *fd;
10782 char_u buf[MAXWLEN];
10783 int i;
10784 time_t timestamp;
10785 int wcount;
10786 int wordnr;
10787 garray_T ga;
10788 int c;
10789
10790 /* Do this for all languages that support sound folding. */
10791 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
10792 {
10793 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
10794 slang = lp->lp_slang;
10795 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10796 {
10797 /* Change ".spl" to ".sug" and open the file. When the file isn't
10798 * found silently skip it. Do set "sl_sugloaded" so that we
10799 * don't try again and again. */
10800 slang->sl_sugloaded = TRUE;
10801
10802 dotp = vim_strrchr(slang->sl_fname, '.');
10803 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10804 continue;
10805 STRCPY(dotp, ".sug");
Bram Moolenaar5555acc2006-04-07 21:33:12 +000010806 fd = mch_fopen((char *)slang->sl_fname, "r");
Bram Moolenaar4770d092006-01-12 23:22:24 +000010807 if (fd == NULL)
10808 goto nextone;
10809
10810 /*
10811 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10812 */
10813 for (i = 0; i < VIMSUGMAGICL; ++i)
10814 buf[i] = getc(fd); /* <fileID> */
10815 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10816 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010817 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010818 slang->sl_fname);
10819 goto nextone;
10820 }
10821 c = getc(fd); /* <versionnr> */
10822 if (c < VIMSUGVERSION)
10823 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010824 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010825 slang->sl_fname);
10826 goto nextone;
10827 }
10828 else if (c > VIMSUGVERSION)
10829 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010830 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010831 slang->sl_fname);
10832 goto nextone;
10833 }
10834
10835 /* Check the timestamp, it must be exactly the same as the one in
10836 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010837 timestamp = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010838 if (timestamp != slang->sl_sugtime)
10839 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010840 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010841 slang->sl_fname);
10842 goto nextone;
10843 }
10844
10845 /*
10846 * <SUGWORDTREE>: <wordtree>
10847 * Read the trie with the soundfolded words.
10848 */
10849 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10850 FALSE, 0) != 0)
10851 {
10852someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010853 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010854 slang->sl_fname);
10855 slang_clear_sug(slang);
10856 goto nextone;
10857 }
10858
10859 /*
10860 * <SUGTABLE>: <sugwcount> <sugline> ...
10861 *
10862 * Read the table with word numbers. We use a file buffer for
10863 * this, because it's so much like a file with lines. Makes it
10864 * possible to swap the info and save on memory use.
10865 */
10866 slang->sl_sugbuf = open_spellbuf();
10867 if (slang->sl_sugbuf == NULL)
10868 goto someerror;
10869 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010870 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010871 if (wcount < 0)
10872 goto someerror;
10873
10874 /* Read all the wordnr lists into the buffer, one NUL terminated
10875 * list per line. */
10876 ga_init2(&ga, 1, 100);
10877 for (wordnr = 0; wordnr < wcount; ++wordnr)
10878 {
10879 ga.ga_len = 0;
10880 for (;;)
10881 {
10882 c = getc(fd); /* <sugline> */
10883 if (c < 0 || ga_grow(&ga, 1) == FAIL)
10884 goto someerror;
10885 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
10886 if (c == NUL)
10887 break;
10888 }
10889 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
10890 ga.ga_data, ga.ga_len, TRUE) == FAIL)
10891 goto someerror;
10892 }
10893 ga_clear(&ga);
10894
10895 /*
10896 * Need to put word counts in the word tries, so that we can find
10897 * a word by its number.
10898 */
10899 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
10900 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
10901
10902nextone:
10903 if (fd != NULL)
10904 fclose(fd);
10905 STRCPY(dotp, ".spl");
10906 }
10907 }
10908}
10909
10910
10911/*
10912 * Fill in the wordcount fields for a trie.
10913 * Returns the total number of words.
10914 */
10915 static void
10916tree_count_words(byts, idxs)
10917 char_u *byts;
10918 idx_T *idxs;
10919{
10920 int depth;
10921 idx_T arridx[MAXWLEN];
10922 int curi[MAXWLEN];
10923 int c;
10924 idx_T n;
10925 int wordcount[MAXWLEN];
10926
10927 arridx[0] = 0;
10928 curi[0] = 1;
10929 wordcount[0] = 0;
10930 depth = 0;
10931 while (depth >= 0 && !got_int)
10932 {
10933 if (curi[depth] > byts[arridx[depth]])
10934 {
10935 /* Done all bytes at this node, go up one level. */
10936 idxs[arridx[depth]] = wordcount[depth];
10937 if (depth > 0)
10938 wordcount[depth - 1] += wordcount[depth];
10939
10940 --depth;
10941 fast_breakcheck();
10942 }
10943 else
10944 {
10945 /* Do one more byte at this node. */
10946 n = arridx[depth] + curi[depth];
10947 ++curi[depth];
10948
10949 c = byts[n];
10950 if (c == 0)
10951 {
10952 /* End of word, count it. */
10953 ++wordcount[depth];
10954
10955 /* Skip over any other NUL bytes (same word with different
10956 * flags). */
10957 while (byts[n + 1] == 0)
10958 {
10959 ++n;
10960 ++curi[depth];
10961 }
10962 }
10963 else
10964 {
10965 /* Normal char, go one level deeper to count the words. */
10966 ++depth;
10967 arridx[depth] = idxs[n];
10968 curi[depth] = 1;
10969 wordcount[depth] = 0;
10970 }
10971 }
10972 }
10973}
10974
10975/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010976 * Free the info put in "*su" by spell_find_suggest().
10977 */
10978 static void
10979spell_find_cleanup(su)
10980 suginfo_T *su;
10981{
10982 int i;
10983
10984 /* Free the suggestions. */
10985 for (i = 0; i < su->su_ga.ga_len; ++i)
10986 vim_free(SUG(su->su_ga, i).st_word);
10987 ga_clear(&su->su_ga);
10988 for (i = 0; i < su->su_sga.ga_len; ++i)
10989 vim_free(SUG(su->su_sga, i).st_word);
10990 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010991
10992 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010993 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010994}
10995
10996/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010997 * Make a copy of "word", with the first letter upper or lower cased, to
10998 * "wcopy[MAXWLEN]". "word" must not be empty.
10999 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011000 */
11001 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011002onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011003 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011004 char_u *wcopy;
11005 int upper; /* TRUE: first letter made upper case */
11006{
11007 char_u *p;
11008 int c;
11009 int l;
11010
11011 p = word;
11012#ifdef FEAT_MBYTE
11013 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011014 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011015 else
11016#endif
11017 c = *p++;
11018 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011019 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011020 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011021 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011022#ifdef FEAT_MBYTE
11023 if (has_mbyte)
11024 l = mb_char2bytes(c, wcopy);
11025 else
11026#endif
11027 {
11028 l = 1;
11029 wcopy[0] = c;
11030 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011031 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011032}
11033
11034/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011035 * Make a copy of "word" with all the letters upper cased into
11036 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011037 */
11038 static void
11039allcap_copy(word, wcopy)
11040 char_u *word;
11041 char_u *wcopy;
11042{
11043 char_u *s;
11044 char_u *d;
11045 int c;
11046
11047 d = wcopy;
11048 for (s = word; *s != NUL; )
11049 {
11050#ifdef FEAT_MBYTE
11051 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011052 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011053 else
11054#endif
11055 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000011056
11057#ifdef FEAT_MBYTE
11058 /* We only change ß to SS when we are certain latin1 is used. It
11059 * would cause weird errors in other 8-bit encodings. */
11060 if (enc_latin1like && c == 0xdf)
11061 {
11062 c = 'S';
11063 if (d - wcopy >= MAXWLEN - 1)
11064 break;
11065 *d++ = c;
11066 }
11067 else
11068#endif
11069 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011070
11071#ifdef FEAT_MBYTE
11072 if (has_mbyte)
11073 {
11074 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
11075 break;
11076 d += mb_char2bytes(c, d);
11077 }
11078 else
11079#endif
11080 {
11081 if (d - wcopy >= MAXWLEN - 1)
11082 break;
11083 *d++ = c;
11084 }
11085 }
11086 *d = NUL;
11087}
11088
11089/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000011090 * Try finding suggestions by recognizing specific situations.
11091 */
11092 static void
11093suggest_try_special(su)
11094 suginfo_T *su;
11095{
11096 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011097 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011098 int c;
11099 char_u word[MAXWLEN];
11100
11101 /*
11102 * Recognize a word that is repeated: "the the".
11103 */
11104 p = skiptowhite(su->su_fbadword);
11105 len = p - su->su_fbadword;
11106 p = skipwhite(p);
11107 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
11108 {
11109 /* Include badflags: if the badword is onecap or allcap
11110 * use that for the goodword too: "The the" -> "The". */
11111 c = su->su_fbadword[len];
11112 su->su_fbadword[len] = NUL;
11113 make_case_word(su->su_fbadword, word, su->su_badflags);
11114 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011115
11116 /* Give a soundalike score of 0, compute the score as if deleting one
11117 * character. */
11118 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011119 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011120 }
11121}
11122
11123/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011124 * Try finding suggestions by adding/removing/swapping letters.
11125 */
11126 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000011127suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011128 suginfo_T *su;
11129{
11130 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011131 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011132 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011133 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011134 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011135
11136 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000011137 * to find matches (esp. REP items). Append some more text, changing
11138 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011139 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011140 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011141 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011142 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011143
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011144 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011145 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011146 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011147
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011148 /* If reloading a spell file fails it's still in the list but
11149 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011150 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011151 continue;
11152
Bram Moolenaar4770d092006-01-12 23:22:24 +000011153 /* Try it for this language. Will add possible suggestions. */
11154 suggest_trie_walk(su, lp, fword, FALSE);
11155 }
11156}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011157
Bram Moolenaar4770d092006-01-12 23:22:24 +000011158/* Check the maximum score, if we go over it we won't try this change. */
11159#define TRY_DEEPER(su, stack, depth, add) \
11160 (stack[depth].ts_score + (add) < su->su_maxscore)
11161
11162/*
11163 * Try finding suggestions by adding/removing/swapping letters.
11164 *
11165 * This uses a state machine. At each node in the tree we try various
11166 * operations. When trying if an operation works "depth" is increased and the
11167 * stack[] is used to store info. This allows combinations, thus insert one
11168 * character, replace one and delete another. The number of changes is
11169 * limited by su->su_maxscore.
11170 *
11171 * After implementing this I noticed an article by Kemal Oflazer that
11172 * describes something similar: "Error-tolerant Finite State Recognition with
11173 * Applications to Morphological Analysis and Spelling Correction" (1996).
11174 * The implementation in the article is simplified and requires a stack of
11175 * unknown depth. The implementation here only needs a stack depth equal to
11176 * the length of the word.
11177 *
11178 * This is also used for the sound-folded word, "soundfold" is TRUE then.
11179 * The mechanism is the same, but we find a match with a sound-folded word
11180 * that comes from one or more original words. Each of these words may be
11181 * added, this is done by add_sound_suggest().
11182 * Don't use:
11183 * the prefix tree or the keep-case tree
11184 * "su->su_badlen"
11185 * anything to do with upper and lower case
11186 * anything to do with word or non-word characters ("spell_iswordp()")
11187 * banned words
11188 * word flags (rare, region, compounding)
11189 * word splitting for now
11190 * "similar_chars()"
11191 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
11192 */
11193 static void
11194suggest_trie_walk(su, lp, fword, soundfold)
11195 suginfo_T *su;
11196 langp_T *lp;
11197 char_u *fword;
11198 int soundfold;
11199{
11200 char_u tword[MAXWLEN]; /* good word collected so far */
11201 trystate_T stack[MAXWLEN];
11202 char_u preword[MAXWLEN * 3]; /* word found with proper case;
11203 * concatanation of prefix compound
11204 * words and split word. NUL terminated
11205 * when going deeper but not when coming
11206 * back. */
11207 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
11208 trystate_T *sp;
11209 int newscore;
11210 int score;
11211 char_u *byts, *fbyts, *pbyts;
11212 idx_T *idxs, *fidxs, *pidxs;
11213 int depth;
11214 int c, c2, c3;
11215 int n = 0;
11216 int flags;
11217 garray_T *gap;
11218 idx_T arridx;
11219 int len;
11220 char_u *p;
11221 fromto_T *ftp;
11222 int fl = 0, tl;
11223 int repextra = 0; /* extra bytes in fword[] from REP item */
11224 slang_T *slang = lp->lp_slang;
11225 int fword_ends;
11226 int goodword_ends;
11227#ifdef DEBUG_TRIEWALK
11228 /* Stores the name of the change made at each level. */
11229 char_u changename[MAXWLEN][80];
11230#endif
11231 int breakcheckcount = 1000;
11232 int compound_ok;
11233
11234 /*
11235 * Go through the whole case-fold tree, try changes at each node.
11236 * "tword[]" contains the word collected from nodes in the tree.
11237 * "fword[]" the word we are trying to match with (initially the bad
11238 * word).
11239 */
11240 depth = 0;
11241 sp = &stack[0];
11242 vim_memset(sp, 0, sizeof(trystate_T));
11243 sp->ts_curi = 1;
11244
11245 if (soundfold)
11246 {
11247 /* Going through the soundfold tree. */
11248 byts = fbyts = slang->sl_sbyts;
11249 idxs = fidxs = slang->sl_sidxs;
11250 pbyts = NULL;
11251 pidxs = NULL;
11252 sp->ts_prefixdepth = PFD_NOPREFIX;
11253 sp->ts_state = STATE_START;
11254 }
11255 else
11256 {
Bram Moolenaarea424162005-06-16 21:51:00 +000011257 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011258 * When there are postponed prefixes we need to use these first. At
11259 * the end of the prefix we continue in the case-fold tree.
11260 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011261 fbyts = slang->sl_fbyts;
11262 fidxs = slang->sl_fidxs;
11263 pbyts = slang->sl_pbyts;
11264 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011265 if (pbyts != NULL)
11266 {
11267 byts = pbyts;
11268 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011269 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011270 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
11271 }
11272 else
11273 {
11274 byts = fbyts;
11275 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011276 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011277 sp->ts_state = STATE_START;
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 /*
11282 * Loop to find all suggestions. At each round we either:
11283 * - For the current state try one operation, advance "ts_curi",
11284 * increase "depth".
11285 * - When a state is done go to the next, set "ts_state".
11286 * - When all states are tried decrease "depth".
11287 */
11288 while (depth >= 0 && !got_int)
11289 {
11290 sp = &stack[depth];
11291 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011292 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011293 case STATE_START:
11294 case STATE_NOPREFIX:
11295 /*
11296 * Start of node: Deal with NUL bytes, which means
11297 * tword[] may end here.
11298 */
11299 arridx = sp->ts_arridx; /* current node in the tree */
11300 len = byts[arridx]; /* bytes in this node */
11301 arridx += sp->ts_curi; /* index of current byte */
11302
11303 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011304 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011305 /* Skip over the NUL bytes, we use them later. */
11306 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
11307 ;
11308 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011309
Bram Moolenaar4770d092006-01-12 23:22:24 +000011310 /* Always past NUL bytes now. */
11311 n = (int)sp->ts_state;
11312 sp->ts_state = STATE_ENDNUL;
11313 sp->ts_save_badflags = su->su_badflags;
11314
11315 /* At end of a prefix or at start of prefixtree: check for
11316 * following word. */
11317 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011318 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011319 /* Set su->su_badflags to the caps type at this position.
11320 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000011321#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011322 if (has_mbyte)
11323 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
11324 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000011325#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011326 n = sp->ts_fidx;
11327 flags = badword_captype(su->su_badptr, su->su_badptr + n);
11328 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011329 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011330#ifdef DEBUG_TRIEWALK
11331 sprintf(changename[depth], "prefix");
11332#endif
11333 go_deeper(stack, depth, 0);
11334 ++depth;
11335 sp = &stack[depth];
11336 sp->ts_prefixdepth = depth - 1;
11337 byts = fbyts;
11338 idxs = fidxs;
11339 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011340
Bram Moolenaar4770d092006-01-12 23:22:24 +000011341 /* Move the prefix to preword[] with the right case
11342 * and make find_keepcap_word() works. */
11343 tword[sp->ts_twordlen] = NUL;
11344 make_case_word(tword + sp->ts_splitoff,
11345 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011346 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011347 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011348 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011349 break;
11350 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011351
Bram Moolenaar4770d092006-01-12 23:22:24 +000011352 if (sp->ts_curi > len || byts[arridx] != 0)
11353 {
11354 /* Past bytes in node and/or past NUL bytes. */
11355 sp->ts_state = STATE_ENDNUL;
11356 sp->ts_save_badflags = su->su_badflags;
11357 break;
11358 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011359
Bram Moolenaar4770d092006-01-12 23:22:24 +000011360 /*
11361 * End of word in tree.
11362 */
11363 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011364
Bram Moolenaar4770d092006-01-12 23:22:24 +000011365 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011366
11367 /* Skip words with the NOSUGGEST flag. */
11368 if (flags & WF_NOSUGGEST)
11369 break;
11370
Bram Moolenaar4770d092006-01-12 23:22:24 +000011371 fword_ends = (fword[sp->ts_fidx] == NUL
11372 || (soundfold
11373 ? vim_iswhite(fword[sp->ts_fidx])
11374 : !spell_iswordp(fword + sp->ts_fidx, curbuf)));
11375 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011376
Bram Moolenaar4770d092006-01-12 23:22:24 +000011377 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000011378 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011379 {
11380 /* There was a prefix before the word. Check that the prefix
11381 * can be used with this word. */
11382 /* Count the length of the NULs in the prefix. If there are
11383 * none this must be the first try without a prefix. */
11384 n = stack[sp->ts_prefixdepth].ts_arridx;
11385 len = pbyts[n++];
11386 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
11387 ;
11388 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011389 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011390 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011391 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011392 if (c == 0)
11393 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011394
Bram Moolenaar4770d092006-01-12 23:22:24 +000011395 /* Use the WF_RARE flag for a rare prefix. */
11396 if (c & WF_RAREPFX)
11397 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011398
Bram Moolenaar4770d092006-01-12 23:22:24 +000011399 /* Tricky: when checking for both prefix and compounding
11400 * we run into the prefix flag first.
11401 * Remember that it's OK, so that we accept the prefix
11402 * when arriving at a compound flag. */
11403 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011404 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011405 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011406
Bram Moolenaar4770d092006-01-12 23:22:24 +000011407 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
11408 * appending another compound word below. */
11409 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011410 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011411 goodword_ends = FALSE;
11412 else
11413 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011414
Bram Moolenaar4770d092006-01-12 23:22:24 +000011415 p = NULL;
11416 compound_ok = TRUE;
11417 if (sp->ts_complen > sp->ts_compsplit)
11418 {
11419 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000011420 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011421 /* There was a word before this word. When there was no
11422 * change in this word (it was correct) add the first word
11423 * as a suggestion. If this word was corrected too, we
11424 * need to check if a correct word follows. */
11425 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000011426 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000011427 && STRNCMP(fword + sp->ts_splitfidx,
11428 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000011429 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011430 {
11431 preword[sp->ts_prewordlen] = NUL;
11432 newscore = score_wordcount_adj(slang, sp->ts_score,
11433 preword + sp->ts_prewordlen,
11434 sp->ts_prewordlen > 0);
11435 /* Add the suggestion if the score isn't too bad. */
11436 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000011437 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011438 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011439 newscore, 0, FALSE,
11440 lp->lp_sallang, FALSE);
11441 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000011442 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011443 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000011444 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000011445 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011446 /* There was a compound word before this word. If this
11447 * word does not support compounding then give up
11448 * (splitting is tried for the word without compound
11449 * flag). */
11450 if (((unsigned)flags >> 24) == 0
11451 || sp->ts_twordlen - sp->ts_splitoff
11452 < slang->sl_compminlen)
11453 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011454#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011455 /* For multi-byte chars check character length against
11456 * COMPOUNDMIN. */
11457 if (has_mbyte
11458 && slang->sl_compminlen > 0
11459 && mb_charlen(tword + sp->ts_splitoff)
11460 < slang->sl_compminlen)
11461 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011462#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000011463
Bram Moolenaar4770d092006-01-12 23:22:24 +000011464 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11465 compflags[sp->ts_complen + 1] = NUL;
11466 vim_strncpy(preword + sp->ts_prewordlen,
11467 tword + sp->ts_splitoff,
11468 sp->ts_twordlen - sp->ts_splitoff);
11469 p = preword;
11470 while (*skiptowhite(p) != NUL)
11471 p = skipwhite(skiptowhite(p));
11472 if (fword_ends && !can_compound(slang, p,
11473 compflags + sp->ts_compsplit))
11474 /* Compound is not allowed. But it may still be
11475 * possible if we add another (short) word. */
11476 compound_ok = FALSE;
11477
11478 /* Get pointer to last char of previous word. */
11479 p = preword + sp->ts_prewordlen;
11480 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011481 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011482 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011483
Bram Moolenaar4770d092006-01-12 23:22:24 +000011484 /*
11485 * Form the word with proper case in preword.
11486 * If there is a word from a previous split, append.
11487 * For the soundfold tree don't change the case, simply append.
11488 */
11489 if (soundfold)
11490 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11491 else if (flags & WF_KEEPCAP)
11492 /* Must find the word in the keep-case tree. */
11493 find_keepcap_word(slang, tword + sp->ts_splitoff,
11494 preword + sp->ts_prewordlen);
11495 else
11496 {
11497 /* Include badflags: If the badword is onecap or allcap
11498 * use that for the goodword too. But if the badword is
11499 * allcap and it's only one char long use onecap. */
11500 c = su->su_badflags;
11501 if ((c & WF_ALLCAP)
11502#ifdef FEAT_MBYTE
11503 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11504#else
11505 && su->su_badlen == 1
11506#endif
11507 )
11508 c = WF_ONECAP;
11509 c |= flags;
11510
11511 /* When appending a compound word after a word character don't
11512 * use Onecap. */
11513 if (p != NULL && spell_iswordp_nmw(p))
11514 c &= ~WF_ONECAP;
11515 make_case_word(tword + sp->ts_splitoff,
11516 preword + sp->ts_prewordlen, c);
11517 }
11518
11519 if (!soundfold)
11520 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011521 /* Don't use a banned word. It may appear again as a good
11522 * word, thus remember it. */
11523 if (flags & WF_BANNED)
11524 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000011525 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011526 break;
11527 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011528 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000011529 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11530 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011531 {
11532 if (slang->sl_compprog == NULL)
11533 break;
11534 /* the word so far was banned but we may try compounding */
11535 goodword_ends = FALSE;
11536 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011537 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011538
Bram Moolenaar4770d092006-01-12 23:22:24 +000011539 newscore = 0;
11540 if (!soundfold) /* soundfold words don't have flags */
11541 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011542 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011543 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011544 newscore += SCORE_REGION;
11545 if (flags & WF_RARE)
11546 newscore += SCORE_RARE;
11547
Bram Moolenaar0c405862005-06-22 22:26:26 +000011548 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011549 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011550 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011551 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011552
Bram Moolenaar4770d092006-01-12 23:22:24 +000011553 /* TODO: how about splitting in the soundfold tree? */
11554 if (fword_ends
11555 && goodword_ends
11556 && sp->ts_fidx >= sp->ts_fidxtry
11557 && compound_ok)
11558 {
11559 /* The badword also ends: add suggestions. */
11560#ifdef DEBUG_TRIEWALK
11561 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011562 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011563 int j;
11564
11565 /* print the stack of changes that brought us here */
11566 smsg("------ %s -------", fword);
11567 for (j = 0; j < depth; ++j)
11568 smsg("%s", changename[j]);
11569 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011570#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011571 if (soundfold)
11572 {
11573 /* For soundfolded words we need to find the original
Bram Moolenaarf711faf2007-05-10 16:48:19 +000011574 * words, the edit distance and then add them. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011575 add_sound_suggest(su, preword, sp->ts_score, lp);
11576 }
11577 else
11578 {
11579 /* Give a penalty when changing non-word char to word
11580 * char, e.g., "thes," -> "these". */
11581 p = fword + sp->ts_fidx;
11582 mb_ptr_back(fword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011583 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011584 {
11585 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011586 mb_ptr_back(preword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011587 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011588 newscore += SCORE_NONWORD;
11589 }
11590
Bram Moolenaar4770d092006-01-12 23:22:24 +000011591 /* Give a bonus to words seen before. */
11592 score = score_wordcount_adj(slang,
11593 sp->ts_score + newscore,
11594 preword + sp->ts_prewordlen,
11595 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011596
Bram Moolenaar4770d092006-01-12 23:22:24 +000011597 /* Add the suggestion if the score isn't too bad. */
11598 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011599 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011600 add_suggestion(su, &su->su_ga, preword,
11601 sp->ts_fidx - repextra,
11602 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011603
11604 if (su->su_badflags & WF_MIXCAP)
11605 {
11606 /* We really don't know if the word should be
11607 * upper or lower case, add both. */
11608 c = captype(preword, NULL);
11609 if (c == 0 || c == WF_ALLCAP)
11610 {
11611 make_case_word(tword + sp->ts_splitoff,
11612 preword + sp->ts_prewordlen,
11613 c == 0 ? WF_ALLCAP : 0);
11614
11615 add_suggestion(su, &su->su_ga, preword,
11616 sp->ts_fidx - repextra,
11617 score + SCORE_ICASE, 0, FALSE,
11618 lp->lp_sallang, FALSE);
11619 }
11620 }
11621 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011622 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011623 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011624
Bram Moolenaar4770d092006-01-12 23:22:24 +000011625 /*
11626 * Try word split and/or compounding.
11627 */
11628 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011629#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011630 /* Don't split halfway a character. */
11631 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011632#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011633 )
11634 {
11635 int try_compound;
11636 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011637
Bram Moolenaar4770d092006-01-12 23:22:24 +000011638 /* If past the end of the bad word don't try a split.
11639 * Otherwise try changing the next word. E.g., find
11640 * suggestions for "the the" where the second "the" is
11641 * different. It's done like a split.
11642 * TODO: word split for soundfold words */
11643 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11644 && !soundfold;
11645
11646 /* Get here in several situations:
11647 * 1. The word in the tree ends:
11648 * If the word allows compounding try that. Otherwise try
11649 * a split by inserting a space. For both check that a
11650 * valid words starts at fword[sp->ts_fidx].
11651 * For NOBREAK do like compounding to be able to check if
11652 * the next word is valid.
11653 * 2. The badword does end, but it was due to a change (e.g.,
11654 * a swap). No need to split, but do check that the
11655 * following word is valid.
11656 * 3. The badword and the word in the tree end. It may still
11657 * be possible to compound another (short) word.
11658 */
11659 try_compound = FALSE;
11660 if (!soundfold
11661 && slang->sl_compprog != NULL
11662 && ((unsigned)flags >> 24) != 0
11663 && sp->ts_twordlen - sp->ts_splitoff
11664 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011665#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011666 && (!has_mbyte
11667 || slang->sl_compminlen == 0
11668 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011669 >= slang->sl_compminlen)
11670#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011671 && (slang->sl_compsylmax < MAXWLEN
11672 || sp->ts_complen + 1 - sp->ts_compsplit
11673 < slang->sl_compmax)
11674 && (byte_in_str(sp->ts_complen == sp->ts_compsplit
11675 ? slang->sl_compstartflags
11676 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +000011677 ((unsigned)flags >> 24))))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011678 {
11679 try_compound = TRUE;
11680 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11681 compflags[sp->ts_complen + 1] = NUL;
11682 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011683
Bram Moolenaar4770d092006-01-12 23:22:24 +000011684 /* For NOBREAK we never try splitting, it won't make any word
11685 * valid. */
11686 if (slang->sl_nobreak)
11687 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011688
Bram Moolenaar4770d092006-01-12 23:22:24 +000011689 /* If we could add a compound word, and it's also possible to
11690 * split at this point, do the split first and set
11691 * TSF_DIDSPLIT to avoid doing it again. */
11692 else if (!fword_ends
11693 && try_compound
11694 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11695 {
11696 try_compound = FALSE;
11697 sp->ts_flags |= TSF_DIDSPLIT;
11698 --sp->ts_curi; /* do the same NUL again */
11699 compflags[sp->ts_complen] = NUL;
11700 }
11701 else
11702 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011703
Bram Moolenaar4770d092006-01-12 23:22:24 +000011704 if (try_split || try_compound)
11705 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011706 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011707 {
11708 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011709 * words so far are valid for compounding. If there
11710 * is only one word it must not have the NEEDCOMPOUND
11711 * flag. */
11712 if (sp->ts_complen == sp->ts_compsplit
11713 && (flags & WF_NEEDCOMP))
11714 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011715 p = preword;
11716 while (*skiptowhite(p) != NUL)
11717 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011718 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011719 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011720 compflags + sp->ts_compsplit))
11721 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011722
11723 if (slang->sl_nosplitsugs)
11724 newscore += SCORE_SPLIT_NO;
11725 else
11726 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011727
11728 /* Give a bonus to words seen before. */
11729 newscore = score_wordcount_adj(slang, newscore,
11730 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011731 }
11732
Bram Moolenaar4770d092006-01-12 23:22:24 +000011733 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011734 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011735 go_deeper(stack, depth, newscore);
11736#ifdef DEBUG_TRIEWALK
11737 if (!try_compound && !fword_ends)
11738 sprintf(changename[depth], "%.*s-%s: split",
11739 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11740 else
11741 sprintf(changename[depth], "%.*s-%s: compound",
11742 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11743#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011744 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011745 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011746 sp->ts_state = STATE_SPLITUNDO;
11747
11748 ++depth;
11749 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011750
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011751 /* Append a space to preword when splitting. */
11752 if (!try_compound && !fword_ends)
11753 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011754 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011755 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011756 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011757
11758 /* If the badword has a non-word character at this
11759 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011760 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011761 * character when the word ends. But only when the
11762 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011763 if (((!try_compound && !spell_iswordp_nmw(fword
11764 + sp->ts_fidx))
11765 || fword_ends)
11766 && fword[sp->ts_fidx] != NUL
11767 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011768 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011769 int l;
11770
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011771#ifdef FEAT_MBYTE
11772 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011773 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011774 else
11775#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011776 l = 1;
11777 if (fword_ends)
11778 {
11779 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011780 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011781 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011782 sp->ts_prewordlen += l;
11783 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011784 }
11785 else
11786 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11787 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011788 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011789
Bram Moolenaard12a1322005-08-21 22:08:24 +000011790 /* When compounding include compound flag in
11791 * compflags[] (already set above). When splitting we
11792 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011793 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011794 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011795 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011796 sp->ts_compsplit = sp->ts_complen;
11797 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011798
Bram Moolenaar53805d12005-08-01 07:08:33 +000011799 /* set su->su_badflags to the caps type at this
11800 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011801#ifdef FEAT_MBYTE
11802 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011803 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011804 else
11805#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011806 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011807 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011808 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011809
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011810 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011811 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011812
11813 /* If there are postponed prefixes, try these too. */
11814 if (pbyts != NULL)
11815 {
11816 byts = pbyts;
11817 idxs = pidxs;
11818 sp->ts_prefixdepth = PFD_PREFIXTREE;
11819 sp->ts_state = STATE_NOPREFIX;
11820 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011821 }
11822 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011823 }
11824 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011825
Bram Moolenaar4770d092006-01-12 23:22:24 +000011826 case STATE_SPLITUNDO:
11827 /* Undo the changes done for word split or compound word. */
11828 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011829
Bram Moolenaar4770d092006-01-12 23:22:24 +000011830 /* Continue looking for NUL bytes. */
11831 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011832
Bram Moolenaar4770d092006-01-12 23:22:24 +000011833 /* In case we went into the prefix tree. */
11834 byts = fbyts;
11835 idxs = fidxs;
11836 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011837
Bram Moolenaar4770d092006-01-12 23:22:24 +000011838 case STATE_ENDNUL:
11839 /* Past the NUL bytes in the node. */
11840 su->su_badflags = sp->ts_save_badflags;
11841 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011842#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011843 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011844#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011845 )
11846 {
11847 /* The badword ends, can't use STATE_PLAIN. */
11848 sp->ts_state = STATE_DEL;
11849 break;
11850 }
11851 sp->ts_state = STATE_PLAIN;
11852 /*FALLTHROUGH*/
11853
11854 case STATE_PLAIN:
11855 /*
11856 * Go over all possible bytes at this node, add each to tword[]
11857 * and use child node. "ts_curi" is the index.
11858 */
11859 arridx = sp->ts_arridx;
11860 if (sp->ts_curi > byts[arridx])
11861 {
11862 /* Done all bytes at this node, do next state. When still at
11863 * already changed bytes skip the other tricks. */
11864 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011865 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011866 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011867 sp->ts_state = STATE_FINAL;
11868 }
11869 else
11870 {
11871 arridx += sp->ts_curi++;
11872 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011873
Bram Moolenaar4770d092006-01-12 23:22:24 +000011874 /* Normal byte, go one level deeper. If it's not equal to the
11875 * byte in the bad word adjust the score. But don't even try
11876 * when the byte was already changed. And don't try when we
11877 * just deleted this byte, accepting it is always cheaper then
11878 * delete + substitute. */
11879 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000011880#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011881 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011882#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011883 )
11884 newscore = 0;
11885 else
11886 newscore = SCORE_SUBST;
11887 if ((newscore == 0
11888 || (sp->ts_fidx >= sp->ts_fidxtry
11889 && ((sp->ts_flags & TSF_DIDDEL) == 0
11890 || c != fword[sp->ts_delidx])))
11891 && TRY_DEEPER(su, stack, depth, newscore))
11892 {
11893 go_deeper(stack, depth, newscore);
11894#ifdef DEBUG_TRIEWALK
11895 if (newscore > 0)
11896 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
11897 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11898 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011899 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011900 sprintf(changename[depth], "%.*s-%s: accept %c",
11901 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11902 fword[sp->ts_fidx]);
11903#endif
11904 ++depth;
11905 sp = &stack[depth];
11906 ++sp->ts_fidx;
11907 tword[sp->ts_twordlen++] = c;
11908 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000011909#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011910 if (newscore == SCORE_SUBST)
11911 sp->ts_isdiff = DIFF_YES;
11912 if (has_mbyte)
11913 {
11914 /* Multi-byte characters are a bit complicated to
11915 * handle: They differ when any of the bytes differ
11916 * and then their length may also differ. */
11917 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011918 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011919 /* First byte. */
11920 sp->ts_tcharidx = 0;
11921 sp->ts_tcharlen = MB_BYTE2LEN(c);
11922 sp->ts_fcharstart = sp->ts_fidx - 1;
11923 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011924 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011925 }
11926 else if (sp->ts_isdiff == DIFF_INSERT)
11927 /* When inserting trail bytes don't advance in the
11928 * bad word. */
11929 --sp->ts_fidx;
11930 if (++sp->ts_tcharidx == sp->ts_tcharlen)
11931 {
11932 /* Last byte of character. */
11933 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000011934 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011935 /* Correct ts_fidx for the byte length of the
11936 * character (we didn't check that before). */
11937 sp->ts_fidx = sp->ts_fcharstart
11938 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000011939 fword[sp->ts_fcharstart]);
11940
Bram Moolenaar4770d092006-01-12 23:22:24 +000011941 /* For changing a composing character adjust
11942 * the score from SCORE_SUBST to
11943 * SCORE_SUBCOMP. */
11944 if (enc_utf8
11945 && utf_iscomposing(
11946 mb_ptr2char(tword
11947 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011948 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011949 && utf_iscomposing(
11950 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011951 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011952 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011953 SCORE_SUBST - SCORE_SUBCOMP;
11954
Bram Moolenaar4770d092006-01-12 23:22:24 +000011955 /* For a similar character adjust score from
11956 * SCORE_SUBST to SCORE_SIMILAR. */
11957 else if (!soundfold
11958 && slang->sl_has_map
11959 && similar_chars(slang,
11960 mb_ptr2char(tword
11961 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000011962 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000011963 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000011964 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011965 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000011966 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000011967 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011968 else if (sp->ts_isdiff == DIFF_INSERT
11969 && sp->ts_twordlen > sp->ts_tcharlen)
11970 {
11971 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
11972 c = mb_ptr2char(p);
11973 if (enc_utf8 && utf_iscomposing(c))
11974 {
11975 /* Inserting a composing char doesn't
11976 * count that much. */
11977 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
11978 }
11979 else
11980 {
11981 /* If the previous character was the same,
11982 * thus doubling a character, give a bonus
11983 * to the score. Also for the soundfold
11984 * tree (might seem illogical but does
11985 * give better scores). */
11986 mb_ptr_back(tword, p);
11987 if (c == mb_ptr2char(p))
11988 sp->ts_score -= SCORE_INS
11989 - SCORE_INSDUP;
11990 }
11991 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011992
Bram Moolenaar4770d092006-01-12 23:22:24 +000011993 /* Starting a new char, reset the length. */
11994 sp->ts_tcharlen = 0;
11995 }
Bram Moolenaarea408852005-06-25 22:49:46 +000011996 }
Bram Moolenaarea424162005-06-16 21:51:00 +000011997 else
11998#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000011999 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012000 /* If we found a similar char adjust the score.
12001 * We do this after calling go_deeper() because
12002 * it's slow. */
12003 if (newscore != 0
12004 && !soundfold
12005 && slang->sl_has_map
12006 && similar_chars(slang,
12007 c, fword[sp->ts_fidx - 1]))
12008 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000012009 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012010 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012011 }
12012 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012013
Bram Moolenaar4770d092006-01-12 23:22:24 +000012014 case STATE_DEL:
12015#ifdef FEAT_MBYTE
12016 /* When past the first byte of a multi-byte char don't try
12017 * delete/insert/swap a character. */
12018 if (has_mbyte && sp->ts_tcharlen > 0)
12019 {
12020 sp->ts_state = STATE_FINAL;
12021 break;
12022 }
12023#endif
12024 /*
12025 * Try skipping one character in the bad word (delete it).
12026 */
12027 sp->ts_state = STATE_INS_PREP;
12028 sp->ts_curi = 1;
12029 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
12030 /* Deleting a vowel at the start of a word counts less, see
12031 * soundalike_score(). */
12032 newscore = 2 * SCORE_DEL / 3;
12033 else
12034 newscore = SCORE_DEL;
12035 if (fword[sp->ts_fidx] != NUL
12036 && TRY_DEEPER(su, stack, depth, newscore))
12037 {
12038 go_deeper(stack, depth, newscore);
12039#ifdef DEBUG_TRIEWALK
12040 sprintf(changename[depth], "%.*s-%s: delete %c",
12041 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12042 fword[sp->ts_fidx]);
12043#endif
12044 ++depth;
12045
12046 /* Remember what character we deleted, so that we can avoid
12047 * inserting it again. */
12048 stack[depth].ts_flags |= TSF_DIDDEL;
12049 stack[depth].ts_delidx = sp->ts_fidx;
12050
12051 /* Advance over the character in fword[]. Give a bonus to the
12052 * score if the same character is following "nn" -> "n". It's
12053 * a bit illogical for soundfold tree but it does give better
12054 * results. */
12055#ifdef FEAT_MBYTE
12056 if (has_mbyte)
12057 {
12058 c = mb_ptr2char(fword + sp->ts_fidx);
12059 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
12060 if (enc_utf8 && utf_iscomposing(c))
12061 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
12062 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
12063 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12064 }
12065 else
12066#endif
12067 {
12068 ++stack[depth].ts_fidx;
12069 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
12070 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12071 }
12072 break;
12073 }
12074 /*FALLTHROUGH*/
12075
12076 case STATE_INS_PREP:
12077 if (sp->ts_flags & TSF_DIDDEL)
12078 {
12079 /* If we just deleted a byte then inserting won't make sense,
12080 * a substitute is always cheaper. */
12081 sp->ts_state = STATE_SWAP;
12082 break;
12083 }
12084
12085 /* skip over NUL bytes */
12086 n = sp->ts_arridx;
12087 for (;;)
12088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012089 if (sp->ts_curi > byts[n])
12090 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012091 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012092 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012093 break;
12094 }
12095 if (byts[n + sp->ts_curi] != NUL)
12096 {
12097 /* Found a byte to insert. */
12098 sp->ts_state = STATE_INS;
12099 break;
12100 }
12101 ++sp->ts_curi;
12102 }
12103 break;
12104
12105 /*FALLTHROUGH*/
12106
12107 case STATE_INS:
12108 /* Insert one byte. Repeat this for each possible byte at this
12109 * node. */
12110 n = sp->ts_arridx;
12111 if (sp->ts_curi > byts[n])
12112 {
12113 /* Done all bytes at this node, go to next state. */
12114 sp->ts_state = STATE_SWAP;
12115 break;
12116 }
12117
12118 /* Do one more byte at this node, but:
12119 * - Skip NUL bytes.
12120 * - Skip the byte if it's equal to the byte in the word,
12121 * accepting that byte is always better.
12122 */
12123 n += sp->ts_curi++;
12124 c = byts[n];
12125 if (soundfold && sp->ts_twordlen == 0 && c == '*')
12126 /* Inserting a vowel at the start of a word counts less,
12127 * see soundalike_score(). */
12128 newscore = 2 * SCORE_INS / 3;
12129 else
12130 newscore = SCORE_INS;
12131 if (c != fword[sp->ts_fidx]
12132 && TRY_DEEPER(su, stack, depth, newscore))
12133 {
12134 go_deeper(stack, depth, newscore);
12135#ifdef DEBUG_TRIEWALK
12136 sprintf(changename[depth], "%.*s-%s: insert %c",
12137 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12138 c);
12139#endif
12140 ++depth;
12141 sp = &stack[depth];
12142 tword[sp->ts_twordlen++] = c;
12143 sp->ts_arridx = idxs[n];
12144#ifdef FEAT_MBYTE
12145 if (has_mbyte)
12146 {
12147 fl = MB_BYTE2LEN(c);
12148 if (fl > 1)
12149 {
12150 /* There are following bytes for the same character.
12151 * We must find all bytes before trying
12152 * delete/insert/swap/etc. */
12153 sp->ts_tcharlen = fl;
12154 sp->ts_tcharidx = 1;
12155 sp->ts_isdiff = DIFF_INSERT;
12156 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012157 }
12158 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012159 fl = 1;
12160 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000012161#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012162 {
12163 /* If the previous character was the same, thus doubling a
12164 * character, give a bonus to the score. Also for
12165 * soundfold words (illogical but does give a better
12166 * score). */
12167 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000012168 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012169 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012170 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012171 }
12172 break;
12173
12174 case STATE_SWAP:
12175 /*
12176 * Swap two bytes in the bad word: "12" -> "21".
12177 * We change "fword" here, it's changed back afterwards at
12178 * STATE_UNSWAP.
12179 */
12180 p = fword + sp->ts_fidx;
12181 c = *p;
12182 if (c == NUL)
12183 {
12184 /* End of word, can't swap or replace. */
12185 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012186 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012187 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012188
Bram Moolenaar4770d092006-01-12 23:22:24 +000012189 /* Don't swap if the first character is not a word character.
12190 * SWAP3 etc. also don't make sense then. */
12191 if (!soundfold && !spell_iswordp(p, curbuf))
12192 {
12193 sp->ts_state = STATE_REP_INI;
12194 break;
12195 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012196
Bram Moolenaar4770d092006-01-12 23:22:24 +000012197#ifdef FEAT_MBYTE
12198 if (has_mbyte)
12199 {
12200 n = mb_cptr2len(p);
12201 c = mb_ptr2char(p);
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012202 if (p[n] == NUL)
12203 c2 = NUL;
12204 else if (!soundfold && !spell_iswordp(p + n, curbuf))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012205 c2 = c; /* don't swap non-word char */
12206 else
12207 c2 = mb_ptr2char(p + n);
12208 }
12209 else
12210#endif
12211 {
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012212 if (p[1] == NUL)
12213 c2 = NUL;
12214 else if (!soundfold && !spell_iswordp(p + 1, curbuf))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012215 c2 = c; /* don't swap non-word char */
12216 else
12217 c2 = p[1];
12218 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012219
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012220 /* When the second character is NUL we can't swap. */
12221 if (c2 == NUL)
12222 {
12223 sp->ts_state = STATE_REP_INI;
12224 break;
12225 }
12226
Bram Moolenaar4770d092006-01-12 23:22:24 +000012227 /* When characters are identical, swap won't do anything.
12228 * Also get here if the second char is not a word character. */
12229 if (c == c2)
12230 {
12231 sp->ts_state = STATE_SWAP3;
12232 break;
12233 }
12234 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
12235 {
12236 go_deeper(stack, depth, SCORE_SWAP);
12237#ifdef DEBUG_TRIEWALK
12238 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
12239 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12240 c, c2);
12241#endif
12242 sp->ts_state = STATE_UNSWAP;
12243 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012244#ifdef FEAT_MBYTE
12245 if (has_mbyte)
12246 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012247 fl = mb_char2len(c2);
12248 mch_memmove(p, p + n, fl);
12249 mb_char2bytes(c, p + fl);
12250 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012251 }
12252 else
12253#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012254 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012255 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012256 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012257 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012258 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012259 }
12260 else
12261 /* If this swap doesn't work then SWAP3 won't either. */
12262 sp->ts_state = STATE_REP_INI;
12263 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000012264
Bram Moolenaar4770d092006-01-12 23:22:24 +000012265 case STATE_UNSWAP:
12266 /* Undo the STATE_SWAP swap: "21" -> "12". */
12267 p = fword + sp->ts_fidx;
12268#ifdef FEAT_MBYTE
12269 if (has_mbyte)
12270 {
12271 n = MB_BYTE2LEN(*p);
12272 c = mb_ptr2char(p + n);
12273 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
12274 mb_char2bytes(c, p);
12275 }
12276 else
12277#endif
12278 {
12279 c = *p;
12280 *p = p[1];
12281 p[1] = c;
12282 }
12283 /*FALLTHROUGH*/
12284
12285 case STATE_SWAP3:
12286 /* Swap two bytes, skipping one: "123" -> "321". We change
12287 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
12288 p = fword + sp->ts_fidx;
12289#ifdef FEAT_MBYTE
12290 if (has_mbyte)
12291 {
12292 n = mb_cptr2len(p);
12293 c = mb_ptr2char(p);
12294 fl = mb_cptr2len(p + n);
12295 c2 = mb_ptr2char(p + n);
12296 if (!soundfold && !spell_iswordp(p + n + fl, curbuf))
12297 c3 = c; /* don't swap non-word char */
12298 else
12299 c3 = mb_ptr2char(p + n + fl);
12300 }
12301 else
12302#endif
12303 {
12304 c = *p;
12305 c2 = p[1];
12306 if (!soundfold && !spell_iswordp(p + 2, curbuf))
12307 c3 = c; /* don't swap non-word char */
12308 else
12309 c3 = p[2];
12310 }
12311
12312 /* When characters are identical: "121" then SWAP3 result is
12313 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
12314 * same as SWAP on next char: "112". Thus skip all swapping.
12315 * Also skip when c3 is NUL.
12316 * Also get here when the third character is not a word character.
12317 * Second character may any char: "a.b" -> "b.a" */
12318 if (c == c3 || c3 == NUL)
12319 {
12320 sp->ts_state = STATE_REP_INI;
12321 break;
12322 }
12323 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12324 {
12325 go_deeper(stack, depth, SCORE_SWAP3);
12326#ifdef DEBUG_TRIEWALK
12327 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
12328 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12329 c, c3);
12330#endif
12331 sp->ts_state = STATE_UNSWAP3;
12332 ++depth;
12333#ifdef FEAT_MBYTE
12334 if (has_mbyte)
12335 {
12336 tl = mb_char2len(c3);
12337 mch_memmove(p, p + n + fl, tl);
12338 mb_char2bytes(c2, p + tl);
12339 mb_char2bytes(c, p + fl + tl);
12340 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
12341 }
12342 else
12343#endif
12344 {
12345 p[0] = p[2];
12346 p[2] = c;
12347 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12348 }
12349 }
12350 else
12351 sp->ts_state = STATE_REP_INI;
12352 break;
12353
12354 case STATE_UNSWAP3:
12355 /* Undo STATE_SWAP3: "321" -> "123" */
12356 p = fword + sp->ts_fidx;
12357#ifdef FEAT_MBYTE
12358 if (has_mbyte)
12359 {
12360 n = MB_BYTE2LEN(*p);
12361 c2 = mb_ptr2char(p + n);
12362 fl = MB_BYTE2LEN(p[n]);
12363 c = mb_ptr2char(p + n + fl);
12364 tl = MB_BYTE2LEN(p[n + fl]);
12365 mch_memmove(p + fl + tl, p, n);
12366 mb_char2bytes(c, p);
12367 mb_char2bytes(c2, p + tl);
12368 p = p + tl;
12369 }
12370 else
12371#endif
12372 {
12373 c = *p;
12374 *p = p[2];
12375 p[2] = c;
12376 ++p;
12377 }
12378
12379 if (!soundfold && !spell_iswordp(p, curbuf))
12380 {
12381 /* Middle char is not a word char, skip the rotate. First and
12382 * third char were already checked at swap and swap3. */
12383 sp->ts_state = STATE_REP_INI;
12384 break;
12385 }
12386
12387 /* Rotate three characters left: "123" -> "231". We change
12388 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
12389 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12390 {
12391 go_deeper(stack, depth, SCORE_SWAP3);
12392#ifdef DEBUG_TRIEWALK
12393 p = fword + sp->ts_fidx;
12394 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
12395 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12396 p[0], p[1], p[2]);
12397#endif
12398 sp->ts_state = STATE_UNROT3L;
12399 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012400 p = fword + sp->ts_fidx;
12401#ifdef FEAT_MBYTE
12402 if (has_mbyte)
12403 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012404 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012405 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012406 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012407 fl += mb_cptr2len(p + n + fl);
12408 mch_memmove(p, p + n, fl);
12409 mb_char2bytes(c, p + fl);
12410 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012411 }
12412 else
12413#endif
12414 {
12415 c = *p;
12416 *p = p[1];
12417 p[1] = p[2];
12418 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012419 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000012420 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012421 }
12422 else
12423 sp->ts_state = STATE_REP_INI;
12424 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012425
Bram Moolenaar4770d092006-01-12 23:22:24 +000012426 case STATE_UNROT3L:
12427 /* Undo ROT3L: "231" -> "123" */
12428 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000012429#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012430 if (has_mbyte)
12431 {
12432 n = MB_BYTE2LEN(*p);
12433 n += MB_BYTE2LEN(p[n]);
12434 c = mb_ptr2char(p + n);
12435 tl = MB_BYTE2LEN(p[n]);
12436 mch_memmove(p + tl, p, n);
12437 mb_char2bytes(c, p);
12438 }
12439 else
Bram Moolenaarea424162005-06-16 21:51:00 +000012440#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012441 {
12442 c = p[2];
12443 p[2] = p[1];
12444 p[1] = *p;
12445 *p = c;
12446 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012447
Bram Moolenaar4770d092006-01-12 23:22:24 +000012448 /* Rotate three bytes right: "123" -> "312". We change "fword"
12449 * here, it's changed back afterwards at STATE_UNROT3R. */
12450 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12451 {
12452 go_deeper(stack, depth, SCORE_SWAP3);
12453#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012454 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012455 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
12456 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12457 p[0], p[1], p[2]);
12458#endif
12459 sp->ts_state = STATE_UNROT3R;
12460 ++depth;
12461 p = fword + sp->ts_fidx;
12462#ifdef FEAT_MBYTE
12463 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012464 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012465 n = mb_cptr2len(p);
12466 n += mb_cptr2len(p + n);
12467 c = mb_ptr2char(p + n);
12468 tl = mb_cptr2len(p + n);
12469 mch_memmove(p + tl, p, n);
12470 mb_char2bytes(c, p);
12471 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012472 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012473 else
12474#endif
12475 {
12476 c = p[2];
12477 p[2] = p[1];
12478 p[1] = *p;
12479 *p = c;
12480 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12481 }
12482 }
12483 else
12484 sp->ts_state = STATE_REP_INI;
12485 break;
12486
12487 case STATE_UNROT3R:
12488 /* Undo ROT3R: "312" -> "123" */
12489 p = fword + sp->ts_fidx;
12490#ifdef FEAT_MBYTE
12491 if (has_mbyte)
12492 {
12493 c = mb_ptr2char(p);
12494 tl = MB_BYTE2LEN(*p);
12495 n = MB_BYTE2LEN(p[tl]);
12496 n += MB_BYTE2LEN(p[tl + n]);
12497 mch_memmove(p, p + tl, n);
12498 mb_char2bytes(c, p + n);
12499 }
12500 else
12501#endif
12502 {
12503 c = *p;
12504 *p = p[1];
12505 p[1] = p[2];
12506 p[2] = c;
12507 }
12508 /*FALLTHROUGH*/
12509
12510 case STATE_REP_INI:
12511 /* Check if matching with REP items from the .aff file would work.
12512 * Quickly skip if:
12513 * - there are no REP items and we are not in the soundfold trie
12514 * - the score is going to be too high anyway
12515 * - already applied a REP item or swapped here */
12516 if ((lp->lp_replang == NULL && !soundfold)
12517 || sp->ts_score + SCORE_REP >= su->su_maxscore
12518 || sp->ts_fidx < sp->ts_fidxtry)
12519 {
12520 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012521 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012522 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012523
Bram Moolenaar4770d092006-01-12 23:22:24 +000012524 /* Use the first byte to quickly find the first entry that may
12525 * match. If the index is -1 there is none. */
12526 if (soundfold)
12527 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12528 else
12529 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012530
Bram Moolenaar4770d092006-01-12 23:22:24 +000012531 if (sp->ts_curi < 0)
12532 {
12533 sp->ts_state = STATE_FINAL;
12534 break;
12535 }
12536
12537 sp->ts_state = STATE_REP;
12538 /*FALLTHROUGH*/
12539
12540 case STATE_REP:
12541 /* Try matching with REP items from the .aff file. For each match
12542 * replace the characters and check if the resulting word is
12543 * valid. */
12544 p = fword + sp->ts_fidx;
12545
12546 if (soundfold)
12547 gap = &slang->sl_repsal;
12548 else
12549 gap = &lp->lp_replang->sl_rep;
12550 while (sp->ts_curi < gap->ga_len)
12551 {
12552 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12553 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012554 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012555 /* past possible matching entries */
12556 sp->ts_curi = gap->ga_len;
12557 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012558 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012559 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12560 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12561 {
12562 go_deeper(stack, depth, SCORE_REP);
12563#ifdef DEBUG_TRIEWALK
12564 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12565 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12566 ftp->ft_from, ftp->ft_to);
12567#endif
12568 /* Need to undo this afterwards. */
12569 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012570
Bram Moolenaar4770d092006-01-12 23:22:24 +000012571 /* Change the "from" to the "to" string. */
12572 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012573 fl = (int)STRLEN(ftp->ft_from);
12574 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012575 if (fl != tl)
12576 {
12577 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
12578 repextra += tl - fl;
12579 }
12580 mch_memmove(p, ftp->ft_to, tl);
12581 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12582#ifdef FEAT_MBYTE
12583 stack[depth].ts_tcharlen = 0;
12584#endif
12585 break;
12586 }
12587 }
12588
12589 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
12590 /* No (more) matches. */
12591 sp->ts_state = STATE_FINAL;
12592
12593 break;
12594
12595 case STATE_REP_UNDO:
12596 /* Undo a REP replacement and continue with the next one. */
12597 if (soundfold)
12598 gap = &slang->sl_repsal;
12599 else
12600 gap = &lp->lp_replang->sl_rep;
12601 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012602 fl = (int)STRLEN(ftp->ft_from);
12603 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012604 p = fword + sp->ts_fidx;
12605 if (fl != tl)
12606 {
12607 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
12608 repextra -= tl - fl;
12609 }
12610 mch_memmove(p, ftp->ft_from, fl);
12611 sp->ts_state = STATE_REP;
12612 break;
12613
12614 default:
12615 /* Did all possible states at this level, go up one level. */
12616 --depth;
12617
12618 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12619 {
12620 /* Continue in or go back to the prefix tree. */
12621 byts = pbyts;
12622 idxs = pidxs;
12623 }
12624
12625 /* Don't check for CTRL-C too often, it takes time. */
12626 if (--breakcheckcount == 0)
12627 {
12628 ui_breakcheck();
12629 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012630 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012631 }
12632 }
12633}
12634
Bram Moolenaar4770d092006-01-12 23:22:24 +000012635
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012636/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012637 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012638 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012639 static void
12640go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012641 trystate_T *stack;
12642 int depth;
12643 int score_add;
12644{
Bram Moolenaarea424162005-06-16 21:51:00 +000012645 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012646 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012647 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012648 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012649 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012650}
12651
Bram Moolenaar53805d12005-08-01 07:08:33 +000012652#ifdef FEAT_MBYTE
12653/*
12654 * Case-folding may change the number of bytes: Count nr of chars in
12655 * fword[flen] and return the byte length of that many chars in "word".
12656 */
12657 static int
12658nofold_len(fword, flen, word)
12659 char_u *fword;
12660 int flen;
12661 char_u *word;
12662{
12663 char_u *p;
12664 int i = 0;
12665
12666 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12667 ++i;
12668 for (p = word; i > 0; mb_ptr_adv(p))
12669 --i;
12670 return (int)(p - word);
12671}
12672#endif
12673
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012674/*
12675 * "fword" is a good word with case folded. Find the matching keep-case
12676 * words and put it in "kword".
12677 * Theoretically there could be several keep-case words that result in the
12678 * same case-folded word, but we only find one...
12679 */
12680 static void
12681find_keepcap_word(slang, fword, kword)
12682 slang_T *slang;
12683 char_u *fword;
12684 char_u *kword;
12685{
12686 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12687 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012688 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012689
12690 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012691 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012692 int round[MAXWLEN];
12693 int fwordidx[MAXWLEN];
12694 int uwordidx[MAXWLEN];
12695 int kwordlen[MAXWLEN];
12696
12697 int flen, ulen;
12698 int l;
12699 int len;
12700 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012701 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012702 char_u *p;
12703 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012704 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012705
12706 if (byts == NULL)
12707 {
12708 /* array is empty: "cannot happen" */
12709 *kword = NUL;
12710 return;
12711 }
12712
12713 /* Make an all-cap version of "fword". */
12714 allcap_copy(fword, uword);
12715
12716 /*
12717 * Each character needs to be tried both case-folded and upper-case.
12718 * All this gets very complicated if we keep in mind that changing case
12719 * may change the byte length of a multi-byte character...
12720 */
12721 depth = 0;
12722 arridx[0] = 0;
12723 round[0] = 0;
12724 fwordidx[0] = 0;
12725 uwordidx[0] = 0;
12726 kwordlen[0] = 0;
12727 while (depth >= 0)
12728 {
12729 if (fword[fwordidx[depth]] == NUL)
12730 {
12731 /* We are at the end of "fword". If the tree allows a word to end
12732 * here we have found a match. */
12733 if (byts[arridx[depth] + 1] == 0)
12734 {
12735 kword[kwordlen[depth]] = NUL;
12736 return;
12737 }
12738
12739 /* kword is getting too long, continue one level up */
12740 --depth;
12741 }
12742 else if (++round[depth] > 2)
12743 {
12744 /* tried both fold-case and upper-case character, continue one
12745 * level up */
12746 --depth;
12747 }
12748 else
12749 {
12750 /*
12751 * round[depth] == 1: Try using the folded-case character.
12752 * round[depth] == 2: Try using the upper-case character.
12753 */
12754#ifdef FEAT_MBYTE
12755 if (has_mbyte)
12756 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012757 flen = mb_cptr2len(fword + fwordidx[depth]);
12758 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012759 }
12760 else
12761#endif
12762 ulen = flen = 1;
12763 if (round[depth] == 1)
12764 {
12765 p = fword + fwordidx[depth];
12766 l = flen;
12767 }
12768 else
12769 {
12770 p = uword + uwordidx[depth];
12771 l = ulen;
12772 }
12773
12774 for (tryidx = arridx[depth]; l > 0; --l)
12775 {
12776 /* Perform a binary search in the list of accepted bytes. */
12777 len = byts[tryidx++];
12778 c = *p++;
12779 lo = tryidx;
12780 hi = tryidx + len - 1;
12781 while (lo < hi)
12782 {
12783 m = (lo + hi) / 2;
12784 if (byts[m] > c)
12785 hi = m - 1;
12786 else if (byts[m] < c)
12787 lo = m + 1;
12788 else
12789 {
12790 lo = hi = m;
12791 break;
12792 }
12793 }
12794
12795 /* Stop if there is no matching byte. */
12796 if (hi < lo || byts[lo] != c)
12797 break;
12798
12799 /* Continue at the child (if there is one). */
12800 tryidx = idxs[lo];
12801 }
12802
12803 if (l == 0)
12804 {
12805 /*
12806 * Found the matching char. Copy it to "kword" and go a
12807 * level deeper.
12808 */
12809 if (round[depth] == 1)
12810 {
12811 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12812 flen);
12813 kwordlen[depth + 1] = kwordlen[depth] + flen;
12814 }
12815 else
12816 {
12817 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12818 ulen);
12819 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12820 }
12821 fwordidx[depth + 1] = fwordidx[depth] + flen;
12822 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12823
12824 ++depth;
12825 arridx[depth] = tryidx;
12826 round[depth] = 0;
12827 }
12828 }
12829 }
12830
12831 /* Didn't find it: "cannot happen". */
12832 *kword = NUL;
12833}
12834
12835/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012836 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12837 * su->su_sga.
12838 */
12839 static void
12840score_comp_sal(su)
12841 suginfo_T *su;
12842{
12843 langp_T *lp;
12844 char_u badsound[MAXWLEN];
12845 int i;
12846 suggest_T *stp;
12847 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012848 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012849 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012850
12851 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
12852 return;
12853
12854 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012855 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012856 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012857 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012858 if (lp->lp_slang->sl_sal.ga_len > 0)
12859 {
12860 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012861 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012862
12863 for (i = 0; i < su->su_ga.ga_len; ++i)
12864 {
12865 stp = &SUG(su->su_ga, i);
12866
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012867 /* Case-fold the suggested word, sound-fold it and compute the
12868 * sound-a-like score. */
12869 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012870 if (score < SCORE_MAXMAX)
12871 {
12872 /* Add the suggestion. */
12873 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
12874 sstp->st_word = vim_strsave(stp->st_word);
12875 if (sstp->st_word != NULL)
12876 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012877 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012878 sstp->st_score = score;
12879 sstp->st_altscore = 0;
12880 sstp->st_orglen = stp->st_orglen;
12881 ++su->su_sga.ga_len;
12882 }
12883 }
12884 }
12885 break;
12886 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012887 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012888}
12889
12890/*
12891 * Combine the list of suggestions in su->su_ga and su->su_sga.
12892 * They are intwined.
12893 */
12894 static void
12895score_combine(su)
12896 suginfo_T *su;
12897{
12898 int i;
12899 int j;
12900 garray_T ga;
12901 garray_T *gap;
12902 langp_T *lp;
12903 suggest_T *stp;
12904 char_u *p;
12905 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012906 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012907 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012908 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012909
12910 /* Add the alternate score to su_ga. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012911 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012912 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012913 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012914 if (lp->lp_slang->sl_sal.ga_len > 0)
12915 {
12916 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012917 slang = lp->lp_slang;
12918 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012919
12920 for (i = 0; i < su->su_ga.ga_len; ++i)
12921 {
12922 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012923 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012924 if (stp->st_altscore == SCORE_MAXMAX)
12925 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
12926 else
12927 stp->st_score = (stp->st_score * 3
12928 + stp->st_altscore) / 4;
12929 stp->st_salscore = FALSE;
12930 }
12931 break;
12932 }
12933 }
12934
Bram Moolenaarf193fff2006-04-27 00:02:13 +000012935 if (slang == NULL) /* Using "double" without sound folding. */
12936 {
12937 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
12938 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012939 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000012940 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012941
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012942 /* Add the alternate score to su_sga. */
12943 for (i = 0; i < su->su_sga.ga_len; ++i)
12944 {
12945 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012946 stp->st_altscore = spell_edit_score(slang,
12947 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012948 if (stp->st_score == SCORE_MAXMAX)
12949 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
12950 else
12951 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
12952 stp->st_salscore = TRUE;
12953 }
12954
Bram Moolenaar4770d092006-01-12 23:22:24 +000012955 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
12956 * for both lists. */
12957 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012958 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012959 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012960 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
12961
12962 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
12963 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
12964 return;
12965
12966 stp = &SUG(ga, 0);
12967 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
12968 {
12969 /* round 1: get a suggestion from su_ga
12970 * round 2: get a suggestion from su_sga */
12971 for (round = 1; round <= 2; ++round)
12972 {
12973 gap = round == 1 ? &su->su_ga : &su->su_sga;
12974 if (i < gap->ga_len)
12975 {
12976 /* Don't add a word if it's already there. */
12977 p = SUG(*gap, i).st_word;
12978 for (j = 0; j < ga.ga_len; ++j)
12979 if (STRCMP(stp[j].st_word, p) == 0)
12980 break;
12981 if (j == ga.ga_len)
12982 stp[ga.ga_len++] = SUG(*gap, i);
12983 else
12984 vim_free(p);
12985 }
12986 }
12987 }
12988
12989 ga_clear(&su->su_ga);
12990 ga_clear(&su->su_sga);
12991
12992 /* Truncate the list to the number of suggestions that will be displayed. */
12993 if (ga.ga_len > su->su_maxcount)
12994 {
12995 for (i = su->su_maxcount; i < ga.ga_len; ++i)
12996 vim_free(stp[i].st_word);
12997 ga.ga_len = su->su_maxcount;
12998 }
12999
13000 su->su_ga = ga;
13001}
13002
13003/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013004 * For the goodword in "stp" compute the soundalike score compared to the
13005 * badword.
13006 */
13007 static int
13008stp_sal_score(stp, su, slang, badsound)
13009 suggest_T *stp;
13010 suginfo_T *su;
13011 slang_T *slang;
13012 char_u *badsound; /* sound-folded badword */
13013{
13014 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013015 char_u *pbad;
13016 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013017 char_u badsound2[MAXWLEN];
13018 char_u fword[MAXWLEN];
13019 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013020 char_u goodword[MAXWLEN];
13021 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013022
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013023 lendiff = (int)(su->su_badlen - stp->st_orglen);
13024 if (lendiff >= 0)
13025 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013026 else
13027 {
13028 /* soundfold the bad word with more characters following */
13029 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
13030
13031 /* When joining two words the sound often changes a lot. E.g., "t he"
13032 * sounds like "t h" while "the" sounds like "@". Avoid that by
13033 * removing the space. Don't do it when the good word also contains a
13034 * space. */
13035 if (vim_iswhite(su->su_badptr[su->su_badlen])
13036 && *skiptowhite(stp->st_word) == NUL)
13037 for (p = fword; *(p = skiptowhite(p)) != NUL; )
13038 mch_memmove(p, p + 1, STRLEN(p));
13039
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013040 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013041 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013042 }
13043
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013044 if (lendiff > 0)
13045 {
13046 /* Add part of the bad word to the good word, so that we soundfold
13047 * what replaces the bad word. */
13048 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013049 vim_strncpy(goodword + stp->st_wordlen,
13050 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013051 pgood = goodword;
13052 }
13053 else
13054 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013055
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013056 /* Sound-fold the word and compute the score for the difference. */
13057 spell_soundfold(slang, pgood, FALSE, goodsound);
13058
13059 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013060}
13061
Bram Moolenaar4770d092006-01-12 23:22:24 +000013062/* structure used to store soundfolded words that add_sound_suggest() has
13063 * handled already. */
13064typedef struct
13065{
13066 short sft_score; /* lowest score used */
13067 char_u sft_word[1]; /* soundfolded word, actually longer */
13068} sftword_T;
13069
13070static sftword_T dumsft;
13071#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
13072#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
13073
13074/*
13075 * Prepare for calling suggest_try_soundalike().
13076 */
13077 static void
13078suggest_try_soundalike_prep()
13079{
13080 langp_T *lp;
13081 int lpi;
13082 slang_T *slang;
13083
13084 /* Do this for all languages that support sound folding and for which a
13085 * .sug file has been loaded. */
13086 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
13087 {
13088 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13089 slang = lp->lp_slang;
13090 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13091 /* prepare the hashtable used by add_sound_suggest() */
13092 hash_init(&slang->sl_sounddone);
13093 }
13094}
13095
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013096/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013097 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013098 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013099 */
13100 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000013101suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013102 suginfo_T *su;
13103{
13104 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013105 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013106 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013107 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013108
Bram Moolenaar4770d092006-01-12 23:22:24 +000013109 /* Do this for all languages that support sound folding and for which a
13110 * .sug file has been loaded. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013111 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013112 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013113 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13114 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013115 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013116 {
13117 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013118 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013119
Bram Moolenaar4770d092006-01-12 23:22:24 +000013120 /* try all kinds of inserts/deletes/swaps/etc. */
13121 /* TODO: also soundfold the next words, so that we can try joining
13122 * and splitting */
13123 suggest_trie_walk(su, lp, salword, TRUE);
13124 }
13125 }
13126}
13127
13128/*
13129 * Finish up after calling suggest_try_soundalike().
13130 */
13131 static void
13132suggest_try_soundalike_finish()
13133{
13134 langp_T *lp;
13135 int lpi;
13136 slang_T *slang;
13137 int todo;
13138 hashitem_T *hi;
13139
13140 /* Do this for all languages that support sound folding and for which a
13141 * .sug file has been loaded. */
13142 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
13143 {
13144 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13145 slang = lp->lp_slang;
13146 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13147 {
13148 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013149 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013150 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
13151 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013152 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013153 vim_free(HI2SFT(hi));
13154 --todo;
13155 }
Bram Moolenaar6417da62007-03-08 13:49:53 +000013156
13157 /* Clear the hashtable, it may also be used by another region. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013158 hash_clear(&slang->sl_sounddone);
Bram Moolenaar6417da62007-03-08 13:49:53 +000013159 hash_init(&slang->sl_sounddone);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013160 }
13161 }
13162}
13163
13164/*
13165 * A match with a soundfolded word is found. Add the good word(s) that
13166 * produce this soundfolded word.
13167 */
13168 static void
13169add_sound_suggest(su, goodword, score, lp)
13170 suginfo_T *su;
13171 char_u *goodword;
13172 int score; /* soundfold score */
13173 langp_T *lp;
13174{
13175 slang_T *slang = lp->lp_slang; /* language for sound folding */
13176 int sfwordnr;
13177 char_u *nrline;
13178 int orgnr;
13179 char_u theword[MAXWLEN];
13180 int i;
13181 int wlen;
13182 char_u *byts;
13183 idx_T *idxs;
13184 int n;
13185 int wordcount;
13186 int wc;
13187 int goodscore;
13188 hash_T hash;
13189 hashitem_T *hi;
13190 sftword_T *sft;
13191 int bc, gc;
13192 int limit;
13193
13194 /*
13195 * It's very well possible that the same soundfold word is found several
13196 * times with different scores. Since the following is quite slow only do
13197 * the words that have a better score than before. Use a hashtable to
13198 * remember the words that have been done.
13199 */
13200 hash = hash_hash(goodword);
13201 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
13202 if (HASHITEM_EMPTY(hi))
13203 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013204 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
13205 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000013206 if (sft != NULL)
13207 {
13208 sft->sft_score = score;
13209 STRCPY(sft->sft_word, goodword);
13210 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
13211 }
13212 }
13213 else
13214 {
13215 sft = HI2SFT(hi);
13216 if (score >= sft->sft_score)
13217 return;
13218 sft->sft_score = score;
13219 }
13220
13221 /*
13222 * Find the word nr in the soundfold tree.
13223 */
13224 sfwordnr = soundfold_find(slang, goodword);
13225 if (sfwordnr < 0)
13226 {
13227 EMSG2(_(e_intern2), "add_sound_suggest()");
13228 return;
13229 }
13230
13231 /*
13232 * go over the list of good words that produce this soundfold word
13233 */
13234 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
13235 orgnr = 0;
13236 while (*nrline != NUL)
13237 {
13238 /* The wordnr was stored in a minimal nr of bytes as an offset to the
13239 * previous wordnr. */
13240 orgnr += bytes2offset(&nrline);
13241
13242 byts = slang->sl_fbyts;
13243 idxs = slang->sl_fidxs;
13244
13245 /* Lookup the word "orgnr" one of the two tries. */
13246 n = 0;
13247 wlen = 0;
13248 wordcount = 0;
13249 for (;;)
13250 {
13251 i = 1;
13252 if (wordcount == orgnr && byts[n + 1] == NUL)
13253 break; /* found end of word */
13254
13255 if (byts[n + 1] == NUL)
13256 ++wordcount;
13257
13258 /* skip over the NUL bytes */
13259 for ( ; byts[n + i] == NUL; ++i)
13260 if (i > byts[n]) /* safety check */
13261 {
13262 STRCPY(theword + wlen, "BAD");
13263 goto badword;
13264 }
13265
13266 /* One of the siblings must have the word. */
13267 for ( ; i < byts[n]; ++i)
13268 {
13269 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
13270 if (wordcount + wc > orgnr)
13271 break;
13272 wordcount += wc;
13273 }
13274
13275 theword[wlen++] = byts[n + i];
13276 n = idxs[n + i];
13277 }
13278badword:
13279 theword[wlen] = NUL;
13280
13281 /* Go over the possible flags and regions. */
13282 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
13283 {
13284 char_u cword[MAXWLEN];
13285 char_u *p;
13286 int flags = (int)idxs[n + i];
13287
Bram Moolenaare1438bb2006-03-01 22:01:55 +000013288 /* Skip words with the NOSUGGEST flag */
13289 if (flags & WF_NOSUGGEST)
13290 continue;
13291
Bram Moolenaar4770d092006-01-12 23:22:24 +000013292 if (flags & WF_KEEPCAP)
13293 {
13294 /* Must find the word in the keep-case tree. */
13295 find_keepcap_word(slang, theword, cword);
13296 p = cword;
13297 }
13298 else
13299 {
13300 flags |= su->su_badflags;
13301 if ((flags & WF_CAPMASK) != 0)
13302 {
13303 /* Need to fix case according to "flags". */
13304 make_case_word(theword, cword, flags);
13305 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013306 }
13307 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013308 p = theword;
13309 }
13310
13311 /* Add the suggestion. */
13312 if (sps_flags & SPS_DOUBLE)
13313 {
13314 /* Add the suggestion if the score isn't too bad. */
13315 if (score <= su->su_maxscore)
13316 add_suggestion(su, &su->su_sga, p, su->su_badlen,
13317 score, 0, FALSE, slang, FALSE);
13318 }
13319 else
13320 {
13321 /* Add a penalty for words in another region. */
13322 if ((flags & WF_REGION)
13323 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
13324 goodscore = SCORE_REGION;
13325 else
13326 goodscore = 0;
13327
13328 /* Add a small penalty for changing the first letter from
13329 * lower to upper case. Helps for "tath" -> "Kath", which is
13330 * less common thatn "tath" -> "path". Don't do it when the
13331 * letter is the same, that has already been counted. */
13332 gc = PTR2CHAR(p);
13333 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013334 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013335 bc = PTR2CHAR(su->su_badword);
13336 if (!SPELL_ISUPPER(bc)
13337 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
13338 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013339 }
13340
Bram Moolenaar4770d092006-01-12 23:22:24 +000013341 /* Compute the score for the good word. This only does letter
13342 * insert/delete/swap/replace. REP items are not considered,
13343 * which may make the score a bit higher.
13344 * Use a limit for the score to make it work faster. Use
13345 * MAXSCORE(), because RESCORE() will change the score.
13346 * If the limit is very high then the iterative method is
13347 * inefficient, using an array is quicker. */
13348 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
13349 if (limit > SCORE_LIMITMAX)
13350 goodscore += spell_edit_score(slang, su->su_badword, p);
13351 else
13352 goodscore += spell_edit_score_limit(slang, su->su_badword,
13353 p, limit);
13354
13355 /* When going over the limit don't bother to do the rest. */
13356 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013357 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013358 /* Give a bonus to words seen before. */
13359 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013360
Bram Moolenaar4770d092006-01-12 23:22:24 +000013361 /* Add the suggestion if the score isn't too bad. */
13362 goodscore = RESCORE(goodscore, score);
13363 if (goodscore <= su->su_sfmaxscore)
13364 add_suggestion(su, &su->su_ga, p, su->su_badlen,
13365 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013366 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013367 }
13368 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013369 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013370 }
13371}
13372
13373/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013374 * Find word "word" in fold-case tree for "slang" and return the word number.
13375 */
13376 static int
13377soundfold_find(slang, word)
13378 slang_T *slang;
13379 char_u *word;
13380{
13381 idx_T arridx = 0;
13382 int len;
13383 int wlen = 0;
13384 int c;
13385 char_u *ptr = word;
13386 char_u *byts;
13387 idx_T *idxs;
13388 int wordnr = 0;
13389
13390 byts = slang->sl_sbyts;
13391 idxs = slang->sl_sidxs;
13392
13393 for (;;)
13394 {
13395 /* First byte is the number of possible bytes. */
13396 len = byts[arridx++];
13397
13398 /* If the first possible byte is a zero the word could end here.
13399 * If the word ends we found the word. If not skip the NUL bytes. */
13400 c = ptr[wlen];
13401 if (byts[arridx] == NUL)
13402 {
13403 if (c == NUL)
13404 break;
13405
13406 /* Skip over the zeros, there can be several. */
13407 while (len > 0 && byts[arridx] == NUL)
13408 {
13409 ++arridx;
13410 --len;
13411 }
13412 if (len == 0)
13413 return -1; /* no children, word should have ended here */
13414 ++wordnr;
13415 }
13416
13417 /* If the word ends we didn't find it. */
13418 if (c == NUL)
13419 return -1;
13420
13421 /* Perform a binary search in the list of accepted bytes. */
13422 if (c == TAB) /* <Tab> is handled like <Space> */
13423 c = ' ';
13424 while (byts[arridx] < c)
13425 {
13426 /* The word count is in the first idxs[] entry of the child. */
13427 wordnr += idxs[idxs[arridx]];
13428 ++arridx;
13429 if (--len == 0) /* end of the bytes, didn't find it */
13430 return -1;
13431 }
13432 if (byts[arridx] != c) /* didn't find the byte */
13433 return -1;
13434
13435 /* Continue at the child (if there is one). */
13436 arridx = idxs[arridx];
13437 ++wlen;
13438
13439 /* One space in the good word may stand for several spaces in the
13440 * checked word. */
13441 if (c == ' ')
13442 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
13443 ++wlen;
13444 }
13445
13446 return wordnr;
13447}
13448
13449/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013450 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013451 */
13452 static void
13453make_case_word(fword, cword, flags)
13454 char_u *fword;
13455 char_u *cword;
13456 int flags;
13457{
13458 if (flags & WF_ALLCAP)
13459 /* Make it all upper-case */
13460 allcap_copy(fword, cword);
13461 else if (flags & WF_ONECAP)
13462 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013463 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013464 else
13465 /* Use goodword as-is. */
13466 STRCPY(cword, fword);
13467}
13468
Bram Moolenaarea424162005-06-16 21:51:00 +000013469/*
13470 * Use map string "map" for languages "lp".
13471 */
13472 static void
13473set_map_str(lp, map)
13474 slang_T *lp;
13475 char_u *map;
13476{
13477 char_u *p;
13478 int headc = 0;
13479 int c;
13480 int i;
13481
13482 if (*map == NUL)
13483 {
13484 lp->sl_has_map = FALSE;
13485 return;
13486 }
13487 lp->sl_has_map = TRUE;
13488
Bram Moolenaar4770d092006-01-12 23:22:24 +000013489 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000013490 for (i = 0; i < 256; ++i)
13491 lp->sl_map_array[i] = 0;
13492#ifdef FEAT_MBYTE
13493 hash_init(&lp->sl_map_hash);
13494#endif
13495
13496 /*
13497 * The similar characters are stored separated with slashes:
13498 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13499 * before the same slash. For characters above 255 sl_map_hash is used.
13500 */
13501 for (p = map; *p != NUL; )
13502 {
13503#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013504 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000013505#else
13506 c = *p++;
13507#endif
13508 if (c == '/')
13509 headc = 0;
13510 else
13511 {
13512 if (headc == 0)
13513 headc = c;
13514
13515#ifdef FEAT_MBYTE
13516 /* Characters above 255 don't fit in sl_map_array[], put them in
13517 * the hash table. Each entry is the char, a NUL the headchar and
13518 * a NUL. */
13519 if (c >= 256)
13520 {
13521 int cl = mb_char2len(c);
13522 int headcl = mb_char2len(headc);
13523 char_u *b;
13524 hash_T hash;
13525 hashitem_T *hi;
13526
13527 b = alloc((unsigned)(cl + headcl + 2));
13528 if (b == NULL)
13529 return;
13530 mb_char2bytes(c, b);
13531 b[cl] = NUL;
13532 mb_char2bytes(headc, b + cl + 1);
13533 b[cl + 1 + headcl] = NUL;
13534 hash = hash_hash(b);
13535 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13536 if (HASHITEM_EMPTY(hi))
13537 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13538 else
13539 {
13540 /* This should have been checked when generating the .spl
13541 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013542 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000013543 vim_free(b);
13544 }
13545 }
13546 else
13547#endif
13548 lp->sl_map_array[c] = headc;
13549 }
13550 }
13551}
13552
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013553/*
13554 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13555 * lines in the .aff file.
13556 */
13557 static int
13558similar_chars(slang, c1, c2)
13559 slang_T *slang;
13560 int c1;
13561 int c2;
13562{
Bram Moolenaarea424162005-06-16 21:51:00 +000013563 int m1, m2;
13564#ifdef FEAT_MBYTE
13565 char_u buf[MB_MAXBYTES];
13566 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013567
Bram Moolenaarea424162005-06-16 21:51:00 +000013568 if (c1 >= 256)
13569 {
13570 buf[mb_char2bytes(c1, buf)] = 0;
13571 hi = hash_find(&slang->sl_map_hash, buf);
13572 if (HASHITEM_EMPTY(hi))
13573 m1 = 0;
13574 else
13575 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13576 }
13577 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013578#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013579 m1 = slang->sl_map_array[c1];
13580 if (m1 == 0)
13581 return FALSE;
13582
13583
13584#ifdef FEAT_MBYTE
13585 if (c2 >= 256)
13586 {
13587 buf[mb_char2bytes(c2, buf)] = 0;
13588 hi = hash_find(&slang->sl_map_hash, buf);
13589 if (HASHITEM_EMPTY(hi))
13590 m2 = 0;
13591 else
13592 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13593 }
13594 else
13595#endif
13596 m2 = slang->sl_map_array[c2];
13597
13598 return m1 == m2;
13599}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013600
13601/*
13602 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013603 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013604 */
13605 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000013606add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
13607 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013608 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013609 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013610 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013611 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013612 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013613 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013614 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013615 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013616 int maxsf; /* su_maxscore applies to soundfold score,
13617 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013618{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013619 int goodlen; /* len of goodword changed */
13620 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013621 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013622 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013623 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013624 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013625
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013626 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13627 * "thee the" is added next to changing the first "the" the "thee". */
13628 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013629 pbad = su->su_badptr + badlenarg;
13630 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013631 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013632 goodlen = (int)(pgood - goodword);
13633 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013634 if (goodlen <= 0 || badlen <= 0)
13635 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013636 mb_ptr_back(goodword, pgood);
13637 mb_ptr_back(su->su_badptr, pbad);
13638#ifdef FEAT_MBYTE
13639 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013640 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013641 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13642 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013643 }
13644 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013645#endif
13646 if (*pgood != *pbad)
13647 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013648 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013649
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013650 if (badlen == 0 && goodlen == 0)
13651 /* goodword doesn't change anything; may happen for "the the" changing
13652 * the first "the" to itself. */
13653 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013654
Bram Moolenaar89d40322006-08-29 15:30:07 +000013655 if (gap->ga_len == 0)
13656 i = -1;
13657 else
13658 {
13659 /* Check if the word is already there. Also check the length that is
13660 * being replaced "thes," -> "these" is a different suggestion from
13661 * "thes" -> "these". */
13662 stp = &SUG(*gap, 0);
13663 for (i = gap->ga_len; --i >= 0; ++stp)
13664 if (stp->st_wordlen == goodlen
13665 && stp->st_orglen == badlen
13666 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013667 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013668 /*
13669 * Found it. Remember the word with the lowest score.
13670 */
13671 if (stp->st_slang == NULL)
13672 stp->st_slang = slang;
13673
13674 new_sug.st_score = score;
13675 new_sug.st_altscore = altscore;
13676 new_sug.st_had_bonus = had_bonus;
13677
13678 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013679 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013680 /* Only one of the two had the soundalike score computed.
13681 * Need to do that for the other one now, otherwise the
13682 * scores can't be compared. This happens because
13683 * suggest_try_change() doesn't compute the soundalike
13684 * word to keep it fast, while some special methods set
13685 * the soundalike score to zero. */
13686 if (had_bonus)
13687 rescore_one(su, stp);
13688 else
13689 {
13690 new_sug.st_word = stp->st_word;
13691 new_sug.st_wordlen = stp->st_wordlen;
13692 new_sug.st_slang = stp->st_slang;
13693 new_sug.st_orglen = badlen;
13694 rescore_one(su, &new_sug);
13695 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013696 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013697
Bram Moolenaar89d40322006-08-29 15:30:07 +000013698 if (stp->st_score > new_sug.st_score)
13699 {
13700 stp->st_score = new_sug.st_score;
13701 stp->st_altscore = new_sug.st_altscore;
13702 stp->st_had_bonus = new_sug.st_had_bonus;
13703 }
13704 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013705 }
Bram Moolenaar89d40322006-08-29 15:30:07 +000013706 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013707
Bram Moolenaar4770d092006-01-12 23:22:24 +000013708 if (i < 0 && ga_grow(gap, 1) == OK)
13709 {
13710 /* Add a suggestion. */
13711 stp = &SUG(*gap, gap->ga_len);
13712 stp->st_word = vim_strnsave(goodword, goodlen);
13713 if (stp->st_word != NULL)
13714 {
13715 stp->st_wordlen = goodlen;
13716 stp->st_score = score;
13717 stp->st_altscore = altscore;
13718 stp->st_had_bonus = had_bonus;
13719 stp->st_orglen = badlen;
13720 stp->st_slang = slang;
13721 ++gap->ga_len;
13722
13723 /* If we have too many suggestions now, sort the list and keep
13724 * the best suggestions. */
13725 if (gap->ga_len > SUG_MAX_COUNT(su))
13726 {
13727 if (maxsf)
13728 su->su_sfmaxscore = cleanup_suggestions(gap,
13729 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13730 else
13731 {
13732 i = su->su_maxscore;
13733 su->su_maxscore = cleanup_suggestions(gap,
13734 su->su_maxscore, SUG_CLEAN_COUNT(su));
13735 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013736 }
13737 }
13738 }
13739}
13740
13741/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013742 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13743 * for split words, such as "the the". Remove these from the list here.
13744 */
13745 static void
13746check_suggestions(su, gap)
13747 suginfo_T *su;
13748 garray_T *gap; /* either su_ga or su_sga */
13749{
13750 suggest_T *stp;
13751 int i;
13752 char_u longword[MAXWLEN + 1];
13753 int len;
13754 hlf_T attr;
13755
13756 stp = &SUG(*gap, 0);
13757 for (i = gap->ga_len - 1; i >= 0; --i)
13758 {
13759 /* Need to append what follows to check for "the the". */
13760 STRCPY(longword, stp[i].st_word);
13761 len = stp[i].st_wordlen;
13762 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13763 MAXWLEN - len);
13764 attr = HLF_COUNT;
13765 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13766 if (attr != HLF_COUNT)
13767 {
13768 /* Remove this entry. */
13769 vim_free(stp[i].st_word);
13770 --gap->ga_len;
13771 if (i < gap->ga_len)
13772 mch_memmove(stp + i, stp + i + 1,
13773 sizeof(suggest_T) * (gap->ga_len - i));
13774 }
13775 }
13776}
13777
13778
13779/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013780 * Add a word to be banned.
13781 */
13782 static void
13783add_banned(su, word)
13784 suginfo_T *su;
13785 char_u *word;
13786{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013787 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013788 hash_T hash;
13789 hashitem_T *hi;
13790
Bram Moolenaar4770d092006-01-12 23:22:24 +000013791 hash = hash_hash(word);
13792 hi = hash_lookup(&su->su_banned, word, hash);
13793 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013794 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013795 s = vim_strsave(word);
13796 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013797 hash_add_item(&su->su_banned, hi, s, hash);
13798 }
13799}
13800
13801/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013802 * Recompute the score for all suggestions if sound-folding is possible. This
13803 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013804 */
13805 static void
13806rescore_suggestions(su)
13807 suginfo_T *su;
13808{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013809 int i;
13810
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013811 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013812 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013813 rescore_one(su, &SUG(su->su_ga, i));
13814}
13815
13816/*
13817 * Recompute the score for one suggestion if sound-folding is possible.
13818 */
13819 static void
13820rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013821 suginfo_T *su;
13822 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013823{
13824 slang_T *slang = stp->st_slang;
13825 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013826 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013827
13828 /* Only rescore suggestions that have no sal score yet and do have a
13829 * language. */
13830 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13831 {
13832 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013833 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013834 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013835 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013836 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013837 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013838 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013839
13840 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013841 if (stp->st_altscore == SCORE_MAXMAX)
13842 stp->st_altscore = SCORE_BIG;
13843 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13844 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013845 }
13846}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013847
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013848static int
13849#ifdef __BORLANDC__
13850_RTLENTRYF
13851#endif
13852sug_compare __ARGS((const void *s1, const void *s2));
13853
13854/*
13855 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013856 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013857 */
13858 static int
13859#ifdef __BORLANDC__
13860_RTLENTRYF
13861#endif
13862sug_compare(s1, s2)
13863 const void *s1;
13864 const void *s2;
13865{
13866 suggest_T *p1 = (suggest_T *)s1;
13867 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013868 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013869
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013870 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013871 {
13872 n = p1->st_altscore - p2->st_altscore;
13873 if (n == 0)
13874 n = STRICMP(p1->st_word, p2->st_word);
13875 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013876 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013877}
13878
13879/*
13880 * Cleanup the suggestions:
13881 * - Sort on score.
13882 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013883 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013884 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013885 static int
13886cleanup_suggestions(gap, maxscore, keep)
13887 garray_T *gap;
13888 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013889 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013890{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013891 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013892 int i;
13893
13894 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013895 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013896
13897 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013898 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013899 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013900 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013901 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013902 gap->ga_len = keep;
13903 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013904 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013905 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013906}
13907
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013908#if defined(FEAT_EVAL) || defined(PROTO)
13909/*
13910 * Soundfold a string, for soundfold().
13911 * Result is in allocated memory, NULL for an error.
13912 */
13913 char_u *
13914eval_soundfold(word)
13915 char_u *word;
13916{
13917 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013918 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013919 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013920
13921 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13922 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013923 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013924 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013925 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013926 if (lp->lp_slang->sl_sal.ga_len > 0)
13927 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013928 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013929 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013930 return vim_strsave(sound);
13931 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013932 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013933
13934 /* No language with sound folding, return word as-is. */
13935 return vim_strsave(word);
13936}
13937#endif
13938
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013939/*
13940 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000013941 *
13942 * There are many ways to turn a word into a sound-a-like representation. The
13943 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
13944 * swedish name matching - survey and test of different algorithms" by Klas
13945 * Erikson.
13946 *
13947 * We support two methods:
13948 * 1. SOFOFROM/SOFOTO do a simple character mapping.
13949 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013950 */
13951 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013952spell_soundfold(slang, inword, folded, res)
13953 slang_T *slang;
13954 char_u *inword;
13955 int folded; /* "inword" is already case-folded */
13956 char_u *res;
13957{
13958 char_u fword[MAXWLEN];
13959 char_u *word;
13960
13961 if (slang->sl_sofo)
13962 /* SOFOFROM and SOFOTO used */
13963 spell_soundfold_sofo(slang, inword, res);
13964 else
13965 {
13966 /* SAL items used. Requires the word to be case-folded. */
13967 if (folded)
13968 word = inword;
13969 else
13970 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013971 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013972 word = fword;
13973 }
13974
13975#ifdef FEAT_MBYTE
13976 if (has_mbyte)
13977 spell_soundfold_wsal(slang, word, res);
13978 else
13979#endif
13980 spell_soundfold_sal(slang, word, res);
13981 }
13982}
13983
13984/*
13985 * Perform sound folding of "inword" into "res" according to SOFOFROM and
13986 * SOFOTO lines.
13987 */
13988 static void
13989spell_soundfold_sofo(slang, inword, res)
13990 slang_T *slang;
13991 char_u *inword;
13992 char_u *res;
13993{
13994 char_u *s;
13995 int ri = 0;
13996 int c;
13997
13998#ifdef FEAT_MBYTE
13999 if (has_mbyte)
14000 {
14001 int prevc = 0;
14002 int *ip;
14003
14004 /* The sl_sal_first[] table contains the translation for chars up to
14005 * 255, sl_sal the rest. */
14006 for (s = inword; *s != NUL; )
14007 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014008 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014009 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14010 c = ' ';
14011 else if (c < 256)
14012 c = slang->sl_sal_first[c];
14013 else
14014 {
14015 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
14016 if (ip == NULL) /* empty list, can't match */
14017 c = NUL;
14018 else
14019 for (;;) /* find "c" in the list */
14020 {
14021 if (*ip == 0) /* not found */
14022 {
14023 c = NUL;
14024 break;
14025 }
14026 if (*ip == c) /* match! */
14027 {
14028 c = ip[1];
14029 break;
14030 }
14031 ip += 2;
14032 }
14033 }
14034
14035 if (c != NUL && c != prevc)
14036 {
14037 ri += mb_char2bytes(c, res + ri);
14038 if (ri + MB_MAXBYTES > MAXWLEN)
14039 break;
14040 prevc = c;
14041 }
14042 }
14043 }
14044 else
14045#endif
14046 {
14047 /* The sl_sal_first[] table contains the translation. */
14048 for (s = inword; (c = *s) != NUL; ++s)
14049 {
14050 if (vim_iswhite(c))
14051 c = ' ';
14052 else
14053 c = slang->sl_sal_first[c];
14054 if (c != NUL && (ri == 0 || res[ri - 1] != c))
14055 res[ri++] = c;
14056 }
14057 }
14058
14059 res[ri] = NUL;
14060}
14061
14062 static void
14063spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014064 slang_T *slang;
14065 char_u *inword;
14066 char_u *res;
14067{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014068 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014069 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014070 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014071 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014072 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014073 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014074 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014075 int n, k = 0;
14076 int z0;
14077 int k0;
14078 int n0;
14079 int c;
14080 int pri;
14081 int p0 = -333;
14082 int c0;
14083
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014084 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014085 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014086 if (slang->sl_rem_accents)
14087 {
14088 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014089 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014090 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014091 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014092 {
14093 *t++ = ' ';
14094 s = skipwhite(s);
14095 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014096 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014097 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014098 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014099 *t++ = *s;
14100 ++s;
14101 }
14102 }
14103 *t = NUL;
14104 }
14105 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014106 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014107
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014108 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014109
14110 /*
14111 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014112 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014113 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014114 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014115 while ((c = word[i]) != NUL)
14116 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014117 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014118 n = slang->sl_sal_first[c];
14119 z0 = 0;
14120
14121 if (n >= 0)
14122 {
14123 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014124 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014125 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014126 /* Quickly skip entries that don't match the word. Most
14127 * entries are less then three chars, optimize for that. */
14128 k = smp[n].sm_leadlen;
14129 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014130 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014131 if (word[i + 1] != s[1])
14132 continue;
14133 if (k > 2)
14134 {
14135 for (j = 2; j < k; ++j)
14136 if (word[i + j] != s[j])
14137 break;
14138 if (j < k)
14139 continue;
14140 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014141 }
14142
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014143 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014144 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014145 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014146 while (*pf != NUL && *pf != word[i + k])
14147 ++pf;
14148 if (*pf == NUL)
14149 continue;
14150 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014151 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014152 s = smp[n].sm_rules;
14153 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014154
14155 p0 = *s;
14156 k0 = k;
14157 while (*s == '-' && k > 1)
14158 {
14159 k--;
14160 s++;
14161 }
14162 if (*s == '<')
14163 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014164 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014165 {
14166 /* determine priority */
14167 pri = *s - '0';
14168 s++;
14169 }
14170 if (*s == '^' && *(s + 1) == '^')
14171 s++;
14172
14173 if (*s == NUL
14174 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014175 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014176 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014177 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014178 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014179 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014180 && spell_iswordp(word + i - 1, curbuf)
14181 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014182 {
14183 /* search for followup rules, if: */
14184 /* followup and k > 1 and NO '-' in searchstring */
14185 c0 = word[i + k - 1];
14186 n0 = slang->sl_sal_first[c0];
14187
14188 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014189 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014190 {
14191 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014192 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014193 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014194 /* Quickly skip entries that don't match the word.
14195 * */
14196 k0 = smp[n0].sm_leadlen;
14197 if (k0 > 1)
14198 {
14199 if (word[i + k] != s[1])
14200 continue;
14201 if (k0 > 2)
14202 {
14203 pf = word + i + k + 1;
14204 for (j = 2; j < k0; ++j)
14205 if (*pf++ != s[j])
14206 break;
14207 if (j < k0)
14208 continue;
14209 }
14210 }
14211 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014212
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014213 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014214 {
14215 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014216 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014217 while (*pf != NUL && *pf != word[i + k0])
14218 ++pf;
14219 if (*pf == NUL)
14220 continue;
14221 ++k0;
14222 }
14223
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014224 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014225 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014226 while (*s == '-')
14227 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014228 /* "k0" gets NOT reduced because
14229 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014230 s++;
14231 }
14232 if (*s == '<')
14233 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014234 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014235 {
14236 p0 = *s - '0';
14237 s++;
14238 }
14239
14240 if (*s == NUL
14241 /* *s == '^' cuts */
14242 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014243 && !spell_iswordp(word + i + k0,
14244 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014245 {
14246 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014247 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014248 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014249
14250 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014251 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014252 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014253 /* rule fits; stop search */
14254 break;
14255 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014256 }
14257
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014258 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014259 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014260 }
14261
14262 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014263 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014264 if (s == NULL)
14265 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014266 pf = smp[n].sm_rules;
14267 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014268 if (p0 == 1 && z == 0)
14269 {
14270 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014271 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
14272 || res[reslen - 1] == *s))
14273 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014274 z0 = 1;
14275 z = 1;
14276 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014277 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014278 {
14279 word[i + k0] = *s;
14280 k0++;
14281 s++;
14282 }
14283 if (k > k0)
14284 mch_memmove(word + i + k0, word + i + k,
14285 STRLEN(word + i + k) + 1);
14286
14287 /* new "actual letter" */
14288 c = word[i];
14289 }
14290 else
14291 {
14292 /* no '<' rule used */
14293 i += k - 1;
14294 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014295 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014296 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014297 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014298 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014299 s++;
14300 }
14301 /* new "actual letter" */
14302 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014303 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014304 {
14305 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014306 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014307 mch_memmove(word, word + i + 1,
14308 STRLEN(word + i + 1) + 1);
14309 i = 0;
14310 z0 = 1;
14311 }
14312 }
14313 break;
14314 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014315 }
14316 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014317 else if (vim_iswhite(c))
14318 {
14319 c = ' ';
14320 k = 1;
14321 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014322
14323 if (z0 == 0)
14324 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014325 if (k && !p0 && reslen < MAXWLEN && c != NUL
14326 && (!slang->sl_collapse || reslen == 0
14327 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014328 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014329 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014330
14331 i++;
14332 z = 0;
14333 k = 0;
14334 }
14335 }
14336
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014337 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014338}
14339
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014340#ifdef FEAT_MBYTE
14341/*
14342 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14343 * Multi-byte version of spell_soundfold().
14344 */
14345 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014346spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014347 slang_T *slang;
14348 char_u *inword;
14349 char_u *res;
14350{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014351 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014352 int word[MAXWLEN];
14353 int wres[MAXWLEN];
14354 int l;
14355 char_u *s;
14356 int *ws;
14357 char_u *t;
14358 int *pf;
14359 int i, j, z;
14360 int reslen;
14361 int n, k = 0;
14362 int z0;
14363 int k0;
14364 int n0;
14365 int c;
14366 int pri;
14367 int p0 = -333;
14368 int c0;
14369 int did_white = FALSE;
14370
14371 /*
14372 * Convert the multi-byte string to a wide-character string.
14373 * Remove accents, if wanted. We actually remove all non-word characters.
14374 * But keep white space.
14375 */
14376 n = 0;
14377 for (s = inword; *s != NUL; )
14378 {
14379 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014380 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014381 if (slang->sl_rem_accents)
14382 {
14383 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14384 {
14385 if (did_white)
14386 continue;
14387 c = ' ';
14388 did_white = TRUE;
14389 }
14390 else
14391 {
14392 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014393 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014394 continue;
14395 }
14396 }
14397 word[n++] = c;
14398 }
14399 word[n] = NUL;
14400
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014401 /*
14402 * This comes from Aspell phonet.cpp.
14403 * Converted from C++ to C. Added support for multi-byte chars.
14404 * Changed to keep spaces.
14405 */
14406 i = reslen = z = 0;
14407 while ((c = word[i]) != NUL)
14408 {
14409 /* Start with the first rule that has the character in the word. */
14410 n = slang->sl_sal_first[c & 0xff];
14411 z0 = 0;
14412
14413 if (n >= 0)
14414 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014415 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014416 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
14417 {
14418 /* Quickly skip entries that don't match the word. Most
14419 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014420 if (c != ws[0])
14421 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014422 k = smp[n].sm_leadlen;
14423 if (k > 1)
14424 {
14425 if (word[i + 1] != ws[1])
14426 continue;
14427 if (k > 2)
14428 {
14429 for (j = 2; j < k; ++j)
14430 if (word[i + j] != ws[j])
14431 break;
14432 if (j < k)
14433 continue;
14434 }
14435 }
14436
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014437 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014438 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014439 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014440 while (*pf != NUL && *pf != word[i + k])
14441 ++pf;
14442 if (*pf == NUL)
14443 continue;
14444 ++k;
14445 }
14446 s = smp[n].sm_rules;
14447 pri = 5; /* default priority */
14448
14449 p0 = *s;
14450 k0 = k;
14451 while (*s == '-' && k > 1)
14452 {
14453 k--;
14454 s++;
14455 }
14456 if (*s == '<')
14457 s++;
14458 if (VIM_ISDIGIT(*s))
14459 {
14460 /* determine priority */
14461 pri = *s - '0';
14462 s++;
14463 }
14464 if (*s == '^' && *(s + 1) == '^')
14465 s++;
14466
14467 if (*s == NUL
14468 || (*s == '^'
14469 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014470 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014471 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014472 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014473 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014474 && spell_iswordp_w(word + i - 1, curbuf)
14475 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014476 {
14477 /* search for followup rules, if: */
14478 /* followup and k > 1 and NO '-' in searchstring */
14479 c0 = word[i + k - 1];
14480 n0 = slang->sl_sal_first[c0 & 0xff];
14481
14482 if (slang->sl_followup && k > 1 && n0 >= 0
14483 && p0 != '-' && word[i + k] != NUL)
14484 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014485 /* Test follow-up rule for "word[i + k]"; loop over
14486 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014487 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14488 == (c0 & 0xff); ++n0)
14489 {
14490 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014491 */
14492 if (c0 != ws[0])
14493 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014494 k0 = smp[n0].sm_leadlen;
14495 if (k0 > 1)
14496 {
14497 if (word[i + k] != ws[1])
14498 continue;
14499 if (k0 > 2)
14500 {
14501 pf = word + i + k + 1;
14502 for (j = 2; j < k0; ++j)
14503 if (*pf++ != ws[j])
14504 break;
14505 if (j < k0)
14506 continue;
14507 }
14508 }
14509 k0 += k - 1;
14510
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014511 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014512 {
14513 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014514 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014515 while (*pf != NUL && *pf != word[i + k0])
14516 ++pf;
14517 if (*pf == NUL)
14518 continue;
14519 ++k0;
14520 }
14521
14522 p0 = 5;
14523 s = smp[n0].sm_rules;
14524 while (*s == '-')
14525 {
14526 /* "k0" gets NOT reduced because
14527 * "if (k0 == k)" */
14528 s++;
14529 }
14530 if (*s == '<')
14531 s++;
14532 if (VIM_ISDIGIT(*s))
14533 {
14534 p0 = *s - '0';
14535 s++;
14536 }
14537
14538 if (*s == NUL
14539 /* *s == '^' cuts */
14540 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014541 && !spell_iswordp_w(word + i + k0,
14542 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014543 {
14544 if (k0 == k)
14545 /* this is just a piece of the string */
14546 continue;
14547
14548 if (p0 < pri)
14549 /* priority too low */
14550 continue;
14551 /* rule fits; stop search */
14552 break;
14553 }
14554 }
14555
14556 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14557 == (c0 & 0xff))
14558 continue;
14559 }
14560
14561 /* replace string */
14562 ws = smp[n].sm_to_w;
14563 s = smp[n].sm_rules;
14564 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14565 if (p0 == 1 && z == 0)
14566 {
14567 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014568 if (reslen > 0 && ws != NULL && *ws != NUL
14569 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014570 || wres[reslen - 1] == *ws))
14571 reslen--;
14572 z0 = 1;
14573 z = 1;
14574 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014575 if (ws != NULL)
14576 while (*ws != NUL && word[i + k0] != NUL)
14577 {
14578 word[i + k0] = *ws;
14579 k0++;
14580 ws++;
14581 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014582 if (k > k0)
14583 mch_memmove(word + i + k0, word + i + k,
14584 sizeof(int) * (STRLEN(word + i + k) + 1));
14585
14586 /* new "actual letter" */
14587 c = word[i];
14588 }
14589 else
14590 {
14591 /* no '<' rule used */
14592 i += k - 1;
14593 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014594 if (ws != NULL)
14595 while (*ws != NUL && ws[1] != NUL
14596 && reslen < MAXWLEN)
14597 {
14598 if (reslen == 0 || wres[reslen - 1] != *ws)
14599 wres[reslen++] = *ws;
14600 ws++;
14601 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014602 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014603 if (ws == NULL)
14604 c = NUL;
14605 else
14606 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014607 if (strstr((char *)s, "^^") != NULL)
14608 {
14609 if (c != NUL)
14610 wres[reslen++] = c;
14611 mch_memmove(word, word + i + 1,
14612 sizeof(int) * (STRLEN(word + i + 1) + 1));
14613 i = 0;
14614 z0 = 1;
14615 }
14616 }
14617 break;
14618 }
14619 }
14620 }
14621 else if (vim_iswhite(c))
14622 {
14623 c = ' ';
14624 k = 1;
14625 }
14626
14627 if (z0 == 0)
14628 {
14629 if (k && !p0 && reslen < MAXWLEN && c != NUL
14630 && (!slang->sl_collapse || reslen == 0
14631 || wres[reslen - 1] != c))
14632 /* condense only double letters */
14633 wres[reslen++] = c;
14634
14635 i++;
14636 z = 0;
14637 k = 0;
14638 }
14639 }
14640
14641 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14642 l = 0;
14643 for (n = 0; n < reslen; ++n)
14644 {
14645 l += mb_char2bytes(wres[n], res + l);
14646 if (l + MB_MAXBYTES > MAXWLEN)
14647 break;
14648 }
14649 res[l] = NUL;
14650}
14651#endif
14652
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014653/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014654 * Compute a score for two sound-a-like words.
14655 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14656 * Instead of a generic loop we write out the code. That keeps it fast by
14657 * avoiding checks that will not be possible.
14658 */
14659 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014660soundalike_score(goodstart, badstart)
14661 char_u *goodstart; /* sound-folded good word */
14662 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014663{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014664 char_u *goodsound = goodstart;
14665 char_u *badsound = badstart;
14666 int goodlen;
14667 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014668 int n;
14669 char_u *pl, *ps;
14670 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014671 int score = 0;
14672
14673 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
14674 * counted so much, vowels halfway the word aren't counted at all. */
14675 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14676 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014677 if (badsound[1] == goodsound[1]
14678 || (badsound[1] != NUL
14679 && goodsound[1] != NUL
14680 && badsound[2] == goodsound[2]))
14681 {
14682 /* handle like a substitute */
14683 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014684 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014685 {
14686 score = 2 * SCORE_DEL / 3;
14687 if (*badsound == '*')
14688 ++badsound;
14689 else
14690 ++goodsound;
14691 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014692 }
14693
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014694 goodlen = (int)STRLEN(goodsound);
14695 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014696
Bram Moolenaarf711faf2007-05-10 16:48:19 +000014697 /* Return quickly if the lengths are too different to be fixed by two
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014698 * changes. */
14699 n = goodlen - badlen;
14700 if (n < -2 || n > 2)
14701 return SCORE_MAXMAX;
14702
14703 if (n > 0)
14704 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014705 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014706 ps = badsound;
14707 }
14708 else
14709 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014710 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014711 ps = goodsound;
14712 }
14713
14714 /* Skip over the identical part. */
14715 while (*pl == *ps && *pl != NUL)
14716 {
14717 ++pl;
14718 ++ps;
14719 }
14720
14721 switch (n)
14722 {
14723 case -2:
14724 case 2:
14725 /*
14726 * Must delete two characters from "pl".
14727 */
14728 ++pl; /* first delete */
14729 while (*pl == *ps)
14730 {
14731 ++pl;
14732 ++ps;
14733 }
14734 /* strings must be equal after second delete */
14735 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014736 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014737
14738 /* Failed to compare. */
14739 break;
14740
14741 case -1:
14742 case 1:
14743 /*
14744 * Minimal one delete from "pl" required.
14745 */
14746
14747 /* 1: delete */
14748 pl2 = pl + 1;
14749 ps2 = ps;
14750 while (*pl2 == *ps2)
14751 {
14752 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014753 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014754 ++pl2;
14755 ++ps2;
14756 }
14757
14758 /* 2: delete then swap, then rest must be equal */
14759 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14760 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014761 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014762
14763 /* 3: delete then substitute, then the rest must be equal */
14764 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014765 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014766
14767 /* 4: first swap then delete */
14768 if (pl[0] == ps[1] && pl[1] == ps[0])
14769 {
14770 pl2 = pl + 2; /* swap, skip two chars */
14771 ps2 = ps + 2;
14772 while (*pl2 == *ps2)
14773 {
14774 ++pl2;
14775 ++ps2;
14776 }
14777 /* delete a char and then strings must be equal */
14778 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014779 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014780 }
14781
14782 /* 5: first substitute then delete */
14783 pl2 = pl + 1; /* substitute, skip one char */
14784 ps2 = ps + 1;
14785 while (*pl2 == *ps2)
14786 {
14787 ++pl2;
14788 ++ps2;
14789 }
14790 /* delete a char and then strings must be equal */
14791 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014792 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014793
14794 /* Failed to compare. */
14795 break;
14796
14797 case 0:
14798 /*
14799 * Lenghts are equal, thus changes must result in same length: An
14800 * insert is only possible in combination with a delete.
14801 * 1: check if for identical strings
14802 */
14803 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014804 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014805
14806 /* 2: swap */
14807 if (pl[0] == ps[1] && pl[1] == ps[0])
14808 {
14809 pl2 = pl + 2; /* swap, skip two chars */
14810 ps2 = ps + 2;
14811 while (*pl2 == *ps2)
14812 {
14813 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014814 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014815 ++pl2;
14816 ++ps2;
14817 }
14818 /* 3: swap and swap again */
14819 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14820 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014821 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014822
14823 /* 4: swap and substitute */
14824 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014825 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014826 }
14827
14828 /* 5: substitute */
14829 pl2 = pl + 1;
14830 ps2 = ps + 1;
14831 while (*pl2 == *ps2)
14832 {
14833 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014834 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014835 ++pl2;
14836 ++ps2;
14837 }
14838
14839 /* 6: substitute and swap */
14840 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14841 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014842 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014843
14844 /* 7: substitute and substitute */
14845 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014846 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014847
14848 /* 8: insert then delete */
14849 pl2 = pl;
14850 ps2 = ps + 1;
14851 while (*pl2 == *ps2)
14852 {
14853 ++pl2;
14854 ++ps2;
14855 }
14856 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014857 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014858
14859 /* 9: delete then insert */
14860 pl2 = pl + 1;
14861 ps2 = ps;
14862 while (*pl2 == *ps2)
14863 {
14864 ++pl2;
14865 ++ps2;
14866 }
14867 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014868 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014869
14870 /* Failed to compare. */
14871 break;
14872 }
14873
14874 return SCORE_MAXMAX;
14875}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014876
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014877/*
14878 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014879 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014880 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000014881 * The algorithm is described by Du and Chang, 1992.
14882 * The implementation of the algorithm comes from Aspell editdist.cpp,
14883 * edit_distance(). It has been converted from C++ to C and modified to
14884 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014885 */
14886 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000014887spell_edit_score(slang, badword, goodword)
14888 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014889 char_u *badword;
14890 char_u *goodword;
14891{
14892 int *cnt;
Bram Moolenaarf711faf2007-05-10 16:48:19 +000014893 int badlen, goodlen; /* lengths including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014894 int j, i;
14895 int t;
14896 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014897 int pbc, pgc;
14898#ifdef FEAT_MBYTE
14899 char_u *p;
14900 int wbadword[MAXWLEN];
14901 int wgoodword[MAXWLEN];
14902
14903 if (has_mbyte)
14904 {
14905 /* Get the characters from the multi-byte strings and put them in an
14906 * int array for easy access. */
14907 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014908 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014909 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014910 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014911 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014912 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014913 }
14914 else
14915#endif
14916 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014917 badlen = (int)STRLEN(badword) + 1;
14918 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014919 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014920
14921 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
14922#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014923 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
14924 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014925 if (cnt == NULL)
14926 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014927
14928 CNT(0, 0) = 0;
14929 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000014930 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014931
14932 for (i = 1; i <= badlen; ++i)
14933 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014934 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014935 for (j = 1; j <= goodlen; ++j)
14936 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014937#ifdef FEAT_MBYTE
14938 if (has_mbyte)
14939 {
14940 bc = wbadword[i - 1];
14941 gc = wgoodword[j - 1];
14942 }
14943 else
14944#endif
14945 {
14946 bc = badword[i - 1];
14947 gc = goodword[j - 1];
14948 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014949 if (bc == gc)
14950 CNT(i, j) = CNT(i - 1, j - 1);
14951 else
14952 {
14953 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014954 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014955 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
14956 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014957 {
14958 /* For a similar character use SCORE_SIMILAR. */
14959 if (slang != NULL
14960 && slang->sl_has_map
14961 && similar_chars(slang, gc, bc))
14962 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
14963 else
14964 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
14965 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014966
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014967 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014968 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014969#ifdef FEAT_MBYTE
14970 if (has_mbyte)
14971 {
14972 pbc = wbadword[i - 2];
14973 pgc = wgoodword[j - 2];
14974 }
14975 else
14976#endif
14977 {
14978 pbc = badword[i - 2];
14979 pgc = goodword[j - 2];
14980 }
14981 if (bc == pgc && pbc == gc)
14982 {
14983 t = SCORE_SWAP + CNT(i - 2, j - 2);
14984 if (t < CNT(i, j))
14985 CNT(i, j) = t;
14986 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014987 }
14988 t = SCORE_DEL + CNT(i - 1, j);
14989 if (t < CNT(i, j))
14990 CNT(i, j) = t;
14991 t = SCORE_INS + CNT(i, j - 1);
14992 if (t < CNT(i, j))
14993 CNT(i, j) = t;
14994 }
14995 }
14996 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014997
14998 i = CNT(badlen - 1, goodlen - 1);
14999 vim_free(cnt);
15000 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015001}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000015002
Bram Moolenaar4770d092006-01-12 23:22:24 +000015003typedef struct
15004{
15005 int badi;
15006 int goodi;
15007 int score;
15008} limitscore_T;
15009
15010/*
15011 * Like spell_edit_score(), but with a limit on the score to make it faster.
15012 * May return SCORE_MAXMAX when the score is higher than "limit".
15013 *
15014 * This uses a stack for the edits still to be tried.
15015 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
15016 * for multi-byte characters.
15017 */
15018 static int
15019spell_edit_score_limit(slang, badword, goodword, limit)
15020 slang_T *slang;
15021 char_u *badword;
15022 char_u *goodword;
15023 int limit;
15024{
15025 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15026 int stackidx;
15027 int bi, gi;
15028 int bi2, gi2;
15029 int bc, gc;
15030 int score;
15031 int score_off;
15032 int minscore;
15033 int round;
15034
15035#ifdef FEAT_MBYTE
15036 /* Multi-byte characters require a bit more work, use a different function
15037 * to avoid testing "has_mbyte" quite often. */
15038 if (has_mbyte)
15039 return spell_edit_score_limit_w(slang, badword, goodword, limit);
15040#endif
15041
15042 /*
15043 * The idea is to go from start to end over the words. So long as
15044 * characters are equal just continue, this always gives the lowest score.
15045 * When there is a difference try several alternatives. Each alternative
15046 * increases "score" for the edit distance. Some of the alternatives are
15047 * pushed unto a stack and tried later, some are tried right away. At the
15048 * end of the word the score for one alternative is known. The lowest
15049 * possible score is stored in "minscore".
15050 */
15051 stackidx = 0;
15052 bi = 0;
15053 gi = 0;
15054 score = 0;
15055 minscore = limit + 1;
15056
15057 for (;;)
15058 {
15059 /* Skip over an equal part, score remains the same. */
15060 for (;;)
15061 {
15062 bc = badword[bi];
15063 gc = goodword[gi];
15064 if (bc != gc) /* stop at a char that's different */
15065 break;
15066 if (bc == NUL) /* both words end */
15067 {
15068 if (score < minscore)
15069 minscore = score;
15070 goto pop; /* do next alternative */
15071 }
15072 ++bi;
15073 ++gi;
15074 }
15075
15076 if (gc == NUL) /* goodword ends, delete badword chars */
15077 {
15078 do
15079 {
15080 if ((score += SCORE_DEL) >= minscore)
15081 goto pop; /* do next alternative */
15082 } while (badword[++bi] != NUL);
15083 minscore = score;
15084 }
15085 else if (bc == NUL) /* badword ends, insert badword chars */
15086 {
15087 do
15088 {
15089 if ((score += SCORE_INS) >= minscore)
15090 goto pop; /* do next alternative */
15091 } while (goodword[++gi] != NUL);
15092 minscore = score;
15093 }
15094 else /* both words continue */
15095 {
15096 /* If not close to the limit, perform a change. Only try changes
15097 * that may lead to a lower score than "minscore".
15098 * round 0: try deleting a char from badword
15099 * round 1: try inserting a char in badword */
15100 for (round = 0; round <= 1; ++round)
15101 {
15102 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15103 if (score_off < minscore)
15104 {
15105 if (score_off + SCORE_EDIT_MIN >= minscore)
15106 {
15107 /* Near the limit, rest of the words must match. We
15108 * can check that right now, no need to push an item
15109 * onto the stack. */
15110 bi2 = bi + 1 - round;
15111 gi2 = gi + round;
15112 while (goodword[gi2] == badword[bi2])
15113 {
15114 if (goodword[gi2] == NUL)
15115 {
15116 minscore = score_off;
15117 break;
15118 }
15119 ++bi2;
15120 ++gi2;
15121 }
15122 }
15123 else
15124 {
15125 /* try deleting/inserting a character later */
15126 stack[stackidx].badi = bi + 1 - round;
15127 stack[stackidx].goodi = gi + round;
15128 stack[stackidx].score = score_off;
15129 ++stackidx;
15130 }
15131 }
15132 }
15133
15134 if (score + SCORE_SWAP < minscore)
15135 {
15136 /* If swapping two characters makes a match then the
15137 * substitution is more expensive, thus there is no need to
15138 * try both. */
15139 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
15140 {
15141 /* Swap two characters, that is: skip them. */
15142 gi += 2;
15143 bi += 2;
15144 score += SCORE_SWAP;
15145 continue;
15146 }
15147 }
15148
15149 /* Substitute one character for another which is the same
15150 * thing as deleting a character from both goodword and badword.
15151 * Use a better score when there is only a case difference. */
15152 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15153 score += SCORE_ICASE;
15154 else
15155 {
15156 /* For a similar character use SCORE_SIMILAR. */
15157 if (slang != NULL
15158 && slang->sl_has_map
15159 && similar_chars(slang, gc, bc))
15160 score += SCORE_SIMILAR;
15161 else
15162 score += SCORE_SUBST;
15163 }
15164
15165 if (score < minscore)
15166 {
15167 /* Do the substitution. */
15168 ++gi;
15169 ++bi;
15170 continue;
15171 }
15172 }
15173pop:
15174 /*
15175 * Get here to try the next alternative, pop it from the stack.
15176 */
15177 if (stackidx == 0) /* stack is empty, finished */
15178 break;
15179
15180 /* pop an item from the stack */
15181 --stackidx;
15182 gi = stack[stackidx].goodi;
15183 bi = stack[stackidx].badi;
15184 score = stack[stackidx].score;
15185 }
15186
15187 /* When the score goes over "limit" it may actually be much higher.
15188 * Return a very large number to avoid going below the limit when giving a
15189 * bonus. */
15190 if (minscore > limit)
15191 return SCORE_MAXMAX;
15192 return minscore;
15193}
15194
15195#ifdef FEAT_MBYTE
15196/*
15197 * Multi-byte version of spell_edit_score_limit().
15198 * Keep it in sync with the above!
15199 */
15200 static int
15201spell_edit_score_limit_w(slang, badword, goodword, limit)
15202 slang_T *slang;
15203 char_u *badword;
15204 char_u *goodword;
15205 int limit;
15206{
15207 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15208 int stackidx;
15209 int bi, gi;
15210 int bi2, gi2;
15211 int bc, gc;
15212 int score;
15213 int score_off;
15214 int minscore;
15215 int round;
15216 char_u *p;
15217 int wbadword[MAXWLEN];
15218 int wgoodword[MAXWLEN];
15219
15220 /* Get the characters from the multi-byte strings and put them in an
15221 * int array for easy access. */
15222 bi = 0;
15223 for (p = badword; *p != NUL; )
15224 wbadword[bi++] = mb_cptr2char_adv(&p);
15225 wbadword[bi++] = 0;
15226 gi = 0;
15227 for (p = goodword; *p != NUL; )
15228 wgoodword[gi++] = mb_cptr2char_adv(&p);
15229 wgoodword[gi++] = 0;
15230
15231 /*
15232 * The idea is to go from start to end over the words. So long as
15233 * characters are equal just continue, this always gives the lowest score.
15234 * When there is a difference try several alternatives. Each alternative
15235 * increases "score" for the edit distance. Some of the alternatives are
15236 * pushed unto a stack and tried later, some are tried right away. At the
15237 * end of the word the score for one alternative is known. The lowest
15238 * possible score is stored in "minscore".
15239 */
15240 stackidx = 0;
15241 bi = 0;
15242 gi = 0;
15243 score = 0;
15244 minscore = limit + 1;
15245
15246 for (;;)
15247 {
15248 /* Skip over an equal part, score remains the same. */
15249 for (;;)
15250 {
15251 bc = wbadword[bi];
15252 gc = wgoodword[gi];
15253
15254 if (bc != gc) /* stop at a char that's different */
15255 break;
15256 if (bc == NUL) /* both words end */
15257 {
15258 if (score < minscore)
15259 minscore = score;
15260 goto pop; /* do next alternative */
15261 }
15262 ++bi;
15263 ++gi;
15264 }
15265
15266 if (gc == NUL) /* goodword ends, delete badword chars */
15267 {
15268 do
15269 {
15270 if ((score += SCORE_DEL) >= minscore)
15271 goto pop; /* do next alternative */
15272 } while (wbadword[++bi] != NUL);
15273 minscore = score;
15274 }
15275 else if (bc == NUL) /* badword ends, insert badword chars */
15276 {
15277 do
15278 {
15279 if ((score += SCORE_INS) >= minscore)
15280 goto pop; /* do next alternative */
15281 } while (wgoodword[++gi] != NUL);
15282 minscore = score;
15283 }
15284 else /* both words continue */
15285 {
15286 /* If not close to the limit, perform a change. Only try changes
15287 * that may lead to a lower score than "minscore".
15288 * round 0: try deleting a char from badword
15289 * round 1: try inserting a char in badword */
15290 for (round = 0; round <= 1; ++round)
15291 {
15292 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15293 if (score_off < minscore)
15294 {
15295 if (score_off + SCORE_EDIT_MIN >= minscore)
15296 {
15297 /* Near the limit, rest of the words must match. We
15298 * can check that right now, no need to push an item
15299 * onto the stack. */
15300 bi2 = bi + 1 - round;
15301 gi2 = gi + round;
15302 while (wgoodword[gi2] == wbadword[bi2])
15303 {
15304 if (wgoodword[gi2] == NUL)
15305 {
15306 minscore = score_off;
15307 break;
15308 }
15309 ++bi2;
15310 ++gi2;
15311 }
15312 }
15313 else
15314 {
15315 /* try deleting a character from badword later */
15316 stack[stackidx].badi = bi + 1 - round;
15317 stack[stackidx].goodi = gi + round;
15318 stack[stackidx].score = score_off;
15319 ++stackidx;
15320 }
15321 }
15322 }
15323
15324 if (score + SCORE_SWAP < minscore)
15325 {
15326 /* If swapping two characters makes a match then the
15327 * substitution is more expensive, thus there is no need to
15328 * try both. */
15329 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
15330 {
15331 /* Swap two characters, that is: skip them. */
15332 gi += 2;
15333 bi += 2;
15334 score += SCORE_SWAP;
15335 continue;
15336 }
15337 }
15338
15339 /* Substitute one character for another which is the same
15340 * thing as deleting a character from both goodword and badword.
15341 * Use a better score when there is only a case difference. */
15342 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15343 score += SCORE_ICASE;
15344 else
15345 {
15346 /* For a similar character use SCORE_SIMILAR. */
15347 if (slang != NULL
15348 && slang->sl_has_map
15349 && similar_chars(slang, gc, bc))
15350 score += SCORE_SIMILAR;
15351 else
15352 score += SCORE_SUBST;
15353 }
15354
15355 if (score < minscore)
15356 {
15357 /* Do the substitution. */
15358 ++gi;
15359 ++bi;
15360 continue;
15361 }
15362 }
15363pop:
15364 /*
15365 * Get here to try the next alternative, pop it from the stack.
15366 */
15367 if (stackidx == 0) /* stack is empty, finished */
15368 break;
15369
15370 /* pop an item from the stack */
15371 --stackidx;
15372 gi = stack[stackidx].goodi;
15373 bi = stack[stackidx].badi;
15374 score = stack[stackidx].score;
15375 }
15376
15377 /* When the score goes over "limit" it may actually be much higher.
15378 * Return a very large number to avoid going below the limit when giving a
15379 * bonus. */
15380 if (minscore > limit)
15381 return SCORE_MAXMAX;
15382 return minscore;
15383}
15384#endif
15385
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015386/*
15387 * ":spellinfo"
15388 */
15389/*ARGSUSED*/
15390 void
15391ex_spellinfo(eap)
15392 exarg_T *eap;
15393{
15394 int lpi;
15395 langp_T *lp;
15396 char_u *p;
15397
15398 if (no_spell_checking(curwin))
15399 return;
15400
15401 msg_start();
15402 for (lpi = 0; lpi < curbuf->b_langp.ga_len && !got_int; ++lpi)
15403 {
15404 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
15405 msg_puts((char_u *)"file: ");
15406 msg_puts(lp->lp_slang->sl_fname);
15407 msg_putchar('\n');
15408 p = lp->lp_slang->sl_info;
15409 if (p != NULL)
15410 {
15411 msg_puts(p);
15412 msg_putchar('\n');
15413 }
15414 }
15415 msg_end();
15416}
15417
Bram Moolenaar4770d092006-01-12 23:22:24 +000015418#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
15419#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015420#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +000015421#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
15422#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015423
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015424/*
15425 * ":spelldump"
15426 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015427 void
15428ex_spelldump(eap)
15429 exarg_T *eap;
15430{
15431 buf_T *buf = curbuf;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015432
15433 if (no_spell_checking(curwin))
15434 return;
15435
15436 /* Create a new empty buffer by splitting the window. */
15437 do_cmdline_cmd((char_u *)"new");
15438 if (!bufempty() || !buf_valid(buf))
15439 return;
15440
15441 spell_dump_compl(buf, NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
15442
15443 /* Delete the empty line that we started with. */
15444 if (curbuf->b_ml.ml_line_count > 1)
15445 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
15446
15447 redraw_later(NOT_VALID);
15448}
15449
15450/*
15451 * Go through all possible words and:
15452 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
15453 * "ic" and "dir" are not used.
15454 * 2. When "pat" is not NULL: add matching words to insert mode completion.
15455 */
15456 void
15457spell_dump_compl(buf, pat, ic, dir, dumpflags_arg)
15458 buf_T *buf; /* buffer with spell checking */
15459 char_u *pat; /* leading part of the word */
15460 int ic; /* ignore case */
15461 int *dir; /* direction for adding matches */
15462 int dumpflags_arg; /* DUMPFLAG_* */
15463{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015464 langp_T *lp;
15465 slang_T *slang;
15466 idx_T arridx[MAXWLEN];
15467 int curi[MAXWLEN];
15468 char_u word[MAXWLEN];
15469 int c;
15470 char_u *byts;
15471 idx_T *idxs;
15472 linenr_T lnum = 0;
15473 int round;
15474 int depth;
15475 int n;
15476 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015477 char_u *region_names = NULL; /* region names being used */
15478 int do_region = TRUE; /* dump region names and numbers */
15479 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015480 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015481 int dumpflags = dumpflags_arg;
15482 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015483
Bram Moolenaard0131a82006-03-04 21:46:13 +000015484 /* When ignoring case or when the pattern starts with capital pass this on
15485 * to dump_word(). */
15486 if (pat != NULL)
15487 {
15488 if (ic)
15489 dumpflags |= DUMPFLAG_ICASE;
15490 else
15491 {
15492 n = captype(pat, NULL);
15493 if (n == WF_ONECAP)
15494 dumpflags |= DUMPFLAG_ONECAP;
15495 else if (n == WF_ALLCAP
15496#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015497 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015498#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015499 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +000015500#endif
15501 )
15502 dumpflags |= DUMPFLAG_ALLCAP;
15503 }
15504 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015505
Bram Moolenaar7887d882005-07-01 22:33:52 +000015506 /* Find out if we can support regions: All languages must support the same
15507 * regions or none at all. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015508 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000015509 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015510 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000015511 p = lp->lp_slang->sl_regions;
15512 if (p[0] != 0)
15513 {
15514 if (region_names == NULL) /* first language with regions */
15515 region_names = p;
15516 else if (STRCMP(region_names, p) != 0)
15517 {
15518 do_region = FALSE; /* region names are different */
15519 break;
15520 }
15521 }
15522 }
15523
15524 if (do_region && region_names != NULL)
15525 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015526 if (pat == NULL)
15527 {
15528 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15529 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15530 }
Bram Moolenaar7887d882005-07-01 22:33:52 +000015531 }
15532 else
15533 do_region = FALSE;
15534
15535 /*
15536 * Loop over all files loaded for the entries in 'spelllang'.
15537 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015538 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015539 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015540 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015541 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015542 if (slang->sl_fbyts == NULL) /* reloading failed */
15543 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015544
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015545 if (pat == NULL)
15546 {
15547 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15548 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15549 }
15550
15551 /* When matching with a pattern and there are no prefixes only use
15552 * parts of the tree that match "pat". */
15553 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015554 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015555 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015556 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015557
15558 /* round 1: case-folded tree
15559 * round 2: keep-case tree */
15560 for (round = 1; round <= 2; ++round)
15561 {
15562 if (round == 1)
15563 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015564 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015565 byts = slang->sl_fbyts;
15566 idxs = slang->sl_fidxs;
15567 }
15568 else
15569 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015570 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015571 byts = slang->sl_kbyts;
15572 idxs = slang->sl_kidxs;
15573 }
15574 if (byts == NULL)
15575 continue; /* array is empty */
15576
15577 depth = 0;
15578 arridx[0] = 0;
15579 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015580 while (depth >= 0 && !got_int
15581 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015582 {
15583 if (curi[depth] > byts[arridx[depth]])
15584 {
15585 /* Done all bytes at this node, go up one level. */
15586 --depth;
15587 line_breakcheck();
Bram Moolenaara2031822006-03-07 22:29:51 +000015588 ins_compl_check_keys(50);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015589 }
15590 else
15591 {
15592 /* Do one more byte at this node. */
15593 n = arridx[depth] + curi[depth];
15594 ++curi[depth];
15595 c = byts[n];
15596 if (c == 0)
15597 {
15598 /* End of word, deal with the word.
15599 * Don't use keep-case words in the fold-case tree,
15600 * they will appear in the keep-case tree.
15601 * Only use the word when the region matches. */
15602 flags = (int)idxs[n];
15603 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015604 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000015605 && (do_region
15606 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015607 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015608 & lp->lp_region) != 0))
15609 {
15610 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015611 if (!do_region)
15612 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015613
15614 /* Dump the basic word if there is no prefix or
15615 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015616 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015617 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015618 {
15619 dump_word(slang, word, pat, dir,
15620 dumpflags, flags, lnum);
15621 if (pat == NULL)
15622 ++lnum;
15623 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015624
15625 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015626 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015627 lnum = dump_prefixes(slang, word, pat, dir,
15628 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015629 }
15630 }
15631 else
15632 {
15633 /* Normal char, go one level deeper. */
15634 word[depth++] = c;
15635 arridx[depth] = idxs[n];
15636 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015637
15638 /* Check if this characters matches with the pattern.
15639 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +000015640 * Always ignore case here, dump_word() will check
15641 * proper case later. This isn't exactly right when
15642 * length changes for multi-byte characters with
15643 * ignore case... */
15644 if (depth <= patlen
15645 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015646 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015647 }
15648 }
15649 }
15650 }
15651 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015652}
15653
15654/*
15655 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015656 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015657 */
15658 static void
Bram Moolenaard0131a82006-03-04 21:46:13 +000015659dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015660 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015661 char_u *word;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015662 char_u *pat;
15663 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015664 int dumpflags;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015665 int wordflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015666 linenr_T lnum;
15667{
15668 int keepcap = FALSE;
15669 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015670 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015671 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000015672 char_u badword[MAXWLEN + 10];
15673 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015674 int flags = wordflags;
15675
15676 if (dumpflags & DUMPFLAG_ONECAP)
15677 flags |= WF_ONECAP;
15678 if (dumpflags & DUMPFLAG_ALLCAP)
15679 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015680
Bram Moolenaar4770d092006-01-12 23:22:24 +000015681 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015682 {
15683 /* Need to fix case according to "flags". */
15684 make_case_word(word, cword, flags);
15685 p = cword;
15686 }
15687 else
15688 {
15689 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015690 if ((dumpflags & DUMPFLAG_KEEPCASE)
15691 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015692 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015693 keepcap = TRUE;
15694 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015695 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015696
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015697 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015698 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015699 /* Add flags and regions after a slash. */
15700 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015701 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015702 STRCPY(badword, p);
15703 STRCAT(badword, "/");
15704 if (keepcap)
15705 STRCAT(badword, "=");
15706 if (flags & WF_BANNED)
15707 STRCAT(badword, "!");
15708 else if (flags & WF_RARE)
15709 STRCAT(badword, "?");
15710 if (flags & WF_REGION)
15711 for (i = 0; i < 7; ++i)
15712 if (flags & (0x10000 << i))
15713 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
15714 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015715 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015716
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015717 if (dumpflags & DUMPFLAG_COUNT)
15718 {
15719 hashitem_T *hi;
15720
15721 /* Include the word count for ":spelldump!". */
15722 hi = hash_find(&slang->sl_wordcount, tw);
15723 if (!HASHITEM_EMPTY(hi))
15724 {
15725 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15726 tw, HI2WC(hi)->wc_count);
15727 p = IObuff;
15728 }
15729 }
15730
15731 ml_append(lnum, p, (colnr_T)0, FALSE);
15732 }
Bram Moolenaard0131a82006-03-04 21:46:13 +000015733 else if (((dumpflags & DUMPFLAG_ICASE)
15734 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
15735 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015736 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaare8c3a142006-08-29 14:30:35 +000015737 p_ic, NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015738 /* if dir was BACKWARD then honor it just once */
15739 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015740}
15741
15742/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000015743 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15744 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015745 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015746 * Return the updated line number.
15747 */
15748 static linenr_T
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015749dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015750 slang_T *slang;
15751 char_u *word; /* case-folded word */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015752 char_u *pat;
15753 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015754 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015755 int flags; /* flags with prefix ID */
15756 linenr_T startlnum;
15757{
15758 idx_T arridx[MAXWLEN];
15759 int curi[MAXWLEN];
15760 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015761 char_u word_up[MAXWLEN];
15762 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015763 int c;
15764 char_u *byts;
15765 idx_T *idxs;
15766 linenr_T lnum = startlnum;
15767 int depth;
15768 int n;
15769 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015770 int i;
15771
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015772 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015773 * upper-case letter in word_up[]. */
15774 c = PTR2CHAR(word);
15775 if (SPELL_TOUPPER(c) != c)
15776 {
15777 onecap_copy(word, word_up, TRUE);
15778 has_word_up = TRUE;
15779 }
15780
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015781 byts = slang->sl_pbyts;
15782 idxs = slang->sl_pidxs;
15783 if (byts != NULL) /* array not is empty */
15784 {
15785 /*
15786 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015787 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015788 */
15789 depth = 0;
15790 arridx[0] = 0;
15791 curi[0] = 1;
15792 while (depth >= 0 && !got_int)
15793 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015794 n = arridx[depth];
15795 len = byts[n];
15796 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015797 {
15798 /* Done all bytes at this node, go up one level. */
15799 --depth;
15800 line_breakcheck();
15801 }
15802 else
15803 {
15804 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015805 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015806 ++curi[depth];
15807 c = byts[n];
15808 if (c == 0)
15809 {
15810 /* End of prefix, find out how many IDs there are. */
15811 for (i = 1; i < len; ++i)
15812 if (byts[n + i] != 0)
15813 break;
15814 curi[depth] += i - 1;
15815
Bram Moolenaar53805d12005-08-01 07:08:33 +000015816 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15817 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015818 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015819 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015820 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015821 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015822 : flags, lnum);
15823 if (lnum != 0)
15824 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015825 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000015826
15827 /* Check for prefix that matches the word when the
15828 * first letter is upper-case, but only if the prefix has
15829 * a condition. */
15830 if (has_word_up)
15831 {
15832 c = valid_word_prefix(i, n, flags, word_up, slang,
15833 TRUE);
15834 if (c != 0)
15835 {
15836 vim_strncpy(prefix + depth, word_up,
15837 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015838 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015839 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015840 : flags, lnum);
15841 if (lnum != 0)
15842 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +000015843 }
15844 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015845 }
15846 else
15847 {
15848 /* Normal char, go one level deeper. */
15849 prefix[depth++] = c;
15850 arridx[depth] = idxs[n];
15851 curi[depth] = 1;
15852 }
15853 }
15854 }
15855 }
15856
15857 return lnum;
15858}
15859
Bram Moolenaar95529562005-08-25 21:21:38 +000015860/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015861 * Move "p" to the end of word "start".
15862 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000015863 */
15864 char_u *
15865spell_to_word_end(start, buf)
15866 char_u *start;
15867 buf_T *buf;
15868{
15869 char_u *p = start;
15870
15871 while (*p != NUL && spell_iswordp(p, buf))
15872 mb_ptr_adv(p);
15873 return p;
15874}
15875
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015876#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015877/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015878 * For Insert mode completion CTRL-X s:
15879 * Find start of the word in front of column "startcol".
15880 * We don't check if it is badly spelled, with completion we can only change
15881 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015882 * Returns the column number of the word.
15883 */
15884 int
15885spell_word_start(startcol)
15886 int startcol;
15887{
15888 char_u *line;
15889 char_u *p;
15890 int col = 0;
15891
Bram Moolenaar95529562005-08-25 21:21:38 +000015892 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015893 return startcol;
15894
15895 /* Find a word character before "startcol". */
15896 line = ml_get_curline();
15897 for (p = line + startcol; p > line; )
15898 {
15899 mb_ptr_back(line, p);
15900 if (spell_iswordp_nmw(p))
15901 break;
15902 }
15903
15904 /* Go back to start of the word. */
15905 while (p > line)
15906 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015907 col = (int)(p - line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015908 mb_ptr_back(line, p);
15909 if (!spell_iswordp(p, curbuf))
15910 break;
15911 col = 0;
15912 }
15913
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015914 return col;
15915}
15916
15917/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000015918 * Need to check for 'spellcapcheck' now, the word is removed before
15919 * expand_spelling() is called. Therefore the ugly global variable.
15920 */
15921static int spell_expand_need_cap;
15922
15923 void
15924spell_expand_check_cap(col)
15925 colnr_T col;
15926{
15927 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
15928}
15929
15930/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015931 * Get list of spelling suggestions.
15932 * Used for Insert mode completion CTRL-X ?.
15933 * Returns the number of matches. The matches are in "matchp[]", array of
15934 * allocated strings.
15935 */
15936/*ARGSUSED*/
15937 int
15938expand_spelling(lnum, col, pat, matchp)
15939 linenr_T lnum;
15940 int col;
15941 char_u *pat;
15942 char_u ***matchp;
15943{
15944 garray_T ga;
15945
Bram Moolenaar4770d092006-01-12 23:22:24 +000015946 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015947 *matchp = ga.ga_data;
15948 return ga.ga_len;
15949}
15950#endif
15951
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000015952#endif /* FEAT_SPELL */