blob: 567c6cd363b81b06cb56ceced9dd3b4c9cbc2047 [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
756static int spell_iswordp_w __ARGS((int *p, buf_T *buf));
757#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000758static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000759
760/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000761 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000762 */
763typedef enum
764{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000765 STATE_START = 0, /* At start of node check for NUL bytes (goodword
766 * ends); if badword ends there is a match, otherwise
767 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000768 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000769 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000770 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
771 STATE_PLAIN, /* Use each byte of the node. */
772 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000773 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000774 STATE_INS, /* Insert a byte in the bad word. */
775 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000776 STATE_UNSWAP, /* Undo swap two characters. */
777 STATE_SWAP3, /* Swap two characters over three. */
778 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000779 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000780 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000781 STATE_REP_INI, /* Prepare for using REP items. */
782 STATE_REP, /* Use matching REP items from the .aff file. */
783 STATE_REP_UNDO, /* Undo a REP item replacement. */
784 STATE_FINAL /* End of this node. */
785} state_T;
786
787/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000788 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000789 */
790typedef struct trystate_S
791{
Bram Moolenaarea424162005-06-16 21:51:00 +0000792 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000793 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000794 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000795 short ts_curi; /* index in list of child nodes */
796 char_u ts_fidx; /* index in fword[], case-folded bad word */
797 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
798 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000799 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000800 * PFD_PREFIXTREE or PFD_NOPREFIX */
801 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000802#ifdef FEAT_MBYTE
803 char_u ts_tcharlen; /* number of bytes in tword character */
804 char_u ts_tcharidx; /* current byte index in tword character */
805 char_u ts_isdiff; /* DIFF_ values */
806 char_u ts_fcharstart; /* index in fword where badword char started */
807#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000808 char_u ts_prewordlen; /* length of word in "preword[]" */
809 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000810 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000811 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000812 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000813 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000814 char_u ts_delidx; /* index in fword for char that was deleted,
815 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000816} trystate_T;
817
Bram Moolenaarea424162005-06-16 21:51:00 +0000818/* values for ts_isdiff */
819#define DIFF_NONE 0 /* no different byte (yet) */
820#define DIFF_YES 1 /* different byte found */
821#define DIFF_INSERT 2 /* inserting character */
822
Bram Moolenaard12a1322005-08-21 22:08:24 +0000823/* values for ts_flags */
824#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
825#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000826#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000827
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000828/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000829#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000830#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000831#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000832
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000833/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000834#define FIND_FOLDWORD 0 /* find word case-folded */
835#define FIND_KEEPWORD 1 /* find keep-case word */
836#define FIND_PREFIX 2 /* find word after prefix */
837#define FIND_COMPOUND 3 /* find case-folded compound word */
838#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000839
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000840static slang_T *slang_alloc __ARGS((char_u *lang));
841static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000842static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000843static void slang_clear_sug __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000844static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000845static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000846static 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 +0000847static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000848static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000849static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000850static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000851static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000852static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000853static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000854static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000855static 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 +0000856static int get2c __ARGS((FILE *fd));
857static int get3c __ARGS((FILE *fd));
858static int get4c __ARGS((FILE *fd));
859static time_t get8c __ARGS((FILE *fd));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000860static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000861static char_u *read_string __ARGS((FILE *fd, int cnt));
862static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
863static int read_charflags_section __ARGS((FILE *fd));
864static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000865static int read_rep_section __ARGS((FILE *fd, garray_T *gap, short *first));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000866static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000867static int read_words_section __ARGS((FILE *fd, slang_T *lp, int len));
868static void count_common_word __ARGS((slang_T *lp, char_u *word, int len, int count));
869static int score_wordcount_adj __ARGS((slang_T *slang, int score, char_u *word, int split));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000870static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
871static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000872static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000873static int init_syl_tab __ARGS((slang_T *slang));
874static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000875static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
876static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000877#ifdef FEAT_MBYTE
878static int *mb_str2wide __ARGS((char_u *s));
879#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000880static int spell_read_tree __ARGS((FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt));
881static 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 +0000882static void clear_midword __ARGS((buf_T *buf));
883static void use_midword __ARGS((slang_T *lp, buf_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000884static int find_region __ARGS((char_u *rp, char_u *region));
885static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000886static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000887static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000888static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000889static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000890static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000891static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar66fa2712006-01-22 23:22:22 +0000892static 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 +0000893#ifdef FEAT_EVAL
894static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
895#endif
896static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000897static void spell_suggest_intern __ARGS((suginfo_T *su, int interactive));
898static void suggest_load_files __ARGS((void));
899static void tree_count_words __ARGS((char_u *byts, idx_T *idxs));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000900static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000901static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000902static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000903static void suggest_try_special __ARGS((suginfo_T *su));
904static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000905static void suggest_trie_walk __ARGS((suginfo_T *su, langp_T *lp, char_u *fword, int soundfold));
906static void go_deeper __ARGS((trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000907#ifdef FEAT_MBYTE
908static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
909#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000910static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000911static void score_comp_sal __ARGS((suginfo_T *su));
912static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000913static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000914static void suggest_try_soundalike_prep __ARGS((void));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000915static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000916static void suggest_try_soundalike_finish __ARGS((void));
917static void add_sound_suggest __ARGS((suginfo_T *su, char_u *goodword, int score, langp_T *lp));
918static int soundfold_find __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000919static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000920static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000921static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000922static 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));
923static void check_suggestions __ARGS((suginfo_T *su, garray_T *gap));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000924static void add_banned __ARGS((suginfo_T *su, char_u *word));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000925static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000926static void rescore_one __ARGS((suginfo_T *su, suggest_T *stp));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000927static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000928static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
929static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
930static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000931#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000932static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000933#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000934static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000935static int spell_edit_score __ARGS((slang_T *slang, char_u *badword, char_u *goodword));
936static int spell_edit_score_limit __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
937#ifdef FEAT_MBYTE
938static int spell_edit_score_limit_w __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
939#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +0000940static void dump_word __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum));
941static 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 +0000942static buf_T *open_spellbuf __ARGS((void));
943static void close_spellbuf __ARGS((buf_T *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000944
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000945/*
946 * Use our own character-case definitions, because the current locale may
947 * differ from what the .spl file uses.
948 * These must not be called with negative number!
949 */
950#ifndef FEAT_MBYTE
951/* Non-multi-byte implementation. */
952# define SPELL_TOFOLD(c) ((c) < 256 ? spelltab.st_fold[c] : (c))
953# define SPELL_TOUPPER(c) ((c) < 256 ? spelltab.st_upper[c] : (c))
954# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
955#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000956# if defined(HAVE_WCHAR_H)
957# include <wchar.h> /* for towupper() and towlower() */
958# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000959/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
960 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
961 * the "w" library function for characters above 255 if available. */
962# ifdef HAVE_TOWLOWER
963# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
964 : (c) < 256 ? spelltab.st_fold[c] : towlower(c))
965# else
966# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
967 : (c) < 256 ? spelltab.st_fold[c] : (c))
968# endif
969
970# ifdef HAVE_TOWUPPER
971# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
972 : (c) < 256 ? spelltab.st_upper[c] : towupper(c))
973# else
974# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
975 : (c) < 256 ? spelltab.st_upper[c] : (c))
976# endif
977
978# ifdef HAVE_ISWUPPER
979# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
980 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
981# else
982# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000983 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000984# endif
985#endif
986
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000987
988static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000989static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000990static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000991static char *e_affname = N_("Affix name too long in %s line %d: %s");
992static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
993static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000994static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000995
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000996/* Remember what "z?" replaced. */
997static char_u *repl_from = NULL;
998static char_u *repl_to = NULL;
999
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001000/*
1001 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001002 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001003 * "*attrp" is set to the highlight index for a badly spelled word. For a
1004 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001005 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001006 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001007 * "capcol" is used to check for a Capitalised word after the end of a
1008 * sentence. If it's zero then perform the check. Return the column where to
1009 * check next, or -1 when no sentence end was found. If it's NULL then don't
1010 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001011 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001012 * Returns the length of the word in bytes, also when it's OK, so that the
1013 * caller can skip over the word.
1014 */
1015 int
Bram Moolenaar4770d092006-01-12 23:22:24 +00001016spell_check(wp, ptr, attrp, capcol, docount)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001017 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001018 char_u *ptr;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001019 hlf_T *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001020 int *capcol; /* column to check for Capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001021 int docount; /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001022{
1023 matchinf_T mi; /* Most things are put in "mi" so that it can
1024 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001025 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001026 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001027 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001028 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001029 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001030
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001031 /* A word never starts at a space or a control character. Return quickly
1032 * then, skipping over the character. */
1033 if (*ptr <= ' ')
1034 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001035
1036 /* Return here when loading language files failed. */
1037 if (wp->w_buffer->b_langp.ga_len == 0)
1038 return 1;
1039
Bram Moolenaar5195e452005-08-19 20:32:47 +00001040 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001041
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001042 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +00001043 * 0X99FF. But always do check spelling to find "3GPP" and "11
1044 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001045 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +00001046 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001047 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
1048 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001049 else
1050 mi.mi_end = skipdigits(ptr);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001051 nrlen = (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001052 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001053
Bram Moolenaar0c405862005-06-22 22:26:26 +00001054 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001055 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +00001056 mi.mi_fend = ptr;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001057 if (spell_iswordp(mi.mi_fend, wp->w_buffer))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001058 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001059 do
Bram Moolenaar51485f02005-06-04 21:55:20 +00001060 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001061 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001062 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp->w_buffer));
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001063
1064 if (capcol != NULL && *capcol == 0 && wp->w_buffer->b_cap_prog != NULL)
1065 {
1066 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00001067 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001068 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001069 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001070 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001071 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001072 if (capcol != NULL)
1073 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001074
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001075 /* We always use the characters up to the next non-word character,
1076 * also for bad words. */
1077 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001078
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001079 /* Check caps type later. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001080 mi.mi_buf = wp->w_buffer;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001081
Bram Moolenaar5195e452005-08-19 20:32:47 +00001082 /* case-fold the word with one non-word character, so that we can check
1083 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001084 if (*mi.mi_fend != NUL)
1085 mb_ptr_adv(mi.mi_fend);
1086
1087 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
1088 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001089 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001090
1091 /* The word is bad unless we recognize it. */
1092 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +00001093 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001094
1095 /*
1096 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001097 * We check them all, because a word may be matched longer in another
1098 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001099 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001100 for (lpi = 0; lpi < wp->w_buffer->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001101 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001102 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, lpi);
1103
1104 /* If reloading fails the language is still in the list but everything
1105 * has been cleared. */
1106 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
1107 continue;
1108
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001109 /* Check for a matching word in case-folded words. */
1110 find_word(&mi, FIND_FOLDWORD);
1111
1112 /* Check for a matching word in keep-case words. */
1113 find_word(&mi, FIND_KEEPWORD);
1114
1115 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001116 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +00001117
1118 /* For a NOBREAK language, may want to use a word without a following
1119 * word as a backup. */
1120 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
1121 && mi.mi_result2 != SP_BAD)
1122 {
1123 mi.mi_result = mi.mi_result2;
1124 mi.mi_end = mi.mi_end2;
1125 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001126
1127 /* Count the word in the first language where it's found to be OK. */
1128 if (count_word && mi.mi_result == SP_OK)
1129 {
1130 count_common_word(mi.mi_lp->lp_slang, ptr,
1131 (int)(mi.mi_end - ptr), 1);
1132 count_word = FALSE;
1133 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001134 }
1135
1136 if (mi.mi_result != SP_OK)
1137 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001138 /* If we found a number skip over it. Allows for "42nd". Do flag
1139 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001140 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001141 {
1142 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1143 return nrlen;
1144 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001145
1146 /* When we are at a non-word character there is no error, just
1147 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001148 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001149 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001150 if (capcol != NULL && wp->w_buffer->b_cap_prog != NULL)
1151 {
1152 regmatch_T regmatch;
1153
1154 /* Check for end of sentence. */
1155 regmatch.regprog = wp->w_buffer->b_cap_prog;
1156 regmatch.rm_ic = FALSE;
1157 if (vim_regexec(&regmatch, ptr, 0))
1158 *capcol = (int)(regmatch.endp[0] - ptr);
1159 }
1160
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001161#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001162 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001163 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001164#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001165 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001166 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001167 else if (mi.mi_end == ptr)
1168 /* Always include at least one character. Required for when there
1169 * is a mixup in "midword". */
1170 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +00001171 else if (mi.mi_result == SP_BAD
1172 && LANGP_ENTRY(wp->w_buffer->b_langp, 0)->lp_slang->sl_nobreak)
1173 {
1174 char_u *p, *fp;
1175 int save_result = mi.mi_result;
1176
1177 /* First language in 'spelllang' is NOBREAK. Find first position
1178 * at which any word would be valid. */
1179 mi.mi_lp = LANGP_ENTRY(wp->w_buffer->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001180 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +00001181 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001182 p = mi.mi_word;
1183 fp = mi.mi_fword;
1184 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +00001185 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001186 mb_ptr_adv(p);
1187 mb_ptr_adv(fp);
1188 if (p >= mi.mi_end)
1189 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001190 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001191 find_word(&mi, FIND_COMPOUND);
1192 if (mi.mi_result != SP_BAD)
1193 {
1194 mi.mi_end = p;
1195 break;
1196 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001197 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001198 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001199 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001200 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001201
1202 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001203 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001204 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001205 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001206 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001207 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001208 }
1209
Bram Moolenaar5195e452005-08-19 20:32:47 +00001210 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1211 {
1212 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001213 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001214 return wrongcaplen;
1215 }
1216
Bram Moolenaar51485f02005-06-04 21:55:20 +00001217 return (int)(mi.mi_end - ptr);
1218}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001219
Bram Moolenaar51485f02005-06-04 21:55:20 +00001220/*
1221 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001222 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1223 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1224 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1225 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001226 *
1227 * For a match mip->mi_result is updated.
1228 */
1229 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001230find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001231 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001232 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001233{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001234 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001235 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001236 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001237 int endidxcnt = 0;
1238 int len;
1239 int wlen = 0;
1240 int flen;
1241 int c;
1242 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001243 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001244#ifdef FEAT_MBYTE
1245 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001246#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001247 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001248 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001249 slang_T *slang = mip->mi_lp->lp_slang;
1250 unsigned flags;
1251 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001252 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001253 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001254 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001255 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001256
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001257 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001258 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001259 /* Check for word with matching case in keep-case tree. */
1260 ptr = mip->mi_word;
1261 flen = 9999; /* no case folding, always enough bytes */
1262 byts = slang->sl_kbyts;
1263 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001264
1265 if (mode == FIND_KEEPCOMPOUND)
1266 /* Skip over the previously found word(s). */
1267 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001268 }
1269 else
1270 {
1271 /* Check for case-folded in case-folded tree. */
1272 ptr = mip->mi_fword;
1273 flen = mip->mi_fwordlen; /* available case-folded bytes */
1274 byts = slang->sl_fbyts;
1275 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001276
1277 if (mode == FIND_PREFIX)
1278 {
1279 /* Skip over the prefix. */
1280 wlen = mip->mi_prefixlen;
1281 flen -= mip->mi_prefixlen;
1282 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001283 else if (mode == FIND_COMPOUND)
1284 {
1285 /* Skip over the previously found word(s). */
1286 wlen = mip->mi_compoff;
1287 flen -= mip->mi_compoff;
1288 }
1289
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001290 }
1291
Bram Moolenaar51485f02005-06-04 21:55:20 +00001292 if (byts == NULL)
1293 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001294
Bram Moolenaar51485f02005-06-04 21:55:20 +00001295 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001296 * Repeat advancing in the tree until:
1297 * - there is a byte that doesn't match,
1298 * - we reach the end of the tree,
1299 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001300 */
1301 for (;;)
1302 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001303 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001304 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001305
1306 len = byts[arridx++];
1307
1308 /* If the first possible byte is a zero the word could end here.
1309 * Remember this index, we first check for the longest word. */
1310 if (byts[arridx] == 0)
1311 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001312 if (endidxcnt == MAXWLEN)
1313 {
1314 /* Must be a corrupted spell file. */
1315 EMSG(_(e_format));
1316 return;
1317 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001318 endlen[endidxcnt] = wlen;
1319 endidx[endidxcnt++] = arridx++;
1320 --len;
1321
1322 /* Skip over the zeros, there can be several flag/region
1323 * combinations. */
1324 while (len > 0 && byts[arridx] == 0)
1325 {
1326 ++arridx;
1327 --len;
1328 }
1329 if (len == 0)
1330 break; /* no children, word must end here */
1331 }
1332
1333 /* Stop looking at end of the line. */
1334 if (ptr[wlen] == NUL)
1335 break;
1336
1337 /* Perform a binary search in the list of accepted bytes. */
1338 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001339 if (c == TAB) /* <Tab> is handled like <Space> */
1340 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001341 lo = arridx;
1342 hi = arridx + len - 1;
1343 while (lo < hi)
1344 {
1345 m = (lo + hi) / 2;
1346 if (byts[m] > c)
1347 hi = m - 1;
1348 else if (byts[m] < c)
1349 lo = m + 1;
1350 else
1351 {
1352 lo = hi = m;
1353 break;
1354 }
1355 }
1356
1357 /* Stop if there is no matching byte. */
1358 if (hi < lo || byts[lo] != c)
1359 break;
1360
1361 /* Continue at the child (if there is one). */
1362 arridx = idxs[lo];
1363 ++wlen;
1364 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001365
1366 /* One space in the good word may stand for several spaces in the
1367 * checked word. */
1368 if (c == ' ')
1369 {
1370 for (;;)
1371 {
1372 if (flen <= 0 && *mip->mi_fend != NUL)
1373 flen = fold_more(mip);
1374 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1375 break;
1376 ++wlen;
1377 --flen;
1378 }
1379 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001380 }
1381
1382 /*
1383 * Verify that one of the possible endings is valid. Try the longest
1384 * first.
1385 */
1386 while (endidxcnt > 0)
1387 {
1388 --endidxcnt;
1389 arridx = endidx[endidxcnt];
1390 wlen = endlen[endidxcnt];
1391
1392#ifdef FEAT_MBYTE
1393 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1394 continue; /* not at first byte of character */
1395#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001396 if (spell_iswordp(ptr + wlen, mip->mi_buf))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001397 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001398 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001399 continue; /* next char is a word character */
1400 word_ends = FALSE;
1401 }
1402 else
1403 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001404 /* The prefix flag is before compound flags. Once a valid prefix flag
1405 * has been found we try compound flags. */
1406 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001407
1408#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001409 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001410 {
1411 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001412 * when folding case. This can be slow, take a shortcut when the
1413 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001414 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001415 if (STRNCMP(ptr, p, wlen) != 0)
1416 {
1417 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1418 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001419 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001420 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001421 }
1422#endif
1423
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001424 /* Check flags and region. For FIND_PREFIX check the condition and
1425 * prefix ID.
1426 * Repeat this if there are more flags/region alternatives until there
1427 * is a match. */
1428 res = SP_BAD;
1429 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1430 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001431 {
1432 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001433
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001434 /* For the fold-case tree check that the case of the checked word
1435 * matches with what the word in the tree requires.
1436 * For keep-case tree the case is always right. For prefixes we
1437 * don't bother to check. */
1438 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001439 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001440 if (mip->mi_cend != mip->mi_word + wlen)
1441 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001442 /* mi_capflags was set for a different word length, need
1443 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001444 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001445 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001446 }
1447
Bram Moolenaar0c405862005-06-22 22:26:26 +00001448 if (mip->mi_capflags == WF_KEEPCAP
1449 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001450 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001451 }
1452
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001453 /* When mode is FIND_PREFIX the word must support the prefix:
1454 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001455 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001456 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001457 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001458 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001459 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001460 mip->mi_word + mip->mi_cprefixlen, slang,
1461 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001462 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001463 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001464
1465 /* Use the WF_RARE flag for a rare prefix. */
1466 if (c & WF_RAREPFX)
1467 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001468 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001469 }
1470
Bram Moolenaar78622822005-08-23 21:00:13 +00001471 if (slang->sl_nobreak)
1472 {
1473 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1474 && (flags & WF_BANNED) == 0)
1475 {
1476 /* NOBREAK: found a valid following word. That's all we
1477 * need to know, so return. */
1478 mip->mi_result = SP_OK;
1479 break;
1480 }
1481 }
1482
1483 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1484 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001485 {
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00001486 /* If there is no compound flag or the word is shorter than
Bram Moolenaar5195e452005-08-19 20:32:47 +00001487 * COMPOUNDMIN reject it quickly.
1488 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001489 * that's too short... Myspell compatibility requires this
1490 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001491 if (((unsigned)flags >> 24) == 0
1492 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001493 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001494#ifdef FEAT_MBYTE
1495 /* For multi-byte chars check character length against
1496 * COMPOUNDMIN. */
1497 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001498 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001499 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1500 wlen - mip->mi_compoff) < slang->sl_compminlen)
1501 continue;
1502#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001503
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001504 /* Limit the number of compound words to COMPOUNDWORDMAX if no
Bram Moolenaare52325c2005-08-22 22:54:29 +00001505 * maximum for syllables is specified. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001506 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
1507 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +00001508 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001509 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001510
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001511 /* Don't allow compounding on a side where an affix was added,
1512 * unless COMPOUNDPERMITFLAG was used. */
1513 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
1514 continue;
1515 if (!word_ends && (flags & WF_NOCOMPAFT))
1516 continue;
1517
Bram Moolenaard12a1322005-08-21 22:08:24 +00001518 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001519 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001520 ? slang->sl_compstartflags
1521 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001522 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001523 continue;
1524
Bram Moolenaare52325c2005-08-22 22:54:29 +00001525 if (mode == FIND_COMPOUND)
1526 {
1527 int capflags;
1528
1529 /* Need to check the caps type of the appended compound
1530 * word. */
1531#ifdef FEAT_MBYTE
1532 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1533 mip->mi_compoff) != 0)
1534 {
1535 /* case folding may have changed the length */
1536 p = mip->mi_word;
1537 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1538 mb_ptr_adv(p);
1539 }
1540 else
1541#endif
1542 p = mip->mi_word + mip->mi_compoff;
1543 capflags = captype(p, mip->mi_word + wlen);
1544 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1545 && (flags & WF_FIXCAP) != 0))
1546 continue;
1547
1548 if (capflags != WF_ALLCAP)
1549 {
1550 /* When the character before the word is a word
1551 * character we do not accept a Onecap word. We do
1552 * accept a no-caps word, even when the dictionary
1553 * word specifies ONECAP. */
1554 mb_ptr_back(mip->mi_word, p);
1555 if (spell_iswordp_nmw(p)
1556 ? capflags == WF_ONECAP
1557 : (flags & WF_ONECAP) != 0
1558 && capflags != WF_ONECAP)
1559 continue;
1560 }
1561 }
1562
Bram Moolenaar5195e452005-08-19 20:32:47 +00001563 /* If the word ends the sequence of compound flags of the
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001564 * words must match with one of the COMPOUNDRULE items and
Bram Moolenaar5195e452005-08-19 20:32:47 +00001565 * the number of syllables must not be too large. */
1566 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1567 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1568 if (word_ends)
1569 {
1570 char_u fword[MAXWLEN];
1571
1572 if (slang->sl_compsylmax < MAXWLEN)
1573 {
1574 /* "fword" is only needed for checking syllables. */
1575 if (ptr == mip->mi_word)
1576 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1577 else
1578 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1579 }
1580 if (!can_compound(slang, fword, mip->mi_compflags))
1581 continue;
1582 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001583 }
1584
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001585 /* Check NEEDCOMPOUND: can't use word without compounding. */
1586 else if (flags & WF_NEEDCOMP)
1587 continue;
1588
Bram Moolenaar78622822005-08-23 21:00:13 +00001589 nobreak_result = SP_OK;
1590
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001591 if (!word_ends)
1592 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001593 int save_result = mip->mi_result;
1594 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001595 langp_T *save_lp = mip->mi_lp;
1596 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001597
1598 /* Check that a valid word follows. If there is one and we
1599 * are compounding, it will set "mi_result", thus we are
1600 * always finished here. For NOBREAK we only check that a
1601 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001602 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001603 if (slang->sl_nobreak)
1604 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001605
1606 /* Find following word in case-folded tree. */
1607 mip->mi_compoff = endlen[endidxcnt];
1608#ifdef FEAT_MBYTE
1609 if (has_mbyte && mode == FIND_KEEPWORD)
1610 {
1611 /* Compute byte length in case-folded word from "wlen":
1612 * byte length in keep-case word. Length may change when
1613 * folding case. This can be slow, take a shortcut when
1614 * the case-folded word is equal to the keep-case word. */
1615 p = mip->mi_fword;
1616 if (STRNCMP(ptr, p, wlen) != 0)
1617 {
1618 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1619 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001620 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001621 }
1622 }
1623#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001624 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001625 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001626 if (flags & WF_COMPROOT)
1627 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001628
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001629 /* For NOBREAK we need to try all NOBREAK languages, at least
1630 * to find the ".add" file(s). */
1631 for (lpi = 0; lpi < mip->mi_buf->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001632 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001633 if (slang->sl_nobreak)
1634 {
1635 mip->mi_lp = LANGP_ENTRY(mip->mi_buf->b_langp, lpi);
1636 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1637 || !mip->mi_lp->lp_slang->sl_nobreak)
1638 continue;
1639 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001640
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001641 find_word(mip, FIND_COMPOUND);
1642
1643 /* When NOBREAK any word that matches is OK. Otherwise we
1644 * need to find the longest match, thus try with keep-case
1645 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001646 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1647 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001648 /* Find following word in keep-case tree. */
1649 mip->mi_compoff = wlen;
1650 find_word(mip, FIND_KEEPCOMPOUND);
1651
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001652#if 0 /* Disabled, a prefix must not appear halfway a compound word,
1653 unless the COMPOUNDPERMITFLAG is used and then it can't be a
1654 postponed prefix. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001655 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1656 {
1657 /* Check for following word with prefix. */
1658 mip->mi_compoff = c;
1659 find_prefix(mip, FIND_COMPOUND);
1660 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001661#endif
Bram Moolenaar78622822005-08-23 21:00:13 +00001662 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001663
1664 if (!slang->sl_nobreak)
1665 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001666 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001667 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001668 if (flags & WF_COMPROOT)
1669 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001670 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001671
Bram Moolenaar78622822005-08-23 21:00:13 +00001672 if (slang->sl_nobreak)
1673 {
1674 nobreak_result = mip->mi_result;
1675 mip->mi_result = save_result;
1676 mip->mi_end = save_end;
1677 }
1678 else
1679 {
1680 if (mip->mi_result == SP_OK)
1681 break;
1682 continue;
1683 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001684 }
1685
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001686 if (flags & WF_BANNED)
1687 res = SP_BANNED;
1688 else if (flags & WF_REGION)
1689 {
1690 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001691 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001692 res = SP_OK;
1693 else
1694 res = SP_LOCAL;
1695 }
1696 else if (flags & WF_RARE)
1697 res = SP_RARE;
1698 else
1699 res = SP_OK;
1700
Bram Moolenaar78622822005-08-23 21:00:13 +00001701 /* Always use the longest match and the best result. For NOBREAK
1702 * we separately keep the longest match without a following good
1703 * word as a fall-back. */
1704 if (nobreak_result == SP_BAD)
1705 {
1706 if (mip->mi_result2 > res)
1707 {
1708 mip->mi_result2 = res;
1709 mip->mi_end2 = mip->mi_word + wlen;
1710 }
1711 else if (mip->mi_result2 == res
1712 && mip->mi_end2 < mip->mi_word + wlen)
1713 mip->mi_end2 = mip->mi_word + wlen;
1714 }
1715 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001716 {
1717 mip->mi_result = res;
1718 mip->mi_end = mip->mi_word + wlen;
1719 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001720 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001721 mip->mi_end = mip->mi_word + wlen;
1722
Bram Moolenaar78622822005-08-23 21:00:13 +00001723 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001724 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001725 }
1726
Bram Moolenaar78622822005-08-23 21:00:13 +00001727 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001728 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001729 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001730}
1731
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001732/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001733 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1734 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001735 */
1736 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001737can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001738 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001739 char_u *word;
1740 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001741{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001742 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001743#ifdef FEAT_MBYTE
1744 char_u uflags[MAXWLEN * 2];
1745 int i;
1746#endif
1747 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001748
1749 if (slang->sl_compprog == NULL)
1750 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001751#ifdef FEAT_MBYTE
1752 if (enc_utf8)
1753 {
1754 /* Need to convert the single byte flags to utf8 characters. */
1755 p = uflags;
1756 for (i = 0; flags[i] != NUL; ++i)
1757 p += mb_char2bytes(flags[i], p);
1758 *p = NUL;
1759 p = uflags;
1760 }
1761 else
1762#endif
1763 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001764 regmatch.regprog = slang->sl_compprog;
1765 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001766 if (!vim_regexec(&regmatch, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001767 return FALSE;
1768
Bram Moolenaare52325c2005-08-22 22:54:29 +00001769 /* Count the number of syllables. This may be slow, do it last. If there
1770 * are too many syllables AND the number of compound words is above
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001771 * COMPOUNDWORDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001772 if (slang->sl_compsylmax < MAXWLEN
1773 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001774 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001775 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001776}
1777
1778/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001779 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1780 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001781 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001782 */
1783 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001784valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001785 int totprefcnt; /* nr of prefix IDs */
1786 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001787 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001788 char_u *word;
1789 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001790 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001791{
1792 int prefcnt;
1793 int pidx;
1794 regprog_T *rp;
1795 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001796 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001797
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001798 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001799 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1800 {
1801 pidx = slang->sl_pidxs[arridx + prefcnt];
1802
1803 /* Check the prefix ID. */
1804 if (prefid != (pidx & 0xff))
1805 continue;
1806
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001807 /* Check if the prefix doesn't combine and the word already has a
1808 * suffix. */
1809 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1810 continue;
1811
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001812 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001813 * stored in the two bytes above the prefix ID byte. */
1814 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001815 if (rp != NULL)
1816 {
1817 regmatch.regprog = rp;
1818 regmatch.rm_ic = FALSE;
1819 if (!vim_regexec(&regmatch, word, 0))
1820 continue;
1821 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001822 else if (cond_req)
1823 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001824
Bram Moolenaar53805d12005-08-01 07:08:33 +00001825 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001826 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001827 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001828 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001829}
1830
1831/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001832 * Check if the word at "mip->mi_word" has a matching prefix.
1833 * If it does, then check the following word.
1834 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001835 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1836 * prefix in a compound word.
1837 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001838 * For a match mip->mi_result is updated.
1839 */
1840 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001841find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001842 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001843 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001844{
1845 idx_T arridx = 0;
1846 int len;
1847 int wlen = 0;
1848 int flen;
1849 int c;
1850 char_u *ptr;
1851 idx_T lo, hi, m;
1852 slang_T *slang = mip->mi_lp->lp_slang;
1853 char_u *byts;
1854 idx_T *idxs;
1855
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001856 byts = slang->sl_pbyts;
1857 if (byts == NULL)
1858 return; /* array is empty */
1859
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001860 /* We use the case-folded word here, since prefixes are always
1861 * case-folded. */
1862 ptr = mip->mi_fword;
1863 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001864 if (mode == FIND_COMPOUND)
1865 {
1866 /* Skip over the previously found word(s). */
1867 ptr += mip->mi_compoff;
1868 flen -= mip->mi_compoff;
1869 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001870 idxs = slang->sl_pidxs;
1871
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001872 /*
1873 * Repeat advancing in the tree until:
1874 * - there is a byte that doesn't match,
1875 * - we reach the end of the tree,
1876 * - or we reach the end of the line.
1877 */
1878 for (;;)
1879 {
1880 if (flen == 0 && *mip->mi_fend != NUL)
1881 flen = fold_more(mip);
1882
1883 len = byts[arridx++];
1884
1885 /* If the first possible byte is a zero the prefix could end here.
1886 * Check if the following word matches and supports the prefix. */
1887 if (byts[arridx] == 0)
1888 {
1889 /* There can be several prefixes with different conditions. We
1890 * try them all, since we don't know which one will give the
1891 * longest match. The word is the same each time, pass the list
1892 * of possible prefixes to find_word(). */
1893 mip->mi_prefarridx = arridx;
1894 mip->mi_prefcnt = len;
1895 while (len > 0 && byts[arridx] == 0)
1896 {
1897 ++arridx;
1898 --len;
1899 }
1900 mip->mi_prefcnt -= len;
1901
1902 /* Find the word that comes after the prefix. */
1903 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001904 if (mode == FIND_COMPOUND)
1905 /* Skip over the previously found word(s). */
1906 mip->mi_prefixlen += mip->mi_compoff;
1907
Bram Moolenaar53805d12005-08-01 07:08:33 +00001908#ifdef FEAT_MBYTE
1909 if (has_mbyte)
1910 {
1911 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001912 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
1913 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00001914 }
1915 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00001916 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001917#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001918 find_word(mip, FIND_PREFIX);
1919
1920
1921 if (len == 0)
1922 break; /* no children, word must end here */
1923 }
1924
1925 /* Stop looking at end of the line. */
1926 if (ptr[wlen] == NUL)
1927 break;
1928
1929 /* Perform a binary search in the list of accepted bytes. */
1930 c = ptr[wlen];
1931 lo = arridx;
1932 hi = arridx + len - 1;
1933 while (lo < hi)
1934 {
1935 m = (lo + hi) / 2;
1936 if (byts[m] > c)
1937 hi = m - 1;
1938 else if (byts[m] < c)
1939 lo = m + 1;
1940 else
1941 {
1942 lo = hi = m;
1943 break;
1944 }
1945 }
1946
1947 /* Stop if there is no matching byte. */
1948 if (hi < lo || byts[lo] != c)
1949 break;
1950
1951 /* Continue at the child (if there is one). */
1952 arridx = idxs[lo];
1953 ++wlen;
1954 --flen;
1955 }
1956}
1957
1958/*
1959 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001960 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001961 * Return the length of the folded chars in bytes.
1962 */
1963 static int
1964fold_more(mip)
1965 matchinf_T *mip;
1966{
1967 int flen;
1968 char_u *p;
1969
1970 p = mip->mi_fend;
1971 do
1972 {
1973 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00001974 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_buf));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001975
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001976 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001977 if (*mip->mi_fend != NUL)
1978 mb_ptr_adv(mip->mi_fend);
1979
1980 (void)spell_casefold(p, (int)(mip->mi_fend - p),
1981 mip->mi_fword + mip->mi_fwordlen,
1982 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001983 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001984 mip->mi_fwordlen += flen;
1985 return flen;
1986}
1987
1988/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001989 * Check case flags for a word. Return TRUE if the word has the requested
1990 * case.
1991 */
1992 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001993spell_valid_case(wordflags, treeflags)
1994 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001995 int treeflags; /* flags for the word in the spell tree */
1996{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00001997 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001998 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001999 && ((treeflags & WF_ONECAP) == 0
2000 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002001}
2002
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002003/*
2004 * Return TRUE if spell checking is not enabled.
2005 */
2006 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00002007no_spell_checking(wp)
2008 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002009{
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002010 if (!wp->w_p_spell || *wp->w_buffer->b_p_spl == NUL
2011 || wp->w_buffer->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002012 {
2013 EMSG(_("E756: Spell checking is not enabled"));
2014 return TRUE;
2015 }
2016 return FALSE;
2017}
Bram Moolenaar51485f02005-06-04 21:55:20 +00002018
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002019/*
2020 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002021 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
2022 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002023 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
2024 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00002025 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002026 */
2027 int
Bram Moolenaar95529562005-08-25 21:21:38 +00002028spell_move_to(wp, dir, allwords, curline, attrp)
2029 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002030 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002031 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002032 int curline;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002033 hlf_T *attrp; /* return: attributes of bad word or NULL
2034 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002035{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002036 linenr_T lnum;
2037 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002038 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002039 char_u *line;
2040 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002041 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002042 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002043 int len;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002044# ifdef FEAT_SYN_HL
Bram Moolenaar95529562005-08-25 21:21:38 +00002045 int has_syntax = syntax_present(wp->w_buffer);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002046# endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00002047 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002048 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002049 char_u *buf = NULL;
2050 int buflen = 0;
2051 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002052 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002053 int found_one = FALSE;
2054 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002055
Bram Moolenaar95529562005-08-25 21:21:38 +00002056 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00002057 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002058
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002059 /*
2060 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00002061 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002062 *
2063 * When searching backwards, we continue in the line to find the last
2064 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00002065 *
2066 * We concatenate the start of the next line, so that wrapped words work
2067 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
2068 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002069 */
Bram Moolenaar95529562005-08-25 21:21:38 +00002070 lnum = wp->w_cursor.lnum;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002071 clearpos(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002072
2073 while (!got_int)
2074 {
Bram Moolenaar95529562005-08-25 21:21:38 +00002075 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002076
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002077 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002078 if (buflen < len + MAXWLEN + 2)
2079 {
2080 vim_free(buf);
2081 buflen = len + MAXWLEN + 2;
2082 buf = alloc(buflen);
2083 if (buf == NULL)
2084 break;
2085 }
2086
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002087 /* In first line check first word for Capital. */
2088 if (lnum == 1)
2089 capcol = 0;
2090
2091 /* For checking first word with a capital skip white space. */
2092 if (capcol == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002093 capcol = (int)(skipwhite(line) - line);
2094 else if (curline && wp == curwin)
2095 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002096 /* For spellbadword(): check if first word needs a capital. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002097 col = (int)(skipwhite(line) - line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002098 if (check_need_cap(lnum, col))
2099 capcol = col;
2100
2101 /* Need to get the line again, may have looked at the previous
2102 * one. */
2103 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2104 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002105
Bram Moolenaar0c405862005-06-22 22:26:26 +00002106 /* Copy the line into "buf" and append the start of the next line if
2107 * possible. */
2108 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00002109 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00002110 spell_cat_line(buf + STRLEN(buf),
2111 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002112
2113 p = buf + skip;
2114 endp = buf + len;
2115 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002116 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002117 /* When searching backward don't search after the cursor. Unless
2118 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002119 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00002120 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002121 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00002122 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002123 break;
2124
2125 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002126 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002127 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002128
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002129 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002130 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002131 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002132 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002133 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002134 /* When searching forward only accept a bad word after
2135 * the cursor. */
2136 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002137 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00002138 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002139 && (wrapped
2140 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00002141 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002142 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002143 {
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002144# ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00002145 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002146 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002147 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002148 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00002149 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00002150 if (!can_spell)
2151 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002152 }
2153 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002154#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002155 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002156
Bram Moolenaar51485f02005-06-04 21:55:20 +00002157 if (can_spell)
2158 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00002159 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002160 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002161 found_pos.col = (int)(p - buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002162#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00002163 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002164#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002165 if (dir == FORWARD)
2166 {
2167 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00002168 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002169 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002170 if (attrp != NULL)
2171 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002172 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002173 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002174 else if (curline)
2175 /* Insert mode completion: put cursor after
2176 * the bad word. */
2177 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002178 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002179 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002180 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00002181 else
2182 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002183 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002184 }
2185
Bram Moolenaar51485f02005-06-04 21:55:20 +00002186 /* advance to character after the word */
2187 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002188 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002189 }
2190
Bram Moolenaar5195e452005-08-19 20:32:47 +00002191 if (dir == BACKWARD && found_pos.lnum != 0)
2192 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002193 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00002194 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002195 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002196 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002197 }
2198
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002199 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002200 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002201
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002202 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002203 if (dir == BACKWARD)
2204 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002205 /* If we are back at the starting line and searched it again there
2206 * is no match, give up. */
2207 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002208 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002209
2210 if (lnum > 1)
2211 --lnum;
2212 else if (!p_ws)
2213 break; /* at first line and 'nowrapscan' */
2214 else
2215 {
2216 /* Wrap around to the end of the buffer. May search the
2217 * starting line again and accept the last match. */
2218 lnum = wp->w_buffer->b_ml.ml_line_count;
2219 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002220 if (!shortmess(SHM_SEARCH))
2221 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002222 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002223 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002224 }
2225 else
2226 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002227 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2228 ++lnum;
2229 else if (!p_ws)
2230 break; /* at first line and 'nowrapscan' */
2231 else
2232 {
2233 /* Wrap around to the start of the buffer. May search the
2234 * starting line again and accept the first match. */
2235 lnum = 1;
2236 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002237 if (!shortmess(SHM_SEARCH))
2238 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002239 }
2240
2241 /* If we are back at the starting line and there is no match then
2242 * give up. */
2243 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002244 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002245
2246 /* Skip the characters at the start of the next line that were
2247 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002248 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002249 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002250 else
2251 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002252
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002253 /* Capcol skips over the inserted space. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002254 --capcol;
2255
2256 /* But after empty line check first word in next line */
2257 if (*skipwhite(line) == NUL)
2258 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002259 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002260
2261 line_breakcheck();
2262 }
2263
Bram Moolenaar0c405862005-06-22 22:26:26 +00002264 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002265 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002266}
2267
2268/*
2269 * For spell checking: concatenate the start of the following line "line" into
2270 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00002271 * Keep the blanks at the start of the next line, this is used in win_line()
2272 * to skip those bytes if the word was OK.
Bram Moolenaar0c405862005-06-22 22:26:26 +00002273 */
2274 void
2275spell_cat_line(buf, line, maxlen)
2276 char_u *buf;
2277 char_u *line;
2278 int maxlen;
2279{
2280 char_u *p;
2281 int n;
2282
2283 p = skipwhite(line);
2284 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2285 p = skipwhite(p + 1);
2286
2287 if (*p != NUL)
2288 {
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00002289 /* Only worth concatenating if there is something else than spaces to
2290 * concatenate. */
2291 n = (int)(p - line) + 1;
2292 if (n < maxlen - 1)
2293 {
2294 vim_memset(buf, ' ', n);
2295 vim_strncpy(buf + n, p, maxlen - 1 - n);
2296 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00002297 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002298}
2299
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002300/*
2301 * Structure used for the cookie argument of do_in_runtimepath().
2302 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002303typedef struct spelload_S
2304{
2305 char_u sl_lang[MAXWLEN + 1]; /* language name */
2306 slang_T *sl_slang; /* resulting slang_T struct */
2307 int sl_nobreak; /* NOBREAK language found */
2308} spelload_T;
2309
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002310/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002311 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002312 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002313 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002314 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002315spell_load_lang(lang)
2316 char_u *lang;
2317{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002318 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002319 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002320 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002321#ifdef FEAT_AUTOCMD
2322 int round;
2323#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002324
Bram Moolenaarb765d632005-06-07 21:00:02 +00002325 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002326 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002327 STRCPY(sl.sl_lang, lang);
2328 sl.sl_slang = NULL;
2329 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002330
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002331#ifdef FEAT_AUTOCMD
2332 /* We may retry when no spell file is found for the language, an
2333 * autocommand may load it then. */
2334 for (round = 1; round <= 2; ++round)
2335#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002336 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002337 /*
2338 * Find the first spell file for "lang" in 'runtimepath' and load it.
2339 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002340 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002341 "spell/%s.%s.spl", lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002342 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002343
2344 if (r == FAIL && *sl.sl_lang != NUL)
2345 {
2346 /* Try loading the ASCII version. */
2347 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2348 "spell/%s.ascii.spl", lang);
2349 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
2350
2351#ifdef FEAT_AUTOCMD
2352 if (r == FAIL && *sl.sl_lang != NUL && round == 1
2353 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
2354 curbuf->b_fname, FALSE, curbuf))
2355 continue;
2356 break;
2357#endif
2358 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002359#ifdef FEAT_AUTOCMD
2360 break;
2361#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002362 }
2363
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002364 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002365 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002366 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2367 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002368 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002369 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002370 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002371 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002372 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002373 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002374 }
2375}
2376
2377/*
2378 * Return the encoding used for spell checking: Use 'encoding', except that we
2379 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2380 */
2381 static char_u *
2382spell_enc()
2383{
2384
2385#ifdef FEAT_MBYTE
2386 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2387 return p_enc;
2388#endif
2389 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002390}
2391
2392/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002393 * Get the name of the .spl file for the internal wordlist into
2394 * "fname[MAXPATHL]".
2395 */
2396 static void
2397int_wordlist_spl(fname)
2398 char_u *fname;
2399{
2400 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2401 int_wordlist, spell_enc());
2402}
2403
2404/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002405 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002406 * Caller must fill "sl_next".
2407 */
2408 static slang_T *
2409slang_alloc(lang)
2410 char_u *lang;
2411{
2412 slang_T *lp;
2413
Bram Moolenaar51485f02005-06-04 21:55:20 +00002414 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002415 if (lp != NULL)
2416 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002417 if (lang != NULL)
2418 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002419 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002420 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002421 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002422 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002423 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002424 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002425
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002426 return lp;
2427}
2428
2429/*
2430 * Free the contents of an slang_T and the structure itself.
2431 */
2432 static void
2433slang_free(lp)
2434 slang_T *lp;
2435{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002436 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002437 vim_free(lp->sl_fname);
2438 slang_clear(lp);
2439 vim_free(lp);
2440}
2441
2442/*
2443 * Clear an slang_T so that the file can be reloaded.
2444 */
2445 static void
2446slang_clear(lp)
2447 slang_T *lp;
2448{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002449 garray_T *gap;
2450 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002451 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002452 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002453 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002454
Bram Moolenaar51485f02005-06-04 21:55:20 +00002455 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002456 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002457 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002458 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002459 vim_free(lp->sl_pbyts);
2460 lp->sl_pbyts = NULL;
2461
Bram Moolenaar51485f02005-06-04 21:55:20 +00002462 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002463 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002464 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002465 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002466 vim_free(lp->sl_pidxs);
2467 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002468
Bram Moolenaar4770d092006-01-12 23:22:24 +00002469 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002470 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002471 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2472 while (gap->ga_len > 0)
2473 {
2474 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2475 vim_free(ftp->ft_from);
2476 vim_free(ftp->ft_to);
2477 }
2478 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002479 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002480
2481 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002482 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002483 {
2484 /* "ga_len" is set to 1 without adding an item for latin1 */
2485 if (gap->ga_data != NULL)
2486 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2487 for (i = 0; i < gap->ga_len; ++i)
2488 vim_free(((int **)gap->ga_data)[i]);
2489 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002490 else
2491 /* SAL items: free salitem_T items */
2492 while (gap->ga_len > 0)
2493 {
2494 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2495 vim_free(smp->sm_lead);
2496 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2497 vim_free(smp->sm_to);
2498#ifdef FEAT_MBYTE
2499 vim_free(smp->sm_lead_w);
2500 vim_free(smp->sm_oneof_w);
2501 vim_free(smp->sm_to_w);
2502#endif
2503 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002504 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002505
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002506 for (i = 0; i < lp->sl_prefixcnt; ++i)
2507 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002508 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002509 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002510 lp->sl_prefprog = NULL;
2511
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002512 vim_free(lp->sl_info);
2513 lp->sl_info = NULL;
2514
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002515 vim_free(lp->sl_midword);
2516 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002517
Bram Moolenaar5195e452005-08-19 20:32:47 +00002518 vim_free(lp->sl_compprog);
2519 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002520 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002521 lp->sl_compprog = NULL;
2522 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002523 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002524
2525 vim_free(lp->sl_syllable);
2526 lp->sl_syllable = NULL;
2527 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002528
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002529 ga_clear_strings(&lp->sl_comppat);
2530
Bram Moolenaar4770d092006-01-12 23:22:24 +00002531 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2532 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002533
Bram Moolenaar4770d092006-01-12 23:22:24 +00002534#ifdef FEAT_MBYTE
2535 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002536#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002537
Bram Moolenaar4770d092006-01-12 23:22:24 +00002538 /* Clear info from .sug file. */
2539 slang_clear_sug(lp);
2540
Bram Moolenaar5195e452005-08-19 20:32:47 +00002541 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002542 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002543 lp->sl_compsylmax = MAXWLEN;
2544 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002545}
2546
2547/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002548 * Clear the info from the .sug file in "lp".
2549 */
2550 static void
2551slang_clear_sug(lp)
2552 slang_T *lp;
2553{
2554 vim_free(lp->sl_sbyts);
2555 lp->sl_sbyts = NULL;
2556 vim_free(lp->sl_sidxs);
2557 lp->sl_sidxs = NULL;
2558 close_spellbuf(lp->sl_sugbuf);
2559 lp->sl_sugbuf = NULL;
2560 lp->sl_sugloaded = FALSE;
2561 lp->sl_sugtime = 0;
2562}
2563
2564/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002565 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002566 * Invoked through do_in_runtimepath().
2567 */
2568 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002569spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002570 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002571 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002572{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002573 spelload_T *slp = (spelload_T *)cookie;
2574 slang_T *slang;
2575
2576 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2577 if (slang != NULL)
2578 {
2579 /* When a previously loaded file has NOBREAK also use it for the
2580 * ".add" files. */
2581 if (slp->sl_nobreak && slang->sl_add)
2582 slang->sl_nobreak = TRUE;
2583 else if (slang->sl_nobreak)
2584 slp->sl_nobreak = TRUE;
2585
2586 slp->sl_slang = slang;
2587 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002588}
2589
2590/*
2591 * Load one spell file and store the info into a slang_T.
2592 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00002593 * This is invoked in three ways:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002594 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2595 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2596 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2597 * points to the existing slang_T.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002598 * - Just after writing a .spl file; it's read back to produce the .sug file.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002599 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
2600 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002601 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002602 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002603 static slang_T *
2604spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002605 char_u *fname;
2606 char_u *lang;
2607 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002608 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002609{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002610 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002611 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002612 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002613 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002614 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002615 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002616 char_u *save_sourcing_name = sourcing_name;
2617 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002618 slang_T *lp = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002619 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002620 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002621
Bram Moolenaarb765d632005-06-07 21:00:02 +00002622 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002623 if (fd == NULL)
2624 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002625 if (!silent)
2626 EMSG2(_(e_notopen), fname);
2627 else if (p_verbose > 2)
2628 {
2629 verbose_enter();
2630 smsg((char_u *)e_notopen, fname);
2631 verbose_leave();
2632 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002633 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002634 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002635 if (p_verbose > 2)
2636 {
2637 verbose_enter();
2638 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2639 verbose_leave();
2640 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002641
Bram Moolenaarb765d632005-06-07 21:00:02 +00002642 if (old_lp == NULL)
2643 {
2644 lp = slang_alloc(lang);
2645 if (lp == NULL)
2646 goto endFAIL;
2647
2648 /* Remember the file name, used to reload the file when it's updated. */
2649 lp->sl_fname = vim_strsave(fname);
2650 if (lp->sl_fname == NULL)
2651 goto endFAIL;
2652
2653 /* Check for .add.spl. */
2654 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2655 }
2656 else
2657 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002658
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002659 /* Set sourcing_name, so that error messages mention the file name. */
2660 sourcing_name = fname;
2661 sourcing_lnum = 0;
2662
Bram Moolenaar4770d092006-01-12 23:22:24 +00002663 /*
2664 * <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002665 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002666 for (i = 0; i < VIMSPELLMAGICL; ++i)
2667 buf[i] = getc(fd); /* <fileID> */
2668 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2669 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002670 EMSG(_("E757: This does not look like a spell file"));
2671 goto endFAIL;
2672 }
2673 c = getc(fd); /* <versionnr> */
2674 if (c < VIMSPELLVERSION)
2675 {
2676 EMSG(_("E771: Old spell file, needs to be updated"));
2677 goto endFAIL;
2678 }
2679 else if (c > VIMSPELLVERSION)
2680 {
2681 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002682 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002683 }
2684
Bram Moolenaar5195e452005-08-19 20:32:47 +00002685
2686 /*
2687 * <SECTIONS>: <section> ... <sectionend>
2688 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2689 */
2690 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002691 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002692 n = getc(fd); /* <sectionID> or <sectionend> */
2693 if (n == SN_END)
2694 break;
2695 c = getc(fd); /* <sectionflags> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002696 len = get4c(fd); /* <sectionlen> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002697 if (len < 0)
2698 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002699
Bram Moolenaar5195e452005-08-19 20:32:47 +00002700 res = 0;
2701 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002702 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002703 case SN_INFO:
2704 lp->sl_info = read_string(fd, len); /* <infotext> */
2705 if (lp->sl_info == NULL)
2706 goto endFAIL;
2707 break;
2708
Bram Moolenaar5195e452005-08-19 20:32:47 +00002709 case SN_REGION:
2710 res = read_region_section(fd, lp, len);
2711 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002712
Bram Moolenaar5195e452005-08-19 20:32:47 +00002713 case SN_CHARFLAGS:
2714 res = read_charflags_section(fd);
2715 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002716
Bram Moolenaar5195e452005-08-19 20:32:47 +00002717 case SN_MIDWORD:
2718 lp->sl_midword = read_string(fd, len); /* <midword> */
2719 if (lp->sl_midword == NULL)
2720 goto endFAIL;
2721 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002722
Bram Moolenaar5195e452005-08-19 20:32:47 +00002723 case SN_PREFCOND:
2724 res = read_prefcond_section(fd, lp);
2725 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002726
Bram Moolenaar5195e452005-08-19 20:32:47 +00002727 case SN_REP:
Bram Moolenaar4770d092006-01-12 23:22:24 +00002728 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
2729 break;
2730
2731 case SN_REPSAL:
2732 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002733 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002734
Bram Moolenaar5195e452005-08-19 20:32:47 +00002735 case SN_SAL:
2736 res = read_sal_section(fd, lp);
2737 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002738
Bram Moolenaar5195e452005-08-19 20:32:47 +00002739 case SN_SOFO:
2740 res = read_sofo_section(fd, lp);
2741 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002742
Bram Moolenaar5195e452005-08-19 20:32:47 +00002743 case SN_MAP:
2744 p = read_string(fd, len); /* <mapstr> */
2745 if (p == NULL)
2746 goto endFAIL;
2747 set_map_str(lp, p);
2748 vim_free(p);
2749 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002750
Bram Moolenaar4770d092006-01-12 23:22:24 +00002751 case SN_WORDS:
2752 res = read_words_section(fd, lp, len);
2753 break;
2754
2755 case SN_SUGFILE:
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002756 lp->sl_sugtime = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002757 break;
2758
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002759 case SN_NOSPLITSUGS:
2760 lp->sl_nosplitsugs = TRUE; /* <timestamp> */
2761 break;
2762
Bram Moolenaar5195e452005-08-19 20:32:47 +00002763 case SN_COMPOUND:
2764 res = read_compound(fd, lp, len);
2765 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002766
Bram Moolenaar78622822005-08-23 21:00:13 +00002767 case SN_NOBREAK:
2768 lp->sl_nobreak = TRUE;
2769 break;
2770
Bram Moolenaar5195e452005-08-19 20:32:47 +00002771 case SN_SYLLABLE:
2772 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2773 if (lp->sl_syllable == NULL)
2774 goto endFAIL;
2775 if (init_syl_tab(lp) == FAIL)
2776 goto endFAIL;
2777 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002778
Bram Moolenaar5195e452005-08-19 20:32:47 +00002779 default:
2780 /* Unsupported section. When it's required give an error
2781 * message. When it's not required skip the contents. */
2782 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002783 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002784 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002785 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002786 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002787 while (--len >= 0)
2788 if (getc(fd) < 0)
2789 goto truncerr;
2790 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002791 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002792someerror:
Bram Moolenaar5195e452005-08-19 20:32:47 +00002793 if (res == SP_FORMERROR)
2794 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002795 EMSG(_(e_format));
2796 goto endFAIL;
2797 }
2798 if (res == SP_TRUNCERROR)
2799 {
2800truncerr:
2801 EMSG(_(e_spell_trunc));
2802 goto endFAIL;
2803 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002804 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002805 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002806 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002807
Bram Moolenaar4770d092006-01-12 23:22:24 +00002808 /* <LWORDTREE> */
2809 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
2810 if (res != 0)
2811 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002812
Bram Moolenaar4770d092006-01-12 23:22:24 +00002813 /* <KWORDTREE> */
2814 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
2815 if (res != 0)
2816 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002817
Bram Moolenaar4770d092006-01-12 23:22:24 +00002818 /* <PREFIXTREE> */
2819 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
2820 lp->sl_prefixcnt);
2821 if (res != 0)
2822 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002823
Bram Moolenaarb765d632005-06-07 21:00:02 +00002824 /* For a new file link it in the list of spell files. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002825 if (old_lp == NULL && lang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002826 {
2827 lp->sl_next = first_lang;
2828 first_lang = lp;
2829 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002830
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002831 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002832
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002833endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002834 if (lang != NULL)
2835 /* truncating the name signals the error to spell_load_lang() */
2836 *lang = NUL;
2837 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002838 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002839 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002840
2841endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002842 if (fd != NULL)
2843 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002844 sourcing_name = save_sourcing_name;
2845 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002846
2847 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002848}
2849
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002850/*
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002851 * Read 2 bytes from "fd" and turn them into an int, MSB first.
2852 */
2853 static int
2854get2c(fd)
2855 FILE *fd;
2856{
2857 long n;
2858
2859 n = getc(fd);
2860 n = (n << 8) + getc(fd);
2861 return n;
2862}
2863
2864/*
2865 * Read 3 bytes from "fd" and turn them into an int, MSB first.
2866 */
2867 static int
2868get3c(fd)
2869 FILE *fd;
2870{
2871 long n;
2872
2873 n = getc(fd);
2874 n = (n << 8) + getc(fd);
2875 n = (n << 8) + getc(fd);
2876 return n;
2877}
2878
2879/*
2880 * Read 4 bytes from "fd" and turn them into an int, MSB first.
2881 */
2882 static int
2883get4c(fd)
2884 FILE *fd;
2885{
2886 long n;
2887
2888 n = getc(fd);
2889 n = (n << 8) + getc(fd);
2890 n = (n << 8) + getc(fd);
2891 n = (n << 8) + getc(fd);
2892 return n;
2893}
2894
2895/*
2896 * Read 8 bytes from "fd" and turn them into a time_t, MSB first.
2897 */
2898 static time_t
2899get8c(fd)
2900 FILE *fd;
2901{
2902 time_t n = 0;
2903 int i;
2904
2905 for (i = 0; i < 8; ++i)
2906 n = (n << 8) + getc(fd);
2907 return n;
2908}
2909
2910/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002911 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002912 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002913 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002914 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2915 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002916 */
2917 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002918read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002919 FILE *fd;
2920 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002921 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002922{
2923 int cnt = 0;
2924 int i;
2925 char_u *str;
2926
2927 /* read the length bytes, MSB first */
2928 for (i = 0; i < cnt_bytes; ++i)
2929 cnt = (cnt << 8) + getc(fd);
2930 if (cnt < 0)
2931 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002932 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002933 return NULL;
2934 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002935 *cntp = cnt;
2936 if (cnt == 0)
2937 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002938
Bram Moolenaar5195e452005-08-19 20:32:47 +00002939 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002940 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002941 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002942 return str;
2943}
2944
Bram Moolenaar7887d882005-07-01 22:33:52 +00002945/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002946 * Read a string of length "cnt" from "fd" into allocated memory.
2947 * Returns NULL when out of memory.
2948 */
2949 static char_u *
2950read_string(fd, cnt)
2951 FILE *fd;
2952 int cnt;
2953{
2954 char_u *str;
2955 int i;
2956
2957 /* allocate memory */
2958 str = alloc((unsigned)cnt + 1);
2959 if (str != NULL)
2960 {
2961 /* Read the string. Doesn't check for truncated file. */
2962 for (i = 0; i < cnt; ++i)
2963 str[i] = getc(fd);
2964 str[i] = NUL;
2965 }
2966 return str;
2967}
2968
2969/*
2970 * Read SN_REGION: <regionname> ...
2971 * Return SP_*ERROR flags.
2972 */
2973 static int
2974read_region_section(fd, lp, len)
2975 FILE *fd;
2976 slang_T *lp;
2977 int len;
2978{
2979 int i;
2980
2981 if (len > 16)
2982 return SP_FORMERROR;
2983 for (i = 0; i < len; ++i)
2984 lp->sl_regions[i] = getc(fd); /* <regionname> */
2985 lp->sl_regions[len] = NUL;
2986 return 0;
2987}
2988
2989/*
2990 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2991 * <folcharslen> <folchars>
2992 * Return SP_*ERROR flags.
2993 */
2994 static int
2995read_charflags_section(fd)
2996 FILE *fd;
2997{
2998 char_u *flags;
2999 char_u *fol;
3000 int flagslen, follen;
3001
3002 /* <charflagslen> <charflags> */
3003 flags = read_cnt_string(fd, 1, &flagslen);
3004 if (flagslen < 0)
3005 return flagslen;
3006
3007 /* <folcharslen> <folchars> */
3008 fol = read_cnt_string(fd, 2, &follen);
3009 if (follen < 0)
3010 {
3011 vim_free(flags);
3012 return follen;
3013 }
3014
3015 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
3016 if (flags != NULL && fol != NULL)
3017 set_spell_charflags(flags, flagslen, fol);
3018
3019 vim_free(flags);
3020 vim_free(fol);
3021
3022 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
3023 if ((flags == NULL) != (fol == NULL))
3024 return SP_FORMERROR;
3025 return 0;
3026}
3027
3028/*
3029 * Read SN_PREFCOND section.
3030 * Return SP_*ERROR flags.
3031 */
3032 static int
3033read_prefcond_section(fd, lp)
3034 FILE *fd;
3035 slang_T *lp;
3036{
3037 int cnt;
3038 int i;
3039 int n;
3040 char_u *p;
3041 char_u buf[MAXWLEN + 1];
3042
3043 /* <prefcondcnt> <prefcond> ... */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003044 cnt = get2c(fd); /* <prefcondcnt> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003045 if (cnt <= 0)
3046 return SP_FORMERROR;
3047
3048 lp->sl_prefprog = (regprog_T **)alloc_clear(
3049 (unsigned)sizeof(regprog_T *) * cnt);
3050 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003051 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003052 lp->sl_prefixcnt = cnt;
3053
3054 for (i = 0; i < cnt; ++i)
3055 {
3056 /* <prefcond> : <condlen> <condstr> */
3057 n = getc(fd); /* <condlen> */
3058 if (n < 0 || n >= MAXWLEN)
3059 return SP_FORMERROR;
3060
3061 /* When <condlen> is zero we have an empty condition. Otherwise
3062 * compile the regexp program used to check for the condition. */
3063 if (n > 0)
3064 {
3065 buf[0] = '^'; /* always match at one position only */
3066 p = buf + 1;
3067 while (n-- > 0)
3068 *p++ = getc(fd); /* <condstr> */
3069 *p = NUL;
3070 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
3071 }
3072 }
3073 return 0;
3074}
3075
3076/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003077 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
Bram Moolenaar5195e452005-08-19 20:32:47 +00003078 * Return SP_*ERROR flags.
3079 */
3080 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00003081read_rep_section(fd, gap, first)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003082 FILE *fd;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003083 garray_T *gap;
3084 short *first;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003085{
3086 int cnt;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003087 fromto_T *ftp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003088 int i;
3089
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003090 cnt = get2c(fd); /* <repcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003091 if (cnt < 0)
3092 return SP_TRUNCERROR;
3093
Bram Moolenaar5195e452005-08-19 20:32:47 +00003094 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003095 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003096
3097 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3098 for (; gap->ga_len < cnt; ++gap->ga_len)
3099 {
3100 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
3101 ftp->ft_from = read_cnt_string(fd, 1, &i);
3102 if (i < 0)
3103 return i;
3104 if (i == 0)
3105 return SP_FORMERROR;
3106 ftp->ft_to = read_cnt_string(fd, 1, &i);
3107 if (i <= 0)
3108 {
3109 vim_free(ftp->ft_from);
3110 if (i < 0)
3111 return i;
3112 return SP_FORMERROR;
3113 }
3114 }
3115
3116 /* Fill the first-index table. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003117 for (i = 0; i < 256; ++i)
3118 first[i] = -1;
3119 for (i = 0; i < gap->ga_len; ++i)
3120 {
3121 ftp = &((fromto_T *)gap->ga_data)[i];
3122 if (first[*ftp->ft_from] == -1)
3123 first[*ftp->ft_from] = i;
3124 }
3125 return 0;
3126}
3127
3128/*
3129 * Read SN_SAL section: <salflags> <salcount> <sal> ...
3130 * Return SP_*ERROR flags.
3131 */
3132 static int
3133read_sal_section(fd, slang)
3134 FILE *fd;
3135 slang_T *slang;
3136{
3137 int i;
3138 int cnt;
3139 garray_T *gap;
3140 salitem_T *smp;
3141 int ccnt;
3142 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003143 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003144
3145 slang->sl_sofo = FALSE;
3146
3147 i = getc(fd); /* <salflags> */
3148 if (i & SAL_F0LLOWUP)
3149 slang->sl_followup = TRUE;
3150 if (i & SAL_COLLAPSE)
3151 slang->sl_collapse = TRUE;
3152 if (i & SAL_REM_ACCENTS)
3153 slang->sl_rem_accents = TRUE;
3154
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003155 cnt = get2c(fd); /* <salcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003156 if (cnt < 0)
3157 return SP_TRUNCERROR;
3158
3159 gap = &slang->sl_sal;
3160 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003161 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003162 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003163
3164 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3165 for (; gap->ga_len < cnt; ++gap->ga_len)
3166 {
3167 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3168 ccnt = getc(fd); /* <salfromlen> */
3169 if (ccnt < 0)
3170 return SP_TRUNCERROR;
3171 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003172 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003173 smp->sm_lead = p;
3174
3175 /* Read up to the first special char into sm_lead. */
3176 for (i = 0; i < ccnt; ++i)
3177 {
3178 c = getc(fd); /* <salfrom> */
3179 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
3180 break;
3181 *p++ = c;
3182 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003183 smp->sm_leadlen = (int)(p - smp->sm_lead);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003184 *p++ = NUL;
3185
3186 /* Put (abc) chars in sm_oneof, if any. */
3187 if (c == '(')
3188 {
3189 smp->sm_oneof = p;
3190 for (++i; i < ccnt; ++i)
3191 {
3192 c = getc(fd); /* <salfrom> */
3193 if (c == ')')
3194 break;
3195 *p++ = c;
3196 }
3197 *p++ = NUL;
3198 if (++i < ccnt)
3199 c = getc(fd);
3200 }
3201 else
3202 smp->sm_oneof = NULL;
3203
3204 /* Any following chars go in sm_rules. */
3205 smp->sm_rules = p;
3206 if (i < ccnt)
3207 /* store the char we got while checking for end of sm_lead */
3208 *p++ = c;
3209 for (++i; i < ccnt; ++i)
3210 *p++ = getc(fd); /* <salfrom> */
3211 *p++ = NUL;
3212
3213 /* <saltolen> <salto> */
3214 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
3215 if (ccnt < 0)
3216 {
3217 vim_free(smp->sm_lead);
3218 return ccnt;
3219 }
3220
3221#ifdef FEAT_MBYTE
3222 if (has_mbyte)
3223 {
3224 /* convert the multi-byte strings to wide char strings */
3225 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3226 smp->sm_leadlen = mb_charlen(smp->sm_lead);
3227 if (smp->sm_oneof == NULL)
3228 smp->sm_oneof_w = NULL;
3229 else
3230 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
3231 if (smp->sm_to == NULL)
3232 smp->sm_to_w = NULL;
3233 else
3234 smp->sm_to_w = mb_str2wide(smp->sm_to);
3235 if (smp->sm_lead_w == NULL
3236 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
3237 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
3238 {
3239 vim_free(smp->sm_lead);
3240 vim_free(smp->sm_to);
3241 vim_free(smp->sm_lead_w);
3242 vim_free(smp->sm_oneof_w);
3243 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003244 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003245 }
3246 }
3247#endif
3248 }
3249
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003250 if (gap->ga_len > 0)
3251 {
3252 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
3253 * that we need to check the index every time. */
3254 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3255 if ((p = alloc(1)) == NULL)
3256 return SP_OTHERERROR;
3257 p[0] = NUL;
3258 smp->sm_lead = p;
3259 smp->sm_leadlen = 0;
3260 smp->sm_oneof = NULL;
3261 smp->sm_rules = p;
3262 smp->sm_to = NULL;
3263#ifdef FEAT_MBYTE
3264 if (has_mbyte)
3265 {
3266 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3267 smp->sm_leadlen = 0;
3268 smp->sm_oneof_w = NULL;
3269 smp->sm_to_w = NULL;
3270 }
3271#endif
3272 ++gap->ga_len;
3273 }
3274
Bram Moolenaar5195e452005-08-19 20:32:47 +00003275 /* Fill the first-index table. */
3276 set_sal_first(slang);
3277
3278 return 0;
3279}
3280
3281/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003282 * Read SN_WORDS: <word> ...
3283 * Return SP_*ERROR flags.
3284 */
3285 static int
3286read_words_section(fd, lp, len)
3287 FILE *fd;
3288 slang_T *lp;
3289 int len;
3290{
3291 int done = 0;
3292 int i;
3293 char_u word[MAXWLEN];
3294
3295 while (done < len)
3296 {
3297 /* Read one word at a time. */
3298 for (i = 0; ; ++i)
3299 {
3300 word[i] = getc(fd);
3301 if (word[i] == NUL)
3302 break;
3303 if (i == MAXWLEN - 1)
3304 return SP_FORMERROR;
3305 }
3306
3307 /* Init the count to 10. */
3308 count_common_word(lp, word, -1, 10);
3309 done += i + 1;
3310 }
3311 return 0;
3312}
3313
3314/*
3315 * Add a word to the hashtable of common words.
3316 * If it's already there then the counter is increased.
3317 */
3318 static void
3319count_common_word(lp, word, len, count)
3320 slang_T *lp;
3321 char_u *word;
3322 int len; /* word length, -1 for upto NUL */
3323 int count; /* 1 to count once, 10 to init */
3324{
3325 hash_T hash;
3326 hashitem_T *hi;
3327 wordcount_T *wc;
3328 char_u buf[MAXWLEN];
3329 char_u *p;
3330
3331 if (len == -1)
3332 p = word;
3333 else
3334 {
3335 vim_strncpy(buf, word, len);
3336 p = buf;
3337 }
3338
3339 hash = hash_hash(p);
3340 hi = hash_lookup(&lp->sl_wordcount, p, hash);
3341 if (HASHITEM_EMPTY(hi))
3342 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003343 wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003344 if (wc == NULL)
3345 return;
3346 STRCPY(wc->wc_word, p);
3347 wc->wc_count = count;
3348 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
3349 }
3350 else
3351 {
3352 wc = HI2WC(hi);
3353 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
3354 wc->wc_count = MAXWORDCOUNT;
3355 }
3356}
3357
3358/*
3359 * Adjust the score of common words.
3360 */
3361 static int
3362score_wordcount_adj(slang, score, word, split)
3363 slang_T *slang;
3364 int score;
3365 char_u *word;
3366 int split; /* word was split, less bonus */
3367{
3368 hashitem_T *hi;
3369 wordcount_T *wc;
3370 int bonus;
3371 int newscore;
3372
3373 hi = hash_find(&slang->sl_wordcount, word);
3374 if (!HASHITEM_EMPTY(hi))
3375 {
3376 wc = HI2WC(hi);
3377 if (wc->wc_count < SCORE_THRES2)
3378 bonus = SCORE_COMMON1;
3379 else if (wc->wc_count < SCORE_THRES3)
3380 bonus = SCORE_COMMON2;
3381 else
3382 bonus = SCORE_COMMON3;
3383 if (split)
3384 newscore = score - bonus / 2;
3385 else
3386 newscore = score - bonus;
3387 if (newscore < 0)
3388 return 0;
3389 return newscore;
3390 }
3391 return score;
3392}
3393
3394/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003395 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
3396 * Return SP_*ERROR flags.
3397 */
3398 static int
3399read_sofo_section(fd, slang)
3400 FILE *fd;
3401 slang_T *slang;
3402{
3403 int cnt;
3404 char_u *from, *to;
3405 int res;
3406
3407 slang->sl_sofo = TRUE;
3408
3409 /* <sofofromlen> <sofofrom> */
3410 from = read_cnt_string(fd, 2, &cnt);
3411 if (cnt < 0)
3412 return cnt;
3413
3414 /* <sofotolen> <sofoto> */
3415 to = read_cnt_string(fd, 2, &cnt);
3416 if (cnt < 0)
3417 {
3418 vim_free(from);
3419 return cnt;
3420 }
3421
3422 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
3423 if (from != NULL && to != NULL)
3424 res = set_sofo(slang, from, to);
3425 else if (from != NULL || to != NULL)
3426 res = SP_FORMERROR; /* only one of two strings is an error */
3427 else
3428 res = 0;
3429
3430 vim_free(from);
3431 vim_free(to);
3432 return res;
3433}
3434
3435/*
3436 * Read the compound section from the .spl file:
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003437 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +00003438 * Returns SP_*ERROR flags.
3439 */
3440 static int
3441read_compound(fd, slang, len)
3442 FILE *fd;
3443 slang_T *slang;
3444 int len;
3445{
3446 int todo = len;
3447 int c;
3448 int atstart;
3449 char_u *pat;
3450 char_u *pp;
3451 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003452 char_u *ap;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003453 int cnt;
3454 garray_T *gap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003455
3456 if (todo < 2)
3457 return SP_FORMERROR; /* need at least two bytes */
3458
3459 --todo;
3460 c = getc(fd); /* <compmax> */
3461 if (c < 2)
3462 c = MAXWLEN;
3463 slang->sl_compmax = c;
3464
3465 --todo;
3466 c = getc(fd); /* <compminlen> */
3467 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003468 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003469 slang->sl_compminlen = c;
3470
3471 --todo;
3472 c = getc(fd); /* <compsylmax> */
3473 if (c < 1)
3474 c = MAXWLEN;
3475 slang->sl_compsylmax = c;
3476
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003477 c = getc(fd); /* <compoptions> */
3478 if (c != 0)
3479 ungetc(c, fd); /* be backwards compatible with Vim 7.0b */
3480 else
3481 {
3482 --todo;
3483 c = getc(fd); /* only use the lower byte for now */
3484 --todo;
3485 slang->sl_compoptions = c;
3486
3487 gap = &slang->sl_comppat;
3488 c = get2c(fd); /* <comppatcount> */
3489 todo -= 2;
3490 ga_init2(gap, sizeof(char_u *), c);
3491 if (ga_grow(gap, c) == OK)
3492 while (--c >= 0)
3493 {
3494 ((char_u **)(gap->ga_data))[gap->ga_len++] =
3495 read_cnt_string(fd, 1, &cnt);
3496 /* <comppatlen> <comppattext> */
3497 if (cnt < 0)
3498 return cnt;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003499 todo -= cnt + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003500 }
3501 }
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003502 if (todo < 0)
3503 return SP_FORMERROR;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003504
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003505 /* Turn the COMPOUNDRULE items into a regexp pattern:
Bram Moolenaar5195e452005-08-19 20:32:47 +00003506 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003507 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3508 * Conversion to utf-8 may double the size. */
3509 c = todo * 2 + 7;
3510#ifdef FEAT_MBYTE
3511 if (enc_utf8)
3512 c += todo * 2;
3513#endif
3514 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003515 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003516 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003517
Bram Moolenaard12a1322005-08-21 22:08:24 +00003518 /* We also need a list of all flags that can appear at the start and one
3519 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003520 cp = alloc(todo + 1);
3521 if (cp == NULL)
3522 {
3523 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003524 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003525 }
3526 slang->sl_compstartflags = cp;
3527 *cp = NUL;
3528
Bram Moolenaard12a1322005-08-21 22:08:24 +00003529 ap = alloc(todo + 1);
3530 if (ap == NULL)
3531 {
3532 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003533 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003534 }
3535 slang->sl_compallflags = ap;
3536 *ap = NUL;
3537
Bram Moolenaar5195e452005-08-19 20:32:47 +00003538 pp = pat;
3539 *pp++ = '^';
3540 *pp++ = '\\';
3541 *pp++ = '(';
3542
3543 atstart = 1;
3544 while (todo-- > 0)
3545 {
3546 c = getc(fd); /* <compflags> */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003547
3548 /* Add all flags to "sl_compallflags". */
3549 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003550 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003551 {
3552 *ap++ = c;
3553 *ap = NUL;
3554 }
3555
Bram Moolenaar5195e452005-08-19 20:32:47 +00003556 if (atstart != 0)
3557 {
3558 /* At start of item: copy flags to "sl_compstartflags". For a
3559 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3560 if (c == '[')
3561 atstart = 2;
3562 else if (c == ']')
3563 atstart = 0;
3564 else
3565 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003566 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003567 {
3568 *cp++ = c;
3569 *cp = NUL;
3570 }
3571 if (atstart == 1)
3572 atstart = 0;
3573 }
3574 }
3575 if (c == '/') /* slash separates two items */
3576 {
3577 *pp++ = '\\';
3578 *pp++ = '|';
3579 atstart = 1;
3580 }
3581 else /* normal char, "[abc]" and '*' are copied as-is */
3582 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003583 if (c == '+' || c == '~')
Bram Moolenaar5195e452005-08-19 20:32:47 +00003584 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003585#ifdef FEAT_MBYTE
3586 if (enc_utf8)
3587 pp += mb_char2bytes(c, pp);
3588 else
3589#endif
3590 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003591 }
3592 }
3593
3594 *pp++ = '\\';
3595 *pp++ = ')';
3596 *pp++ = '$';
3597 *pp = NUL;
3598
3599 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3600 vim_free(pat);
3601 if (slang->sl_compprog == NULL)
3602 return SP_FORMERROR;
3603
3604 return 0;
3605}
3606
Bram Moolenaar6de68532005-08-24 22:08:48 +00003607/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003608 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003609 * Like strchr() but independent of locale.
3610 */
3611 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003612byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003613 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003614 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003615{
3616 char_u *p;
3617
3618 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003619 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003620 return TRUE;
3621 return FALSE;
3622}
3623
Bram Moolenaar5195e452005-08-19 20:32:47 +00003624#define SY_MAXLEN 30
3625typedef struct syl_item_S
3626{
3627 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3628 int sy_len;
3629} syl_item_T;
3630
3631/*
3632 * Truncate "slang->sl_syllable" at the first slash and put the following items
3633 * in "slang->sl_syl_items".
3634 */
3635 static int
3636init_syl_tab(slang)
3637 slang_T *slang;
3638{
3639 char_u *p;
3640 char_u *s;
3641 int l;
3642 syl_item_T *syl;
3643
3644 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3645 p = vim_strchr(slang->sl_syllable, '/');
3646 while (p != NULL)
3647 {
3648 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003649 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003650 break;
3651 s = p;
3652 p = vim_strchr(p, '/');
3653 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003654 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003655 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003656 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003657 if (l >= SY_MAXLEN)
3658 return SP_FORMERROR;
3659 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003660 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003661 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3662 + slang->sl_syl_items.ga_len++;
3663 vim_strncpy(syl->sy_chars, s, l);
3664 syl->sy_len = l;
3665 }
3666 return OK;
3667}
3668
3669/*
3670 * Count the number of syllables in "word".
3671 * When "word" contains spaces the syllables after the last space are counted.
3672 * Returns zero if syllables are not defines.
3673 */
3674 static int
3675count_syllables(slang, word)
3676 slang_T *slang;
3677 char_u *word;
3678{
3679 int cnt = 0;
3680 int skip = FALSE;
3681 char_u *p;
3682 int len;
3683 int i;
3684 syl_item_T *syl;
3685 int c;
3686
3687 if (slang->sl_syllable == NULL)
3688 return 0;
3689
3690 for (p = word; *p != NUL; p += len)
3691 {
3692 /* When running into a space reset counter. */
3693 if (*p == ' ')
3694 {
3695 len = 1;
3696 cnt = 0;
3697 continue;
3698 }
3699
3700 /* Find longest match of syllable items. */
3701 len = 0;
3702 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3703 {
3704 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3705 if (syl->sy_len > len
3706 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3707 len = syl->sy_len;
3708 }
3709 if (len != 0) /* found a match, count syllable */
3710 {
3711 ++cnt;
3712 skip = FALSE;
3713 }
3714 else
3715 {
3716 /* No recognized syllable item, at least a syllable char then? */
3717#ifdef FEAT_MBYTE
3718 c = mb_ptr2char(p);
3719 len = (*mb_ptr2len)(p);
3720#else
3721 c = *p;
3722 len = 1;
3723#endif
3724 if (vim_strchr(slang->sl_syllable, c) == NULL)
3725 skip = FALSE; /* No, search for next syllable */
3726 else if (!skip)
3727 {
3728 ++cnt; /* Yes, count it */
3729 skip = TRUE; /* don't count following syllable chars */
3730 }
3731 }
3732 }
3733 return cnt;
3734}
3735
3736/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003737 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003738 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003739 */
3740 static int
3741set_sofo(lp, from, to)
3742 slang_T *lp;
3743 char_u *from;
3744 char_u *to;
3745{
3746 int i;
3747
3748#ifdef FEAT_MBYTE
3749 garray_T *gap;
3750 char_u *s;
3751 char_u *p;
3752 int c;
3753 int *inp;
3754
3755 if (has_mbyte)
3756 {
3757 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3758 * characters. The index is the low byte of the character.
3759 * The list contains from-to pairs with a terminating NUL.
3760 * sl_sal_first[] is used for latin1 "from" characters. */
3761 gap = &lp->sl_sal;
3762 ga_init2(gap, sizeof(int *), 1);
3763 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003764 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003765 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3766 gap->ga_len = 256;
3767
3768 /* First count the number of items for each list. Temporarily use
3769 * sl_sal_first[] for this. */
3770 for (p = from, s = to; *p != NUL && *s != NUL; )
3771 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003772 c = mb_cptr2char_adv(&p);
3773 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003774 if (c >= 256)
3775 ++lp->sl_sal_first[c & 0xff];
3776 }
3777 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003778 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003779
3780 /* Allocate the lists. */
3781 for (i = 0; i < 256; ++i)
3782 if (lp->sl_sal_first[i] > 0)
3783 {
3784 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3785 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003786 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003787 ((int **)gap->ga_data)[i] = (int *)p;
3788 *(int *)p = 0;
3789 }
3790
3791 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3792 * list. */
3793 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3794 for (p = from, s = to; *p != NUL && *s != NUL; )
3795 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003796 c = mb_cptr2char_adv(&p);
3797 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003798 if (c >= 256)
3799 {
3800 /* Append the from-to chars at the end of the list with
3801 * the low byte. */
3802 inp = ((int **)gap->ga_data)[c & 0xff];
3803 while (*inp != 0)
3804 ++inp;
3805 *inp++ = c; /* from char */
3806 *inp++ = i; /* to char */
3807 *inp++ = NUL; /* NUL at the end */
3808 }
3809 else
3810 /* mapping byte to char is done in sl_sal_first[] */
3811 lp->sl_sal_first[c] = i;
3812 }
3813 }
3814 else
3815#endif
3816 {
3817 /* mapping bytes to bytes is done in sl_sal_first[] */
3818 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003819 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003820
3821 for (i = 0; to[i] != NUL; ++i)
3822 lp->sl_sal_first[from[i]] = to[i];
3823 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3824 }
3825
Bram Moolenaar5195e452005-08-19 20:32:47 +00003826 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003827}
3828
3829/*
3830 * Fill the first-index table for "lp".
3831 */
3832 static void
3833set_sal_first(lp)
3834 slang_T *lp;
3835{
3836 salfirst_T *sfirst;
3837 int i;
3838 salitem_T *smp;
3839 int c;
3840 garray_T *gap = &lp->sl_sal;
3841
3842 sfirst = lp->sl_sal_first;
3843 for (i = 0; i < 256; ++i)
3844 sfirst[i] = -1;
3845 smp = (salitem_T *)gap->ga_data;
3846 for (i = 0; i < gap->ga_len; ++i)
3847 {
3848#ifdef FEAT_MBYTE
3849 if (has_mbyte)
3850 /* Use the lowest byte of the first character. For latin1 it's
3851 * the character, for other encodings it should differ for most
3852 * characters. */
3853 c = *smp[i].sm_lead_w & 0xff;
3854 else
3855#endif
3856 c = *smp[i].sm_lead;
3857 if (sfirst[c] == -1)
3858 {
3859 sfirst[c] = i;
3860#ifdef FEAT_MBYTE
3861 if (has_mbyte)
3862 {
3863 int n;
3864
3865 /* Make sure all entries with this byte are following each
3866 * other. Move the ones that are in the wrong position. Do
3867 * keep the same ordering! */
3868 while (i + 1 < gap->ga_len
3869 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3870 /* Skip over entry with same index byte. */
3871 ++i;
3872
3873 for (n = 1; i + n < gap->ga_len; ++n)
3874 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3875 {
3876 salitem_T tsal;
3877
3878 /* Move entry with same index byte after the entries
3879 * we already found. */
3880 ++i;
3881 --n;
3882 tsal = smp[i + n];
3883 mch_memmove(smp + i + 1, smp + i,
3884 sizeof(salitem_T) * n);
3885 smp[i] = tsal;
3886 }
3887 }
3888#endif
3889 }
3890 }
3891}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003892
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003893#ifdef FEAT_MBYTE
3894/*
3895 * Turn a multi-byte string into a wide character string.
3896 * Return it in allocated memory (NULL for out-of-memory)
3897 */
3898 static int *
3899mb_str2wide(s)
3900 char_u *s;
3901{
3902 int *res;
3903 char_u *p;
3904 int i = 0;
3905
3906 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3907 if (res != NULL)
3908 {
3909 for (p = s; *p != NUL; )
3910 res[i++] = mb_ptr2char_adv(&p);
3911 res[i] = NUL;
3912 }
3913 return res;
3914}
3915#endif
3916
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003917/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003918 * Read a tree from the .spl or .sug file.
3919 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
3920 * This is skipped when the tree has zero length.
3921 * Returns zero when OK, SP_ value for an error.
3922 */
3923 static int
3924spell_read_tree(fd, bytsp, idxsp, prefixtree, prefixcnt)
3925 FILE *fd;
3926 char_u **bytsp;
3927 idx_T **idxsp;
3928 int prefixtree; /* TRUE for the prefix tree */
3929 int prefixcnt; /* when "prefixtree" is TRUE: prefix count */
3930{
3931 int len;
3932 int idx;
3933 char_u *bp;
3934 idx_T *ip;
3935
3936 /* The tree size was computed when writing the file, so that we can
3937 * allocate it as one long block. <nodecount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003938 len = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003939 if (len < 0)
3940 return SP_TRUNCERROR;
3941 if (len > 0)
3942 {
3943 /* Allocate the byte array. */
3944 bp = lalloc((long_u)len, TRUE);
3945 if (bp == NULL)
3946 return SP_OTHERERROR;
3947 *bytsp = bp;
3948
3949 /* Allocate the index array. */
3950 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
3951 if (ip == NULL)
3952 return SP_OTHERERROR;
3953 *idxsp = ip;
3954
3955 /* Recursively read the tree and store it in the array. */
3956 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
3957 if (idx < 0)
3958 return idx;
3959 }
3960 return 0;
3961}
3962
3963/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003964 * Read one row of siblings from the spell file and store it in the byte array
3965 * "byts" and index array "idxs". Recursively read the children.
3966 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003967 * NOTE: The code here must match put_node()!
Bram Moolenaar51485f02005-06-04 21:55:20 +00003968 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003969 * Returns the index (>= 0) following the siblings.
3970 * Returns SP_TRUNCERROR if the file is shorter than expected.
3971 * Returns SP_FORMERROR if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003972 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003973 static idx_T
Bram Moolenaar4770d092006-01-12 23:22:24 +00003974read_tree_node(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003975 FILE *fd;
3976 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003977 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003978 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003979 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003980 int prefixtree; /* TRUE for reading PREFIXTREE */
3981 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003982{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003983 int len;
3984 int i;
3985 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003986 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003987 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003988 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003989#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003990
Bram Moolenaar51485f02005-06-04 21:55:20 +00003991 len = getc(fd); /* <siblingcount> */
3992 if (len <= 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003993 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003994
3995 if (startidx + len >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003996 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003997 byts[idx++] = len;
3998
3999 /* Read the byte values, flag/region bytes and shared indexes. */
4000 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004001 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004002 c = getc(fd); /* <byte> */
4003 if (c < 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004004 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004005 if (c <= BY_SPECIAL)
4006 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004007 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004008 {
4009 /* No flags, all regions. */
4010 idxs[idx] = 0;
4011 c = 0;
4012 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004013 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004014 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004015 if (prefixtree)
4016 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004017 /* Read the optional pflags byte, the prefix ID and the
4018 * condition nr. In idxs[] store the prefix ID in the low
4019 * byte, the condition index shifted up 8 bits, the flags
4020 * shifted up 24 bits. */
4021 if (c == BY_FLAGS)
4022 c = getc(fd) << 24; /* <pflags> */
4023 else
4024 c = 0;
4025
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004026 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004027
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004028 n = get2c(fd); /* <prefcondnr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004029 if (n >= maxprefcondnr)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004030 return SP_FORMERROR;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004031 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004032 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004033 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004034 {
4035 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004036 * idxs[] the flags go in the low two bytes, region above
4037 * that and prefix ID above the region. */
4038 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004039 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004040 if (c2 == BY_FLAGS2)
4041 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004042 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004043 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004044 if (c & WF_AFX)
4045 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004046 }
4047
Bram Moolenaar51485f02005-06-04 21:55:20 +00004048 idxs[idx] = c;
4049 c = 0;
4050 }
4051 else /* c == BY_INDEX */
4052 {
4053 /* <nodeidx> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004054 n = get3c(fd);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004055 if (n < 0 || n >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004056 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004057 idxs[idx] = n + SHARED_MASK;
4058 c = getc(fd); /* <xbyte> */
4059 }
4060 }
4061 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004062 }
4063
Bram Moolenaar51485f02005-06-04 21:55:20 +00004064 /* Recursively read the children for non-shared siblings.
4065 * Skip the end-of-word ones (zero byte value) and the shared ones (and
4066 * remove SHARED_MASK) */
4067 for (i = 1; i <= len; ++i)
4068 if (byts[startidx + i] != 0)
4069 {
4070 if (idxs[startidx + i] & SHARED_MASK)
4071 idxs[startidx + i] &= ~SHARED_MASK;
4072 else
4073 {
4074 idxs[startidx + i] = idx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004075 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004076 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004077 if (idx < 0)
4078 break;
4079 }
4080 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004081
Bram Moolenaar51485f02005-06-04 21:55:20 +00004082 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004083}
4084
4085/*
4086 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004087 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004088 */
4089 char_u *
4090did_set_spelllang(buf)
4091 buf_T *buf;
4092{
4093 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004094 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004095 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00004096 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004097 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004098 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004099 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004100 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004101 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004102 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004103 int len;
4104 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004105 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004106 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004107 char_u *use_region = NULL;
4108 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004109 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004110 int i, j;
4111 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004112 static int recursive = FALSE;
4113 char_u *ret_msg = NULL;
4114 char_u *spl_copy;
4115
4116 /* We don't want to do this recursively. May happen when a language is
4117 * not available and the SpellFileMissing autocommand opens a new buffer
4118 * in which 'spell' is set. */
4119 if (recursive)
4120 return NULL;
4121 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004122
4123 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004124 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004125
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004126 /* Make a copy of 'spellang', the SpellFileMissing autocommands may change
4127 * it under our fingers. */
4128 spl_copy = vim_strsave(buf->b_p_spl);
4129 if (spl_copy == NULL)
4130 goto theend;
4131
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004132 /* loop over comma separated language names. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004133 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004134 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004135 /* Get one language name. */
4136 copy_option_part(&splp, lang, MAXWLEN, ",");
4137
Bram Moolenaar5482f332005-04-17 20:18:43 +00004138 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004139 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004140
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004141 /* If the name ends in ".spl" use it as the name of the spell file.
4142 * If there is a region name let "region" point to it and remove it
4143 * from the name. */
4144 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
4145 {
4146 filename = TRUE;
4147
Bram Moolenaarb6356332005-07-18 21:40:44 +00004148 /* Locate a region and remove it from the file name. */
4149 p = vim_strchr(gettail(lang), '_');
4150 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
4151 && !ASCII_ISALPHA(p[3]))
4152 {
4153 vim_strncpy(region_cp, p + 1, 2);
4154 mch_memmove(p, p + 3, len - (p - lang) - 2);
4155 len -= 3;
4156 region = region_cp;
4157 }
4158 else
4159 dont_use_region = TRUE;
4160
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004161 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004162 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4163 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004164 break;
4165 }
4166 else
4167 {
4168 filename = FALSE;
4169 if (len > 3 && lang[len - 3] == '_')
4170 {
4171 region = lang + len - 2;
4172 len -= 3;
4173 lang[len] = NUL;
4174 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004175 else
4176 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004177
4178 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004179 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4180 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004181 break;
4182 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004183
Bram Moolenaarb6356332005-07-18 21:40:44 +00004184 if (region != NULL)
4185 {
4186 /* If the region differs from what was used before then don't
4187 * use it for 'spellfile'. */
4188 if (use_region != NULL && STRCMP(region, use_region) != 0)
4189 dont_use_region = TRUE;
4190 use_region = region;
4191 }
4192
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004193 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004194 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004195 {
4196 if (filename)
4197 (void)spell_load_file(lang, lang, NULL, FALSE);
4198 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004199 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004200 spell_load_lang(lang);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004201#ifdef FEAT_AUTOCMD
4202 /* SpellFileMissing autocommands may do anything, including
4203 * destroying the buffer we are using... */
4204 if (!buf_valid(buf))
4205 {
4206 ret_msg = (char_u *)"E797: SpellFileMissing autocommand deleted buffer";
4207 goto theend;
4208 }
4209#endif
4210 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004211 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004212
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004213 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004214 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004215 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004216 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4217 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4218 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004219 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004220 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004221 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004222 {
4223 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004224 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004225 if (c == REGION_ALL)
4226 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004227 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004228 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004229 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004230 /* This addition file is for other regions. */
4231 region_mask = 0;
4232 }
4233 else
4234 /* This is probably an error. Give a warning and
4235 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004236 smsg((char_u *)
4237 _("Warning: region %s not supported"),
4238 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004239 }
4240 else
4241 region_mask = 1 << c;
4242 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004243
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004244 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004245 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004246 if (ga_grow(&ga, 1) == FAIL)
4247 {
4248 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004249 ret_msg = e_outofmem;
4250 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004251 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004252 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004253 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4254 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004255 use_midword(slang, buf);
4256 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004257 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004258 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004259 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004260 }
4261
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004262 /* round 0: load int_wordlist, if possible.
4263 * round 1: load first name in 'spellfile'.
4264 * round 2: load second name in 'spellfile.
4265 * etc. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004266 spf = buf->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004267 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004268 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004269 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004270 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004271 /* Internal wordlist, if there is one. */
4272 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004273 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004274 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004275 }
4276 else
4277 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004278 /* One entry in 'spellfile'. */
4279 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4280 STRCAT(spf_name, ".spl");
4281
4282 /* If it was already found above then skip it. */
4283 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004284 {
4285 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4286 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004287 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004288 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004289 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004290 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004291 }
4292
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004293 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004294 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4295 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004296 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004297 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004298 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004299 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004300 * region name, the region is ignored otherwise. for int_wordlist
4301 * use an arbitrary name. */
4302 if (round == 0)
4303 STRCPY(lang, "internal wordlist");
4304 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00004305 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004306 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004307 p = vim_strchr(lang, '.');
4308 if (p != NULL)
4309 *p = NUL; /* truncate at ".encoding.add" */
4310 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004311 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004312
4313 /* If one of the languages has NOBREAK we assume the addition
4314 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004315 if (slang != NULL && nobreak)
4316 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004317 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004318 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004319 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004320 region_mask = REGION_ALL;
4321 if (use_region != NULL && !dont_use_region)
4322 {
4323 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004324 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004325 if (c != REGION_ALL)
4326 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004327 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004328 /* This spell file is for other regions. */
4329 region_mask = 0;
4330 }
4331
4332 if (region_mask != 0)
4333 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004334 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4335 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4336 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004337 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4338 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004339 use_midword(slang, buf);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004340 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004341 }
4342 }
4343
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004344 /* Everything is fine, store the new b_langp value. */
4345 ga_clear(&buf->b_langp);
4346 buf->b_langp = ga;
4347
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004348 /* For each language figure out what language to use for sound folding and
4349 * REP items. If the language doesn't support it itself use another one
4350 * with the same name. E.g. for "en-math" use "en". */
4351 for (i = 0; i < ga.ga_len; ++i)
4352 {
4353 lp = LANGP_ENTRY(ga, i);
4354
4355 /* sound folding */
4356 if (lp->lp_slang->sl_sal.ga_len > 0)
4357 /* language does sound folding itself */
4358 lp->lp_sallang = lp->lp_slang;
4359 else
4360 /* find first similar language that does sound folding */
4361 for (j = 0; j < ga.ga_len; ++j)
4362 {
4363 lp2 = LANGP_ENTRY(ga, j);
4364 if (lp2->lp_slang->sl_sal.ga_len > 0
4365 && STRNCMP(lp->lp_slang->sl_name,
4366 lp2->lp_slang->sl_name, 2) == 0)
4367 {
4368 lp->lp_sallang = lp2->lp_slang;
4369 break;
4370 }
4371 }
4372
4373 /* REP items */
4374 if (lp->lp_slang->sl_rep.ga_len > 0)
4375 /* language has REP items itself */
4376 lp->lp_replang = lp->lp_slang;
4377 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004378 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004379 for (j = 0; j < ga.ga_len; ++j)
4380 {
4381 lp2 = LANGP_ENTRY(ga, j);
4382 if (lp2->lp_slang->sl_rep.ga_len > 0
4383 && STRNCMP(lp->lp_slang->sl_name,
4384 lp2->lp_slang->sl_name, 2) == 0)
4385 {
4386 lp->lp_replang = lp2->lp_slang;
4387 break;
4388 }
4389 }
4390 }
4391
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004392theend:
4393 vim_free(spl_copy);
4394 recursive = FALSE;
4395 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004396}
4397
4398/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004399 * Clear the midword characters for buffer "buf".
4400 */
4401 static void
4402clear_midword(buf)
4403 buf_T *buf;
4404{
4405 vim_memset(buf->b_spell_ismw, 0, 256);
4406#ifdef FEAT_MBYTE
4407 vim_free(buf->b_spell_ismw_mb);
4408 buf->b_spell_ismw_mb = NULL;
4409#endif
4410}
4411
4412/*
4413 * Use the "sl_midword" field of language "lp" for buffer "buf".
4414 * They add up to any currently used midword characters.
4415 */
4416 static void
4417use_midword(lp, buf)
4418 slang_T *lp;
4419 buf_T *buf;
4420{
4421 char_u *p;
4422
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004423 if (lp->sl_midword == NULL) /* there aren't any */
4424 return;
4425
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004426 for (p = lp->sl_midword; *p != NUL; )
4427#ifdef FEAT_MBYTE
4428 if (has_mbyte)
4429 {
4430 int c, l, n;
4431 char_u *bp;
4432
4433 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004434 l = (*mb_ptr2len)(p);
4435 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004436 buf->b_spell_ismw[c] = TRUE;
4437 else if (buf->b_spell_ismw_mb == NULL)
4438 /* First multi-byte char in "b_spell_ismw_mb". */
4439 buf->b_spell_ismw_mb = vim_strnsave(p, l);
4440 else
4441 {
4442 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004443 n = (int)STRLEN(buf->b_spell_ismw_mb);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004444 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
4445 if (bp != NULL)
4446 {
4447 vim_free(buf->b_spell_ismw_mb);
4448 buf->b_spell_ismw_mb = bp;
4449 vim_strncpy(bp + n, p, l);
4450 }
4451 }
4452 p += l;
4453 }
4454 else
4455#endif
4456 buf->b_spell_ismw[*p++] = TRUE;
4457}
4458
4459/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004460 * Find the region "region[2]" in "rp" (points to "sl_regions").
4461 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00004462 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004463 */
4464 static int
4465find_region(rp, region)
4466 char_u *rp;
4467 char_u *region;
4468{
4469 int i;
4470
4471 for (i = 0; ; i += 2)
4472 {
4473 if (rp[i] == NUL)
4474 return REGION_ALL;
4475 if (rp[i] == region[0] && rp[i + 1] == region[1])
4476 break;
4477 }
4478 return i / 2;
4479}
4480
4481/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004482 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004483 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00004484 * Word WF_ONECAP
4485 * W WORD WF_ALLCAP
4486 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004487 */
4488 static int
4489captype(word, end)
4490 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004491 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004492{
4493 char_u *p;
4494 int c;
4495 int firstcap;
4496 int allcap;
4497 int past_second = FALSE; /* past second word char */
4498
4499 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004500 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004501 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004502 return 0; /* only non-word characters, illegal word */
4503#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004504 if (has_mbyte)
4505 c = mb_ptr2char_adv(&p);
4506 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004507#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004508 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004509 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004510
4511 /*
4512 * Need to check all letters to find a word with mixed upper/lower.
4513 * But a word with an upper char only at start is a ONECAP.
4514 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004515 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004516 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004517 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004518 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004519 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004520 {
4521 /* UUl -> KEEPCAP */
4522 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004523 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004524 allcap = FALSE;
4525 }
4526 else if (!allcap)
4527 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004528 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004529 past_second = TRUE;
4530 }
4531
4532 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004533 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004534 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004535 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004536 return 0;
4537}
4538
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004539/*
4540 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4541 * capital. So that make_case_word() can turn WOrd into Word.
4542 * Add ALLCAP for "WOrD".
4543 */
4544 static int
4545badword_captype(word, end)
4546 char_u *word;
4547 char_u *end;
4548{
4549 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004550 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004551 int l, u;
4552 int first;
4553 char_u *p;
4554
4555 if (flags & WF_KEEPCAP)
4556 {
4557 /* Count the number of UPPER and lower case letters. */
4558 l = u = 0;
4559 first = FALSE;
4560 for (p = word; p < end; mb_ptr_adv(p))
4561 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004562 c = PTR2CHAR(p);
4563 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004564 {
4565 ++u;
4566 if (p == word)
4567 first = TRUE;
4568 }
4569 else
4570 ++l;
4571 }
4572
4573 /* If there are more UPPER than lower case letters suggest an
4574 * ALLCAP word. Otherwise, if the first letter is UPPER then
4575 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4576 * require three upper case letters. */
4577 if (u > l && u > 2)
4578 flags |= WF_ALLCAP;
4579 else if (first)
4580 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004581
4582 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4583 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004584 }
4585 return flags;
4586}
4587
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004588# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4589/*
4590 * Free all languages.
4591 */
4592 void
4593spell_free_all()
4594{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004595 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004596 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004597 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004598
4599 /* Go through all buffers and handle 'spelllang'. */
4600 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4601 ga_clear(&buf->b_langp);
4602
4603 while (first_lang != NULL)
4604 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004605 slang = first_lang;
4606 first_lang = slang->sl_next;
4607 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004608 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004609
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004610 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004611 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004612 /* Delete the internal wordlist and its .spl file */
4613 mch_remove(int_wordlist);
4614 int_wordlist_spl(fname);
4615 mch_remove(fname);
4616 vim_free(int_wordlist);
4617 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004618 }
4619
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004620 init_spell_chartab();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004621
4622 vim_free(repl_to);
4623 repl_to = NULL;
4624 vim_free(repl_from);
4625 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004626}
4627# endif
4628
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004629# if defined(FEAT_MBYTE) || defined(PROTO)
4630/*
4631 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004632 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004633 */
4634 void
4635spell_reload()
4636{
4637 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004638 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004639
Bram Moolenaarea408852005-06-25 22:49:46 +00004640 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004641 init_spell_chartab();
4642
4643 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004644 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004645
4646 /* Go through all buffers and handle 'spelllang'. */
4647 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4648 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004649 /* Only load the wordlists when 'spelllang' is set and there is a
4650 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004651 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004652 {
4653 FOR_ALL_WINDOWS(wp)
4654 if (wp->w_buffer == buf && wp->w_p_spell)
4655 {
4656 (void)did_set_spelllang(buf);
4657# ifdef FEAT_WINDOWS
4658 break;
4659# endif
4660 }
4661 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004662 }
4663}
4664# endif
4665
Bram Moolenaarb765d632005-06-07 21:00:02 +00004666/*
4667 * Reload the spell file "fname" if it's loaded.
4668 */
4669 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004670spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004671 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004672 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004673{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004674 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004675 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004676
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004677 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004678 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004679 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004680 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004681 slang_clear(slang);
4682 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004683 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004684 slang_clear(slang);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004685 redraw_all_later(SOME_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004686 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004687 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004688 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004689
4690 /* When "zg" was used and the file wasn't loaded yet, should redo
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004691 * 'spelllang' to load it now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004692 if (added_word && !didit)
4693 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004694}
4695
4696
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004697/*
4698 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004699 */
4700
Bram Moolenaar51485f02005-06-04 21:55:20 +00004701#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004702 and .dic file. */
4703/*
4704 * Main structure to store the contents of a ".aff" file.
4705 */
4706typedef struct afffile_S
4707{
4708 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004709 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004710 unsigned af_rare; /* RARE ID for rare word */
4711 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004712 unsigned af_bad; /* BAD ID for banned word */
4713 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004714 unsigned af_circumfix; /* CIRCUMFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004715 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004716 unsigned af_comproot; /* COMPOUNDROOT ID */
4717 unsigned af_compforbid; /* COMPOUNDFORBIDFLAG ID */
4718 unsigned af_comppermit; /* COMPOUNDPERMITFLAG ID */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004719 unsigned af_nosuggest; /* NOSUGGEST ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004720 int af_pfxpostpone; /* postpone prefixes without chop string and
4721 without flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004722 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4723 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004724 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004725} afffile_T;
4726
Bram Moolenaar6de68532005-08-24 22:08:48 +00004727#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004728#define AFT_LONG 1 /* flags are two characters */
4729#define AFT_CAPLONG 2 /* flags are one or two characters */
4730#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004731
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004732typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004733/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4734struct affentry_S
4735{
4736 affentry_T *ae_next; /* next affix with same name/number */
4737 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4738 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004739 char_u *ae_flags; /* flags on the affix (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004740 char_u *ae_cond; /* condition (NULL for ".") */
4741 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004742 char ae_compforbid; /* COMPOUNDFORBIDFLAG found */
4743 char ae_comppermit; /* COMPOUNDPERMITFLAG found */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004744};
4745
Bram Moolenaar6de68532005-08-24 22:08:48 +00004746#ifdef FEAT_MBYTE
4747# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4748#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004749# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004750#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004751
Bram Moolenaar51485f02005-06-04 21:55:20 +00004752/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4753typedef struct affheader_S
4754{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004755 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4756 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4757 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004758 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004759 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004760 affentry_T *ah_first; /* first affix entry */
4761} affheader_T;
4762
4763#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4764
Bram Moolenaar6de68532005-08-24 22:08:48 +00004765/* Flag used in compound items. */
4766typedef struct compitem_S
4767{
4768 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4769 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4770 int ci_newID; /* affix ID after renumbering. */
4771} compitem_T;
4772
4773#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4774
Bram Moolenaar51485f02005-06-04 21:55:20 +00004775/*
4776 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004777 * the need to keep track of each allocated thing, everything is freed all at
4778 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004779 */
4780#define SBLOCKSIZE 16000 /* size of sb_data */
4781typedef struct sblock_S sblock_T;
4782struct sblock_S
4783{
4784 sblock_T *sb_next; /* next block in list */
4785 int sb_used; /* nr of bytes already in use */
4786 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004787};
4788
4789/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004790 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004791 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004792typedef struct wordnode_S wordnode_T;
4793struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004794{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004795 union /* shared to save space */
4796 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004797 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004798 int index; /* index in written nodes (valid after first
4799 round) */
4800 } wn_u1;
4801 union /* shared to save space */
4802 {
4803 wordnode_T *next; /* next node with same hash key */
4804 wordnode_T *wnode; /* parent node that will write this node */
4805 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004806 wordnode_T *wn_child; /* child (next byte in word) */
4807 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4808 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004809 int wn_refs; /* Nr. of references to this node. Only
4810 relevant for first node in a list of
4811 siblings, in following siblings it is
4812 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004813 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004814
4815 /* Info for when "wn_byte" is NUL.
4816 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4817 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4818 * "wn_region" the LSW of the wordnr. */
4819 char_u wn_affixID; /* supported/required prefix ID or 0 */
4820 short_u wn_flags; /* WF_ flags */
4821 short wn_region; /* region mask */
4822
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004823#ifdef SPELL_PRINTTREE
4824 int wn_nr; /* sequence nr for printing */
4825#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004826};
4827
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004828#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4829
Bram Moolenaar51485f02005-06-04 21:55:20 +00004830#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004831
Bram Moolenaar51485f02005-06-04 21:55:20 +00004832/*
4833 * Info used while reading the spell files.
4834 */
4835typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004836{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004837 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004838 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004839
Bram Moolenaar51485f02005-06-04 21:55:20 +00004840 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004841 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004842
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004843 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004844
Bram Moolenaar4770d092006-01-12 23:22:24 +00004845 long si_sugtree; /* creating the soundfolding trie */
4846
Bram Moolenaar51485f02005-06-04 21:55:20 +00004847 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004848 long si_blocks_cnt; /* memory blocks allocated */
4849 long si_compress_cnt; /* words to add before lowering
4850 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004851 wordnode_T *si_first_free; /* List of nodes that have been freed during
4852 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004853 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004854#ifdef SPELL_PRINTTREE
4855 int si_wordnode_nr; /* sequence nr for nodes */
4856#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004857 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004858
Bram Moolenaar51485f02005-06-04 21:55:20 +00004859 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004860 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004861 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004862 int si_region; /* region mask */
4863 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004864 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004865 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004866 int si_msg_count; /* number of words added since last message */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004867 char_u *si_info; /* info text chars or NULL */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004868 int si_region_count; /* number of regions supported (1 when there
4869 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004870 char_u si_region_name[16]; /* region names; used only if
4871 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004872
4873 garray_T si_rep; /* list of fromto_T entries from REP lines */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004874 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004875 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004876 char_u *si_sofofr; /* SOFOFROM text */
4877 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004878 int si_nosugfile; /* NOSUGFILE item found */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004879 int si_nosplitsugs; /* NOSPLITSUGS item found */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004880 int si_followup; /* soundsalike: ? */
4881 int si_collapse; /* soundsalike: ? */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004882 hashtab_T si_commonwords; /* hashtable for common words */
4883 time_t si_sugtime; /* timestamp for .sug file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004884 int si_rem_accents; /* soundsalike: remove accents */
4885 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004886 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004887 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004888 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004889 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004890 int si_compoptions; /* COMP_ flags */
4891 garray_T si_comppat; /* CHECKCOMPOUNDPATTERN items, each stored as
4892 a string */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004893 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004894 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004895 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004896 garray_T si_prefcond; /* table with conditions for postponed
4897 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004898 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004899 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004900} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004901
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004902static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004903static void aff_process_flags __ARGS((afffile_T *affile, affentry_T *entry));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004904static int spell_info_item __ARGS((char_u *s));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004905static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
4906static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
4907static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004908static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004909static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
4910static void aff_check_number __ARGS((int spinval, int affval, char *name));
4911static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004912static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004913static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4914static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00004915static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004916static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004917static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004918static int get_affix_flags __ARGS((afffile_T *affile, char_u *afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004919static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004920static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004921static 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 +00004922static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
4923static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
4924static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004925static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004926static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004927static 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 +00004928static 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 +00004929static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004930static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004931static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
4932static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
4933static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004934static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004935static void put_sugtime __ARGS((spellinfo_T *spin, FILE *fd));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004936static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00004937static void clear_node __ARGS((wordnode_T *node));
4938static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004939static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname));
4940static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang));
4941static int sug_maketable __ARGS((spellinfo_T *spin));
4942static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap));
4943static int offset2bytes __ARGS((int nr, char_u *buf));
4944static int bytes2offset __ARGS((char_u **pp));
4945static void sug_write __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004946static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004947static void spell_message __ARGS((spellinfo_T *spin, char_u *str));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004948static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004949
Bram Moolenaar53805d12005-08-01 07:08:33 +00004950/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
4951 * but it must be negative to indicate the prefix tree to tree_add_word().
4952 * Use a negative number with the lower 8 bits zero. */
4953#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004954
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004955/* flags for "condit" argument of store_aff_word() */
4956#define CONDIT_COMB 1 /* affix must combine */
4957#define CONDIT_CFIX 2 /* affix must have CIRCUMFIX flag */
4958#define CONDIT_SUF 4 /* add a suffix for matching flags */
4959#define CONDIT_AFF 8 /* word already has an affix */
4960
Bram Moolenaar5195e452005-08-19 20:32:47 +00004961/*
4962 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
4963 */
4964static long compress_start = 30000; /* memory / SBLOCKSIZE */
4965static long compress_inc = 100; /* memory / SBLOCKSIZE */
4966static long compress_added = 500000; /* word count */
4967
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004968#ifdef SPELL_PRINTTREE
4969/*
4970 * For debugging the tree code: print the current tree in a (more or less)
4971 * readable format, so that we can see what happens when adding a word and/or
4972 * compressing the tree.
4973 * Based on code from Olaf Seibert.
4974 */
4975#define PRINTLINESIZE 1000
4976#define PRINTWIDTH 6
4977
4978#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4979 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4980
4981static char line1[PRINTLINESIZE];
4982static char line2[PRINTLINESIZE];
4983static char line3[PRINTLINESIZE];
4984
4985 static void
4986spell_clear_flags(wordnode_T *node)
4987{
4988 wordnode_T *np;
4989
4990 for (np = node; np != NULL; np = np->wn_sibling)
4991 {
4992 np->wn_u1.index = FALSE;
4993 spell_clear_flags(np->wn_child);
4994 }
4995}
4996
4997 static void
4998spell_print_node(wordnode_T *node, int depth)
4999{
5000 if (node->wn_u1.index)
5001 {
5002 /* Done this node before, print the reference. */
5003 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
5004 PRINTSOME(line2, depth, " ", 0, 0);
5005 PRINTSOME(line3, depth, " ", 0, 0);
5006 msg(line1);
5007 msg(line2);
5008 msg(line3);
5009 }
5010 else
5011 {
5012 node->wn_u1.index = TRUE;
5013
5014 if (node->wn_byte != NUL)
5015 {
5016 if (node->wn_child != NULL)
5017 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
5018 else
5019 /* Cannot happen? */
5020 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
5021 }
5022 else
5023 PRINTSOME(line1, depth, " $ ", 0, 0);
5024
5025 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
5026
5027 if (node->wn_sibling != NULL)
5028 PRINTSOME(line3, depth, " | ", 0, 0);
5029 else
5030 PRINTSOME(line3, depth, " ", 0, 0);
5031
5032 if (node->wn_byte == NUL)
5033 {
5034 msg(line1);
5035 msg(line2);
5036 msg(line3);
5037 }
5038
5039 /* do the children */
5040 if (node->wn_byte != NUL && node->wn_child != NULL)
5041 spell_print_node(node->wn_child, depth + 1);
5042
5043 /* do the siblings */
5044 if (node->wn_sibling != NULL)
5045 {
5046 /* get rid of all parent details except | */
5047 STRCPY(line1, line3);
5048 STRCPY(line2, line3);
5049 spell_print_node(node->wn_sibling, depth);
5050 }
5051 }
5052}
5053
5054 static void
5055spell_print_tree(wordnode_T *root)
5056{
5057 if (root != NULL)
5058 {
5059 /* Clear the "wn_u1.index" fields, used to remember what has been
5060 * done. */
5061 spell_clear_flags(root);
5062
5063 /* Recursively print the tree. */
5064 spell_print_node(root, 0);
5065 }
5066}
5067#endif /* SPELL_PRINTTREE */
5068
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005069/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005070 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00005071 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005072 */
5073 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005074spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005075 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005076 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005077{
5078 FILE *fd;
5079 afffile_T *aff;
5080 char_u rline[MAXLINELEN];
5081 char_u *line;
5082 char_u *pc = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005083#define MAXITEMCNT 30
Bram Moolenaar8db73182005-06-17 21:51:16 +00005084 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005085 int itemcnt;
5086 char_u *p;
5087 int lnum = 0;
5088 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005089 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005090 int aff_todo = 0;
5091 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005092 char_u *low = NULL;
5093 char_u *fol = NULL;
5094 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005095 int do_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005096 int do_repsal;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005097 int do_sal;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005098 int do_mapline;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005099 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005100 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005101 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005102 int compminlen = 0; /* COMPOUNDMIN value */
5103 int compsylmax = 0; /* COMPOUNDSYLMAX value */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005104 int compoptions = 0; /* COMP_ flags */
5105 int compmax = 0; /* COMPOUNDWORDMAX value */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005106 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDRULE
Bram Moolenaar6de68532005-08-24 22:08:48 +00005107 concatenated */
5108 char_u *midword = NULL; /* MIDWORD value */
5109 char_u *syllable = NULL; /* SYLLABLE value */
5110 char_u *sofofrom = NULL; /* SOFOFROM value */
5111 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005112
Bram Moolenaar51485f02005-06-04 21:55:20 +00005113 /*
5114 * Open the file.
5115 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005116 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005117 if (fd == NULL)
5118 {
5119 EMSG2(_(e_notopen), fname);
5120 return NULL;
5121 }
5122
Bram Moolenaar4770d092006-01-12 23:22:24 +00005123 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
5124 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005125
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005126 /* Only do REP lines when not done in another .aff file already. */
5127 do_rep = spin->si_rep.ga_len == 0;
5128
Bram Moolenaar4770d092006-01-12 23:22:24 +00005129 /* Only do REPSAL lines when not done in another .aff file already. */
5130 do_repsal = spin->si_repsal.ga_len == 0;
5131
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005132 /* Only do SAL lines when not done in another .aff file already. */
5133 do_sal = spin->si_sal.ga_len == 0;
5134
5135 /* Only do MAP lines when not done in another .aff file already. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00005136 do_mapline = spin->si_map.ga_len == 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005137
Bram Moolenaar51485f02005-06-04 21:55:20 +00005138 /*
5139 * Allocate and init the afffile_T structure.
5140 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005141 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005142 if (aff == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005143 {
5144 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005145 return NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005146 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005147 hash_init(&aff->af_pref);
5148 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005149 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005150
5151 /*
5152 * Read all the lines in the file one by one.
5153 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005154 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005155 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005156 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005157 ++lnum;
5158
5159 /* Skip comment lines. */
5160 if (*rline == '#')
5161 continue;
5162
5163 /* Convert from "SET" to 'encoding' when needed. */
5164 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005165#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005166 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005167 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005168 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005169 if (pc == NULL)
5170 {
5171 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5172 fname, lnum, rline);
5173 continue;
5174 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005175 line = pc;
5176 }
5177 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005178#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005179 {
5180 pc = NULL;
5181 line = rline;
5182 }
5183
5184 /* Split the line up in white separated items. Put a NUL after each
5185 * item. */
5186 itemcnt = 0;
5187 for (p = line; ; )
5188 {
5189 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
5190 ++p;
5191 if (*p == NUL)
5192 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00005193 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005194 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005195 items[itemcnt++] = p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005196 /* A few items have arbitrary text argument, don't split them. */
5197 if (itemcnt == 2 && spell_info_item(items[0]))
5198 while (*p >= ' ' || *p == TAB) /* skip until CR/NL */
5199 ++p;
5200 else
5201 while (*p > ' ') /* skip until white space or CR/NL */
5202 ++p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005203 if (*p == NUL)
5204 break;
5205 *p++ = NUL;
5206 }
5207
5208 /* Handle non-empty lines. */
5209 if (itemcnt > 0)
5210 {
5211 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
5212 && aff->af_enc == NULL)
5213 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005214#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005215 /* Setup for conversion from "ENC" to 'encoding'. */
5216 aff->af_enc = enc_canonize(items[1]);
5217 if (aff->af_enc != NULL && !spin->si_ascii
5218 && convert_setup(&spin->si_conv, aff->af_enc,
5219 p_enc) == FAIL)
5220 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
5221 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005222 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005223#else
5224 smsg((char_u *)_("Conversion in %s not supported"), fname);
5225#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005226 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005227 else if (STRCMP(items[0], "FLAG") == 0 && itemcnt == 2
5228 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005229 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005230 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005231 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005232 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005233 aff->af_flagtype = AFT_NUM;
5234 else if (STRCMP(items[1], "caplong") == 0)
5235 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005236 else
5237 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
5238 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00005239 if (aff->af_rare != 0
5240 || aff->af_keepcase != 0
5241 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005242 || aff->af_needaffix != 0
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005243 || aff->af_circumfix != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005244 || aff->af_needcomp != 0
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005245 || aff->af_comproot != 0
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005246 || aff->af_nosuggest != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005247 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00005248 || aff->af_suff.ht_used > 0
5249 || aff->af_pref.ht_used > 0)
5250 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
5251 fname, lnum, items[1]);
5252 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005253 else if (spell_info_item(items[0]))
5254 {
5255 p = (char_u *)getroom(spin,
5256 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
5257 + STRLEN(items[0])
5258 + STRLEN(items[1]) + 3, FALSE);
5259 if (p != NULL)
5260 {
5261 if (spin->si_info != NULL)
5262 {
5263 STRCPY(p, spin->si_info);
5264 STRCAT(p, "\n");
5265 }
5266 STRCAT(p, items[0]);
5267 STRCAT(p, " ");
5268 STRCAT(p, items[1]);
5269 spin->si_info = p;
5270 }
5271 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005272 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2
5273 && midword == NULL)
5274 {
5275 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005276 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005277 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005279 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005280 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005281 /* TODO: remove "RAR" later */
5282 else if ((STRCMP(items[0], "RAR") == 0
5283 || STRCMP(items[0], "RARE") == 0) && itemcnt == 2
5284 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005285 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005286 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005287 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005288 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005289 /* TODO: remove "KEP" later */
5290 else if ((STRCMP(items[0], "KEP") == 0
5291 || STRCMP(items[0], "KEEPCASE") == 0) && itemcnt == 2
5292 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005293 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005294 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005295 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005296 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005297 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
5298 && aff->af_bad == 0)
5299 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005300 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5301 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005302 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005303 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
5304 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005305 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005306 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5307 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005308 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005309 else if (STRCMP(items[0], "CIRCUMFIX") == 0 && itemcnt == 2
5310 && aff->af_circumfix == 0)
5311 {
5312 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
5313 fname, lnum);
5314 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005315 else if (STRCMP(items[0], "NOSUGGEST") == 0 && itemcnt == 2
5316 && aff->af_nosuggest == 0)
5317 {
5318 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
5319 fname, lnum);
5320 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005321 else if (STRCMP(items[0], "NEEDCOMPOUND") == 0 && itemcnt == 2
5322 && aff->af_needcomp == 0)
5323 {
5324 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5325 fname, lnum);
5326 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005327 else if (STRCMP(items[0], "COMPOUNDROOT") == 0 && itemcnt == 2
5328 && aff->af_comproot == 0)
5329 {
5330 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
5331 fname, lnum);
5332 }
5333 else if (STRCMP(items[0], "COMPOUNDFORBIDFLAG") == 0
5334 && itemcnt == 2 && aff->af_compforbid == 0)
5335 {
5336 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
5337 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005338 if (aff->af_pref.ht_used > 0)
5339 smsg((char_u *)_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
5340 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005341 }
5342 else if (STRCMP(items[0], "COMPOUNDPERMITFLAG") == 0
5343 && itemcnt == 2 && aff->af_comppermit == 0)
5344 {
5345 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
5346 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005347 if (aff->af_pref.ht_used > 0)
5348 smsg((char_u *)_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
5349 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005350 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005351 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005352 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005353 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005354 /* Turn flag "c" into COMPOUNDRULE compatible string "c+",
Bram Moolenaar6de68532005-08-24 22:08:48 +00005355 * "Na" into "Na+", "1234" into "1234+". */
5356 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005357 if (p != NULL)
5358 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005359 STRCPY(p, items[1]);
5360 STRCAT(p, "+");
5361 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005362 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005363 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005364 else if (STRCMP(items[0], "COMPOUNDRULE") == 0 && itemcnt == 2)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005365 {
5366 /* Concatenate this string to previously defined ones, using a
5367 * slash to separate them. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005368 l = (int)STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005369 if (compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005370 l += (int)STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005371 p = getroom(spin, l, FALSE);
5372 if (p != NULL)
5373 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005374 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005375 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005376 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005377 STRCAT(p, "/");
5378 }
5379 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005380 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005381 }
5382 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005383 else if (STRCMP(items[0], "COMPOUNDWORDMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005384 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005385 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005386 compmax = atoi((char *)items[1]);
5387 if (compmax == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005388 smsg((char_u *)_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar5195e452005-08-19 20:32:47 +00005389 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005390 }
5391 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005392 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005393 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005394 compminlen = atoi((char *)items[1]);
5395 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005396 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5397 fname, lnum, items[1]);
5398 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005399 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005400 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005401 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005402 compsylmax = atoi((char *)items[1]);
5403 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005404 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5405 fname, lnum, items[1]);
5406 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005407 else if (STRCMP(items[0], "CHECKCOMPOUNDDUP") == 0 && itemcnt == 1)
5408 {
5409 compoptions |= COMP_CHECKDUP;
5410 }
5411 else if (STRCMP(items[0], "CHECKCOMPOUNDREP") == 0 && itemcnt == 1)
5412 {
5413 compoptions |= COMP_CHECKREP;
5414 }
5415 else if (STRCMP(items[0], "CHECKCOMPOUNDCASE") == 0 && itemcnt == 1)
5416 {
5417 compoptions |= COMP_CHECKCASE;
5418 }
5419 else if (STRCMP(items[0], "CHECKCOMPOUNDTRIPLE") == 0
5420 && itemcnt == 1)
5421 {
5422 compoptions |= COMP_CHECKTRIPLE;
5423 }
5424 else if (STRCMP(items[0], "CHECKCOMPOUNDPATTERN") == 0
5425 && itemcnt == 2)
5426 {
5427 if (atoi((char *)items[1]) == 0)
5428 smsg((char_u *)_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
5429 fname, lnum, items[1]);
5430 }
5431 else if (STRCMP(items[0], "CHECKCOMPOUNDPATTERN") == 0
5432 && itemcnt == 3)
5433 {
5434 garray_T *gap = &spin->si_comppat;
5435 int i;
5436
5437 /* Only add the couple if it isn't already there. */
5438 for (i = 0; i < gap->ga_len - 1; i += 2)
5439 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
5440 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
5441 items[2]) == 0)
5442 break;
5443 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
5444 {
5445 ((char_u **)(gap->ga_data))[gap->ga_len++]
5446 = getroom_save(spin, items[1]);
5447 ((char_u **)(gap->ga_data))[gap->ga_len++]
5448 = getroom_save(spin, items[2]);
5449 }
5450 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005451 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005452 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005453 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005454 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005455 }
Bram Moolenaar78622822005-08-23 21:00:13 +00005456 else if (STRCMP(items[0], "NOBREAK") == 0 && itemcnt == 1)
5457 {
5458 spin->si_nobreak = TRUE;
5459 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005460 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
5461 {
5462 spin->si_nosplitsugs = TRUE;
5463 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005464 else if (STRCMP(items[0], "NOSUGFILE") == 0 && itemcnt == 1)
5465 {
5466 spin->si_nosugfile = TRUE;
5467 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005468 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
5469 {
5470 aff->af_pfxpostpone = TRUE;
5471 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005472 else if ((STRCMP(items[0], "PFX") == 0
5473 || STRCMP(items[0], "SFX") == 0)
5474 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005475 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005476 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005477 int lasti = 4;
5478 char_u key[AH_KEY_LEN];
5479
5480 if (*items[0] == 'P')
5481 tp = &aff->af_pref;
5482 else
5483 tp = &aff->af_suff;
5484
5485 /* Myspell allows the same affix name to be used multiple
5486 * times. The affix files that do this have an undocumented
5487 * "S" flag on all but the last block, thus we check for that
5488 * and store it in ah_follows. */
5489 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5490 hi = hash_find(tp, key);
5491 if (!HASHITEM_EMPTY(hi))
5492 {
5493 cur_aff = HI2AH(hi);
5494 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5495 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5496 fname, lnum, items[1]);
5497 if (!cur_aff->ah_follows)
5498 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5499 fname, lnum, items[1]);
5500 }
5501 else
5502 {
5503 /* New affix letter. */
5504 cur_aff = (affheader_T *)getroom(spin,
5505 sizeof(affheader_T), TRUE);
5506 if (cur_aff == NULL)
5507 break;
5508 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5509 fname, lnum);
5510 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5511 break;
5512 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00005513 || cur_aff->ah_flag == aff->af_rare
5514 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005515 || cur_aff->ah_flag == aff->af_needaffix
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005516 || cur_aff->ah_flag == aff->af_circumfix
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005517 || cur_aff->ah_flag == aff->af_nosuggest
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005518 || cur_aff->ah_flag == aff->af_needcomp
5519 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005520 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 +00005521 fname, lnum, items[1]);
5522 STRCPY(cur_aff->ah_key, items[1]);
5523 hash_add(tp, cur_aff->ah_key);
5524
5525 cur_aff->ah_combine = (*items[2] == 'Y');
5526 }
5527
5528 /* Check for the "S" flag, which apparently means that another
5529 * block with the same affix name is following. */
5530 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5531 {
5532 ++lasti;
5533 cur_aff->ah_follows = TRUE;
5534 }
5535 else
5536 cur_aff->ah_follows = FALSE;
5537
Bram Moolenaar8db73182005-06-17 21:51:16 +00005538 /* Myspell allows extra text after the item, but that might
5539 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00005540 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005541 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005542
Bram Moolenaar95529562005-08-25 21:21:38 +00005543 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005544 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5545 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005546
Bram Moolenaar95529562005-08-25 21:21:38 +00005547 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005548 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005549 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005550 {
5551 /* Use a new number in the .spl file later, to be able
5552 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005553 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005554 cur_aff->ah_newID = ++spin->si_newprefID;
5555
5556 /* We only really use ah_newID if the prefix is
5557 * postponed. We know that only after handling all
5558 * the items. */
5559 did_postpone_prefix = FALSE;
5560 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005561 else
5562 /* Did use the ID in a previous block. */
5563 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005564 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005565
Bram Moolenaar51485f02005-06-04 21:55:20 +00005566 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005567 }
5568 else if ((STRCMP(items[0], "PFX") == 0
5569 || STRCMP(items[0], "SFX") == 0)
5570 && aff_todo > 0
5571 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005572 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005573 {
5574 affentry_T *aff_entry;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005575 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005576 int lasti = 5;
5577
Bram Moolenaar8db73182005-06-17 21:51:16 +00005578 /* Myspell allows extra text after the item, but that might
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005579 * mean mistakes go unnoticed. Require a comment-starter.
5580 * Hunspell uses a "-" item. */
5581 if (itemcnt > lasti && *items[lasti] != '#'
5582 && (STRCMP(items[lasti], "-") != 0
5583 || itemcnt != lasti + 1))
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005584 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005585
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005586 /* New item for an affix letter. */
5587 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005588 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005589 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005590 if (aff_entry == NULL)
5591 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005592
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005593 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005594 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005595 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005596 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005597 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005598
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005599 /* Recognize flags on the affix: abcd/XYZ */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005600 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
5601 if (aff_entry->ae_flags != NULL)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005602 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005603 *aff_entry->ae_flags++ = NUL;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005604 aff_process_flags(aff, aff_entry);
5605 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005606 }
5607
Bram Moolenaar51485f02005-06-04 21:55:20 +00005608 /* Don't use an affix entry with non-ASCII characters when
5609 * "spin->si_ascii" is TRUE. */
5610 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005611 || has_non_ascii(aff_entry->ae_add)))
5612 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00005613 aff_entry->ae_next = cur_aff->ah_first;
5614 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005615
5616 if (STRCMP(items[4], ".") != 0)
5617 {
5618 char_u buf[MAXLINELEN];
5619
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005620 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005621 if (*items[0] == 'P')
5622 sprintf((char *)buf, "^%s", items[4]);
5623 else
5624 sprintf((char *)buf, "%s$", items[4]);
5625 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005626 RE_MAGIC + RE_STRING + RE_STRICT);
5627 if (aff_entry->ae_prog == NULL)
5628 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5629 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005630 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005631
5632 /* For postponed prefixes we need an entry in si_prefcond
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005633 * for the condition. Use an existing one if possible.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005634 * Can't be done for an affix with flags, ignoring
5635 * COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005636 if (*items[0] == 'P' && aff->af_pfxpostpone
5637 && aff_entry->ae_flags == NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005638 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005639 /* When the chop string is one lower-case letter and
5640 * the add string ends in the upper-case letter we set
5641 * the "upper" flag, clear "ae_chop" and remove the
5642 * letters from "ae_add". The condition must either
5643 * be empty or start with the same letter. */
5644 if (aff_entry->ae_chop != NULL
5645 && aff_entry->ae_add != NULL
5646#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005647 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00005648 aff_entry->ae_chop)] == NUL
5649#else
5650 && aff_entry->ae_chop[1] == NUL
5651#endif
5652 )
5653 {
5654 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005655
Bram Moolenaar53805d12005-08-01 07:08:33 +00005656 c = PTR2CHAR(aff_entry->ae_chop);
5657 c_up = SPELL_TOUPPER(c);
5658 if (c_up != c
5659 && (aff_entry->ae_cond == NULL
5660 || PTR2CHAR(aff_entry->ae_cond) == c))
5661 {
5662 p = aff_entry->ae_add
5663 + STRLEN(aff_entry->ae_add);
5664 mb_ptr_back(aff_entry->ae_add, p);
5665 if (PTR2CHAR(p) == c_up)
5666 {
5667 upper = TRUE;
5668 aff_entry->ae_chop = NULL;
5669 *p = NUL;
5670
5671 /* The condition is matched with the
5672 * actual word, thus must check for the
5673 * upper-case letter. */
5674 if (aff_entry->ae_cond != NULL)
5675 {
5676 char_u buf[MAXLINELEN];
5677#ifdef FEAT_MBYTE
5678 if (has_mbyte)
5679 {
5680 onecap_copy(items[4], buf, TRUE);
5681 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005682 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005683 }
5684 else
5685#endif
5686 *aff_entry->ae_cond = c_up;
5687 if (aff_entry->ae_cond != NULL)
5688 {
5689 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005690 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005691 vim_free(aff_entry->ae_prog);
5692 aff_entry->ae_prog = vim_regcomp(
5693 buf, RE_MAGIC + RE_STRING);
5694 }
5695 }
5696 }
5697 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005698 }
5699
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005700 if (aff_entry->ae_chop == NULL
5701 && aff_entry->ae_flags == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005702 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005703 int idx;
5704 char_u **pp;
5705 int n;
5706
Bram Moolenaar6de68532005-08-24 22:08:48 +00005707 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005708 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5709 --idx)
5710 {
5711 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5712 if (str_equal(p, aff_entry->ae_cond))
5713 break;
5714 }
5715 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5716 {
5717 /* Not found, add a new condition. */
5718 idx = spin->si_prefcond.ga_len++;
5719 pp = ((char_u **)spin->si_prefcond.ga_data)
5720 + idx;
5721 if (aff_entry->ae_cond == NULL)
5722 *pp = NULL;
5723 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005724 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005725 aff_entry->ae_cond);
5726 }
5727
5728 /* Add the prefix to the prefix tree. */
5729 if (aff_entry->ae_add == NULL)
5730 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005731 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005732 p = aff_entry->ae_add;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005733
Bram Moolenaar53805d12005-08-01 07:08:33 +00005734 /* PFX_FLAGS is a negative number, so that
5735 * tree_add_word() knows this is the prefix tree. */
5736 n = PFX_FLAGS;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005737 if (!cur_aff->ah_combine)
5738 n |= WFP_NC;
5739 if (upper)
5740 n |= WFP_UP;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005741 if (aff_entry->ae_comppermit)
5742 n |= WFP_COMPPERMIT;
5743 if (aff_entry->ae_compforbid)
5744 n |= WFP_COMPFORBID;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005745 tree_add_word(spin, p, spin->si_prefroot, n,
5746 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005747 did_postpone_prefix = TRUE;
5748 }
5749
5750 /* Didn't actually use ah_newID, backup si_newprefID. */
5751 if (aff_todo == 0 && !did_postpone_prefix)
5752 {
5753 --spin->si_newprefID;
5754 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005755 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005756 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005757 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005758 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005759 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2
5760 && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005761 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005762 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005763 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005764 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2
5765 && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005766 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005767 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005768 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005769 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2
5770 && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005771 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005772 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005773 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005774 else if ((STRCMP(items[0], "REP") == 0
5775 || STRCMP(items[0], "REPSAL") == 0)
5776 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005777 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005778 /* Ignore REP/REPSAL count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005779 if (!isdigit(*items[1]))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005780 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005781 fname, lnum);
5782 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005783 else if ((STRCMP(items[0], "REP") == 0
5784 || STRCMP(items[0], "REPSAL") == 0)
5785 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005786 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005787 /* REP/REPSAL item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005788 /* Myspell ignores extra arguments, we require it starts with
5789 * # to detect mistakes. */
5790 if (itemcnt > 3 && items[3][0] != '#')
5791 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005792 if (items[0][3] == 'S' ? do_repsal : do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005793 {
5794 /* Replace underscore with space (can't include a space
5795 * directly). */
5796 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5797 if (*p == '_')
5798 *p = ' ';
5799 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5800 if (*p == '_')
5801 *p = ' ';
Bram Moolenaar4770d092006-01-12 23:22:24 +00005802 add_fromto(spin, items[0][3] == 'S'
5803 ? &spin->si_repsal
5804 : &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005805 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005806 }
5807 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
5808 {
5809 /* MAP item or count */
5810 if (!found_map)
5811 {
5812 /* First line contains the count. */
5813 found_map = TRUE;
5814 if (!isdigit(*items[1]))
5815 smsg((char_u *)_("Expected MAP count in %s line %d"),
5816 fname, lnum);
5817 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005818 else if (do_mapline)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005819 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005820 int c;
5821
5822 /* Check that every character appears only once. */
5823 for (p = items[1]; *p != NUL; )
5824 {
5825#ifdef FEAT_MBYTE
5826 c = mb_ptr2char_adv(&p);
5827#else
5828 c = *p++;
5829#endif
5830 if ((spin->si_map.ga_len > 0
5831 && vim_strchr(spin->si_map.ga_data, c)
5832 != NULL)
5833 || vim_strchr(p, c) != NULL)
5834 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5835 fname, lnum);
5836 }
5837
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005838 /* We simply concatenate all the MAP strings, separated by
5839 * slashes. */
5840 ga_concat(&spin->si_map, items[1]);
5841 ga_append(&spin->si_map, '/');
5842 }
5843 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005844 /* Accept "SAL from to" and "SAL from to # comment". */
5845 else if (STRCMP(items[0], "SAL") == 0
5846 && (itemcnt == 3 || (itemcnt > 3 && items[3][0] == '#')))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005847 {
5848 if (do_sal)
5849 {
5850 /* SAL item (sounds-a-like)
5851 * Either one of the known keys or a from-to pair. */
5852 if (STRCMP(items[1], "followup") == 0)
5853 spin->si_followup = sal_to_bool(items[2]);
5854 else if (STRCMP(items[1], "collapse_result") == 0)
5855 spin->si_collapse = sal_to_bool(items[2]);
5856 else if (STRCMP(items[1], "remove_accents") == 0)
5857 spin->si_rem_accents = sal_to_bool(items[2]);
5858 else
5859 /* when "to" is "_" it means empty */
5860 add_fromto(spin, &spin->si_sal, items[1],
5861 STRCMP(items[2], "_") == 0 ? (char_u *)""
5862 : items[2]);
5863 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005864 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005865 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005866 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005867 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005868 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005869 }
5870 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005871 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005872 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005873 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005874 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005875 else if (STRCMP(items[0], "COMMON") == 0)
5876 {
5877 int i;
5878
5879 for (i = 1; i < itemcnt; ++i)
5880 {
5881 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
5882 items[i])))
5883 {
5884 p = vim_strsave(items[i]);
5885 if (p == NULL)
5886 break;
5887 hash_add(&spin->si_commonwords, p);
5888 }
5889 }
5890 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005891 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005892 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005893 fname, lnum, items[0]);
5894 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005895 }
5896
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005897 if (fol != NULL || low != NULL || upp != NULL)
5898 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005899 if (spin->si_clear_chartab)
5900 {
5901 /* Clear the char type tables, don't want to use any of the
5902 * currently used spell properties. */
5903 init_spell_chartab();
5904 spin->si_clear_chartab = FALSE;
5905 }
5906
Bram Moolenaar3982c542005-06-08 21:56:31 +00005907 /*
5908 * Don't write a word table for an ASCII file, so that we don't check
5909 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005910 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00005911 * mb_get_class(), the list of chars in the file will be incomplete.
5912 */
5913 if (!spin->si_ascii
5914#ifdef FEAT_MBYTE
5915 && !enc_utf8
5916#endif
5917 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005918 {
5919 if (fol == NULL || low == NULL || upp == NULL)
5920 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
5921 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00005922 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005923 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005924
5925 vim_free(fol);
5926 vim_free(low);
5927 vim_free(upp);
5928 }
5929
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005930 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005931 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005932 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005933 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
Bram Moolenaar6de68532005-08-24 22:08:48 +00005934 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005935 }
5936
Bram Moolenaar6de68532005-08-24 22:08:48 +00005937 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005938 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005939 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
5940 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005941 }
5942
Bram Moolenaar6de68532005-08-24 22:08:48 +00005943 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005944 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005945 if (syllable == NULL)
5946 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
5947 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
5948 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005949 }
5950
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005951 if (compoptions != 0)
5952 {
5953 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
5954 spin->si_compoptions |= compoptions;
5955 }
5956
Bram Moolenaar6de68532005-08-24 22:08:48 +00005957 if (compflags != NULL)
5958 process_compflags(spin, aff, compflags);
5959
5960 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005961 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005962 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005963 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005964 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005965 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005966 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005967 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005968 MSG(_("Too many posponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005969 }
5970
Bram Moolenaar6de68532005-08-24 22:08:48 +00005971 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005972 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005973 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
5974 spin->si_syllable = syllable;
5975 }
5976
5977 if (sofofrom != NULL || sofoto != NULL)
5978 {
5979 if (sofofrom == NULL || sofoto == NULL)
5980 smsg((char_u *)_("Missing SOFO%s line in %s"),
5981 sofofrom == NULL ? "FROM" : "TO", fname);
5982 else if (spin->si_sal.ga_len > 0)
5983 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005984 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005985 {
5986 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
5987 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
5988 spin->si_sofofr = sofofrom;
5989 spin->si_sofoto = sofoto;
5990 }
5991 }
5992
5993 if (midword != NULL)
5994 {
5995 aff_check_string(spin->si_midword, midword, "MIDWORD");
5996 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005997 }
5998
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005999 vim_free(pc);
6000 fclose(fd);
6001 return aff;
6002}
6003
6004/*
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006005 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
6006 * ae_flags to ae_comppermit and ae_compforbid.
6007 */
6008 static void
6009aff_process_flags(affile, entry)
6010 afffile_T *affile;
6011 affentry_T *entry;
6012{
6013 char_u *p;
6014 char_u *prevp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006015 unsigned flag;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006016
6017 if (entry->ae_flags != NULL
6018 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
6019 {
6020 for (p = entry->ae_flags; *p != NUL; )
6021 {
6022 prevp = p;
6023 flag = get_affitem(affile->af_flagtype, &p);
6024 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
6025 {
6026 mch_memmove(prevp, p, STRLEN(p) + 1);
6027 p = prevp;
6028 if (flag == affile->af_comppermit)
6029 entry->ae_comppermit = TRUE;
6030 else
6031 entry->ae_compforbid = TRUE;
6032 }
6033 if (affile->af_flagtype == AFT_NUM && *p == ',')
6034 ++p;
6035 }
6036 if (*entry->ae_flags == NUL)
6037 entry->ae_flags = NULL; /* nothing left */
6038 }
6039}
6040
6041/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006042 * Return TRUE if "s" is the name of an info item in the affix file.
6043 */
6044 static int
6045spell_info_item(s)
6046 char_u *s;
6047{
6048 return STRCMP(s, "NAME") == 0
6049 || STRCMP(s, "HOME") == 0
6050 || STRCMP(s, "VERSION") == 0
6051 || STRCMP(s, "AUTHOR") == 0
6052 || STRCMP(s, "EMAIL") == 0
6053 || STRCMP(s, "COPYRIGHT") == 0;
6054}
6055
6056/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006057 * Turn an affix flag name into a number, according to the FLAG type.
6058 * returns zero for failure.
6059 */
6060 static unsigned
6061affitem2flag(flagtype, item, fname, lnum)
6062 int flagtype;
6063 char_u *item;
6064 char_u *fname;
6065 int lnum;
6066{
6067 unsigned res;
6068 char_u *p = item;
6069
6070 res = get_affitem(flagtype, &p);
6071 if (res == 0)
6072 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006073 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006074 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
6075 fname, lnum, item);
6076 else
6077 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
6078 fname, lnum, item);
6079 }
6080 if (*p != NUL)
6081 {
6082 smsg((char_u *)_(e_affname), fname, lnum, item);
6083 return 0;
6084 }
6085
6086 return res;
6087}
6088
6089/*
6090 * Get one affix name from "*pp" and advance the pointer.
6091 * Returns zero for an error, still advances the pointer then.
6092 */
6093 static unsigned
6094get_affitem(flagtype, pp)
6095 int flagtype;
6096 char_u **pp;
6097{
6098 int res;
6099
Bram Moolenaar95529562005-08-25 21:21:38 +00006100 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006101 {
6102 if (!VIM_ISDIGIT(**pp))
6103 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006104 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006105 return 0;
6106 }
6107 res = getdigits(pp);
6108 }
6109 else
6110 {
6111#ifdef FEAT_MBYTE
6112 res = mb_ptr2char_adv(pp);
6113#else
6114 res = *(*pp)++;
6115#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006116 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00006117 && res >= 'A' && res <= 'Z'))
6118 {
6119 if (**pp == NUL)
6120 return 0;
6121#ifdef FEAT_MBYTE
6122 res = mb_ptr2char_adv(pp) + (res << 16);
6123#else
6124 res = *(*pp)++ + (res << 16);
6125#endif
6126 }
6127 }
6128 return res;
6129}
6130
6131/*
6132 * Process the "compflags" string used in an affix file and append it to
6133 * spin->si_compflags.
6134 * The processing involves changing the affix names to ID numbers, so that
6135 * they fit in one byte.
6136 */
6137 static void
6138process_compflags(spin, aff, compflags)
6139 spellinfo_T *spin;
6140 afffile_T *aff;
6141 char_u *compflags;
6142{
6143 char_u *p;
6144 char_u *prevp;
6145 unsigned flag;
6146 compitem_T *ci;
6147 int id;
6148 int len;
6149 char_u *tp;
6150 char_u key[AH_KEY_LEN];
6151 hashitem_T *hi;
6152
6153 /* Make room for the old and the new compflags, concatenated with a / in
6154 * between. Processing it makes it shorter, but we don't know by how
6155 * much, thus allocate the maximum. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006156 len = (int)STRLEN(compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006157 if (spin->si_compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006158 len += (int)STRLEN(spin->si_compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006159 p = getroom(spin, len, FALSE);
6160 if (p == NULL)
6161 return;
6162 if (spin->si_compflags != NULL)
6163 {
6164 STRCPY(p, spin->si_compflags);
6165 STRCAT(p, "/");
6166 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00006167 spin->si_compflags = p;
6168 tp = p + STRLEN(p);
6169
6170 for (p = compflags; *p != NUL; )
6171 {
6172 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
6173 /* Copy non-flag characters directly. */
6174 *tp++ = *p++;
6175 else
6176 {
6177 /* First get the flag number, also checks validity. */
6178 prevp = p;
6179 flag = get_affitem(aff->af_flagtype, &p);
6180 if (flag != 0)
6181 {
6182 /* Find the flag in the hashtable. If it was used before, use
6183 * the existing ID. Otherwise add a new entry. */
6184 vim_strncpy(key, prevp, p - prevp);
6185 hi = hash_find(&aff->af_comp, key);
6186 if (!HASHITEM_EMPTY(hi))
6187 id = HI2CI(hi)->ci_newID;
6188 else
6189 {
6190 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
6191 if (ci == NULL)
6192 break;
6193 STRCPY(ci->ci_key, key);
6194 ci->ci_flag = flag;
6195 /* Avoid using a flag ID that has a special meaning in a
6196 * regexp (also inside []). */
6197 do
6198 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006199 check_renumber(spin);
6200 id = spin->si_newcompID--;
6201 } while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006202 ci->ci_newID = id;
6203 hash_add(&aff->af_comp, ci->ci_key);
6204 }
6205 *tp++ = id;
6206 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006207 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006208 ++p;
6209 }
6210 }
6211
6212 *tp = NUL;
6213}
6214
6215/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006216 * Check that the new IDs for postponed affixes and compounding don't overrun
6217 * each other. We have almost 255 available, but start at 0-127 to avoid
6218 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
6219 * When that is used up an error message is given.
6220 */
6221 static void
6222check_renumber(spin)
6223 spellinfo_T *spin;
6224{
6225 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
6226 {
6227 spin->si_newprefID = 127;
6228 spin->si_newcompID = 255;
6229 }
6230}
6231
6232/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006233 * Return TRUE if flag "flag" appears in affix list "afflist".
6234 */
6235 static int
6236flag_in_afflist(flagtype, afflist, flag)
6237 int flagtype;
6238 char_u *afflist;
6239 unsigned flag;
6240{
6241 char_u *p;
6242 unsigned n;
6243
6244 switch (flagtype)
6245 {
6246 case AFT_CHAR:
6247 return vim_strchr(afflist, flag) != NULL;
6248
Bram Moolenaar95529562005-08-25 21:21:38 +00006249 case AFT_CAPLONG:
6250 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006251 for (p = afflist; *p != NUL; )
6252 {
6253#ifdef FEAT_MBYTE
6254 n = mb_ptr2char_adv(&p);
6255#else
6256 n = *p++;
6257#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006258 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00006259 && *p != NUL)
6260#ifdef FEAT_MBYTE
6261 n = mb_ptr2char_adv(&p) + (n << 16);
6262#else
6263 n = *p++ + (n << 16);
6264#endif
6265 if (n == flag)
6266 return TRUE;
6267 }
6268 break;
6269
Bram Moolenaar95529562005-08-25 21:21:38 +00006270 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006271 for (p = afflist; *p != NUL; )
6272 {
6273 n = getdigits(&p);
6274 if (n == flag)
6275 return TRUE;
6276 if (*p != NUL) /* skip over comma */
6277 ++p;
6278 }
6279 break;
6280 }
6281 return FALSE;
6282}
6283
6284/*
6285 * Give a warning when "spinval" and "affval" numbers are set and not the same.
6286 */
6287 static void
6288aff_check_number(spinval, affval, name)
6289 int spinval;
6290 int affval;
6291 char *name;
6292{
6293 if (spinval != 0 && spinval != affval)
6294 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6295}
6296
6297/*
6298 * Give a warning when "spinval" and "affval" strings are set and not the same.
6299 */
6300 static void
6301aff_check_string(spinval, affval, name)
6302 char_u *spinval;
6303 char_u *affval;
6304 char *name;
6305{
6306 if (spinval != NULL && STRCMP(spinval, affval) != 0)
6307 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6308}
6309
6310/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006311 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
6312 * NULL as equal.
6313 */
6314 static int
6315str_equal(s1, s2)
6316 char_u *s1;
6317 char_u *s2;
6318{
6319 if (s1 == NULL || s2 == NULL)
6320 return s1 == s2;
6321 return STRCMP(s1, s2) == 0;
6322}
6323
6324/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006325 * Add a from-to item to "gap". Used for REP and SAL items.
6326 * They are stored case-folded.
6327 */
6328 static void
6329add_fromto(spin, gap, from, to)
6330 spellinfo_T *spin;
6331 garray_T *gap;
6332 char_u *from;
6333 char_u *to;
6334{
6335 fromto_T *ftp;
6336 char_u word[MAXWLEN];
6337
6338 if (ga_grow(gap, 1) == OK)
6339 {
6340 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006341 (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006342 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006343 (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006344 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006345 ++gap->ga_len;
6346 }
6347}
6348
6349/*
6350 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6351 */
6352 static int
6353sal_to_bool(s)
6354 char_u *s;
6355{
6356 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
6357}
6358
6359/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00006360 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6361 * When "s" is NULL FALSE is returned.
6362 */
6363 static int
6364has_non_ascii(s)
6365 char_u *s;
6366{
6367 char_u *p;
6368
6369 if (s != NULL)
6370 for (p = s; *p != NUL; ++p)
6371 if (*p >= 128)
6372 return TRUE;
6373 return FALSE;
6374}
6375
6376/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006377 * Free the structure filled by spell_read_aff().
6378 */
6379 static void
6380spell_free_aff(aff)
6381 afffile_T *aff;
6382{
6383 hashtab_T *ht;
6384 hashitem_T *hi;
6385 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006386 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006387 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006388
6389 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006390
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006391 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006392 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
6393 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006394 todo = (int)ht->ht_used;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006395 for (hi = ht->ht_array; todo > 0; ++hi)
6396 {
6397 if (!HASHITEM_EMPTY(hi))
6398 {
6399 --todo;
6400 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006401 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
6402 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006403 }
6404 }
6405 if (ht == &aff->af_suff)
6406 break;
6407 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006408
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006409 hash_clear(&aff->af_pref);
6410 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006411 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006412}
6413
6414/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006415 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006416 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006417 */
6418 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006419spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006420 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006421 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006422 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006423{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006424 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006425 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006426 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006427 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006428 char_u store_afflist[MAXWLEN];
6429 int pfxlen;
6430 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006431 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006432 char_u *pc;
6433 char_u *w;
6434 int l;
6435 hash_T hash;
6436 hashitem_T *hi;
6437 FILE *fd;
6438 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006439 int non_ascii = 0;
6440 int retval = OK;
6441 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006442 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006443 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006444
Bram Moolenaar51485f02005-06-04 21:55:20 +00006445 /*
6446 * Open the file.
6447 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006448 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006449 if (fd == NULL)
6450 {
6451 EMSG2(_(e_notopen), fname);
6452 return FAIL;
6453 }
6454
Bram Moolenaar51485f02005-06-04 21:55:20 +00006455 /* The hashtable is only used to detect duplicated words. */
6456 hash_init(&ht);
6457
Bram Moolenaar4770d092006-01-12 23:22:24 +00006458 vim_snprintf((char *)IObuff, IOSIZE,
6459 _("Reading dictionary file %s ..."), fname);
6460 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006461
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006462 /* start with a message for the first line */
6463 spin->si_msg_count = 999999;
6464
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006465 /* Read and ignore the first line: word count. */
6466 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006467 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006468 EMSG2(_("E760: No word count in %s"), fname);
6469
6470 /*
6471 * Read all the lines in the file one by one.
6472 * The words are converted to 'encoding' here, before being added to
6473 * the hashtable.
6474 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006475 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006476 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006477 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006478 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00006479 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006480 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006481
Bram Moolenaar51485f02005-06-04 21:55:20 +00006482 /* Remove CR, LF and white space from the end. White space halfway
6483 * the word is kept to allow e.g., "et al.". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006484 l = (int)STRLEN(line);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006485 while (l > 0 && line[l - 1] <= ' ')
6486 --l;
6487 if (l == 0)
6488 continue; /* empty line */
6489 line[l] = NUL;
6490
Bram Moolenaarb765d632005-06-07 21:00:02 +00006491#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006492 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006493 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006494 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006495 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006496 if (pc == NULL)
6497 {
6498 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6499 fname, lnum, line);
6500 continue;
6501 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006502 w = pc;
6503 }
6504 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006505#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006506 {
6507 pc = NULL;
6508 w = line;
6509 }
6510
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006511 /* Truncate the word at the "/", set "afflist" to what follows.
6512 * Replace "\/" by "/" and "\\" by "\". */
6513 afflist = NULL;
6514 for (p = w; *p != NUL; mb_ptr_adv(p))
6515 {
6516 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
6517 mch_memmove(p, p + 1, STRLEN(p));
6518 else if (*p == '/')
6519 {
6520 *p = NUL;
6521 afflist = p + 1;
6522 break;
6523 }
6524 }
6525
6526 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6527 if (spin->si_ascii && has_non_ascii(w))
6528 {
6529 ++non_ascii;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006530 vim_free(pc);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006531 continue;
6532 }
6533
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006534 /* This takes time, print a message every 10000 words. */
6535 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006536 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006537 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006538 vim_snprintf((char *)message, sizeof(message),
6539 _("line %6d, word %6d - %s"),
6540 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6541 msg_start();
6542 msg_puts_long_attr(message, 0);
6543 msg_clr_eos();
6544 msg_didout = FALSE;
6545 msg_col = 0;
6546 out_flush();
6547 }
6548
Bram Moolenaar51485f02005-06-04 21:55:20 +00006549 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006550 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006551 if (dw == NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006552 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006553 retval = FAIL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006554 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006555 break;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006556 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006557
Bram Moolenaar51485f02005-06-04 21:55:20 +00006558 hash = hash_hash(dw);
6559 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006560 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006561 {
6562 if (p_verbose > 0)
6563 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006564 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006565 else if (duplicate == 0)
6566 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6567 fname, lnum, dw);
6568 ++duplicate;
6569 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006570 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006571 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006572
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006573 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006574 store_afflist[0] = NUL;
6575 pfxlen = 0;
6576 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006577 if (afflist != NULL)
6578 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006579 /* Extract flags from the affix list. */
6580 flags |= get_affix_flags(affile, afflist);
6581
Bram Moolenaar6de68532005-08-24 22:08:48 +00006582 if (affile->af_needaffix != 0 && flag_in_afflist(
6583 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006584 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006585
6586 if (affile->af_pfxpostpone)
6587 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006588 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006589
Bram Moolenaar5195e452005-08-19 20:32:47 +00006590 if (spin->si_compflags != NULL)
6591 /* Need to store the list of compound flags with the word.
6592 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006593 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006594 }
6595
Bram Moolenaar51485f02005-06-04 21:55:20 +00006596 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006597 if (store_word(spin, dw, flags, spin->si_region,
6598 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006599 retval = FAIL;
6600
6601 if (afflist != NULL)
6602 {
6603 /* Find all matching suffixes and add the resulting words.
6604 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006605 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006606 &affile->af_suff, &affile->af_pref,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006607 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006608 retval = FAIL;
6609
6610 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006611 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006612 &affile->af_pref, NULL,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006613 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006614 retval = FAIL;
6615 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006616
6617 vim_free(pc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006618 }
6619
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006620 if (duplicate > 0)
6621 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006622 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006623 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6624 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006625 hash_clear(&ht);
6626
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006627 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006628 return retval;
6629}
6630
6631/*
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006632 * Check for affix flags in "afflist" that are turned into word flags.
6633 * Return WF_ flags.
6634 */
6635 static int
6636get_affix_flags(affile, afflist)
6637 afffile_T *affile;
6638 char_u *afflist;
6639{
6640 int flags = 0;
6641
6642 if (affile->af_keepcase != 0 && flag_in_afflist(
6643 affile->af_flagtype, afflist, affile->af_keepcase))
6644 flags |= WF_KEEPCAP | WF_FIXCAP;
6645 if (affile->af_rare != 0 && flag_in_afflist(
6646 affile->af_flagtype, afflist, affile->af_rare))
6647 flags |= WF_RARE;
6648 if (affile->af_bad != 0 && flag_in_afflist(
6649 affile->af_flagtype, afflist, affile->af_bad))
6650 flags |= WF_BANNED;
6651 if (affile->af_needcomp != 0 && flag_in_afflist(
6652 affile->af_flagtype, afflist, affile->af_needcomp))
6653 flags |= WF_NEEDCOMP;
6654 if (affile->af_comproot != 0 && flag_in_afflist(
6655 affile->af_flagtype, afflist, affile->af_comproot))
6656 flags |= WF_COMPROOT;
6657 if (affile->af_nosuggest != 0 && flag_in_afflist(
6658 affile->af_flagtype, afflist, affile->af_nosuggest))
6659 flags |= WF_NOSUGGEST;
6660 return flags;
6661}
6662
6663/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006664 * Get the list of prefix IDs from the affix list "afflist".
6665 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006666 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6667 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006668 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006669 static int
6670get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006671 afffile_T *affile;
6672 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006673 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006674{
6675 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006676 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006677 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006678 int id;
6679 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006680 hashitem_T *hi;
6681
Bram Moolenaar6de68532005-08-24 22:08:48 +00006682 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006683 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006684 prevp = p;
6685 if (get_affitem(affile->af_flagtype, &p) != 0)
6686 {
6687 /* A flag is a postponed prefix flag if it appears in "af_pref"
6688 * and it's ID is not zero. */
6689 vim_strncpy(key, prevp, p - prevp);
6690 hi = hash_find(&affile->af_pref, key);
6691 if (!HASHITEM_EMPTY(hi))
6692 {
6693 id = HI2AH(hi)->ah_newID;
6694 if (id != 0)
6695 store_afflist[cnt++] = id;
6696 }
6697 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006698 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006699 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006700 }
6701
Bram Moolenaar5195e452005-08-19 20:32:47 +00006702 store_afflist[cnt] = NUL;
6703 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006704}
6705
6706/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006707 * Get the list of compound IDs from the affix list "afflist" that are used
6708 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006709 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006710 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006711 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00006712get_compflags(affile, afflist, store_afflist)
6713 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006714 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006715 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006716{
6717 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006718 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006719 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006720 char_u key[AH_KEY_LEN];
6721 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006722
Bram Moolenaar6de68532005-08-24 22:08:48 +00006723 for (p = afflist; *p != NUL; )
6724 {
6725 prevp = p;
6726 if (get_affitem(affile->af_flagtype, &p) != 0)
6727 {
6728 /* A flag is a compound flag if it appears in "af_comp". */
6729 vim_strncpy(key, prevp, p - prevp);
6730 hi = hash_find(&affile->af_comp, key);
6731 if (!HASHITEM_EMPTY(hi))
6732 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6733 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006734 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006735 ++p;
6736 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006737
Bram Moolenaar5195e452005-08-19 20:32:47 +00006738 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006739}
6740
6741/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006742 * Apply affixes to a word and store the resulting words.
6743 * "ht" is the hashtable with affentry_T that need to be applied, either
6744 * prefixes or suffixes.
6745 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6746 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006747 *
6748 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006749 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006750 static int
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006751store_aff_word(spin, word, afflist, affile, ht, xht, condit, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006752 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006753 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006754 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006755 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006756 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006757 hashtab_T *ht;
6758 hashtab_T *xht;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006759 int condit; /* CONDIT_SUF et al. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006760 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006761 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006762 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
6763 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006764{
6765 int todo;
6766 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006767 affheader_T *ah;
6768 affentry_T *ae;
6769 regmatch_T regmatch;
6770 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006771 int retval = OK;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006772 int i, j;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006773 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006774 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006775 char_u *use_pfxlist;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006776 int use_pfxlen;
6777 int need_affix;
6778 char_u store_afflist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006779 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006780 size_t wordlen = STRLEN(word);
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006781 int use_condit;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006782
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006783 todo = (int)ht->ht_used;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006784 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006785 {
6786 if (!HASHITEM_EMPTY(hi))
6787 {
6788 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006789 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006790
Bram Moolenaar51485f02005-06-04 21:55:20 +00006791 /* Check that the affix combines, if required, and that the word
6792 * supports this affix. */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006793 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
6794 && flag_in_afflist(affile->af_flagtype, afflist,
6795 ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006796 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006797 /* Loop over all affix entries with this name. */
6798 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006799 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006800 /* Check the condition. It's not logical to match case
6801 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006802 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006803 * Another requirement from Myspell is that the chop
6804 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006805 * For prefixes, when "PFXPOSTPONE" was used, only do
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006806 * prefixes with a chop string and/or flags.
6807 * When a previously added affix had CIRCUMFIX this one
6808 * must have it too, if it had not then this one must not
6809 * have one either. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006810 regmatch.regprog = ae->ae_prog;
6811 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006812 if ((xht != NULL || !affile->af_pfxpostpone
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006813 || ae->ae_chop != NULL
6814 || ae->ae_flags != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006815 && (ae->ae_chop == NULL
6816 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006817 && (ae->ae_prog == NULL
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006818 || vim_regexec(&regmatch, word, (colnr_T)0))
6819 && (((condit & CONDIT_CFIX) == 0)
6820 == ((condit & CONDIT_AFF) == 0
6821 || ae->ae_flags == NULL
6822 || !flag_in_afflist(affile->af_flagtype,
6823 ae->ae_flags, affile->af_circumfix))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006824 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006825 /* Match. Remove the chop and add the affix. */
6826 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006827 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006828 /* prefix: chop/add at the start of the word */
6829 if (ae->ae_add == NULL)
6830 *newword = NUL;
6831 else
6832 STRCPY(newword, ae->ae_add);
6833 p = word;
6834 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006835 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006836 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006837#ifdef FEAT_MBYTE
6838 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006839 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006840 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006841 for ( ; i > 0; --i)
6842 mb_ptr_adv(p);
6843 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006844 else
6845#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006846 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006847 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006848 STRCAT(newword, p);
6849 }
6850 else
6851 {
6852 /* suffix: chop/add at the end of the word */
6853 STRCPY(newword, word);
6854 if (ae->ae_chop != NULL)
6855 {
6856 /* Remove chop string. */
6857 p = newword + STRLEN(newword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006858 i = (int)MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006859 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006860 mb_ptr_back(newword, p);
6861 *p = NUL;
6862 }
6863 if (ae->ae_add != NULL)
6864 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006865 }
6866
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006867 use_flags = flags;
6868 use_pfxlist = pfxlist;
6869 use_pfxlen = pfxlen;
6870 need_affix = FALSE;
6871 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
6872 if (ae->ae_flags != NULL)
6873 {
6874 /* Extract flags from the affix list. */
6875 use_flags |= get_affix_flags(affile, ae->ae_flags);
6876
6877 if (affile->af_needaffix != 0 && flag_in_afflist(
6878 affile->af_flagtype, ae->ae_flags,
6879 affile->af_needaffix))
6880 need_affix = TRUE;
6881
6882 /* When there is a CIRCUMFIX flag the other affix
6883 * must also have it and we don't add the word
6884 * with one affix. */
6885 if (affile->af_circumfix != 0 && flag_in_afflist(
6886 affile->af_flagtype, ae->ae_flags,
6887 affile->af_circumfix))
6888 {
6889 use_condit |= CONDIT_CFIX;
6890 if ((condit & CONDIT_CFIX) == 0)
6891 need_affix = TRUE;
6892 }
6893
6894 if (affile->af_pfxpostpone
6895 || spin->si_compflags != NULL)
6896 {
6897 if (affile->af_pfxpostpone)
6898 /* Get prefix IDS from the affix list. */
6899 use_pfxlen = get_pfxlist(affile,
6900 ae->ae_flags, store_afflist);
6901 else
6902 use_pfxlen = 0;
6903 use_pfxlist = store_afflist;
6904
6905 /* Combine the prefix IDs. Avoid adding the
6906 * same ID twice. */
6907 for (i = 0; i < pfxlen; ++i)
6908 {
6909 for (j = 0; j < use_pfxlen; ++j)
6910 if (pfxlist[i] == use_pfxlist[j])
6911 break;
6912 if (j == use_pfxlen)
6913 use_pfxlist[use_pfxlen++] = pfxlist[i];
6914 }
6915
6916 if (spin->si_compflags != NULL)
6917 /* Get compound IDS from the affix list. */
6918 get_compflags(affile, ae->ae_flags,
6919 use_pfxlist + use_pfxlen);
6920
6921 /* Combine the list of compound flags.
6922 * Concatenate them to the prefix IDs list.
6923 * Avoid adding the same ID twice. */
6924 for (i = pfxlen; pfxlist[i] != NUL; ++i)
6925 {
6926 for (j = use_pfxlen;
6927 use_pfxlist[j] != NUL; ++j)
6928 if (pfxlist[i] == use_pfxlist[j])
6929 break;
6930 if (use_pfxlist[j] == NUL)
6931 {
6932 use_pfxlist[j++] = pfxlist[i];
6933 use_pfxlist[j] = NUL;
6934 }
6935 }
6936 }
6937 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00006938
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006939 /* Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006940 * use the compound flags. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006941 if (use_pfxlist != NULL && ae->ae_compforbid)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006942 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006943 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006944 use_pfxlist = pfx_pfxlist;
6945 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006946
6947 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00006948 if (spin->si_prefroot != NULL
6949 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006950 {
6951 /* ... add a flag to indicate an affix was used. */
6952 use_flags |= WF_HAS_AFF;
6953
6954 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00006955 * affixes is not allowed. But do use the
6956 * compound flags after them. */
Bram Moolenaar18144c82006-04-12 21:52:12 +00006957 if (!ah->ah_combine && use_pfxlist != NULL)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006958 use_pfxlist += use_pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006959 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006960
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006961 /* When compounding is supported and there is no
6962 * "COMPOUNDPERMITFLAG" then forbid compounding on the
6963 * side where the affix is applied. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006964 if (spin->si_compflags != NULL && !ae->ae_comppermit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006965 {
6966 if (xht != NULL)
6967 use_flags |= WF_NOCOMPAFT;
6968 else
6969 use_flags |= WF_NOCOMPBEF;
6970 }
6971
Bram Moolenaar51485f02005-06-04 21:55:20 +00006972 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006973 if (store_word(spin, newword, use_flags,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006974 spin->si_region, use_pfxlist,
6975 need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006976 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006977
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006978 /* When added a prefix or a first suffix and the affix
6979 * has flags may add a(nother) suffix. RECURSIVE! */
6980 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
6981 if (store_aff_word(spin, newword, ae->ae_flags,
6982 affile, &affile->af_suff, xht,
6983 use_condit & (xht == NULL
6984 ? ~0 : ~CONDIT_SUF),
Bram Moolenaar5195e452005-08-19 20:32:47 +00006985 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006986 retval = FAIL;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006987
6988 /* When added a suffix and combining is allowed also
6989 * try adding a prefix additionally. Both for the
6990 * word flags and for the affix flags. RECURSIVE! */
6991 if (xht != NULL && ah->ah_combine)
6992 {
6993 if (store_aff_word(spin, newword,
6994 afflist, affile,
6995 xht, NULL, use_condit,
6996 use_flags, use_pfxlist,
6997 pfxlen) == FAIL
6998 || (ae->ae_flags != NULL
6999 && store_aff_word(spin, newword,
7000 ae->ae_flags, affile,
7001 xht, NULL, use_condit,
7002 use_flags, use_pfxlist,
7003 pfxlen) == FAIL))
7004 retval = FAIL;
7005 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007006 }
7007 }
7008 }
7009 }
7010 }
7011
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007012 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007013}
7014
7015/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007016 * Read a file with a list of words.
7017 */
7018 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007019spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007020 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007021 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007022{
7023 FILE *fd;
7024 long lnum = 0;
7025 char_u rline[MAXLINELEN];
7026 char_u *line;
7027 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007028 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007029 int l;
7030 int retval = OK;
7031 int did_word = FALSE;
7032 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007033 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007034 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007035
7036 /*
7037 * Open the file.
7038 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007039 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007040 if (fd == NULL)
7041 {
7042 EMSG2(_(e_notopen), fname);
7043 return FAIL;
7044 }
7045
Bram Moolenaar4770d092006-01-12 23:22:24 +00007046 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
7047 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007048
7049 /*
7050 * Read all the lines in the file one by one.
7051 */
7052 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
7053 {
7054 line_breakcheck();
7055 ++lnum;
7056
7057 /* Skip comment lines. */
7058 if (*rline == '#')
7059 continue;
7060
7061 /* Remove CR, LF and white space from the end. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007062 l = (int)STRLEN(rline);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007063 while (l > 0 && rline[l - 1] <= ' ')
7064 --l;
7065 if (l == 0)
7066 continue; /* empty or blank line */
7067 rline[l] = NUL;
7068
Bram Moolenaar9c102382006-05-03 21:26:49 +00007069 /* Convert from "/encoding={encoding}" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007070 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007071#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00007072 if (spin->si_conv.vc_type != CONV_NONE)
7073 {
7074 pc = string_convert(&spin->si_conv, rline, NULL);
7075 if (pc == NULL)
7076 {
7077 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
7078 fname, lnum, rline);
7079 continue;
7080 }
7081 line = pc;
7082 }
7083 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00007084#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007085 {
7086 pc = NULL;
7087 line = rline;
7088 }
7089
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007090 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00007091 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007092 ++line;
7093 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007094 {
7095 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007096 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
7097 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007098 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007099 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
7100 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007101 else
7102 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007103#ifdef FEAT_MBYTE
7104 char_u *enc;
7105
Bram Moolenaar51485f02005-06-04 21:55:20 +00007106 /* Setup for conversion to 'encoding'. */
Bram Moolenaar9c102382006-05-03 21:26:49 +00007107 line += 9;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007108 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007109 if (enc != NULL && !spin->si_ascii
7110 && convert_setup(&spin->si_conv, enc,
7111 p_enc) == FAIL)
7112 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00007113 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007114 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007115 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007116#else
7117 smsg((char_u *)_("Conversion in %s not supported"), fname);
7118#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007119 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007120 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007121 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007122
Bram Moolenaar3982c542005-06-08 21:56:31 +00007123 if (STRNCMP(line, "regions=", 8) == 0)
7124 {
7125 if (spin->si_region_count > 1)
7126 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
7127 fname, lnum, line);
7128 else
7129 {
7130 line += 8;
7131 if (STRLEN(line) > 16)
7132 smsg((char_u *)_("Too many regions in %s line %d: %s"),
7133 fname, lnum, line);
7134 else
7135 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007136 spin->si_region_count = (int)STRLEN(line) / 2;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007137 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007138
7139 /* Adjust the mask for a word valid in all regions. */
7140 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007141 }
7142 }
7143 continue;
7144 }
7145
Bram Moolenaar7887d882005-07-01 22:33:52 +00007146 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
7147 fname, lnum, line - 1);
7148 continue;
7149 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007150
Bram Moolenaar7887d882005-07-01 22:33:52 +00007151 flags = 0;
7152 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007153
Bram Moolenaar7887d882005-07-01 22:33:52 +00007154 /* Check for flags and region after a slash. */
7155 p = vim_strchr(line, '/');
7156 if (p != NULL)
7157 {
7158 *p++ = NUL;
7159 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007160 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007161 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007162 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007163 else if (*p == '!') /* Bad, bad, wicked word. */
7164 flags |= WF_BANNED;
7165 else if (*p == '?') /* Rare word. */
7166 flags |= WF_RARE;
7167 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007168 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007169 if ((flags & WF_REGION) == 0) /* first one */
7170 regionmask = 0;
7171 flags |= WF_REGION;
7172
7173 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00007174 if (l > spin->si_region_count)
7175 {
7176 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00007177 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007178 break;
7179 }
7180 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007181 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007182 else
7183 {
7184 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
7185 fname, lnum, p);
7186 break;
7187 }
7188 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007189 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007190 }
7191
7192 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
7193 if (spin->si_ascii && has_non_ascii(line))
7194 {
7195 ++non_ascii;
7196 continue;
7197 }
7198
7199 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007200 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007201 {
7202 retval = FAIL;
7203 break;
7204 }
7205 did_word = TRUE;
7206 }
7207
7208 vim_free(pc);
7209 fclose(fd);
7210
Bram Moolenaar4770d092006-01-12 23:22:24 +00007211 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007212 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007213 vim_snprintf((char *)IObuff, IOSIZE,
7214 _("Ignored %d words with non-ASCII characters"), non_ascii);
7215 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007216 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007217
Bram Moolenaar51485f02005-06-04 21:55:20 +00007218 return retval;
7219}
7220
7221/*
7222 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007223 * This avoids calling free() for every little struct we use (and keeping
7224 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00007225 * The memory is cleared to all zeros.
7226 * Returns NULL when out of memory.
7227 */
7228 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007229getroom(spin, len, align)
7230 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007231 size_t len; /* length needed */
7232 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007233{
7234 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007235 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007236
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007237 if (align && bl != NULL)
7238 /* Round size up for alignment. On some systems structures need to be
7239 * aligned to the size of a pointer (e.g., SPARC). */
7240 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
7241 & ~(sizeof(char *) - 1);
7242
Bram Moolenaar51485f02005-06-04 21:55:20 +00007243 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
7244 {
7245 /* Allocate a block of memory. This is not freed until much later. */
7246 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
7247 if (bl == NULL)
7248 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007249 bl->sb_next = spin->si_blocks;
7250 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007251 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007252 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007253 }
7254
7255 p = bl->sb_data + bl->sb_used;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007256 bl->sb_used += (int)len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007257
7258 return p;
7259}
7260
7261/*
7262 * Make a copy of a string into memory allocated with getroom().
7263 */
7264 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007265getroom_save(spin, s)
7266 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007267 char_u *s;
7268{
7269 char_u *sc;
7270
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007271 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007272 if (sc != NULL)
7273 STRCPY(sc, s);
7274 return sc;
7275}
7276
7277
7278/*
7279 * Free the list of allocated sblock_T.
7280 */
7281 static void
7282free_blocks(bl)
7283 sblock_T *bl;
7284{
7285 sblock_T *next;
7286
7287 while (bl != NULL)
7288 {
7289 next = bl->sb_next;
7290 vim_free(bl);
7291 bl = next;
7292 }
7293}
7294
7295/*
7296 * Allocate the root of a word tree.
7297 */
7298 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007299wordtree_alloc(spin)
7300 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007301{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007302 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007303}
7304
7305/*
7306 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007307 * Always store it in the case-folded tree. For a keep-case word this is
7308 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
7309 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007310 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007311 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
7312 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007313 */
7314 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00007315store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007316 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007317 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007318 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007319 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007320 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007321 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007322{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007323 int len = (int)STRLEN(word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007324 int ct = captype(word, word + len);
7325 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007326 int res = OK;
7327 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007328
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007329 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007330 for (p = pfxlist; res == OK; ++p)
7331 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007332 if (!need_affix || (p != NULL && *p != NUL))
7333 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007334 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007335 if (p == NULL || *p == NUL)
7336 break;
7337 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007338 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007339
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007340 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00007341 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007342 for (p = pfxlist; res == OK; ++p)
7343 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007344 if (!need_affix || (p != NULL && *p != NUL))
7345 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007346 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007347 if (p == NULL || *p == NUL)
7348 break;
7349 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007350 ++spin->si_keepwcount;
7351 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007352 return res;
7353}
7354
7355/*
7356 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00007357 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007358 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007359 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007360 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007361 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007362tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007363 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007364 char_u *word;
7365 wordnode_T *root;
7366 int flags;
7367 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007368 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007369{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007370 wordnode_T *node = root;
7371 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007372 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007373 wordnode_T **prev = NULL;
7374 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007375
Bram Moolenaar51485f02005-06-04 21:55:20 +00007376 /* Add each byte of the word to the tree, including the NUL at the end. */
7377 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007378 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007379 /* When there is more than one reference to this node we need to make
7380 * a copy, so that we can modify it. Copy the whole list of siblings
7381 * (we don't optimize for a partly shared list of siblings). */
7382 if (node != NULL && node->wn_refs > 1)
7383 {
7384 --node->wn_refs;
7385 copyprev = prev;
7386 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
7387 {
7388 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007389 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007390 if (np == NULL)
7391 return FAIL;
7392 np->wn_child = copyp->wn_child;
7393 if (np->wn_child != NULL)
7394 ++np->wn_child->wn_refs; /* child gets extra ref */
7395 np->wn_byte = copyp->wn_byte;
7396 if (np->wn_byte == NUL)
7397 {
7398 np->wn_flags = copyp->wn_flags;
7399 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007400 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007401 }
7402
7403 /* Link the new node in the list, there will be one ref. */
7404 np->wn_refs = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007405 if (copyprev != NULL)
7406 *copyprev = np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007407 copyprev = &np->wn_sibling;
7408
7409 /* Let "node" point to the head of the copied list. */
7410 if (copyp == node)
7411 node = np;
7412 }
7413 }
7414
Bram Moolenaar51485f02005-06-04 21:55:20 +00007415 /* Look for the sibling that has the same character. They are sorted
7416 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007417 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007418 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007419 while (node != NULL
7420 && (node->wn_byte < word[i]
7421 || (node->wn_byte == NUL
7422 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007423 ? node->wn_affixID < (unsigned)affixID
7424 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007425 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007426 && (spin->si_sugtree
7427 ? (node->wn_region & 0xffff) < region
7428 : node->wn_affixID
7429 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007430 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007431 prev = &node->wn_sibling;
7432 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007433 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007434 if (node == NULL
7435 || node->wn_byte != word[i]
7436 || (word[i] == NUL
7437 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007438 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007439 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007440 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007441 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007442 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007443 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007444 if (np == NULL)
7445 return FAIL;
7446 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007447
7448 /* If "node" is NULL this is a new child or the end of the sibling
7449 * list: ref count is one. Otherwise use ref count of sibling and
7450 * make ref count of sibling one (matters when inserting in front
7451 * of the list of siblings). */
7452 if (node == NULL)
7453 np->wn_refs = 1;
7454 else
7455 {
7456 np->wn_refs = node->wn_refs;
7457 node->wn_refs = 1;
7458 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007459 *prev = np;
7460 np->wn_sibling = node;
7461 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007462 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007463
Bram Moolenaar51485f02005-06-04 21:55:20 +00007464 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007465 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007466 node->wn_flags = flags;
7467 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007468 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007469 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00007470 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007471 prev = &node->wn_child;
7472 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007473 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007474#ifdef SPELL_PRINTTREE
7475 smsg("Added \"%s\"", word);
7476 spell_print_tree(root->wn_sibling);
7477#endif
7478
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007479 /* count nr of words added since last message */
7480 ++spin->si_msg_count;
7481
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007482 if (spin->si_compress_cnt > 1)
7483 {
7484 if (--spin->si_compress_cnt == 1)
7485 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007486 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007487 }
7488
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007489 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007490 * When we have allocated lots of memory we need to compress the word tree
7491 * to free up some room. But compression is slow, and we might actually
7492 * need that room, thus only compress in the following situations:
7493 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00007494 * "compress_start" blocks.
7495 * 2. When compressed before and used "compress_inc" blocks before
7496 * adding "compress_added" words (si_compress_cnt > 1).
7497 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007498 * (si_compress_cnt == 1) and the number of free nodes drops below the
7499 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007500 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007501#ifndef SPELL_PRINTTREE
7502 if (spin->si_compress_cnt == 1
7503 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00007504 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007505#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007506 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007507 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00007508 * when the freed up room has been used and another "compress_inc"
7509 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007510 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007511 spin->si_blocks_cnt -= compress_inc;
7512 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007513
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007514 if (spin->si_verbose)
7515 {
7516 msg_start();
7517 msg_puts((char_u *)_(msg_compressing));
7518 msg_clr_eos();
7519 msg_didout = FALSE;
7520 msg_col = 0;
7521 out_flush();
7522 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007523
7524 /* Compress both trees. Either they both have many nodes, which makes
7525 * compression useful, or one of them is small, which means
Bram Moolenaar4770d092006-01-12 23:22:24 +00007526 * compression goes fast. But when filling the souldfold word tree
7527 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007528 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007529 if (affixID >= 0)
7530 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007531 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007532
7533 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007534}
7535
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007536/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007537 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7538 * Sets "sps_flags".
7539 */
7540 int
7541spell_check_msm()
7542{
7543 char_u *p = p_msm;
7544 long start = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007545 long incr = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007546 long added = 0;
7547
7548 if (!VIM_ISDIGIT(*p))
7549 return FAIL;
7550 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7551 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7552 if (*p != ',')
7553 return FAIL;
7554 ++p;
7555 if (!VIM_ISDIGIT(*p))
7556 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007557 incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007558 if (*p != ',')
7559 return FAIL;
7560 ++p;
7561 if (!VIM_ISDIGIT(*p))
7562 return FAIL;
7563 added = getdigits(&p) * 1024;
7564 if (*p != NUL)
7565 return FAIL;
7566
Bram Moolenaar89d40322006-08-29 15:30:07 +00007567 if (start == 0 || incr == 0 || added == 0 || incr > start)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007568 return FAIL;
7569
7570 compress_start = start;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007571 compress_inc = incr;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007572 compress_added = added;
7573 return OK;
7574}
7575
7576
7577/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007578 * Get a wordnode_T, either from the list of previously freed nodes or
7579 * allocate a new one.
7580 */
7581 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007582get_wordnode(spin)
7583 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007584{
7585 wordnode_T *n;
7586
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007587 if (spin->si_first_free == NULL)
7588 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007589 else
7590 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007591 n = spin->si_first_free;
7592 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007593 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007594 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007595 }
7596#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007597 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007598#endif
7599 return n;
7600}
7601
7602/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007603 * Decrement the reference count on a node (which is the head of a list of
7604 * siblings). If the reference count becomes zero free the node and its
7605 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007606 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007607 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007608 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007609deref_wordnode(spin, node)
7610 spellinfo_T *spin;
7611 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007612{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007613 wordnode_T *np;
7614 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007615
7616 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007617 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007618 for (np = node; np != NULL; np = np->wn_sibling)
7619 {
7620 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007621 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007622 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007623 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007624 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007625 ++cnt; /* length field */
7626 }
7627 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007628}
7629
7630/*
7631 * Free a wordnode_T for re-use later.
7632 * Only the "wn_child" field becomes invalid.
7633 */
7634 static void
7635free_wordnode(spin, n)
7636 spellinfo_T *spin;
7637 wordnode_T *n;
7638{
7639 n->wn_child = spin->si_first_free;
7640 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007641 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007642}
7643
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007644/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007645 * Compress a tree: find tails that are identical and can be shared.
7646 */
7647 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007648wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007649 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007650 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007651{
7652 hashtab_T ht;
7653 int n;
7654 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007655 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007656
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007657 /* Skip the root itself, it's not actually used. The first sibling is the
7658 * start of the tree. */
7659 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007660 {
7661 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007662 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007663
7664#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007665 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007666#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007667 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007668 if (tot > 1000000)
7669 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007670 else if (tot == 0)
7671 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007672 else
7673 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007674 vim_snprintf((char *)IObuff, IOSIZE,
7675 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7676 n, tot, tot - n, perc);
7677 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007678 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007679#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007680 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007681#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007682 hash_clear(&ht);
7683 }
7684}
7685
7686/*
7687 * Compress a node, its siblings and its children, depth first.
7688 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007689 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007690 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007691node_compress(spin, node, ht, tot)
7692 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007693 wordnode_T *node;
7694 hashtab_T *ht;
7695 int *tot; /* total count of nodes before compressing,
7696 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007697{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007698 wordnode_T *np;
7699 wordnode_T *tp;
7700 wordnode_T *child;
7701 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007702 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007703 int len = 0;
7704 unsigned nr, n;
7705 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007706
Bram Moolenaar51485f02005-06-04 21:55:20 +00007707 /*
7708 * Go through the list of siblings. Compress each child and then try
7709 * finding an identical child to replace it.
7710 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007711 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007712 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007713 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007714 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007715 ++len;
7716 if ((child = np->wn_child) != NULL)
7717 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007718 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007719 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007720
7721 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007722 hash = hash_hash(child->wn_u1.hashkey);
7723 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007724 if (!HASHITEM_EMPTY(hi))
7725 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007726 /* There are children we encountered before with a hash value
7727 * identical to the current child. Now check if there is one
7728 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007729 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007730 if (node_equal(child, tp))
7731 {
7732 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007733 * current one. This means the current child and all
7734 * its siblings is unlinked from the tree. */
7735 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007736 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007737 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007738 break;
7739 }
7740 if (tp == NULL)
7741 {
7742 /* No other child with this hash value equals the child of
7743 * the node, add it to the linked list after the first
7744 * item. */
7745 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007746 child->wn_u2.next = tp->wn_u2.next;
7747 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007748 }
7749 }
7750 else
7751 /* No other child has this hash value, add it to the
7752 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007753 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007754 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007755 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007756 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007757
7758 /*
7759 * Make a hash key for the node and its siblings, so that we can quickly
7760 * find a lookalike node. This must be done after compressing the sibling
7761 * list, otherwise the hash key would become invalid by the compression.
7762 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007763 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007764 nr = 0;
7765 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007766 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007767 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007768 /* end node: use wn_flags, wn_region and wn_affixID */
7769 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007770 else
7771 /* byte node: use the byte value and the child pointer */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007772 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
Bram Moolenaar51485f02005-06-04 21:55:20 +00007773 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007774 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007775
7776 /* Avoid NUL bytes, it terminates the hash key. */
7777 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007778 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007779 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007780 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007781 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007782 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007783 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007784 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7785 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007786
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007787 /* Check for CTRL-C pressed now and then. */
7788 fast_breakcheck();
7789
Bram Moolenaar51485f02005-06-04 21:55:20 +00007790 return compressed;
7791}
7792
7793/*
7794 * Return TRUE when two nodes have identical siblings and children.
7795 */
7796 static int
7797node_equal(n1, n2)
7798 wordnode_T *n1;
7799 wordnode_T *n2;
7800{
7801 wordnode_T *p1;
7802 wordnode_T *p2;
7803
7804 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7805 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7806 if (p1->wn_byte != p2->wn_byte
7807 || (p1->wn_byte == NUL
7808 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007809 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007810 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007811 : (p1->wn_child != p2->wn_child)))
7812 break;
7813
7814 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007815}
7816
7817/*
7818 * Write a number to file "fd", MSB first, in "len" bytes.
7819 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007820 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007821put_bytes(fd, nr, len)
7822 FILE *fd;
7823 long_u nr;
7824 int len;
7825{
7826 int i;
7827
7828 for (i = len - 1; i >= 0; --i)
7829 putc((int)(nr >> (i * 8)), fd);
7830}
7831
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007832#ifdef _MSC_VER
7833# if (_MSC_VER <= 1200)
7834/* This line is required for VC6 without the service pack. Also see the
7835 * matching #pragma below. */
Bram Moolenaar5fdec472007-07-24 08:45:13 +00007836 # pragma optimize("", off)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007837# endif
7838#endif
7839
Bram Moolenaar4770d092006-01-12 23:22:24 +00007840/*
7841 * Write spin->si_sugtime to file "fd".
7842 */
7843 static void
7844put_sugtime(spin, fd)
7845 spellinfo_T *spin;
7846 FILE *fd;
7847{
7848 int c;
7849 int i;
7850
7851 /* time_t can be up to 8 bytes in size, more than long_u, thus we
7852 * can't use put_bytes() here. */
7853 for (i = 7; i >= 0; --i)
7854 if (i + 1 > sizeof(time_t))
7855 /* ">>" doesn't work well when shifting more bits than avail */
7856 putc(0, fd);
7857 else
7858 {
7859 c = (unsigned)spin->si_sugtime >> (i * 8);
7860 putc(c, fd);
7861 }
7862}
7863
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007864#ifdef _MSC_VER
7865# if (_MSC_VER <= 1200)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00007866 # pragma optimize("", on)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007867# endif
7868#endif
7869
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007870static int
7871#ifdef __BORLANDC__
7872_RTLENTRYF
7873#endif
7874rep_compare __ARGS((const void *s1, const void *s2));
7875
7876/*
7877 * Function given to qsort() to sort the REP items on "from" string.
7878 */
7879 static int
7880#ifdef __BORLANDC__
7881_RTLENTRYF
7882#endif
7883rep_compare(s1, s2)
7884 const void *s1;
7885 const void *s2;
7886{
7887 fromto_T *p1 = (fromto_T *)s1;
7888 fromto_T *p2 = (fromto_T *)s2;
7889
7890 return STRCMP(p1->ft_from, p2->ft_from);
7891}
7892
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007893/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007894 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007895 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007896 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007897 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007898write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007899 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007900 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007901{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007902 FILE *fd;
7903 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007904 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007905 wordnode_T *tree;
7906 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007907 int i;
7908 int l;
7909 garray_T *gap;
7910 fromto_T *ftp;
7911 char_u *p;
7912 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007913 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007914
Bram Moolenaarb765d632005-06-07 21:00:02 +00007915 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007916 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007917 {
7918 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007919 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007920 }
7921
Bram Moolenaar5195e452005-08-19 20:32:47 +00007922 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007923 /* <fileID> */
7924 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007925 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007926 EMSG(_(e_write));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007927 retval = FAIL;
7928 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007929 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007930
Bram Moolenaar5195e452005-08-19 20:32:47 +00007931 /*
7932 * <SECTIONS>: <section> ... <sectionend>
7933 */
7934
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007935 /* SN_INFO: <infotext> */
7936 if (spin->si_info != NULL)
7937 {
7938 putc(SN_INFO, fd); /* <sectionID> */
7939 putc(0, fd); /* <sectionflags> */
7940
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007941 i = (int)STRLEN(spin->si_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007942 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
7943 fwrite(spin->si_info, (size_t)i, (size_t)1, fd); /* <infotext> */
7944 }
7945
Bram Moolenaar5195e452005-08-19 20:32:47 +00007946 /* SN_REGION: <regionname> ...
7947 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007948 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007949 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007950 putc(SN_REGION, fd); /* <sectionID> */
7951 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7952 l = spin->si_region_count * 2;
7953 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7954 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
7955 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007956 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007957 }
7958 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00007959 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007960
Bram Moolenaar5195e452005-08-19 20:32:47 +00007961 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
7962 *
7963 * The table with character flags and the table for case folding.
7964 * This makes sure the same characters are recognized as word characters
7965 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007966 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007967 * 'encoding'.
7968 * Also skip this for an .add.spl file, the main spell file must contain
7969 * the table (avoids that it conflicts). File is shorter too.
7970 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007971 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007972 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007973 char_u folchars[128 * 8];
7974 int flags;
7975
Bram Moolenaard12a1322005-08-21 22:08:24 +00007976 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007977 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7978
7979 /* Form the <folchars> string first, we need to know its length. */
7980 l = 0;
7981 for (i = 128; i < 256; ++i)
7982 {
7983#ifdef FEAT_MBYTE
7984 if (has_mbyte)
7985 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
7986 else
7987#endif
7988 folchars[l++] = spelltab.st_fold[i];
7989 }
7990 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
7991
7992 fputc(128, fd); /* <charflagslen> */
7993 for (i = 128; i < 256; ++i)
7994 {
7995 flags = 0;
7996 if (spelltab.st_isw[i])
7997 flags |= CF_WORD;
7998 if (spelltab.st_isu[i])
7999 flags |= CF_UPPER;
8000 fputc(flags, fd); /* <charflags> */
8001 }
8002
8003 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
8004 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008005 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008006
Bram Moolenaar5195e452005-08-19 20:32:47 +00008007 /* SN_MIDWORD: <midword> */
8008 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008009 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008010 putc(SN_MIDWORD, fd); /* <sectionID> */
8011 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8012
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008013 i = (int)STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008014 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008015 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
8016 }
8017
Bram Moolenaar5195e452005-08-19 20:32:47 +00008018 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
8019 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008020 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008021 putc(SN_PREFCOND, fd); /* <sectionID> */
8022 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8023
8024 l = write_spell_prefcond(NULL, &spin->si_prefcond);
8025 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8026
8027 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008028 }
8029
Bram Moolenaar5195e452005-08-19 20:32:47 +00008030 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00008031 * SN_SAL: <salflags> <salcount> <sal> ...
8032 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008033
Bram Moolenaar5195e452005-08-19 20:32:47 +00008034 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00008035 * round 2: SN_SAL section (unless SN_SOFO is used)
8036 * round 3: SN_REPSAL section */
8037 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008038 {
8039 if (round == 1)
8040 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008041 else if (round == 2)
8042 {
8043 /* Don't write SN_SAL when using a SN_SOFO section */
8044 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8045 continue;
8046 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008047 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008048 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00008049 gap = &spin->si_repsal;
8050
8051 /* Don't write the section if there are no items. */
8052 if (gap->ga_len == 0)
8053 continue;
8054
8055 /* Sort the REP/REPSAL items. */
8056 if (round != 2)
8057 qsort(gap->ga_data, (size_t)gap->ga_len,
8058 sizeof(fromto_T), rep_compare);
8059
8060 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
8061 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008062
Bram Moolenaar5195e452005-08-19 20:32:47 +00008063 /* This is for making suggestions, section is not required. */
8064 putc(0, fd); /* <sectionflags> */
8065
8066 /* Compute the length of what follows. */
8067 l = 2; /* count <repcount> or <salcount> */
8068 for (i = 0; i < gap->ga_len; ++i)
8069 {
8070 ftp = &((fromto_T *)gap->ga_data)[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008071 l += 1 + (int)STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
8072 l += 1 + (int)STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008073 }
8074 if (round == 2)
8075 ++l; /* count <salflags> */
8076 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8077
8078 if (round == 2)
8079 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008080 i = 0;
8081 if (spin->si_followup)
8082 i |= SAL_F0LLOWUP;
8083 if (spin->si_collapse)
8084 i |= SAL_COLLAPSE;
8085 if (spin->si_rem_accents)
8086 i |= SAL_REM_ACCENTS;
8087 putc(i, fd); /* <salflags> */
8088 }
8089
8090 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
8091 for (i = 0; i < gap->ga_len; ++i)
8092 {
8093 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
8094 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
8095 ftp = &((fromto_T *)gap->ga_data)[i];
8096 for (rr = 1; rr <= 2; ++rr)
8097 {
8098 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008099 l = (int)STRLEN(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008100 putc(l, fd);
8101 fwrite(p, l, (size_t)1, fd);
8102 }
8103 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008104
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008105 }
8106
Bram Moolenaar5195e452005-08-19 20:32:47 +00008107 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
8108 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008109 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8110 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008111 putc(SN_SOFO, fd); /* <sectionID> */
8112 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008113
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008114 l = (int)STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008115 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
8116 /* <sectionlen> */
8117
8118 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
8119 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008120
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008121 l = (int)STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008122 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
8123 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008124 }
8125
Bram Moolenaar4770d092006-01-12 23:22:24 +00008126 /* SN_WORDS: <word> ...
8127 * This is for making suggestions, section is not required. */
8128 if (spin->si_commonwords.ht_used > 0)
8129 {
8130 putc(SN_WORDS, fd); /* <sectionID> */
8131 putc(0, fd); /* <sectionflags> */
8132
8133 /* round 1: count the bytes
8134 * round 2: write the bytes */
8135 for (round = 1; round <= 2; ++round)
8136 {
8137 int todo;
8138 int len = 0;
8139 hashitem_T *hi;
8140
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008141 todo = (int)spin->si_commonwords.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008142 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
8143 if (!HASHITEM_EMPTY(hi))
8144 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008145 l = (int)STRLEN(hi->hi_key) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008146 len += l;
8147 if (round == 2) /* <word> */
8148 fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
8149 --todo;
8150 }
8151 if (round == 1)
8152 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
8153 }
8154 }
8155
Bram Moolenaar5195e452005-08-19 20:32:47 +00008156 /* SN_MAP: <mapstr>
8157 * This is for making suggestions, section is not required. */
8158 if (spin->si_map.ga_len > 0)
8159 {
8160 putc(SN_MAP, fd); /* <sectionID> */
8161 putc(0, fd); /* <sectionflags> */
8162 l = spin->si_map.ga_len;
8163 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8164 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
8165 /* <mapstr> */
8166 }
8167
Bram Moolenaar4770d092006-01-12 23:22:24 +00008168 /* SN_SUGFILE: <timestamp>
8169 * This is used to notify that a .sug file may be available and at the
8170 * same time allows for checking that a .sug file that is found matches
8171 * with this .spl file. That's because the word numbers must be exactly
8172 * right. */
8173 if (!spin->si_nosugfile
8174 && (spin->si_sal.ga_len > 0
8175 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
8176 {
8177 putc(SN_SUGFILE, fd); /* <sectionID> */
8178 putc(0, fd); /* <sectionflags> */
8179 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
8180
8181 /* Set si_sugtime and write it to the file. */
8182 spin->si_sugtime = time(NULL);
8183 put_sugtime(spin, fd); /* <timestamp> */
8184 }
8185
Bram Moolenaare1438bb2006-03-01 22:01:55 +00008186 /* SN_NOSPLITSUGS: nothing
8187 * This is used to notify that no suggestions with word splits are to be
8188 * made. */
8189 if (spin->si_nosplitsugs)
8190 {
8191 putc(SN_NOSPLITSUGS, fd); /* <sectionID> */
8192 putc(0, fd); /* <sectionflags> */
8193 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8194 }
8195
Bram Moolenaar5195e452005-08-19 20:32:47 +00008196 /* SN_COMPOUND: compound info.
8197 * We don't mark it required, when not supported all compound words will
8198 * be bad words. */
8199 if (spin->si_compflags != NULL)
8200 {
8201 putc(SN_COMPOUND, fd); /* <sectionID> */
8202 putc(0, fd); /* <sectionflags> */
8203
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008204 l = (int)STRLEN(spin->si_compflags);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008205 for (i = 0; i < spin->si_comppat.ga_len; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008206 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008207 put_bytes(fd, (long_u)(l + 7), 4); /* <sectionlen> */
8208
Bram Moolenaar5195e452005-08-19 20:32:47 +00008209 putc(spin->si_compmax, fd); /* <compmax> */
8210 putc(spin->si_compminlen, fd); /* <compminlen> */
8211 putc(spin->si_compsylmax, fd); /* <compsylmax> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008212 putc(0, fd); /* for Vim 7.0b compatibility */
8213 putc(spin->si_compoptions, fd); /* <compoptions> */
8214 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
8215 /* <comppatcount> */
8216 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8217 {
8218 p = ((char_u **)(spin->si_comppat.ga_data))[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008219 putc((int)STRLEN(p), fd); /* <comppatlen> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008220 fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);/* <comppattext> */
8221 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008222 /* <compflags> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008223 fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
8224 (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008225 }
8226
Bram Moolenaar78622822005-08-23 21:00:13 +00008227 /* SN_NOBREAK: NOBREAK flag */
8228 if (spin->si_nobreak)
8229 {
8230 putc(SN_NOBREAK, fd); /* <sectionID> */
8231 putc(0, fd); /* <sectionflags> */
8232
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008233 /* It's empty, the presence of the section flags the feature. */
Bram Moolenaar78622822005-08-23 21:00:13 +00008234 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8235 }
8236
Bram Moolenaar5195e452005-08-19 20:32:47 +00008237 /* SN_SYLLABLE: syllable info.
8238 * We don't mark it required, when not supported syllables will not be
8239 * counted. */
8240 if (spin->si_syllable != NULL)
8241 {
8242 putc(SN_SYLLABLE, fd); /* <sectionID> */
8243 putc(0, fd); /* <sectionflags> */
8244
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008245 l = (int)STRLEN(spin->si_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008246 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8247 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
8248 }
8249
8250 /* end of <SECTIONS> */
8251 putc(SN_END, fd); /* <sectionend> */
8252
Bram Moolenaar50cde822005-06-05 21:54:54 +00008253
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008254 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008255 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008256 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008257 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008258 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008259 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008260 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008261 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008262 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008263 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008264 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008265 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008266
Bram Moolenaar0c405862005-06-22 22:26:26 +00008267 /* Clear the index and wnode fields in the tree. */
8268 clear_node(tree);
8269
Bram Moolenaar51485f02005-06-04 21:55:20 +00008270 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00008271 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00008272 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008273 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008274
Bram Moolenaar51485f02005-06-04 21:55:20 +00008275 /* number of nodes in 4 bytes */
8276 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00008277 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008278
Bram Moolenaar51485f02005-06-04 21:55:20 +00008279 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008280 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008281 }
8282
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008283 /* Write another byte to check for errors. */
8284 if (putc(0, fd) == EOF)
8285 retval = FAIL;
8286
8287 if (fclose(fd) == EOF)
8288 retval = FAIL;
8289
8290 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008291}
8292
8293/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008294 * Clear the index and wnode fields of "node", it siblings and its
8295 * children. This is needed because they are a union with other items to save
8296 * space.
8297 */
8298 static void
8299clear_node(node)
8300 wordnode_T *node;
8301{
8302 wordnode_T *np;
8303
8304 if (node != NULL)
8305 for (np = node; np != NULL; np = np->wn_sibling)
8306 {
8307 np->wn_u1.index = 0;
8308 np->wn_u2.wnode = NULL;
8309
8310 if (np->wn_byte != NUL)
8311 clear_node(np->wn_child);
8312 }
8313}
8314
8315
8316/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008317 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008318 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00008319 * This first writes the list of possible bytes (siblings). Then for each
8320 * byte recursively write the children.
8321 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00008322 * NOTE: The code here must match the code in read_tree_node(), since
8323 * assumptions are made about the indexes (so that we don't have to write them
8324 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00008325 *
8326 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008327 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008328 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00008329put_node(fd, node, idx, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008330 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008331 wordnode_T *node;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008332 int idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008333 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008334 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008335{
Bram Moolenaar89d40322006-08-29 15:30:07 +00008336 int newindex = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008337 int siblingcount = 0;
8338 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008339 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008340
Bram Moolenaar51485f02005-06-04 21:55:20 +00008341 /* If "node" is zero the tree is empty. */
8342 if (node == NULL)
8343 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008344
Bram Moolenaar51485f02005-06-04 21:55:20 +00008345 /* Store the index where this node is written. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008346 node->wn_u1.index = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008347
8348 /* Count the number of siblings. */
8349 for (np = node; np != NULL; np = np->wn_sibling)
8350 ++siblingcount;
8351
8352 /* Write the sibling count. */
8353 if (fd != NULL)
8354 putc(siblingcount, fd); /* <siblingcount> */
8355
8356 /* Write each sibling byte and optionally extra info. */
8357 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008358 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008359 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008360 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008361 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008362 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008363 /* For a NUL byte (end of word) write the flags etc. */
8364 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008365 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008366 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008367 * associated condition nr (stored in wn_region). The
8368 * byte value is misused to store the "rare" and "not
8369 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008370 if (np->wn_flags == (short_u)PFX_FLAGS)
8371 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008372 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00008373 {
8374 putc(BY_FLAGS, fd); /* <byte> */
8375 putc(np->wn_flags, fd); /* <pflags> */
8376 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008377 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008378 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008379 }
8380 else
8381 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008382 /* For word trees we write the flag/region items. */
8383 flags = np->wn_flags;
8384 if (regionmask != 0 && np->wn_region != regionmask)
8385 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008386 if (np->wn_affixID != 0)
8387 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008388 if (flags == 0)
8389 {
8390 /* word without flags or region */
8391 putc(BY_NOFLAGS, fd); /* <byte> */
8392 }
8393 else
8394 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008395 if (np->wn_flags >= 0x100)
8396 {
8397 putc(BY_FLAGS2, fd); /* <byte> */
8398 putc(flags, fd); /* <flags> */
8399 putc((unsigned)flags >> 8, fd); /* <flags2> */
8400 }
8401 else
8402 {
8403 putc(BY_FLAGS, fd); /* <byte> */
8404 putc(flags, fd); /* <flags> */
8405 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008406 if (flags & WF_REGION)
8407 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008408 if (flags & WF_AFX)
8409 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008410 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008411 }
8412 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008413 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008414 else
8415 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008416 if (np->wn_child->wn_u1.index != 0
8417 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008418 {
8419 /* The child is written elsewhere, write the reference. */
8420 if (fd != NULL)
8421 {
8422 putc(BY_INDEX, fd); /* <byte> */
8423 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008424 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008425 }
8426 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008427 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008428 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008429 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008430
Bram Moolenaar51485f02005-06-04 21:55:20 +00008431 if (fd != NULL)
8432 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
8433 {
8434 EMSG(_(e_write));
8435 return 0;
8436 }
8437 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008438 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008439
8440 /* Space used in the array when reading: one for each sibling and one for
8441 * the count. */
8442 newindex += siblingcount + 1;
8443
8444 /* Recursively dump the children of each sibling. */
8445 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008446 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
8447 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008448 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008449
8450 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008451}
8452
8453
8454/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008455 * ":mkspell [-ascii] outfile infile ..."
8456 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008457 */
8458 void
8459ex_mkspell(eap)
8460 exarg_T *eap;
8461{
8462 int fcount;
8463 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008464 char_u *arg = eap->arg;
8465 int ascii = FALSE;
8466
8467 if (STRNCMP(arg, "-ascii", 6) == 0)
8468 {
8469 ascii = TRUE;
8470 arg = skipwhite(arg + 6);
8471 }
8472
8473 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
8474 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
8475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008476 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008477 FreeWild(fcount, fnames);
8478 }
8479}
8480
8481/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00008482 * Create the .sug file.
8483 * Uses the soundfold info in "spin".
8484 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
8485 */
8486 static void
8487spell_make_sugfile(spin, wfname)
8488 spellinfo_T *spin;
8489 char_u *wfname;
8490{
8491 char_u fname[MAXPATHL];
8492 int len;
8493 slang_T *slang;
8494 int free_slang = FALSE;
8495
8496 /*
8497 * Read back the .spl file that was written. This fills the required
8498 * info for soundfolding. This also uses less memory than the
8499 * pointer-linked version of the trie. And it avoids having two versions
8500 * of the code for the soundfolding stuff.
8501 * It might have been done already by spell_reload_one().
8502 */
8503 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
8504 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
8505 break;
8506 if (slang == NULL)
8507 {
8508 spell_message(spin, (char_u *)_("Reading back spell file..."));
8509 slang = spell_load_file(wfname, NULL, NULL, FALSE);
8510 if (slang == NULL)
8511 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008512 free_slang = TRUE;
8513 }
8514
8515 /*
8516 * Clear the info in "spin" that is used.
8517 */
8518 spin->si_blocks = NULL;
8519 spin->si_blocks_cnt = 0;
8520 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
8521 spin->si_free_count = 0;
8522 spin->si_first_free = NULL;
8523 spin->si_foldwcount = 0;
8524
8525 /*
8526 * Go through the trie of good words, soundfold each word and add it to
8527 * the soundfold trie.
8528 */
8529 spell_message(spin, (char_u *)_("Performing soundfolding..."));
8530 if (sug_filltree(spin, slang) == FAIL)
8531 goto theend;
8532
8533 /*
8534 * Create the table which links each soundfold word with a list of the
8535 * good words it may come from. Creates buffer "spin->si_spellbuf".
8536 * This also removes the wordnr from the NUL byte entries to make
8537 * compression possible.
8538 */
8539 if (sug_maketable(spin) == FAIL)
8540 goto theend;
8541
8542 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8543 (long)spin->si_spellbuf->b_ml.ml_line_count);
8544
8545 /*
8546 * Compress the soundfold trie.
8547 */
8548 spell_message(spin, (char_u *)_(msg_compressing));
8549 wordtree_compress(spin, spin->si_foldroot);
8550
8551 /*
8552 * Write the .sug file.
8553 * Make the file name by changing ".spl" to ".sug".
8554 */
8555 STRCPY(fname, wfname);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008556 len = (int)STRLEN(fname);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008557 fname[len - 2] = 'u';
8558 fname[len - 1] = 'g';
8559 sug_write(spin, fname);
8560
8561theend:
8562 if (free_slang)
8563 slang_free(slang);
8564 free_blocks(spin->si_blocks);
8565 close_spellbuf(spin->si_spellbuf);
8566}
8567
8568/*
8569 * Build the soundfold trie for language "slang".
8570 */
8571 static int
8572sug_filltree(spin, slang)
8573 spellinfo_T *spin;
8574 slang_T *slang;
8575{
8576 char_u *byts;
8577 idx_T *idxs;
8578 int depth;
8579 idx_T arridx[MAXWLEN];
8580 int curi[MAXWLEN];
8581 char_u tword[MAXWLEN];
8582 char_u tsalword[MAXWLEN];
8583 int c;
8584 idx_T n;
8585 unsigned words_done = 0;
8586 int wordcount[MAXWLEN];
8587
8588 /* We use si_foldroot for the souldfolded trie. */
8589 spin->si_foldroot = wordtree_alloc(spin);
8590 if (spin->si_foldroot == NULL)
8591 return FAIL;
8592
8593 /* let tree_add_word() know we're adding to the soundfolded tree */
8594 spin->si_sugtree = TRUE;
8595
8596 /*
8597 * Go through the whole case-folded tree, soundfold each word and put it
8598 * in the trie.
8599 */
8600 byts = slang->sl_fbyts;
8601 idxs = slang->sl_fidxs;
8602
8603 arridx[0] = 0;
8604 curi[0] = 1;
8605 wordcount[0] = 0;
8606
8607 depth = 0;
8608 while (depth >= 0 && !got_int)
8609 {
8610 if (curi[depth] > byts[arridx[depth]])
8611 {
8612 /* Done all bytes at this node, go up one level. */
8613 idxs[arridx[depth]] = wordcount[depth];
8614 if (depth > 0)
8615 wordcount[depth - 1] += wordcount[depth];
8616
8617 --depth;
8618 line_breakcheck();
8619 }
8620 else
8621 {
8622
8623 /* Do one more byte at this node. */
8624 n = arridx[depth] + curi[depth];
8625 ++curi[depth];
8626
8627 c = byts[n];
8628 if (c == 0)
8629 {
8630 /* Sound-fold the word. */
8631 tword[depth] = NUL;
8632 spell_soundfold(slang, tword, TRUE, tsalword);
8633
8634 /* We use the "flags" field for the MSB of the wordnr,
8635 * "region" for the LSB of the wordnr. */
8636 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8637 words_done >> 16, words_done & 0xffff,
8638 0) == FAIL)
8639 return FAIL;
8640
8641 ++words_done;
8642 ++wordcount[depth];
8643
8644 /* Reset the block count each time to avoid compression
8645 * kicking in. */
8646 spin->si_blocks_cnt = 0;
8647
8648 /* Skip over any other NUL bytes (same word with different
8649 * flags). */
8650 while (byts[n + 1] == 0)
8651 {
8652 ++n;
8653 ++curi[depth];
8654 }
8655 }
8656 else
8657 {
8658 /* Normal char, go one level deeper. */
8659 tword[depth++] = c;
8660 arridx[depth] = idxs[n];
8661 curi[depth] = 1;
8662 wordcount[depth] = 0;
8663 }
8664 }
8665 }
8666
8667 smsg((char_u *)_("Total number of words: %d"), words_done);
8668
8669 return OK;
8670}
8671
8672/*
8673 * Make the table that links each word in the soundfold trie to the words it
8674 * can be produced from.
8675 * This is not unlike lines in a file, thus use a memfile to be able to access
8676 * the table efficiently.
8677 * Returns FAIL when out of memory.
8678 */
8679 static int
8680sug_maketable(spin)
8681 spellinfo_T *spin;
8682{
8683 garray_T ga;
8684 int res = OK;
8685
8686 /* Allocate a buffer, open a memline for it and create the swap file
8687 * (uses a temp file, not a .swp file). */
8688 spin->si_spellbuf = open_spellbuf();
8689 if (spin->si_spellbuf == NULL)
8690 return FAIL;
8691
8692 /* Use a buffer to store the line info, avoids allocating many small
8693 * pieces of memory. */
8694 ga_init2(&ga, 1, 100);
8695
8696 /* recursively go through the tree */
8697 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8698 res = FAIL;
8699
8700 ga_clear(&ga);
8701 return res;
8702}
8703
8704/*
8705 * Fill the table for one node and its children.
8706 * Returns the wordnr at the start of the node.
8707 * Returns -1 when out of memory.
8708 */
8709 static int
8710sug_filltable(spin, node, startwordnr, gap)
8711 spellinfo_T *spin;
8712 wordnode_T *node;
8713 int startwordnr;
8714 garray_T *gap; /* place to store line of numbers */
8715{
8716 wordnode_T *p, *np;
8717 int wordnr = startwordnr;
8718 int nr;
8719 int prev_nr;
8720
8721 for (p = node; p != NULL; p = p->wn_sibling)
8722 {
8723 if (p->wn_byte == NUL)
8724 {
8725 gap->ga_len = 0;
8726 prev_nr = 0;
8727 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8728 {
8729 if (ga_grow(gap, 10) == FAIL)
8730 return -1;
8731
8732 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8733 /* Compute the offset from the previous nr and store the
8734 * offset in a way that it takes a minimum number of bytes.
8735 * It's a bit like utf-8, but without the need to mark
8736 * following bytes. */
8737 nr -= prev_nr;
8738 prev_nr += nr;
8739 gap->ga_len += offset2bytes(nr,
8740 (char_u *)gap->ga_data + gap->ga_len);
8741 }
8742
8743 /* add the NUL byte */
8744 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8745
8746 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8747 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8748 return -1;
8749 ++wordnr;
8750
8751 /* Remove extra NUL entries, we no longer need them. We don't
8752 * bother freeing the nodes, the won't be reused anyway. */
8753 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8754 p->wn_sibling = p->wn_sibling->wn_sibling;
8755
8756 /* Clear the flags on the remaining NUL node, so that compression
8757 * works a lot better. */
8758 p->wn_flags = 0;
8759 p->wn_region = 0;
8760 }
8761 else
8762 {
8763 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8764 if (wordnr == -1)
8765 return -1;
8766 }
8767 }
8768 return wordnr;
8769}
8770
8771/*
8772 * Convert an offset into a minimal number of bytes.
8773 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8774 * bytes.
8775 */
8776 static int
8777offset2bytes(nr, buf)
8778 int nr;
8779 char_u *buf;
8780{
8781 int rem;
8782 int b1, b2, b3, b4;
8783
8784 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8785 b1 = nr % 255 + 1;
8786 rem = nr / 255;
8787 b2 = rem % 255 + 1;
8788 rem = rem / 255;
8789 b3 = rem % 255 + 1;
8790 b4 = rem / 255 + 1;
8791
8792 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8793 {
8794 buf[0] = 0xe0 + b4;
8795 buf[1] = b3;
8796 buf[2] = b2;
8797 buf[3] = b1;
8798 return 4;
8799 }
8800 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8801 {
8802 buf[0] = 0xc0 + b3;
8803 buf[1] = b2;
8804 buf[2] = b1;
8805 return 3;
8806 }
8807 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8808 {
8809 buf[0] = 0x80 + b2;
8810 buf[1] = b1;
8811 return 2;
8812 }
8813 /* 1 byte */
8814 buf[0] = b1;
8815 return 1;
8816}
8817
8818/*
8819 * Opposite of offset2bytes().
8820 * "pp" points to the bytes and is advanced over it.
8821 * Returns the offset.
8822 */
8823 static int
8824bytes2offset(pp)
8825 char_u **pp;
8826{
8827 char_u *p = *pp;
8828 int nr;
8829 int c;
8830
8831 c = *p++;
8832 if ((c & 0x80) == 0x00) /* 1 byte */
8833 {
8834 nr = c - 1;
8835 }
8836 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8837 {
8838 nr = (c & 0x3f) - 1;
8839 nr = nr * 255 + (*p++ - 1);
8840 }
8841 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8842 {
8843 nr = (c & 0x1f) - 1;
8844 nr = nr * 255 + (*p++ - 1);
8845 nr = nr * 255 + (*p++ - 1);
8846 }
8847 else /* 4 bytes */
8848 {
8849 nr = (c & 0x0f) - 1;
8850 nr = nr * 255 + (*p++ - 1);
8851 nr = nr * 255 + (*p++ - 1);
8852 nr = nr * 255 + (*p++ - 1);
8853 }
8854
8855 *pp = p;
8856 return nr;
8857}
8858
8859/*
8860 * Write the .sug file in "fname".
8861 */
8862 static void
8863sug_write(spin, fname)
8864 spellinfo_T *spin;
8865 char_u *fname;
8866{
8867 FILE *fd;
8868 wordnode_T *tree;
8869 int nodecount;
8870 int wcount;
8871 char_u *line;
8872 linenr_T lnum;
8873 int len;
8874
8875 /* Create the file. Note that an existing file is silently overwritten! */
8876 fd = mch_fopen((char *)fname, "w");
8877 if (fd == NULL)
8878 {
8879 EMSG2(_(e_notopen), fname);
8880 return;
8881 }
8882
8883 vim_snprintf((char *)IObuff, IOSIZE,
8884 _("Writing suggestion file %s ..."), fname);
8885 spell_message(spin, IObuff);
8886
8887 /*
8888 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8889 */
8890 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
8891 {
8892 EMSG(_(e_write));
8893 goto theend;
8894 }
8895 putc(VIMSUGVERSION, fd); /* <versionnr> */
8896
8897 /* Write si_sugtime to the file. */
8898 put_sugtime(spin, fd); /* <timestamp> */
8899
8900 /*
8901 * <SUGWORDTREE>
8902 */
8903 spin->si_memtot = 0;
8904 tree = spin->si_foldroot->wn_sibling;
8905
8906 /* Clear the index and wnode fields in the tree. */
8907 clear_node(tree);
8908
8909 /* Count the number of nodes. Needed to be able to allocate the
8910 * memory when reading the nodes. Also fills in index for shared
8911 * nodes. */
8912 nodecount = put_node(NULL, tree, 0, 0, FALSE);
8913
8914 /* number of nodes in 4 bytes */
8915 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
8916 spin->si_memtot += nodecount + nodecount * sizeof(int);
8917
8918 /* Write the nodes. */
8919 (void)put_node(fd, tree, 0, 0, FALSE);
8920
8921 /*
8922 * <SUGTABLE>: <sugwcount> <sugline> ...
8923 */
8924 wcount = spin->si_spellbuf->b_ml.ml_line_count;
8925 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
8926
8927 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
8928 {
8929 /* <sugline>: <sugnr> ... NUL */
8930 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008931 len = (int)STRLEN(line) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008932 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
8933 {
8934 EMSG(_(e_write));
8935 goto theend;
8936 }
8937 spin->si_memtot += len;
8938 }
8939
8940 /* Write another byte to check for errors. */
8941 if (putc(0, fd) == EOF)
8942 EMSG(_(e_write));
8943
8944 vim_snprintf((char *)IObuff, IOSIZE,
8945 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
8946 spell_message(spin, IObuff);
8947
8948theend:
8949 /* close the file */
8950 fclose(fd);
8951}
8952
8953/*
8954 * Open a spell buffer. This is a nameless buffer that is not in the buffer
8955 * list and only contains text lines. Can use a swapfile to reduce memory
8956 * use.
8957 * Most other fields are invalid! Esp. watch out for string options being
8958 * NULL and there is no undo info.
8959 * Returns NULL when out of memory.
8960 */
8961 static buf_T *
8962open_spellbuf()
8963{
8964 buf_T *buf;
8965
8966 buf = (buf_T *)alloc_clear(sizeof(buf_T));
8967 if (buf != NULL)
8968 {
8969 buf->b_spell = TRUE;
8970 buf->b_p_swf = TRUE; /* may create a swap file */
8971 ml_open(buf);
8972 ml_open_file(buf); /* create swap file now */
8973 }
8974 return buf;
8975}
8976
8977/*
8978 * Close the buffer used for spell info.
8979 */
8980 static void
8981close_spellbuf(buf)
8982 buf_T *buf;
8983{
8984 if (buf != NULL)
8985 {
8986 ml_close(buf, TRUE);
8987 vim_free(buf);
8988 }
8989}
8990
8991
8992/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008993 * Create a Vim spell file from one or more word lists.
8994 * "fnames[0]" is the output file name.
8995 * "fnames[fcount - 1]" is the last input file name.
8996 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
8997 * and ".spl" is appended to make the output file name.
8998 */
8999 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009000mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009001 int fcount;
9002 char_u **fnames;
9003 int ascii; /* -ascii argument given */
9004 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009005 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009006{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009007 char_u fname[MAXPATHL];
9008 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00009009 char_u **innames;
9010 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009011 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009012 int i;
9013 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009014 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00009015 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009016 spellinfo_T spin;
9017
9018 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009019 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009020 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009021 spin.si_followup = TRUE;
9022 spin.si_rem_accents = TRUE;
9023 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009024 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009025 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
9026 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009027 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009028 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009029 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009030 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009031
Bram Moolenaarb765d632005-06-07 21:00:02 +00009032 /* default: fnames[0] is output file, following are input files */
9033 innames = &fnames[1];
9034 incount = fcount - 1;
9035
9036 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00009037 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009038 len = (int)STRLEN(fnames[0]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009039 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
9040 {
9041 /* For ":mkspell path/en.latin1.add" output file is
9042 * "path/en.latin1.add.spl". */
9043 innames = &fnames[0];
9044 incount = 1;
9045 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
9046 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009047 else if (fcount == 1)
9048 {
9049 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
9050 innames = &fnames[0];
9051 incount = 1;
9052 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
9053 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
9054 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009055 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
9056 {
9057 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009058 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009059 }
9060 else
9061 /* Name should be language, make the file name from it. */
9062 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
9063 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
9064
9065 /* Check for .ascii.spl. */
9066 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
9067 spin.si_ascii = TRUE;
9068
9069 /* Check for .add.spl. */
9070 if (strstr((char *)gettail(wfname), ".add.") != NULL)
9071 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00009072 }
9073
Bram Moolenaarb765d632005-06-07 21:00:02 +00009074 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009075 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009076 else if (vim_strchr(gettail(wfname), '_') != NULL)
9077 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009078 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009079 EMSG(_("E754: Only up to 8 regions supported"));
9080 else
9081 {
9082 /* Check for overwriting before doing things that may take a lot of
9083 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009084 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009085 {
9086 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009087 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009088 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009089 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009090 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009091 EMSG2(_(e_isadir2), wfname);
9092 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009093 }
9094
9095 /*
9096 * Init the aff and dic pointers.
9097 * Get the region names if there are more than 2 arguments.
9098 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009099 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009100 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009101 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009102
Bram Moolenaar3982c542005-06-08 21:56:31 +00009103 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009104 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009105 len = (int)STRLEN(innames[i]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009106 if (STRLEN(gettail(innames[i])) < 5
9107 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009108 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009109 EMSG2(_("E755: Invalid region in %s"), innames[i]);
9110 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009111 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009112 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
9113 spin.si_region_name[i * 2 + 1] =
9114 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009115 }
9116 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009117 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009118
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009119 spin.si_foldroot = wordtree_alloc(&spin);
9120 spin.si_keeproot = wordtree_alloc(&spin);
9121 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009122 if (spin.si_foldroot == NULL
9123 || spin.si_keeproot == NULL
9124 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009125 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00009126 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009127 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009128 }
9129
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009130 /* When not producing a .add.spl file clear the character table when
9131 * we encounter one in the .aff file. This means we dump the current
9132 * one in the .spl file if the .aff file doesn't define one. That's
9133 * better than guessing the contents, the table will match a
9134 * previously loaded spell file. */
9135 if (!spin.si_add)
9136 spin.si_clear_chartab = TRUE;
9137
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009138 /*
9139 * Read all the .aff and .dic files.
9140 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00009141 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009142 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009143 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009144 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009145 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009146 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009147
Bram Moolenaarb765d632005-06-07 21:00:02 +00009148 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009149 if (mch_stat((char *)fname, &st) >= 0)
9150 {
9151 /* Read the .aff file. Will init "spin->si_conv" based on the
9152 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009153 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009154 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009155 error = TRUE;
9156 else
9157 {
9158 /* Read the .dic file and store the words in the trees. */
9159 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00009160 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009161 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009162 error = TRUE;
9163 }
9164 }
9165 else
9166 {
9167 /* No .aff file, try reading the file as a word list. Store
9168 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009169 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009170 error = TRUE;
9171 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009172
Bram Moolenaarb765d632005-06-07 21:00:02 +00009173#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009174 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00009175 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009176#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009177 }
9178
Bram Moolenaar78622822005-08-23 21:00:13 +00009179 if (spin.si_compflags != NULL && spin.si_nobreak)
9180 MSG(_("Warning: both compounding and NOBREAK specified"));
9181
Bram Moolenaar4770d092006-01-12 23:22:24 +00009182 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009183 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009184 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00009185 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009186 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009187 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009188 wordtree_compress(&spin, spin.si_foldroot);
9189 wordtree_compress(&spin, spin.si_keeproot);
9190 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009191 }
9192
Bram Moolenaar4770d092006-01-12 23:22:24 +00009193 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009194 {
9195 /*
9196 * Write the info in the spell file.
9197 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009198 vim_snprintf((char *)IObuff, IOSIZE,
9199 _("Writing spell file %s ..."), wfname);
9200 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00009201
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009202 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009203
Bram Moolenaar4770d092006-01-12 23:22:24 +00009204 spell_message(&spin, (char_u *)_("Done!"));
9205 vim_snprintf((char *)IObuff, IOSIZE,
9206 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
9207 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009208
Bram Moolenaar4770d092006-01-12 23:22:24 +00009209 /*
9210 * If the file is loaded need to reload it.
9211 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009212 if (!error)
9213 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009214 }
9215
9216 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009217 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009218 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009219 ga_clear(&spin.si_sal);
9220 ga_clear(&spin.si_map);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009221 ga_clear(&spin.si_comppat);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009222 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009223 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009224
9225 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009226 for (i = 0; i < incount; ++i)
9227 if (afile[i] != NULL)
9228 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009229
9230 /* Free all the bits and pieces at once. */
9231 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009232
9233 /*
9234 * If there is soundfolding info and no NOSUGFILE item create the
9235 * .sug file with the soundfolded word trie.
9236 */
9237 if (spin.si_sugtime != 0 && !error && !got_int)
9238 spell_make_sugfile(&spin, wfname);
9239
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009240 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009241}
9242
Bram Moolenaar4770d092006-01-12 23:22:24 +00009243/*
9244 * Display a message for spell file processing when 'verbose' is set or using
9245 * ":mkspell". "str" can be IObuff.
9246 */
9247 static void
9248spell_message(spin, str)
9249 spellinfo_T *spin;
9250 char_u *str;
9251{
9252 if (spin->si_verbose || p_verbose > 2)
9253 {
9254 if (!spin->si_verbose)
9255 verbose_enter();
9256 MSG(str);
9257 out_flush();
9258 if (!spin->si_verbose)
9259 verbose_leave();
9260 }
9261}
Bram Moolenaarb765d632005-06-07 21:00:02 +00009262
9263/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009264 * ":[count]spellgood {word}"
9265 * ":[count]spellwrong {word}"
Bram Moolenaard0131a82006-03-04 21:46:13 +00009266 * ":[count]spellundo {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00009267 */
9268 void
9269ex_spell(eap)
9270 exarg_T *eap;
9271{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009272 spell_add_word(eap->arg, (int)STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaard0131a82006-03-04 21:46:13 +00009273 eap->forceit ? 0 : (int)eap->line2,
9274 eap->cmdidx == CMD_spellundo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009275}
9276
9277/*
9278 * Add "word[len]" to 'spellfile' as a good or bad word.
9279 */
9280 void
Bram Moolenaar89d40322006-08-29 15:30:07 +00009281spell_add_word(word, len, bad, idx, undo)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009282 char_u *word;
9283 int len;
9284 int bad;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009285 int idx; /* "zG" and "zW": zero, otherwise index in
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009286 'spellfile' */
Bram Moolenaard0131a82006-03-04 21:46:13 +00009287 int undo; /* TRUE for "zug", "zuG", "zuw" and "zuW" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009288{
Bram Moolenaara3917072006-09-14 08:48:14 +00009289 FILE *fd = NULL;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009290 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009291 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009292 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009293 char_u fnamebuf[MAXPATHL];
9294 char_u line[MAXWLEN * 2];
9295 long fpos, fpos_next = 0;
9296 int i;
9297 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009298
Bram Moolenaar89d40322006-08-29 15:30:07 +00009299 if (idx == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009300 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009301 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009302 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009303 int_wordlist = vim_tempname('s');
9304 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009305 return;
9306 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009307 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009308 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009309 else
9310 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009311 /* If 'spellfile' isn't set figure out a good default value. */
9312 if (*curbuf->b_p_spf == NUL)
9313 {
9314 init_spellfile();
9315 new_spf = TRUE;
9316 }
9317
9318 if (*curbuf->b_p_spf == NUL)
9319 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00009320 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009321 return;
9322 }
9323
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009324 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
9325 {
9326 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
Bram Moolenaar89d40322006-08-29 15:30:07 +00009327 if (i == idx)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009328 break;
9329 if (*spf == NUL)
9330 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009331 EMSGN(_("E765: 'spellfile' does not have %ld entries"), idx);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009332 return;
9333 }
9334 }
9335
Bram Moolenaarb765d632005-06-07 21:00:02 +00009336 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009337 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009338 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
9339 buf = NULL;
9340 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00009341 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009342 EMSG(_(e_bufloaded));
9343 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009344 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009345
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009346 fname = fnamebuf;
9347 }
9348
Bram Moolenaard0131a82006-03-04 21:46:13 +00009349 if (bad || undo)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009350 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009351 /* When the word appears as good word we need to remove that one,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009352 * since its flags sort before the one with WF_BANNED. */
9353 fd = mch_fopen((char *)fname, "r");
9354 if (fd != NULL)
9355 {
9356 while (!vim_fgets(line, MAXWLEN * 2, fd))
9357 {
9358 fpos = fpos_next;
9359 fpos_next = ftell(fd);
9360 if (STRNCMP(word, line, len) == 0
9361 && (line[len] == '/' || line[len] < ' '))
9362 {
9363 /* Found duplicate word. Remove it by writing a '#' at
9364 * the start of the line. Mixing reading and writing
9365 * doesn't work for all systems, close the file first. */
9366 fclose(fd);
9367 fd = mch_fopen((char *)fname, "r+");
9368 if (fd == NULL)
9369 break;
9370 if (fseek(fd, fpos, SEEK_SET) == 0)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009371 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009372 fputc('#', fd);
Bram Moolenaard0131a82006-03-04 21:46:13 +00009373 if (undo)
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009374 {
9375 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009376 smsg((char_u *)_("Word removed from %s"), NameBuff);
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009377 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00009378 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009379 fseek(fd, fpos_next, SEEK_SET);
9380 }
9381 }
9382 fclose(fd);
9383 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009384 }
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009385
9386 if (!undo)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009387 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009388 fd = mch_fopen((char *)fname, "a");
9389 if (fd == NULL && new_spf)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009390 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009391 char_u *p;
9392
Bram Moolenaard0131a82006-03-04 21:46:13 +00009393 /* We just initialized the 'spellfile' option and can't open the
9394 * file. We may need to create the "spell" directory first. We
9395 * already checked the runtime directory is writable in
9396 * init_spellfile(). */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009397 if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009398 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009399 int c = *p;
9400
Bram Moolenaard0131a82006-03-04 21:46:13 +00009401 /* The directory doesn't exist. Try creating it and opening
9402 * the file again. */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009403 *p = NUL;
9404 vim_mkdir(fname, 0755);
9405 *p = c;
Bram Moolenaard0131a82006-03-04 21:46:13 +00009406 fd = mch_fopen((char *)fname, "a");
9407 }
9408 }
9409
9410 if (fd == NULL)
9411 EMSG2(_(e_notopen), fname);
9412 else
9413 {
9414 if (bad)
9415 fprintf(fd, "%.*s/!\n", len, word);
9416 else
9417 fprintf(fd, "%.*s\n", len, word);
9418 fclose(fd);
9419
9420 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
9421 smsg((char_u *)_("Word added to %s"), NameBuff);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009422 }
9423 }
9424
Bram Moolenaard0131a82006-03-04 21:46:13 +00009425 if (fd != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009426 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009427 /* Update the .add.spl file. */
9428 mkspell(1, &fname, FALSE, TRUE, TRUE);
9429
9430 /* If the .add file is edited somewhere, reload it. */
9431 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00009432 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009433
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009434 redraw_all_later(SOME_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009435 }
9436}
9437
9438/*
9439 * Initialize 'spellfile' for the current buffer.
9440 */
9441 static void
9442init_spellfile()
9443{
9444 char_u buf[MAXPATHL];
9445 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009446 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009447 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009448 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009449 int aspath = FALSE;
9450 char_u *lstart = curbuf->b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009451
9452 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
9453 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009454 /* Find the end of the language name. Exclude the region. If there
9455 * is a path separator remember the start of the tail. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009456 for (lend = curbuf->b_p_spl; *lend != NUL
9457 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009458 if (vim_ispathsep(*lend))
9459 {
9460 aspath = TRUE;
9461 lstart = lend + 1;
9462 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009463
9464 /* Loop over all entries in 'runtimepath'. Use the first one where we
9465 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009466 rtp = p_rtp;
9467 while (*rtp != NUL)
9468 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009469 if (aspath)
9470 /* Use directory of an entry with path, e.g., for
9471 * "/dir/lg.utf-8.spl" use "/dir". */
9472 vim_strncpy(buf, curbuf->b_p_spl, lstart - curbuf->b_p_spl - 1);
9473 else
9474 /* Copy the path from 'runtimepath' to buf[]. */
9475 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009476 if (filewritable(buf) == 2)
9477 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00009478 /* Use the first language name from 'spelllang' and the
9479 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009480 if (aspath)
9481 vim_strncpy(buf, curbuf->b_p_spl, lend - curbuf->b_p_spl);
9482 else
9483 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009484 /* Create the "spell" directory if it doesn't exist yet. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009485 l = (int)STRLEN(buf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009486 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
9487 if (!filewritable(buf) != 2)
9488 vim_mkdir(buf, 0755);
9489
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009490 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009491 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009492 "/%.*s", (int)(lend - lstart), lstart);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009493 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009494 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009495 fname = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang->sl_fname;
9496 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
9497 fname != NULL
9498 && strstr((char *)gettail(fname), ".ascii.") != NULL
9499 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009500 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
9501 break;
9502 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009503 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009504 }
9505 }
9506}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009507
Bram Moolenaar51485f02005-06-04 21:55:20 +00009508
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009509/*
9510 * Init the chartab used for spelling for ASCII.
9511 * EBCDIC is not supported!
9512 */
9513 static void
9514clear_spell_chartab(sp)
9515 spelltab_T *sp;
9516{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009517 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009518
9519 /* Init everything to FALSE. */
9520 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
9521 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
9522 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009523 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009524 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009525 sp->st_upper[i] = i;
9526 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009527
9528 /* We include digits. A word shouldn't start with a digit, but handling
9529 * that is done separately. */
9530 for (i = '0'; i <= '9'; ++i)
9531 sp->st_isw[i] = TRUE;
9532 for (i = 'A'; i <= 'Z'; ++i)
9533 {
9534 sp->st_isw[i] = TRUE;
9535 sp->st_isu[i] = TRUE;
9536 sp->st_fold[i] = i + 0x20;
9537 }
9538 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009539 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009540 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009541 sp->st_upper[i] = i - 0x20;
9542 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009543}
9544
9545/*
9546 * Init the chartab used for spelling. Only depends on 'encoding'.
9547 * Called once while starting up and when 'encoding' changes.
9548 * The default is to use isalpha(), but the spell file should define the word
9549 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009550 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009551 */
9552 void
9553init_spell_chartab()
9554{
9555 int i;
9556
9557 did_set_spelltab = FALSE;
9558 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009559#ifdef FEAT_MBYTE
9560 if (enc_dbcs)
9561 {
9562 /* DBCS: assume double-wide characters are word characters. */
9563 for (i = 128; i <= 255; ++i)
9564 if (MB_BYTE2LEN(i) == 2)
9565 spelltab.st_isw[i] = TRUE;
9566 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009567 else if (enc_utf8)
9568 {
9569 for (i = 128; i < 256; ++i)
9570 {
9571 spelltab.st_isu[i] = utf_isupper(i);
9572 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
9573 spelltab.st_fold[i] = utf_fold(i);
9574 spelltab.st_upper[i] = utf_toupper(i);
9575 }
9576 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009577 else
9578#endif
9579 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009580 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009581 for (i = 128; i < 256; ++i)
9582 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009583 if (MB_ISUPPER(i))
9584 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009585 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009586 spelltab.st_isu[i] = TRUE;
9587 spelltab.st_fold[i] = MB_TOLOWER(i);
9588 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009589 else if (MB_ISLOWER(i))
9590 {
9591 spelltab.st_isw[i] = TRUE;
9592 spelltab.st_upper[i] = MB_TOUPPER(i);
9593 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009594 }
9595 }
9596}
9597
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009598/*
9599 * Set the spell character tables from strings in the affix file.
9600 */
9601 static int
9602set_spell_chartab(fol, low, upp)
9603 char_u *fol;
9604 char_u *low;
9605 char_u *upp;
9606{
9607 /* We build the new tables here first, so that we can compare with the
9608 * previous one. */
9609 spelltab_T new_st;
9610 char_u *pf = fol, *pl = low, *pu = upp;
9611 int f, l, u;
9612
9613 clear_spell_chartab(&new_st);
9614
9615 while (*pf != NUL)
9616 {
9617 if (*pl == NUL || *pu == NUL)
9618 {
9619 EMSG(_(e_affform));
9620 return FAIL;
9621 }
9622#ifdef FEAT_MBYTE
9623 f = mb_ptr2char_adv(&pf);
9624 l = mb_ptr2char_adv(&pl);
9625 u = mb_ptr2char_adv(&pu);
9626#else
9627 f = *pf++;
9628 l = *pl++;
9629 u = *pu++;
9630#endif
9631 /* Every character that appears is a word character. */
9632 if (f < 256)
9633 new_st.st_isw[f] = TRUE;
9634 if (l < 256)
9635 new_st.st_isw[l] = TRUE;
9636 if (u < 256)
9637 new_st.st_isw[u] = TRUE;
9638
9639 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9640 * case-folding */
9641 if (l < 256 && l != f)
9642 {
9643 if (f >= 256)
9644 {
9645 EMSG(_(e_affrange));
9646 return FAIL;
9647 }
9648 new_st.st_fold[l] = f;
9649 }
9650
9651 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009652 * case-folding, it's upper case and the "UPP" is the upper case of
9653 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009654 if (u < 256 && u != f)
9655 {
9656 if (f >= 256)
9657 {
9658 EMSG(_(e_affrange));
9659 return FAIL;
9660 }
9661 new_st.st_fold[u] = f;
9662 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009663 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009664 }
9665 }
9666
9667 if (*pl != NUL || *pu != NUL)
9668 {
9669 EMSG(_(e_affform));
9670 return FAIL;
9671 }
9672
9673 return set_spell_finish(&new_st);
9674}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009675
9676/*
9677 * Set the spell character tables from strings in the .spl file.
9678 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009679 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009680set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009681 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009682 int cnt; /* length of "flags" */
9683 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009684{
9685 /* We build the new tables here first, so that we can compare with the
9686 * previous one. */
9687 spelltab_T new_st;
9688 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009689 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009690 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009691
9692 clear_spell_chartab(&new_st);
9693
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009694 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009695 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009696 if (i < cnt)
9697 {
9698 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9699 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9700 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009701
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009702 if (*p != NUL)
9703 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009704#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009705 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009706#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009707 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009708#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009709 new_st.st_fold[i + 128] = c;
9710 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9711 new_st.st_upper[c] = i + 128;
9712 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009713 }
9714
Bram Moolenaar5195e452005-08-19 20:32:47 +00009715 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009716}
9717
9718 static int
9719set_spell_finish(new_st)
9720 spelltab_T *new_st;
9721{
9722 int i;
9723
9724 if (did_set_spelltab)
9725 {
9726 /* check that it's the same table */
9727 for (i = 0; i < 256; ++i)
9728 {
9729 if (spelltab.st_isw[i] != new_st->st_isw[i]
9730 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009731 || spelltab.st_fold[i] != new_st->st_fold[i]
9732 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009733 {
9734 EMSG(_("E763: Word characters differ between spell files"));
9735 return FAIL;
9736 }
9737 }
9738 }
9739 else
9740 {
9741 /* copy the new spelltab into the one being used */
9742 spelltab = *new_st;
9743 did_set_spelltab = TRUE;
9744 }
9745
9746 return OK;
9747}
9748
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009749/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009750 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009751 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009752 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009753 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009754 */
9755 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009756spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00009757 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009758 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009759{
Bram Moolenaarea408852005-06-25 22:49:46 +00009760#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009761 char_u *s;
9762 int l;
9763 int c;
9764
9765 if (has_mbyte)
9766 {
9767 l = MB_BYTE2LEN(*p);
9768 s = p;
9769 if (l == 1)
9770 {
9771 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009772 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009773 {
9774 s = p + 1; /* skip a mid-word character */
9775 l = MB_BYTE2LEN(*s);
9776 }
9777 }
9778 else
9779 {
9780 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009781 if (c < 256 ? buf->b_spell_ismw[c]
9782 : (buf->b_spell_ismw_mb != NULL
9783 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009784 {
9785 s = p + l;
9786 l = MB_BYTE2LEN(*s);
9787 }
9788 }
9789
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009790 c = mb_ptr2char(s);
9791 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009792 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009793 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009794 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009795#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009796
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009797 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
9798}
9799
9800/*
9801 * Return TRUE if "p" points to a word character.
9802 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9803 */
9804 static int
9805spell_iswordp_nmw(p)
9806 char_u *p;
9807{
9808#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009809 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009810
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009811 if (has_mbyte)
9812 {
9813 c = mb_ptr2char(p);
9814 if (c > 255)
9815 return mb_get_class(p) >= 2;
9816 return spelltab.st_isw[c];
9817 }
9818#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009819 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009820}
9821
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009822#ifdef FEAT_MBYTE
9823/*
9824 * Return TRUE if "p" points to a word character.
9825 * Wide version of spell_iswordp().
9826 */
9827 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009828spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009829 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009830 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009831{
9832 int *s;
9833
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009834 if (*p < 256 ? buf->b_spell_ismw[*p]
9835 : (buf->b_spell_ismw_mb != NULL
9836 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009837 s = p + 1;
9838 else
9839 s = p;
9840
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009841 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009842 {
9843 if (enc_utf8)
9844 return utf_class(*s) >= 2;
9845 if (enc_dbcs)
9846 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
9847 return 0;
9848 }
9849 return spelltab.st_isw[*s];
9850}
9851#endif
9852
Bram Moolenaarea408852005-06-25 22:49:46 +00009853/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009854 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009855 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009856 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009857 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009858write_spell_prefcond(fd, gap)
9859 FILE *fd;
9860 garray_T *gap;
9861{
9862 int i;
9863 char_u *p;
9864 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009865 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009866
Bram Moolenaar5195e452005-08-19 20:32:47 +00009867 if (fd != NULL)
9868 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
9869
9870 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009871
9872 for (i = 0; i < gap->ga_len; ++i)
9873 {
9874 /* <prefcond> : <condlen> <condstr> */
9875 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009876 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009877 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009878 len = (int)STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009879 if (fd != NULL)
9880 {
9881 fputc(len, fd);
9882 fwrite(p, (size_t)len, (size_t)1, fd);
9883 }
9884 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009885 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009886 else if (fd != NULL)
9887 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009888 }
9889
Bram Moolenaar5195e452005-08-19 20:32:47 +00009890 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009891}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009892
9893/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009894 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
9895 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009896 * When using a multi-byte 'encoding' the length may change!
9897 * Returns FAIL when something wrong.
9898 */
9899 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009900spell_casefold(str, len, buf, buflen)
9901 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009902 int len;
9903 char_u *buf;
9904 int buflen;
9905{
9906 int i;
9907
9908 if (len >= buflen)
9909 {
9910 buf[0] = NUL;
9911 return FAIL; /* result will not fit */
9912 }
9913
9914#ifdef FEAT_MBYTE
9915 if (has_mbyte)
9916 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009917 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009918 char_u *p;
9919 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009920
9921 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009922 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009923 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009924 if (outi + MB_MAXBYTES > buflen)
9925 {
9926 buf[outi] = NUL;
9927 return FAIL;
9928 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009929 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009930 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009931 }
9932 buf[outi] = NUL;
9933 }
9934 else
9935#endif
9936 {
9937 /* Be quick for non-multibyte encodings. */
9938 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009939 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009940 buf[i] = NUL;
9941 }
9942
9943 return OK;
9944}
9945
Bram Moolenaar4770d092006-01-12 23:22:24 +00009946/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009947#define SPS_BEST 1
9948#define SPS_FAST 2
9949#define SPS_DOUBLE 4
9950
Bram Moolenaar4770d092006-01-12 23:22:24 +00009951static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
9952static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009953
9954/*
9955 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009956 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009957 */
9958 int
9959spell_check_sps()
9960{
9961 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009962 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009963 char_u buf[MAXPATHL];
9964 int f;
9965
9966 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009967 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009968
9969 for (p = p_sps; *p != NUL; )
9970 {
9971 copy_option_part(&p, buf, MAXPATHL, ",");
9972
9973 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009974 if (VIM_ISDIGIT(*buf))
9975 {
9976 s = buf;
9977 sps_limit = getdigits(&s);
9978 if (*s != NUL && !VIM_ISDIGIT(*s))
9979 f = -1;
9980 }
9981 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009982 f = SPS_BEST;
9983 else if (STRCMP(buf, "fast") == 0)
9984 f = SPS_FAST;
9985 else if (STRCMP(buf, "double") == 0)
9986 f = SPS_DOUBLE;
9987 else if (STRNCMP(buf, "expr:", 5) != 0
9988 && STRNCMP(buf, "file:", 5) != 0)
9989 f = -1;
9990
9991 if (f == -1 || (sps_flags != 0 && f != 0))
9992 {
9993 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009994 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009995 return FAIL;
9996 }
9997 if (f != 0)
9998 sps_flags = f;
9999 }
10000
10001 if (sps_flags == 0)
10002 sps_flags = SPS_BEST;
10003
10004 return OK;
10005}
10006
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010007/*
10008 * "z?": Find badly spelled word under or after the cursor.
10009 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010010 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +000010011 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010012 */
10013 void
Bram Moolenaard12a1322005-08-21 22:08:24 +000010014spell_suggest(count)
10015 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010016{
10017 char_u *line;
10018 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010019 char_u wcopy[MAXWLEN + 2];
10020 char_u *p;
10021 int i;
10022 int c;
10023 suginfo_T sug;
10024 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010025 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010026 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010027 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010028 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010029 int badlen = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010030
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010031 if (no_spell_checking(curwin))
10032 return;
10033
10034#ifdef FEAT_VISUAL
10035 if (VIsual_active)
10036 {
10037 /* Use the Visually selected text as the bad word. But reject
10038 * a multi-line selection. */
10039 if (curwin->w_cursor.lnum != VIsual.lnum)
10040 {
10041 vim_beep();
10042 return;
10043 }
10044 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
10045 if (badlen < 0)
10046 badlen = -badlen;
10047 else
10048 curwin->w_cursor.col = VIsual.col;
10049 ++badlen;
10050 end_visual_mode();
10051 }
10052 else
10053#endif
10054 /* Find the start of the badly spelled word. */
10055 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +000010056 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010057 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010058 /* No bad word or it starts after the cursor: use the word under the
10059 * cursor. */
10060 curwin->w_cursor = prev_cursor;
10061 line = ml_get_curline();
10062 p = line + curwin->w_cursor.col;
10063 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010064 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010065 mb_ptr_back(line, p);
10066 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010067 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010068 mb_ptr_adv(p);
10069
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010070 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010071 {
10072 beep_flush();
10073 return;
10074 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010075 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010076 }
10077
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010078 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010079
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010080 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010081 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010082
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010083 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010084
Bram Moolenaar5195e452005-08-19 20:32:47 +000010085 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
10086 * 'spellsuggest', whatever is smaller. */
10087 if (sps_limit > (int)Rows - 2)
10088 limit = (int)Rows - 2;
10089 else
10090 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010091 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010092 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010093
10094 if (sug.su_ga.ga_len == 0)
10095 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +000010096 else if (count > 0)
10097 {
10098 if (count > sug.su_ga.ga_len)
10099 smsg((char_u *)_("Sorry, only %ld suggestions"),
10100 (long)sug.su_ga.ga_len);
10101 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010102 else
10103 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010104 vim_free(repl_from);
10105 repl_from = NULL;
10106 vim_free(repl_to);
10107 repl_to = NULL;
10108
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010109#ifdef FEAT_RIGHTLEFT
10110 /* When 'rightleft' is set the list is drawn right-left. */
10111 cmdmsg_rl = curwin->w_p_rl;
10112 if (cmdmsg_rl)
10113 msg_col = Columns - 1;
10114#endif
10115
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010116 /* List the suggestions. */
10117 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000010118 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010119 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010120 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
10121 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010122#ifdef FEAT_RIGHTLEFT
10123 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
10124 {
10125 /* And now the rabbit from the high hat: Avoid showing the
10126 * untranslated message rightleft. */
10127 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
10128 sug.su_badlen, sug.su_badptr);
10129 }
10130#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010131 msg_puts(IObuff);
10132 msg_clr_eos();
10133 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +000010134
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010135 msg_scroll = TRUE;
10136 for (i = 0; i < sug.su_ga.ga_len; ++i)
10137 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010138 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010139
10140 /* The suggested word may replace only part of the bad word, add
10141 * the not replaced part. */
10142 STRCPY(wcopy, stp->st_word);
10143 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010144 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010145 sug.su_badptr + stp->st_orglen,
10146 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010147 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
10148#ifdef FEAT_RIGHTLEFT
10149 if (cmdmsg_rl)
10150 rl_mirror(IObuff);
10151#endif
10152 msg_puts(IObuff);
10153
10154 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010155 msg_puts(IObuff);
10156
10157 /* The word may replace more than "su_badlen". */
10158 if (sug.su_badlen < stp->st_orglen)
10159 {
10160 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
10161 stp->st_orglen, sug.su_badptr);
10162 msg_puts(IObuff);
10163 }
10164
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010165 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010166 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010167 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010168 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010169 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010170 stp->st_salscore ? "s " : "",
10171 stp->st_score, stp->st_altscore);
10172 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010173 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +000010174 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010175#ifdef FEAT_RIGHTLEFT
10176 if (cmdmsg_rl)
10177 /* Mirror the numbers, but keep the leading space. */
10178 rl_mirror(IObuff + 1);
10179#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +000010180 msg_advance(30);
10181 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010182 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010183 msg_putchar('\n');
10184 }
10185
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010186#ifdef FEAT_RIGHTLEFT
10187 cmdmsg_rl = FALSE;
10188 msg_col = 0;
10189#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010190 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010191 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010192 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010193 selected -= lines_left;
Bram Moolenaar0fd92892006-03-09 22:27:48 +000010194 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010195 }
10196
Bram Moolenaard12a1322005-08-21 22:08:24 +000010197 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
10198 {
10199 /* Save the from and to text for :spellrepall. */
10200 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000010201 if (sug.su_badlen > stp->st_orglen)
10202 {
10203 /* Replacing less than "su_badlen", append the remainder to
10204 * repl_to. */
10205 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
10206 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
10207 sug.su_badlen - stp->st_orglen,
10208 sug.su_badptr + stp->st_orglen);
10209 repl_to = vim_strsave(IObuff);
10210 }
10211 else
10212 {
10213 /* Replacing su_badlen or more, use the whole word. */
10214 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
10215 repl_to = vim_strsave(stp->st_word);
10216 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010217
10218 /* Replace the word. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010219 p = alloc((unsigned)STRLEN(line) - stp->st_orglen + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010220 if (p != NULL)
10221 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010222 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010223 mch_memmove(p, line, c);
10224 STRCPY(p + c, stp->st_word);
10225 STRCAT(p, sug.su_badptr + stp->st_orglen);
10226 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10227 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010228
10229 /* For redo we use a change-word command. */
10230 ResetRedobuff();
10231 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +000010232 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010233 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010234 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010235
10236 /* After this "p" may be invalid. */
10237 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010238 }
10239 }
10240 else
10241 curwin->w_cursor = prev_cursor;
10242
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010243 spell_find_cleanup(&sug);
10244}
10245
10246/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010247 * Check if the word at line "lnum" column "col" is required to start with a
10248 * capital. This uses 'spellcapcheck' of the current buffer.
10249 */
10250 static int
10251check_need_cap(lnum, col)
10252 linenr_T lnum;
10253 colnr_T col;
10254{
10255 int need_cap = FALSE;
10256 char_u *line;
10257 char_u *line_copy = NULL;
10258 char_u *p;
10259 colnr_T endcol;
10260 regmatch_T regmatch;
10261
10262 if (curbuf->b_cap_prog == NULL)
10263 return FALSE;
10264
10265 line = ml_get_curline();
10266 endcol = 0;
10267 if ((int)(skipwhite(line) - line) >= (int)col)
10268 {
10269 /* At start of line, check if previous line is empty or sentence
10270 * ends there. */
10271 if (lnum == 1)
10272 need_cap = TRUE;
10273 else
10274 {
10275 line = ml_get(lnum - 1);
10276 if (*skipwhite(line) == NUL)
10277 need_cap = TRUE;
10278 else
10279 {
10280 /* Append a space in place of the line break. */
10281 line_copy = concat_str(line, (char_u *)" ");
10282 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010283 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010284 }
10285 }
10286 }
10287 else
10288 endcol = col;
10289
10290 if (endcol > 0)
10291 {
10292 /* Check if sentence ends before the bad word. */
10293 regmatch.regprog = curbuf->b_cap_prog;
10294 regmatch.rm_ic = FALSE;
10295 p = line + endcol;
10296 for (;;)
10297 {
10298 mb_ptr_back(line, p);
10299 if (p == line || spell_iswordp_nmw(p))
10300 break;
10301 if (vim_regexec(&regmatch, p, 0)
10302 && regmatch.endp[0] == line + endcol)
10303 {
10304 need_cap = TRUE;
10305 break;
10306 }
10307 }
10308 }
10309
10310 vim_free(line_copy);
10311
10312 return need_cap;
10313}
10314
10315
10316/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010317 * ":spellrepall"
10318 */
10319/*ARGSUSED*/
10320 void
10321ex_spellrepall(eap)
10322 exarg_T *eap;
10323{
10324 pos_T pos = curwin->w_cursor;
10325 char_u *frompat;
10326 int addlen;
10327 char_u *line;
10328 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010329 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010330 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010331
10332 if (repl_from == NULL || repl_to == NULL)
10333 {
10334 EMSG(_("E752: No previous spell replacement"));
10335 return;
10336 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010337 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010338
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010339 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010340 if (frompat == NULL)
10341 return;
10342 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
10343 p_ws = FALSE;
10344
Bram Moolenaar5195e452005-08-19 20:32:47 +000010345 sub_nsubs = 0;
10346 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010347 curwin->w_cursor.lnum = 0;
10348 while (!got_int)
10349 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +000010350 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010351 || u_save_cursor() == FAIL)
10352 break;
10353
10354 /* Only replace when the right word isn't there yet. This happens
10355 * when changing "etc" to "etc.". */
10356 line = ml_get_curline();
10357 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
10358 repl_to, STRLEN(repl_to)) != 0)
10359 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010360 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010361 if (p == NULL)
10362 break;
10363 mch_memmove(p, line, curwin->w_cursor.col);
10364 STRCPY(p + curwin->w_cursor.col, repl_to);
10365 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
10366 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10367 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010368
10369 if (curwin->w_cursor.lnum != prev_lnum)
10370 {
10371 ++sub_nlines;
10372 prev_lnum = curwin->w_cursor.lnum;
10373 }
10374 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010375 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010376 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010377 }
10378
10379 p_ws = save_ws;
10380 curwin->w_cursor = pos;
10381 vim_free(frompat);
10382
Bram Moolenaar5195e452005-08-19 20:32:47 +000010383 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010384 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010385 else
10386 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010387}
10388
10389/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010390 * Find spell suggestions for "word". Return them in the growarray "*gap" as
10391 * a list of allocated strings.
10392 */
10393 void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010394spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010395 garray_T *gap;
10396 char_u *word;
10397 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010398 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010399 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010400{
10401 suginfo_T sug;
10402 int i;
10403 suggest_T *stp;
10404 char_u *wcopy;
10405
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010406 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010407
10408 /* Make room in "gap". */
10409 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010410 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010411 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010412 for (i = 0; i < sug.su_ga.ga_len; ++i)
10413 {
10414 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010415
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010416 /* The suggested word may replace only part of "word", add the not
10417 * replaced part. */
10418 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010419 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010420 if (wcopy == NULL)
10421 break;
10422 STRCPY(wcopy, stp->st_word);
10423 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
10424 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
10425 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010426 }
10427
10428 spell_find_cleanup(&sug);
10429}
10430
10431/*
10432 * Find spell suggestions for the word at the start of "badptr".
10433 * Return the suggestions in "su->su_ga".
10434 * The maximum number of suggestions is "maxcount".
10435 * Note: does use info for the current window.
10436 * This is based on the mechanisms of Aspell, but completely reimplemented.
10437 */
10438 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010439spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010440 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010441 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010442 suginfo_T *su;
10443 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +000010444 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010445 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010446 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010447{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010448 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010449 char_u buf[MAXPATHL];
10450 char_u *p;
10451 int do_combine = FALSE;
10452 char_u *sps_copy;
10453#ifdef FEAT_EVAL
10454 static int expr_busy = FALSE;
10455#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010456 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010457 int i;
10458 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010459
10460 /*
10461 * Set the info in "*su".
10462 */
10463 vim_memset(su, 0, sizeof(suginfo_T));
10464 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10465 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010466 if (*badptr == NUL)
10467 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010468 hash_init(&su->su_banned);
10469
10470 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010471 if (badlen != 0)
10472 su->su_badlen = badlen;
10473 else
10474 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010475 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010476 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010477
10478 if (su->su_badlen >= MAXWLEN)
10479 su->su_badlen = MAXWLEN - 1; /* just in case */
10480 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10481 (void)spell_casefold(su->su_badptr, su->su_badlen,
10482 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010483 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010484 su->su_badflags = badword_captype(su->su_badptr,
10485 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010486 if (need_cap)
10487 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010488
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010489 /* Find the default language for sound folding. We simply use the first
10490 * one in 'spelllang' that supports sound folding. That's good for when
10491 * using multiple files for one language, it's not that bad when mixing
10492 * languages (e.g., "pl,en"). */
10493 for (i = 0; i < curbuf->b_langp.ga_len; ++i)
10494 {
10495 lp = LANGP_ENTRY(curbuf->b_langp, i);
10496 if (lp->lp_sallang != NULL)
10497 {
10498 su->su_sallang = lp->lp_sallang;
10499 break;
10500 }
10501 }
10502
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010503 /* Soundfold the bad word with the default sound folding, so that we don't
10504 * have to do this many times. */
10505 if (su->su_sallang != NULL)
10506 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10507 su->su_sal_badword);
10508
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010509 /* If the word is not capitalised and spell_check() doesn't consider the
10510 * word to be bad then it might need to be capitalised. Add a suggestion
10511 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010512 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010513 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010514 {
10515 make_case_word(su->su_badword, buf, WF_ONECAP);
10516 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010517 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010518 }
10519
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010520 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +000010521 if (banbadword)
10522 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010523
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010524 /* Make a copy of 'spellsuggest', because the expression may change it. */
10525 sps_copy = vim_strsave(p_sps);
10526 if (sps_copy == NULL)
10527 return;
10528
10529 /* Loop over the items in 'spellsuggest'. */
10530 for (p = sps_copy; *p != NUL; )
10531 {
10532 copy_option_part(&p, buf, MAXPATHL, ",");
10533
10534 if (STRNCMP(buf, "expr:", 5) == 0)
10535 {
10536#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010537 /* Evaluate an expression. Skip this when called recursively,
10538 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010539 if (!expr_busy)
10540 {
10541 expr_busy = TRUE;
10542 spell_suggest_expr(su, buf + 5);
10543 expr_busy = FALSE;
10544 }
10545#endif
10546 }
10547 else if (STRNCMP(buf, "file:", 5) == 0)
10548 /* Use list of suggestions in a file. */
10549 spell_suggest_file(su, buf + 5);
10550 else
10551 {
10552 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010553 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010554 if (sps_flags & SPS_DOUBLE)
10555 do_combine = TRUE;
10556 }
10557 }
10558
10559 vim_free(sps_copy);
10560
10561 if (do_combine)
10562 /* Combine the two list of suggestions. This must be done last,
10563 * because sorting changes the order again. */
10564 score_combine(su);
10565}
10566
10567#ifdef FEAT_EVAL
10568/*
10569 * Find suggestions by evaluating expression "expr".
10570 */
10571 static void
10572spell_suggest_expr(su, expr)
10573 suginfo_T *su;
10574 char_u *expr;
10575{
10576 list_T *list;
10577 listitem_T *li;
10578 int score;
10579 char_u *p;
10580
10581 /* The work is split up in a few parts to avoid having to export
10582 * suginfo_T.
10583 * First evaluate the expression and get the resulting list. */
10584 list = eval_spell_expr(su->su_badword, expr);
10585 if (list != NULL)
10586 {
10587 /* Loop over the items in the list. */
10588 for (li = list->lv_first; li != NULL; li = li->li_next)
10589 if (li->li_tv.v_type == VAR_LIST)
10590 {
10591 /* Get the word and the score from the items. */
10592 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010593 if (score >= 0 && score <= su->su_maxscore)
10594 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10595 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010596 }
10597 list_unref(list);
10598 }
10599
Bram Moolenaar4770d092006-01-12 23:22:24 +000010600 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10601 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010602 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10603}
10604#endif
10605
10606/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010607 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010608 */
10609 static void
10610spell_suggest_file(su, fname)
10611 suginfo_T *su;
10612 char_u *fname;
10613{
10614 FILE *fd;
10615 char_u line[MAXWLEN * 2];
10616 char_u *p;
10617 int len;
10618 char_u cword[MAXWLEN];
10619
10620 /* Open the file. */
10621 fd = mch_fopen((char *)fname, "r");
10622 if (fd == NULL)
10623 {
10624 EMSG2(_(e_notopen), fname);
10625 return;
10626 }
10627
10628 /* Read it line by line. */
10629 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10630 {
10631 line_breakcheck();
10632
10633 p = vim_strchr(line, '/');
10634 if (p == NULL)
10635 continue; /* No Tab found, just skip the line. */
10636 *p++ = NUL;
10637 if (STRICMP(su->su_badword, line) == 0)
10638 {
10639 /* Match! Isolate the good word, until CR or NL. */
10640 for (len = 0; p[len] >= ' '; ++len)
10641 ;
10642 p[len] = NUL;
10643
10644 /* If the suggestion doesn't have specific case duplicate the case
10645 * of the bad word. */
10646 if (captype(p, NULL) == 0)
10647 {
10648 make_case_word(p, cword, su->su_badflags);
10649 p = cword;
10650 }
10651
10652 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010653 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010654 }
10655 }
10656
10657 fclose(fd);
10658
Bram Moolenaar4770d092006-01-12 23:22:24 +000010659 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10660 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010661 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10662}
10663
10664/*
10665 * Find suggestions for the internal method indicated by "sps_flags".
10666 */
10667 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010668spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010669 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010670 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010671{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010672 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010673 * Load the .sug file(s) that are available and not done yet.
10674 */
10675 suggest_load_files();
10676
10677 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010678 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010679 *
10680 * Set a maximum score to limit the combination of operations that is
10681 * tried.
10682 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010683 suggest_try_special(su);
10684
10685 /*
10686 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10687 * from the .aff file and inserting a space (split the word).
10688 */
10689 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010690
10691 /* For the resulting top-scorers compute the sound-a-like score. */
10692 if (sps_flags & SPS_DOUBLE)
10693 score_comp_sal(su);
10694
10695 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010696 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010697 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010698 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010699 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010700 if (sps_flags & SPS_BEST)
10701 /* Adjust the word score for the suggestions found so far for how
10702 * they sounds like. */
10703 rescore_suggestions(su);
10704
10705 /*
10706 * While going throught the soundfold tree "su_maxscore" is the score
10707 * for the soundfold word, limits the changes that are being tried,
10708 * and "su_sfmaxscore" the rescored score, which is set by
10709 * cleanup_suggestions().
10710 * First find words with a small edit distance, because this is much
10711 * faster and often already finds the top-N suggestions. If we didn't
10712 * find many suggestions try again with a higher edit distance.
10713 * "sl_sounddone" is used to avoid doing the same word twice.
10714 */
10715 suggest_try_soundalike_prep();
10716 su->su_maxscore = SCORE_SFMAX1;
10717 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010718 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010719 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10720 {
10721 /* We didn't find enough matches, try again, allowing more
10722 * changes to the soundfold word. */
10723 su->su_maxscore = SCORE_SFMAX2;
10724 suggest_try_soundalike(su);
10725 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10726 {
10727 /* Still didn't find enough matches, try again, allowing even
10728 * more changes to the soundfold word. */
10729 su->su_maxscore = SCORE_SFMAX3;
10730 suggest_try_soundalike(su);
10731 }
10732 }
10733 su->su_maxscore = su->su_sfmaxscore;
10734 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010735 }
10736
Bram Moolenaar4770d092006-01-12 23:22:24 +000010737 /* When CTRL-C was hit while searching do show the results. Only clear
10738 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010739 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010740 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010741 {
10742 (void)vgetc();
10743 got_int = FALSE;
10744 }
10745
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010746 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010747 {
10748 if (sps_flags & SPS_BEST)
10749 /* Adjust the word score for how it sounds like. */
10750 rescore_suggestions(su);
10751
Bram Moolenaar4770d092006-01-12 23:22:24 +000010752 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10753 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010754 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010755 }
10756}
10757
10758/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010759 * Load the .sug files for languages that have one and weren't loaded yet.
10760 */
10761 static void
10762suggest_load_files()
10763{
10764 langp_T *lp;
10765 int lpi;
10766 slang_T *slang;
10767 char_u *dotp;
10768 FILE *fd;
10769 char_u buf[MAXWLEN];
10770 int i;
10771 time_t timestamp;
10772 int wcount;
10773 int wordnr;
10774 garray_T ga;
10775 int c;
10776
10777 /* Do this for all languages that support sound folding. */
10778 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
10779 {
10780 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
10781 slang = lp->lp_slang;
10782 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10783 {
10784 /* Change ".spl" to ".sug" and open the file. When the file isn't
10785 * found silently skip it. Do set "sl_sugloaded" so that we
10786 * don't try again and again. */
10787 slang->sl_sugloaded = TRUE;
10788
10789 dotp = vim_strrchr(slang->sl_fname, '.');
10790 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10791 continue;
10792 STRCPY(dotp, ".sug");
Bram Moolenaar5555acc2006-04-07 21:33:12 +000010793 fd = mch_fopen((char *)slang->sl_fname, "r");
Bram Moolenaar4770d092006-01-12 23:22:24 +000010794 if (fd == NULL)
10795 goto nextone;
10796
10797 /*
10798 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10799 */
10800 for (i = 0; i < VIMSUGMAGICL; ++i)
10801 buf[i] = getc(fd); /* <fileID> */
10802 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10803 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010804 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010805 slang->sl_fname);
10806 goto nextone;
10807 }
10808 c = getc(fd); /* <versionnr> */
10809 if (c < VIMSUGVERSION)
10810 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010811 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010812 slang->sl_fname);
10813 goto nextone;
10814 }
10815 else if (c > VIMSUGVERSION)
10816 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010817 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010818 slang->sl_fname);
10819 goto nextone;
10820 }
10821
10822 /* Check the timestamp, it must be exactly the same as the one in
10823 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010824 timestamp = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010825 if (timestamp != slang->sl_sugtime)
10826 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010827 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010828 slang->sl_fname);
10829 goto nextone;
10830 }
10831
10832 /*
10833 * <SUGWORDTREE>: <wordtree>
10834 * Read the trie with the soundfolded words.
10835 */
10836 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10837 FALSE, 0) != 0)
10838 {
10839someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010840 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010841 slang->sl_fname);
10842 slang_clear_sug(slang);
10843 goto nextone;
10844 }
10845
10846 /*
10847 * <SUGTABLE>: <sugwcount> <sugline> ...
10848 *
10849 * Read the table with word numbers. We use a file buffer for
10850 * this, because it's so much like a file with lines. Makes it
10851 * possible to swap the info and save on memory use.
10852 */
10853 slang->sl_sugbuf = open_spellbuf();
10854 if (slang->sl_sugbuf == NULL)
10855 goto someerror;
10856 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010857 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010858 if (wcount < 0)
10859 goto someerror;
10860
10861 /* Read all the wordnr lists into the buffer, one NUL terminated
10862 * list per line. */
10863 ga_init2(&ga, 1, 100);
10864 for (wordnr = 0; wordnr < wcount; ++wordnr)
10865 {
10866 ga.ga_len = 0;
10867 for (;;)
10868 {
10869 c = getc(fd); /* <sugline> */
10870 if (c < 0 || ga_grow(&ga, 1) == FAIL)
10871 goto someerror;
10872 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
10873 if (c == NUL)
10874 break;
10875 }
10876 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
10877 ga.ga_data, ga.ga_len, TRUE) == FAIL)
10878 goto someerror;
10879 }
10880 ga_clear(&ga);
10881
10882 /*
10883 * Need to put word counts in the word tries, so that we can find
10884 * a word by its number.
10885 */
10886 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
10887 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
10888
10889nextone:
10890 if (fd != NULL)
10891 fclose(fd);
10892 STRCPY(dotp, ".spl");
10893 }
10894 }
10895}
10896
10897
10898/*
10899 * Fill in the wordcount fields for a trie.
10900 * Returns the total number of words.
10901 */
10902 static void
10903tree_count_words(byts, idxs)
10904 char_u *byts;
10905 idx_T *idxs;
10906{
10907 int depth;
10908 idx_T arridx[MAXWLEN];
10909 int curi[MAXWLEN];
10910 int c;
10911 idx_T n;
10912 int wordcount[MAXWLEN];
10913
10914 arridx[0] = 0;
10915 curi[0] = 1;
10916 wordcount[0] = 0;
10917 depth = 0;
10918 while (depth >= 0 && !got_int)
10919 {
10920 if (curi[depth] > byts[arridx[depth]])
10921 {
10922 /* Done all bytes at this node, go up one level. */
10923 idxs[arridx[depth]] = wordcount[depth];
10924 if (depth > 0)
10925 wordcount[depth - 1] += wordcount[depth];
10926
10927 --depth;
10928 fast_breakcheck();
10929 }
10930 else
10931 {
10932 /* Do one more byte at this node. */
10933 n = arridx[depth] + curi[depth];
10934 ++curi[depth];
10935
10936 c = byts[n];
10937 if (c == 0)
10938 {
10939 /* End of word, count it. */
10940 ++wordcount[depth];
10941
10942 /* Skip over any other NUL bytes (same word with different
10943 * flags). */
10944 while (byts[n + 1] == 0)
10945 {
10946 ++n;
10947 ++curi[depth];
10948 }
10949 }
10950 else
10951 {
10952 /* Normal char, go one level deeper to count the words. */
10953 ++depth;
10954 arridx[depth] = idxs[n];
10955 curi[depth] = 1;
10956 wordcount[depth] = 0;
10957 }
10958 }
10959 }
10960}
10961
10962/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010963 * Free the info put in "*su" by spell_find_suggest().
10964 */
10965 static void
10966spell_find_cleanup(su)
10967 suginfo_T *su;
10968{
10969 int i;
10970
10971 /* Free the suggestions. */
10972 for (i = 0; i < su->su_ga.ga_len; ++i)
10973 vim_free(SUG(su->su_ga, i).st_word);
10974 ga_clear(&su->su_ga);
10975 for (i = 0; i < su->su_sga.ga_len; ++i)
10976 vim_free(SUG(su->su_sga, i).st_word);
10977 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010978
10979 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010980 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010981}
10982
10983/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010984 * Make a copy of "word", with the first letter upper or lower cased, to
10985 * "wcopy[MAXWLEN]". "word" must not be empty.
10986 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010987 */
10988 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010989onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010990 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010991 char_u *wcopy;
10992 int upper; /* TRUE: first letter made upper case */
10993{
10994 char_u *p;
10995 int c;
10996 int l;
10997
10998 p = word;
10999#ifdef FEAT_MBYTE
11000 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011001 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011002 else
11003#endif
11004 c = *p++;
11005 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011006 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011007 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011008 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011009#ifdef FEAT_MBYTE
11010 if (has_mbyte)
11011 l = mb_char2bytes(c, wcopy);
11012 else
11013#endif
11014 {
11015 l = 1;
11016 wcopy[0] = c;
11017 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011018 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011019}
11020
11021/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011022 * Make a copy of "word" with all the letters upper cased into
11023 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011024 */
11025 static void
11026allcap_copy(word, wcopy)
11027 char_u *word;
11028 char_u *wcopy;
11029{
11030 char_u *s;
11031 char_u *d;
11032 int c;
11033
11034 d = wcopy;
11035 for (s = word; *s != NUL; )
11036 {
11037#ifdef FEAT_MBYTE
11038 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011039 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011040 else
11041#endif
11042 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000011043
11044#ifdef FEAT_MBYTE
11045 /* We only change ß to SS when we are certain latin1 is used. It
11046 * would cause weird errors in other 8-bit encodings. */
11047 if (enc_latin1like && c == 0xdf)
11048 {
11049 c = 'S';
11050 if (d - wcopy >= MAXWLEN - 1)
11051 break;
11052 *d++ = c;
11053 }
11054 else
11055#endif
11056 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011057
11058#ifdef FEAT_MBYTE
11059 if (has_mbyte)
11060 {
11061 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
11062 break;
11063 d += mb_char2bytes(c, d);
11064 }
11065 else
11066#endif
11067 {
11068 if (d - wcopy >= MAXWLEN - 1)
11069 break;
11070 *d++ = c;
11071 }
11072 }
11073 *d = NUL;
11074}
11075
11076/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000011077 * Try finding suggestions by recognizing specific situations.
11078 */
11079 static void
11080suggest_try_special(su)
11081 suginfo_T *su;
11082{
11083 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011084 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011085 int c;
11086 char_u word[MAXWLEN];
11087
11088 /*
11089 * Recognize a word that is repeated: "the the".
11090 */
11091 p = skiptowhite(su->su_fbadword);
11092 len = p - su->su_fbadword;
11093 p = skipwhite(p);
11094 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
11095 {
11096 /* Include badflags: if the badword is onecap or allcap
11097 * use that for the goodword too: "The the" -> "The". */
11098 c = su->su_fbadword[len];
11099 su->su_fbadword[len] = NUL;
11100 make_case_word(su->su_fbadword, word, su->su_badflags);
11101 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011102
11103 /* Give a soundalike score of 0, compute the score as if deleting one
11104 * character. */
11105 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011106 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011107 }
11108}
11109
11110/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011111 * Try finding suggestions by adding/removing/swapping letters.
11112 */
11113 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000011114suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011115 suginfo_T *su;
11116{
11117 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011118 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011119 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011120 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011121 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011122
11123 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000011124 * to find matches (esp. REP items). Append some more text, changing
11125 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011126 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011127 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011128 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011129 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011130
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011131 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011132 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011133 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011134
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011135 /* If reloading a spell file fails it's still in the list but
11136 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011137 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011138 continue;
11139
Bram Moolenaar4770d092006-01-12 23:22:24 +000011140 /* Try it for this language. Will add possible suggestions. */
11141 suggest_trie_walk(su, lp, fword, FALSE);
11142 }
11143}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011144
Bram Moolenaar4770d092006-01-12 23:22:24 +000011145/* Check the maximum score, if we go over it we won't try this change. */
11146#define TRY_DEEPER(su, stack, depth, add) \
11147 (stack[depth].ts_score + (add) < su->su_maxscore)
11148
11149/*
11150 * Try finding suggestions by adding/removing/swapping letters.
11151 *
11152 * This uses a state machine. At each node in the tree we try various
11153 * operations. When trying if an operation works "depth" is increased and the
11154 * stack[] is used to store info. This allows combinations, thus insert one
11155 * character, replace one and delete another. The number of changes is
11156 * limited by su->su_maxscore.
11157 *
11158 * After implementing this I noticed an article by Kemal Oflazer that
11159 * describes something similar: "Error-tolerant Finite State Recognition with
11160 * Applications to Morphological Analysis and Spelling Correction" (1996).
11161 * The implementation in the article is simplified and requires a stack of
11162 * unknown depth. The implementation here only needs a stack depth equal to
11163 * the length of the word.
11164 *
11165 * This is also used for the sound-folded word, "soundfold" is TRUE then.
11166 * The mechanism is the same, but we find a match with a sound-folded word
11167 * that comes from one or more original words. Each of these words may be
11168 * added, this is done by add_sound_suggest().
11169 * Don't use:
11170 * the prefix tree or the keep-case tree
11171 * "su->su_badlen"
11172 * anything to do with upper and lower case
11173 * anything to do with word or non-word characters ("spell_iswordp()")
11174 * banned words
11175 * word flags (rare, region, compounding)
11176 * word splitting for now
11177 * "similar_chars()"
11178 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
11179 */
11180 static void
11181suggest_trie_walk(su, lp, fword, soundfold)
11182 suginfo_T *su;
11183 langp_T *lp;
11184 char_u *fword;
11185 int soundfold;
11186{
11187 char_u tword[MAXWLEN]; /* good word collected so far */
11188 trystate_T stack[MAXWLEN];
11189 char_u preword[MAXWLEN * 3]; /* word found with proper case;
11190 * concatanation of prefix compound
11191 * words and split word. NUL terminated
11192 * when going deeper but not when coming
11193 * back. */
11194 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
11195 trystate_T *sp;
11196 int newscore;
11197 int score;
11198 char_u *byts, *fbyts, *pbyts;
11199 idx_T *idxs, *fidxs, *pidxs;
11200 int depth;
11201 int c, c2, c3;
11202 int n = 0;
11203 int flags;
11204 garray_T *gap;
11205 idx_T arridx;
11206 int len;
11207 char_u *p;
11208 fromto_T *ftp;
11209 int fl = 0, tl;
11210 int repextra = 0; /* extra bytes in fword[] from REP item */
11211 slang_T *slang = lp->lp_slang;
11212 int fword_ends;
11213 int goodword_ends;
11214#ifdef DEBUG_TRIEWALK
11215 /* Stores the name of the change made at each level. */
11216 char_u changename[MAXWLEN][80];
11217#endif
11218 int breakcheckcount = 1000;
11219 int compound_ok;
11220
11221 /*
11222 * Go through the whole case-fold tree, try changes at each node.
11223 * "tword[]" contains the word collected from nodes in the tree.
11224 * "fword[]" the word we are trying to match with (initially the bad
11225 * word).
11226 */
11227 depth = 0;
11228 sp = &stack[0];
11229 vim_memset(sp, 0, sizeof(trystate_T));
11230 sp->ts_curi = 1;
11231
11232 if (soundfold)
11233 {
11234 /* Going through the soundfold tree. */
11235 byts = fbyts = slang->sl_sbyts;
11236 idxs = fidxs = slang->sl_sidxs;
11237 pbyts = NULL;
11238 pidxs = NULL;
11239 sp->ts_prefixdepth = PFD_NOPREFIX;
11240 sp->ts_state = STATE_START;
11241 }
11242 else
11243 {
Bram Moolenaarea424162005-06-16 21:51:00 +000011244 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011245 * When there are postponed prefixes we need to use these first. At
11246 * the end of the prefix we continue in the case-fold tree.
11247 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011248 fbyts = slang->sl_fbyts;
11249 fidxs = slang->sl_fidxs;
11250 pbyts = slang->sl_pbyts;
11251 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011252 if (pbyts != NULL)
11253 {
11254 byts = pbyts;
11255 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011256 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011257 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
11258 }
11259 else
11260 {
11261 byts = fbyts;
11262 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011263 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011264 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011265 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011266 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011267
Bram Moolenaar4770d092006-01-12 23:22:24 +000011268 /*
11269 * Loop to find all suggestions. At each round we either:
11270 * - For the current state try one operation, advance "ts_curi",
11271 * increase "depth".
11272 * - When a state is done go to the next, set "ts_state".
11273 * - When all states are tried decrease "depth".
11274 */
11275 while (depth >= 0 && !got_int)
11276 {
11277 sp = &stack[depth];
11278 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011279 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011280 case STATE_START:
11281 case STATE_NOPREFIX:
11282 /*
11283 * Start of node: Deal with NUL bytes, which means
11284 * tword[] may end here.
11285 */
11286 arridx = sp->ts_arridx; /* current node in the tree */
11287 len = byts[arridx]; /* bytes in this node */
11288 arridx += sp->ts_curi; /* index of current byte */
11289
11290 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011291 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011292 /* Skip over the NUL bytes, we use them later. */
11293 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
11294 ;
11295 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011296
Bram Moolenaar4770d092006-01-12 23:22:24 +000011297 /* Always past NUL bytes now. */
11298 n = (int)sp->ts_state;
11299 sp->ts_state = STATE_ENDNUL;
11300 sp->ts_save_badflags = su->su_badflags;
11301
11302 /* At end of a prefix or at start of prefixtree: check for
11303 * following word. */
11304 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011305 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011306 /* Set su->su_badflags to the caps type at this position.
11307 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000011308#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011309 if (has_mbyte)
11310 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
11311 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000011312#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011313 n = sp->ts_fidx;
11314 flags = badword_captype(su->su_badptr, su->su_badptr + n);
11315 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011316 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011317#ifdef DEBUG_TRIEWALK
11318 sprintf(changename[depth], "prefix");
11319#endif
11320 go_deeper(stack, depth, 0);
11321 ++depth;
11322 sp = &stack[depth];
11323 sp->ts_prefixdepth = depth - 1;
11324 byts = fbyts;
11325 idxs = fidxs;
11326 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011327
Bram Moolenaar4770d092006-01-12 23:22:24 +000011328 /* Move the prefix to preword[] with the right case
11329 * and make find_keepcap_word() works. */
11330 tword[sp->ts_twordlen] = NUL;
11331 make_case_word(tword + sp->ts_splitoff,
11332 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011333 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011334 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011335 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011336 break;
11337 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011338
Bram Moolenaar4770d092006-01-12 23:22:24 +000011339 if (sp->ts_curi > len || byts[arridx] != 0)
11340 {
11341 /* Past bytes in node and/or past NUL bytes. */
11342 sp->ts_state = STATE_ENDNUL;
11343 sp->ts_save_badflags = su->su_badflags;
11344 break;
11345 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011346
Bram Moolenaar4770d092006-01-12 23:22:24 +000011347 /*
11348 * End of word in tree.
11349 */
11350 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011351
Bram Moolenaar4770d092006-01-12 23:22:24 +000011352 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011353
11354 /* Skip words with the NOSUGGEST flag. */
11355 if (flags & WF_NOSUGGEST)
11356 break;
11357
Bram Moolenaar4770d092006-01-12 23:22:24 +000011358 fword_ends = (fword[sp->ts_fidx] == NUL
11359 || (soundfold
11360 ? vim_iswhite(fword[sp->ts_fidx])
11361 : !spell_iswordp(fword + sp->ts_fidx, curbuf)));
11362 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011363
Bram Moolenaar4770d092006-01-12 23:22:24 +000011364 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000011365 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011366 {
11367 /* There was a prefix before the word. Check that the prefix
11368 * can be used with this word. */
11369 /* Count the length of the NULs in the prefix. If there are
11370 * none this must be the first try without a prefix. */
11371 n = stack[sp->ts_prefixdepth].ts_arridx;
11372 len = pbyts[n++];
11373 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
11374 ;
11375 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011376 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011377 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011378 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011379 if (c == 0)
11380 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011381
Bram Moolenaar4770d092006-01-12 23:22:24 +000011382 /* Use the WF_RARE flag for a rare prefix. */
11383 if (c & WF_RAREPFX)
11384 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011385
Bram Moolenaar4770d092006-01-12 23:22:24 +000011386 /* Tricky: when checking for both prefix and compounding
11387 * we run into the prefix flag first.
11388 * Remember that it's OK, so that we accept the prefix
11389 * when arriving at a compound flag. */
11390 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011391 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011392 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011393
Bram Moolenaar4770d092006-01-12 23:22:24 +000011394 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
11395 * appending another compound word below. */
11396 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011397 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011398 goodword_ends = FALSE;
11399 else
11400 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011401
Bram Moolenaar4770d092006-01-12 23:22:24 +000011402 p = NULL;
11403 compound_ok = TRUE;
11404 if (sp->ts_complen > sp->ts_compsplit)
11405 {
11406 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000011407 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011408 /* There was a word before this word. When there was no
11409 * change in this word (it was correct) add the first word
11410 * as a suggestion. If this word was corrected too, we
11411 * need to check if a correct word follows. */
11412 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000011413 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000011414 && STRNCMP(fword + sp->ts_splitfidx,
11415 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000011416 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011417 {
11418 preword[sp->ts_prewordlen] = NUL;
11419 newscore = score_wordcount_adj(slang, sp->ts_score,
11420 preword + sp->ts_prewordlen,
11421 sp->ts_prewordlen > 0);
11422 /* Add the suggestion if the score isn't too bad. */
11423 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000011424 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011425 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011426 newscore, 0, FALSE,
11427 lp->lp_sallang, FALSE);
11428 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000011429 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011430 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000011431 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000011432 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011433 /* There was a compound word before this word. If this
11434 * word does not support compounding then give up
11435 * (splitting is tried for the word without compound
11436 * flag). */
11437 if (((unsigned)flags >> 24) == 0
11438 || sp->ts_twordlen - sp->ts_splitoff
11439 < slang->sl_compminlen)
11440 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011441#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011442 /* For multi-byte chars check character length against
11443 * COMPOUNDMIN. */
11444 if (has_mbyte
11445 && slang->sl_compminlen > 0
11446 && mb_charlen(tword + sp->ts_splitoff)
11447 < slang->sl_compminlen)
11448 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011449#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000011450
Bram Moolenaar4770d092006-01-12 23:22:24 +000011451 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11452 compflags[sp->ts_complen + 1] = NUL;
11453 vim_strncpy(preword + sp->ts_prewordlen,
11454 tword + sp->ts_splitoff,
11455 sp->ts_twordlen - sp->ts_splitoff);
11456 p = preword;
11457 while (*skiptowhite(p) != NUL)
11458 p = skipwhite(skiptowhite(p));
11459 if (fword_ends && !can_compound(slang, p,
11460 compflags + sp->ts_compsplit))
11461 /* Compound is not allowed. But it may still be
11462 * possible if we add another (short) word. */
11463 compound_ok = FALSE;
11464
11465 /* Get pointer to last char of previous word. */
11466 p = preword + sp->ts_prewordlen;
11467 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011468 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011469 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011470
Bram Moolenaar4770d092006-01-12 23:22:24 +000011471 /*
11472 * Form the word with proper case in preword.
11473 * If there is a word from a previous split, append.
11474 * For the soundfold tree don't change the case, simply append.
11475 */
11476 if (soundfold)
11477 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11478 else if (flags & WF_KEEPCAP)
11479 /* Must find the word in the keep-case tree. */
11480 find_keepcap_word(slang, tword + sp->ts_splitoff,
11481 preword + sp->ts_prewordlen);
11482 else
11483 {
11484 /* Include badflags: If the badword is onecap or allcap
11485 * use that for the goodword too. But if the badword is
11486 * allcap and it's only one char long use onecap. */
11487 c = su->su_badflags;
11488 if ((c & WF_ALLCAP)
11489#ifdef FEAT_MBYTE
11490 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11491#else
11492 && su->su_badlen == 1
11493#endif
11494 )
11495 c = WF_ONECAP;
11496 c |= flags;
11497
11498 /* When appending a compound word after a word character don't
11499 * use Onecap. */
11500 if (p != NULL && spell_iswordp_nmw(p))
11501 c &= ~WF_ONECAP;
11502 make_case_word(tword + sp->ts_splitoff,
11503 preword + sp->ts_prewordlen, c);
11504 }
11505
11506 if (!soundfold)
11507 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011508 /* Don't use a banned word. It may appear again as a good
11509 * word, thus remember it. */
11510 if (flags & WF_BANNED)
11511 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000011512 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011513 break;
11514 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011515 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000011516 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11517 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011518 {
11519 if (slang->sl_compprog == NULL)
11520 break;
11521 /* the word so far was banned but we may try compounding */
11522 goodword_ends = FALSE;
11523 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011524 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011525
Bram Moolenaar4770d092006-01-12 23:22:24 +000011526 newscore = 0;
11527 if (!soundfold) /* soundfold words don't have flags */
11528 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011529 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011530 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011531 newscore += SCORE_REGION;
11532 if (flags & WF_RARE)
11533 newscore += SCORE_RARE;
11534
Bram Moolenaar0c405862005-06-22 22:26:26 +000011535 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011536 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011537 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011538 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011539
Bram Moolenaar4770d092006-01-12 23:22:24 +000011540 /* TODO: how about splitting in the soundfold tree? */
11541 if (fword_ends
11542 && goodword_ends
11543 && sp->ts_fidx >= sp->ts_fidxtry
11544 && compound_ok)
11545 {
11546 /* The badword also ends: add suggestions. */
11547#ifdef DEBUG_TRIEWALK
11548 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011549 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011550 int j;
11551
11552 /* print the stack of changes that brought us here */
11553 smsg("------ %s -------", fword);
11554 for (j = 0; j < depth; ++j)
11555 smsg("%s", changename[j]);
11556 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011557#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011558 if (soundfold)
11559 {
11560 /* For soundfolded words we need to find the original
Bram Moolenaarf711faf2007-05-10 16:48:19 +000011561 * words, the edit distance and then add them. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011562 add_sound_suggest(su, preword, sp->ts_score, lp);
11563 }
11564 else
11565 {
11566 /* Give a penalty when changing non-word char to word
11567 * char, e.g., "thes," -> "these". */
11568 p = fword + sp->ts_fidx;
11569 mb_ptr_back(fword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011570 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011571 {
11572 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011573 mb_ptr_back(preword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011574 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011575 newscore += SCORE_NONWORD;
11576 }
11577
Bram Moolenaar4770d092006-01-12 23:22:24 +000011578 /* Give a bonus to words seen before. */
11579 score = score_wordcount_adj(slang,
11580 sp->ts_score + newscore,
11581 preword + sp->ts_prewordlen,
11582 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011583
Bram Moolenaar4770d092006-01-12 23:22:24 +000011584 /* Add the suggestion if the score isn't too bad. */
11585 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011586 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011587 add_suggestion(su, &su->su_ga, preword,
11588 sp->ts_fidx - repextra,
11589 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011590
11591 if (su->su_badflags & WF_MIXCAP)
11592 {
11593 /* We really don't know if the word should be
11594 * upper or lower case, add both. */
11595 c = captype(preword, NULL);
11596 if (c == 0 || c == WF_ALLCAP)
11597 {
11598 make_case_word(tword + sp->ts_splitoff,
11599 preword + sp->ts_prewordlen,
11600 c == 0 ? WF_ALLCAP : 0);
11601
11602 add_suggestion(su, &su->su_ga, preword,
11603 sp->ts_fidx - repextra,
11604 score + SCORE_ICASE, 0, FALSE,
11605 lp->lp_sallang, FALSE);
11606 }
11607 }
11608 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011609 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011610 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011611
Bram Moolenaar4770d092006-01-12 23:22:24 +000011612 /*
11613 * Try word split and/or compounding.
11614 */
11615 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011616#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011617 /* Don't split halfway a character. */
11618 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011619#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011620 )
11621 {
11622 int try_compound;
11623 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011624
Bram Moolenaar4770d092006-01-12 23:22:24 +000011625 /* If past the end of the bad word don't try a split.
11626 * Otherwise try changing the next word. E.g., find
11627 * suggestions for "the the" where the second "the" is
11628 * different. It's done like a split.
11629 * TODO: word split for soundfold words */
11630 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11631 && !soundfold;
11632
11633 /* Get here in several situations:
11634 * 1. The word in the tree ends:
11635 * If the word allows compounding try that. Otherwise try
11636 * a split by inserting a space. For both check that a
11637 * valid words starts at fword[sp->ts_fidx].
11638 * For NOBREAK do like compounding to be able to check if
11639 * the next word is valid.
11640 * 2. The badword does end, but it was due to a change (e.g.,
11641 * a swap). No need to split, but do check that the
11642 * following word is valid.
11643 * 3. The badword and the word in the tree end. It may still
11644 * be possible to compound another (short) word.
11645 */
11646 try_compound = FALSE;
11647 if (!soundfold
11648 && slang->sl_compprog != NULL
11649 && ((unsigned)flags >> 24) != 0
11650 && sp->ts_twordlen - sp->ts_splitoff
11651 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011652#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011653 && (!has_mbyte
11654 || slang->sl_compminlen == 0
11655 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011656 >= slang->sl_compminlen)
11657#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011658 && (slang->sl_compsylmax < MAXWLEN
11659 || sp->ts_complen + 1 - sp->ts_compsplit
11660 < slang->sl_compmax)
11661 && (byte_in_str(sp->ts_complen == sp->ts_compsplit
11662 ? slang->sl_compstartflags
11663 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +000011664 ((unsigned)flags >> 24))))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011665 {
11666 try_compound = TRUE;
11667 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11668 compflags[sp->ts_complen + 1] = NUL;
11669 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011670
Bram Moolenaar4770d092006-01-12 23:22:24 +000011671 /* For NOBREAK we never try splitting, it won't make any word
11672 * valid. */
11673 if (slang->sl_nobreak)
11674 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011675
Bram Moolenaar4770d092006-01-12 23:22:24 +000011676 /* If we could add a compound word, and it's also possible to
11677 * split at this point, do the split first and set
11678 * TSF_DIDSPLIT to avoid doing it again. */
11679 else if (!fword_ends
11680 && try_compound
11681 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11682 {
11683 try_compound = FALSE;
11684 sp->ts_flags |= TSF_DIDSPLIT;
11685 --sp->ts_curi; /* do the same NUL again */
11686 compflags[sp->ts_complen] = NUL;
11687 }
11688 else
11689 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011690
Bram Moolenaar4770d092006-01-12 23:22:24 +000011691 if (try_split || try_compound)
11692 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011693 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011694 {
11695 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011696 * words so far are valid for compounding. If there
11697 * is only one word it must not have the NEEDCOMPOUND
11698 * flag. */
11699 if (sp->ts_complen == sp->ts_compsplit
11700 && (flags & WF_NEEDCOMP))
11701 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011702 p = preword;
11703 while (*skiptowhite(p) != NUL)
11704 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011705 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011706 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011707 compflags + sp->ts_compsplit))
11708 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011709
11710 if (slang->sl_nosplitsugs)
11711 newscore += SCORE_SPLIT_NO;
11712 else
11713 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011714
11715 /* Give a bonus to words seen before. */
11716 newscore = score_wordcount_adj(slang, newscore,
11717 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011718 }
11719
Bram Moolenaar4770d092006-01-12 23:22:24 +000011720 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011721 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011722 go_deeper(stack, depth, newscore);
11723#ifdef DEBUG_TRIEWALK
11724 if (!try_compound && !fword_ends)
11725 sprintf(changename[depth], "%.*s-%s: split",
11726 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11727 else
11728 sprintf(changename[depth], "%.*s-%s: compound",
11729 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11730#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011731 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011732 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011733 sp->ts_state = STATE_SPLITUNDO;
11734
11735 ++depth;
11736 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011737
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011738 /* Append a space to preword when splitting. */
11739 if (!try_compound && !fword_ends)
11740 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011741 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011742 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011743 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011744
11745 /* If the badword has a non-word character at this
11746 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011747 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011748 * character when the word ends. But only when the
11749 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011750 if (((!try_compound && !spell_iswordp_nmw(fword
11751 + sp->ts_fidx))
11752 || fword_ends)
11753 && fword[sp->ts_fidx] != NUL
11754 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011755 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011756 int l;
11757
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011758#ifdef FEAT_MBYTE
11759 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011760 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011761 else
11762#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011763 l = 1;
11764 if (fword_ends)
11765 {
11766 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011767 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011768 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011769 sp->ts_prewordlen += l;
11770 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011771 }
11772 else
11773 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11774 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011775 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011776
Bram Moolenaard12a1322005-08-21 22:08:24 +000011777 /* When compounding include compound flag in
11778 * compflags[] (already set above). When splitting we
11779 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011780 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011781 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011782 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011783 sp->ts_compsplit = sp->ts_complen;
11784 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011785
Bram Moolenaar53805d12005-08-01 07:08:33 +000011786 /* set su->su_badflags to the caps type at this
11787 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011788#ifdef FEAT_MBYTE
11789 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011790 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011791 else
11792#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011793 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011794 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011795 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011796
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011797 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011798 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011799
11800 /* If there are postponed prefixes, try these too. */
11801 if (pbyts != NULL)
11802 {
11803 byts = pbyts;
11804 idxs = pidxs;
11805 sp->ts_prefixdepth = PFD_PREFIXTREE;
11806 sp->ts_state = STATE_NOPREFIX;
11807 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011808 }
11809 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011810 }
11811 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011812
Bram Moolenaar4770d092006-01-12 23:22:24 +000011813 case STATE_SPLITUNDO:
11814 /* Undo the changes done for word split or compound word. */
11815 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011816
Bram Moolenaar4770d092006-01-12 23:22:24 +000011817 /* Continue looking for NUL bytes. */
11818 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011819
Bram Moolenaar4770d092006-01-12 23:22:24 +000011820 /* In case we went into the prefix tree. */
11821 byts = fbyts;
11822 idxs = fidxs;
11823 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011824
Bram Moolenaar4770d092006-01-12 23:22:24 +000011825 case STATE_ENDNUL:
11826 /* Past the NUL bytes in the node. */
11827 su->su_badflags = sp->ts_save_badflags;
11828 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011829#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011830 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011831#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011832 )
11833 {
11834 /* The badword ends, can't use STATE_PLAIN. */
11835 sp->ts_state = STATE_DEL;
11836 break;
11837 }
11838 sp->ts_state = STATE_PLAIN;
11839 /*FALLTHROUGH*/
11840
11841 case STATE_PLAIN:
11842 /*
11843 * Go over all possible bytes at this node, add each to tword[]
11844 * and use child node. "ts_curi" is the index.
11845 */
11846 arridx = sp->ts_arridx;
11847 if (sp->ts_curi > byts[arridx])
11848 {
11849 /* Done all bytes at this node, do next state. When still at
11850 * already changed bytes skip the other tricks. */
11851 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011852 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011853 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011854 sp->ts_state = STATE_FINAL;
11855 }
11856 else
11857 {
11858 arridx += sp->ts_curi++;
11859 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011860
Bram Moolenaar4770d092006-01-12 23:22:24 +000011861 /* Normal byte, go one level deeper. If it's not equal to the
11862 * byte in the bad word adjust the score. But don't even try
11863 * when the byte was already changed. And don't try when we
11864 * just deleted this byte, accepting it is always cheaper then
11865 * delete + substitute. */
11866 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000011867#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011868 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011869#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011870 )
11871 newscore = 0;
11872 else
11873 newscore = SCORE_SUBST;
11874 if ((newscore == 0
11875 || (sp->ts_fidx >= sp->ts_fidxtry
11876 && ((sp->ts_flags & TSF_DIDDEL) == 0
11877 || c != fword[sp->ts_delidx])))
11878 && TRY_DEEPER(su, stack, depth, newscore))
11879 {
11880 go_deeper(stack, depth, newscore);
11881#ifdef DEBUG_TRIEWALK
11882 if (newscore > 0)
11883 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
11884 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11885 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011886 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011887 sprintf(changename[depth], "%.*s-%s: accept %c",
11888 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11889 fword[sp->ts_fidx]);
11890#endif
11891 ++depth;
11892 sp = &stack[depth];
11893 ++sp->ts_fidx;
11894 tword[sp->ts_twordlen++] = c;
11895 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000011896#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011897 if (newscore == SCORE_SUBST)
11898 sp->ts_isdiff = DIFF_YES;
11899 if (has_mbyte)
11900 {
11901 /* Multi-byte characters are a bit complicated to
11902 * handle: They differ when any of the bytes differ
11903 * and then their length may also differ. */
11904 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011905 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011906 /* First byte. */
11907 sp->ts_tcharidx = 0;
11908 sp->ts_tcharlen = MB_BYTE2LEN(c);
11909 sp->ts_fcharstart = sp->ts_fidx - 1;
11910 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011911 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011912 }
11913 else if (sp->ts_isdiff == DIFF_INSERT)
11914 /* When inserting trail bytes don't advance in the
11915 * bad word. */
11916 --sp->ts_fidx;
11917 if (++sp->ts_tcharidx == sp->ts_tcharlen)
11918 {
11919 /* Last byte of character. */
11920 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000011921 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011922 /* Correct ts_fidx for the byte length of the
11923 * character (we didn't check that before). */
11924 sp->ts_fidx = sp->ts_fcharstart
11925 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000011926 fword[sp->ts_fcharstart]);
11927
Bram Moolenaar4770d092006-01-12 23:22:24 +000011928 /* For changing a composing character adjust
11929 * the score from SCORE_SUBST to
11930 * SCORE_SUBCOMP. */
11931 if (enc_utf8
11932 && utf_iscomposing(
11933 mb_ptr2char(tword
11934 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011935 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011936 && utf_iscomposing(
11937 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011938 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011939 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011940 SCORE_SUBST - SCORE_SUBCOMP;
11941
Bram Moolenaar4770d092006-01-12 23:22:24 +000011942 /* For a similar character adjust score from
11943 * SCORE_SUBST to SCORE_SIMILAR. */
11944 else if (!soundfold
11945 && slang->sl_has_map
11946 && similar_chars(slang,
11947 mb_ptr2char(tword
11948 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000011949 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000011950 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000011951 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011952 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000011953 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000011954 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011955 else if (sp->ts_isdiff == DIFF_INSERT
11956 && sp->ts_twordlen > sp->ts_tcharlen)
11957 {
11958 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
11959 c = mb_ptr2char(p);
11960 if (enc_utf8 && utf_iscomposing(c))
11961 {
11962 /* Inserting a composing char doesn't
11963 * count that much. */
11964 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
11965 }
11966 else
11967 {
11968 /* If the previous character was the same,
11969 * thus doubling a character, give a bonus
11970 * to the score. Also for the soundfold
11971 * tree (might seem illogical but does
11972 * give better scores). */
11973 mb_ptr_back(tword, p);
11974 if (c == mb_ptr2char(p))
11975 sp->ts_score -= SCORE_INS
11976 - SCORE_INSDUP;
11977 }
11978 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011979
Bram Moolenaar4770d092006-01-12 23:22:24 +000011980 /* Starting a new char, reset the length. */
11981 sp->ts_tcharlen = 0;
11982 }
Bram Moolenaarea408852005-06-25 22:49:46 +000011983 }
Bram Moolenaarea424162005-06-16 21:51:00 +000011984 else
11985#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000011986 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011987 /* If we found a similar char adjust the score.
11988 * We do this after calling go_deeper() because
11989 * it's slow. */
11990 if (newscore != 0
11991 && !soundfold
11992 && slang->sl_has_map
11993 && similar_chars(slang,
11994 c, fword[sp->ts_fidx - 1]))
11995 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000011996 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011997 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011998 }
11999 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012000
Bram Moolenaar4770d092006-01-12 23:22:24 +000012001 case STATE_DEL:
12002#ifdef FEAT_MBYTE
12003 /* When past the first byte of a multi-byte char don't try
12004 * delete/insert/swap a character. */
12005 if (has_mbyte && sp->ts_tcharlen > 0)
12006 {
12007 sp->ts_state = STATE_FINAL;
12008 break;
12009 }
12010#endif
12011 /*
12012 * Try skipping one character in the bad word (delete it).
12013 */
12014 sp->ts_state = STATE_INS_PREP;
12015 sp->ts_curi = 1;
12016 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
12017 /* Deleting a vowel at the start of a word counts less, see
12018 * soundalike_score(). */
12019 newscore = 2 * SCORE_DEL / 3;
12020 else
12021 newscore = SCORE_DEL;
12022 if (fword[sp->ts_fidx] != NUL
12023 && TRY_DEEPER(su, stack, depth, newscore))
12024 {
12025 go_deeper(stack, depth, newscore);
12026#ifdef DEBUG_TRIEWALK
12027 sprintf(changename[depth], "%.*s-%s: delete %c",
12028 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12029 fword[sp->ts_fidx]);
12030#endif
12031 ++depth;
12032
12033 /* Remember what character we deleted, so that we can avoid
12034 * inserting it again. */
12035 stack[depth].ts_flags |= TSF_DIDDEL;
12036 stack[depth].ts_delidx = sp->ts_fidx;
12037
12038 /* Advance over the character in fword[]. Give a bonus to the
12039 * score if the same character is following "nn" -> "n". It's
12040 * a bit illogical for soundfold tree but it does give better
12041 * results. */
12042#ifdef FEAT_MBYTE
12043 if (has_mbyte)
12044 {
12045 c = mb_ptr2char(fword + sp->ts_fidx);
12046 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
12047 if (enc_utf8 && utf_iscomposing(c))
12048 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
12049 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
12050 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12051 }
12052 else
12053#endif
12054 {
12055 ++stack[depth].ts_fidx;
12056 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
12057 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12058 }
12059 break;
12060 }
12061 /*FALLTHROUGH*/
12062
12063 case STATE_INS_PREP:
12064 if (sp->ts_flags & TSF_DIDDEL)
12065 {
12066 /* If we just deleted a byte then inserting won't make sense,
12067 * a substitute is always cheaper. */
12068 sp->ts_state = STATE_SWAP;
12069 break;
12070 }
12071
12072 /* skip over NUL bytes */
12073 n = sp->ts_arridx;
12074 for (;;)
12075 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012076 if (sp->ts_curi > byts[n])
12077 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012078 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012079 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012080 break;
12081 }
12082 if (byts[n + sp->ts_curi] != NUL)
12083 {
12084 /* Found a byte to insert. */
12085 sp->ts_state = STATE_INS;
12086 break;
12087 }
12088 ++sp->ts_curi;
12089 }
12090 break;
12091
12092 /*FALLTHROUGH*/
12093
12094 case STATE_INS:
12095 /* Insert one byte. Repeat this for each possible byte at this
12096 * node. */
12097 n = sp->ts_arridx;
12098 if (sp->ts_curi > byts[n])
12099 {
12100 /* Done all bytes at this node, go to next state. */
12101 sp->ts_state = STATE_SWAP;
12102 break;
12103 }
12104
12105 /* Do one more byte at this node, but:
12106 * - Skip NUL bytes.
12107 * - Skip the byte if it's equal to the byte in the word,
12108 * accepting that byte is always better.
12109 */
12110 n += sp->ts_curi++;
12111 c = byts[n];
12112 if (soundfold && sp->ts_twordlen == 0 && c == '*')
12113 /* Inserting a vowel at the start of a word counts less,
12114 * see soundalike_score(). */
12115 newscore = 2 * SCORE_INS / 3;
12116 else
12117 newscore = SCORE_INS;
12118 if (c != fword[sp->ts_fidx]
12119 && TRY_DEEPER(su, stack, depth, newscore))
12120 {
12121 go_deeper(stack, depth, newscore);
12122#ifdef DEBUG_TRIEWALK
12123 sprintf(changename[depth], "%.*s-%s: insert %c",
12124 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12125 c);
12126#endif
12127 ++depth;
12128 sp = &stack[depth];
12129 tword[sp->ts_twordlen++] = c;
12130 sp->ts_arridx = idxs[n];
12131#ifdef FEAT_MBYTE
12132 if (has_mbyte)
12133 {
12134 fl = MB_BYTE2LEN(c);
12135 if (fl > 1)
12136 {
12137 /* There are following bytes for the same character.
12138 * We must find all bytes before trying
12139 * delete/insert/swap/etc. */
12140 sp->ts_tcharlen = fl;
12141 sp->ts_tcharidx = 1;
12142 sp->ts_isdiff = DIFF_INSERT;
12143 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012144 }
12145 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012146 fl = 1;
12147 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000012148#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012149 {
12150 /* If the previous character was the same, thus doubling a
12151 * character, give a bonus to the score. Also for
12152 * soundfold words (illogical but does give a better
12153 * score). */
12154 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000012155 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012156 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012157 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012158 }
12159 break;
12160
12161 case STATE_SWAP:
12162 /*
12163 * Swap two bytes in the bad word: "12" -> "21".
12164 * We change "fword" here, it's changed back afterwards at
12165 * STATE_UNSWAP.
12166 */
12167 p = fword + sp->ts_fidx;
12168 c = *p;
12169 if (c == NUL)
12170 {
12171 /* End of word, can't swap or replace. */
12172 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012173 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012174 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012175
Bram Moolenaar4770d092006-01-12 23:22:24 +000012176 /* Don't swap if the first character is not a word character.
12177 * SWAP3 etc. also don't make sense then. */
12178 if (!soundfold && !spell_iswordp(p, curbuf))
12179 {
12180 sp->ts_state = STATE_REP_INI;
12181 break;
12182 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012183
Bram Moolenaar4770d092006-01-12 23:22:24 +000012184#ifdef FEAT_MBYTE
12185 if (has_mbyte)
12186 {
12187 n = mb_cptr2len(p);
12188 c = mb_ptr2char(p);
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012189 if (p[n] == NUL)
12190 c2 = NUL;
12191 else if (!soundfold && !spell_iswordp(p + n, curbuf))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012192 c2 = c; /* don't swap non-word char */
12193 else
12194 c2 = mb_ptr2char(p + n);
12195 }
12196 else
12197#endif
12198 {
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012199 if (p[1] == NUL)
12200 c2 = NUL;
12201 else if (!soundfold && !spell_iswordp(p + 1, curbuf))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012202 c2 = c; /* don't swap non-word char */
12203 else
12204 c2 = p[1];
12205 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012206
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012207 /* When the second character is NUL we can't swap. */
12208 if (c2 == NUL)
12209 {
12210 sp->ts_state = STATE_REP_INI;
12211 break;
12212 }
12213
Bram Moolenaar4770d092006-01-12 23:22:24 +000012214 /* When characters are identical, swap won't do anything.
12215 * Also get here if the second char is not a word character. */
12216 if (c == c2)
12217 {
12218 sp->ts_state = STATE_SWAP3;
12219 break;
12220 }
12221 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
12222 {
12223 go_deeper(stack, depth, SCORE_SWAP);
12224#ifdef DEBUG_TRIEWALK
12225 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
12226 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12227 c, c2);
12228#endif
12229 sp->ts_state = STATE_UNSWAP;
12230 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012231#ifdef FEAT_MBYTE
12232 if (has_mbyte)
12233 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012234 fl = mb_char2len(c2);
12235 mch_memmove(p, p + n, fl);
12236 mb_char2bytes(c, p + fl);
12237 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012238 }
12239 else
12240#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012241 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012242 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012243 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012244 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012245 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012246 }
12247 else
12248 /* If this swap doesn't work then SWAP3 won't either. */
12249 sp->ts_state = STATE_REP_INI;
12250 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000012251
Bram Moolenaar4770d092006-01-12 23:22:24 +000012252 case STATE_UNSWAP:
12253 /* Undo the STATE_SWAP swap: "21" -> "12". */
12254 p = fword + sp->ts_fidx;
12255#ifdef FEAT_MBYTE
12256 if (has_mbyte)
12257 {
12258 n = MB_BYTE2LEN(*p);
12259 c = mb_ptr2char(p + n);
12260 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
12261 mb_char2bytes(c, p);
12262 }
12263 else
12264#endif
12265 {
12266 c = *p;
12267 *p = p[1];
12268 p[1] = c;
12269 }
12270 /*FALLTHROUGH*/
12271
12272 case STATE_SWAP3:
12273 /* Swap two bytes, skipping one: "123" -> "321". We change
12274 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
12275 p = fword + sp->ts_fidx;
12276#ifdef FEAT_MBYTE
12277 if (has_mbyte)
12278 {
12279 n = mb_cptr2len(p);
12280 c = mb_ptr2char(p);
12281 fl = mb_cptr2len(p + n);
12282 c2 = mb_ptr2char(p + n);
12283 if (!soundfold && !spell_iswordp(p + n + fl, curbuf))
12284 c3 = c; /* don't swap non-word char */
12285 else
12286 c3 = mb_ptr2char(p + n + fl);
12287 }
12288 else
12289#endif
12290 {
12291 c = *p;
12292 c2 = p[1];
12293 if (!soundfold && !spell_iswordp(p + 2, curbuf))
12294 c3 = c; /* don't swap non-word char */
12295 else
12296 c3 = p[2];
12297 }
12298
12299 /* When characters are identical: "121" then SWAP3 result is
12300 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
12301 * same as SWAP on next char: "112". Thus skip all swapping.
12302 * Also skip when c3 is NUL.
12303 * Also get here when the third character is not a word character.
12304 * Second character may any char: "a.b" -> "b.a" */
12305 if (c == c3 || c3 == NUL)
12306 {
12307 sp->ts_state = STATE_REP_INI;
12308 break;
12309 }
12310 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12311 {
12312 go_deeper(stack, depth, SCORE_SWAP3);
12313#ifdef DEBUG_TRIEWALK
12314 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
12315 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12316 c, c3);
12317#endif
12318 sp->ts_state = STATE_UNSWAP3;
12319 ++depth;
12320#ifdef FEAT_MBYTE
12321 if (has_mbyte)
12322 {
12323 tl = mb_char2len(c3);
12324 mch_memmove(p, p + n + fl, tl);
12325 mb_char2bytes(c2, p + tl);
12326 mb_char2bytes(c, p + fl + tl);
12327 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
12328 }
12329 else
12330#endif
12331 {
12332 p[0] = p[2];
12333 p[2] = c;
12334 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12335 }
12336 }
12337 else
12338 sp->ts_state = STATE_REP_INI;
12339 break;
12340
12341 case STATE_UNSWAP3:
12342 /* Undo STATE_SWAP3: "321" -> "123" */
12343 p = fword + sp->ts_fidx;
12344#ifdef FEAT_MBYTE
12345 if (has_mbyte)
12346 {
12347 n = MB_BYTE2LEN(*p);
12348 c2 = mb_ptr2char(p + n);
12349 fl = MB_BYTE2LEN(p[n]);
12350 c = mb_ptr2char(p + n + fl);
12351 tl = MB_BYTE2LEN(p[n + fl]);
12352 mch_memmove(p + fl + tl, p, n);
12353 mb_char2bytes(c, p);
12354 mb_char2bytes(c2, p + tl);
12355 p = p + tl;
12356 }
12357 else
12358#endif
12359 {
12360 c = *p;
12361 *p = p[2];
12362 p[2] = c;
12363 ++p;
12364 }
12365
12366 if (!soundfold && !spell_iswordp(p, curbuf))
12367 {
12368 /* Middle char is not a word char, skip the rotate. First and
12369 * third char were already checked at swap and swap3. */
12370 sp->ts_state = STATE_REP_INI;
12371 break;
12372 }
12373
12374 /* Rotate three characters left: "123" -> "231". We change
12375 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
12376 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12377 {
12378 go_deeper(stack, depth, SCORE_SWAP3);
12379#ifdef DEBUG_TRIEWALK
12380 p = fword + sp->ts_fidx;
12381 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
12382 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12383 p[0], p[1], p[2]);
12384#endif
12385 sp->ts_state = STATE_UNROT3L;
12386 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012387 p = fword + sp->ts_fidx;
12388#ifdef FEAT_MBYTE
12389 if (has_mbyte)
12390 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012391 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012392 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012393 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012394 fl += mb_cptr2len(p + n + fl);
12395 mch_memmove(p, p + n, fl);
12396 mb_char2bytes(c, p + fl);
12397 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012398 }
12399 else
12400#endif
12401 {
12402 c = *p;
12403 *p = p[1];
12404 p[1] = p[2];
12405 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012406 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000012407 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012408 }
12409 else
12410 sp->ts_state = STATE_REP_INI;
12411 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012412
Bram Moolenaar4770d092006-01-12 23:22:24 +000012413 case STATE_UNROT3L:
12414 /* Undo ROT3L: "231" -> "123" */
12415 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000012416#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012417 if (has_mbyte)
12418 {
12419 n = MB_BYTE2LEN(*p);
12420 n += MB_BYTE2LEN(p[n]);
12421 c = mb_ptr2char(p + n);
12422 tl = MB_BYTE2LEN(p[n]);
12423 mch_memmove(p + tl, p, n);
12424 mb_char2bytes(c, p);
12425 }
12426 else
Bram Moolenaarea424162005-06-16 21:51:00 +000012427#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012428 {
12429 c = p[2];
12430 p[2] = p[1];
12431 p[1] = *p;
12432 *p = c;
12433 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012434
Bram Moolenaar4770d092006-01-12 23:22:24 +000012435 /* Rotate three bytes right: "123" -> "312". We change "fword"
12436 * here, it's changed back afterwards at STATE_UNROT3R. */
12437 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12438 {
12439 go_deeper(stack, depth, SCORE_SWAP3);
12440#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012441 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012442 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
12443 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12444 p[0], p[1], p[2]);
12445#endif
12446 sp->ts_state = STATE_UNROT3R;
12447 ++depth;
12448 p = fword + sp->ts_fidx;
12449#ifdef FEAT_MBYTE
12450 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012451 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012452 n = mb_cptr2len(p);
12453 n += mb_cptr2len(p + n);
12454 c = mb_ptr2char(p + n);
12455 tl = mb_cptr2len(p + n);
12456 mch_memmove(p + tl, p, n);
12457 mb_char2bytes(c, p);
12458 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012459 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012460 else
12461#endif
12462 {
12463 c = p[2];
12464 p[2] = p[1];
12465 p[1] = *p;
12466 *p = c;
12467 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12468 }
12469 }
12470 else
12471 sp->ts_state = STATE_REP_INI;
12472 break;
12473
12474 case STATE_UNROT3R:
12475 /* Undo ROT3R: "312" -> "123" */
12476 p = fword + sp->ts_fidx;
12477#ifdef FEAT_MBYTE
12478 if (has_mbyte)
12479 {
12480 c = mb_ptr2char(p);
12481 tl = MB_BYTE2LEN(*p);
12482 n = MB_BYTE2LEN(p[tl]);
12483 n += MB_BYTE2LEN(p[tl + n]);
12484 mch_memmove(p, p + tl, n);
12485 mb_char2bytes(c, p + n);
12486 }
12487 else
12488#endif
12489 {
12490 c = *p;
12491 *p = p[1];
12492 p[1] = p[2];
12493 p[2] = c;
12494 }
12495 /*FALLTHROUGH*/
12496
12497 case STATE_REP_INI:
12498 /* Check if matching with REP items from the .aff file would work.
12499 * Quickly skip if:
12500 * - there are no REP items and we are not in the soundfold trie
12501 * - the score is going to be too high anyway
12502 * - already applied a REP item or swapped here */
12503 if ((lp->lp_replang == NULL && !soundfold)
12504 || sp->ts_score + SCORE_REP >= su->su_maxscore
12505 || sp->ts_fidx < sp->ts_fidxtry)
12506 {
12507 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012508 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012509 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012510
Bram Moolenaar4770d092006-01-12 23:22:24 +000012511 /* Use the first byte to quickly find the first entry that may
12512 * match. If the index is -1 there is none. */
12513 if (soundfold)
12514 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12515 else
12516 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012517
Bram Moolenaar4770d092006-01-12 23:22:24 +000012518 if (sp->ts_curi < 0)
12519 {
12520 sp->ts_state = STATE_FINAL;
12521 break;
12522 }
12523
12524 sp->ts_state = STATE_REP;
12525 /*FALLTHROUGH*/
12526
12527 case STATE_REP:
12528 /* Try matching with REP items from the .aff file. For each match
12529 * replace the characters and check if the resulting word is
12530 * valid. */
12531 p = fword + sp->ts_fidx;
12532
12533 if (soundfold)
12534 gap = &slang->sl_repsal;
12535 else
12536 gap = &lp->lp_replang->sl_rep;
12537 while (sp->ts_curi < gap->ga_len)
12538 {
12539 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12540 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012541 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012542 /* past possible matching entries */
12543 sp->ts_curi = gap->ga_len;
12544 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012545 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012546 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12547 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12548 {
12549 go_deeper(stack, depth, SCORE_REP);
12550#ifdef DEBUG_TRIEWALK
12551 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12552 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12553 ftp->ft_from, ftp->ft_to);
12554#endif
12555 /* Need to undo this afterwards. */
12556 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012557
Bram Moolenaar4770d092006-01-12 23:22:24 +000012558 /* Change the "from" to the "to" string. */
12559 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012560 fl = (int)STRLEN(ftp->ft_from);
12561 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012562 if (fl != tl)
12563 {
12564 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
12565 repextra += tl - fl;
12566 }
12567 mch_memmove(p, ftp->ft_to, tl);
12568 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12569#ifdef FEAT_MBYTE
12570 stack[depth].ts_tcharlen = 0;
12571#endif
12572 break;
12573 }
12574 }
12575
12576 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
12577 /* No (more) matches. */
12578 sp->ts_state = STATE_FINAL;
12579
12580 break;
12581
12582 case STATE_REP_UNDO:
12583 /* Undo a REP replacement and continue with the next one. */
12584 if (soundfold)
12585 gap = &slang->sl_repsal;
12586 else
12587 gap = &lp->lp_replang->sl_rep;
12588 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012589 fl = (int)STRLEN(ftp->ft_from);
12590 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012591 p = fword + sp->ts_fidx;
12592 if (fl != tl)
12593 {
12594 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
12595 repextra -= tl - fl;
12596 }
12597 mch_memmove(p, ftp->ft_from, fl);
12598 sp->ts_state = STATE_REP;
12599 break;
12600
12601 default:
12602 /* Did all possible states at this level, go up one level. */
12603 --depth;
12604
12605 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12606 {
12607 /* Continue in or go back to the prefix tree. */
12608 byts = pbyts;
12609 idxs = pidxs;
12610 }
12611
12612 /* Don't check for CTRL-C too often, it takes time. */
12613 if (--breakcheckcount == 0)
12614 {
12615 ui_breakcheck();
12616 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012617 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012618 }
12619 }
12620}
12621
Bram Moolenaar4770d092006-01-12 23:22:24 +000012622
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012623/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012624 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012625 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012626 static void
12627go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012628 trystate_T *stack;
12629 int depth;
12630 int score_add;
12631{
Bram Moolenaarea424162005-06-16 21:51:00 +000012632 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012633 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012634 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012635 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012636 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012637}
12638
Bram Moolenaar53805d12005-08-01 07:08:33 +000012639#ifdef FEAT_MBYTE
12640/*
12641 * Case-folding may change the number of bytes: Count nr of chars in
12642 * fword[flen] and return the byte length of that many chars in "word".
12643 */
12644 static int
12645nofold_len(fword, flen, word)
12646 char_u *fword;
12647 int flen;
12648 char_u *word;
12649{
12650 char_u *p;
12651 int i = 0;
12652
12653 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12654 ++i;
12655 for (p = word; i > 0; mb_ptr_adv(p))
12656 --i;
12657 return (int)(p - word);
12658}
12659#endif
12660
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012661/*
12662 * "fword" is a good word with case folded. Find the matching keep-case
12663 * words and put it in "kword".
12664 * Theoretically there could be several keep-case words that result in the
12665 * same case-folded word, but we only find one...
12666 */
12667 static void
12668find_keepcap_word(slang, fword, kword)
12669 slang_T *slang;
12670 char_u *fword;
12671 char_u *kword;
12672{
12673 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12674 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012675 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012676
12677 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012678 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012679 int round[MAXWLEN];
12680 int fwordidx[MAXWLEN];
12681 int uwordidx[MAXWLEN];
12682 int kwordlen[MAXWLEN];
12683
12684 int flen, ulen;
12685 int l;
12686 int len;
12687 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012688 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012689 char_u *p;
12690 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012691 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012692
12693 if (byts == NULL)
12694 {
12695 /* array is empty: "cannot happen" */
12696 *kword = NUL;
12697 return;
12698 }
12699
12700 /* Make an all-cap version of "fword". */
12701 allcap_copy(fword, uword);
12702
12703 /*
12704 * Each character needs to be tried both case-folded and upper-case.
12705 * All this gets very complicated if we keep in mind that changing case
12706 * may change the byte length of a multi-byte character...
12707 */
12708 depth = 0;
12709 arridx[0] = 0;
12710 round[0] = 0;
12711 fwordidx[0] = 0;
12712 uwordidx[0] = 0;
12713 kwordlen[0] = 0;
12714 while (depth >= 0)
12715 {
12716 if (fword[fwordidx[depth]] == NUL)
12717 {
12718 /* We are at the end of "fword". If the tree allows a word to end
12719 * here we have found a match. */
12720 if (byts[arridx[depth] + 1] == 0)
12721 {
12722 kword[kwordlen[depth]] = NUL;
12723 return;
12724 }
12725
12726 /* kword is getting too long, continue one level up */
12727 --depth;
12728 }
12729 else if (++round[depth] > 2)
12730 {
12731 /* tried both fold-case and upper-case character, continue one
12732 * level up */
12733 --depth;
12734 }
12735 else
12736 {
12737 /*
12738 * round[depth] == 1: Try using the folded-case character.
12739 * round[depth] == 2: Try using the upper-case character.
12740 */
12741#ifdef FEAT_MBYTE
12742 if (has_mbyte)
12743 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012744 flen = mb_cptr2len(fword + fwordidx[depth]);
12745 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012746 }
12747 else
12748#endif
12749 ulen = flen = 1;
12750 if (round[depth] == 1)
12751 {
12752 p = fword + fwordidx[depth];
12753 l = flen;
12754 }
12755 else
12756 {
12757 p = uword + uwordidx[depth];
12758 l = ulen;
12759 }
12760
12761 for (tryidx = arridx[depth]; l > 0; --l)
12762 {
12763 /* Perform a binary search in the list of accepted bytes. */
12764 len = byts[tryidx++];
12765 c = *p++;
12766 lo = tryidx;
12767 hi = tryidx + len - 1;
12768 while (lo < hi)
12769 {
12770 m = (lo + hi) / 2;
12771 if (byts[m] > c)
12772 hi = m - 1;
12773 else if (byts[m] < c)
12774 lo = m + 1;
12775 else
12776 {
12777 lo = hi = m;
12778 break;
12779 }
12780 }
12781
12782 /* Stop if there is no matching byte. */
12783 if (hi < lo || byts[lo] != c)
12784 break;
12785
12786 /* Continue at the child (if there is one). */
12787 tryidx = idxs[lo];
12788 }
12789
12790 if (l == 0)
12791 {
12792 /*
12793 * Found the matching char. Copy it to "kword" and go a
12794 * level deeper.
12795 */
12796 if (round[depth] == 1)
12797 {
12798 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12799 flen);
12800 kwordlen[depth + 1] = kwordlen[depth] + flen;
12801 }
12802 else
12803 {
12804 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12805 ulen);
12806 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12807 }
12808 fwordidx[depth + 1] = fwordidx[depth] + flen;
12809 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12810
12811 ++depth;
12812 arridx[depth] = tryidx;
12813 round[depth] = 0;
12814 }
12815 }
12816 }
12817
12818 /* Didn't find it: "cannot happen". */
12819 *kword = NUL;
12820}
12821
12822/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012823 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12824 * su->su_sga.
12825 */
12826 static void
12827score_comp_sal(su)
12828 suginfo_T *su;
12829{
12830 langp_T *lp;
12831 char_u badsound[MAXWLEN];
12832 int i;
12833 suggest_T *stp;
12834 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012835 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012836 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012837
12838 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
12839 return;
12840
12841 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012842 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012843 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012844 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012845 if (lp->lp_slang->sl_sal.ga_len > 0)
12846 {
12847 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012848 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012849
12850 for (i = 0; i < su->su_ga.ga_len; ++i)
12851 {
12852 stp = &SUG(su->su_ga, i);
12853
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012854 /* Case-fold the suggested word, sound-fold it and compute the
12855 * sound-a-like score. */
12856 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012857 if (score < SCORE_MAXMAX)
12858 {
12859 /* Add the suggestion. */
12860 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
12861 sstp->st_word = vim_strsave(stp->st_word);
12862 if (sstp->st_word != NULL)
12863 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012864 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012865 sstp->st_score = score;
12866 sstp->st_altscore = 0;
12867 sstp->st_orglen = stp->st_orglen;
12868 ++su->su_sga.ga_len;
12869 }
12870 }
12871 }
12872 break;
12873 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012874 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012875}
12876
12877/*
12878 * Combine the list of suggestions in su->su_ga and su->su_sga.
12879 * They are intwined.
12880 */
12881 static void
12882score_combine(su)
12883 suginfo_T *su;
12884{
12885 int i;
12886 int j;
12887 garray_T ga;
12888 garray_T *gap;
12889 langp_T *lp;
12890 suggest_T *stp;
12891 char_u *p;
12892 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012893 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012894 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012895 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012896
12897 /* Add the alternate score to su_ga. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012898 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012899 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012900 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012901 if (lp->lp_slang->sl_sal.ga_len > 0)
12902 {
12903 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012904 slang = lp->lp_slang;
12905 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012906
12907 for (i = 0; i < su->su_ga.ga_len; ++i)
12908 {
12909 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012910 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012911 if (stp->st_altscore == SCORE_MAXMAX)
12912 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
12913 else
12914 stp->st_score = (stp->st_score * 3
12915 + stp->st_altscore) / 4;
12916 stp->st_salscore = FALSE;
12917 }
12918 break;
12919 }
12920 }
12921
Bram Moolenaarf193fff2006-04-27 00:02:13 +000012922 if (slang == NULL) /* Using "double" without sound folding. */
12923 {
12924 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
12925 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012926 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000012927 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012928
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012929 /* Add the alternate score to su_sga. */
12930 for (i = 0; i < su->su_sga.ga_len; ++i)
12931 {
12932 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012933 stp->st_altscore = spell_edit_score(slang,
12934 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012935 if (stp->st_score == SCORE_MAXMAX)
12936 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
12937 else
12938 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
12939 stp->st_salscore = TRUE;
12940 }
12941
Bram Moolenaar4770d092006-01-12 23:22:24 +000012942 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
12943 * for both lists. */
12944 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012945 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012946 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012947 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
12948
12949 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
12950 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
12951 return;
12952
12953 stp = &SUG(ga, 0);
12954 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
12955 {
12956 /* round 1: get a suggestion from su_ga
12957 * round 2: get a suggestion from su_sga */
12958 for (round = 1; round <= 2; ++round)
12959 {
12960 gap = round == 1 ? &su->su_ga : &su->su_sga;
12961 if (i < gap->ga_len)
12962 {
12963 /* Don't add a word if it's already there. */
12964 p = SUG(*gap, i).st_word;
12965 for (j = 0; j < ga.ga_len; ++j)
12966 if (STRCMP(stp[j].st_word, p) == 0)
12967 break;
12968 if (j == ga.ga_len)
12969 stp[ga.ga_len++] = SUG(*gap, i);
12970 else
12971 vim_free(p);
12972 }
12973 }
12974 }
12975
12976 ga_clear(&su->su_ga);
12977 ga_clear(&su->su_sga);
12978
12979 /* Truncate the list to the number of suggestions that will be displayed. */
12980 if (ga.ga_len > su->su_maxcount)
12981 {
12982 for (i = su->su_maxcount; i < ga.ga_len; ++i)
12983 vim_free(stp[i].st_word);
12984 ga.ga_len = su->su_maxcount;
12985 }
12986
12987 su->su_ga = ga;
12988}
12989
12990/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012991 * For the goodword in "stp" compute the soundalike score compared to the
12992 * badword.
12993 */
12994 static int
12995stp_sal_score(stp, su, slang, badsound)
12996 suggest_T *stp;
12997 suginfo_T *su;
12998 slang_T *slang;
12999 char_u *badsound; /* sound-folded badword */
13000{
13001 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013002 char_u *pbad;
13003 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013004 char_u badsound2[MAXWLEN];
13005 char_u fword[MAXWLEN];
13006 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013007 char_u goodword[MAXWLEN];
13008 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013009
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013010 lendiff = (int)(su->su_badlen - stp->st_orglen);
13011 if (lendiff >= 0)
13012 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013013 else
13014 {
13015 /* soundfold the bad word with more characters following */
13016 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
13017
13018 /* When joining two words the sound often changes a lot. E.g., "t he"
13019 * sounds like "t h" while "the" sounds like "@". Avoid that by
13020 * removing the space. Don't do it when the good word also contains a
13021 * space. */
13022 if (vim_iswhite(su->su_badptr[su->su_badlen])
13023 && *skiptowhite(stp->st_word) == NUL)
13024 for (p = fword; *(p = skiptowhite(p)) != NUL; )
13025 mch_memmove(p, p + 1, STRLEN(p));
13026
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013027 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013028 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013029 }
13030
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013031 if (lendiff > 0)
13032 {
13033 /* Add part of the bad word to the good word, so that we soundfold
13034 * what replaces the bad word. */
13035 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013036 vim_strncpy(goodword + stp->st_wordlen,
13037 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013038 pgood = goodword;
13039 }
13040 else
13041 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013042
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013043 /* Sound-fold the word and compute the score for the difference. */
13044 spell_soundfold(slang, pgood, FALSE, goodsound);
13045
13046 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013047}
13048
Bram Moolenaar4770d092006-01-12 23:22:24 +000013049/* structure used to store soundfolded words that add_sound_suggest() has
13050 * handled already. */
13051typedef struct
13052{
13053 short sft_score; /* lowest score used */
13054 char_u sft_word[1]; /* soundfolded word, actually longer */
13055} sftword_T;
13056
13057static sftword_T dumsft;
13058#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
13059#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
13060
13061/*
13062 * Prepare for calling suggest_try_soundalike().
13063 */
13064 static void
13065suggest_try_soundalike_prep()
13066{
13067 langp_T *lp;
13068 int lpi;
13069 slang_T *slang;
13070
13071 /* Do this for all languages that support sound folding and for which a
13072 * .sug file has been loaded. */
13073 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
13074 {
13075 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13076 slang = lp->lp_slang;
13077 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13078 /* prepare the hashtable used by add_sound_suggest() */
13079 hash_init(&slang->sl_sounddone);
13080 }
13081}
13082
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013083/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013084 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013085 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013086 */
13087 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000013088suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013089 suginfo_T *su;
13090{
13091 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013092 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013093 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013094 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013095
Bram Moolenaar4770d092006-01-12 23:22:24 +000013096 /* Do this for all languages that support sound folding and for which a
13097 * .sug file has been loaded. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013098 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013099 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013100 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13101 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013102 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013103 {
13104 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013105 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013106
Bram Moolenaar4770d092006-01-12 23:22:24 +000013107 /* try all kinds of inserts/deletes/swaps/etc. */
13108 /* TODO: also soundfold the next words, so that we can try joining
13109 * and splitting */
13110 suggest_trie_walk(su, lp, salword, TRUE);
13111 }
13112 }
13113}
13114
13115/*
13116 * Finish up after calling suggest_try_soundalike().
13117 */
13118 static void
13119suggest_try_soundalike_finish()
13120{
13121 langp_T *lp;
13122 int lpi;
13123 slang_T *slang;
13124 int todo;
13125 hashitem_T *hi;
13126
13127 /* Do this for all languages that support sound folding and for which a
13128 * .sug file has been loaded. */
13129 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
13130 {
13131 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13132 slang = lp->lp_slang;
13133 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13134 {
13135 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013136 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013137 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
13138 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013139 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013140 vim_free(HI2SFT(hi));
13141 --todo;
13142 }
Bram Moolenaar6417da62007-03-08 13:49:53 +000013143
13144 /* Clear the hashtable, it may also be used by another region. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013145 hash_clear(&slang->sl_sounddone);
Bram Moolenaar6417da62007-03-08 13:49:53 +000013146 hash_init(&slang->sl_sounddone);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013147 }
13148 }
13149}
13150
13151/*
13152 * A match with a soundfolded word is found. Add the good word(s) that
13153 * produce this soundfolded word.
13154 */
13155 static void
13156add_sound_suggest(su, goodword, score, lp)
13157 suginfo_T *su;
13158 char_u *goodword;
13159 int score; /* soundfold score */
13160 langp_T *lp;
13161{
13162 slang_T *slang = lp->lp_slang; /* language for sound folding */
13163 int sfwordnr;
13164 char_u *nrline;
13165 int orgnr;
13166 char_u theword[MAXWLEN];
13167 int i;
13168 int wlen;
13169 char_u *byts;
13170 idx_T *idxs;
13171 int n;
13172 int wordcount;
13173 int wc;
13174 int goodscore;
13175 hash_T hash;
13176 hashitem_T *hi;
13177 sftword_T *sft;
13178 int bc, gc;
13179 int limit;
13180
13181 /*
13182 * It's very well possible that the same soundfold word is found several
13183 * times with different scores. Since the following is quite slow only do
13184 * the words that have a better score than before. Use a hashtable to
13185 * remember the words that have been done.
13186 */
13187 hash = hash_hash(goodword);
13188 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
13189 if (HASHITEM_EMPTY(hi))
13190 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013191 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
13192 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000013193 if (sft != NULL)
13194 {
13195 sft->sft_score = score;
13196 STRCPY(sft->sft_word, goodword);
13197 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
13198 }
13199 }
13200 else
13201 {
13202 sft = HI2SFT(hi);
13203 if (score >= sft->sft_score)
13204 return;
13205 sft->sft_score = score;
13206 }
13207
13208 /*
13209 * Find the word nr in the soundfold tree.
13210 */
13211 sfwordnr = soundfold_find(slang, goodword);
13212 if (sfwordnr < 0)
13213 {
13214 EMSG2(_(e_intern2), "add_sound_suggest()");
13215 return;
13216 }
13217
13218 /*
13219 * go over the list of good words that produce this soundfold word
13220 */
13221 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
13222 orgnr = 0;
13223 while (*nrline != NUL)
13224 {
13225 /* The wordnr was stored in a minimal nr of bytes as an offset to the
13226 * previous wordnr. */
13227 orgnr += bytes2offset(&nrline);
13228
13229 byts = slang->sl_fbyts;
13230 idxs = slang->sl_fidxs;
13231
13232 /* Lookup the word "orgnr" one of the two tries. */
13233 n = 0;
13234 wlen = 0;
13235 wordcount = 0;
13236 for (;;)
13237 {
13238 i = 1;
13239 if (wordcount == orgnr && byts[n + 1] == NUL)
13240 break; /* found end of word */
13241
13242 if (byts[n + 1] == NUL)
13243 ++wordcount;
13244
13245 /* skip over the NUL bytes */
13246 for ( ; byts[n + i] == NUL; ++i)
13247 if (i > byts[n]) /* safety check */
13248 {
13249 STRCPY(theword + wlen, "BAD");
13250 goto badword;
13251 }
13252
13253 /* One of the siblings must have the word. */
13254 for ( ; i < byts[n]; ++i)
13255 {
13256 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
13257 if (wordcount + wc > orgnr)
13258 break;
13259 wordcount += wc;
13260 }
13261
13262 theword[wlen++] = byts[n + i];
13263 n = idxs[n + i];
13264 }
13265badword:
13266 theword[wlen] = NUL;
13267
13268 /* Go over the possible flags and regions. */
13269 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
13270 {
13271 char_u cword[MAXWLEN];
13272 char_u *p;
13273 int flags = (int)idxs[n + i];
13274
Bram Moolenaare1438bb2006-03-01 22:01:55 +000013275 /* Skip words with the NOSUGGEST flag */
13276 if (flags & WF_NOSUGGEST)
13277 continue;
13278
Bram Moolenaar4770d092006-01-12 23:22:24 +000013279 if (flags & WF_KEEPCAP)
13280 {
13281 /* Must find the word in the keep-case tree. */
13282 find_keepcap_word(slang, theword, cword);
13283 p = cword;
13284 }
13285 else
13286 {
13287 flags |= su->su_badflags;
13288 if ((flags & WF_CAPMASK) != 0)
13289 {
13290 /* Need to fix case according to "flags". */
13291 make_case_word(theword, cword, flags);
13292 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013293 }
13294 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013295 p = theword;
13296 }
13297
13298 /* Add the suggestion. */
13299 if (sps_flags & SPS_DOUBLE)
13300 {
13301 /* Add the suggestion if the score isn't too bad. */
13302 if (score <= su->su_maxscore)
13303 add_suggestion(su, &su->su_sga, p, su->su_badlen,
13304 score, 0, FALSE, slang, FALSE);
13305 }
13306 else
13307 {
13308 /* Add a penalty for words in another region. */
13309 if ((flags & WF_REGION)
13310 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
13311 goodscore = SCORE_REGION;
13312 else
13313 goodscore = 0;
13314
13315 /* Add a small penalty for changing the first letter from
13316 * lower to upper case. Helps for "tath" -> "Kath", which is
13317 * less common thatn "tath" -> "path". Don't do it when the
13318 * letter is the same, that has already been counted. */
13319 gc = PTR2CHAR(p);
13320 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013321 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013322 bc = PTR2CHAR(su->su_badword);
13323 if (!SPELL_ISUPPER(bc)
13324 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
13325 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013326 }
13327
Bram Moolenaar4770d092006-01-12 23:22:24 +000013328 /* Compute the score for the good word. This only does letter
13329 * insert/delete/swap/replace. REP items are not considered,
13330 * which may make the score a bit higher.
13331 * Use a limit for the score to make it work faster. Use
13332 * MAXSCORE(), because RESCORE() will change the score.
13333 * If the limit is very high then the iterative method is
13334 * inefficient, using an array is quicker. */
13335 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
13336 if (limit > SCORE_LIMITMAX)
13337 goodscore += spell_edit_score(slang, su->su_badword, p);
13338 else
13339 goodscore += spell_edit_score_limit(slang, su->su_badword,
13340 p, limit);
13341
13342 /* When going over the limit don't bother to do the rest. */
13343 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013344 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013345 /* Give a bonus to words seen before. */
13346 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013347
Bram Moolenaar4770d092006-01-12 23:22:24 +000013348 /* Add the suggestion if the score isn't too bad. */
13349 goodscore = RESCORE(goodscore, score);
13350 if (goodscore <= su->su_sfmaxscore)
13351 add_suggestion(su, &su->su_ga, p, su->su_badlen,
13352 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013353 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013354 }
13355 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013356 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013357 }
13358}
13359
13360/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013361 * Find word "word" in fold-case tree for "slang" and return the word number.
13362 */
13363 static int
13364soundfold_find(slang, word)
13365 slang_T *slang;
13366 char_u *word;
13367{
13368 idx_T arridx = 0;
13369 int len;
13370 int wlen = 0;
13371 int c;
13372 char_u *ptr = word;
13373 char_u *byts;
13374 idx_T *idxs;
13375 int wordnr = 0;
13376
13377 byts = slang->sl_sbyts;
13378 idxs = slang->sl_sidxs;
13379
13380 for (;;)
13381 {
13382 /* First byte is the number of possible bytes. */
13383 len = byts[arridx++];
13384
13385 /* If the first possible byte is a zero the word could end here.
13386 * If the word ends we found the word. If not skip the NUL bytes. */
13387 c = ptr[wlen];
13388 if (byts[arridx] == NUL)
13389 {
13390 if (c == NUL)
13391 break;
13392
13393 /* Skip over the zeros, there can be several. */
13394 while (len > 0 && byts[arridx] == NUL)
13395 {
13396 ++arridx;
13397 --len;
13398 }
13399 if (len == 0)
13400 return -1; /* no children, word should have ended here */
13401 ++wordnr;
13402 }
13403
13404 /* If the word ends we didn't find it. */
13405 if (c == NUL)
13406 return -1;
13407
13408 /* Perform a binary search in the list of accepted bytes. */
13409 if (c == TAB) /* <Tab> is handled like <Space> */
13410 c = ' ';
13411 while (byts[arridx] < c)
13412 {
13413 /* The word count is in the first idxs[] entry of the child. */
13414 wordnr += idxs[idxs[arridx]];
13415 ++arridx;
13416 if (--len == 0) /* end of the bytes, didn't find it */
13417 return -1;
13418 }
13419 if (byts[arridx] != c) /* didn't find the byte */
13420 return -1;
13421
13422 /* Continue at the child (if there is one). */
13423 arridx = idxs[arridx];
13424 ++wlen;
13425
13426 /* One space in the good word may stand for several spaces in the
13427 * checked word. */
13428 if (c == ' ')
13429 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
13430 ++wlen;
13431 }
13432
13433 return wordnr;
13434}
13435
13436/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013437 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013438 */
13439 static void
13440make_case_word(fword, cword, flags)
13441 char_u *fword;
13442 char_u *cword;
13443 int flags;
13444{
13445 if (flags & WF_ALLCAP)
13446 /* Make it all upper-case */
13447 allcap_copy(fword, cword);
13448 else if (flags & WF_ONECAP)
13449 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013450 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013451 else
13452 /* Use goodword as-is. */
13453 STRCPY(cword, fword);
13454}
13455
Bram Moolenaarea424162005-06-16 21:51:00 +000013456/*
13457 * Use map string "map" for languages "lp".
13458 */
13459 static void
13460set_map_str(lp, map)
13461 slang_T *lp;
13462 char_u *map;
13463{
13464 char_u *p;
13465 int headc = 0;
13466 int c;
13467 int i;
13468
13469 if (*map == NUL)
13470 {
13471 lp->sl_has_map = FALSE;
13472 return;
13473 }
13474 lp->sl_has_map = TRUE;
13475
Bram Moolenaar4770d092006-01-12 23:22:24 +000013476 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000013477 for (i = 0; i < 256; ++i)
13478 lp->sl_map_array[i] = 0;
13479#ifdef FEAT_MBYTE
13480 hash_init(&lp->sl_map_hash);
13481#endif
13482
13483 /*
13484 * The similar characters are stored separated with slashes:
13485 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13486 * before the same slash. For characters above 255 sl_map_hash is used.
13487 */
13488 for (p = map; *p != NUL; )
13489 {
13490#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013491 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000013492#else
13493 c = *p++;
13494#endif
13495 if (c == '/')
13496 headc = 0;
13497 else
13498 {
13499 if (headc == 0)
13500 headc = c;
13501
13502#ifdef FEAT_MBYTE
13503 /* Characters above 255 don't fit in sl_map_array[], put them in
13504 * the hash table. Each entry is the char, a NUL the headchar and
13505 * a NUL. */
13506 if (c >= 256)
13507 {
13508 int cl = mb_char2len(c);
13509 int headcl = mb_char2len(headc);
13510 char_u *b;
13511 hash_T hash;
13512 hashitem_T *hi;
13513
13514 b = alloc((unsigned)(cl + headcl + 2));
13515 if (b == NULL)
13516 return;
13517 mb_char2bytes(c, b);
13518 b[cl] = NUL;
13519 mb_char2bytes(headc, b + cl + 1);
13520 b[cl + 1 + headcl] = NUL;
13521 hash = hash_hash(b);
13522 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13523 if (HASHITEM_EMPTY(hi))
13524 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13525 else
13526 {
13527 /* This should have been checked when generating the .spl
13528 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013529 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000013530 vim_free(b);
13531 }
13532 }
13533 else
13534#endif
13535 lp->sl_map_array[c] = headc;
13536 }
13537 }
13538}
13539
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013540/*
13541 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13542 * lines in the .aff file.
13543 */
13544 static int
13545similar_chars(slang, c1, c2)
13546 slang_T *slang;
13547 int c1;
13548 int c2;
13549{
Bram Moolenaarea424162005-06-16 21:51:00 +000013550 int m1, m2;
13551#ifdef FEAT_MBYTE
13552 char_u buf[MB_MAXBYTES];
13553 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013554
Bram Moolenaarea424162005-06-16 21:51:00 +000013555 if (c1 >= 256)
13556 {
13557 buf[mb_char2bytes(c1, buf)] = 0;
13558 hi = hash_find(&slang->sl_map_hash, buf);
13559 if (HASHITEM_EMPTY(hi))
13560 m1 = 0;
13561 else
13562 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13563 }
13564 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013565#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013566 m1 = slang->sl_map_array[c1];
13567 if (m1 == 0)
13568 return FALSE;
13569
13570
13571#ifdef FEAT_MBYTE
13572 if (c2 >= 256)
13573 {
13574 buf[mb_char2bytes(c2, buf)] = 0;
13575 hi = hash_find(&slang->sl_map_hash, buf);
13576 if (HASHITEM_EMPTY(hi))
13577 m2 = 0;
13578 else
13579 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13580 }
13581 else
13582#endif
13583 m2 = slang->sl_map_array[c2];
13584
13585 return m1 == m2;
13586}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013587
13588/*
13589 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013590 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013591 */
13592 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000013593add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
13594 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013595 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013596 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013597 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013598 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013599 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013600 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013601 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013602 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013603 int maxsf; /* su_maxscore applies to soundfold score,
13604 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013605{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013606 int goodlen; /* len of goodword changed */
13607 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013608 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013609 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013610 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013611 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013612
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013613 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13614 * "thee the" is added next to changing the first "the" the "thee". */
13615 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013616 pbad = su->su_badptr + badlenarg;
13617 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013618 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013619 goodlen = (int)(pgood - goodword);
13620 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013621 if (goodlen <= 0 || badlen <= 0)
13622 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013623 mb_ptr_back(goodword, pgood);
13624 mb_ptr_back(su->su_badptr, pbad);
13625#ifdef FEAT_MBYTE
13626 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013627 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013628 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13629 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013630 }
13631 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013632#endif
13633 if (*pgood != *pbad)
13634 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013635 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013636
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013637 if (badlen == 0 && goodlen == 0)
13638 /* goodword doesn't change anything; may happen for "the the" changing
13639 * the first "the" to itself. */
13640 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013641
Bram Moolenaar89d40322006-08-29 15:30:07 +000013642 if (gap->ga_len == 0)
13643 i = -1;
13644 else
13645 {
13646 /* Check if the word is already there. Also check the length that is
13647 * being replaced "thes," -> "these" is a different suggestion from
13648 * "thes" -> "these". */
13649 stp = &SUG(*gap, 0);
13650 for (i = gap->ga_len; --i >= 0; ++stp)
13651 if (stp->st_wordlen == goodlen
13652 && stp->st_orglen == badlen
13653 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013654 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013655 /*
13656 * Found it. Remember the word with the lowest score.
13657 */
13658 if (stp->st_slang == NULL)
13659 stp->st_slang = slang;
13660
13661 new_sug.st_score = score;
13662 new_sug.st_altscore = altscore;
13663 new_sug.st_had_bonus = had_bonus;
13664
13665 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013666 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013667 /* Only one of the two had the soundalike score computed.
13668 * Need to do that for the other one now, otherwise the
13669 * scores can't be compared. This happens because
13670 * suggest_try_change() doesn't compute the soundalike
13671 * word to keep it fast, while some special methods set
13672 * the soundalike score to zero. */
13673 if (had_bonus)
13674 rescore_one(su, stp);
13675 else
13676 {
13677 new_sug.st_word = stp->st_word;
13678 new_sug.st_wordlen = stp->st_wordlen;
13679 new_sug.st_slang = stp->st_slang;
13680 new_sug.st_orglen = badlen;
13681 rescore_one(su, &new_sug);
13682 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013683 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013684
Bram Moolenaar89d40322006-08-29 15:30:07 +000013685 if (stp->st_score > new_sug.st_score)
13686 {
13687 stp->st_score = new_sug.st_score;
13688 stp->st_altscore = new_sug.st_altscore;
13689 stp->st_had_bonus = new_sug.st_had_bonus;
13690 }
13691 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013692 }
Bram Moolenaar89d40322006-08-29 15:30:07 +000013693 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013694
Bram Moolenaar4770d092006-01-12 23:22:24 +000013695 if (i < 0 && ga_grow(gap, 1) == OK)
13696 {
13697 /* Add a suggestion. */
13698 stp = &SUG(*gap, gap->ga_len);
13699 stp->st_word = vim_strnsave(goodword, goodlen);
13700 if (stp->st_word != NULL)
13701 {
13702 stp->st_wordlen = goodlen;
13703 stp->st_score = score;
13704 stp->st_altscore = altscore;
13705 stp->st_had_bonus = had_bonus;
13706 stp->st_orglen = badlen;
13707 stp->st_slang = slang;
13708 ++gap->ga_len;
13709
13710 /* If we have too many suggestions now, sort the list and keep
13711 * the best suggestions. */
13712 if (gap->ga_len > SUG_MAX_COUNT(su))
13713 {
13714 if (maxsf)
13715 su->su_sfmaxscore = cleanup_suggestions(gap,
13716 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13717 else
13718 {
13719 i = su->su_maxscore;
13720 su->su_maxscore = cleanup_suggestions(gap,
13721 su->su_maxscore, SUG_CLEAN_COUNT(su));
13722 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013723 }
13724 }
13725 }
13726}
13727
13728/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013729 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13730 * for split words, such as "the the". Remove these from the list here.
13731 */
13732 static void
13733check_suggestions(su, gap)
13734 suginfo_T *su;
13735 garray_T *gap; /* either su_ga or su_sga */
13736{
13737 suggest_T *stp;
13738 int i;
13739 char_u longword[MAXWLEN + 1];
13740 int len;
13741 hlf_T attr;
13742
13743 stp = &SUG(*gap, 0);
13744 for (i = gap->ga_len - 1; i >= 0; --i)
13745 {
13746 /* Need to append what follows to check for "the the". */
13747 STRCPY(longword, stp[i].st_word);
13748 len = stp[i].st_wordlen;
13749 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13750 MAXWLEN - len);
13751 attr = HLF_COUNT;
13752 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13753 if (attr != HLF_COUNT)
13754 {
13755 /* Remove this entry. */
13756 vim_free(stp[i].st_word);
13757 --gap->ga_len;
13758 if (i < gap->ga_len)
13759 mch_memmove(stp + i, stp + i + 1,
13760 sizeof(suggest_T) * (gap->ga_len - i));
13761 }
13762 }
13763}
13764
13765
13766/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013767 * Add a word to be banned.
13768 */
13769 static void
13770add_banned(su, word)
13771 suginfo_T *su;
13772 char_u *word;
13773{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013774 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013775 hash_T hash;
13776 hashitem_T *hi;
13777
Bram Moolenaar4770d092006-01-12 23:22:24 +000013778 hash = hash_hash(word);
13779 hi = hash_lookup(&su->su_banned, word, hash);
13780 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013781 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013782 s = vim_strsave(word);
13783 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013784 hash_add_item(&su->su_banned, hi, s, hash);
13785 }
13786}
13787
13788/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013789 * Recompute the score for all suggestions if sound-folding is possible. This
13790 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013791 */
13792 static void
13793rescore_suggestions(su)
13794 suginfo_T *su;
13795{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013796 int i;
13797
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013798 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013799 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013800 rescore_one(su, &SUG(su->su_ga, i));
13801}
13802
13803/*
13804 * Recompute the score for one suggestion if sound-folding is possible.
13805 */
13806 static void
13807rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013808 suginfo_T *su;
13809 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013810{
13811 slang_T *slang = stp->st_slang;
13812 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013813 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013814
13815 /* Only rescore suggestions that have no sal score yet and do have a
13816 * language. */
13817 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13818 {
13819 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013820 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013821 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013822 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013823 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013824 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013825 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013826
13827 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013828 if (stp->st_altscore == SCORE_MAXMAX)
13829 stp->st_altscore = SCORE_BIG;
13830 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13831 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013832 }
13833}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013834
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013835static int
13836#ifdef __BORLANDC__
13837_RTLENTRYF
13838#endif
13839sug_compare __ARGS((const void *s1, const void *s2));
13840
13841/*
13842 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013843 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013844 */
13845 static int
13846#ifdef __BORLANDC__
13847_RTLENTRYF
13848#endif
13849sug_compare(s1, s2)
13850 const void *s1;
13851 const void *s2;
13852{
13853 suggest_T *p1 = (suggest_T *)s1;
13854 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013855 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013856
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013857 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013858 {
13859 n = p1->st_altscore - p2->st_altscore;
13860 if (n == 0)
13861 n = STRICMP(p1->st_word, p2->st_word);
13862 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013863 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013864}
13865
13866/*
13867 * Cleanup the suggestions:
13868 * - Sort on score.
13869 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013870 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013871 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013872 static int
13873cleanup_suggestions(gap, maxscore, keep)
13874 garray_T *gap;
13875 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013876 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013877{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013878 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013879 int i;
13880
13881 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013882 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013883
13884 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013885 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013886 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013887 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013888 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013889 gap->ga_len = keep;
13890 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013891 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013892 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013893}
13894
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013895#if defined(FEAT_EVAL) || defined(PROTO)
13896/*
13897 * Soundfold a string, for soundfold().
13898 * Result is in allocated memory, NULL for an error.
13899 */
13900 char_u *
13901eval_soundfold(word)
13902 char_u *word;
13903{
13904 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013905 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013906 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013907
13908 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13909 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013910 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013911 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013912 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013913 if (lp->lp_slang->sl_sal.ga_len > 0)
13914 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013915 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013916 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013917 return vim_strsave(sound);
13918 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013919 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013920
13921 /* No language with sound folding, return word as-is. */
13922 return vim_strsave(word);
13923}
13924#endif
13925
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013926/*
13927 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000013928 *
13929 * There are many ways to turn a word into a sound-a-like representation. The
13930 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
13931 * swedish name matching - survey and test of different algorithms" by Klas
13932 * Erikson.
13933 *
13934 * We support two methods:
13935 * 1. SOFOFROM/SOFOTO do a simple character mapping.
13936 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013937 */
13938 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013939spell_soundfold(slang, inword, folded, res)
13940 slang_T *slang;
13941 char_u *inword;
13942 int folded; /* "inword" is already case-folded */
13943 char_u *res;
13944{
13945 char_u fword[MAXWLEN];
13946 char_u *word;
13947
13948 if (slang->sl_sofo)
13949 /* SOFOFROM and SOFOTO used */
13950 spell_soundfold_sofo(slang, inword, res);
13951 else
13952 {
13953 /* SAL items used. Requires the word to be case-folded. */
13954 if (folded)
13955 word = inword;
13956 else
13957 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013958 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013959 word = fword;
13960 }
13961
13962#ifdef FEAT_MBYTE
13963 if (has_mbyte)
13964 spell_soundfold_wsal(slang, word, res);
13965 else
13966#endif
13967 spell_soundfold_sal(slang, word, res);
13968 }
13969}
13970
13971/*
13972 * Perform sound folding of "inword" into "res" according to SOFOFROM and
13973 * SOFOTO lines.
13974 */
13975 static void
13976spell_soundfold_sofo(slang, inword, res)
13977 slang_T *slang;
13978 char_u *inword;
13979 char_u *res;
13980{
13981 char_u *s;
13982 int ri = 0;
13983 int c;
13984
13985#ifdef FEAT_MBYTE
13986 if (has_mbyte)
13987 {
13988 int prevc = 0;
13989 int *ip;
13990
13991 /* The sl_sal_first[] table contains the translation for chars up to
13992 * 255, sl_sal the rest. */
13993 for (s = inword; *s != NUL; )
13994 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013995 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013996 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
13997 c = ' ';
13998 else if (c < 256)
13999 c = slang->sl_sal_first[c];
14000 else
14001 {
14002 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
14003 if (ip == NULL) /* empty list, can't match */
14004 c = NUL;
14005 else
14006 for (;;) /* find "c" in the list */
14007 {
14008 if (*ip == 0) /* not found */
14009 {
14010 c = NUL;
14011 break;
14012 }
14013 if (*ip == c) /* match! */
14014 {
14015 c = ip[1];
14016 break;
14017 }
14018 ip += 2;
14019 }
14020 }
14021
14022 if (c != NUL && c != prevc)
14023 {
14024 ri += mb_char2bytes(c, res + ri);
14025 if (ri + MB_MAXBYTES > MAXWLEN)
14026 break;
14027 prevc = c;
14028 }
14029 }
14030 }
14031 else
14032#endif
14033 {
14034 /* The sl_sal_first[] table contains the translation. */
14035 for (s = inword; (c = *s) != NUL; ++s)
14036 {
14037 if (vim_iswhite(c))
14038 c = ' ';
14039 else
14040 c = slang->sl_sal_first[c];
14041 if (c != NUL && (ri == 0 || res[ri - 1] != c))
14042 res[ri++] = c;
14043 }
14044 }
14045
14046 res[ri] = NUL;
14047}
14048
14049 static void
14050spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014051 slang_T *slang;
14052 char_u *inword;
14053 char_u *res;
14054{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014055 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014056 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014057 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014058 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014059 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014060 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014061 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014062 int n, k = 0;
14063 int z0;
14064 int k0;
14065 int n0;
14066 int c;
14067 int pri;
14068 int p0 = -333;
14069 int c0;
14070
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014071 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014072 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014073 if (slang->sl_rem_accents)
14074 {
14075 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014076 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014077 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014078 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014079 {
14080 *t++ = ' ';
14081 s = skipwhite(s);
14082 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014083 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014084 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014085 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014086 *t++ = *s;
14087 ++s;
14088 }
14089 }
14090 *t = NUL;
14091 }
14092 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014093 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014094
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014095 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014096
14097 /*
14098 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014099 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014100 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014101 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014102 while ((c = word[i]) != NUL)
14103 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014104 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014105 n = slang->sl_sal_first[c];
14106 z0 = 0;
14107
14108 if (n >= 0)
14109 {
14110 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014111 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014112 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014113 /* Quickly skip entries that don't match the word. Most
14114 * entries are less then three chars, optimize for that. */
14115 k = smp[n].sm_leadlen;
14116 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014117 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014118 if (word[i + 1] != s[1])
14119 continue;
14120 if (k > 2)
14121 {
14122 for (j = 2; j < k; ++j)
14123 if (word[i + j] != s[j])
14124 break;
14125 if (j < k)
14126 continue;
14127 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014128 }
14129
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014130 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014131 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014132 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014133 while (*pf != NUL && *pf != word[i + k])
14134 ++pf;
14135 if (*pf == NUL)
14136 continue;
14137 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014138 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014139 s = smp[n].sm_rules;
14140 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014141
14142 p0 = *s;
14143 k0 = k;
14144 while (*s == '-' && k > 1)
14145 {
14146 k--;
14147 s++;
14148 }
14149 if (*s == '<')
14150 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014151 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014152 {
14153 /* determine priority */
14154 pri = *s - '0';
14155 s++;
14156 }
14157 if (*s == '^' && *(s + 1) == '^')
14158 s++;
14159
14160 if (*s == NUL
14161 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014162 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014163 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014164 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014165 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014166 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014167 && spell_iswordp(word + i - 1, curbuf)
14168 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014169 {
14170 /* search for followup rules, if: */
14171 /* followup and k > 1 and NO '-' in searchstring */
14172 c0 = word[i + k - 1];
14173 n0 = slang->sl_sal_first[c0];
14174
14175 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014176 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014177 {
14178 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014179 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014180 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014181 /* Quickly skip entries that don't match the word.
14182 * */
14183 k0 = smp[n0].sm_leadlen;
14184 if (k0 > 1)
14185 {
14186 if (word[i + k] != s[1])
14187 continue;
14188 if (k0 > 2)
14189 {
14190 pf = word + i + k + 1;
14191 for (j = 2; j < k0; ++j)
14192 if (*pf++ != s[j])
14193 break;
14194 if (j < k0)
14195 continue;
14196 }
14197 }
14198 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014199
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014200 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014201 {
14202 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014203 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014204 while (*pf != NUL && *pf != word[i + k0])
14205 ++pf;
14206 if (*pf == NUL)
14207 continue;
14208 ++k0;
14209 }
14210
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014211 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014212 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014213 while (*s == '-')
14214 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014215 /* "k0" gets NOT reduced because
14216 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014217 s++;
14218 }
14219 if (*s == '<')
14220 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014221 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014222 {
14223 p0 = *s - '0';
14224 s++;
14225 }
14226
14227 if (*s == NUL
14228 /* *s == '^' cuts */
14229 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014230 && !spell_iswordp(word + i + k0,
14231 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014232 {
14233 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014234 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014235 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014236
14237 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014238 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014239 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014240 /* rule fits; stop search */
14241 break;
14242 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014243 }
14244
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014245 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014246 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014247 }
14248
14249 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014250 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014251 if (s == NULL)
14252 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014253 pf = smp[n].sm_rules;
14254 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014255 if (p0 == 1 && z == 0)
14256 {
14257 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014258 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
14259 || res[reslen - 1] == *s))
14260 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014261 z0 = 1;
14262 z = 1;
14263 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014264 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014265 {
14266 word[i + k0] = *s;
14267 k0++;
14268 s++;
14269 }
14270 if (k > k0)
14271 mch_memmove(word + i + k0, word + i + k,
14272 STRLEN(word + i + k) + 1);
14273
14274 /* new "actual letter" */
14275 c = word[i];
14276 }
14277 else
14278 {
14279 /* no '<' rule used */
14280 i += k - 1;
14281 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014282 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014283 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014284 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014285 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014286 s++;
14287 }
14288 /* new "actual letter" */
14289 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014290 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014291 {
14292 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014293 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014294 mch_memmove(word, word + i + 1,
14295 STRLEN(word + i + 1) + 1);
14296 i = 0;
14297 z0 = 1;
14298 }
14299 }
14300 break;
14301 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014302 }
14303 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014304 else if (vim_iswhite(c))
14305 {
14306 c = ' ';
14307 k = 1;
14308 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014309
14310 if (z0 == 0)
14311 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014312 if (k && !p0 && reslen < MAXWLEN && c != NUL
14313 && (!slang->sl_collapse || reslen == 0
14314 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014315 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014316 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014317
14318 i++;
14319 z = 0;
14320 k = 0;
14321 }
14322 }
14323
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014324 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014325}
14326
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014327#ifdef FEAT_MBYTE
14328/*
14329 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14330 * Multi-byte version of spell_soundfold().
14331 */
14332 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014333spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014334 slang_T *slang;
14335 char_u *inword;
14336 char_u *res;
14337{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014338 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014339 int word[MAXWLEN];
14340 int wres[MAXWLEN];
14341 int l;
14342 char_u *s;
14343 int *ws;
14344 char_u *t;
14345 int *pf;
14346 int i, j, z;
14347 int reslen;
14348 int n, k = 0;
14349 int z0;
14350 int k0;
14351 int n0;
14352 int c;
14353 int pri;
14354 int p0 = -333;
14355 int c0;
14356 int did_white = FALSE;
14357
14358 /*
14359 * Convert the multi-byte string to a wide-character string.
14360 * Remove accents, if wanted. We actually remove all non-word characters.
14361 * But keep white space.
14362 */
14363 n = 0;
14364 for (s = inword; *s != NUL; )
14365 {
14366 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014367 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014368 if (slang->sl_rem_accents)
14369 {
14370 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14371 {
14372 if (did_white)
14373 continue;
14374 c = ' ';
14375 did_white = TRUE;
14376 }
14377 else
14378 {
14379 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014380 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014381 continue;
14382 }
14383 }
14384 word[n++] = c;
14385 }
14386 word[n] = NUL;
14387
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014388 /*
14389 * This comes from Aspell phonet.cpp.
14390 * Converted from C++ to C. Added support for multi-byte chars.
14391 * Changed to keep spaces.
14392 */
14393 i = reslen = z = 0;
14394 while ((c = word[i]) != NUL)
14395 {
14396 /* Start with the first rule that has the character in the word. */
14397 n = slang->sl_sal_first[c & 0xff];
14398 z0 = 0;
14399
14400 if (n >= 0)
14401 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014402 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014403 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
14404 {
14405 /* Quickly skip entries that don't match the word. Most
14406 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014407 if (c != ws[0])
14408 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014409 k = smp[n].sm_leadlen;
14410 if (k > 1)
14411 {
14412 if (word[i + 1] != ws[1])
14413 continue;
14414 if (k > 2)
14415 {
14416 for (j = 2; j < k; ++j)
14417 if (word[i + j] != ws[j])
14418 break;
14419 if (j < k)
14420 continue;
14421 }
14422 }
14423
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014424 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014425 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014426 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014427 while (*pf != NUL && *pf != word[i + k])
14428 ++pf;
14429 if (*pf == NUL)
14430 continue;
14431 ++k;
14432 }
14433 s = smp[n].sm_rules;
14434 pri = 5; /* default priority */
14435
14436 p0 = *s;
14437 k0 = k;
14438 while (*s == '-' && k > 1)
14439 {
14440 k--;
14441 s++;
14442 }
14443 if (*s == '<')
14444 s++;
14445 if (VIM_ISDIGIT(*s))
14446 {
14447 /* determine priority */
14448 pri = *s - '0';
14449 s++;
14450 }
14451 if (*s == '^' && *(s + 1) == '^')
14452 s++;
14453
14454 if (*s == NUL
14455 || (*s == '^'
14456 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014457 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014458 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014459 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014460 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014461 && spell_iswordp_w(word + i - 1, curbuf)
14462 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014463 {
14464 /* search for followup rules, if: */
14465 /* followup and k > 1 and NO '-' in searchstring */
14466 c0 = word[i + k - 1];
14467 n0 = slang->sl_sal_first[c0 & 0xff];
14468
14469 if (slang->sl_followup && k > 1 && n0 >= 0
14470 && p0 != '-' && word[i + k] != NUL)
14471 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014472 /* Test follow-up rule for "word[i + k]"; loop over
14473 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014474 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14475 == (c0 & 0xff); ++n0)
14476 {
14477 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014478 */
14479 if (c0 != ws[0])
14480 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014481 k0 = smp[n0].sm_leadlen;
14482 if (k0 > 1)
14483 {
14484 if (word[i + k] != ws[1])
14485 continue;
14486 if (k0 > 2)
14487 {
14488 pf = word + i + k + 1;
14489 for (j = 2; j < k0; ++j)
14490 if (*pf++ != ws[j])
14491 break;
14492 if (j < k0)
14493 continue;
14494 }
14495 }
14496 k0 += k - 1;
14497
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014498 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014499 {
14500 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014501 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014502 while (*pf != NUL && *pf != word[i + k0])
14503 ++pf;
14504 if (*pf == NUL)
14505 continue;
14506 ++k0;
14507 }
14508
14509 p0 = 5;
14510 s = smp[n0].sm_rules;
14511 while (*s == '-')
14512 {
14513 /* "k0" gets NOT reduced because
14514 * "if (k0 == k)" */
14515 s++;
14516 }
14517 if (*s == '<')
14518 s++;
14519 if (VIM_ISDIGIT(*s))
14520 {
14521 p0 = *s - '0';
14522 s++;
14523 }
14524
14525 if (*s == NUL
14526 /* *s == '^' cuts */
14527 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014528 && !spell_iswordp_w(word + i + k0,
14529 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014530 {
14531 if (k0 == k)
14532 /* this is just a piece of the string */
14533 continue;
14534
14535 if (p0 < pri)
14536 /* priority too low */
14537 continue;
14538 /* rule fits; stop search */
14539 break;
14540 }
14541 }
14542
14543 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14544 == (c0 & 0xff))
14545 continue;
14546 }
14547
14548 /* replace string */
14549 ws = smp[n].sm_to_w;
14550 s = smp[n].sm_rules;
14551 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14552 if (p0 == 1 && z == 0)
14553 {
14554 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014555 if (reslen > 0 && ws != NULL && *ws != NUL
14556 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014557 || wres[reslen - 1] == *ws))
14558 reslen--;
14559 z0 = 1;
14560 z = 1;
14561 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014562 if (ws != NULL)
14563 while (*ws != NUL && word[i + k0] != NUL)
14564 {
14565 word[i + k0] = *ws;
14566 k0++;
14567 ws++;
14568 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014569 if (k > k0)
14570 mch_memmove(word + i + k0, word + i + k,
14571 sizeof(int) * (STRLEN(word + i + k) + 1));
14572
14573 /* new "actual letter" */
14574 c = word[i];
14575 }
14576 else
14577 {
14578 /* no '<' rule used */
14579 i += k - 1;
14580 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014581 if (ws != NULL)
14582 while (*ws != NUL && ws[1] != NUL
14583 && reslen < MAXWLEN)
14584 {
14585 if (reslen == 0 || wres[reslen - 1] != *ws)
14586 wres[reslen++] = *ws;
14587 ws++;
14588 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014589 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014590 if (ws == NULL)
14591 c = NUL;
14592 else
14593 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014594 if (strstr((char *)s, "^^") != NULL)
14595 {
14596 if (c != NUL)
14597 wres[reslen++] = c;
14598 mch_memmove(word, word + i + 1,
14599 sizeof(int) * (STRLEN(word + i + 1) + 1));
14600 i = 0;
14601 z0 = 1;
14602 }
14603 }
14604 break;
14605 }
14606 }
14607 }
14608 else if (vim_iswhite(c))
14609 {
14610 c = ' ';
14611 k = 1;
14612 }
14613
14614 if (z0 == 0)
14615 {
14616 if (k && !p0 && reslen < MAXWLEN && c != NUL
14617 && (!slang->sl_collapse || reslen == 0
14618 || wres[reslen - 1] != c))
14619 /* condense only double letters */
14620 wres[reslen++] = c;
14621
14622 i++;
14623 z = 0;
14624 k = 0;
14625 }
14626 }
14627
14628 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14629 l = 0;
14630 for (n = 0; n < reslen; ++n)
14631 {
14632 l += mb_char2bytes(wres[n], res + l);
14633 if (l + MB_MAXBYTES > MAXWLEN)
14634 break;
14635 }
14636 res[l] = NUL;
14637}
14638#endif
14639
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014640/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014641 * Compute a score for two sound-a-like words.
14642 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14643 * Instead of a generic loop we write out the code. That keeps it fast by
14644 * avoiding checks that will not be possible.
14645 */
14646 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014647soundalike_score(goodstart, badstart)
14648 char_u *goodstart; /* sound-folded good word */
14649 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014650{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014651 char_u *goodsound = goodstart;
14652 char_u *badsound = badstart;
14653 int goodlen;
14654 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014655 int n;
14656 char_u *pl, *ps;
14657 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014658 int score = 0;
14659
14660 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
14661 * counted so much, vowels halfway the word aren't counted at all. */
14662 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14663 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014664 if (badsound[1] == goodsound[1]
14665 || (badsound[1] != NUL
14666 && goodsound[1] != NUL
14667 && badsound[2] == goodsound[2]))
14668 {
14669 /* handle like a substitute */
14670 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014671 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014672 {
14673 score = 2 * SCORE_DEL / 3;
14674 if (*badsound == '*')
14675 ++badsound;
14676 else
14677 ++goodsound;
14678 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014679 }
14680
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014681 goodlen = (int)STRLEN(goodsound);
14682 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014683
Bram Moolenaarf711faf2007-05-10 16:48:19 +000014684 /* Return quickly if the lengths are too different to be fixed by two
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014685 * changes. */
14686 n = goodlen - badlen;
14687 if (n < -2 || n > 2)
14688 return SCORE_MAXMAX;
14689
14690 if (n > 0)
14691 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014692 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014693 ps = badsound;
14694 }
14695 else
14696 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014697 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014698 ps = goodsound;
14699 }
14700
14701 /* Skip over the identical part. */
14702 while (*pl == *ps && *pl != NUL)
14703 {
14704 ++pl;
14705 ++ps;
14706 }
14707
14708 switch (n)
14709 {
14710 case -2:
14711 case 2:
14712 /*
14713 * Must delete two characters from "pl".
14714 */
14715 ++pl; /* first delete */
14716 while (*pl == *ps)
14717 {
14718 ++pl;
14719 ++ps;
14720 }
14721 /* strings must be equal after second delete */
14722 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014723 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014724
14725 /* Failed to compare. */
14726 break;
14727
14728 case -1:
14729 case 1:
14730 /*
14731 * Minimal one delete from "pl" required.
14732 */
14733
14734 /* 1: delete */
14735 pl2 = pl + 1;
14736 ps2 = ps;
14737 while (*pl2 == *ps2)
14738 {
14739 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014740 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014741 ++pl2;
14742 ++ps2;
14743 }
14744
14745 /* 2: delete then swap, then rest must be equal */
14746 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14747 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014748 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014749
14750 /* 3: delete then substitute, then the rest must be equal */
14751 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014752 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014753
14754 /* 4: first swap then delete */
14755 if (pl[0] == ps[1] && pl[1] == ps[0])
14756 {
14757 pl2 = pl + 2; /* swap, skip two chars */
14758 ps2 = ps + 2;
14759 while (*pl2 == *ps2)
14760 {
14761 ++pl2;
14762 ++ps2;
14763 }
14764 /* delete a char and then strings must be equal */
14765 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014766 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014767 }
14768
14769 /* 5: first substitute then delete */
14770 pl2 = pl + 1; /* substitute, skip one char */
14771 ps2 = ps + 1;
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_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014780
14781 /* Failed to compare. */
14782 break;
14783
14784 case 0:
14785 /*
14786 * Lenghts are equal, thus changes must result in same length: An
14787 * insert is only possible in combination with a delete.
14788 * 1: check if for identical strings
14789 */
14790 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014791 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014792
14793 /* 2: swap */
14794 if (pl[0] == ps[1] && pl[1] == ps[0])
14795 {
14796 pl2 = pl + 2; /* swap, skip two chars */
14797 ps2 = ps + 2;
14798 while (*pl2 == *ps2)
14799 {
14800 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014801 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014802 ++pl2;
14803 ++ps2;
14804 }
14805 /* 3: swap and swap again */
14806 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14807 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014808 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014809
14810 /* 4: swap and substitute */
14811 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014812 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014813 }
14814
14815 /* 5: substitute */
14816 pl2 = pl + 1;
14817 ps2 = ps + 1;
14818 while (*pl2 == *ps2)
14819 {
14820 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014821 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014822 ++pl2;
14823 ++ps2;
14824 }
14825
14826 /* 6: substitute and swap */
14827 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14828 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014829 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014830
14831 /* 7: substitute and substitute */
14832 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014833 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014834
14835 /* 8: insert then delete */
14836 pl2 = pl;
14837 ps2 = ps + 1;
14838 while (*pl2 == *ps2)
14839 {
14840 ++pl2;
14841 ++ps2;
14842 }
14843 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014844 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014845
14846 /* 9: delete then insert */
14847 pl2 = pl + 1;
14848 ps2 = ps;
14849 while (*pl2 == *ps2)
14850 {
14851 ++pl2;
14852 ++ps2;
14853 }
14854 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014855 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014856
14857 /* Failed to compare. */
14858 break;
14859 }
14860
14861 return SCORE_MAXMAX;
14862}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014863
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014864/*
14865 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014866 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014867 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000014868 * The algorithm is described by Du and Chang, 1992.
14869 * The implementation of the algorithm comes from Aspell editdist.cpp,
14870 * edit_distance(). It has been converted from C++ to C and modified to
14871 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014872 */
14873 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000014874spell_edit_score(slang, badword, goodword)
14875 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014876 char_u *badword;
14877 char_u *goodword;
14878{
14879 int *cnt;
Bram Moolenaarf711faf2007-05-10 16:48:19 +000014880 int badlen, goodlen; /* lengths including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014881 int j, i;
14882 int t;
14883 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014884 int pbc, pgc;
14885#ifdef FEAT_MBYTE
14886 char_u *p;
14887 int wbadword[MAXWLEN];
14888 int wgoodword[MAXWLEN];
14889
14890 if (has_mbyte)
14891 {
14892 /* Get the characters from the multi-byte strings and put them in an
14893 * int array for easy access. */
14894 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014895 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014896 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014897 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014898 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014899 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014900 }
14901 else
14902#endif
14903 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014904 badlen = (int)STRLEN(badword) + 1;
14905 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014906 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014907
14908 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
14909#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014910 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
14911 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014912 if (cnt == NULL)
14913 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014914
14915 CNT(0, 0) = 0;
14916 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000014917 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014918
14919 for (i = 1; i <= badlen; ++i)
14920 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014921 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014922 for (j = 1; j <= goodlen; ++j)
14923 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014924#ifdef FEAT_MBYTE
14925 if (has_mbyte)
14926 {
14927 bc = wbadword[i - 1];
14928 gc = wgoodword[j - 1];
14929 }
14930 else
14931#endif
14932 {
14933 bc = badword[i - 1];
14934 gc = goodword[j - 1];
14935 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014936 if (bc == gc)
14937 CNT(i, j) = CNT(i - 1, j - 1);
14938 else
14939 {
14940 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014941 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014942 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
14943 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014944 {
14945 /* For a similar character use SCORE_SIMILAR. */
14946 if (slang != NULL
14947 && slang->sl_has_map
14948 && similar_chars(slang, gc, bc))
14949 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
14950 else
14951 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
14952 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014953
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014954 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014955 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014956#ifdef FEAT_MBYTE
14957 if (has_mbyte)
14958 {
14959 pbc = wbadword[i - 2];
14960 pgc = wgoodword[j - 2];
14961 }
14962 else
14963#endif
14964 {
14965 pbc = badword[i - 2];
14966 pgc = goodword[j - 2];
14967 }
14968 if (bc == pgc && pbc == gc)
14969 {
14970 t = SCORE_SWAP + CNT(i - 2, j - 2);
14971 if (t < CNT(i, j))
14972 CNT(i, j) = t;
14973 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014974 }
14975 t = SCORE_DEL + CNT(i - 1, j);
14976 if (t < CNT(i, j))
14977 CNT(i, j) = t;
14978 t = SCORE_INS + CNT(i, j - 1);
14979 if (t < CNT(i, j))
14980 CNT(i, j) = t;
14981 }
14982 }
14983 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014984
14985 i = CNT(badlen - 1, goodlen - 1);
14986 vim_free(cnt);
14987 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014988}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000014989
Bram Moolenaar4770d092006-01-12 23:22:24 +000014990typedef struct
14991{
14992 int badi;
14993 int goodi;
14994 int score;
14995} limitscore_T;
14996
14997/*
14998 * Like spell_edit_score(), but with a limit on the score to make it faster.
14999 * May return SCORE_MAXMAX when the score is higher than "limit".
15000 *
15001 * This uses a stack for the edits still to be tried.
15002 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
15003 * for multi-byte characters.
15004 */
15005 static int
15006spell_edit_score_limit(slang, badword, goodword, limit)
15007 slang_T *slang;
15008 char_u *badword;
15009 char_u *goodword;
15010 int limit;
15011{
15012 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15013 int stackidx;
15014 int bi, gi;
15015 int bi2, gi2;
15016 int bc, gc;
15017 int score;
15018 int score_off;
15019 int minscore;
15020 int round;
15021
15022#ifdef FEAT_MBYTE
15023 /* Multi-byte characters require a bit more work, use a different function
15024 * to avoid testing "has_mbyte" quite often. */
15025 if (has_mbyte)
15026 return spell_edit_score_limit_w(slang, badword, goodword, limit);
15027#endif
15028
15029 /*
15030 * The idea is to go from start to end over the words. So long as
15031 * characters are equal just continue, this always gives the lowest score.
15032 * When there is a difference try several alternatives. Each alternative
15033 * increases "score" for the edit distance. Some of the alternatives are
15034 * pushed unto a stack and tried later, some are tried right away. At the
15035 * end of the word the score for one alternative is known. The lowest
15036 * possible score is stored in "minscore".
15037 */
15038 stackidx = 0;
15039 bi = 0;
15040 gi = 0;
15041 score = 0;
15042 minscore = limit + 1;
15043
15044 for (;;)
15045 {
15046 /* Skip over an equal part, score remains the same. */
15047 for (;;)
15048 {
15049 bc = badword[bi];
15050 gc = goodword[gi];
15051 if (bc != gc) /* stop at a char that's different */
15052 break;
15053 if (bc == NUL) /* both words end */
15054 {
15055 if (score < minscore)
15056 minscore = score;
15057 goto pop; /* do next alternative */
15058 }
15059 ++bi;
15060 ++gi;
15061 }
15062
15063 if (gc == NUL) /* goodword ends, delete badword chars */
15064 {
15065 do
15066 {
15067 if ((score += SCORE_DEL) >= minscore)
15068 goto pop; /* do next alternative */
15069 } while (badword[++bi] != NUL);
15070 minscore = score;
15071 }
15072 else if (bc == NUL) /* badword ends, insert badword chars */
15073 {
15074 do
15075 {
15076 if ((score += SCORE_INS) >= minscore)
15077 goto pop; /* do next alternative */
15078 } while (goodword[++gi] != NUL);
15079 minscore = score;
15080 }
15081 else /* both words continue */
15082 {
15083 /* If not close to the limit, perform a change. Only try changes
15084 * that may lead to a lower score than "minscore".
15085 * round 0: try deleting a char from badword
15086 * round 1: try inserting a char in badword */
15087 for (round = 0; round <= 1; ++round)
15088 {
15089 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15090 if (score_off < minscore)
15091 {
15092 if (score_off + SCORE_EDIT_MIN >= minscore)
15093 {
15094 /* Near the limit, rest of the words must match. We
15095 * can check that right now, no need to push an item
15096 * onto the stack. */
15097 bi2 = bi + 1 - round;
15098 gi2 = gi + round;
15099 while (goodword[gi2] == badword[bi2])
15100 {
15101 if (goodword[gi2] == NUL)
15102 {
15103 minscore = score_off;
15104 break;
15105 }
15106 ++bi2;
15107 ++gi2;
15108 }
15109 }
15110 else
15111 {
15112 /* try deleting/inserting a character later */
15113 stack[stackidx].badi = bi + 1 - round;
15114 stack[stackidx].goodi = gi + round;
15115 stack[stackidx].score = score_off;
15116 ++stackidx;
15117 }
15118 }
15119 }
15120
15121 if (score + SCORE_SWAP < minscore)
15122 {
15123 /* If swapping two characters makes a match then the
15124 * substitution is more expensive, thus there is no need to
15125 * try both. */
15126 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
15127 {
15128 /* Swap two characters, that is: skip them. */
15129 gi += 2;
15130 bi += 2;
15131 score += SCORE_SWAP;
15132 continue;
15133 }
15134 }
15135
15136 /* Substitute one character for another which is the same
15137 * thing as deleting a character from both goodword and badword.
15138 * Use a better score when there is only a case difference. */
15139 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15140 score += SCORE_ICASE;
15141 else
15142 {
15143 /* For a similar character use SCORE_SIMILAR. */
15144 if (slang != NULL
15145 && slang->sl_has_map
15146 && similar_chars(slang, gc, bc))
15147 score += SCORE_SIMILAR;
15148 else
15149 score += SCORE_SUBST;
15150 }
15151
15152 if (score < minscore)
15153 {
15154 /* Do the substitution. */
15155 ++gi;
15156 ++bi;
15157 continue;
15158 }
15159 }
15160pop:
15161 /*
15162 * Get here to try the next alternative, pop it from the stack.
15163 */
15164 if (stackidx == 0) /* stack is empty, finished */
15165 break;
15166
15167 /* pop an item from the stack */
15168 --stackidx;
15169 gi = stack[stackidx].goodi;
15170 bi = stack[stackidx].badi;
15171 score = stack[stackidx].score;
15172 }
15173
15174 /* When the score goes over "limit" it may actually be much higher.
15175 * Return a very large number to avoid going below the limit when giving a
15176 * bonus. */
15177 if (minscore > limit)
15178 return SCORE_MAXMAX;
15179 return minscore;
15180}
15181
15182#ifdef FEAT_MBYTE
15183/*
15184 * Multi-byte version of spell_edit_score_limit().
15185 * Keep it in sync with the above!
15186 */
15187 static int
15188spell_edit_score_limit_w(slang, badword, goodword, limit)
15189 slang_T *slang;
15190 char_u *badword;
15191 char_u *goodword;
15192 int limit;
15193{
15194 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15195 int stackidx;
15196 int bi, gi;
15197 int bi2, gi2;
15198 int bc, gc;
15199 int score;
15200 int score_off;
15201 int minscore;
15202 int round;
15203 char_u *p;
15204 int wbadword[MAXWLEN];
15205 int wgoodword[MAXWLEN];
15206
15207 /* Get the characters from the multi-byte strings and put them in an
15208 * int array for easy access. */
15209 bi = 0;
15210 for (p = badword; *p != NUL; )
15211 wbadword[bi++] = mb_cptr2char_adv(&p);
15212 wbadword[bi++] = 0;
15213 gi = 0;
15214 for (p = goodword; *p != NUL; )
15215 wgoodword[gi++] = mb_cptr2char_adv(&p);
15216 wgoodword[gi++] = 0;
15217
15218 /*
15219 * The idea is to go from start to end over the words. So long as
15220 * characters are equal just continue, this always gives the lowest score.
15221 * When there is a difference try several alternatives. Each alternative
15222 * increases "score" for the edit distance. Some of the alternatives are
15223 * pushed unto a stack and tried later, some are tried right away. At the
15224 * end of the word the score for one alternative is known. The lowest
15225 * possible score is stored in "minscore".
15226 */
15227 stackidx = 0;
15228 bi = 0;
15229 gi = 0;
15230 score = 0;
15231 minscore = limit + 1;
15232
15233 for (;;)
15234 {
15235 /* Skip over an equal part, score remains the same. */
15236 for (;;)
15237 {
15238 bc = wbadword[bi];
15239 gc = wgoodword[gi];
15240
15241 if (bc != gc) /* stop at a char that's different */
15242 break;
15243 if (bc == NUL) /* both words end */
15244 {
15245 if (score < minscore)
15246 minscore = score;
15247 goto pop; /* do next alternative */
15248 }
15249 ++bi;
15250 ++gi;
15251 }
15252
15253 if (gc == NUL) /* goodword ends, delete badword chars */
15254 {
15255 do
15256 {
15257 if ((score += SCORE_DEL) >= minscore)
15258 goto pop; /* do next alternative */
15259 } while (wbadword[++bi] != NUL);
15260 minscore = score;
15261 }
15262 else if (bc == NUL) /* badword ends, insert badword chars */
15263 {
15264 do
15265 {
15266 if ((score += SCORE_INS) >= minscore)
15267 goto pop; /* do next alternative */
15268 } while (wgoodword[++gi] != NUL);
15269 minscore = score;
15270 }
15271 else /* both words continue */
15272 {
15273 /* If not close to the limit, perform a change. Only try changes
15274 * that may lead to a lower score than "minscore".
15275 * round 0: try deleting a char from badword
15276 * round 1: try inserting a char in badword */
15277 for (round = 0; round <= 1; ++round)
15278 {
15279 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15280 if (score_off < minscore)
15281 {
15282 if (score_off + SCORE_EDIT_MIN >= minscore)
15283 {
15284 /* Near the limit, rest of the words must match. We
15285 * can check that right now, no need to push an item
15286 * onto the stack. */
15287 bi2 = bi + 1 - round;
15288 gi2 = gi + round;
15289 while (wgoodword[gi2] == wbadword[bi2])
15290 {
15291 if (wgoodword[gi2] == NUL)
15292 {
15293 minscore = score_off;
15294 break;
15295 }
15296 ++bi2;
15297 ++gi2;
15298 }
15299 }
15300 else
15301 {
15302 /* try deleting a character from badword later */
15303 stack[stackidx].badi = bi + 1 - round;
15304 stack[stackidx].goodi = gi + round;
15305 stack[stackidx].score = score_off;
15306 ++stackidx;
15307 }
15308 }
15309 }
15310
15311 if (score + SCORE_SWAP < minscore)
15312 {
15313 /* If swapping two characters makes a match then the
15314 * substitution is more expensive, thus there is no need to
15315 * try both. */
15316 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
15317 {
15318 /* Swap two characters, that is: skip them. */
15319 gi += 2;
15320 bi += 2;
15321 score += SCORE_SWAP;
15322 continue;
15323 }
15324 }
15325
15326 /* Substitute one character for another which is the same
15327 * thing as deleting a character from both goodword and badword.
15328 * Use a better score when there is only a case difference. */
15329 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15330 score += SCORE_ICASE;
15331 else
15332 {
15333 /* For a similar character use SCORE_SIMILAR. */
15334 if (slang != NULL
15335 && slang->sl_has_map
15336 && similar_chars(slang, gc, bc))
15337 score += SCORE_SIMILAR;
15338 else
15339 score += SCORE_SUBST;
15340 }
15341
15342 if (score < minscore)
15343 {
15344 /* Do the substitution. */
15345 ++gi;
15346 ++bi;
15347 continue;
15348 }
15349 }
15350pop:
15351 /*
15352 * Get here to try the next alternative, pop it from the stack.
15353 */
15354 if (stackidx == 0) /* stack is empty, finished */
15355 break;
15356
15357 /* pop an item from the stack */
15358 --stackidx;
15359 gi = stack[stackidx].goodi;
15360 bi = stack[stackidx].badi;
15361 score = stack[stackidx].score;
15362 }
15363
15364 /* When the score goes over "limit" it may actually be much higher.
15365 * Return a very large number to avoid going below the limit when giving a
15366 * bonus. */
15367 if (minscore > limit)
15368 return SCORE_MAXMAX;
15369 return minscore;
15370}
15371#endif
15372
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015373/*
15374 * ":spellinfo"
15375 */
15376/*ARGSUSED*/
15377 void
15378ex_spellinfo(eap)
15379 exarg_T *eap;
15380{
15381 int lpi;
15382 langp_T *lp;
15383 char_u *p;
15384
15385 if (no_spell_checking(curwin))
15386 return;
15387
15388 msg_start();
15389 for (lpi = 0; lpi < curbuf->b_langp.ga_len && !got_int; ++lpi)
15390 {
15391 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
15392 msg_puts((char_u *)"file: ");
15393 msg_puts(lp->lp_slang->sl_fname);
15394 msg_putchar('\n');
15395 p = lp->lp_slang->sl_info;
15396 if (p != NULL)
15397 {
15398 msg_puts(p);
15399 msg_putchar('\n');
15400 }
15401 }
15402 msg_end();
15403}
15404
Bram Moolenaar4770d092006-01-12 23:22:24 +000015405#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
15406#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015407#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +000015408#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
15409#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015410
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015411/*
15412 * ":spelldump"
15413 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015414 void
15415ex_spelldump(eap)
15416 exarg_T *eap;
15417{
15418 buf_T *buf = curbuf;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015419
15420 if (no_spell_checking(curwin))
15421 return;
15422
15423 /* Create a new empty buffer by splitting the window. */
15424 do_cmdline_cmd((char_u *)"new");
15425 if (!bufempty() || !buf_valid(buf))
15426 return;
15427
15428 spell_dump_compl(buf, NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
15429
15430 /* Delete the empty line that we started with. */
15431 if (curbuf->b_ml.ml_line_count > 1)
15432 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
15433
15434 redraw_later(NOT_VALID);
15435}
15436
15437/*
15438 * Go through all possible words and:
15439 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
15440 * "ic" and "dir" are not used.
15441 * 2. When "pat" is not NULL: add matching words to insert mode completion.
15442 */
15443 void
15444spell_dump_compl(buf, pat, ic, dir, dumpflags_arg)
15445 buf_T *buf; /* buffer with spell checking */
15446 char_u *pat; /* leading part of the word */
15447 int ic; /* ignore case */
15448 int *dir; /* direction for adding matches */
15449 int dumpflags_arg; /* DUMPFLAG_* */
15450{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015451 langp_T *lp;
15452 slang_T *slang;
15453 idx_T arridx[MAXWLEN];
15454 int curi[MAXWLEN];
15455 char_u word[MAXWLEN];
15456 int c;
15457 char_u *byts;
15458 idx_T *idxs;
15459 linenr_T lnum = 0;
15460 int round;
15461 int depth;
15462 int n;
15463 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015464 char_u *region_names = NULL; /* region names being used */
15465 int do_region = TRUE; /* dump region names and numbers */
15466 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015467 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015468 int dumpflags = dumpflags_arg;
15469 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015470
Bram Moolenaard0131a82006-03-04 21:46:13 +000015471 /* When ignoring case or when the pattern starts with capital pass this on
15472 * to dump_word(). */
15473 if (pat != NULL)
15474 {
15475 if (ic)
15476 dumpflags |= DUMPFLAG_ICASE;
15477 else
15478 {
15479 n = captype(pat, NULL);
15480 if (n == WF_ONECAP)
15481 dumpflags |= DUMPFLAG_ONECAP;
15482 else if (n == WF_ALLCAP
15483#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015484 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015485#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015486 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +000015487#endif
15488 )
15489 dumpflags |= DUMPFLAG_ALLCAP;
15490 }
15491 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015492
Bram Moolenaar7887d882005-07-01 22:33:52 +000015493 /* Find out if we can support regions: All languages must support the same
15494 * regions or none at all. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015495 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000015496 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015497 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000015498 p = lp->lp_slang->sl_regions;
15499 if (p[0] != 0)
15500 {
15501 if (region_names == NULL) /* first language with regions */
15502 region_names = p;
15503 else if (STRCMP(region_names, p) != 0)
15504 {
15505 do_region = FALSE; /* region names are different */
15506 break;
15507 }
15508 }
15509 }
15510
15511 if (do_region && region_names != NULL)
15512 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015513 if (pat == NULL)
15514 {
15515 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15516 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15517 }
Bram Moolenaar7887d882005-07-01 22:33:52 +000015518 }
15519 else
15520 do_region = FALSE;
15521
15522 /*
15523 * Loop over all files loaded for the entries in 'spelllang'.
15524 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015525 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015526 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015527 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015528 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015529 if (slang->sl_fbyts == NULL) /* reloading failed */
15530 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015531
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015532 if (pat == NULL)
15533 {
15534 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15535 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15536 }
15537
15538 /* When matching with a pattern and there are no prefixes only use
15539 * parts of the tree that match "pat". */
15540 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015541 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015542 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015543 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015544
15545 /* round 1: case-folded tree
15546 * round 2: keep-case tree */
15547 for (round = 1; round <= 2; ++round)
15548 {
15549 if (round == 1)
15550 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015551 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015552 byts = slang->sl_fbyts;
15553 idxs = slang->sl_fidxs;
15554 }
15555 else
15556 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015557 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015558 byts = slang->sl_kbyts;
15559 idxs = slang->sl_kidxs;
15560 }
15561 if (byts == NULL)
15562 continue; /* array is empty */
15563
15564 depth = 0;
15565 arridx[0] = 0;
15566 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015567 while (depth >= 0 && !got_int
15568 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015569 {
15570 if (curi[depth] > byts[arridx[depth]])
15571 {
15572 /* Done all bytes at this node, go up one level. */
15573 --depth;
15574 line_breakcheck();
Bram Moolenaara2031822006-03-07 22:29:51 +000015575 ins_compl_check_keys(50);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015576 }
15577 else
15578 {
15579 /* Do one more byte at this node. */
15580 n = arridx[depth] + curi[depth];
15581 ++curi[depth];
15582 c = byts[n];
15583 if (c == 0)
15584 {
15585 /* End of word, deal with the word.
15586 * Don't use keep-case words in the fold-case tree,
15587 * they will appear in the keep-case tree.
15588 * Only use the word when the region matches. */
15589 flags = (int)idxs[n];
15590 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015591 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000015592 && (do_region
15593 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015594 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015595 & lp->lp_region) != 0))
15596 {
15597 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015598 if (!do_region)
15599 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015600
15601 /* Dump the basic word if there is no prefix or
15602 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015603 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015604 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015605 {
15606 dump_word(slang, word, pat, dir,
15607 dumpflags, flags, lnum);
15608 if (pat == NULL)
15609 ++lnum;
15610 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015611
15612 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015613 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015614 lnum = dump_prefixes(slang, word, pat, dir,
15615 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015616 }
15617 }
15618 else
15619 {
15620 /* Normal char, go one level deeper. */
15621 word[depth++] = c;
15622 arridx[depth] = idxs[n];
15623 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015624
15625 /* Check if this characters matches with the pattern.
15626 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +000015627 * Always ignore case here, dump_word() will check
15628 * proper case later. This isn't exactly right when
15629 * length changes for multi-byte characters with
15630 * ignore case... */
15631 if (depth <= patlen
15632 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015633 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015634 }
15635 }
15636 }
15637 }
15638 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015639}
15640
15641/*
15642 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015643 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015644 */
15645 static void
Bram Moolenaard0131a82006-03-04 21:46:13 +000015646dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015647 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015648 char_u *word;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015649 char_u *pat;
15650 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015651 int dumpflags;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015652 int wordflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015653 linenr_T lnum;
15654{
15655 int keepcap = FALSE;
15656 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015657 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015658 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000015659 char_u badword[MAXWLEN + 10];
15660 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015661 int flags = wordflags;
15662
15663 if (dumpflags & DUMPFLAG_ONECAP)
15664 flags |= WF_ONECAP;
15665 if (dumpflags & DUMPFLAG_ALLCAP)
15666 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015667
Bram Moolenaar4770d092006-01-12 23:22:24 +000015668 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015669 {
15670 /* Need to fix case according to "flags". */
15671 make_case_word(word, cword, flags);
15672 p = cword;
15673 }
15674 else
15675 {
15676 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015677 if ((dumpflags & DUMPFLAG_KEEPCASE)
15678 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015679 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015680 keepcap = TRUE;
15681 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015682 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015683
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015684 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015685 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015686 /* Add flags and regions after a slash. */
15687 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015688 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015689 STRCPY(badword, p);
15690 STRCAT(badword, "/");
15691 if (keepcap)
15692 STRCAT(badword, "=");
15693 if (flags & WF_BANNED)
15694 STRCAT(badword, "!");
15695 else if (flags & WF_RARE)
15696 STRCAT(badword, "?");
15697 if (flags & WF_REGION)
15698 for (i = 0; i < 7; ++i)
15699 if (flags & (0x10000 << i))
15700 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
15701 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015702 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015703
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015704 if (dumpflags & DUMPFLAG_COUNT)
15705 {
15706 hashitem_T *hi;
15707
15708 /* Include the word count for ":spelldump!". */
15709 hi = hash_find(&slang->sl_wordcount, tw);
15710 if (!HASHITEM_EMPTY(hi))
15711 {
15712 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15713 tw, HI2WC(hi)->wc_count);
15714 p = IObuff;
15715 }
15716 }
15717
15718 ml_append(lnum, p, (colnr_T)0, FALSE);
15719 }
Bram Moolenaard0131a82006-03-04 21:46:13 +000015720 else if (((dumpflags & DUMPFLAG_ICASE)
15721 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
15722 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015723 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaare8c3a142006-08-29 14:30:35 +000015724 p_ic, NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015725 /* if dir was BACKWARD then honor it just once */
15726 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015727}
15728
15729/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000015730 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15731 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015732 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015733 * Return the updated line number.
15734 */
15735 static linenr_T
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015736dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015737 slang_T *slang;
15738 char_u *word; /* case-folded word */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015739 char_u *pat;
15740 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015741 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015742 int flags; /* flags with prefix ID */
15743 linenr_T startlnum;
15744{
15745 idx_T arridx[MAXWLEN];
15746 int curi[MAXWLEN];
15747 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015748 char_u word_up[MAXWLEN];
15749 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015750 int c;
15751 char_u *byts;
15752 idx_T *idxs;
15753 linenr_T lnum = startlnum;
15754 int depth;
15755 int n;
15756 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015757 int i;
15758
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015759 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015760 * upper-case letter in word_up[]. */
15761 c = PTR2CHAR(word);
15762 if (SPELL_TOUPPER(c) != c)
15763 {
15764 onecap_copy(word, word_up, TRUE);
15765 has_word_up = TRUE;
15766 }
15767
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015768 byts = slang->sl_pbyts;
15769 idxs = slang->sl_pidxs;
15770 if (byts != NULL) /* array not is empty */
15771 {
15772 /*
15773 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015774 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015775 */
15776 depth = 0;
15777 arridx[0] = 0;
15778 curi[0] = 1;
15779 while (depth >= 0 && !got_int)
15780 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015781 n = arridx[depth];
15782 len = byts[n];
15783 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015784 {
15785 /* Done all bytes at this node, go up one level. */
15786 --depth;
15787 line_breakcheck();
15788 }
15789 else
15790 {
15791 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015792 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015793 ++curi[depth];
15794 c = byts[n];
15795 if (c == 0)
15796 {
15797 /* End of prefix, find out how many IDs there are. */
15798 for (i = 1; i < len; ++i)
15799 if (byts[n + i] != 0)
15800 break;
15801 curi[depth] += i - 1;
15802
Bram Moolenaar53805d12005-08-01 07:08:33 +000015803 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15804 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015805 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015806 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015807 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015808 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015809 : flags, lnum);
15810 if (lnum != 0)
15811 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015812 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000015813
15814 /* Check for prefix that matches the word when the
15815 * first letter is upper-case, but only if the prefix has
15816 * a condition. */
15817 if (has_word_up)
15818 {
15819 c = valid_word_prefix(i, n, flags, word_up, slang,
15820 TRUE);
15821 if (c != 0)
15822 {
15823 vim_strncpy(prefix + depth, word_up,
15824 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015825 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015826 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015827 : flags, lnum);
15828 if (lnum != 0)
15829 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +000015830 }
15831 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015832 }
15833 else
15834 {
15835 /* Normal char, go one level deeper. */
15836 prefix[depth++] = c;
15837 arridx[depth] = idxs[n];
15838 curi[depth] = 1;
15839 }
15840 }
15841 }
15842 }
15843
15844 return lnum;
15845}
15846
Bram Moolenaar95529562005-08-25 21:21:38 +000015847/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015848 * Move "p" to the end of word "start".
15849 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000015850 */
15851 char_u *
15852spell_to_word_end(start, buf)
15853 char_u *start;
15854 buf_T *buf;
15855{
15856 char_u *p = start;
15857
15858 while (*p != NUL && spell_iswordp(p, buf))
15859 mb_ptr_adv(p);
15860 return p;
15861}
15862
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015863#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015864/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015865 * For Insert mode completion CTRL-X s:
15866 * Find start of the word in front of column "startcol".
15867 * We don't check if it is badly spelled, with completion we can only change
15868 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015869 * Returns the column number of the word.
15870 */
15871 int
15872spell_word_start(startcol)
15873 int startcol;
15874{
15875 char_u *line;
15876 char_u *p;
15877 int col = 0;
15878
Bram Moolenaar95529562005-08-25 21:21:38 +000015879 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015880 return startcol;
15881
15882 /* Find a word character before "startcol". */
15883 line = ml_get_curline();
15884 for (p = line + startcol; p > line; )
15885 {
15886 mb_ptr_back(line, p);
15887 if (spell_iswordp_nmw(p))
15888 break;
15889 }
15890
15891 /* Go back to start of the word. */
15892 while (p > line)
15893 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015894 col = (int)(p - line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015895 mb_ptr_back(line, p);
15896 if (!spell_iswordp(p, curbuf))
15897 break;
15898 col = 0;
15899 }
15900
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015901 return col;
15902}
15903
15904/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000015905 * Need to check for 'spellcapcheck' now, the word is removed before
15906 * expand_spelling() is called. Therefore the ugly global variable.
15907 */
15908static int spell_expand_need_cap;
15909
15910 void
15911spell_expand_check_cap(col)
15912 colnr_T col;
15913{
15914 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
15915}
15916
15917/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015918 * Get list of spelling suggestions.
15919 * Used for Insert mode completion CTRL-X ?.
15920 * Returns the number of matches. The matches are in "matchp[]", array of
15921 * allocated strings.
15922 */
15923/*ARGSUSED*/
15924 int
15925expand_spelling(lnum, col, pat, matchp)
15926 linenr_T lnum;
15927 int col;
15928 char_u *pat;
15929 char_u ***matchp;
15930{
15931 garray_T ga;
15932
Bram Moolenaar4770d092006-01-12 23:22:24 +000015933 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015934 *matchp = ga.ga_data;
15935 return ga.ga_len;
15936}
15937#endif
15938
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000015939#endif /* FEAT_SPELL */