blob: 787a2c1ca3200038ddd45a52388683d34f322df5 [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;
590#define WC_KEY_OFF (dumwc.wc_word - (char_u *)&dumwc)
591#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 Moolenaar43abc522005-12-10 20:15:02 +00001051 nrlen = 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);
1089 mi.mi_fwordlen = STRLEN(mi.mi_fword);
1090
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;
1190 mi.mi_compoff = fp - mi.mi_fword;
1191 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);
1419 wlen = p - mip->mi_word;
1420 }
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 Moolenaar5195e452005-08-19 20:32:47 +00001486 /* If there is no flag or the word is shorter than
1487 * 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);
1620 mip->mi_compoff = p - mip->mi_fword;
1621 }
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);
1983 flen = STRLEN(mip->mi_fword + mip->mi_fwordlen);
1984 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 Moolenaar2cf8b302005-04-20 19:37:22 +00002046 int col;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002047# endif
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 Moolenaar0c405862005-06-22 22:26:26 +00002077 len = STRLEN(line);
2078 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)
2093 capcol = skipwhite(line) - line;
2094
Bram Moolenaar0c405862005-06-22 22:26:26 +00002095 /* Copy the line into "buf" and append the start of the next line if
2096 * possible. */
2097 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00002098 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002099 spell_cat_line(buf + STRLEN(buf), ml_get(lnum + 1), MAXWLEN);
2100
2101 p = buf + skip;
2102 endp = buf + len;
2103 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002104 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002105 /* When searching backward don't search after the cursor. Unless
2106 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002107 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00002108 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002109 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00002110 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002111 break;
2112
2113 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002114 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002115 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002116
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002117 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002118 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002119 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002120 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002121 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002122 found_one = TRUE;
2123
Bram Moolenaar51485f02005-06-04 21:55:20 +00002124 /* When searching forward only accept a bad word after
2125 * the cursor. */
2126 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002127 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00002128 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002129 && (wrapped
2130 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00002131 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002132 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002133 {
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002134# ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00002135 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002136 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00002137 col = p - buf;
Bram Moolenaar95529562005-08-25 21:21:38 +00002138 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar51485f02005-06-04 21:55:20 +00002139 FALSE, &can_spell);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002140 }
2141 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002142#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002143 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002144
Bram Moolenaar51485f02005-06-04 21:55:20 +00002145 if (can_spell)
2146 {
2147 found_pos.lnum = lnum;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002148 found_pos.col = p - buf;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002149#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00002150 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002151#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002152 if (dir == FORWARD)
2153 {
2154 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00002155 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002156 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002157 if (attrp != NULL)
2158 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002159 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002160 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002161 else if (curline)
2162 /* Insert mode completion: put cursor after
2163 * the bad word. */
2164 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002165 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002166 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002167 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002168 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002169 }
2170
Bram Moolenaar51485f02005-06-04 21:55:20 +00002171 /* advance to character after the word */
2172 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002173 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002174 }
2175
Bram Moolenaar5195e452005-08-19 20:32:47 +00002176 if (dir == BACKWARD && found_pos.lnum != 0)
2177 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002178 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00002179 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002180 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002181 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002182 }
2183
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002184 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002185 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002186
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002187 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002188 if (dir == BACKWARD)
2189 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002190 /* If we are back at the starting line and searched it again there
2191 * is no match, give up. */
2192 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002193 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002194
2195 if (lnum > 1)
2196 --lnum;
2197 else if (!p_ws)
2198 break; /* at first line and 'nowrapscan' */
2199 else
2200 {
2201 /* Wrap around to the end of the buffer. May search the
2202 * starting line again and accept the last match. */
2203 lnum = wp->w_buffer->b_ml.ml_line_count;
2204 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002205 if (!shortmess(SHM_SEARCH))
2206 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002207 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002208 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002209 }
2210 else
2211 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002212 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2213 ++lnum;
2214 else if (!p_ws)
2215 break; /* at first line and 'nowrapscan' */
2216 else
2217 {
2218 /* Wrap around to the start of the buffer. May search the
2219 * starting line again and accept the first match. */
2220 lnum = 1;
2221 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002222 if (!shortmess(SHM_SEARCH))
2223 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002224 }
2225
2226 /* If we are back at the starting line and there is no match then
2227 * give up. */
2228 if (lnum == wp->w_cursor.lnum && !found_one)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002229 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002230
2231 /* Skip the characters at the start of the next line that were
2232 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002233 if (attr == HLF_COUNT)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002234 skip = p - endp;
2235 else
2236 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002237
2238 /* Capscol skips over the inserted space. */
2239 --capcol;
2240
2241 /* But after empty line check first word in next line */
2242 if (*skipwhite(line) == NUL)
2243 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002244 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002245
2246 line_breakcheck();
2247 }
2248
Bram Moolenaar0c405862005-06-22 22:26:26 +00002249 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002250 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002251}
2252
2253/*
2254 * For spell checking: concatenate the start of the following line "line" into
2255 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
2256 */
2257 void
2258spell_cat_line(buf, line, maxlen)
2259 char_u *buf;
2260 char_u *line;
2261 int maxlen;
2262{
2263 char_u *p;
2264 int n;
2265
2266 p = skipwhite(line);
2267 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2268 p = skipwhite(p + 1);
2269
2270 if (*p != NUL)
2271 {
2272 *buf = ' ';
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002273 vim_strncpy(buf + 1, line, maxlen - 2);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002274 n = p - line;
2275 if (n >= maxlen)
2276 n = maxlen - 1;
2277 vim_memset(buf + 1, ' ', n);
2278 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002279}
2280
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002281/*
2282 * Structure used for the cookie argument of do_in_runtimepath().
2283 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002284typedef struct spelload_S
2285{
2286 char_u sl_lang[MAXWLEN + 1]; /* language name */
2287 slang_T *sl_slang; /* resulting slang_T struct */
2288 int sl_nobreak; /* NOBREAK language found */
2289} spelload_T;
2290
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002291/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002292 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002293 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002294 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002295 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002296spell_load_lang(lang)
2297 char_u *lang;
2298{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002299 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002300 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002301 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002302#ifdef FEAT_AUTOCMD
2303 int round;
2304#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002305
Bram Moolenaarb765d632005-06-07 21:00:02 +00002306 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002307 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002308 STRCPY(sl.sl_lang, lang);
2309 sl.sl_slang = NULL;
2310 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002311
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002312#ifdef FEAT_AUTOCMD
2313 /* We may retry when no spell file is found for the language, an
2314 * autocommand may load it then. */
2315 for (round = 1; round <= 2; ++round)
2316#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002317 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002318 /*
2319 * Find the first spell file for "lang" in 'runtimepath' and load it.
2320 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002321 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002322 "spell/%s.%s.spl", lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002323 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002324
2325 if (r == FAIL && *sl.sl_lang != NUL)
2326 {
2327 /* Try loading the ASCII version. */
2328 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2329 "spell/%s.ascii.spl", lang);
2330 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
2331
2332#ifdef FEAT_AUTOCMD
2333 if (r == FAIL && *sl.sl_lang != NUL && round == 1
2334 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
2335 curbuf->b_fname, FALSE, curbuf))
2336 continue;
2337 break;
2338#endif
2339 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002340#ifdef FEAT_AUTOCMD
2341 break;
2342#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002343 }
2344
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002345 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002346 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002347 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2348 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002349 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002350 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002351 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002352 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002353 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002354 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002355 }
2356}
2357
2358/*
2359 * Return the encoding used for spell checking: Use 'encoding', except that we
2360 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2361 */
2362 static char_u *
2363spell_enc()
2364{
2365
2366#ifdef FEAT_MBYTE
2367 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2368 return p_enc;
2369#endif
2370 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002371}
2372
2373/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002374 * Get the name of the .spl file for the internal wordlist into
2375 * "fname[MAXPATHL]".
2376 */
2377 static void
2378int_wordlist_spl(fname)
2379 char_u *fname;
2380{
2381 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2382 int_wordlist, spell_enc());
2383}
2384
2385/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002386 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002387 * Caller must fill "sl_next".
2388 */
2389 static slang_T *
2390slang_alloc(lang)
2391 char_u *lang;
2392{
2393 slang_T *lp;
2394
Bram Moolenaar51485f02005-06-04 21:55:20 +00002395 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002396 if (lp != NULL)
2397 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002398 if (lang != NULL)
2399 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002400 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002401 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002402 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002403 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002404 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002405 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002406
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002407 return lp;
2408}
2409
2410/*
2411 * Free the contents of an slang_T and the structure itself.
2412 */
2413 static void
2414slang_free(lp)
2415 slang_T *lp;
2416{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002417 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002418 vim_free(lp->sl_fname);
2419 slang_clear(lp);
2420 vim_free(lp);
2421}
2422
2423/*
2424 * Clear an slang_T so that the file can be reloaded.
2425 */
2426 static void
2427slang_clear(lp)
2428 slang_T *lp;
2429{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002430 garray_T *gap;
2431 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002432 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002433 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002434 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002435
Bram Moolenaar51485f02005-06-04 21:55:20 +00002436 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002437 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002438 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002439 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002440 vim_free(lp->sl_pbyts);
2441 lp->sl_pbyts = NULL;
2442
Bram Moolenaar51485f02005-06-04 21:55:20 +00002443 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002444 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002445 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002446 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002447 vim_free(lp->sl_pidxs);
2448 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002449
Bram Moolenaar4770d092006-01-12 23:22:24 +00002450 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002451 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002452 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2453 while (gap->ga_len > 0)
2454 {
2455 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2456 vim_free(ftp->ft_from);
2457 vim_free(ftp->ft_to);
2458 }
2459 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002460 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002461
2462 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002463 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002464 {
2465 /* "ga_len" is set to 1 without adding an item for latin1 */
2466 if (gap->ga_data != NULL)
2467 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2468 for (i = 0; i < gap->ga_len; ++i)
2469 vim_free(((int **)gap->ga_data)[i]);
2470 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002471 else
2472 /* SAL items: free salitem_T items */
2473 while (gap->ga_len > 0)
2474 {
2475 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2476 vim_free(smp->sm_lead);
2477 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2478 vim_free(smp->sm_to);
2479#ifdef FEAT_MBYTE
2480 vim_free(smp->sm_lead_w);
2481 vim_free(smp->sm_oneof_w);
2482 vim_free(smp->sm_to_w);
2483#endif
2484 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002485 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002486
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002487 for (i = 0; i < lp->sl_prefixcnt; ++i)
2488 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002489 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002490 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002491 lp->sl_prefprog = NULL;
2492
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002493 vim_free(lp->sl_info);
2494 lp->sl_info = NULL;
2495
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002496 vim_free(lp->sl_midword);
2497 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002498
Bram Moolenaar5195e452005-08-19 20:32:47 +00002499 vim_free(lp->sl_compprog);
2500 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002501 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002502 lp->sl_compprog = NULL;
2503 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002504 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002505
2506 vim_free(lp->sl_syllable);
2507 lp->sl_syllable = NULL;
2508 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002509
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002510 ga_clear_strings(&lp->sl_comppat);
2511
Bram Moolenaar4770d092006-01-12 23:22:24 +00002512 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2513 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002514
Bram Moolenaar4770d092006-01-12 23:22:24 +00002515#ifdef FEAT_MBYTE
2516 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002517#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002518
Bram Moolenaar4770d092006-01-12 23:22:24 +00002519 /* Clear info from .sug file. */
2520 slang_clear_sug(lp);
2521
Bram Moolenaar5195e452005-08-19 20:32:47 +00002522 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002523 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002524 lp->sl_compsylmax = MAXWLEN;
2525 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002526}
2527
2528/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002529 * Clear the info from the .sug file in "lp".
2530 */
2531 static void
2532slang_clear_sug(lp)
2533 slang_T *lp;
2534{
2535 vim_free(lp->sl_sbyts);
2536 lp->sl_sbyts = NULL;
2537 vim_free(lp->sl_sidxs);
2538 lp->sl_sidxs = NULL;
2539 close_spellbuf(lp->sl_sugbuf);
2540 lp->sl_sugbuf = NULL;
2541 lp->sl_sugloaded = FALSE;
2542 lp->sl_sugtime = 0;
2543}
2544
2545/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002546 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002547 * Invoked through do_in_runtimepath().
2548 */
2549 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002550spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002551 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002552 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002553{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002554 spelload_T *slp = (spelload_T *)cookie;
2555 slang_T *slang;
2556
2557 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2558 if (slang != NULL)
2559 {
2560 /* When a previously loaded file has NOBREAK also use it for the
2561 * ".add" files. */
2562 if (slp->sl_nobreak && slang->sl_add)
2563 slang->sl_nobreak = TRUE;
2564 else if (slang->sl_nobreak)
2565 slp->sl_nobreak = TRUE;
2566
2567 slp->sl_slang = slang;
2568 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002569}
2570
2571/*
2572 * Load one spell file and store the info into a slang_T.
2573 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00002574 * This is invoked in three ways:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002575 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2576 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2577 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2578 * points to the existing slang_T.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002579 * - Just after writing a .spl file; it's read back to produce the .sug file.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002580 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
2581 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002582 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002583 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002584 static slang_T *
2585spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002586 char_u *fname;
2587 char_u *lang;
2588 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002589 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002590{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002591 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002592 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002593 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002594 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002595 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002596 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002597 char_u *save_sourcing_name = sourcing_name;
2598 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002599 slang_T *lp = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002600 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002601 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002602
Bram Moolenaarb765d632005-06-07 21:00:02 +00002603 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002604 if (fd == NULL)
2605 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002606 if (!silent)
2607 EMSG2(_(e_notopen), fname);
2608 else if (p_verbose > 2)
2609 {
2610 verbose_enter();
2611 smsg((char_u *)e_notopen, fname);
2612 verbose_leave();
2613 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002614 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002615 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002616 if (p_verbose > 2)
2617 {
2618 verbose_enter();
2619 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2620 verbose_leave();
2621 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002622
Bram Moolenaarb765d632005-06-07 21:00:02 +00002623 if (old_lp == NULL)
2624 {
2625 lp = slang_alloc(lang);
2626 if (lp == NULL)
2627 goto endFAIL;
2628
2629 /* Remember the file name, used to reload the file when it's updated. */
2630 lp->sl_fname = vim_strsave(fname);
2631 if (lp->sl_fname == NULL)
2632 goto endFAIL;
2633
2634 /* Check for .add.spl. */
2635 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2636 }
2637 else
2638 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002639
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002640 /* Set sourcing_name, so that error messages mention the file name. */
2641 sourcing_name = fname;
2642 sourcing_lnum = 0;
2643
Bram Moolenaar4770d092006-01-12 23:22:24 +00002644 /*
2645 * <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002646 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002647 for (i = 0; i < VIMSPELLMAGICL; ++i)
2648 buf[i] = getc(fd); /* <fileID> */
2649 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2650 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002651 EMSG(_("E757: This does not look like a spell file"));
2652 goto endFAIL;
2653 }
2654 c = getc(fd); /* <versionnr> */
2655 if (c < VIMSPELLVERSION)
2656 {
2657 EMSG(_("E771: Old spell file, needs to be updated"));
2658 goto endFAIL;
2659 }
2660 else if (c > VIMSPELLVERSION)
2661 {
2662 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002663 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002664 }
2665
Bram Moolenaar5195e452005-08-19 20:32:47 +00002666
2667 /*
2668 * <SECTIONS>: <section> ... <sectionend>
2669 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2670 */
2671 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002672 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002673 n = getc(fd); /* <sectionID> or <sectionend> */
2674 if (n == SN_END)
2675 break;
2676 c = getc(fd); /* <sectionflags> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002677 len = get4c(fd); /* <sectionlen> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002678 if (len < 0)
2679 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002680
Bram Moolenaar5195e452005-08-19 20:32:47 +00002681 res = 0;
2682 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002683 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002684 case SN_INFO:
2685 lp->sl_info = read_string(fd, len); /* <infotext> */
2686 if (lp->sl_info == NULL)
2687 goto endFAIL;
2688 break;
2689
Bram Moolenaar5195e452005-08-19 20:32:47 +00002690 case SN_REGION:
2691 res = read_region_section(fd, lp, len);
2692 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002693
Bram Moolenaar5195e452005-08-19 20:32:47 +00002694 case SN_CHARFLAGS:
2695 res = read_charflags_section(fd);
2696 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002697
Bram Moolenaar5195e452005-08-19 20:32:47 +00002698 case SN_MIDWORD:
2699 lp->sl_midword = read_string(fd, len); /* <midword> */
2700 if (lp->sl_midword == NULL)
2701 goto endFAIL;
2702 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002703
Bram Moolenaar5195e452005-08-19 20:32:47 +00002704 case SN_PREFCOND:
2705 res = read_prefcond_section(fd, lp);
2706 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002707
Bram Moolenaar5195e452005-08-19 20:32:47 +00002708 case SN_REP:
Bram Moolenaar4770d092006-01-12 23:22:24 +00002709 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
2710 break;
2711
2712 case SN_REPSAL:
2713 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002714 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002715
Bram Moolenaar5195e452005-08-19 20:32:47 +00002716 case SN_SAL:
2717 res = read_sal_section(fd, lp);
2718 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002719
Bram Moolenaar5195e452005-08-19 20:32:47 +00002720 case SN_SOFO:
2721 res = read_sofo_section(fd, lp);
2722 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002723
Bram Moolenaar5195e452005-08-19 20:32:47 +00002724 case SN_MAP:
2725 p = read_string(fd, len); /* <mapstr> */
2726 if (p == NULL)
2727 goto endFAIL;
2728 set_map_str(lp, p);
2729 vim_free(p);
2730 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002731
Bram Moolenaar4770d092006-01-12 23:22:24 +00002732 case SN_WORDS:
2733 res = read_words_section(fd, lp, len);
2734 break;
2735
2736 case SN_SUGFILE:
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002737 lp->sl_sugtime = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002738 break;
2739
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002740 case SN_NOSPLITSUGS:
2741 lp->sl_nosplitsugs = TRUE; /* <timestamp> */
2742 break;
2743
Bram Moolenaar5195e452005-08-19 20:32:47 +00002744 case SN_COMPOUND:
2745 res = read_compound(fd, lp, len);
2746 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002747
Bram Moolenaar78622822005-08-23 21:00:13 +00002748 case SN_NOBREAK:
2749 lp->sl_nobreak = TRUE;
2750 break;
2751
Bram Moolenaar5195e452005-08-19 20:32:47 +00002752 case SN_SYLLABLE:
2753 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2754 if (lp->sl_syllable == NULL)
2755 goto endFAIL;
2756 if (init_syl_tab(lp) == FAIL)
2757 goto endFAIL;
2758 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002759
Bram Moolenaar5195e452005-08-19 20:32:47 +00002760 default:
2761 /* Unsupported section. When it's required give an error
2762 * message. When it's not required skip the contents. */
2763 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002764 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002765 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002766 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002767 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002768 while (--len >= 0)
2769 if (getc(fd) < 0)
2770 goto truncerr;
2771 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002772 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002773someerror:
Bram Moolenaar5195e452005-08-19 20:32:47 +00002774 if (res == SP_FORMERROR)
2775 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002776 EMSG(_(e_format));
2777 goto endFAIL;
2778 }
2779 if (res == SP_TRUNCERROR)
2780 {
2781truncerr:
2782 EMSG(_(e_spell_trunc));
2783 goto endFAIL;
2784 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002785 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002786 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002787 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002788
Bram Moolenaar4770d092006-01-12 23:22:24 +00002789 /* <LWORDTREE> */
2790 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
2791 if (res != 0)
2792 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002793
Bram Moolenaar4770d092006-01-12 23:22:24 +00002794 /* <KWORDTREE> */
2795 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
2796 if (res != 0)
2797 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002798
Bram Moolenaar4770d092006-01-12 23:22:24 +00002799 /* <PREFIXTREE> */
2800 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
2801 lp->sl_prefixcnt);
2802 if (res != 0)
2803 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002804
Bram Moolenaarb765d632005-06-07 21:00:02 +00002805 /* For a new file link it in the list of spell files. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002806 if (old_lp == NULL && lang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002807 {
2808 lp->sl_next = first_lang;
2809 first_lang = lp;
2810 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002811
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002812 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002813
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002814endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002815 if (lang != NULL)
2816 /* truncating the name signals the error to spell_load_lang() */
2817 *lang = NUL;
2818 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002819 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002820 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002821
2822endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002823 if (fd != NULL)
2824 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002825 sourcing_name = save_sourcing_name;
2826 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002827
2828 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002829}
2830
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002831/*
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002832 * Read 2 bytes from "fd" and turn them into an int, MSB first.
2833 */
2834 static int
2835get2c(fd)
2836 FILE *fd;
2837{
2838 long n;
2839
2840 n = getc(fd);
2841 n = (n << 8) + getc(fd);
2842 return n;
2843}
2844
2845/*
2846 * Read 3 bytes from "fd" and turn them into an int, MSB first.
2847 */
2848 static int
2849get3c(fd)
2850 FILE *fd;
2851{
2852 long n;
2853
2854 n = getc(fd);
2855 n = (n << 8) + getc(fd);
2856 n = (n << 8) + getc(fd);
2857 return n;
2858}
2859
2860/*
2861 * Read 4 bytes from "fd" and turn them into an int, MSB first.
2862 */
2863 static int
2864get4c(fd)
2865 FILE *fd;
2866{
2867 long n;
2868
2869 n = getc(fd);
2870 n = (n << 8) + getc(fd);
2871 n = (n << 8) + getc(fd);
2872 n = (n << 8) + getc(fd);
2873 return n;
2874}
2875
2876/*
2877 * Read 8 bytes from "fd" and turn them into a time_t, MSB first.
2878 */
2879 static time_t
2880get8c(fd)
2881 FILE *fd;
2882{
2883 time_t n = 0;
2884 int i;
2885
2886 for (i = 0; i < 8; ++i)
2887 n = (n << 8) + getc(fd);
2888 return n;
2889}
2890
2891/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002892 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002893 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002894 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002895 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2896 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002897 */
2898 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002899read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002900 FILE *fd;
2901 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002902 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002903{
2904 int cnt = 0;
2905 int i;
2906 char_u *str;
2907
2908 /* read the length bytes, MSB first */
2909 for (i = 0; i < cnt_bytes; ++i)
2910 cnt = (cnt << 8) + getc(fd);
2911 if (cnt < 0)
2912 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002913 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002914 return NULL;
2915 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002916 *cntp = cnt;
2917 if (cnt == 0)
2918 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002919
Bram Moolenaar5195e452005-08-19 20:32:47 +00002920 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002921 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00002922 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002923 return str;
2924}
2925
Bram Moolenaar7887d882005-07-01 22:33:52 +00002926/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00002927 * Read a string of length "cnt" from "fd" into allocated memory.
2928 * Returns NULL when out of memory.
2929 */
2930 static char_u *
2931read_string(fd, cnt)
2932 FILE *fd;
2933 int cnt;
2934{
2935 char_u *str;
2936 int i;
2937
2938 /* allocate memory */
2939 str = alloc((unsigned)cnt + 1);
2940 if (str != NULL)
2941 {
2942 /* Read the string. Doesn't check for truncated file. */
2943 for (i = 0; i < cnt; ++i)
2944 str[i] = getc(fd);
2945 str[i] = NUL;
2946 }
2947 return str;
2948}
2949
2950/*
2951 * Read SN_REGION: <regionname> ...
2952 * Return SP_*ERROR flags.
2953 */
2954 static int
2955read_region_section(fd, lp, len)
2956 FILE *fd;
2957 slang_T *lp;
2958 int len;
2959{
2960 int i;
2961
2962 if (len > 16)
2963 return SP_FORMERROR;
2964 for (i = 0; i < len; ++i)
2965 lp->sl_regions[i] = getc(fd); /* <regionname> */
2966 lp->sl_regions[len] = NUL;
2967 return 0;
2968}
2969
2970/*
2971 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
2972 * <folcharslen> <folchars>
2973 * Return SP_*ERROR flags.
2974 */
2975 static int
2976read_charflags_section(fd)
2977 FILE *fd;
2978{
2979 char_u *flags;
2980 char_u *fol;
2981 int flagslen, follen;
2982
2983 /* <charflagslen> <charflags> */
2984 flags = read_cnt_string(fd, 1, &flagslen);
2985 if (flagslen < 0)
2986 return flagslen;
2987
2988 /* <folcharslen> <folchars> */
2989 fol = read_cnt_string(fd, 2, &follen);
2990 if (follen < 0)
2991 {
2992 vim_free(flags);
2993 return follen;
2994 }
2995
2996 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
2997 if (flags != NULL && fol != NULL)
2998 set_spell_charflags(flags, flagslen, fol);
2999
3000 vim_free(flags);
3001 vim_free(fol);
3002
3003 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
3004 if ((flags == NULL) != (fol == NULL))
3005 return SP_FORMERROR;
3006 return 0;
3007}
3008
3009/*
3010 * Read SN_PREFCOND section.
3011 * Return SP_*ERROR flags.
3012 */
3013 static int
3014read_prefcond_section(fd, lp)
3015 FILE *fd;
3016 slang_T *lp;
3017{
3018 int cnt;
3019 int i;
3020 int n;
3021 char_u *p;
3022 char_u buf[MAXWLEN + 1];
3023
3024 /* <prefcondcnt> <prefcond> ... */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003025 cnt = get2c(fd); /* <prefcondcnt> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003026 if (cnt <= 0)
3027 return SP_FORMERROR;
3028
3029 lp->sl_prefprog = (regprog_T **)alloc_clear(
3030 (unsigned)sizeof(regprog_T *) * cnt);
3031 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003032 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003033 lp->sl_prefixcnt = cnt;
3034
3035 for (i = 0; i < cnt; ++i)
3036 {
3037 /* <prefcond> : <condlen> <condstr> */
3038 n = getc(fd); /* <condlen> */
3039 if (n < 0 || n >= MAXWLEN)
3040 return SP_FORMERROR;
3041
3042 /* When <condlen> is zero we have an empty condition. Otherwise
3043 * compile the regexp program used to check for the condition. */
3044 if (n > 0)
3045 {
3046 buf[0] = '^'; /* always match at one position only */
3047 p = buf + 1;
3048 while (n-- > 0)
3049 *p++ = getc(fd); /* <condstr> */
3050 *p = NUL;
3051 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
3052 }
3053 }
3054 return 0;
3055}
3056
3057/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003058 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
Bram Moolenaar5195e452005-08-19 20:32:47 +00003059 * Return SP_*ERROR flags.
3060 */
3061 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00003062read_rep_section(fd, gap, first)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003063 FILE *fd;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003064 garray_T *gap;
3065 short *first;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003066{
3067 int cnt;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003068 fromto_T *ftp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003069 int i;
3070
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003071 cnt = get2c(fd); /* <repcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003072 if (cnt < 0)
3073 return SP_TRUNCERROR;
3074
Bram Moolenaar5195e452005-08-19 20:32:47 +00003075 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003076 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003077
3078 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3079 for (; gap->ga_len < cnt; ++gap->ga_len)
3080 {
3081 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
3082 ftp->ft_from = read_cnt_string(fd, 1, &i);
3083 if (i < 0)
3084 return i;
3085 if (i == 0)
3086 return SP_FORMERROR;
3087 ftp->ft_to = read_cnt_string(fd, 1, &i);
3088 if (i <= 0)
3089 {
3090 vim_free(ftp->ft_from);
3091 if (i < 0)
3092 return i;
3093 return SP_FORMERROR;
3094 }
3095 }
3096
3097 /* Fill the first-index table. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003098 for (i = 0; i < 256; ++i)
3099 first[i] = -1;
3100 for (i = 0; i < gap->ga_len; ++i)
3101 {
3102 ftp = &((fromto_T *)gap->ga_data)[i];
3103 if (first[*ftp->ft_from] == -1)
3104 first[*ftp->ft_from] = i;
3105 }
3106 return 0;
3107}
3108
3109/*
3110 * Read SN_SAL section: <salflags> <salcount> <sal> ...
3111 * Return SP_*ERROR flags.
3112 */
3113 static int
3114read_sal_section(fd, slang)
3115 FILE *fd;
3116 slang_T *slang;
3117{
3118 int i;
3119 int cnt;
3120 garray_T *gap;
3121 salitem_T *smp;
3122 int ccnt;
3123 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003124 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003125
3126 slang->sl_sofo = FALSE;
3127
3128 i = getc(fd); /* <salflags> */
3129 if (i & SAL_F0LLOWUP)
3130 slang->sl_followup = TRUE;
3131 if (i & SAL_COLLAPSE)
3132 slang->sl_collapse = TRUE;
3133 if (i & SAL_REM_ACCENTS)
3134 slang->sl_rem_accents = TRUE;
3135
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003136 cnt = get2c(fd); /* <salcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003137 if (cnt < 0)
3138 return SP_TRUNCERROR;
3139
3140 gap = &slang->sl_sal;
3141 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003142 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003143 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003144
3145 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3146 for (; gap->ga_len < cnt; ++gap->ga_len)
3147 {
3148 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3149 ccnt = getc(fd); /* <salfromlen> */
3150 if (ccnt < 0)
3151 return SP_TRUNCERROR;
3152 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003153 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003154 smp->sm_lead = p;
3155
3156 /* Read up to the first special char into sm_lead. */
3157 for (i = 0; i < ccnt; ++i)
3158 {
3159 c = getc(fd); /* <salfrom> */
3160 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
3161 break;
3162 *p++ = c;
3163 }
3164 smp->sm_leadlen = p - smp->sm_lead;
3165 *p++ = NUL;
3166
3167 /* Put (abc) chars in sm_oneof, if any. */
3168 if (c == '(')
3169 {
3170 smp->sm_oneof = p;
3171 for (++i; i < ccnt; ++i)
3172 {
3173 c = getc(fd); /* <salfrom> */
3174 if (c == ')')
3175 break;
3176 *p++ = c;
3177 }
3178 *p++ = NUL;
3179 if (++i < ccnt)
3180 c = getc(fd);
3181 }
3182 else
3183 smp->sm_oneof = NULL;
3184
3185 /* Any following chars go in sm_rules. */
3186 smp->sm_rules = p;
3187 if (i < ccnt)
3188 /* store the char we got while checking for end of sm_lead */
3189 *p++ = c;
3190 for (++i; i < ccnt; ++i)
3191 *p++ = getc(fd); /* <salfrom> */
3192 *p++ = NUL;
3193
3194 /* <saltolen> <salto> */
3195 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
3196 if (ccnt < 0)
3197 {
3198 vim_free(smp->sm_lead);
3199 return ccnt;
3200 }
3201
3202#ifdef FEAT_MBYTE
3203 if (has_mbyte)
3204 {
3205 /* convert the multi-byte strings to wide char strings */
3206 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3207 smp->sm_leadlen = mb_charlen(smp->sm_lead);
3208 if (smp->sm_oneof == NULL)
3209 smp->sm_oneof_w = NULL;
3210 else
3211 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
3212 if (smp->sm_to == NULL)
3213 smp->sm_to_w = NULL;
3214 else
3215 smp->sm_to_w = mb_str2wide(smp->sm_to);
3216 if (smp->sm_lead_w == NULL
3217 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
3218 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
3219 {
3220 vim_free(smp->sm_lead);
3221 vim_free(smp->sm_to);
3222 vim_free(smp->sm_lead_w);
3223 vim_free(smp->sm_oneof_w);
3224 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003225 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003226 }
3227 }
3228#endif
3229 }
3230
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003231 if (gap->ga_len > 0)
3232 {
3233 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
3234 * that we need to check the index every time. */
3235 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3236 if ((p = alloc(1)) == NULL)
3237 return SP_OTHERERROR;
3238 p[0] = NUL;
3239 smp->sm_lead = p;
3240 smp->sm_leadlen = 0;
3241 smp->sm_oneof = NULL;
3242 smp->sm_rules = p;
3243 smp->sm_to = NULL;
3244#ifdef FEAT_MBYTE
3245 if (has_mbyte)
3246 {
3247 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3248 smp->sm_leadlen = 0;
3249 smp->sm_oneof_w = NULL;
3250 smp->sm_to_w = NULL;
3251 }
3252#endif
3253 ++gap->ga_len;
3254 }
3255
Bram Moolenaar5195e452005-08-19 20:32:47 +00003256 /* Fill the first-index table. */
3257 set_sal_first(slang);
3258
3259 return 0;
3260}
3261
3262/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003263 * Read SN_WORDS: <word> ...
3264 * Return SP_*ERROR flags.
3265 */
3266 static int
3267read_words_section(fd, lp, len)
3268 FILE *fd;
3269 slang_T *lp;
3270 int len;
3271{
3272 int done = 0;
3273 int i;
3274 char_u word[MAXWLEN];
3275
3276 while (done < len)
3277 {
3278 /* Read one word at a time. */
3279 for (i = 0; ; ++i)
3280 {
3281 word[i] = getc(fd);
3282 if (word[i] == NUL)
3283 break;
3284 if (i == MAXWLEN - 1)
3285 return SP_FORMERROR;
3286 }
3287
3288 /* Init the count to 10. */
3289 count_common_word(lp, word, -1, 10);
3290 done += i + 1;
3291 }
3292 return 0;
3293}
3294
3295/*
3296 * Add a word to the hashtable of common words.
3297 * If it's already there then the counter is increased.
3298 */
3299 static void
3300count_common_word(lp, word, len, count)
3301 slang_T *lp;
3302 char_u *word;
3303 int len; /* word length, -1 for upto NUL */
3304 int count; /* 1 to count once, 10 to init */
3305{
3306 hash_T hash;
3307 hashitem_T *hi;
3308 wordcount_T *wc;
3309 char_u buf[MAXWLEN];
3310 char_u *p;
3311
3312 if (len == -1)
3313 p = word;
3314 else
3315 {
3316 vim_strncpy(buf, word, len);
3317 p = buf;
3318 }
3319
3320 hash = hash_hash(p);
3321 hi = hash_lookup(&lp->sl_wordcount, p, hash);
3322 if (HASHITEM_EMPTY(hi))
3323 {
3324 wc = (wordcount_T *)alloc(sizeof(wordcount_T) + STRLEN(p));
3325 if (wc == NULL)
3326 return;
3327 STRCPY(wc->wc_word, p);
3328 wc->wc_count = count;
3329 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
3330 }
3331 else
3332 {
3333 wc = HI2WC(hi);
3334 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
3335 wc->wc_count = MAXWORDCOUNT;
3336 }
3337}
3338
3339/*
3340 * Adjust the score of common words.
3341 */
3342 static int
3343score_wordcount_adj(slang, score, word, split)
3344 slang_T *slang;
3345 int score;
3346 char_u *word;
3347 int split; /* word was split, less bonus */
3348{
3349 hashitem_T *hi;
3350 wordcount_T *wc;
3351 int bonus;
3352 int newscore;
3353
3354 hi = hash_find(&slang->sl_wordcount, word);
3355 if (!HASHITEM_EMPTY(hi))
3356 {
3357 wc = HI2WC(hi);
3358 if (wc->wc_count < SCORE_THRES2)
3359 bonus = SCORE_COMMON1;
3360 else if (wc->wc_count < SCORE_THRES3)
3361 bonus = SCORE_COMMON2;
3362 else
3363 bonus = SCORE_COMMON3;
3364 if (split)
3365 newscore = score - bonus / 2;
3366 else
3367 newscore = score - bonus;
3368 if (newscore < 0)
3369 return 0;
3370 return newscore;
3371 }
3372 return score;
3373}
3374
3375/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003376 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
3377 * Return SP_*ERROR flags.
3378 */
3379 static int
3380read_sofo_section(fd, slang)
3381 FILE *fd;
3382 slang_T *slang;
3383{
3384 int cnt;
3385 char_u *from, *to;
3386 int res;
3387
3388 slang->sl_sofo = TRUE;
3389
3390 /* <sofofromlen> <sofofrom> */
3391 from = read_cnt_string(fd, 2, &cnt);
3392 if (cnt < 0)
3393 return cnt;
3394
3395 /* <sofotolen> <sofoto> */
3396 to = read_cnt_string(fd, 2, &cnt);
3397 if (cnt < 0)
3398 {
3399 vim_free(from);
3400 return cnt;
3401 }
3402
3403 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
3404 if (from != NULL && to != NULL)
3405 res = set_sofo(slang, from, to);
3406 else if (from != NULL || to != NULL)
3407 res = SP_FORMERROR; /* only one of two strings is an error */
3408 else
3409 res = 0;
3410
3411 vim_free(from);
3412 vim_free(to);
3413 return res;
3414}
3415
3416/*
3417 * Read the compound section from the .spl file:
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003418 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +00003419 * Returns SP_*ERROR flags.
3420 */
3421 static int
3422read_compound(fd, slang, len)
3423 FILE *fd;
3424 slang_T *slang;
3425 int len;
3426{
3427 int todo = len;
3428 int c;
3429 int atstart;
3430 char_u *pat;
3431 char_u *pp;
3432 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003433 char_u *ap;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003434 int cnt;
3435 garray_T *gap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003436
3437 if (todo < 2)
3438 return SP_FORMERROR; /* need at least two bytes */
3439
3440 --todo;
3441 c = getc(fd); /* <compmax> */
3442 if (c < 2)
3443 c = MAXWLEN;
3444 slang->sl_compmax = c;
3445
3446 --todo;
3447 c = getc(fd); /* <compminlen> */
3448 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003449 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003450 slang->sl_compminlen = c;
3451
3452 --todo;
3453 c = getc(fd); /* <compsylmax> */
3454 if (c < 1)
3455 c = MAXWLEN;
3456 slang->sl_compsylmax = c;
3457
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003458 c = getc(fd); /* <compoptions> */
3459 if (c != 0)
3460 ungetc(c, fd); /* be backwards compatible with Vim 7.0b */
3461 else
3462 {
3463 --todo;
3464 c = getc(fd); /* only use the lower byte for now */
3465 --todo;
3466 slang->sl_compoptions = c;
3467
3468 gap = &slang->sl_comppat;
3469 c = get2c(fd); /* <comppatcount> */
3470 todo -= 2;
3471 ga_init2(gap, sizeof(char_u *), c);
3472 if (ga_grow(gap, c) == OK)
3473 while (--c >= 0)
3474 {
3475 ((char_u **)(gap->ga_data))[gap->ga_len++] =
3476 read_cnt_string(fd, 1, &cnt);
3477 /* <comppatlen> <comppattext> */
3478 if (cnt < 0)
3479 return cnt;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003480 todo -= cnt + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003481 }
3482 }
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003483 if (todo < 0)
3484 return SP_FORMERROR;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003485
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003486 /* Turn the COMPOUNDRULE items into a regexp pattern:
Bram Moolenaar5195e452005-08-19 20:32:47 +00003487 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003488 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3489 * Conversion to utf-8 may double the size. */
3490 c = todo * 2 + 7;
3491#ifdef FEAT_MBYTE
3492 if (enc_utf8)
3493 c += todo * 2;
3494#endif
3495 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003496 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003497 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003498
Bram Moolenaard12a1322005-08-21 22:08:24 +00003499 /* We also need a list of all flags that can appear at the start and one
3500 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003501 cp = alloc(todo + 1);
3502 if (cp == NULL)
3503 {
3504 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003505 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003506 }
3507 slang->sl_compstartflags = cp;
3508 *cp = NUL;
3509
Bram Moolenaard12a1322005-08-21 22:08:24 +00003510 ap = alloc(todo + 1);
3511 if (ap == NULL)
3512 {
3513 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003514 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003515 }
3516 slang->sl_compallflags = ap;
3517 *ap = NUL;
3518
Bram Moolenaar5195e452005-08-19 20:32:47 +00003519 pp = pat;
3520 *pp++ = '^';
3521 *pp++ = '\\';
3522 *pp++ = '(';
3523
3524 atstart = 1;
3525 while (todo-- > 0)
3526 {
3527 c = getc(fd); /* <compflags> */
Bram Moolenaard12a1322005-08-21 22:08:24 +00003528
3529 /* Add all flags to "sl_compallflags". */
3530 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003531 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003532 {
3533 *ap++ = c;
3534 *ap = NUL;
3535 }
3536
Bram Moolenaar5195e452005-08-19 20:32:47 +00003537 if (atstart != 0)
3538 {
3539 /* At start of item: copy flags to "sl_compstartflags". For a
3540 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3541 if (c == '[')
3542 atstart = 2;
3543 else if (c == ']')
3544 atstart = 0;
3545 else
3546 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003547 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003548 {
3549 *cp++ = c;
3550 *cp = NUL;
3551 }
3552 if (atstart == 1)
3553 atstart = 0;
3554 }
3555 }
3556 if (c == '/') /* slash separates two items */
3557 {
3558 *pp++ = '\\';
3559 *pp++ = '|';
3560 atstart = 1;
3561 }
3562 else /* normal char, "[abc]" and '*' are copied as-is */
3563 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003564 if (c == '+' || c == '~')
Bram Moolenaar5195e452005-08-19 20:32:47 +00003565 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003566#ifdef FEAT_MBYTE
3567 if (enc_utf8)
3568 pp += mb_char2bytes(c, pp);
3569 else
3570#endif
3571 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003572 }
3573 }
3574
3575 *pp++ = '\\';
3576 *pp++ = ')';
3577 *pp++ = '$';
3578 *pp = NUL;
3579
3580 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3581 vim_free(pat);
3582 if (slang->sl_compprog == NULL)
3583 return SP_FORMERROR;
3584
3585 return 0;
3586}
3587
Bram Moolenaar6de68532005-08-24 22:08:48 +00003588/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003589 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003590 * Like strchr() but independent of locale.
3591 */
3592 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003593byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003594 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003595 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003596{
3597 char_u *p;
3598
3599 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003600 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003601 return TRUE;
3602 return FALSE;
3603}
3604
Bram Moolenaar5195e452005-08-19 20:32:47 +00003605#define SY_MAXLEN 30
3606typedef struct syl_item_S
3607{
3608 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3609 int sy_len;
3610} syl_item_T;
3611
3612/*
3613 * Truncate "slang->sl_syllable" at the first slash and put the following items
3614 * in "slang->sl_syl_items".
3615 */
3616 static int
3617init_syl_tab(slang)
3618 slang_T *slang;
3619{
3620 char_u *p;
3621 char_u *s;
3622 int l;
3623 syl_item_T *syl;
3624
3625 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3626 p = vim_strchr(slang->sl_syllable, '/');
3627 while (p != NULL)
3628 {
3629 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003630 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003631 break;
3632 s = p;
3633 p = vim_strchr(p, '/');
3634 if (p == NULL)
3635 l = STRLEN(s);
3636 else
3637 l = p - s;
3638 if (l >= SY_MAXLEN)
3639 return SP_FORMERROR;
3640 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003641 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003642 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3643 + slang->sl_syl_items.ga_len++;
3644 vim_strncpy(syl->sy_chars, s, l);
3645 syl->sy_len = l;
3646 }
3647 return OK;
3648}
3649
3650/*
3651 * Count the number of syllables in "word".
3652 * When "word" contains spaces the syllables after the last space are counted.
3653 * Returns zero if syllables are not defines.
3654 */
3655 static int
3656count_syllables(slang, word)
3657 slang_T *slang;
3658 char_u *word;
3659{
3660 int cnt = 0;
3661 int skip = FALSE;
3662 char_u *p;
3663 int len;
3664 int i;
3665 syl_item_T *syl;
3666 int c;
3667
3668 if (slang->sl_syllable == NULL)
3669 return 0;
3670
3671 for (p = word; *p != NUL; p += len)
3672 {
3673 /* When running into a space reset counter. */
3674 if (*p == ' ')
3675 {
3676 len = 1;
3677 cnt = 0;
3678 continue;
3679 }
3680
3681 /* Find longest match of syllable items. */
3682 len = 0;
3683 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3684 {
3685 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3686 if (syl->sy_len > len
3687 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3688 len = syl->sy_len;
3689 }
3690 if (len != 0) /* found a match, count syllable */
3691 {
3692 ++cnt;
3693 skip = FALSE;
3694 }
3695 else
3696 {
3697 /* No recognized syllable item, at least a syllable char then? */
3698#ifdef FEAT_MBYTE
3699 c = mb_ptr2char(p);
3700 len = (*mb_ptr2len)(p);
3701#else
3702 c = *p;
3703 len = 1;
3704#endif
3705 if (vim_strchr(slang->sl_syllable, c) == NULL)
3706 skip = FALSE; /* No, search for next syllable */
3707 else if (!skip)
3708 {
3709 ++cnt; /* Yes, count it */
3710 skip = TRUE; /* don't count following syllable chars */
3711 }
3712 }
3713 }
3714 return cnt;
3715}
3716
3717/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003718 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003719 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003720 */
3721 static int
3722set_sofo(lp, from, to)
3723 slang_T *lp;
3724 char_u *from;
3725 char_u *to;
3726{
3727 int i;
3728
3729#ifdef FEAT_MBYTE
3730 garray_T *gap;
3731 char_u *s;
3732 char_u *p;
3733 int c;
3734 int *inp;
3735
3736 if (has_mbyte)
3737 {
3738 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3739 * characters. The index is the low byte of the character.
3740 * The list contains from-to pairs with a terminating NUL.
3741 * sl_sal_first[] is used for latin1 "from" characters. */
3742 gap = &lp->sl_sal;
3743 ga_init2(gap, sizeof(int *), 1);
3744 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003745 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003746 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3747 gap->ga_len = 256;
3748
3749 /* First count the number of items for each list. Temporarily use
3750 * sl_sal_first[] for this. */
3751 for (p = from, s = to; *p != NUL && *s != NUL; )
3752 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003753 c = mb_cptr2char_adv(&p);
3754 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003755 if (c >= 256)
3756 ++lp->sl_sal_first[c & 0xff];
3757 }
3758 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003759 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003760
3761 /* Allocate the lists. */
3762 for (i = 0; i < 256; ++i)
3763 if (lp->sl_sal_first[i] > 0)
3764 {
3765 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3766 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003767 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003768 ((int **)gap->ga_data)[i] = (int *)p;
3769 *(int *)p = 0;
3770 }
3771
3772 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3773 * list. */
3774 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3775 for (p = from, s = to; *p != NUL && *s != NUL; )
3776 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003777 c = mb_cptr2char_adv(&p);
3778 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003779 if (c >= 256)
3780 {
3781 /* Append the from-to chars at the end of the list with
3782 * the low byte. */
3783 inp = ((int **)gap->ga_data)[c & 0xff];
3784 while (*inp != 0)
3785 ++inp;
3786 *inp++ = c; /* from char */
3787 *inp++ = i; /* to char */
3788 *inp++ = NUL; /* NUL at the end */
3789 }
3790 else
3791 /* mapping byte to char is done in sl_sal_first[] */
3792 lp->sl_sal_first[c] = i;
3793 }
3794 }
3795 else
3796#endif
3797 {
3798 /* mapping bytes to bytes is done in sl_sal_first[] */
3799 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003800 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003801
3802 for (i = 0; to[i] != NUL; ++i)
3803 lp->sl_sal_first[from[i]] = to[i];
3804 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3805 }
3806
Bram Moolenaar5195e452005-08-19 20:32:47 +00003807 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003808}
3809
3810/*
3811 * Fill the first-index table for "lp".
3812 */
3813 static void
3814set_sal_first(lp)
3815 slang_T *lp;
3816{
3817 salfirst_T *sfirst;
3818 int i;
3819 salitem_T *smp;
3820 int c;
3821 garray_T *gap = &lp->sl_sal;
3822
3823 sfirst = lp->sl_sal_first;
3824 for (i = 0; i < 256; ++i)
3825 sfirst[i] = -1;
3826 smp = (salitem_T *)gap->ga_data;
3827 for (i = 0; i < gap->ga_len; ++i)
3828 {
3829#ifdef FEAT_MBYTE
3830 if (has_mbyte)
3831 /* Use the lowest byte of the first character. For latin1 it's
3832 * the character, for other encodings it should differ for most
3833 * characters. */
3834 c = *smp[i].sm_lead_w & 0xff;
3835 else
3836#endif
3837 c = *smp[i].sm_lead;
3838 if (sfirst[c] == -1)
3839 {
3840 sfirst[c] = i;
3841#ifdef FEAT_MBYTE
3842 if (has_mbyte)
3843 {
3844 int n;
3845
3846 /* Make sure all entries with this byte are following each
3847 * other. Move the ones that are in the wrong position. Do
3848 * keep the same ordering! */
3849 while (i + 1 < gap->ga_len
3850 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3851 /* Skip over entry with same index byte. */
3852 ++i;
3853
3854 for (n = 1; i + n < gap->ga_len; ++n)
3855 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3856 {
3857 salitem_T tsal;
3858
3859 /* Move entry with same index byte after the entries
3860 * we already found. */
3861 ++i;
3862 --n;
3863 tsal = smp[i + n];
3864 mch_memmove(smp + i + 1, smp + i,
3865 sizeof(salitem_T) * n);
3866 smp[i] = tsal;
3867 }
3868 }
3869#endif
3870 }
3871 }
3872}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003873
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003874#ifdef FEAT_MBYTE
3875/*
3876 * Turn a multi-byte string into a wide character string.
3877 * Return it in allocated memory (NULL for out-of-memory)
3878 */
3879 static int *
3880mb_str2wide(s)
3881 char_u *s;
3882{
3883 int *res;
3884 char_u *p;
3885 int i = 0;
3886
3887 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3888 if (res != NULL)
3889 {
3890 for (p = s; *p != NUL; )
3891 res[i++] = mb_ptr2char_adv(&p);
3892 res[i] = NUL;
3893 }
3894 return res;
3895}
3896#endif
3897
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003898/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003899 * Read a tree from the .spl or .sug file.
3900 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
3901 * This is skipped when the tree has zero length.
3902 * Returns zero when OK, SP_ value for an error.
3903 */
3904 static int
3905spell_read_tree(fd, bytsp, idxsp, prefixtree, prefixcnt)
3906 FILE *fd;
3907 char_u **bytsp;
3908 idx_T **idxsp;
3909 int prefixtree; /* TRUE for the prefix tree */
3910 int prefixcnt; /* when "prefixtree" is TRUE: prefix count */
3911{
3912 int len;
3913 int idx;
3914 char_u *bp;
3915 idx_T *ip;
3916
3917 /* The tree size was computed when writing the file, so that we can
3918 * allocate it as one long block. <nodecount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003919 len = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00003920 if (len < 0)
3921 return SP_TRUNCERROR;
3922 if (len > 0)
3923 {
3924 /* Allocate the byte array. */
3925 bp = lalloc((long_u)len, TRUE);
3926 if (bp == NULL)
3927 return SP_OTHERERROR;
3928 *bytsp = bp;
3929
3930 /* Allocate the index array. */
3931 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
3932 if (ip == NULL)
3933 return SP_OTHERERROR;
3934 *idxsp = ip;
3935
3936 /* Recursively read the tree and store it in the array. */
3937 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
3938 if (idx < 0)
3939 return idx;
3940 }
3941 return 0;
3942}
3943
3944/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00003945 * Read one row of siblings from the spell file and store it in the byte array
3946 * "byts" and index array "idxs". Recursively read the children.
3947 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003948 * NOTE: The code here must match put_node()!
Bram Moolenaar51485f02005-06-04 21:55:20 +00003949 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00003950 * Returns the index (>= 0) following the siblings.
3951 * Returns SP_TRUNCERROR if the file is shorter than expected.
3952 * Returns SP_FORMERROR if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003953 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003954 static idx_T
Bram Moolenaar4770d092006-01-12 23:22:24 +00003955read_tree_node(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003956 FILE *fd;
3957 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003958 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003959 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003960 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003961 int prefixtree; /* TRUE for reading PREFIXTREE */
3962 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003963{
Bram Moolenaar51485f02005-06-04 21:55:20 +00003964 int len;
3965 int i;
3966 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00003967 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003968 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003969 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003970#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003971
Bram Moolenaar51485f02005-06-04 21:55:20 +00003972 len = getc(fd); /* <siblingcount> */
3973 if (len <= 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003974 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003975
3976 if (startidx + len >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003977 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003978 byts[idx++] = len;
3979
3980 /* Read the byte values, flag/region bytes and shared indexes. */
3981 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003982 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00003983 c = getc(fd); /* <byte> */
3984 if (c < 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00003985 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00003986 if (c <= BY_SPECIAL)
3987 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00003988 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003989 {
3990 /* No flags, all regions. */
3991 idxs[idx] = 0;
3992 c = 0;
3993 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00003994 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00003995 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00003996 if (prefixtree)
3997 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00003998 /* Read the optional pflags byte, the prefix ID and the
3999 * condition nr. In idxs[] store the prefix ID in the low
4000 * byte, the condition index shifted up 8 bits, the flags
4001 * shifted up 24 bits. */
4002 if (c == BY_FLAGS)
4003 c = getc(fd) << 24; /* <pflags> */
4004 else
4005 c = 0;
4006
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004007 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004008
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004009 n = get2c(fd); /* <prefcondnr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004010 if (n >= maxprefcondnr)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004011 return SP_FORMERROR;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004012 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004013 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004014 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004015 {
4016 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004017 * idxs[] the flags go in the low two bytes, region above
4018 * that and prefix ID above the region. */
4019 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004020 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004021 if (c2 == BY_FLAGS2)
4022 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004023 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004024 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004025 if (c & WF_AFX)
4026 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004027 }
4028
Bram Moolenaar51485f02005-06-04 21:55:20 +00004029 idxs[idx] = c;
4030 c = 0;
4031 }
4032 else /* c == BY_INDEX */
4033 {
4034 /* <nodeidx> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004035 n = get3c(fd);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004036 if (n < 0 || n >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004037 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004038 idxs[idx] = n + SHARED_MASK;
4039 c = getc(fd); /* <xbyte> */
4040 }
4041 }
4042 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004043 }
4044
Bram Moolenaar51485f02005-06-04 21:55:20 +00004045 /* Recursively read the children for non-shared siblings.
4046 * Skip the end-of-word ones (zero byte value) and the shared ones (and
4047 * remove SHARED_MASK) */
4048 for (i = 1; i <= len; ++i)
4049 if (byts[startidx + i] != 0)
4050 {
4051 if (idxs[startidx + i] & SHARED_MASK)
4052 idxs[startidx + i] &= ~SHARED_MASK;
4053 else
4054 {
4055 idxs[startidx + i] = idx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004056 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004057 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004058 if (idx < 0)
4059 break;
4060 }
4061 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004062
Bram Moolenaar51485f02005-06-04 21:55:20 +00004063 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004064}
4065
4066/*
4067 * Parse 'spelllang' and set buf->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004068 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004069 */
4070 char_u *
4071did_set_spelllang(buf)
4072 buf_T *buf;
4073{
4074 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004075 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004076 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00004077 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004078 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004079 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004080 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004081 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004082 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004083 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004084 int len;
4085 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004086 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004087 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004088 char_u *use_region = NULL;
4089 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004090 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004091 int i, j;
4092 langp_T *lp, *lp2;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004093
4094 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004095 clear_midword(buf);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004096
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004097 /* loop over comma separated language names. */
4098 for (splp = buf->b_p_spl; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004099 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004100 /* Get one language name. */
4101 copy_option_part(&splp, lang, MAXWLEN, ",");
4102
Bram Moolenaar5482f332005-04-17 20:18:43 +00004103 region = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004104 len = STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004105
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004106 /* If the name ends in ".spl" use it as the name of the spell file.
4107 * If there is a region name let "region" point to it and remove it
4108 * from the name. */
4109 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
4110 {
4111 filename = TRUE;
4112
Bram Moolenaarb6356332005-07-18 21:40:44 +00004113 /* Locate a region and remove it from the file name. */
4114 p = vim_strchr(gettail(lang), '_');
4115 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
4116 && !ASCII_ISALPHA(p[3]))
4117 {
4118 vim_strncpy(region_cp, p + 1, 2);
4119 mch_memmove(p, p + 3, len - (p - lang) - 2);
4120 len -= 3;
4121 region = region_cp;
4122 }
4123 else
4124 dont_use_region = TRUE;
4125
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004126 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004127 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4128 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004129 break;
4130 }
4131 else
4132 {
4133 filename = FALSE;
4134 if (len > 3 && lang[len - 3] == '_')
4135 {
4136 region = lang + len - 2;
4137 len -= 3;
4138 lang[len] = NUL;
4139 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004140 else
4141 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004142
4143 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004144 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4145 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004146 break;
4147 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004148
Bram Moolenaarb6356332005-07-18 21:40:44 +00004149 if (region != NULL)
4150 {
4151 /* If the region differs from what was used before then don't
4152 * use it for 'spellfile'. */
4153 if (use_region != NULL && STRCMP(region, use_region) != 0)
4154 dont_use_region = TRUE;
4155 use_region = region;
4156 }
4157
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004158 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004159 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004160 {
4161 if (filename)
4162 (void)spell_load_file(lang, lang, NULL, FALSE);
4163 else
4164 spell_load_lang(lang);
4165 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004166
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004167 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004168 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004169 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004170 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4171 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4172 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004173 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004174 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004175 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004176 {
4177 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004178 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004179 if (c == REGION_ALL)
4180 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004181 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004182 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004183 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004184 /* This addition file is for other regions. */
4185 region_mask = 0;
4186 }
4187 else
4188 /* This is probably an error. Give a warning and
4189 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004190 smsg((char_u *)
4191 _("Warning: region %s not supported"),
4192 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004193 }
4194 else
4195 region_mask = 1 << c;
4196 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004197
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004198 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004199 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004200 if (ga_grow(&ga, 1) == FAIL)
4201 {
4202 ga_clear(&ga);
4203 return e_outofmem;
4204 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004205 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004206 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4207 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004208 use_midword(slang, buf);
4209 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004210 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004211 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004212 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004213 }
4214
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004215 /* round 0: load int_wordlist, if possible.
4216 * round 1: load first name in 'spellfile'.
4217 * round 2: load second name in 'spellfile.
4218 * etc. */
4219 spf = curbuf->b_p_spf;
4220 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004221 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004222 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004223 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004224 /* Internal wordlist, if there is one. */
4225 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004226 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004227 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004228 }
4229 else
4230 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004231 /* One entry in 'spellfile'. */
4232 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4233 STRCAT(spf_name, ".spl");
4234
4235 /* If it was already found above then skip it. */
4236 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004237 {
4238 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4239 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004240 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004241 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004242 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004243 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004244 }
4245
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004246 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004247 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4248 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004249 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004250 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004251 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004252 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004253 * region name, the region is ignored otherwise. for int_wordlist
4254 * use an arbitrary name. */
4255 if (round == 0)
4256 STRCPY(lang, "internal wordlist");
4257 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00004258 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004259 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004260 p = vim_strchr(lang, '.');
4261 if (p != NULL)
4262 *p = NUL; /* truncate at ".encoding.add" */
4263 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004264 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004265
4266 /* If one of the languages has NOBREAK we assume the addition
4267 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004268 if (slang != NULL && nobreak)
4269 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004270 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004271 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004272 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004273 region_mask = REGION_ALL;
4274 if (use_region != NULL && !dont_use_region)
4275 {
4276 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004277 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004278 if (c != REGION_ALL)
4279 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004280 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004281 /* This spell file is for other regions. */
4282 region_mask = 0;
4283 }
4284
4285 if (region_mask != 0)
4286 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004287 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4288 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4289 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004290 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4291 ++ga.ga_len;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004292 use_midword(slang, buf);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004293 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004294 }
4295 }
4296
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004297 /* Everything is fine, store the new b_langp value. */
4298 ga_clear(&buf->b_langp);
4299 buf->b_langp = ga;
4300
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004301 /* For each language figure out what language to use for sound folding and
4302 * REP items. If the language doesn't support it itself use another one
4303 * with the same name. E.g. for "en-math" use "en". */
4304 for (i = 0; i < ga.ga_len; ++i)
4305 {
4306 lp = LANGP_ENTRY(ga, i);
4307
4308 /* sound folding */
4309 if (lp->lp_slang->sl_sal.ga_len > 0)
4310 /* language does sound folding itself */
4311 lp->lp_sallang = lp->lp_slang;
4312 else
4313 /* find first similar language that does sound folding */
4314 for (j = 0; j < ga.ga_len; ++j)
4315 {
4316 lp2 = LANGP_ENTRY(ga, j);
4317 if (lp2->lp_slang->sl_sal.ga_len > 0
4318 && STRNCMP(lp->lp_slang->sl_name,
4319 lp2->lp_slang->sl_name, 2) == 0)
4320 {
4321 lp->lp_sallang = lp2->lp_slang;
4322 break;
4323 }
4324 }
4325
4326 /* REP items */
4327 if (lp->lp_slang->sl_rep.ga_len > 0)
4328 /* language has REP items itself */
4329 lp->lp_replang = lp->lp_slang;
4330 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004331 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004332 for (j = 0; j < ga.ga_len; ++j)
4333 {
4334 lp2 = LANGP_ENTRY(ga, j);
4335 if (lp2->lp_slang->sl_rep.ga_len > 0
4336 && STRNCMP(lp->lp_slang->sl_name,
4337 lp2->lp_slang->sl_name, 2) == 0)
4338 {
4339 lp->lp_replang = lp2->lp_slang;
4340 break;
4341 }
4342 }
4343 }
4344
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004345 return NULL;
4346}
4347
4348/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004349 * Clear the midword characters for buffer "buf".
4350 */
4351 static void
4352clear_midword(buf)
4353 buf_T *buf;
4354{
4355 vim_memset(buf->b_spell_ismw, 0, 256);
4356#ifdef FEAT_MBYTE
4357 vim_free(buf->b_spell_ismw_mb);
4358 buf->b_spell_ismw_mb = NULL;
4359#endif
4360}
4361
4362/*
4363 * Use the "sl_midword" field of language "lp" for buffer "buf".
4364 * They add up to any currently used midword characters.
4365 */
4366 static void
4367use_midword(lp, buf)
4368 slang_T *lp;
4369 buf_T *buf;
4370{
4371 char_u *p;
4372
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004373 if (lp->sl_midword == NULL) /* there aren't any */
4374 return;
4375
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004376 for (p = lp->sl_midword; *p != NUL; )
4377#ifdef FEAT_MBYTE
4378 if (has_mbyte)
4379 {
4380 int c, l, n;
4381 char_u *bp;
4382
4383 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004384 l = (*mb_ptr2len)(p);
4385 if (c < 256 && l <= 2)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004386 buf->b_spell_ismw[c] = TRUE;
4387 else if (buf->b_spell_ismw_mb == NULL)
4388 /* First multi-byte char in "b_spell_ismw_mb". */
4389 buf->b_spell_ismw_mb = vim_strnsave(p, l);
4390 else
4391 {
4392 /* Append multi-byte chars to "b_spell_ismw_mb". */
4393 n = STRLEN(buf->b_spell_ismw_mb);
4394 bp = vim_strnsave(buf->b_spell_ismw_mb, n + l);
4395 if (bp != NULL)
4396 {
4397 vim_free(buf->b_spell_ismw_mb);
4398 buf->b_spell_ismw_mb = bp;
4399 vim_strncpy(bp + n, p, l);
4400 }
4401 }
4402 p += l;
4403 }
4404 else
4405#endif
4406 buf->b_spell_ismw[*p++] = TRUE;
4407}
4408
4409/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004410 * Find the region "region[2]" in "rp" (points to "sl_regions").
4411 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00004412 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004413 */
4414 static int
4415find_region(rp, region)
4416 char_u *rp;
4417 char_u *region;
4418{
4419 int i;
4420
4421 for (i = 0; ; i += 2)
4422 {
4423 if (rp[i] == NUL)
4424 return REGION_ALL;
4425 if (rp[i] == region[0] && rp[i + 1] == region[1])
4426 break;
4427 }
4428 return i / 2;
4429}
4430
4431/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004432 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004433 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00004434 * Word WF_ONECAP
4435 * W WORD WF_ALLCAP
4436 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004437 */
4438 static int
4439captype(word, end)
4440 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004441 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004442{
4443 char_u *p;
4444 int c;
4445 int firstcap;
4446 int allcap;
4447 int past_second = FALSE; /* past second word char */
4448
4449 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004450 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004451 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004452 return 0; /* only non-word characters, illegal word */
4453#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004454 if (has_mbyte)
4455 c = mb_ptr2char_adv(&p);
4456 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004457#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004458 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004459 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004460
4461 /*
4462 * Need to check all letters to find a word with mixed upper/lower.
4463 * But a word with an upper char only at start is a ONECAP.
4464 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004465 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004466 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004467 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004468 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004469 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004470 {
4471 /* UUl -> KEEPCAP */
4472 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004473 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004474 allcap = FALSE;
4475 }
4476 else if (!allcap)
4477 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004478 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004479 past_second = TRUE;
4480 }
4481
4482 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004483 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004484 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004485 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004486 return 0;
4487}
4488
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004489/*
4490 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4491 * capital. So that make_case_word() can turn WOrd into Word.
4492 * Add ALLCAP for "WOrD".
4493 */
4494 static int
4495badword_captype(word, end)
4496 char_u *word;
4497 char_u *end;
4498{
4499 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004500 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004501 int l, u;
4502 int first;
4503 char_u *p;
4504
4505 if (flags & WF_KEEPCAP)
4506 {
4507 /* Count the number of UPPER and lower case letters. */
4508 l = u = 0;
4509 first = FALSE;
4510 for (p = word; p < end; mb_ptr_adv(p))
4511 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004512 c = PTR2CHAR(p);
4513 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004514 {
4515 ++u;
4516 if (p == word)
4517 first = TRUE;
4518 }
4519 else
4520 ++l;
4521 }
4522
4523 /* If there are more UPPER than lower case letters suggest an
4524 * ALLCAP word. Otherwise, if the first letter is UPPER then
4525 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4526 * require three upper case letters. */
4527 if (u > l && u > 2)
4528 flags |= WF_ALLCAP;
4529 else if (first)
4530 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004531
4532 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4533 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004534 }
4535 return flags;
4536}
4537
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004538# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4539/*
4540 * Free all languages.
4541 */
4542 void
4543spell_free_all()
4544{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004545 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004546 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004547 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004548
4549 /* Go through all buffers and handle 'spelllang'. */
4550 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4551 ga_clear(&buf->b_langp);
4552
4553 while (first_lang != NULL)
4554 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004555 slang = first_lang;
4556 first_lang = slang->sl_next;
4557 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004558 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004559
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004560 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004561 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004562 /* Delete the internal wordlist and its .spl file */
4563 mch_remove(int_wordlist);
4564 int_wordlist_spl(fname);
4565 mch_remove(fname);
4566 vim_free(int_wordlist);
4567 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004568 }
4569
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004570 init_spell_chartab();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004571
4572 vim_free(repl_to);
4573 repl_to = NULL;
4574 vim_free(repl_from);
4575 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004576}
4577# endif
4578
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004579# if defined(FEAT_MBYTE) || defined(PROTO)
4580/*
4581 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004582 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004583 */
4584 void
4585spell_reload()
4586{
4587 buf_T *buf;
Bram Moolenaar3982c542005-06-08 21:56:31 +00004588 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004589
Bram Moolenaarea408852005-06-25 22:49:46 +00004590 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004591 init_spell_chartab();
4592
4593 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004594 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004595
4596 /* Go through all buffers and handle 'spelllang'. */
4597 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4598 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004599 /* Only load the wordlists when 'spelllang' is set and there is a
4600 * window for this buffer in which 'spell' is set. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004601 if (*buf->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004602 {
4603 FOR_ALL_WINDOWS(wp)
4604 if (wp->w_buffer == buf && wp->w_p_spell)
4605 {
4606 (void)did_set_spelllang(buf);
4607# ifdef FEAT_WINDOWS
4608 break;
4609# endif
4610 }
4611 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004612 }
4613}
4614# endif
4615
Bram Moolenaarb765d632005-06-07 21:00:02 +00004616/*
4617 * Reload the spell file "fname" if it's loaded.
4618 */
4619 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004620spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004621 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004622 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004623{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004624 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004625 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004626
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004627 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004628 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004629 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004630 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004631 slang_clear(slang);
4632 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004633 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004634 slang_clear(slang);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004635 redraw_all_later(SOME_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004636 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004637 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004638 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004639
4640 /* When "zg" was used and the file wasn't loaded yet, should redo
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004641 * 'spelllang' to load it now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004642 if (added_word && !didit)
4643 did_set_spelllang(curbuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004644}
4645
4646
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004647/*
4648 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004649 */
4650
Bram Moolenaar51485f02005-06-04 21:55:20 +00004651#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004652 and .dic file. */
4653/*
4654 * Main structure to store the contents of a ".aff" file.
4655 */
4656typedef struct afffile_S
4657{
4658 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004659 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004660 unsigned af_rare; /* RARE ID for rare word */
4661 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004662 unsigned af_bad; /* BAD ID for banned word */
4663 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004664 unsigned af_circumfix; /* CIRCUMFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004665 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004666 unsigned af_comproot; /* COMPOUNDROOT ID */
4667 unsigned af_compforbid; /* COMPOUNDFORBIDFLAG ID */
4668 unsigned af_comppermit; /* COMPOUNDPERMITFLAG ID */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004669 unsigned af_nosuggest; /* NOSUGGEST ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004670 int af_pfxpostpone; /* postpone prefixes without chop string and
4671 without flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004672 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4673 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004674 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004675} afffile_T;
4676
Bram Moolenaar6de68532005-08-24 22:08:48 +00004677#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004678#define AFT_LONG 1 /* flags are two characters */
4679#define AFT_CAPLONG 2 /* flags are one or two characters */
4680#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004681
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004682typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004683/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4684struct affentry_S
4685{
4686 affentry_T *ae_next; /* next affix with same name/number */
4687 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4688 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004689 char_u *ae_flags; /* flags on the affix (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004690 char_u *ae_cond; /* condition (NULL for ".") */
4691 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004692 char ae_compforbid; /* COMPOUNDFORBIDFLAG found */
4693 char ae_comppermit; /* COMPOUNDPERMITFLAG found */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004694};
4695
Bram Moolenaar6de68532005-08-24 22:08:48 +00004696#ifdef FEAT_MBYTE
4697# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4698#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004699# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004700#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004701
Bram Moolenaar51485f02005-06-04 21:55:20 +00004702/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4703typedef struct affheader_S
4704{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004705 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4706 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4707 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004708 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004709 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004710 affentry_T *ah_first; /* first affix entry */
4711} affheader_T;
4712
4713#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4714
Bram Moolenaar6de68532005-08-24 22:08:48 +00004715/* Flag used in compound items. */
4716typedef struct compitem_S
4717{
4718 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4719 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4720 int ci_newID; /* affix ID after renumbering. */
4721} compitem_T;
4722
4723#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4724
Bram Moolenaar51485f02005-06-04 21:55:20 +00004725/*
4726 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004727 * the need to keep track of each allocated thing, everything is freed all at
4728 * once after ":mkspell" is done.
Bram Moolenaar51485f02005-06-04 21:55:20 +00004729 */
4730#define SBLOCKSIZE 16000 /* size of sb_data */
4731typedef struct sblock_S sblock_T;
4732struct sblock_S
4733{
4734 sblock_T *sb_next; /* next block in list */
4735 int sb_used; /* nr of bytes already in use */
4736 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004737};
4738
4739/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004740 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004741 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004742typedef struct wordnode_S wordnode_T;
4743struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004744{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004745 union /* shared to save space */
4746 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004747 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004748 int index; /* index in written nodes (valid after first
4749 round) */
4750 } wn_u1;
4751 union /* shared to save space */
4752 {
4753 wordnode_T *next; /* next node with same hash key */
4754 wordnode_T *wnode; /* parent node that will write this node */
4755 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004756 wordnode_T *wn_child; /* child (next byte in word) */
4757 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4758 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004759 int wn_refs; /* Nr. of references to this node. Only
4760 relevant for first node in a list of
4761 siblings, in following siblings it is
4762 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004763 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004764
4765 /* Info for when "wn_byte" is NUL.
4766 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4767 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4768 * "wn_region" the LSW of the wordnr. */
4769 char_u wn_affixID; /* supported/required prefix ID or 0 */
4770 short_u wn_flags; /* WF_ flags */
4771 short wn_region; /* region mask */
4772
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004773#ifdef SPELL_PRINTTREE
4774 int wn_nr; /* sequence nr for printing */
4775#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004776};
4777
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004778#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4779
Bram Moolenaar51485f02005-06-04 21:55:20 +00004780#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004781
Bram Moolenaar51485f02005-06-04 21:55:20 +00004782/*
4783 * Info used while reading the spell files.
4784 */
4785typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004786{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004787 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004788 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004789
Bram Moolenaar51485f02005-06-04 21:55:20 +00004790 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004791 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004792
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004793 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004794
Bram Moolenaar4770d092006-01-12 23:22:24 +00004795 long si_sugtree; /* creating the soundfolding trie */
4796
Bram Moolenaar51485f02005-06-04 21:55:20 +00004797 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004798 long si_blocks_cnt; /* memory blocks allocated */
4799 long si_compress_cnt; /* words to add before lowering
4800 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004801 wordnode_T *si_first_free; /* List of nodes that have been freed during
4802 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004803 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004804#ifdef SPELL_PRINTTREE
4805 int si_wordnode_nr; /* sequence nr for nodes */
4806#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004807 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004808
Bram Moolenaar51485f02005-06-04 21:55:20 +00004809 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004810 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004811 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004812 int si_region; /* region mask */
4813 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004814 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004815 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004816 int si_msg_count; /* number of words added since last message */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004817 char_u *si_info; /* info text chars or NULL */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004818 int si_region_count; /* number of regions supported (1 when there
4819 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004820 char_u si_region_name[16]; /* region names; used only if
4821 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004822
4823 garray_T si_rep; /* list of fromto_T entries from REP lines */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004824 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004825 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004826 char_u *si_sofofr; /* SOFOFROM text */
4827 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004828 int si_nosugfile; /* NOSUGFILE item found */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004829 int si_nosplitsugs; /* NOSPLITSUGS item found */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004830 int si_followup; /* soundsalike: ? */
4831 int si_collapse; /* soundsalike: ? */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004832 hashtab_T si_commonwords; /* hashtable for common words */
4833 time_t si_sugtime; /* timestamp for .sug file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004834 int si_rem_accents; /* soundsalike: remove accents */
4835 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004836 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004837 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004838 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004839 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004840 int si_compoptions; /* COMP_ flags */
4841 garray_T si_comppat; /* CHECKCOMPOUNDPATTERN items, each stored as
4842 a string */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004843 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004844 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004845 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004846 garray_T si_prefcond; /* table with conditions for postponed
4847 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004848 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004849 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004850} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004851
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004852static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004853static void aff_process_flags __ARGS((afffile_T *affile, affentry_T *entry));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004854static int spell_info_item __ARGS((char_u *s));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004855static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
4856static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
4857static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004858static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004859static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
4860static void aff_check_number __ARGS((int spinval, int affval, char *name));
4861static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004862static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004863static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4864static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00004865static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004866static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004867static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004868static int get_affix_flags __ARGS((afffile_T *affile, char_u *afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004869static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004870static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004871static 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 +00004872static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
4873static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
4874static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004875static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004876static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00004877static 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 +00004878static 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 +00004879static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004880static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004881static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
4882static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
4883static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00004884static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004885static void put_sugtime __ARGS((spellinfo_T *spin, FILE *fd));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004886static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00004887static void clear_node __ARGS((wordnode_T *node));
4888static int put_node __ARGS((FILE *fd, wordnode_T *node, int index, int regionmask, int prefixtree));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004889static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname));
4890static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang));
4891static int sug_maketable __ARGS((spellinfo_T *spin));
4892static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap));
4893static int offset2bytes __ARGS((int nr, char_u *buf));
4894static int bytes2offset __ARGS((char_u **pp));
4895static void sug_write __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004896static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaar4770d092006-01-12 23:22:24 +00004897static void spell_message __ARGS((spellinfo_T *spin, char_u *str));
Bram Moolenaarb765d632005-06-07 21:00:02 +00004898static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004899
Bram Moolenaar53805d12005-08-01 07:08:33 +00004900/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
4901 * but it must be negative to indicate the prefix tree to tree_add_word().
4902 * Use a negative number with the lower 8 bits zero. */
4903#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004904
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004905/* flags for "condit" argument of store_aff_word() */
4906#define CONDIT_COMB 1 /* affix must combine */
4907#define CONDIT_CFIX 2 /* affix must have CIRCUMFIX flag */
4908#define CONDIT_SUF 4 /* add a suffix for matching flags */
4909#define CONDIT_AFF 8 /* word already has an affix */
4910
Bram Moolenaar5195e452005-08-19 20:32:47 +00004911/*
4912 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
4913 */
4914static long compress_start = 30000; /* memory / SBLOCKSIZE */
4915static long compress_inc = 100; /* memory / SBLOCKSIZE */
4916static long compress_added = 500000; /* word count */
4917
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004918#ifdef SPELL_PRINTTREE
4919/*
4920 * For debugging the tree code: print the current tree in a (more or less)
4921 * readable format, so that we can see what happens when adding a word and/or
4922 * compressing the tree.
4923 * Based on code from Olaf Seibert.
4924 */
4925#define PRINTLINESIZE 1000
4926#define PRINTWIDTH 6
4927
4928#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
4929 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
4930
4931static char line1[PRINTLINESIZE];
4932static char line2[PRINTLINESIZE];
4933static char line3[PRINTLINESIZE];
4934
4935 static void
4936spell_clear_flags(wordnode_T *node)
4937{
4938 wordnode_T *np;
4939
4940 for (np = node; np != NULL; np = np->wn_sibling)
4941 {
4942 np->wn_u1.index = FALSE;
4943 spell_clear_flags(np->wn_child);
4944 }
4945}
4946
4947 static void
4948spell_print_node(wordnode_T *node, int depth)
4949{
4950 if (node->wn_u1.index)
4951 {
4952 /* Done this node before, print the reference. */
4953 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
4954 PRINTSOME(line2, depth, " ", 0, 0);
4955 PRINTSOME(line3, depth, " ", 0, 0);
4956 msg(line1);
4957 msg(line2);
4958 msg(line3);
4959 }
4960 else
4961 {
4962 node->wn_u1.index = TRUE;
4963
4964 if (node->wn_byte != NUL)
4965 {
4966 if (node->wn_child != NULL)
4967 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
4968 else
4969 /* Cannot happen? */
4970 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
4971 }
4972 else
4973 PRINTSOME(line1, depth, " $ ", 0, 0);
4974
4975 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
4976
4977 if (node->wn_sibling != NULL)
4978 PRINTSOME(line3, depth, " | ", 0, 0);
4979 else
4980 PRINTSOME(line3, depth, " ", 0, 0);
4981
4982 if (node->wn_byte == NUL)
4983 {
4984 msg(line1);
4985 msg(line2);
4986 msg(line3);
4987 }
4988
4989 /* do the children */
4990 if (node->wn_byte != NUL && node->wn_child != NULL)
4991 spell_print_node(node->wn_child, depth + 1);
4992
4993 /* do the siblings */
4994 if (node->wn_sibling != NULL)
4995 {
4996 /* get rid of all parent details except | */
4997 STRCPY(line1, line3);
4998 STRCPY(line2, line3);
4999 spell_print_node(node->wn_sibling, depth);
5000 }
5001 }
5002}
5003
5004 static void
5005spell_print_tree(wordnode_T *root)
5006{
5007 if (root != NULL)
5008 {
5009 /* Clear the "wn_u1.index" fields, used to remember what has been
5010 * done. */
5011 spell_clear_flags(root);
5012
5013 /* Recursively print the tree. */
5014 spell_print_node(root, 0);
5015 }
5016}
5017#endif /* SPELL_PRINTTREE */
5018
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005019/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005020 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00005021 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005022 */
5023 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005024spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005025 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005026 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005027{
5028 FILE *fd;
5029 afffile_T *aff;
5030 char_u rline[MAXLINELEN];
5031 char_u *line;
5032 char_u *pc = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005033#define MAXITEMCNT 30
Bram Moolenaar8db73182005-06-17 21:51:16 +00005034 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005035 int itemcnt;
5036 char_u *p;
5037 int lnum = 0;
5038 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005039 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005040 int aff_todo = 0;
5041 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005042 char_u *low = NULL;
5043 char_u *fol = NULL;
5044 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005045 int do_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005046 int do_repsal;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005047 int do_sal;
5048 int do_map;
5049 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005050 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005051 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005052 int compminlen = 0; /* COMPOUNDMIN value */
5053 int compsylmax = 0; /* COMPOUNDSYLMAX value */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005054 int compoptions = 0; /* COMP_ flags */
5055 int compmax = 0; /* COMPOUNDWORDMAX value */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005056 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDRULE
Bram Moolenaar6de68532005-08-24 22:08:48 +00005057 concatenated */
5058 char_u *midword = NULL; /* MIDWORD value */
5059 char_u *syllable = NULL; /* SYLLABLE value */
5060 char_u *sofofrom = NULL; /* SOFOFROM value */
5061 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005062
Bram Moolenaar51485f02005-06-04 21:55:20 +00005063 /*
5064 * Open the file.
5065 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005066 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005067 if (fd == NULL)
5068 {
5069 EMSG2(_(e_notopen), fname);
5070 return NULL;
5071 }
5072
Bram Moolenaar4770d092006-01-12 23:22:24 +00005073 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
5074 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005075
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005076 /* Only do REP lines when not done in another .aff file already. */
5077 do_rep = spin->si_rep.ga_len == 0;
5078
Bram Moolenaar4770d092006-01-12 23:22:24 +00005079 /* Only do REPSAL lines when not done in another .aff file already. */
5080 do_repsal = spin->si_repsal.ga_len == 0;
5081
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005082 /* Only do SAL lines when not done in another .aff file already. */
5083 do_sal = spin->si_sal.ga_len == 0;
5084
5085 /* Only do MAP lines when not done in another .aff file already. */
5086 do_map = spin->si_map.ga_len == 0;
5087
Bram Moolenaar51485f02005-06-04 21:55:20 +00005088 /*
5089 * Allocate and init the afffile_T structure.
5090 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005091 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005092 if (aff == NULL)
5093 return NULL;
5094 hash_init(&aff->af_pref);
5095 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005096 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005097
5098 /*
5099 * Read all the lines in the file one by one.
5100 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005101 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005102 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005103 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005104 ++lnum;
5105
5106 /* Skip comment lines. */
5107 if (*rline == '#')
5108 continue;
5109
5110 /* Convert from "SET" to 'encoding' when needed. */
5111 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005112#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005113 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005114 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005115 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005116 if (pc == NULL)
5117 {
5118 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5119 fname, lnum, rline);
5120 continue;
5121 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005122 line = pc;
5123 }
5124 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005125#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005126 {
5127 pc = NULL;
5128 line = rline;
5129 }
5130
5131 /* Split the line up in white separated items. Put a NUL after each
5132 * item. */
5133 itemcnt = 0;
5134 for (p = line; ; )
5135 {
5136 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
5137 ++p;
5138 if (*p == NUL)
5139 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00005140 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005141 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005142 items[itemcnt++] = p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005143 /* A few items have arbitrary text argument, don't split them. */
5144 if (itemcnt == 2 && spell_info_item(items[0]))
5145 while (*p >= ' ' || *p == TAB) /* skip until CR/NL */
5146 ++p;
5147 else
5148 while (*p > ' ') /* skip until white space or CR/NL */
5149 ++p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005150 if (*p == NUL)
5151 break;
5152 *p++ = NUL;
5153 }
5154
5155 /* Handle non-empty lines. */
5156 if (itemcnt > 0)
5157 {
5158 if (STRCMP(items[0], "SET") == 0 && itemcnt == 2
5159 && aff->af_enc == NULL)
5160 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005161#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005162 /* Setup for conversion from "ENC" to 'encoding'. */
5163 aff->af_enc = enc_canonize(items[1]);
5164 if (aff->af_enc != NULL && !spin->si_ascii
5165 && convert_setup(&spin->si_conv, aff->af_enc,
5166 p_enc) == FAIL)
5167 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
5168 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005169 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005170#else
5171 smsg((char_u *)_("Conversion in %s not supported"), fname);
5172#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005173 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005174 else if (STRCMP(items[0], "FLAG") == 0 && itemcnt == 2
5175 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005176 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005177 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005178 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005179 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005180 aff->af_flagtype = AFT_NUM;
5181 else if (STRCMP(items[1], "caplong") == 0)
5182 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005183 else
5184 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
5185 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00005186 if (aff->af_rare != 0
5187 || aff->af_keepcase != 0
5188 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005189 || aff->af_needaffix != 0
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005190 || aff->af_circumfix != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005191 || aff->af_needcomp != 0
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005192 || aff->af_comproot != 0
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005193 || aff->af_nosuggest != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005194 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00005195 || aff->af_suff.ht_used > 0
5196 || aff->af_pref.ht_used > 0)
5197 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
5198 fname, lnum, items[1]);
5199 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005200 else if (spell_info_item(items[0]))
5201 {
5202 p = (char_u *)getroom(spin,
5203 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
5204 + STRLEN(items[0])
5205 + STRLEN(items[1]) + 3, FALSE);
5206 if (p != NULL)
5207 {
5208 if (spin->si_info != NULL)
5209 {
5210 STRCPY(p, spin->si_info);
5211 STRCAT(p, "\n");
5212 }
5213 STRCAT(p, items[0]);
5214 STRCAT(p, " ");
5215 STRCAT(p, items[1]);
5216 spin->si_info = p;
5217 }
5218 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005219 else if (STRCMP(items[0], "MIDWORD") == 0 && itemcnt == 2
5220 && midword == NULL)
5221 {
5222 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005223 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005224 else if (STRCMP(items[0], "TRY") == 0 && itemcnt == 2)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005226 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005227 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005228 /* TODO: remove "RAR" later */
5229 else if ((STRCMP(items[0], "RAR") == 0
5230 || STRCMP(items[0], "RARE") == 0) && itemcnt == 2
5231 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005232 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005233 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005234 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005235 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005236 /* TODO: remove "KEP" later */
5237 else if ((STRCMP(items[0], "KEP") == 0
5238 || STRCMP(items[0], "KEEPCASE") == 0) && itemcnt == 2
5239 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005240 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005241 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005242 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005243 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00005244 else if (STRCMP(items[0], "BAD") == 0 && itemcnt == 2
5245 && aff->af_bad == 0)
5246 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005247 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5248 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005249 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005250 else if (STRCMP(items[0], "NEEDAFFIX") == 0 && itemcnt == 2
5251 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005252 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005253 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5254 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005255 }
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005256 else if (STRCMP(items[0], "CIRCUMFIX") == 0 && itemcnt == 2
5257 && aff->af_circumfix == 0)
5258 {
5259 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
5260 fname, lnum);
5261 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005262 else if (STRCMP(items[0], "NOSUGGEST") == 0 && itemcnt == 2
5263 && aff->af_nosuggest == 0)
5264 {
5265 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
5266 fname, lnum);
5267 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005268 else if (STRCMP(items[0], "NEEDCOMPOUND") == 0 && itemcnt == 2
5269 && aff->af_needcomp == 0)
5270 {
5271 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5272 fname, lnum);
5273 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005274 else if (STRCMP(items[0], "COMPOUNDROOT") == 0 && itemcnt == 2
5275 && aff->af_comproot == 0)
5276 {
5277 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
5278 fname, lnum);
5279 }
5280 else if (STRCMP(items[0], "COMPOUNDFORBIDFLAG") == 0
5281 && itemcnt == 2 && aff->af_compforbid == 0)
5282 {
5283 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
5284 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005285 if (aff->af_pref.ht_used > 0)
5286 smsg((char_u *)_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
5287 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005288 }
5289 else if (STRCMP(items[0], "COMPOUNDPERMITFLAG") == 0
5290 && itemcnt == 2 && aff->af_comppermit == 0)
5291 {
5292 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
5293 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005294 if (aff->af_pref.ht_used > 0)
5295 smsg((char_u *)_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
5296 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005297 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005298 else if (STRCMP(items[0], "COMPOUNDFLAG") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005299 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005300 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005301 /* Turn flag "c" into COMPOUNDRULE compatible string "c+",
Bram Moolenaar6de68532005-08-24 22:08:48 +00005302 * "Na" into "Na+", "1234" into "1234+". */
5303 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005304 if (p != NULL)
5305 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005306 STRCPY(p, items[1]);
5307 STRCAT(p, "+");
5308 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005309 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005310 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005311 else if (STRCMP(items[0], "COMPOUNDRULE") == 0 && itemcnt == 2)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005312 {
5313 /* Concatenate this string to previously defined ones, using a
5314 * slash to separate them. */
5315 l = STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005316 if (compflags != NULL)
5317 l += STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005318 p = getroom(spin, l, FALSE);
5319 if (p != NULL)
5320 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005321 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005322 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005323 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005324 STRCAT(p, "/");
5325 }
5326 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005327 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005328 }
5329 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005330 else if (STRCMP(items[0], "COMPOUNDWORDMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005331 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005332 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005333 compmax = atoi((char *)items[1]);
5334 if (compmax == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005335 smsg((char_u *)_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar5195e452005-08-19 20:32:47 +00005336 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005337 }
5338 else if (STRCMP(items[0], "COMPOUNDMIN") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005339 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005340 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005341 compminlen = atoi((char *)items[1]);
5342 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005343 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5344 fname, lnum, items[1]);
5345 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005346 else if (STRCMP(items[0], "COMPOUNDSYLMAX") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005347 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005348 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005349 compsylmax = atoi((char *)items[1]);
5350 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005351 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5352 fname, lnum, items[1]);
5353 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005354 else if (STRCMP(items[0], "CHECKCOMPOUNDDUP") == 0 && itemcnt == 1)
5355 {
5356 compoptions |= COMP_CHECKDUP;
5357 }
5358 else if (STRCMP(items[0], "CHECKCOMPOUNDREP") == 0 && itemcnt == 1)
5359 {
5360 compoptions |= COMP_CHECKREP;
5361 }
5362 else if (STRCMP(items[0], "CHECKCOMPOUNDCASE") == 0 && itemcnt == 1)
5363 {
5364 compoptions |= COMP_CHECKCASE;
5365 }
5366 else if (STRCMP(items[0], "CHECKCOMPOUNDTRIPLE") == 0
5367 && itemcnt == 1)
5368 {
5369 compoptions |= COMP_CHECKTRIPLE;
5370 }
5371 else if (STRCMP(items[0], "CHECKCOMPOUNDPATTERN") == 0
5372 && itemcnt == 2)
5373 {
5374 if (atoi((char *)items[1]) == 0)
5375 smsg((char_u *)_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
5376 fname, lnum, items[1]);
5377 }
5378 else if (STRCMP(items[0], "CHECKCOMPOUNDPATTERN") == 0
5379 && itemcnt == 3)
5380 {
5381 garray_T *gap = &spin->si_comppat;
5382 int i;
5383
5384 /* Only add the couple if it isn't already there. */
5385 for (i = 0; i < gap->ga_len - 1; i += 2)
5386 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
5387 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
5388 items[2]) == 0)
5389 break;
5390 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
5391 {
5392 ((char_u **)(gap->ga_data))[gap->ga_len++]
5393 = getroom_save(spin, items[1]);
5394 ((char_u **)(gap->ga_data))[gap->ga_len++]
5395 = getroom_save(spin, items[2]);
5396 }
5397 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005398 else if (STRCMP(items[0], "SYLLABLE") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005399 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005400 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005401 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005402 }
Bram Moolenaar78622822005-08-23 21:00:13 +00005403 else if (STRCMP(items[0], "NOBREAK") == 0 && itemcnt == 1)
5404 {
5405 spin->si_nobreak = TRUE;
5406 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005407 else if (STRCMP(items[0], "NOSPLITSUGS") == 0 && itemcnt == 1)
5408 {
5409 spin->si_nosplitsugs = TRUE;
5410 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005411 else if (STRCMP(items[0], "NOSUGFILE") == 0 && itemcnt == 1)
5412 {
5413 spin->si_nosugfile = TRUE;
5414 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005415 else if (STRCMP(items[0], "PFXPOSTPONE") == 0 && itemcnt == 1)
5416 {
5417 aff->af_pfxpostpone = TRUE;
5418 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005419 else if ((STRCMP(items[0], "PFX") == 0
5420 || STRCMP(items[0], "SFX") == 0)
5421 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005422 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005423 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005424 int lasti = 4;
5425 char_u key[AH_KEY_LEN];
5426
5427 if (*items[0] == 'P')
5428 tp = &aff->af_pref;
5429 else
5430 tp = &aff->af_suff;
5431
5432 /* Myspell allows the same affix name to be used multiple
5433 * times. The affix files that do this have an undocumented
5434 * "S" flag on all but the last block, thus we check for that
5435 * and store it in ah_follows. */
5436 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5437 hi = hash_find(tp, key);
5438 if (!HASHITEM_EMPTY(hi))
5439 {
5440 cur_aff = HI2AH(hi);
5441 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5442 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5443 fname, lnum, items[1]);
5444 if (!cur_aff->ah_follows)
5445 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5446 fname, lnum, items[1]);
5447 }
5448 else
5449 {
5450 /* New affix letter. */
5451 cur_aff = (affheader_T *)getroom(spin,
5452 sizeof(affheader_T), TRUE);
5453 if (cur_aff == NULL)
5454 break;
5455 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5456 fname, lnum);
5457 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5458 break;
5459 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00005460 || cur_aff->ah_flag == aff->af_rare
5461 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005462 || cur_aff->ah_flag == aff->af_needaffix
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005463 || cur_aff->ah_flag == aff->af_circumfix
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005464 || cur_aff->ah_flag == aff->af_nosuggest
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005465 || cur_aff->ah_flag == aff->af_needcomp
5466 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005467 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 +00005468 fname, lnum, items[1]);
5469 STRCPY(cur_aff->ah_key, items[1]);
5470 hash_add(tp, cur_aff->ah_key);
5471
5472 cur_aff->ah_combine = (*items[2] == 'Y');
5473 }
5474
5475 /* Check for the "S" flag, which apparently means that another
5476 * block with the same affix name is following. */
5477 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5478 {
5479 ++lasti;
5480 cur_aff->ah_follows = TRUE;
5481 }
5482 else
5483 cur_aff->ah_follows = FALSE;
5484
Bram Moolenaar8db73182005-06-17 21:51:16 +00005485 /* Myspell allows extra text after the item, but that might
5486 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00005487 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005488 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005489
Bram Moolenaar95529562005-08-25 21:21:38 +00005490 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005491 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5492 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005493
Bram Moolenaar95529562005-08-25 21:21:38 +00005494 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005495 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005496 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005497 {
5498 /* Use a new number in the .spl file later, to be able
5499 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005500 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005501 cur_aff->ah_newID = ++spin->si_newprefID;
5502
5503 /* We only really use ah_newID if the prefix is
5504 * postponed. We know that only after handling all
5505 * the items. */
5506 did_postpone_prefix = FALSE;
5507 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005508 else
5509 /* Did use the ID in a previous block. */
5510 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005511 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005512
Bram Moolenaar51485f02005-06-04 21:55:20 +00005513 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005514 }
5515 else if ((STRCMP(items[0], "PFX") == 0
5516 || STRCMP(items[0], "SFX") == 0)
5517 && aff_todo > 0
5518 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005519 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005520 {
5521 affentry_T *aff_entry;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005522 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005523 int lasti = 5;
5524
Bram Moolenaar8db73182005-06-17 21:51:16 +00005525 /* Myspell allows extra text after the item, but that might
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005526 * mean mistakes go unnoticed. Require a comment-starter.
5527 * Hunspell uses a "-" item. */
5528 if (itemcnt > lasti && *items[lasti] != '#'
5529 && (STRCMP(items[lasti], "-") != 0
5530 || itemcnt != lasti + 1))
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005531 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005532
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005533 /* New item for an affix letter. */
5534 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005535 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005536 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005537 if (aff_entry == NULL)
5538 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005539
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005540 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005541 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005542 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005543 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005544 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005545
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005546 /* Recognize flags on the affix: abcd/XYZ */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005547 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
5548 if (aff_entry->ae_flags != NULL)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005549 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005550 *aff_entry->ae_flags++ = NUL;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005551 aff_process_flags(aff, aff_entry);
5552 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005553 }
5554
Bram Moolenaar51485f02005-06-04 21:55:20 +00005555 /* Don't use an affix entry with non-ASCII characters when
5556 * "spin->si_ascii" is TRUE. */
5557 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005558 || has_non_ascii(aff_entry->ae_add)))
5559 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00005560 aff_entry->ae_next = cur_aff->ah_first;
5561 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005562
5563 if (STRCMP(items[4], ".") != 0)
5564 {
5565 char_u buf[MAXLINELEN];
5566
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005567 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005568 if (*items[0] == 'P')
5569 sprintf((char *)buf, "^%s", items[4]);
5570 else
5571 sprintf((char *)buf, "%s$", items[4]);
5572 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005573 RE_MAGIC + RE_STRING + RE_STRICT);
5574 if (aff_entry->ae_prog == NULL)
5575 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5576 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005577 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005578
5579 /* For postponed prefixes we need an entry in si_prefcond
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005580 * for the condition. Use an existing one if possible.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005581 * Can't be done for an affix with flags, ignoring
5582 * COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005583 if (*items[0] == 'P' && aff->af_pfxpostpone
5584 && aff_entry->ae_flags == NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005585 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005586 /* When the chop string is one lower-case letter and
5587 * the add string ends in the upper-case letter we set
5588 * the "upper" flag, clear "ae_chop" and remove the
5589 * letters from "ae_add". The condition must either
5590 * be empty or start with the same letter. */
5591 if (aff_entry->ae_chop != NULL
5592 && aff_entry->ae_add != NULL
5593#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005594 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00005595 aff_entry->ae_chop)] == NUL
5596#else
5597 && aff_entry->ae_chop[1] == NUL
5598#endif
5599 )
5600 {
5601 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005602
Bram Moolenaar53805d12005-08-01 07:08:33 +00005603 c = PTR2CHAR(aff_entry->ae_chop);
5604 c_up = SPELL_TOUPPER(c);
5605 if (c_up != c
5606 && (aff_entry->ae_cond == NULL
5607 || PTR2CHAR(aff_entry->ae_cond) == c))
5608 {
5609 p = aff_entry->ae_add
5610 + STRLEN(aff_entry->ae_add);
5611 mb_ptr_back(aff_entry->ae_add, p);
5612 if (PTR2CHAR(p) == c_up)
5613 {
5614 upper = TRUE;
5615 aff_entry->ae_chop = NULL;
5616 *p = NUL;
5617
5618 /* The condition is matched with the
5619 * actual word, thus must check for the
5620 * upper-case letter. */
5621 if (aff_entry->ae_cond != NULL)
5622 {
5623 char_u buf[MAXLINELEN];
5624#ifdef FEAT_MBYTE
5625 if (has_mbyte)
5626 {
5627 onecap_copy(items[4], buf, TRUE);
5628 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005629 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005630 }
5631 else
5632#endif
5633 *aff_entry->ae_cond = c_up;
5634 if (aff_entry->ae_cond != NULL)
5635 {
5636 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005637 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005638 vim_free(aff_entry->ae_prog);
5639 aff_entry->ae_prog = vim_regcomp(
5640 buf, RE_MAGIC + RE_STRING);
5641 }
5642 }
5643 }
5644 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005645 }
5646
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005647 if (aff_entry->ae_chop == NULL
5648 && aff_entry->ae_flags == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005649 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005650 int idx;
5651 char_u **pp;
5652 int n;
5653
Bram Moolenaar6de68532005-08-24 22:08:48 +00005654 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005655 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5656 --idx)
5657 {
5658 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5659 if (str_equal(p, aff_entry->ae_cond))
5660 break;
5661 }
5662 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5663 {
5664 /* Not found, add a new condition. */
5665 idx = spin->si_prefcond.ga_len++;
5666 pp = ((char_u **)spin->si_prefcond.ga_data)
5667 + idx;
5668 if (aff_entry->ae_cond == NULL)
5669 *pp = NULL;
5670 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005671 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005672 aff_entry->ae_cond);
5673 }
5674
5675 /* Add the prefix to the prefix tree. */
5676 if (aff_entry->ae_add == NULL)
5677 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005678 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005679 p = aff_entry->ae_add;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005680
Bram Moolenaar53805d12005-08-01 07:08:33 +00005681 /* PFX_FLAGS is a negative number, so that
5682 * tree_add_word() knows this is the prefix tree. */
5683 n = PFX_FLAGS;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005684 if (!cur_aff->ah_combine)
5685 n |= WFP_NC;
5686 if (upper)
5687 n |= WFP_UP;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005688 if (aff_entry->ae_comppermit)
5689 n |= WFP_COMPPERMIT;
5690 if (aff_entry->ae_compforbid)
5691 n |= WFP_COMPFORBID;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005692 tree_add_word(spin, p, spin->si_prefroot, n,
5693 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005694 did_postpone_prefix = TRUE;
5695 }
5696
5697 /* Didn't actually use ah_newID, backup si_newprefID. */
5698 if (aff_todo == 0 && !did_postpone_prefix)
5699 {
5700 --spin->si_newprefID;
5701 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005702 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005703 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005704 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005705 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005706 else if (STRCMP(items[0], "FOL") == 0 && itemcnt == 2
5707 && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005708 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005709 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005710 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005711 else if (STRCMP(items[0], "LOW") == 0 && itemcnt == 2
5712 && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005713 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005714 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005715 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00005716 else if (STRCMP(items[0], "UPP") == 0 && itemcnt == 2
5717 && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005718 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005719 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005720 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005721 else if ((STRCMP(items[0], "REP") == 0
5722 || STRCMP(items[0], "REPSAL") == 0)
5723 && itemcnt == 2)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005724 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005725 /* Ignore REP/REPSAL count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005726 if (!isdigit(*items[1]))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005727 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005728 fname, lnum);
5729 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005730 else if ((STRCMP(items[0], "REP") == 0
5731 || STRCMP(items[0], "REPSAL") == 0)
5732 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005733 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005734 /* REP/REPSAL item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005735 /* Myspell ignores extra arguments, we require it starts with
5736 * # to detect mistakes. */
5737 if (itemcnt > 3 && items[3][0] != '#')
5738 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005739 if (items[0][3] == 'S' ? do_repsal : do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005740 {
5741 /* Replace underscore with space (can't include a space
5742 * directly). */
5743 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5744 if (*p == '_')
5745 *p = ' ';
5746 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5747 if (*p == '_')
5748 *p = ' ';
Bram Moolenaar4770d092006-01-12 23:22:24 +00005749 add_fromto(spin, items[0][3] == 'S'
5750 ? &spin->si_repsal
5751 : &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005752 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005753 }
5754 else if (STRCMP(items[0], "MAP") == 0 && itemcnt == 2)
5755 {
5756 /* MAP item or count */
5757 if (!found_map)
5758 {
5759 /* First line contains the count. */
5760 found_map = TRUE;
5761 if (!isdigit(*items[1]))
5762 smsg((char_u *)_("Expected MAP count in %s line %d"),
5763 fname, lnum);
5764 }
5765 else if (do_map)
5766 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005767 int c;
5768
5769 /* Check that every character appears only once. */
5770 for (p = items[1]; *p != NUL; )
5771 {
5772#ifdef FEAT_MBYTE
5773 c = mb_ptr2char_adv(&p);
5774#else
5775 c = *p++;
5776#endif
5777 if ((spin->si_map.ga_len > 0
5778 && vim_strchr(spin->si_map.ga_data, c)
5779 != NULL)
5780 || vim_strchr(p, c) != NULL)
5781 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5782 fname, lnum);
5783 }
5784
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005785 /* We simply concatenate all the MAP strings, separated by
5786 * slashes. */
5787 ga_concat(&spin->si_map, items[1]);
5788 ga_append(&spin->si_map, '/');
5789 }
5790 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005791 /* Accept "SAL from to" and "SAL from to # comment". */
5792 else if (STRCMP(items[0], "SAL") == 0
5793 && (itemcnt == 3 || (itemcnt > 3 && items[3][0] == '#')))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005794 {
5795 if (do_sal)
5796 {
5797 /* SAL item (sounds-a-like)
5798 * Either one of the known keys or a from-to pair. */
5799 if (STRCMP(items[1], "followup") == 0)
5800 spin->si_followup = sal_to_bool(items[2]);
5801 else if (STRCMP(items[1], "collapse_result") == 0)
5802 spin->si_collapse = sal_to_bool(items[2]);
5803 else if (STRCMP(items[1], "remove_accents") == 0)
5804 spin->si_rem_accents = sal_to_bool(items[2]);
5805 else
5806 /* when "to" is "_" it means empty */
5807 add_fromto(spin, &spin->si_sal, items[1],
5808 STRCMP(items[2], "_") == 0 ? (char_u *)""
5809 : items[2]);
5810 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005811 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005812 else if (STRCMP(items[0], "SOFOFROM") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005813 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005814 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005815 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005816 }
5817 else if (STRCMP(items[0], "SOFOTO") == 0 && itemcnt == 2
Bram Moolenaar6de68532005-08-24 22:08:48 +00005818 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005819 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005820 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005821 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005822 else if (STRCMP(items[0], "COMMON") == 0)
5823 {
5824 int i;
5825
5826 for (i = 1; i < itemcnt; ++i)
5827 {
5828 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
5829 items[i])))
5830 {
5831 p = vim_strsave(items[i]);
5832 if (p == NULL)
5833 break;
5834 hash_add(&spin->si_commonwords, p);
5835 }
5836 }
5837 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005838 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005839 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005840 fname, lnum, items[0]);
5841 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005842 }
5843
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005844 if (fol != NULL || low != NULL || upp != NULL)
5845 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005846 if (spin->si_clear_chartab)
5847 {
5848 /* Clear the char type tables, don't want to use any of the
5849 * currently used spell properties. */
5850 init_spell_chartab();
5851 spin->si_clear_chartab = FALSE;
5852 }
5853
Bram Moolenaar3982c542005-06-08 21:56:31 +00005854 /*
5855 * Don't write a word table for an ASCII file, so that we don't check
5856 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005857 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00005858 * mb_get_class(), the list of chars in the file will be incomplete.
5859 */
5860 if (!spin->si_ascii
5861#ifdef FEAT_MBYTE
5862 && !enc_utf8
5863#endif
5864 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005865 {
5866 if (fol == NULL || low == NULL || upp == NULL)
5867 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
5868 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00005869 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00005870 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005871
5872 vim_free(fol);
5873 vim_free(low);
5874 vim_free(upp);
5875 }
5876
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005877 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005878 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005879 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005880 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
Bram Moolenaar6de68532005-08-24 22:08:48 +00005881 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005882 }
5883
Bram Moolenaar6de68532005-08-24 22:08:48 +00005884 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005885 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005886 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
5887 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005888 }
5889
Bram Moolenaar6de68532005-08-24 22:08:48 +00005890 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005891 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005892 if (syllable == NULL)
5893 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
5894 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
5895 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005896 }
5897
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005898 if (compoptions != 0)
5899 {
5900 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
5901 spin->si_compoptions |= compoptions;
5902 }
5903
Bram Moolenaar6de68532005-08-24 22:08:48 +00005904 if (compflags != NULL)
5905 process_compflags(spin, aff, compflags);
5906
5907 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005908 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005909 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005910 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005911 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005912 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005913 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005914 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005915 MSG(_("Too many posponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005916 }
5917
Bram Moolenaar6de68532005-08-24 22:08:48 +00005918 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005919 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005920 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
5921 spin->si_syllable = syllable;
5922 }
5923
5924 if (sofofrom != NULL || sofoto != NULL)
5925 {
5926 if (sofofrom == NULL || sofoto == NULL)
5927 smsg((char_u *)_("Missing SOFO%s line in %s"),
5928 sofofrom == NULL ? "FROM" : "TO", fname);
5929 else if (spin->si_sal.ga_len > 0)
5930 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005931 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00005932 {
5933 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
5934 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
5935 spin->si_sofofr = sofofrom;
5936 spin->si_sofoto = sofoto;
5937 }
5938 }
5939
5940 if (midword != NULL)
5941 {
5942 aff_check_string(spin->si_midword, midword, "MIDWORD");
5943 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005944 }
5945
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005946 vim_free(pc);
5947 fclose(fd);
5948 return aff;
5949}
5950
5951/*
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005952 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
5953 * ae_flags to ae_comppermit and ae_compforbid.
5954 */
5955 static void
5956aff_process_flags(affile, entry)
5957 afffile_T *affile;
5958 affentry_T *entry;
5959{
5960 char_u *p;
5961 char_u *prevp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00005962 unsigned flag;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005963
5964 if (entry->ae_flags != NULL
5965 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
5966 {
5967 for (p = entry->ae_flags; *p != NUL; )
5968 {
5969 prevp = p;
5970 flag = get_affitem(affile->af_flagtype, &p);
5971 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
5972 {
5973 mch_memmove(prevp, p, STRLEN(p) + 1);
5974 p = prevp;
5975 if (flag == affile->af_comppermit)
5976 entry->ae_comppermit = TRUE;
5977 else
5978 entry->ae_compforbid = TRUE;
5979 }
5980 if (affile->af_flagtype == AFT_NUM && *p == ',')
5981 ++p;
5982 }
5983 if (*entry->ae_flags == NUL)
5984 entry->ae_flags = NULL; /* nothing left */
5985 }
5986}
5987
5988/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005989 * Return TRUE if "s" is the name of an info item in the affix file.
5990 */
5991 static int
5992spell_info_item(s)
5993 char_u *s;
5994{
5995 return STRCMP(s, "NAME") == 0
5996 || STRCMP(s, "HOME") == 0
5997 || STRCMP(s, "VERSION") == 0
5998 || STRCMP(s, "AUTHOR") == 0
5999 || STRCMP(s, "EMAIL") == 0
6000 || STRCMP(s, "COPYRIGHT") == 0;
6001}
6002
6003/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006004 * Turn an affix flag name into a number, according to the FLAG type.
6005 * returns zero for failure.
6006 */
6007 static unsigned
6008affitem2flag(flagtype, item, fname, lnum)
6009 int flagtype;
6010 char_u *item;
6011 char_u *fname;
6012 int lnum;
6013{
6014 unsigned res;
6015 char_u *p = item;
6016
6017 res = get_affitem(flagtype, &p);
6018 if (res == 0)
6019 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006020 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006021 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
6022 fname, lnum, item);
6023 else
6024 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
6025 fname, lnum, item);
6026 }
6027 if (*p != NUL)
6028 {
6029 smsg((char_u *)_(e_affname), fname, lnum, item);
6030 return 0;
6031 }
6032
6033 return res;
6034}
6035
6036/*
6037 * Get one affix name from "*pp" and advance the pointer.
6038 * Returns zero for an error, still advances the pointer then.
6039 */
6040 static unsigned
6041get_affitem(flagtype, pp)
6042 int flagtype;
6043 char_u **pp;
6044{
6045 int res;
6046
Bram Moolenaar95529562005-08-25 21:21:38 +00006047 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006048 {
6049 if (!VIM_ISDIGIT(**pp))
6050 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006051 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006052 return 0;
6053 }
6054 res = getdigits(pp);
6055 }
6056 else
6057 {
6058#ifdef FEAT_MBYTE
6059 res = mb_ptr2char_adv(pp);
6060#else
6061 res = *(*pp)++;
6062#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006063 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00006064 && res >= 'A' && res <= 'Z'))
6065 {
6066 if (**pp == NUL)
6067 return 0;
6068#ifdef FEAT_MBYTE
6069 res = mb_ptr2char_adv(pp) + (res << 16);
6070#else
6071 res = *(*pp)++ + (res << 16);
6072#endif
6073 }
6074 }
6075 return res;
6076}
6077
6078/*
6079 * Process the "compflags" string used in an affix file and append it to
6080 * spin->si_compflags.
6081 * The processing involves changing the affix names to ID numbers, so that
6082 * they fit in one byte.
6083 */
6084 static void
6085process_compflags(spin, aff, compflags)
6086 spellinfo_T *spin;
6087 afffile_T *aff;
6088 char_u *compflags;
6089{
6090 char_u *p;
6091 char_u *prevp;
6092 unsigned flag;
6093 compitem_T *ci;
6094 int id;
6095 int len;
6096 char_u *tp;
6097 char_u key[AH_KEY_LEN];
6098 hashitem_T *hi;
6099
6100 /* Make room for the old and the new compflags, concatenated with a / in
6101 * between. Processing it makes it shorter, but we don't know by how
6102 * much, thus allocate the maximum. */
6103 len = STRLEN(compflags) + 1;
6104 if (spin->si_compflags != NULL)
6105 len += STRLEN(spin->si_compflags) + 1;
6106 p = getroom(spin, len, FALSE);
6107 if (p == NULL)
6108 return;
6109 if (spin->si_compflags != NULL)
6110 {
6111 STRCPY(p, spin->si_compflags);
6112 STRCAT(p, "/");
6113 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00006114 spin->si_compflags = p;
6115 tp = p + STRLEN(p);
6116
6117 for (p = compflags; *p != NUL; )
6118 {
6119 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
6120 /* Copy non-flag characters directly. */
6121 *tp++ = *p++;
6122 else
6123 {
6124 /* First get the flag number, also checks validity. */
6125 prevp = p;
6126 flag = get_affitem(aff->af_flagtype, &p);
6127 if (flag != 0)
6128 {
6129 /* Find the flag in the hashtable. If it was used before, use
6130 * the existing ID. Otherwise add a new entry. */
6131 vim_strncpy(key, prevp, p - prevp);
6132 hi = hash_find(&aff->af_comp, key);
6133 if (!HASHITEM_EMPTY(hi))
6134 id = HI2CI(hi)->ci_newID;
6135 else
6136 {
6137 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
6138 if (ci == NULL)
6139 break;
6140 STRCPY(ci->ci_key, key);
6141 ci->ci_flag = flag;
6142 /* Avoid using a flag ID that has a special meaning in a
6143 * regexp (also inside []). */
6144 do
6145 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006146 check_renumber(spin);
6147 id = spin->si_newcompID--;
6148 } while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006149 ci->ci_newID = id;
6150 hash_add(&aff->af_comp, ci->ci_key);
6151 }
6152 *tp++ = id;
6153 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006154 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006155 ++p;
6156 }
6157 }
6158
6159 *tp = NUL;
6160}
6161
6162/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006163 * Check that the new IDs for postponed affixes and compounding don't overrun
6164 * each other. We have almost 255 available, but start at 0-127 to avoid
6165 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
6166 * When that is used up an error message is given.
6167 */
6168 static void
6169check_renumber(spin)
6170 spellinfo_T *spin;
6171{
6172 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
6173 {
6174 spin->si_newprefID = 127;
6175 spin->si_newcompID = 255;
6176 }
6177}
6178
6179/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006180 * Return TRUE if flag "flag" appears in affix list "afflist".
6181 */
6182 static int
6183flag_in_afflist(flagtype, afflist, flag)
6184 int flagtype;
6185 char_u *afflist;
6186 unsigned flag;
6187{
6188 char_u *p;
6189 unsigned n;
6190
6191 switch (flagtype)
6192 {
6193 case AFT_CHAR:
6194 return vim_strchr(afflist, flag) != NULL;
6195
Bram Moolenaar95529562005-08-25 21:21:38 +00006196 case AFT_CAPLONG:
6197 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006198 for (p = afflist; *p != NUL; )
6199 {
6200#ifdef FEAT_MBYTE
6201 n = mb_ptr2char_adv(&p);
6202#else
6203 n = *p++;
6204#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006205 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00006206 && *p != NUL)
6207#ifdef FEAT_MBYTE
6208 n = mb_ptr2char_adv(&p) + (n << 16);
6209#else
6210 n = *p++ + (n << 16);
6211#endif
6212 if (n == flag)
6213 return TRUE;
6214 }
6215 break;
6216
Bram Moolenaar95529562005-08-25 21:21:38 +00006217 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006218 for (p = afflist; *p != NUL; )
6219 {
6220 n = getdigits(&p);
6221 if (n == flag)
6222 return TRUE;
6223 if (*p != NUL) /* skip over comma */
6224 ++p;
6225 }
6226 break;
6227 }
6228 return FALSE;
6229}
6230
6231/*
6232 * Give a warning when "spinval" and "affval" numbers are set and not the same.
6233 */
6234 static void
6235aff_check_number(spinval, affval, name)
6236 int spinval;
6237 int affval;
6238 char *name;
6239{
6240 if (spinval != 0 && spinval != affval)
6241 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6242}
6243
6244/*
6245 * Give a warning when "spinval" and "affval" strings are set and not the same.
6246 */
6247 static void
6248aff_check_string(spinval, affval, name)
6249 char_u *spinval;
6250 char_u *affval;
6251 char *name;
6252{
6253 if (spinval != NULL && STRCMP(spinval, affval) != 0)
6254 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6255}
6256
6257/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006258 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
6259 * NULL as equal.
6260 */
6261 static int
6262str_equal(s1, s2)
6263 char_u *s1;
6264 char_u *s2;
6265{
6266 if (s1 == NULL || s2 == NULL)
6267 return s1 == s2;
6268 return STRCMP(s1, s2) == 0;
6269}
6270
6271/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006272 * Add a from-to item to "gap". Used for REP and SAL items.
6273 * They are stored case-folded.
6274 */
6275 static void
6276add_fromto(spin, gap, from, to)
6277 spellinfo_T *spin;
6278 garray_T *gap;
6279 char_u *from;
6280 char_u *to;
6281{
6282 fromto_T *ftp;
6283 char_u word[MAXWLEN];
6284
6285 if (ga_grow(gap, 1) == OK)
6286 {
6287 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
6288 (void)spell_casefold(from, STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006289 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006290 (void)spell_casefold(to, STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006291 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006292 ++gap->ga_len;
6293 }
6294}
6295
6296/*
6297 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6298 */
6299 static int
6300sal_to_bool(s)
6301 char_u *s;
6302{
6303 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
6304}
6305
6306/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00006307 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6308 * When "s" is NULL FALSE is returned.
6309 */
6310 static int
6311has_non_ascii(s)
6312 char_u *s;
6313{
6314 char_u *p;
6315
6316 if (s != NULL)
6317 for (p = s; *p != NUL; ++p)
6318 if (*p >= 128)
6319 return TRUE;
6320 return FALSE;
6321}
6322
6323/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006324 * Free the structure filled by spell_read_aff().
6325 */
6326 static void
6327spell_free_aff(aff)
6328 afffile_T *aff;
6329{
6330 hashtab_T *ht;
6331 hashitem_T *hi;
6332 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006333 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006334 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006335
6336 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006337
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006338 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006339 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
6340 {
6341 todo = ht->ht_used;
6342 for (hi = ht->ht_array; todo > 0; ++hi)
6343 {
6344 if (!HASHITEM_EMPTY(hi))
6345 {
6346 --todo;
6347 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006348 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
6349 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006350 }
6351 }
6352 if (ht == &aff->af_suff)
6353 break;
6354 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006355
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006356 hash_clear(&aff->af_pref);
6357 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006358 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006359}
6360
6361/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006362 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006363 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006364 */
6365 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006366spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006367 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006368 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006369 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006370{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006371 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006372 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006373 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006374 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006375 char_u store_afflist[MAXWLEN];
6376 int pfxlen;
6377 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006378 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006379 char_u *pc;
6380 char_u *w;
6381 int l;
6382 hash_T hash;
6383 hashitem_T *hi;
6384 FILE *fd;
6385 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006386 int non_ascii = 0;
6387 int retval = OK;
6388 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006389 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006390 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006391
Bram Moolenaar51485f02005-06-04 21:55:20 +00006392 /*
6393 * Open the file.
6394 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006395 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006396 if (fd == NULL)
6397 {
6398 EMSG2(_(e_notopen), fname);
6399 return FAIL;
6400 }
6401
Bram Moolenaar51485f02005-06-04 21:55:20 +00006402 /* The hashtable is only used to detect duplicated words. */
6403 hash_init(&ht);
6404
Bram Moolenaar4770d092006-01-12 23:22:24 +00006405 vim_snprintf((char *)IObuff, IOSIZE,
6406 _("Reading dictionary file %s ..."), fname);
6407 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006408
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006409 /* start with a message for the first line */
6410 spin->si_msg_count = 999999;
6411
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006412 /* Read and ignore the first line: word count. */
6413 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006414 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006415 EMSG2(_("E760: No word count in %s"), fname);
6416
6417 /*
6418 * Read all the lines in the file one by one.
6419 * The words are converted to 'encoding' here, before being added to
6420 * the hashtable.
6421 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006422 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006423 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006424 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006425 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00006426 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006427 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006428
Bram Moolenaar51485f02005-06-04 21:55:20 +00006429 /* Remove CR, LF and white space from the end. White space halfway
6430 * the word is kept to allow e.g., "et al.". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006431 l = STRLEN(line);
6432 while (l > 0 && line[l - 1] <= ' ')
6433 --l;
6434 if (l == 0)
6435 continue; /* empty line */
6436 line[l] = NUL;
6437
Bram Moolenaarb765d632005-06-07 21:00:02 +00006438#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006439 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006440 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006441 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006442 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006443 if (pc == NULL)
6444 {
6445 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6446 fname, lnum, line);
6447 continue;
6448 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006449 w = pc;
6450 }
6451 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006452#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006453 {
6454 pc = NULL;
6455 w = line;
6456 }
6457
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006458 /* Truncate the word at the "/", set "afflist" to what follows.
6459 * Replace "\/" by "/" and "\\" by "\". */
6460 afflist = NULL;
6461 for (p = w; *p != NUL; mb_ptr_adv(p))
6462 {
6463 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
6464 mch_memmove(p, p + 1, STRLEN(p));
6465 else if (*p == '/')
6466 {
6467 *p = NUL;
6468 afflist = p + 1;
6469 break;
6470 }
6471 }
6472
6473 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6474 if (spin->si_ascii && has_non_ascii(w))
6475 {
6476 ++non_ascii;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006477 vim_free(pc);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006478 continue;
6479 }
6480
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006481 /* This takes time, print a message every 10000 words. */
6482 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006483 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006484 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006485 vim_snprintf((char *)message, sizeof(message),
6486 _("line %6d, word %6d - %s"),
6487 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6488 msg_start();
6489 msg_puts_long_attr(message, 0);
6490 msg_clr_eos();
6491 msg_didout = FALSE;
6492 msg_col = 0;
6493 out_flush();
6494 }
6495
Bram Moolenaar51485f02005-06-04 21:55:20 +00006496 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006497 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006498 if (dw == NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006499 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006500 retval = FAIL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006501 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006502 break;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006503 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006504
Bram Moolenaar51485f02005-06-04 21:55:20 +00006505 hash = hash_hash(dw);
6506 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006507 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006508 {
6509 if (p_verbose > 0)
6510 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006511 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006512 else if (duplicate == 0)
6513 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6514 fname, lnum, dw);
6515 ++duplicate;
6516 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006517 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006518 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006519
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006520 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006521 store_afflist[0] = NUL;
6522 pfxlen = 0;
6523 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006524 if (afflist != NULL)
6525 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006526 /* Extract flags from the affix list. */
6527 flags |= get_affix_flags(affile, afflist);
6528
Bram Moolenaar6de68532005-08-24 22:08:48 +00006529 if (affile->af_needaffix != 0 && flag_in_afflist(
6530 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006531 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006532
6533 if (affile->af_pfxpostpone)
6534 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006535 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006536
Bram Moolenaar5195e452005-08-19 20:32:47 +00006537 if (spin->si_compflags != NULL)
6538 /* Need to store the list of compound flags with the word.
6539 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006540 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006541 }
6542
Bram Moolenaar51485f02005-06-04 21:55:20 +00006543 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006544 if (store_word(spin, dw, flags, spin->si_region,
6545 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006546 retval = FAIL;
6547
6548 if (afflist != NULL)
6549 {
6550 /* Find all matching suffixes and add the resulting words.
6551 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006552 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006553 &affile->af_suff, &affile->af_pref,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006554 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006555 retval = FAIL;
6556
6557 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006558 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006559 &affile->af_pref, NULL,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006560 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006561 retval = FAIL;
6562 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006563
6564 vim_free(pc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006565 }
6566
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006567 if (duplicate > 0)
6568 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006569 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006570 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6571 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006572 hash_clear(&ht);
6573
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006574 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006575 return retval;
6576}
6577
6578/*
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006579 * Check for affix flags in "afflist" that are turned into word flags.
6580 * Return WF_ flags.
6581 */
6582 static int
6583get_affix_flags(affile, afflist)
6584 afffile_T *affile;
6585 char_u *afflist;
6586{
6587 int flags = 0;
6588
6589 if (affile->af_keepcase != 0 && flag_in_afflist(
6590 affile->af_flagtype, afflist, affile->af_keepcase))
6591 flags |= WF_KEEPCAP | WF_FIXCAP;
6592 if (affile->af_rare != 0 && flag_in_afflist(
6593 affile->af_flagtype, afflist, affile->af_rare))
6594 flags |= WF_RARE;
6595 if (affile->af_bad != 0 && flag_in_afflist(
6596 affile->af_flagtype, afflist, affile->af_bad))
6597 flags |= WF_BANNED;
6598 if (affile->af_needcomp != 0 && flag_in_afflist(
6599 affile->af_flagtype, afflist, affile->af_needcomp))
6600 flags |= WF_NEEDCOMP;
6601 if (affile->af_comproot != 0 && flag_in_afflist(
6602 affile->af_flagtype, afflist, affile->af_comproot))
6603 flags |= WF_COMPROOT;
6604 if (affile->af_nosuggest != 0 && flag_in_afflist(
6605 affile->af_flagtype, afflist, affile->af_nosuggest))
6606 flags |= WF_NOSUGGEST;
6607 return flags;
6608}
6609
6610/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006611 * Get the list of prefix IDs from the affix list "afflist".
6612 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006613 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6614 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006615 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006616 static int
6617get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006618 afffile_T *affile;
6619 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006620 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006621{
6622 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006623 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006624 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006625 int id;
6626 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006627 hashitem_T *hi;
6628
Bram Moolenaar6de68532005-08-24 22:08:48 +00006629 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006630 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006631 prevp = p;
6632 if (get_affitem(affile->af_flagtype, &p) != 0)
6633 {
6634 /* A flag is a postponed prefix flag if it appears in "af_pref"
6635 * and it's ID is not zero. */
6636 vim_strncpy(key, prevp, p - prevp);
6637 hi = hash_find(&affile->af_pref, key);
6638 if (!HASHITEM_EMPTY(hi))
6639 {
6640 id = HI2AH(hi)->ah_newID;
6641 if (id != 0)
6642 store_afflist[cnt++] = id;
6643 }
6644 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006645 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006646 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006647 }
6648
Bram Moolenaar5195e452005-08-19 20:32:47 +00006649 store_afflist[cnt] = NUL;
6650 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006651}
6652
6653/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006654 * Get the list of compound IDs from the affix list "afflist" that are used
6655 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006656 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006657 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006658 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00006659get_compflags(affile, afflist, store_afflist)
6660 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006661 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006662 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006663{
6664 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006665 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006666 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006667 char_u key[AH_KEY_LEN];
6668 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006669
Bram Moolenaar6de68532005-08-24 22:08:48 +00006670 for (p = afflist; *p != NUL; )
6671 {
6672 prevp = p;
6673 if (get_affitem(affile->af_flagtype, &p) != 0)
6674 {
6675 /* A flag is a compound flag if it appears in "af_comp". */
6676 vim_strncpy(key, prevp, p - prevp);
6677 hi = hash_find(&affile->af_comp, key);
6678 if (!HASHITEM_EMPTY(hi))
6679 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6680 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006681 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006682 ++p;
6683 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006684
Bram Moolenaar5195e452005-08-19 20:32:47 +00006685 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006686}
6687
6688/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006689 * Apply affixes to a word and store the resulting words.
6690 * "ht" is the hashtable with affentry_T that need to be applied, either
6691 * prefixes or suffixes.
6692 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6693 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006694 *
6695 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006696 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006697 static int
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006698store_aff_word(spin, word, afflist, affile, ht, xht, condit, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006699 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006700 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006701 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006702 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006703 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006704 hashtab_T *ht;
6705 hashtab_T *xht;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006706 int condit; /* CONDIT_SUF et al. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006707 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006708 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006709 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
6710 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006711{
6712 int todo;
6713 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006714 affheader_T *ah;
6715 affentry_T *ae;
6716 regmatch_T regmatch;
6717 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006718 int retval = OK;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006719 int i, j;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006720 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006721 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006722 char_u *use_pfxlist;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006723 int use_pfxlen;
6724 int need_affix;
6725 char_u store_afflist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006726 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006727 size_t wordlen = STRLEN(word);
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006728 int use_condit;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006729
Bram Moolenaar51485f02005-06-04 21:55:20 +00006730 todo = ht->ht_used;
6731 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006732 {
6733 if (!HASHITEM_EMPTY(hi))
6734 {
6735 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006736 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006737
Bram Moolenaar51485f02005-06-04 21:55:20 +00006738 /* Check that the affix combines, if required, and that the word
6739 * supports this affix. */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006740 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
6741 && flag_in_afflist(affile->af_flagtype, afflist,
6742 ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006743 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006744 /* Loop over all affix entries with this name. */
6745 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006746 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006747 /* Check the condition. It's not logical to match case
6748 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006749 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006750 * Another requirement from Myspell is that the chop
6751 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006752 * For prefixes, when "PFXPOSTPONE" was used, only do
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006753 * prefixes with a chop string and/or flags.
6754 * When a previously added affix had CIRCUMFIX this one
6755 * must have it too, if it had not then this one must not
6756 * have one either. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006757 regmatch.regprog = ae->ae_prog;
6758 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006759 if ((xht != NULL || !affile->af_pfxpostpone
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006760 || ae->ae_chop != NULL
6761 || ae->ae_flags != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006762 && (ae->ae_chop == NULL
6763 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006764 && (ae->ae_prog == NULL
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006765 || vim_regexec(&regmatch, word, (colnr_T)0))
6766 && (((condit & CONDIT_CFIX) == 0)
6767 == ((condit & CONDIT_AFF) == 0
6768 || ae->ae_flags == NULL
6769 || !flag_in_afflist(affile->af_flagtype,
6770 ae->ae_flags, affile->af_circumfix))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006771 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006772 /* Match. Remove the chop and add the affix. */
6773 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006774 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006775 /* prefix: chop/add at the start of the word */
6776 if (ae->ae_add == NULL)
6777 *newword = NUL;
6778 else
6779 STRCPY(newword, ae->ae_add);
6780 p = word;
6781 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006782 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006783 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006784#ifdef FEAT_MBYTE
6785 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006786 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006787 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006788 for ( ; i > 0; --i)
6789 mb_ptr_adv(p);
6790 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006791 else
6792#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006793 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006794 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006795 STRCAT(newword, p);
6796 }
6797 else
6798 {
6799 /* suffix: chop/add at the end of the word */
6800 STRCPY(newword, word);
6801 if (ae->ae_chop != NULL)
6802 {
6803 /* Remove chop string. */
6804 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006805 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006806 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006807 mb_ptr_back(newword, p);
6808 *p = NUL;
6809 }
6810 if (ae->ae_add != NULL)
6811 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006812 }
6813
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006814 use_flags = flags;
6815 use_pfxlist = pfxlist;
6816 use_pfxlen = pfxlen;
6817 need_affix = FALSE;
6818 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
6819 if (ae->ae_flags != NULL)
6820 {
6821 /* Extract flags from the affix list. */
6822 use_flags |= get_affix_flags(affile, ae->ae_flags);
6823
6824 if (affile->af_needaffix != 0 && flag_in_afflist(
6825 affile->af_flagtype, ae->ae_flags,
6826 affile->af_needaffix))
6827 need_affix = TRUE;
6828
6829 /* When there is a CIRCUMFIX flag the other affix
6830 * must also have it and we don't add the word
6831 * with one affix. */
6832 if (affile->af_circumfix != 0 && flag_in_afflist(
6833 affile->af_flagtype, ae->ae_flags,
6834 affile->af_circumfix))
6835 {
6836 use_condit |= CONDIT_CFIX;
6837 if ((condit & CONDIT_CFIX) == 0)
6838 need_affix = TRUE;
6839 }
6840
6841 if (affile->af_pfxpostpone
6842 || spin->si_compflags != NULL)
6843 {
6844 if (affile->af_pfxpostpone)
6845 /* Get prefix IDS from the affix list. */
6846 use_pfxlen = get_pfxlist(affile,
6847 ae->ae_flags, store_afflist);
6848 else
6849 use_pfxlen = 0;
6850 use_pfxlist = store_afflist;
6851
6852 /* Combine the prefix IDs. Avoid adding the
6853 * same ID twice. */
6854 for (i = 0; i < pfxlen; ++i)
6855 {
6856 for (j = 0; j < use_pfxlen; ++j)
6857 if (pfxlist[i] == use_pfxlist[j])
6858 break;
6859 if (j == use_pfxlen)
6860 use_pfxlist[use_pfxlen++] = pfxlist[i];
6861 }
6862
6863 if (spin->si_compflags != NULL)
6864 /* Get compound IDS from the affix list. */
6865 get_compflags(affile, ae->ae_flags,
6866 use_pfxlist + use_pfxlen);
6867
6868 /* Combine the list of compound flags.
6869 * Concatenate them to the prefix IDs list.
6870 * Avoid adding the same ID twice. */
6871 for (i = pfxlen; pfxlist[i] != NUL; ++i)
6872 {
6873 for (j = use_pfxlen;
6874 use_pfxlist[j] != NUL; ++j)
6875 if (pfxlist[i] == use_pfxlist[j])
6876 break;
6877 if (use_pfxlist[j] == NUL)
6878 {
6879 use_pfxlist[j++] = pfxlist[i];
6880 use_pfxlist[j] = NUL;
6881 }
6882 }
6883 }
6884 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00006885
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006886 /* Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006887 * use the compound flags. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006888 if (use_pfxlist != NULL && ae->ae_compforbid)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006889 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006890 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006891 use_pfxlist = pfx_pfxlist;
6892 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006893
6894 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00006895 if (spin->si_prefroot != NULL
6896 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006897 {
6898 /* ... add a flag to indicate an affix was used. */
6899 use_flags |= WF_HAS_AFF;
6900
6901 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00006902 * affixes is not allowed. But do use the
6903 * compound flags after them. */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006904 if ((!ah->ah_combine || (condit & CONDIT_COMB))
6905 && use_pfxlist != NULL)
6906 use_pfxlist += use_pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006907 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006908
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006909 /* When compounding is supported and there is no
6910 * "COMPOUNDPERMITFLAG" then forbid compounding on the
6911 * side where the affix is applied. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006912 if (spin->si_compflags != NULL && !ae->ae_comppermit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006913 {
6914 if (xht != NULL)
6915 use_flags |= WF_NOCOMPAFT;
6916 else
6917 use_flags |= WF_NOCOMPBEF;
6918 }
6919
Bram Moolenaar51485f02005-06-04 21:55:20 +00006920 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006921 if (store_word(spin, newword, use_flags,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006922 spin->si_region, use_pfxlist,
6923 need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006924 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006925
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006926 /* When added a prefix or a first suffix and the affix
6927 * has flags may add a(nother) suffix. RECURSIVE! */
6928 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
6929 if (store_aff_word(spin, newword, ae->ae_flags,
6930 affile, &affile->af_suff, xht,
6931 use_condit & (xht == NULL
6932 ? ~0 : ~CONDIT_SUF),
Bram Moolenaar5195e452005-08-19 20:32:47 +00006933 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006934 retval = FAIL;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006935
6936 /* When added a suffix and combining is allowed also
6937 * try adding a prefix additionally. Both for the
6938 * word flags and for the affix flags. RECURSIVE! */
6939 if (xht != NULL && ah->ah_combine)
6940 {
6941 if (store_aff_word(spin, newword,
6942 afflist, affile,
6943 xht, NULL, use_condit,
6944 use_flags, use_pfxlist,
6945 pfxlen) == FAIL
6946 || (ae->ae_flags != NULL
6947 && store_aff_word(spin, newword,
6948 ae->ae_flags, affile,
6949 xht, NULL, use_condit,
6950 use_flags, use_pfxlist,
6951 pfxlen) == FAIL))
6952 retval = FAIL;
6953 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006954 }
6955 }
6956 }
6957 }
6958 }
6959
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006960 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006961}
6962
6963/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006964 * Read a file with a list of words.
6965 */
6966 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006967spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006968 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006969 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006970{
6971 FILE *fd;
6972 long lnum = 0;
6973 char_u rline[MAXLINELEN];
6974 char_u *line;
6975 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006976 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006977 int l;
6978 int retval = OK;
6979 int did_word = FALSE;
6980 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006981 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006982 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006983
6984 /*
6985 * Open the file.
6986 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006987 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006988 if (fd == NULL)
6989 {
6990 EMSG2(_(e_notopen), fname);
6991 return FAIL;
6992 }
6993
Bram Moolenaar4770d092006-01-12 23:22:24 +00006994 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
6995 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006996
6997 /*
6998 * Read all the lines in the file one by one.
6999 */
7000 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
7001 {
7002 line_breakcheck();
7003 ++lnum;
7004
7005 /* Skip comment lines. */
7006 if (*rline == '#')
7007 continue;
7008
7009 /* Remove CR, LF and white space from the end. */
7010 l = STRLEN(rline);
7011 while (l > 0 && rline[l - 1] <= ' ')
7012 --l;
7013 if (l == 0)
7014 continue; /* empty or blank line */
7015 rline[l] = NUL;
7016
7017 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
7018 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007019#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00007020 if (spin->si_conv.vc_type != CONV_NONE)
7021 {
7022 pc = string_convert(&spin->si_conv, rline, NULL);
7023 if (pc == NULL)
7024 {
7025 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
7026 fname, lnum, rline);
7027 continue;
7028 }
7029 line = pc;
7030 }
7031 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00007032#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007033 {
7034 pc = NULL;
7035 line = rline;
7036 }
7037
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007038 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00007039 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007040 ++line;
7041 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007042 {
7043 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007044 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
7045 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007046 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007047 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
7048 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007049 else
7050 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007051#ifdef FEAT_MBYTE
7052 char_u *enc;
7053
Bram Moolenaar51485f02005-06-04 21:55:20 +00007054 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007055 line += 10;
7056 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007057 if (enc != NULL && !spin->si_ascii
7058 && convert_setup(&spin->si_conv, enc,
7059 p_enc) == FAIL)
7060 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00007061 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007062 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007063 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007064#else
7065 smsg((char_u *)_("Conversion in %s not supported"), fname);
7066#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007067 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007068 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007069 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007070
Bram Moolenaar3982c542005-06-08 21:56:31 +00007071 if (STRNCMP(line, "regions=", 8) == 0)
7072 {
7073 if (spin->si_region_count > 1)
7074 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
7075 fname, lnum, line);
7076 else
7077 {
7078 line += 8;
7079 if (STRLEN(line) > 16)
7080 smsg((char_u *)_("Too many regions in %s line %d: %s"),
7081 fname, lnum, line);
7082 else
7083 {
7084 spin->si_region_count = STRLEN(line) / 2;
7085 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007086
7087 /* Adjust the mask for a word valid in all regions. */
7088 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007089 }
7090 }
7091 continue;
7092 }
7093
Bram Moolenaar7887d882005-07-01 22:33:52 +00007094 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
7095 fname, lnum, line - 1);
7096 continue;
7097 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007098
Bram Moolenaar7887d882005-07-01 22:33:52 +00007099 flags = 0;
7100 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007101
Bram Moolenaar7887d882005-07-01 22:33:52 +00007102 /* Check for flags and region after a slash. */
7103 p = vim_strchr(line, '/');
7104 if (p != NULL)
7105 {
7106 *p++ = NUL;
7107 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007108 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007109 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007110 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007111 else if (*p == '!') /* Bad, bad, wicked word. */
7112 flags |= WF_BANNED;
7113 else if (*p == '?') /* Rare word. */
7114 flags |= WF_RARE;
7115 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007116 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007117 if ((flags & WF_REGION) == 0) /* first one */
7118 regionmask = 0;
7119 flags |= WF_REGION;
7120
7121 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00007122 if (l > spin->si_region_count)
7123 {
7124 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00007125 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007126 break;
7127 }
7128 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007129 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007130 else
7131 {
7132 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
7133 fname, lnum, p);
7134 break;
7135 }
7136 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007137 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007138 }
7139
7140 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
7141 if (spin->si_ascii && has_non_ascii(line))
7142 {
7143 ++non_ascii;
7144 continue;
7145 }
7146
7147 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007148 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007149 {
7150 retval = FAIL;
7151 break;
7152 }
7153 did_word = TRUE;
7154 }
7155
7156 vim_free(pc);
7157 fclose(fd);
7158
Bram Moolenaar4770d092006-01-12 23:22:24 +00007159 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007160 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007161 vim_snprintf((char *)IObuff, IOSIZE,
7162 _("Ignored %d words with non-ASCII characters"), non_ascii);
7163 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007164 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007165
Bram Moolenaar51485f02005-06-04 21:55:20 +00007166 return retval;
7167}
7168
7169/*
7170 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007171 * This avoids calling free() for every little struct we use (and keeping
7172 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00007173 * The memory is cleared to all zeros.
7174 * Returns NULL when out of memory.
7175 */
7176 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007177getroom(spin, len, align)
7178 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007179 size_t len; /* length needed */
7180 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007181{
7182 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007183 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007184
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007185 if (align && bl != NULL)
7186 /* Round size up for alignment. On some systems structures need to be
7187 * aligned to the size of a pointer (e.g., SPARC). */
7188 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
7189 & ~(sizeof(char *) - 1);
7190
Bram Moolenaar51485f02005-06-04 21:55:20 +00007191 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
7192 {
7193 /* Allocate a block of memory. This is not freed until much later. */
7194 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
7195 if (bl == NULL)
7196 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007197 bl->sb_next = spin->si_blocks;
7198 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007199 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007200 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007201 }
7202
7203 p = bl->sb_data + bl->sb_used;
7204 bl->sb_used += len;
7205
7206 return p;
7207}
7208
7209/*
7210 * Make a copy of a string into memory allocated with getroom().
7211 */
7212 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007213getroom_save(spin, s)
7214 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007215 char_u *s;
7216{
7217 char_u *sc;
7218
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007219 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007220 if (sc != NULL)
7221 STRCPY(sc, s);
7222 return sc;
7223}
7224
7225
7226/*
7227 * Free the list of allocated sblock_T.
7228 */
7229 static void
7230free_blocks(bl)
7231 sblock_T *bl;
7232{
7233 sblock_T *next;
7234
7235 while (bl != NULL)
7236 {
7237 next = bl->sb_next;
7238 vim_free(bl);
7239 bl = next;
7240 }
7241}
7242
7243/*
7244 * Allocate the root of a word tree.
7245 */
7246 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007247wordtree_alloc(spin)
7248 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007249{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007250 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007251}
7252
7253/*
7254 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007255 * Always store it in the case-folded tree. For a keep-case word this is
7256 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
7257 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007258 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007259 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
7260 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007261 */
7262 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00007263store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007264 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007265 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007266 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007267 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007268 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007269 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007270{
7271 int len = STRLEN(word);
7272 int ct = captype(word, word + len);
7273 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007274 int res = OK;
7275 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007276
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007277 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007278 for (p = pfxlist; res == OK; ++p)
7279 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007280 if (!need_affix || (p != NULL && *p != NUL))
7281 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007282 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007283 if (p == NULL || *p == NUL)
7284 break;
7285 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007286 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007287
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007288 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00007289 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007290 for (p = pfxlist; res == OK; ++p)
7291 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007292 if (!need_affix || (p != NULL && *p != NUL))
7293 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007294 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007295 if (p == NULL || *p == NUL)
7296 break;
7297 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007298 ++spin->si_keepwcount;
7299 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007300 return res;
7301}
7302
7303/*
7304 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00007305 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007306 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007307 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007308 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007309 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007310tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007311 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007312 char_u *word;
7313 wordnode_T *root;
7314 int flags;
7315 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007316 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007317{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007318 wordnode_T *node = root;
7319 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007320 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007321 wordnode_T **prev = NULL;
7322 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007323
Bram Moolenaar51485f02005-06-04 21:55:20 +00007324 /* Add each byte of the word to the tree, including the NUL at the end. */
7325 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007326 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007327 /* When there is more than one reference to this node we need to make
7328 * a copy, so that we can modify it. Copy the whole list of siblings
7329 * (we don't optimize for a partly shared list of siblings). */
7330 if (node != NULL && node->wn_refs > 1)
7331 {
7332 --node->wn_refs;
7333 copyprev = prev;
7334 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
7335 {
7336 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007337 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007338 if (np == NULL)
7339 return FAIL;
7340 np->wn_child = copyp->wn_child;
7341 if (np->wn_child != NULL)
7342 ++np->wn_child->wn_refs; /* child gets extra ref */
7343 np->wn_byte = copyp->wn_byte;
7344 if (np->wn_byte == NUL)
7345 {
7346 np->wn_flags = copyp->wn_flags;
7347 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007348 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007349 }
7350
7351 /* Link the new node in the list, there will be one ref. */
7352 np->wn_refs = 1;
7353 *copyprev = np;
7354 copyprev = &np->wn_sibling;
7355
7356 /* Let "node" point to the head of the copied list. */
7357 if (copyp == node)
7358 node = np;
7359 }
7360 }
7361
Bram Moolenaar51485f02005-06-04 21:55:20 +00007362 /* Look for the sibling that has the same character. They are sorted
7363 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007364 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007365 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007366 while (node != NULL
7367 && (node->wn_byte < word[i]
7368 || (node->wn_byte == NUL
7369 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007370 ? node->wn_affixID < (unsigned)affixID
7371 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007372 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007373 && (spin->si_sugtree
7374 ? (node->wn_region & 0xffff) < region
7375 : node->wn_affixID
7376 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007377 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007378 prev = &node->wn_sibling;
7379 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007380 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007381 if (node == NULL
7382 || node->wn_byte != word[i]
7383 || (word[i] == NUL
7384 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007385 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007386 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007387 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007388 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007389 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007390 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007391 if (np == NULL)
7392 return FAIL;
7393 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007394
7395 /* If "node" is NULL this is a new child or the end of the sibling
7396 * list: ref count is one. Otherwise use ref count of sibling and
7397 * make ref count of sibling one (matters when inserting in front
7398 * of the list of siblings). */
7399 if (node == NULL)
7400 np->wn_refs = 1;
7401 else
7402 {
7403 np->wn_refs = node->wn_refs;
7404 node->wn_refs = 1;
7405 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007406 *prev = np;
7407 np->wn_sibling = node;
7408 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007409 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007410
Bram Moolenaar51485f02005-06-04 21:55:20 +00007411 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007412 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007413 node->wn_flags = flags;
7414 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007415 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007416 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00007417 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007418 prev = &node->wn_child;
7419 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007420 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007421#ifdef SPELL_PRINTTREE
7422 smsg("Added \"%s\"", word);
7423 spell_print_tree(root->wn_sibling);
7424#endif
7425
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007426 /* count nr of words added since last message */
7427 ++spin->si_msg_count;
7428
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007429 if (spin->si_compress_cnt > 1)
7430 {
7431 if (--spin->si_compress_cnt == 1)
7432 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007433 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007434 }
7435
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007436 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007437 * When we have allocated lots of memory we need to compress the word tree
7438 * to free up some room. But compression is slow, and we might actually
7439 * need that room, thus only compress in the following situations:
7440 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00007441 * "compress_start" blocks.
7442 * 2. When compressed before and used "compress_inc" blocks before
7443 * adding "compress_added" words (si_compress_cnt > 1).
7444 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007445 * (si_compress_cnt == 1) and the number of free nodes drops below the
7446 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007447 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007448#ifndef SPELL_PRINTTREE
7449 if (spin->si_compress_cnt == 1
7450 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00007451 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007452#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007453 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007454 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00007455 * when the freed up room has been used and another "compress_inc"
7456 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007457 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007458 spin->si_blocks_cnt -= compress_inc;
7459 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007460
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007461 if (spin->si_verbose)
7462 {
7463 msg_start();
7464 msg_puts((char_u *)_(msg_compressing));
7465 msg_clr_eos();
7466 msg_didout = FALSE;
7467 msg_col = 0;
7468 out_flush();
7469 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007470
7471 /* Compress both trees. Either they both have many nodes, which makes
7472 * compression useful, or one of them is small, which means
Bram Moolenaar4770d092006-01-12 23:22:24 +00007473 * compression goes fast. But when filling the souldfold word tree
7474 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007475 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007476 if (affixID >= 0)
7477 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007478 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007479
7480 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007481}
7482
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007483/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007484 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7485 * Sets "sps_flags".
7486 */
7487 int
7488spell_check_msm()
7489{
7490 char_u *p = p_msm;
7491 long start = 0;
7492 long inc = 0;
7493 long added = 0;
7494
7495 if (!VIM_ISDIGIT(*p))
7496 return FAIL;
7497 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7498 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7499 if (*p != ',')
7500 return FAIL;
7501 ++p;
7502 if (!VIM_ISDIGIT(*p))
7503 return FAIL;
7504 inc = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
7505 if (*p != ',')
7506 return FAIL;
7507 ++p;
7508 if (!VIM_ISDIGIT(*p))
7509 return FAIL;
7510 added = getdigits(&p) * 1024;
7511 if (*p != NUL)
7512 return FAIL;
7513
7514 if (start == 0 || inc == 0 || added == 0 || inc > start)
7515 return FAIL;
7516
7517 compress_start = start;
7518 compress_inc = inc;
7519 compress_added = added;
7520 return OK;
7521}
7522
7523
7524/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007525 * Get a wordnode_T, either from the list of previously freed nodes or
7526 * allocate a new one.
7527 */
7528 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007529get_wordnode(spin)
7530 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007531{
7532 wordnode_T *n;
7533
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007534 if (spin->si_first_free == NULL)
7535 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007536 else
7537 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007538 n = spin->si_first_free;
7539 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007540 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007541 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007542 }
7543#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007544 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007545#endif
7546 return n;
7547}
7548
7549/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007550 * Decrement the reference count on a node (which is the head of a list of
7551 * siblings). If the reference count becomes zero free the node and its
7552 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007553 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007554 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007555 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007556deref_wordnode(spin, node)
7557 spellinfo_T *spin;
7558 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007559{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007560 wordnode_T *np;
7561 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007562
7563 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007564 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007565 for (np = node; np != NULL; np = np->wn_sibling)
7566 {
7567 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007568 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007569 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007570 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007571 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007572 ++cnt; /* length field */
7573 }
7574 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007575}
7576
7577/*
7578 * Free a wordnode_T for re-use later.
7579 * Only the "wn_child" field becomes invalid.
7580 */
7581 static void
7582free_wordnode(spin, n)
7583 spellinfo_T *spin;
7584 wordnode_T *n;
7585{
7586 n->wn_child = spin->si_first_free;
7587 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007588 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007589}
7590
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007591/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007592 * Compress a tree: find tails that are identical and can be shared.
7593 */
7594 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007595wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007596 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007597 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007598{
7599 hashtab_T ht;
7600 int n;
7601 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007602 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007603
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007604 /* Skip the root itself, it's not actually used. The first sibling is the
7605 * start of the tree. */
7606 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007607 {
7608 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007609 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007610
7611#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007612 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007613#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007614 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007615 if (tot > 1000000)
7616 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007617 else if (tot == 0)
7618 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007619 else
7620 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007621 vim_snprintf((char *)IObuff, IOSIZE,
7622 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7623 n, tot, tot - n, perc);
7624 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007625 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007626#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007627 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007628#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007629 hash_clear(&ht);
7630 }
7631}
7632
7633/*
7634 * Compress a node, its siblings and its children, depth first.
7635 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007636 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007637 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007638node_compress(spin, node, ht, tot)
7639 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007640 wordnode_T *node;
7641 hashtab_T *ht;
7642 int *tot; /* total count of nodes before compressing,
7643 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007644{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007645 wordnode_T *np;
7646 wordnode_T *tp;
7647 wordnode_T *child;
7648 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007649 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007650 int len = 0;
7651 unsigned nr, n;
7652 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007653
Bram Moolenaar51485f02005-06-04 21:55:20 +00007654 /*
7655 * Go through the list of siblings. Compress each child and then try
7656 * finding an identical child to replace it.
7657 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007658 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007659 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007660 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007661 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007662 ++len;
7663 if ((child = np->wn_child) != NULL)
7664 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007665 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007666 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007667
7668 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007669 hash = hash_hash(child->wn_u1.hashkey);
7670 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007671 if (!HASHITEM_EMPTY(hi))
7672 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007673 /* There are children we encountered before with a hash value
7674 * identical to the current child. Now check if there is one
7675 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007676 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007677 if (node_equal(child, tp))
7678 {
7679 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007680 * current one. This means the current child and all
7681 * its siblings is unlinked from the tree. */
7682 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007683 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007684 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007685 break;
7686 }
7687 if (tp == NULL)
7688 {
7689 /* No other child with this hash value equals the child of
7690 * the node, add it to the linked list after the first
7691 * item. */
7692 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007693 child->wn_u2.next = tp->wn_u2.next;
7694 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007695 }
7696 }
7697 else
7698 /* No other child has this hash value, add it to the
7699 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007700 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007701 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007702 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007703 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007704
7705 /*
7706 * Make a hash key for the node and its siblings, so that we can quickly
7707 * find a lookalike node. This must be done after compressing the sibling
7708 * list, otherwise the hash key would become invalid by the compression.
7709 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007710 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007711 nr = 0;
7712 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007713 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007714 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007715 /* end node: use wn_flags, wn_region and wn_affixID */
7716 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007717 else
7718 /* byte node: use the byte value and the child pointer */
7719 n = np->wn_byte + ((long_u)np->wn_child << 8);
7720 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007721 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007722
7723 /* Avoid NUL bytes, it terminates the hash key. */
7724 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007725 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007726 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007727 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007728 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007729 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007730 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007731 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7732 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007733
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007734 /* Check for CTRL-C pressed now and then. */
7735 fast_breakcheck();
7736
Bram Moolenaar51485f02005-06-04 21:55:20 +00007737 return compressed;
7738}
7739
7740/*
7741 * Return TRUE when two nodes have identical siblings and children.
7742 */
7743 static int
7744node_equal(n1, n2)
7745 wordnode_T *n1;
7746 wordnode_T *n2;
7747{
7748 wordnode_T *p1;
7749 wordnode_T *p2;
7750
7751 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7752 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7753 if (p1->wn_byte != p2->wn_byte
7754 || (p1->wn_byte == NUL
7755 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007756 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007757 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007758 : (p1->wn_child != p2->wn_child)))
7759 break;
7760
7761 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007762}
7763
7764/*
7765 * Write a number to file "fd", MSB first, in "len" bytes.
7766 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007767 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007768put_bytes(fd, nr, len)
7769 FILE *fd;
7770 long_u nr;
7771 int len;
7772{
7773 int i;
7774
7775 for (i = len - 1; i >= 0; --i)
7776 putc((int)(nr >> (i * 8)), fd);
7777}
7778
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007779#ifdef _MSC_VER
7780# if (_MSC_VER <= 1200)
7781/* This line is required for VC6 without the service pack. Also see the
7782 * matching #pragma below. */
7783/* # pragma optimize("", off) */
7784# endif
7785#endif
7786
Bram Moolenaar4770d092006-01-12 23:22:24 +00007787/*
7788 * Write spin->si_sugtime to file "fd".
7789 */
7790 static void
7791put_sugtime(spin, fd)
7792 spellinfo_T *spin;
7793 FILE *fd;
7794{
7795 int c;
7796 int i;
7797
7798 /* time_t can be up to 8 bytes in size, more than long_u, thus we
7799 * can't use put_bytes() here. */
7800 for (i = 7; i >= 0; --i)
7801 if (i + 1 > sizeof(time_t))
7802 /* ">>" doesn't work well when shifting more bits than avail */
7803 putc(0, fd);
7804 else
7805 {
7806 c = (unsigned)spin->si_sugtime >> (i * 8);
7807 putc(c, fd);
7808 }
7809}
7810
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007811#ifdef _MSC_VER
7812# if (_MSC_VER <= 1200)
7813/* # pragma optimize("", on) */
7814# endif
7815#endif
7816
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007817static int
7818#ifdef __BORLANDC__
7819_RTLENTRYF
7820#endif
7821rep_compare __ARGS((const void *s1, const void *s2));
7822
7823/*
7824 * Function given to qsort() to sort the REP items on "from" string.
7825 */
7826 static int
7827#ifdef __BORLANDC__
7828_RTLENTRYF
7829#endif
7830rep_compare(s1, s2)
7831 const void *s1;
7832 const void *s2;
7833{
7834 fromto_T *p1 = (fromto_T *)s1;
7835 fromto_T *p2 = (fromto_T *)s2;
7836
7837 return STRCMP(p1->ft_from, p2->ft_from);
7838}
7839
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007840/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007841 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007842 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007843 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007844 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007845write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007846 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007847 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007848{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007849 FILE *fd;
7850 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007851 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007852 wordnode_T *tree;
7853 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007854 int i;
7855 int l;
7856 garray_T *gap;
7857 fromto_T *ftp;
7858 char_u *p;
7859 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007860 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007861
Bram Moolenaarb765d632005-06-07 21:00:02 +00007862 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007863 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007864 {
7865 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007866 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007867 }
7868
Bram Moolenaar5195e452005-08-19 20:32:47 +00007869 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007870 /* <fileID> */
7871 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007872 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007873 EMSG(_(e_write));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007874 retval = FAIL;
7875 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007876 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007877
Bram Moolenaar5195e452005-08-19 20:32:47 +00007878 /*
7879 * <SECTIONS>: <section> ... <sectionend>
7880 */
7881
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007882 /* SN_INFO: <infotext> */
7883 if (spin->si_info != NULL)
7884 {
7885 putc(SN_INFO, fd); /* <sectionID> */
7886 putc(0, fd); /* <sectionflags> */
7887
7888 i = STRLEN(spin->si_info);
7889 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
7890 fwrite(spin->si_info, (size_t)i, (size_t)1, fd); /* <infotext> */
7891 }
7892
Bram Moolenaar5195e452005-08-19 20:32:47 +00007893 /* SN_REGION: <regionname> ...
7894 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007895 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007896 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007897 putc(SN_REGION, fd); /* <sectionID> */
7898 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7899 l = spin->si_region_count * 2;
7900 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7901 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
7902 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007903 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007904 }
7905 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00007906 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007907
Bram Moolenaar5195e452005-08-19 20:32:47 +00007908 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
7909 *
7910 * The table with character flags and the table for case folding.
7911 * This makes sure the same characters are recognized as word characters
7912 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007913 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007914 * 'encoding'.
7915 * Also skip this for an .add.spl file, the main spell file must contain
7916 * the table (avoids that it conflicts). File is shorter too.
7917 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007918 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007919 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007920 char_u folchars[128 * 8];
7921 int flags;
7922
Bram Moolenaard12a1322005-08-21 22:08:24 +00007923 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007924 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7925
7926 /* Form the <folchars> string first, we need to know its length. */
7927 l = 0;
7928 for (i = 128; i < 256; ++i)
7929 {
7930#ifdef FEAT_MBYTE
7931 if (has_mbyte)
7932 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
7933 else
7934#endif
7935 folchars[l++] = spelltab.st_fold[i];
7936 }
7937 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
7938
7939 fputc(128, fd); /* <charflagslen> */
7940 for (i = 128; i < 256; ++i)
7941 {
7942 flags = 0;
7943 if (spelltab.st_isw[i])
7944 flags |= CF_WORD;
7945 if (spelltab.st_isu[i])
7946 flags |= CF_UPPER;
7947 fputc(flags, fd); /* <charflags> */
7948 }
7949
7950 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
7951 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007952 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007953
Bram Moolenaar5195e452005-08-19 20:32:47 +00007954 /* SN_MIDWORD: <midword> */
7955 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007956 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007957 putc(SN_MIDWORD, fd); /* <sectionID> */
7958 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7959
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007960 i = STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007961 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007962 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
7963 }
7964
Bram Moolenaar5195e452005-08-19 20:32:47 +00007965 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
7966 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007967 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007968 putc(SN_PREFCOND, fd); /* <sectionID> */
7969 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7970
7971 l = write_spell_prefcond(NULL, &spin->si_prefcond);
7972 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7973
7974 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007975 }
7976
Bram Moolenaar5195e452005-08-19 20:32:47 +00007977 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00007978 * SN_SAL: <salflags> <salcount> <sal> ...
7979 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007980
Bram Moolenaar5195e452005-08-19 20:32:47 +00007981 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00007982 * round 2: SN_SAL section (unless SN_SOFO is used)
7983 * round 3: SN_REPSAL section */
7984 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007985 {
7986 if (round == 1)
7987 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007988 else if (round == 2)
7989 {
7990 /* Don't write SN_SAL when using a SN_SOFO section */
7991 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
7992 continue;
7993 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007994 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007995 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00007996 gap = &spin->si_repsal;
7997
7998 /* Don't write the section if there are no items. */
7999 if (gap->ga_len == 0)
8000 continue;
8001
8002 /* Sort the REP/REPSAL items. */
8003 if (round != 2)
8004 qsort(gap->ga_data, (size_t)gap->ga_len,
8005 sizeof(fromto_T), rep_compare);
8006
8007 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
8008 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008009
Bram Moolenaar5195e452005-08-19 20:32:47 +00008010 /* This is for making suggestions, section is not required. */
8011 putc(0, fd); /* <sectionflags> */
8012
8013 /* Compute the length of what follows. */
8014 l = 2; /* count <repcount> or <salcount> */
8015 for (i = 0; i < gap->ga_len; ++i)
8016 {
8017 ftp = &((fromto_T *)gap->ga_data)[i];
8018 l += 1 + STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
8019 l += 1 + STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
8020 }
8021 if (round == 2)
8022 ++l; /* count <salflags> */
8023 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8024
8025 if (round == 2)
8026 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008027 i = 0;
8028 if (spin->si_followup)
8029 i |= SAL_F0LLOWUP;
8030 if (spin->si_collapse)
8031 i |= SAL_COLLAPSE;
8032 if (spin->si_rem_accents)
8033 i |= SAL_REM_ACCENTS;
8034 putc(i, fd); /* <salflags> */
8035 }
8036
8037 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
8038 for (i = 0; i < gap->ga_len; ++i)
8039 {
8040 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
8041 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
8042 ftp = &((fromto_T *)gap->ga_data)[i];
8043 for (rr = 1; rr <= 2; ++rr)
8044 {
8045 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
8046 l = STRLEN(p);
8047 putc(l, fd);
8048 fwrite(p, l, (size_t)1, fd);
8049 }
8050 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008051
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008052 }
8053
Bram Moolenaar5195e452005-08-19 20:32:47 +00008054 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
8055 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008056 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8057 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008058 putc(SN_SOFO, fd); /* <sectionID> */
8059 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008060
8061 l = STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008062 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
8063 /* <sectionlen> */
8064
8065 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
8066 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008067
8068 l = STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008069 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
8070 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008071 }
8072
Bram Moolenaar4770d092006-01-12 23:22:24 +00008073 /* SN_WORDS: <word> ...
8074 * This is for making suggestions, section is not required. */
8075 if (spin->si_commonwords.ht_used > 0)
8076 {
8077 putc(SN_WORDS, fd); /* <sectionID> */
8078 putc(0, fd); /* <sectionflags> */
8079
8080 /* round 1: count the bytes
8081 * round 2: write the bytes */
8082 for (round = 1; round <= 2; ++round)
8083 {
8084 int todo;
8085 int len = 0;
8086 hashitem_T *hi;
8087
8088 todo = spin->si_commonwords.ht_used;
8089 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
8090 if (!HASHITEM_EMPTY(hi))
8091 {
8092 l = STRLEN(hi->hi_key) + 1;
8093 len += l;
8094 if (round == 2) /* <word> */
8095 fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
8096 --todo;
8097 }
8098 if (round == 1)
8099 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
8100 }
8101 }
8102
Bram Moolenaar5195e452005-08-19 20:32:47 +00008103 /* SN_MAP: <mapstr>
8104 * This is for making suggestions, section is not required. */
8105 if (spin->si_map.ga_len > 0)
8106 {
8107 putc(SN_MAP, fd); /* <sectionID> */
8108 putc(0, fd); /* <sectionflags> */
8109 l = spin->si_map.ga_len;
8110 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8111 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
8112 /* <mapstr> */
8113 }
8114
Bram Moolenaar4770d092006-01-12 23:22:24 +00008115 /* SN_SUGFILE: <timestamp>
8116 * This is used to notify that a .sug file may be available and at the
8117 * same time allows for checking that a .sug file that is found matches
8118 * with this .spl file. That's because the word numbers must be exactly
8119 * right. */
8120 if (!spin->si_nosugfile
8121 && (spin->si_sal.ga_len > 0
8122 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
8123 {
8124 putc(SN_SUGFILE, fd); /* <sectionID> */
8125 putc(0, fd); /* <sectionflags> */
8126 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
8127
8128 /* Set si_sugtime and write it to the file. */
8129 spin->si_sugtime = time(NULL);
8130 put_sugtime(spin, fd); /* <timestamp> */
8131 }
8132
Bram Moolenaare1438bb2006-03-01 22:01:55 +00008133 /* SN_NOSPLITSUGS: nothing
8134 * This is used to notify that no suggestions with word splits are to be
8135 * made. */
8136 if (spin->si_nosplitsugs)
8137 {
8138 putc(SN_NOSPLITSUGS, fd); /* <sectionID> */
8139 putc(0, fd); /* <sectionflags> */
8140 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8141 }
8142
Bram Moolenaar5195e452005-08-19 20:32:47 +00008143 /* SN_COMPOUND: compound info.
8144 * We don't mark it required, when not supported all compound words will
8145 * be bad words. */
8146 if (spin->si_compflags != NULL)
8147 {
8148 putc(SN_COMPOUND, fd); /* <sectionID> */
8149 putc(0, fd); /* <sectionflags> */
8150
8151 l = STRLEN(spin->si_compflags);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008152 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8153 l += STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
8154 put_bytes(fd, (long_u)(l + 7), 4); /* <sectionlen> */
8155
Bram Moolenaar5195e452005-08-19 20:32:47 +00008156 putc(spin->si_compmax, fd); /* <compmax> */
8157 putc(spin->si_compminlen, fd); /* <compminlen> */
8158 putc(spin->si_compsylmax, fd); /* <compsylmax> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008159 putc(0, fd); /* for Vim 7.0b compatibility */
8160 putc(spin->si_compoptions, fd); /* <compoptions> */
8161 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
8162 /* <comppatcount> */
8163 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8164 {
8165 p = ((char_u **)(spin->si_comppat.ga_data))[i];
8166 putc(STRLEN(p), fd); /* <comppatlen> */
8167 fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);/* <comppattext> */
8168 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008169 /* <compflags> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008170 fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
8171 (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008172 }
8173
Bram Moolenaar78622822005-08-23 21:00:13 +00008174 /* SN_NOBREAK: NOBREAK flag */
8175 if (spin->si_nobreak)
8176 {
8177 putc(SN_NOBREAK, fd); /* <sectionID> */
8178 putc(0, fd); /* <sectionflags> */
8179
8180 /* It's empty, the precense of the section flags the feature. */
8181 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8182 }
8183
Bram Moolenaar5195e452005-08-19 20:32:47 +00008184 /* SN_SYLLABLE: syllable info.
8185 * We don't mark it required, when not supported syllables will not be
8186 * counted. */
8187 if (spin->si_syllable != NULL)
8188 {
8189 putc(SN_SYLLABLE, fd); /* <sectionID> */
8190 putc(0, fd); /* <sectionflags> */
8191
8192 l = STRLEN(spin->si_syllable);
8193 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8194 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
8195 }
8196
8197 /* end of <SECTIONS> */
8198 putc(SN_END, fd); /* <sectionend> */
8199
Bram Moolenaar50cde822005-06-05 21:54:54 +00008200
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008201 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008202 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008203 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008204 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008205 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008206 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008207 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008208 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008209 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008210 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008211 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008212 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008213
Bram Moolenaar0c405862005-06-22 22:26:26 +00008214 /* Clear the index and wnode fields in the tree. */
8215 clear_node(tree);
8216
Bram Moolenaar51485f02005-06-04 21:55:20 +00008217 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00008218 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00008219 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008220 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008221
Bram Moolenaar51485f02005-06-04 21:55:20 +00008222 /* number of nodes in 4 bytes */
8223 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00008224 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008225
Bram Moolenaar51485f02005-06-04 21:55:20 +00008226 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008227 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008228 }
8229
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008230 /* Write another byte to check for errors. */
8231 if (putc(0, fd) == EOF)
8232 retval = FAIL;
8233
8234 if (fclose(fd) == EOF)
8235 retval = FAIL;
8236
8237 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008238}
8239
8240/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008241 * Clear the index and wnode fields of "node", it siblings and its
8242 * children. This is needed because they are a union with other items to save
8243 * space.
8244 */
8245 static void
8246clear_node(node)
8247 wordnode_T *node;
8248{
8249 wordnode_T *np;
8250
8251 if (node != NULL)
8252 for (np = node; np != NULL; np = np->wn_sibling)
8253 {
8254 np->wn_u1.index = 0;
8255 np->wn_u2.wnode = NULL;
8256
8257 if (np->wn_byte != NUL)
8258 clear_node(np->wn_child);
8259 }
8260}
8261
8262
8263/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008264 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008265 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00008266 * This first writes the list of possible bytes (siblings). Then for each
8267 * byte recursively write the children.
8268 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00008269 * NOTE: The code here must match the code in read_tree_node(), since
8270 * assumptions are made about the indexes (so that we don't have to write them
8271 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00008272 *
8273 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008274 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008275 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00008276put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008277 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008278 wordnode_T *node;
8279 int index;
8280 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008281 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008282{
Bram Moolenaar51485f02005-06-04 21:55:20 +00008283 int newindex = index;
8284 int siblingcount = 0;
8285 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008286 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008287
Bram Moolenaar51485f02005-06-04 21:55:20 +00008288 /* If "node" is zero the tree is empty. */
8289 if (node == NULL)
8290 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008291
Bram Moolenaar51485f02005-06-04 21:55:20 +00008292 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008293 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008294
8295 /* Count the number of siblings. */
8296 for (np = node; np != NULL; np = np->wn_sibling)
8297 ++siblingcount;
8298
8299 /* Write the sibling count. */
8300 if (fd != NULL)
8301 putc(siblingcount, fd); /* <siblingcount> */
8302
8303 /* Write each sibling byte and optionally extra info. */
8304 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008305 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008306 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008307 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008308 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008309 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008310 /* For a NUL byte (end of word) write the flags etc. */
8311 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008312 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008313 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008314 * associated condition nr (stored in wn_region). The
8315 * byte value is misused to store the "rare" and "not
8316 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008317 if (np->wn_flags == (short_u)PFX_FLAGS)
8318 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008319 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00008320 {
8321 putc(BY_FLAGS, fd); /* <byte> */
8322 putc(np->wn_flags, fd); /* <pflags> */
8323 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008324 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008325 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008326 }
8327 else
8328 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008329 /* For word trees we write the flag/region items. */
8330 flags = np->wn_flags;
8331 if (regionmask != 0 && np->wn_region != regionmask)
8332 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008333 if (np->wn_affixID != 0)
8334 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008335 if (flags == 0)
8336 {
8337 /* word without flags or region */
8338 putc(BY_NOFLAGS, fd); /* <byte> */
8339 }
8340 else
8341 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008342 if (np->wn_flags >= 0x100)
8343 {
8344 putc(BY_FLAGS2, fd); /* <byte> */
8345 putc(flags, fd); /* <flags> */
8346 putc((unsigned)flags >> 8, fd); /* <flags2> */
8347 }
8348 else
8349 {
8350 putc(BY_FLAGS, fd); /* <byte> */
8351 putc(flags, fd); /* <flags> */
8352 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008353 if (flags & WF_REGION)
8354 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008355 if (flags & WF_AFX)
8356 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008357 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008358 }
8359 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008360 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008361 else
8362 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008363 if (np->wn_child->wn_u1.index != 0
8364 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008365 {
8366 /* The child is written elsewhere, write the reference. */
8367 if (fd != NULL)
8368 {
8369 putc(BY_INDEX, fd); /* <byte> */
8370 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008371 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008372 }
8373 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008374 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008375 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008376 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008377
Bram Moolenaar51485f02005-06-04 21:55:20 +00008378 if (fd != NULL)
8379 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
8380 {
8381 EMSG(_(e_write));
8382 return 0;
8383 }
8384 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008385 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008386
8387 /* Space used in the array when reading: one for each sibling and one for
8388 * the count. */
8389 newindex += siblingcount + 1;
8390
8391 /* Recursively dump the children of each sibling. */
8392 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008393 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
8394 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008395 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008396
8397 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008398}
8399
8400
8401/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008402 * ":mkspell [-ascii] outfile infile ..."
8403 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008404 */
8405 void
8406ex_mkspell(eap)
8407 exarg_T *eap;
8408{
8409 int fcount;
8410 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008411 char_u *arg = eap->arg;
8412 int ascii = FALSE;
8413
8414 if (STRNCMP(arg, "-ascii", 6) == 0)
8415 {
8416 ascii = TRUE;
8417 arg = skipwhite(arg + 6);
8418 }
8419
8420 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
8421 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
8422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008423 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008424 FreeWild(fcount, fnames);
8425 }
8426}
8427
8428/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00008429 * Create the .sug file.
8430 * Uses the soundfold info in "spin".
8431 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
8432 */
8433 static void
8434spell_make_sugfile(spin, wfname)
8435 spellinfo_T *spin;
8436 char_u *wfname;
8437{
8438 char_u fname[MAXPATHL];
8439 int len;
8440 slang_T *slang;
8441 int free_slang = FALSE;
8442
8443 /*
8444 * Read back the .spl file that was written. This fills the required
8445 * info for soundfolding. This also uses less memory than the
8446 * pointer-linked version of the trie. And it avoids having two versions
8447 * of the code for the soundfolding stuff.
8448 * It might have been done already by spell_reload_one().
8449 */
8450 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
8451 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
8452 break;
8453 if (slang == NULL)
8454 {
8455 spell_message(spin, (char_u *)_("Reading back spell file..."));
8456 slang = spell_load_file(wfname, NULL, NULL, FALSE);
8457 if (slang == NULL)
8458 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008459 free_slang = TRUE;
8460 }
8461
8462 /*
8463 * Clear the info in "spin" that is used.
8464 */
8465 spin->si_blocks = NULL;
8466 spin->si_blocks_cnt = 0;
8467 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
8468 spin->si_free_count = 0;
8469 spin->si_first_free = NULL;
8470 spin->si_foldwcount = 0;
8471
8472 /*
8473 * Go through the trie of good words, soundfold each word and add it to
8474 * the soundfold trie.
8475 */
8476 spell_message(spin, (char_u *)_("Performing soundfolding..."));
8477 if (sug_filltree(spin, slang) == FAIL)
8478 goto theend;
8479
8480 /*
8481 * Create the table which links each soundfold word with a list of the
8482 * good words it may come from. Creates buffer "spin->si_spellbuf".
8483 * This also removes the wordnr from the NUL byte entries to make
8484 * compression possible.
8485 */
8486 if (sug_maketable(spin) == FAIL)
8487 goto theend;
8488
8489 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8490 (long)spin->si_spellbuf->b_ml.ml_line_count);
8491
8492 /*
8493 * Compress the soundfold trie.
8494 */
8495 spell_message(spin, (char_u *)_(msg_compressing));
8496 wordtree_compress(spin, spin->si_foldroot);
8497
8498 /*
8499 * Write the .sug file.
8500 * Make the file name by changing ".spl" to ".sug".
8501 */
8502 STRCPY(fname, wfname);
8503 len = STRLEN(fname);
8504 fname[len - 2] = 'u';
8505 fname[len - 1] = 'g';
8506 sug_write(spin, fname);
8507
8508theend:
8509 if (free_slang)
8510 slang_free(slang);
8511 free_blocks(spin->si_blocks);
8512 close_spellbuf(spin->si_spellbuf);
8513}
8514
8515/*
8516 * Build the soundfold trie for language "slang".
8517 */
8518 static int
8519sug_filltree(spin, slang)
8520 spellinfo_T *spin;
8521 slang_T *slang;
8522{
8523 char_u *byts;
8524 idx_T *idxs;
8525 int depth;
8526 idx_T arridx[MAXWLEN];
8527 int curi[MAXWLEN];
8528 char_u tword[MAXWLEN];
8529 char_u tsalword[MAXWLEN];
8530 int c;
8531 idx_T n;
8532 unsigned words_done = 0;
8533 int wordcount[MAXWLEN];
8534
8535 /* We use si_foldroot for the souldfolded trie. */
8536 spin->si_foldroot = wordtree_alloc(spin);
8537 if (spin->si_foldroot == NULL)
8538 return FAIL;
8539
8540 /* let tree_add_word() know we're adding to the soundfolded tree */
8541 spin->si_sugtree = TRUE;
8542
8543 /*
8544 * Go through the whole case-folded tree, soundfold each word and put it
8545 * in the trie.
8546 */
8547 byts = slang->sl_fbyts;
8548 idxs = slang->sl_fidxs;
8549
8550 arridx[0] = 0;
8551 curi[0] = 1;
8552 wordcount[0] = 0;
8553
8554 depth = 0;
8555 while (depth >= 0 && !got_int)
8556 {
8557 if (curi[depth] > byts[arridx[depth]])
8558 {
8559 /* Done all bytes at this node, go up one level. */
8560 idxs[arridx[depth]] = wordcount[depth];
8561 if (depth > 0)
8562 wordcount[depth - 1] += wordcount[depth];
8563
8564 --depth;
8565 line_breakcheck();
8566 }
8567 else
8568 {
8569
8570 /* Do one more byte at this node. */
8571 n = arridx[depth] + curi[depth];
8572 ++curi[depth];
8573
8574 c = byts[n];
8575 if (c == 0)
8576 {
8577 /* Sound-fold the word. */
8578 tword[depth] = NUL;
8579 spell_soundfold(slang, tword, TRUE, tsalword);
8580
8581 /* We use the "flags" field for the MSB of the wordnr,
8582 * "region" for the LSB of the wordnr. */
8583 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8584 words_done >> 16, words_done & 0xffff,
8585 0) == FAIL)
8586 return FAIL;
8587
8588 ++words_done;
8589 ++wordcount[depth];
8590
8591 /* Reset the block count each time to avoid compression
8592 * kicking in. */
8593 spin->si_blocks_cnt = 0;
8594
8595 /* Skip over any other NUL bytes (same word with different
8596 * flags). */
8597 while (byts[n + 1] == 0)
8598 {
8599 ++n;
8600 ++curi[depth];
8601 }
8602 }
8603 else
8604 {
8605 /* Normal char, go one level deeper. */
8606 tword[depth++] = c;
8607 arridx[depth] = idxs[n];
8608 curi[depth] = 1;
8609 wordcount[depth] = 0;
8610 }
8611 }
8612 }
8613
8614 smsg((char_u *)_("Total number of words: %d"), words_done);
8615
8616 return OK;
8617}
8618
8619/*
8620 * Make the table that links each word in the soundfold trie to the words it
8621 * can be produced from.
8622 * This is not unlike lines in a file, thus use a memfile to be able to access
8623 * the table efficiently.
8624 * Returns FAIL when out of memory.
8625 */
8626 static int
8627sug_maketable(spin)
8628 spellinfo_T *spin;
8629{
8630 garray_T ga;
8631 int res = OK;
8632
8633 /* Allocate a buffer, open a memline for it and create the swap file
8634 * (uses a temp file, not a .swp file). */
8635 spin->si_spellbuf = open_spellbuf();
8636 if (spin->si_spellbuf == NULL)
8637 return FAIL;
8638
8639 /* Use a buffer to store the line info, avoids allocating many small
8640 * pieces of memory. */
8641 ga_init2(&ga, 1, 100);
8642
8643 /* recursively go through the tree */
8644 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8645 res = FAIL;
8646
8647 ga_clear(&ga);
8648 return res;
8649}
8650
8651/*
8652 * Fill the table for one node and its children.
8653 * Returns the wordnr at the start of the node.
8654 * Returns -1 when out of memory.
8655 */
8656 static int
8657sug_filltable(spin, node, startwordnr, gap)
8658 spellinfo_T *spin;
8659 wordnode_T *node;
8660 int startwordnr;
8661 garray_T *gap; /* place to store line of numbers */
8662{
8663 wordnode_T *p, *np;
8664 int wordnr = startwordnr;
8665 int nr;
8666 int prev_nr;
8667
8668 for (p = node; p != NULL; p = p->wn_sibling)
8669 {
8670 if (p->wn_byte == NUL)
8671 {
8672 gap->ga_len = 0;
8673 prev_nr = 0;
8674 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8675 {
8676 if (ga_grow(gap, 10) == FAIL)
8677 return -1;
8678
8679 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8680 /* Compute the offset from the previous nr and store the
8681 * offset in a way that it takes a minimum number of bytes.
8682 * It's a bit like utf-8, but without the need to mark
8683 * following bytes. */
8684 nr -= prev_nr;
8685 prev_nr += nr;
8686 gap->ga_len += offset2bytes(nr,
8687 (char_u *)gap->ga_data + gap->ga_len);
8688 }
8689
8690 /* add the NUL byte */
8691 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8692
8693 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8694 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8695 return -1;
8696 ++wordnr;
8697
8698 /* Remove extra NUL entries, we no longer need them. We don't
8699 * bother freeing the nodes, the won't be reused anyway. */
8700 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8701 p->wn_sibling = p->wn_sibling->wn_sibling;
8702
8703 /* Clear the flags on the remaining NUL node, so that compression
8704 * works a lot better. */
8705 p->wn_flags = 0;
8706 p->wn_region = 0;
8707 }
8708 else
8709 {
8710 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8711 if (wordnr == -1)
8712 return -1;
8713 }
8714 }
8715 return wordnr;
8716}
8717
8718/*
8719 * Convert an offset into a minimal number of bytes.
8720 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8721 * bytes.
8722 */
8723 static int
8724offset2bytes(nr, buf)
8725 int nr;
8726 char_u *buf;
8727{
8728 int rem;
8729 int b1, b2, b3, b4;
8730
8731 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8732 b1 = nr % 255 + 1;
8733 rem = nr / 255;
8734 b2 = rem % 255 + 1;
8735 rem = rem / 255;
8736 b3 = rem % 255 + 1;
8737 b4 = rem / 255 + 1;
8738
8739 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8740 {
8741 buf[0] = 0xe0 + b4;
8742 buf[1] = b3;
8743 buf[2] = b2;
8744 buf[3] = b1;
8745 return 4;
8746 }
8747 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8748 {
8749 buf[0] = 0xc0 + b3;
8750 buf[1] = b2;
8751 buf[2] = b1;
8752 return 3;
8753 }
8754 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8755 {
8756 buf[0] = 0x80 + b2;
8757 buf[1] = b1;
8758 return 2;
8759 }
8760 /* 1 byte */
8761 buf[0] = b1;
8762 return 1;
8763}
8764
8765/*
8766 * Opposite of offset2bytes().
8767 * "pp" points to the bytes and is advanced over it.
8768 * Returns the offset.
8769 */
8770 static int
8771bytes2offset(pp)
8772 char_u **pp;
8773{
8774 char_u *p = *pp;
8775 int nr;
8776 int c;
8777
8778 c = *p++;
8779 if ((c & 0x80) == 0x00) /* 1 byte */
8780 {
8781 nr = c - 1;
8782 }
8783 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8784 {
8785 nr = (c & 0x3f) - 1;
8786 nr = nr * 255 + (*p++ - 1);
8787 }
8788 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8789 {
8790 nr = (c & 0x1f) - 1;
8791 nr = nr * 255 + (*p++ - 1);
8792 nr = nr * 255 + (*p++ - 1);
8793 }
8794 else /* 4 bytes */
8795 {
8796 nr = (c & 0x0f) - 1;
8797 nr = nr * 255 + (*p++ - 1);
8798 nr = nr * 255 + (*p++ - 1);
8799 nr = nr * 255 + (*p++ - 1);
8800 }
8801
8802 *pp = p;
8803 return nr;
8804}
8805
8806/*
8807 * Write the .sug file in "fname".
8808 */
8809 static void
8810sug_write(spin, fname)
8811 spellinfo_T *spin;
8812 char_u *fname;
8813{
8814 FILE *fd;
8815 wordnode_T *tree;
8816 int nodecount;
8817 int wcount;
8818 char_u *line;
8819 linenr_T lnum;
8820 int len;
8821
8822 /* Create the file. Note that an existing file is silently overwritten! */
8823 fd = mch_fopen((char *)fname, "w");
8824 if (fd == NULL)
8825 {
8826 EMSG2(_(e_notopen), fname);
8827 return;
8828 }
8829
8830 vim_snprintf((char *)IObuff, IOSIZE,
8831 _("Writing suggestion file %s ..."), fname);
8832 spell_message(spin, IObuff);
8833
8834 /*
8835 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8836 */
8837 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
8838 {
8839 EMSG(_(e_write));
8840 goto theend;
8841 }
8842 putc(VIMSUGVERSION, fd); /* <versionnr> */
8843
8844 /* Write si_sugtime to the file. */
8845 put_sugtime(spin, fd); /* <timestamp> */
8846
8847 /*
8848 * <SUGWORDTREE>
8849 */
8850 spin->si_memtot = 0;
8851 tree = spin->si_foldroot->wn_sibling;
8852
8853 /* Clear the index and wnode fields in the tree. */
8854 clear_node(tree);
8855
8856 /* Count the number of nodes. Needed to be able to allocate the
8857 * memory when reading the nodes. Also fills in index for shared
8858 * nodes. */
8859 nodecount = put_node(NULL, tree, 0, 0, FALSE);
8860
8861 /* number of nodes in 4 bytes */
8862 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
8863 spin->si_memtot += nodecount + nodecount * sizeof(int);
8864
8865 /* Write the nodes. */
8866 (void)put_node(fd, tree, 0, 0, FALSE);
8867
8868 /*
8869 * <SUGTABLE>: <sugwcount> <sugline> ...
8870 */
8871 wcount = spin->si_spellbuf->b_ml.ml_line_count;
8872 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
8873
8874 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
8875 {
8876 /* <sugline>: <sugnr> ... NUL */
8877 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
8878 len = STRLEN(line) + 1;
8879 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
8880 {
8881 EMSG(_(e_write));
8882 goto theend;
8883 }
8884 spin->si_memtot += len;
8885 }
8886
8887 /* Write another byte to check for errors. */
8888 if (putc(0, fd) == EOF)
8889 EMSG(_(e_write));
8890
8891 vim_snprintf((char *)IObuff, IOSIZE,
8892 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
8893 spell_message(spin, IObuff);
8894
8895theend:
8896 /* close the file */
8897 fclose(fd);
8898}
8899
8900/*
8901 * Open a spell buffer. This is a nameless buffer that is not in the buffer
8902 * list and only contains text lines. Can use a swapfile to reduce memory
8903 * use.
8904 * Most other fields are invalid! Esp. watch out for string options being
8905 * NULL and there is no undo info.
8906 * Returns NULL when out of memory.
8907 */
8908 static buf_T *
8909open_spellbuf()
8910{
8911 buf_T *buf;
8912
8913 buf = (buf_T *)alloc_clear(sizeof(buf_T));
8914 if (buf != NULL)
8915 {
8916 buf->b_spell = TRUE;
8917 buf->b_p_swf = TRUE; /* may create a swap file */
8918 ml_open(buf);
8919 ml_open_file(buf); /* create swap file now */
8920 }
8921 return buf;
8922}
8923
8924/*
8925 * Close the buffer used for spell info.
8926 */
8927 static void
8928close_spellbuf(buf)
8929 buf_T *buf;
8930{
8931 if (buf != NULL)
8932 {
8933 ml_close(buf, TRUE);
8934 vim_free(buf);
8935 }
8936}
8937
8938
8939/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008940 * Create a Vim spell file from one or more word lists.
8941 * "fnames[0]" is the output file name.
8942 * "fnames[fcount - 1]" is the last input file name.
8943 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
8944 * and ".spl" is appended to make the output file name.
8945 */
8946 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008947mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00008948 int fcount;
8949 char_u **fnames;
8950 int ascii; /* -ascii argument given */
8951 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008952 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008953{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008954 char_u fname[MAXPATHL];
8955 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00008956 char_u **innames;
8957 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008958 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008959 int i;
8960 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008961 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008962 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008963 spellinfo_T spin;
8964
8965 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008966 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008967 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008968 spin.si_followup = TRUE;
8969 spin.si_rem_accents = TRUE;
8970 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008971 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008972 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
8973 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008974 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008975 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008976 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008977 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008978
Bram Moolenaarb765d632005-06-07 21:00:02 +00008979 /* default: fnames[0] is output file, following are input files */
8980 innames = &fnames[1];
8981 incount = fcount - 1;
8982
8983 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00008984 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008985 len = STRLEN(fnames[0]);
8986 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
8987 {
8988 /* For ":mkspell path/en.latin1.add" output file is
8989 * "path/en.latin1.add.spl". */
8990 innames = &fnames[0];
8991 incount = 1;
8992 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
8993 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008994 else if (fcount == 1)
8995 {
8996 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
8997 innames = &fnames[0];
8998 incount = 1;
8999 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
9000 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
9001 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009002 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
9003 {
9004 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009005 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009006 }
9007 else
9008 /* Name should be language, make the file name from it. */
9009 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
9010 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
9011
9012 /* Check for .ascii.spl. */
9013 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
9014 spin.si_ascii = TRUE;
9015
9016 /* Check for .add.spl. */
9017 if (strstr((char *)gettail(wfname), ".add.") != NULL)
9018 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00009019 }
9020
Bram Moolenaarb765d632005-06-07 21:00:02 +00009021 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009022 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009023 else if (vim_strchr(gettail(wfname), '_') != NULL)
9024 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009025 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009026 EMSG(_("E754: Only up to 8 regions supported"));
9027 else
9028 {
9029 /* Check for overwriting before doing things that may take a lot of
9030 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009031 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009032 {
9033 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009034 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009035 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009036 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009037 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009038 EMSG2(_(e_isadir2), wfname);
9039 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009040 }
9041
9042 /*
9043 * Init the aff and dic pointers.
9044 * Get the region names if there are more than 2 arguments.
9045 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009046 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009047 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009048 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009049
Bram Moolenaar3982c542005-06-08 21:56:31 +00009050 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009051 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009052 len = STRLEN(innames[i]);
9053 if (STRLEN(gettail(innames[i])) < 5
9054 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009055 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009056 EMSG2(_("E755: Invalid region in %s"), innames[i]);
9057 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009058 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009059 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
9060 spin.si_region_name[i * 2 + 1] =
9061 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009062 }
9063 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009064 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009065
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009066 spin.si_foldroot = wordtree_alloc(&spin);
9067 spin.si_keeproot = wordtree_alloc(&spin);
9068 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009069 if (spin.si_foldroot == NULL
9070 || spin.si_keeproot == NULL
9071 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009072 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00009073 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009074 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009075 }
9076
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009077 /* When not producing a .add.spl file clear the character table when
9078 * we encounter one in the .aff file. This means we dump the current
9079 * one in the .spl file if the .aff file doesn't define one. That's
9080 * better than guessing the contents, the table will match a
9081 * previously loaded spell file. */
9082 if (!spin.si_add)
9083 spin.si_clear_chartab = TRUE;
9084
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009085 /*
9086 * Read all the .aff and .dic files.
9087 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00009088 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009089 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009090 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009091 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009092 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009093 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009094
Bram Moolenaarb765d632005-06-07 21:00:02 +00009095 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009096 if (mch_stat((char *)fname, &st) >= 0)
9097 {
9098 /* Read the .aff file. Will init "spin->si_conv" based on the
9099 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009100 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009101 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009102 error = TRUE;
9103 else
9104 {
9105 /* Read the .dic file and store the words in the trees. */
9106 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00009107 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009108 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009109 error = TRUE;
9110 }
9111 }
9112 else
9113 {
9114 /* No .aff file, try reading the file as a word list. Store
9115 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009116 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009117 error = TRUE;
9118 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009119
Bram Moolenaarb765d632005-06-07 21:00:02 +00009120#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009121 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00009122 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009123#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009124 }
9125
Bram Moolenaar78622822005-08-23 21:00:13 +00009126 if (spin.si_compflags != NULL && spin.si_nobreak)
9127 MSG(_("Warning: both compounding and NOBREAK specified"));
9128
Bram Moolenaar4770d092006-01-12 23:22:24 +00009129 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009130 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009131 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00009132 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009133 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009134 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009135 wordtree_compress(&spin, spin.si_foldroot);
9136 wordtree_compress(&spin, spin.si_keeproot);
9137 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009138 }
9139
Bram Moolenaar4770d092006-01-12 23:22:24 +00009140 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009141 {
9142 /*
9143 * Write the info in the spell file.
9144 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009145 vim_snprintf((char *)IObuff, IOSIZE,
9146 _("Writing spell file %s ..."), wfname);
9147 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00009148
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009149 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009150
Bram Moolenaar4770d092006-01-12 23:22:24 +00009151 spell_message(&spin, (char_u *)_("Done!"));
9152 vim_snprintf((char *)IObuff, IOSIZE,
9153 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
9154 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009155
Bram Moolenaar4770d092006-01-12 23:22:24 +00009156 /*
9157 * If the file is loaded need to reload it.
9158 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009159 if (!error)
9160 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009161 }
9162
9163 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009164 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009165 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009166 ga_clear(&spin.si_sal);
9167 ga_clear(&spin.si_map);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009168 ga_clear(&spin.si_comppat);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009169 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009170 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009171
9172 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009173 for (i = 0; i < incount; ++i)
9174 if (afile[i] != NULL)
9175 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009176
9177 /* Free all the bits and pieces at once. */
9178 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009179
9180 /*
9181 * If there is soundfolding info and no NOSUGFILE item create the
9182 * .sug file with the soundfolded word trie.
9183 */
9184 if (spin.si_sugtime != 0 && !error && !got_int)
9185 spell_make_sugfile(&spin, wfname);
9186
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009187 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009188}
9189
Bram Moolenaar4770d092006-01-12 23:22:24 +00009190/*
9191 * Display a message for spell file processing when 'verbose' is set or using
9192 * ":mkspell". "str" can be IObuff.
9193 */
9194 static void
9195spell_message(spin, str)
9196 spellinfo_T *spin;
9197 char_u *str;
9198{
9199 if (spin->si_verbose || p_verbose > 2)
9200 {
9201 if (!spin->si_verbose)
9202 verbose_enter();
9203 MSG(str);
9204 out_flush();
9205 if (!spin->si_verbose)
9206 verbose_leave();
9207 }
9208}
Bram Moolenaarb765d632005-06-07 21:00:02 +00009209
9210/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009211 * ":[count]spellgood {word}"
9212 * ":[count]spellwrong {word}"
Bram Moolenaard0131a82006-03-04 21:46:13 +00009213 * ":[count]spellundo {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00009214 */
9215 void
9216ex_spell(eap)
9217 exarg_T *eap;
9218{
Bram Moolenaar7887d882005-07-01 22:33:52 +00009219 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaard0131a82006-03-04 21:46:13 +00009220 eap->forceit ? 0 : (int)eap->line2,
9221 eap->cmdidx == CMD_spellundo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009222}
9223
9224/*
9225 * Add "word[len]" to 'spellfile' as a good or bad word.
9226 */
9227 void
Bram Moolenaard0131a82006-03-04 21:46:13 +00009228spell_add_word(word, len, bad, index, undo)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009229 char_u *word;
9230 int len;
9231 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009232 int index; /* "zG" and "zW": zero, otherwise index in
9233 'spellfile' */
Bram Moolenaard0131a82006-03-04 21:46:13 +00009234 int undo; /* TRUE for "zug", "zuG", "zuw" and "zuW" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009235{
9236 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009237 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009238 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009239 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009240 char_u fnamebuf[MAXPATHL];
9241 char_u line[MAXWLEN * 2];
9242 long fpos, fpos_next = 0;
9243 int i;
9244 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009245
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009246 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009247 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009248 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009249 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009250 int_wordlist = vim_tempname('s');
9251 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009252 return;
9253 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009254 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009255 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009256 else
9257 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009258 /* If 'spellfile' isn't set figure out a good default value. */
9259 if (*curbuf->b_p_spf == NUL)
9260 {
9261 init_spellfile();
9262 new_spf = TRUE;
9263 }
9264
9265 if (*curbuf->b_p_spf == NUL)
9266 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00009267 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009268 return;
9269 }
9270
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009271 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
9272 {
9273 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
9274 if (i == index)
9275 break;
9276 if (*spf == NUL)
9277 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00009278 EMSGN(_("E765: 'spellfile' does not have %ld entries"), index);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009279 return;
9280 }
9281 }
9282
Bram Moolenaarb765d632005-06-07 21:00:02 +00009283 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009284 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009285 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
9286 buf = NULL;
9287 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00009288 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009289 EMSG(_(e_bufloaded));
9290 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009291 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009292
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009293 fname = fnamebuf;
9294 }
9295
Bram Moolenaard0131a82006-03-04 21:46:13 +00009296 if (bad || undo)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009297 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009298 /* When the word appears as good word we need to remove that one,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009299 * since its flags sort before the one with WF_BANNED. */
9300 fd = mch_fopen((char *)fname, "r");
9301 if (fd != NULL)
9302 {
9303 while (!vim_fgets(line, MAXWLEN * 2, fd))
9304 {
9305 fpos = fpos_next;
9306 fpos_next = ftell(fd);
9307 if (STRNCMP(word, line, len) == 0
9308 && (line[len] == '/' || line[len] < ' '))
9309 {
9310 /* Found duplicate word. Remove it by writing a '#' at
9311 * the start of the line. Mixing reading and writing
9312 * doesn't work for all systems, close the file first. */
9313 fclose(fd);
9314 fd = mch_fopen((char *)fname, "r+");
9315 if (fd == NULL)
9316 break;
9317 if (fseek(fd, fpos, SEEK_SET) == 0)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009318 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009319 fputc('#', fd);
Bram Moolenaard0131a82006-03-04 21:46:13 +00009320 if (undo)
9321 smsg((char_u *)_("Word removed from %s"), NameBuff);
9322 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009323 fseek(fd, fpos_next, SEEK_SET);
9324 }
9325 }
9326 fclose(fd);
9327 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009328 }
9329
Bram Moolenaard0131a82006-03-04 21:46:13 +00009330 if (!undo)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009331 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009332 fd = mch_fopen((char *)fname, "a");
9333 if (fd == NULL && new_spf)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009334 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009335 /* We just initialized the 'spellfile' option and can't open the
9336 * file. We may need to create the "spell" directory first. We
9337 * already checked the runtime directory is writable in
9338 * init_spellfile(). */
9339 if (!dir_of_file_exists(fname))
9340 {
9341 /* The directory doesn't exist. Try creating it and opening
9342 * the file again. */
9343 vim_mkdir(NameBuff, 0755);
9344 fd = mch_fopen((char *)fname, "a");
9345 }
9346 }
9347
9348 if (fd == NULL)
9349 EMSG2(_(e_notopen), fname);
9350 else
9351 {
9352 if (bad)
9353 fprintf(fd, "%.*s/!\n", len, word);
9354 else
9355 fprintf(fd, "%.*s\n", len, word);
9356 fclose(fd);
9357
9358 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
9359 smsg((char_u *)_("Word added to %s"), NameBuff);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009360 }
9361 }
9362
Bram Moolenaard0131a82006-03-04 21:46:13 +00009363 if (fd != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009364 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009365 /* Update the .add.spl file. */
9366 mkspell(1, &fname, FALSE, TRUE, TRUE);
9367
9368 /* If the .add file is edited somewhere, reload it. */
9369 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00009370 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009371
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009372 redraw_all_later(SOME_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009373 }
9374}
9375
9376/*
9377 * Initialize 'spellfile' for the current buffer.
9378 */
9379 static void
9380init_spellfile()
9381{
9382 char_u buf[MAXPATHL];
9383 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009384 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009385 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009386 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009387 int aspath = FALSE;
9388 char_u *lstart = curbuf->b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009389
9390 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
9391 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009392 /* Find the end of the language name. Exclude the region. If there
9393 * is a path separator remember the start of the tail. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009394 for (lend = curbuf->b_p_spl; *lend != NUL
9395 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009396 if (vim_ispathsep(*lend))
9397 {
9398 aspath = TRUE;
9399 lstart = lend + 1;
9400 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009401
9402 /* Loop over all entries in 'runtimepath'. Use the first one where we
9403 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009404 rtp = p_rtp;
9405 while (*rtp != NUL)
9406 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009407 if (aspath)
9408 /* Use directory of an entry with path, e.g., for
9409 * "/dir/lg.utf-8.spl" use "/dir". */
9410 vim_strncpy(buf, curbuf->b_p_spl, lstart - curbuf->b_p_spl - 1);
9411 else
9412 /* Copy the path from 'runtimepath' to buf[]. */
9413 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009414 if (filewritable(buf) == 2)
9415 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00009416 /* Use the first language name from 'spelllang' and the
9417 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009418 if (aspath)
9419 vim_strncpy(buf, curbuf->b_p_spl, lend - curbuf->b_p_spl);
9420 else
9421 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009422 /* Create the "spell" directory if it doesn't exist yet. */
9423 l = STRLEN(buf);
9424 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
9425 if (!filewritable(buf) != 2)
9426 vim_mkdir(buf, 0755);
9427
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009428 l = STRLEN(buf);
9429 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009430 "/%.*s", (int)(lend - lstart), lstart);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009431 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009432 l = STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009433 fname = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang->sl_fname;
9434 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
9435 fname != NULL
9436 && strstr((char *)gettail(fname), ".ascii.") != NULL
9437 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009438 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
9439 break;
9440 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009441 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009442 }
9443 }
9444}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009445
Bram Moolenaar51485f02005-06-04 21:55:20 +00009446
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009447/*
9448 * Init the chartab used for spelling for ASCII.
9449 * EBCDIC is not supported!
9450 */
9451 static void
9452clear_spell_chartab(sp)
9453 spelltab_T *sp;
9454{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009455 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009456
9457 /* Init everything to FALSE. */
9458 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
9459 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
9460 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009461 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009462 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009463 sp->st_upper[i] = i;
9464 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009465
9466 /* We include digits. A word shouldn't start with a digit, but handling
9467 * that is done separately. */
9468 for (i = '0'; i <= '9'; ++i)
9469 sp->st_isw[i] = TRUE;
9470 for (i = 'A'; i <= 'Z'; ++i)
9471 {
9472 sp->st_isw[i] = TRUE;
9473 sp->st_isu[i] = TRUE;
9474 sp->st_fold[i] = i + 0x20;
9475 }
9476 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009477 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009478 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009479 sp->st_upper[i] = i - 0x20;
9480 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009481}
9482
9483/*
9484 * Init the chartab used for spelling. Only depends on 'encoding'.
9485 * Called once while starting up and when 'encoding' changes.
9486 * The default is to use isalpha(), but the spell file should define the word
9487 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009488 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009489 */
9490 void
9491init_spell_chartab()
9492{
9493 int i;
9494
9495 did_set_spelltab = FALSE;
9496 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009497#ifdef FEAT_MBYTE
9498 if (enc_dbcs)
9499 {
9500 /* DBCS: assume double-wide characters are word characters. */
9501 for (i = 128; i <= 255; ++i)
9502 if (MB_BYTE2LEN(i) == 2)
9503 spelltab.st_isw[i] = TRUE;
9504 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009505 else if (enc_utf8)
9506 {
9507 for (i = 128; i < 256; ++i)
9508 {
9509 spelltab.st_isu[i] = utf_isupper(i);
9510 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
9511 spelltab.st_fold[i] = utf_fold(i);
9512 spelltab.st_upper[i] = utf_toupper(i);
9513 }
9514 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009515 else
9516#endif
9517 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009518 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009519 for (i = 128; i < 256; ++i)
9520 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009521 if (MB_ISUPPER(i))
9522 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009523 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009524 spelltab.st_isu[i] = TRUE;
9525 spelltab.st_fold[i] = MB_TOLOWER(i);
9526 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009527 else if (MB_ISLOWER(i))
9528 {
9529 spelltab.st_isw[i] = TRUE;
9530 spelltab.st_upper[i] = MB_TOUPPER(i);
9531 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009532 }
9533 }
9534}
9535
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009536/*
9537 * Set the spell character tables from strings in the affix file.
9538 */
9539 static int
9540set_spell_chartab(fol, low, upp)
9541 char_u *fol;
9542 char_u *low;
9543 char_u *upp;
9544{
9545 /* We build the new tables here first, so that we can compare with the
9546 * previous one. */
9547 spelltab_T new_st;
9548 char_u *pf = fol, *pl = low, *pu = upp;
9549 int f, l, u;
9550
9551 clear_spell_chartab(&new_st);
9552
9553 while (*pf != NUL)
9554 {
9555 if (*pl == NUL || *pu == NUL)
9556 {
9557 EMSG(_(e_affform));
9558 return FAIL;
9559 }
9560#ifdef FEAT_MBYTE
9561 f = mb_ptr2char_adv(&pf);
9562 l = mb_ptr2char_adv(&pl);
9563 u = mb_ptr2char_adv(&pu);
9564#else
9565 f = *pf++;
9566 l = *pl++;
9567 u = *pu++;
9568#endif
9569 /* Every character that appears is a word character. */
9570 if (f < 256)
9571 new_st.st_isw[f] = TRUE;
9572 if (l < 256)
9573 new_st.st_isw[l] = TRUE;
9574 if (u < 256)
9575 new_st.st_isw[u] = TRUE;
9576
9577 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9578 * case-folding */
9579 if (l < 256 && l != f)
9580 {
9581 if (f >= 256)
9582 {
9583 EMSG(_(e_affrange));
9584 return FAIL;
9585 }
9586 new_st.st_fold[l] = f;
9587 }
9588
9589 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009590 * case-folding, it's upper case and the "UPP" is the upper case of
9591 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009592 if (u < 256 && u != f)
9593 {
9594 if (f >= 256)
9595 {
9596 EMSG(_(e_affrange));
9597 return FAIL;
9598 }
9599 new_st.st_fold[u] = f;
9600 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009601 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009602 }
9603 }
9604
9605 if (*pl != NUL || *pu != NUL)
9606 {
9607 EMSG(_(e_affform));
9608 return FAIL;
9609 }
9610
9611 return set_spell_finish(&new_st);
9612}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009613
9614/*
9615 * Set the spell character tables from strings in the .spl file.
9616 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009617 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009618set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009619 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009620 int cnt; /* length of "flags" */
9621 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009622{
9623 /* We build the new tables here first, so that we can compare with the
9624 * previous one. */
9625 spelltab_T new_st;
9626 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009627 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009628 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009629
9630 clear_spell_chartab(&new_st);
9631
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009632 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009633 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009634 if (i < cnt)
9635 {
9636 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9637 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9638 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009639
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009640 if (*p != NUL)
9641 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009642#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009643 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009644#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009645 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009646#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009647 new_st.st_fold[i + 128] = c;
9648 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9649 new_st.st_upper[c] = i + 128;
9650 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009651 }
9652
Bram Moolenaar5195e452005-08-19 20:32:47 +00009653 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009654}
9655
9656 static int
9657set_spell_finish(new_st)
9658 spelltab_T *new_st;
9659{
9660 int i;
9661
9662 if (did_set_spelltab)
9663 {
9664 /* check that it's the same table */
9665 for (i = 0; i < 256; ++i)
9666 {
9667 if (spelltab.st_isw[i] != new_st->st_isw[i]
9668 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009669 || spelltab.st_fold[i] != new_st->st_fold[i]
9670 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009671 {
9672 EMSG(_("E763: Word characters differ between spell files"));
9673 return FAIL;
9674 }
9675 }
9676 }
9677 else
9678 {
9679 /* copy the new spelltab into the one being used */
9680 spelltab = *new_st;
9681 did_set_spelltab = TRUE;
9682 }
9683
9684 return OK;
9685}
9686
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009687/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009688 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009689 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009690 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009691 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009692 */
9693 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009694spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00009695 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009696 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009697{
Bram Moolenaarea408852005-06-25 22:49:46 +00009698#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009699 char_u *s;
9700 int l;
9701 int c;
9702
9703 if (has_mbyte)
9704 {
9705 l = MB_BYTE2LEN(*p);
9706 s = p;
9707 if (l == 1)
9708 {
9709 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009710 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009711 {
9712 s = p + 1; /* skip a mid-word character */
9713 l = MB_BYTE2LEN(*s);
9714 }
9715 }
9716 else
9717 {
9718 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009719 if (c < 256 ? buf->b_spell_ismw[c]
9720 : (buf->b_spell_ismw_mb != NULL
9721 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009722 {
9723 s = p + l;
9724 l = MB_BYTE2LEN(*s);
9725 }
9726 }
9727
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009728 c = mb_ptr2char(s);
9729 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009730 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009731 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009732 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009733#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009734
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009735 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
9736}
9737
9738/*
9739 * Return TRUE if "p" points to a word character.
9740 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9741 */
9742 static int
9743spell_iswordp_nmw(p)
9744 char_u *p;
9745{
9746#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009747 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009748
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009749 if (has_mbyte)
9750 {
9751 c = mb_ptr2char(p);
9752 if (c > 255)
9753 return mb_get_class(p) >= 2;
9754 return spelltab.st_isw[c];
9755 }
9756#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009757 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009758}
9759
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009760#ifdef FEAT_MBYTE
9761/*
9762 * Return TRUE if "p" points to a word character.
9763 * Wide version of spell_iswordp().
9764 */
9765 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009766spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009767 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009768 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009769{
9770 int *s;
9771
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009772 if (*p < 256 ? buf->b_spell_ismw[*p]
9773 : (buf->b_spell_ismw_mb != NULL
9774 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009775 s = p + 1;
9776 else
9777 s = p;
9778
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009779 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009780 {
9781 if (enc_utf8)
9782 return utf_class(*s) >= 2;
9783 if (enc_dbcs)
9784 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
9785 return 0;
9786 }
9787 return spelltab.st_isw[*s];
9788}
9789#endif
9790
Bram Moolenaarea408852005-06-25 22:49:46 +00009791/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009792 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009793 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009794 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009795 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009796write_spell_prefcond(fd, gap)
9797 FILE *fd;
9798 garray_T *gap;
9799{
9800 int i;
9801 char_u *p;
9802 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009803 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009804
Bram Moolenaar5195e452005-08-19 20:32:47 +00009805 if (fd != NULL)
9806 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
9807
9808 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009809
9810 for (i = 0; i < gap->ga_len; ++i)
9811 {
9812 /* <prefcond> : <condlen> <condstr> */
9813 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009814 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009815 {
9816 len = STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009817 if (fd != NULL)
9818 {
9819 fputc(len, fd);
9820 fwrite(p, (size_t)len, (size_t)1, fd);
9821 }
9822 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009823 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009824 else if (fd != NULL)
9825 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009826 }
9827
Bram Moolenaar5195e452005-08-19 20:32:47 +00009828 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009829}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009830
9831/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009832 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
9833 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009834 * When using a multi-byte 'encoding' the length may change!
9835 * Returns FAIL when something wrong.
9836 */
9837 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009838spell_casefold(str, len, buf, buflen)
9839 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009840 int len;
9841 char_u *buf;
9842 int buflen;
9843{
9844 int i;
9845
9846 if (len >= buflen)
9847 {
9848 buf[0] = NUL;
9849 return FAIL; /* result will not fit */
9850 }
9851
9852#ifdef FEAT_MBYTE
9853 if (has_mbyte)
9854 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009855 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009856 char_u *p;
9857 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009858
9859 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009860 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009861 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009862 if (outi + MB_MAXBYTES > buflen)
9863 {
9864 buf[outi] = NUL;
9865 return FAIL;
9866 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009867 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009868 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009869 }
9870 buf[outi] = NUL;
9871 }
9872 else
9873#endif
9874 {
9875 /* Be quick for non-multibyte encodings. */
9876 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009877 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009878 buf[i] = NUL;
9879 }
9880
9881 return OK;
9882}
9883
Bram Moolenaar4770d092006-01-12 23:22:24 +00009884/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009885#define SPS_BEST 1
9886#define SPS_FAST 2
9887#define SPS_DOUBLE 4
9888
Bram Moolenaar4770d092006-01-12 23:22:24 +00009889static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
9890static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009891
9892/*
9893 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009894 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009895 */
9896 int
9897spell_check_sps()
9898{
9899 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009900 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009901 char_u buf[MAXPATHL];
9902 int f;
9903
9904 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009905 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009906
9907 for (p = p_sps; *p != NUL; )
9908 {
9909 copy_option_part(&p, buf, MAXPATHL, ",");
9910
9911 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009912 if (VIM_ISDIGIT(*buf))
9913 {
9914 s = buf;
9915 sps_limit = getdigits(&s);
9916 if (*s != NUL && !VIM_ISDIGIT(*s))
9917 f = -1;
9918 }
9919 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009920 f = SPS_BEST;
9921 else if (STRCMP(buf, "fast") == 0)
9922 f = SPS_FAST;
9923 else if (STRCMP(buf, "double") == 0)
9924 f = SPS_DOUBLE;
9925 else if (STRNCMP(buf, "expr:", 5) != 0
9926 && STRNCMP(buf, "file:", 5) != 0)
9927 f = -1;
9928
9929 if (f == -1 || (sps_flags != 0 && f != 0))
9930 {
9931 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009932 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009933 return FAIL;
9934 }
9935 if (f != 0)
9936 sps_flags = f;
9937 }
9938
9939 if (sps_flags == 0)
9940 sps_flags = SPS_BEST;
9941
9942 return OK;
9943}
9944
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009945/*
9946 * "z?": Find badly spelled word under or after the cursor.
9947 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009948 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00009949 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009950 */
9951 void
Bram Moolenaard12a1322005-08-21 22:08:24 +00009952spell_suggest(count)
9953 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009954{
9955 char_u *line;
9956 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009957 char_u wcopy[MAXWLEN + 2];
9958 char_u *p;
9959 int i;
9960 int c;
9961 suginfo_T sug;
9962 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009963 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009964 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009965 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009966 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009967 int badlen = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009968
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009969 if (no_spell_checking(curwin))
9970 return;
9971
9972#ifdef FEAT_VISUAL
9973 if (VIsual_active)
9974 {
9975 /* Use the Visually selected text as the bad word. But reject
9976 * a multi-line selection. */
9977 if (curwin->w_cursor.lnum != VIsual.lnum)
9978 {
9979 vim_beep();
9980 return;
9981 }
9982 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
9983 if (badlen < 0)
9984 badlen = -badlen;
9985 else
9986 curwin->w_cursor.col = VIsual.col;
9987 ++badlen;
9988 end_visual_mode();
9989 }
9990 else
9991#endif
9992 /* Find the start of the badly spelled word. */
9993 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00009994 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009995 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00009996 /* No bad word or it starts after the cursor: use the word under the
9997 * cursor. */
9998 curwin->w_cursor = prev_cursor;
9999 line = ml_get_curline();
10000 p = line + curwin->w_cursor.col;
10001 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010002 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010003 mb_ptr_back(line, p);
10004 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010005 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010006 mb_ptr_adv(p);
10007
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010008 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010009 {
10010 beep_flush();
10011 return;
10012 }
10013 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010014 }
10015
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010016 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010017
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010018 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010019 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010020
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010021 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010022
Bram Moolenaar5195e452005-08-19 20:32:47 +000010023 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
10024 * 'spellsuggest', whatever is smaller. */
10025 if (sps_limit > (int)Rows - 2)
10026 limit = (int)Rows - 2;
10027 else
10028 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010029 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010030 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010031
10032 if (sug.su_ga.ga_len == 0)
10033 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +000010034 else if (count > 0)
10035 {
10036 if (count > sug.su_ga.ga_len)
10037 smsg((char_u *)_("Sorry, only %ld suggestions"),
10038 (long)sug.su_ga.ga_len);
10039 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010040 else
10041 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010042 vim_free(repl_from);
10043 repl_from = NULL;
10044 vim_free(repl_to);
10045 repl_to = NULL;
10046
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010047#ifdef FEAT_RIGHTLEFT
10048 /* When 'rightleft' is set the list is drawn right-left. */
10049 cmdmsg_rl = curwin->w_p_rl;
10050 if (cmdmsg_rl)
10051 msg_col = Columns - 1;
10052#endif
10053
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010054 /* List the suggestions. */
10055 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010056 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010057 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
10058 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010059#ifdef FEAT_RIGHTLEFT
10060 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
10061 {
10062 /* And now the rabbit from the high hat: Avoid showing the
10063 * untranslated message rightleft. */
10064 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
10065 sug.su_badlen, sug.su_badptr);
10066 }
10067#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010068 msg_puts(IObuff);
10069 msg_clr_eos();
10070 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +000010071
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010072 msg_scroll = TRUE;
10073 for (i = 0; i < sug.su_ga.ga_len; ++i)
10074 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010075 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010076
10077 /* The suggested word may replace only part of the bad word, add
10078 * the not replaced part. */
10079 STRCPY(wcopy, stp->st_word);
10080 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010081 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010082 sug.su_badptr + stp->st_orglen,
10083 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010084 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
10085#ifdef FEAT_RIGHTLEFT
10086 if (cmdmsg_rl)
10087 rl_mirror(IObuff);
10088#endif
10089 msg_puts(IObuff);
10090
10091 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010092 msg_puts(IObuff);
10093
10094 /* The word may replace more than "su_badlen". */
10095 if (sug.su_badlen < stp->st_orglen)
10096 {
10097 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
10098 stp->st_orglen, sug.su_badptr);
10099 msg_puts(IObuff);
10100 }
10101
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010102 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010103 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010104 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010105 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010106 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010107 stp->st_salscore ? "s " : "",
10108 stp->st_score, stp->st_altscore);
10109 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010110 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +000010111 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010112#ifdef FEAT_RIGHTLEFT
10113 if (cmdmsg_rl)
10114 /* Mirror the numbers, but keep the leading space. */
10115 rl_mirror(IObuff + 1);
10116#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +000010117 msg_advance(30);
10118 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010119 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010120 msg_putchar('\n');
10121 }
10122
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010123#ifdef FEAT_RIGHTLEFT
10124 cmdmsg_rl = FALSE;
10125 msg_col = 0;
10126#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010127 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010128 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010129 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010130 selected -= lines_left;
Bram Moolenaar0fd92892006-03-09 22:27:48 +000010131 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010132 }
10133
Bram Moolenaard12a1322005-08-21 22:08:24 +000010134 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
10135 {
10136 /* Save the from and to text for :spellrepall. */
10137 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000010138 if (sug.su_badlen > stp->st_orglen)
10139 {
10140 /* Replacing less than "su_badlen", append the remainder to
10141 * repl_to. */
10142 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
10143 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
10144 sug.su_badlen - stp->st_orglen,
10145 sug.su_badptr + stp->st_orglen);
10146 repl_to = vim_strsave(IObuff);
10147 }
10148 else
10149 {
10150 /* Replacing su_badlen or more, use the whole word. */
10151 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
10152 repl_to = vim_strsave(stp->st_word);
10153 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010154
10155 /* Replace the word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010156 p = alloc(STRLEN(line) - stp->st_orglen + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010157 if (p != NULL)
10158 {
10159 c = sug.su_badptr - line;
10160 mch_memmove(p, line, c);
10161 STRCPY(p + c, stp->st_word);
10162 STRCAT(p, sug.su_badptr + stp->st_orglen);
10163 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10164 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010165
10166 /* For redo we use a change-word command. */
10167 ResetRedobuff();
10168 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +000010169 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010170 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010171 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010172
10173 /* After this "p" may be invalid. */
10174 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010175 }
10176 }
10177 else
10178 curwin->w_cursor = prev_cursor;
10179
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010180 spell_find_cleanup(&sug);
10181}
10182
10183/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010184 * Check if the word at line "lnum" column "col" is required to start with a
10185 * capital. This uses 'spellcapcheck' of the current buffer.
10186 */
10187 static int
10188check_need_cap(lnum, col)
10189 linenr_T lnum;
10190 colnr_T col;
10191{
10192 int need_cap = FALSE;
10193 char_u *line;
10194 char_u *line_copy = NULL;
10195 char_u *p;
10196 colnr_T endcol;
10197 regmatch_T regmatch;
10198
10199 if (curbuf->b_cap_prog == NULL)
10200 return FALSE;
10201
10202 line = ml_get_curline();
10203 endcol = 0;
10204 if ((int)(skipwhite(line) - line) >= (int)col)
10205 {
10206 /* At start of line, check if previous line is empty or sentence
10207 * ends there. */
10208 if (lnum == 1)
10209 need_cap = TRUE;
10210 else
10211 {
10212 line = ml_get(lnum - 1);
10213 if (*skipwhite(line) == NUL)
10214 need_cap = TRUE;
10215 else
10216 {
10217 /* Append a space in place of the line break. */
10218 line_copy = concat_str(line, (char_u *)" ");
10219 line = line_copy;
10220 endcol = STRLEN(line);
10221 }
10222 }
10223 }
10224 else
10225 endcol = col;
10226
10227 if (endcol > 0)
10228 {
10229 /* Check if sentence ends before the bad word. */
10230 regmatch.regprog = curbuf->b_cap_prog;
10231 regmatch.rm_ic = FALSE;
10232 p = line + endcol;
10233 for (;;)
10234 {
10235 mb_ptr_back(line, p);
10236 if (p == line || spell_iswordp_nmw(p))
10237 break;
10238 if (vim_regexec(&regmatch, p, 0)
10239 && regmatch.endp[0] == line + endcol)
10240 {
10241 need_cap = TRUE;
10242 break;
10243 }
10244 }
10245 }
10246
10247 vim_free(line_copy);
10248
10249 return need_cap;
10250}
10251
10252
10253/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010254 * ":spellrepall"
10255 */
10256/*ARGSUSED*/
10257 void
10258ex_spellrepall(eap)
10259 exarg_T *eap;
10260{
10261 pos_T pos = curwin->w_cursor;
10262 char_u *frompat;
10263 int addlen;
10264 char_u *line;
10265 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010266 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010267 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010268
10269 if (repl_from == NULL || repl_to == NULL)
10270 {
10271 EMSG(_("E752: No previous spell replacement"));
10272 return;
10273 }
10274 addlen = STRLEN(repl_to) - STRLEN(repl_from);
10275
10276 frompat = alloc(STRLEN(repl_from) + 7);
10277 if (frompat == NULL)
10278 return;
10279 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
10280 p_ws = FALSE;
10281
Bram Moolenaar5195e452005-08-19 20:32:47 +000010282 sub_nsubs = 0;
10283 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010284 curwin->w_cursor.lnum = 0;
10285 while (!got_int)
10286 {
10287 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
10288 || u_save_cursor() == FAIL)
10289 break;
10290
10291 /* Only replace when the right word isn't there yet. This happens
10292 * when changing "etc" to "etc.". */
10293 line = ml_get_curline();
10294 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
10295 repl_to, STRLEN(repl_to)) != 0)
10296 {
10297 p = alloc(STRLEN(line) + addlen + 1);
10298 if (p == NULL)
10299 break;
10300 mch_memmove(p, line, curwin->w_cursor.col);
10301 STRCPY(p + curwin->w_cursor.col, repl_to);
10302 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
10303 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10304 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010305
10306 if (curwin->w_cursor.lnum != prev_lnum)
10307 {
10308 ++sub_nlines;
10309 prev_lnum = curwin->w_cursor.lnum;
10310 }
10311 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010312 }
10313 curwin->w_cursor.col += STRLEN(repl_to);
10314 }
10315
10316 p_ws = save_ws;
10317 curwin->w_cursor = pos;
10318 vim_free(frompat);
10319
Bram Moolenaar5195e452005-08-19 20:32:47 +000010320 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010321 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010322 else
10323 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010324}
10325
10326/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010327 * Find spell suggestions for "word". Return them in the growarray "*gap" as
10328 * a list of allocated strings.
10329 */
10330 void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010331spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010332 garray_T *gap;
10333 char_u *word;
10334 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010335 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010336 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010337{
10338 suginfo_T sug;
10339 int i;
10340 suggest_T *stp;
10341 char_u *wcopy;
10342
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010343 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010344
10345 /* Make room in "gap". */
10346 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010347 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010348 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010349 for (i = 0; i < sug.su_ga.ga_len; ++i)
10350 {
10351 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010352
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010353 /* The suggested word may replace only part of "word", add the not
10354 * replaced part. */
10355 wcopy = alloc(stp->st_wordlen
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010356 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010357 if (wcopy == NULL)
10358 break;
10359 STRCPY(wcopy, stp->st_word);
10360 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
10361 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
10362 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010363 }
10364
10365 spell_find_cleanup(&sug);
10366}
10367
10368/*
10369 * Find spell suggestions for the word at the start of "badptr".
10370 * Return the suggestions in "su->su_ga".
10371 * The maximum number of suggestions is "maxcount".
10372 * Note: does use info for the current window.
10373 * This is based on the mechanisms of Aspell, but completely reimplemented.
10374 */
10375 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010376spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010377 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010378 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010379 suginfo_T *su;
10380 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +000010381 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010382 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010383 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010384{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010385 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010386 char_u buf[MAXPATHL];
10387 char_u *p;
10388 int do_combine = FALSE;
10389 char_u *sps_copy;
10390#ifdef FEAT_EVAL
10391 static int expr_busy = FALSE;
10392#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010393 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010394 int i;
10395 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010396
10397 /*
10398 * Set the info in "*su".
10399 */
10400 vim_memset(su, 0, sizeof(suginfo_T));
10401 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10402 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010403 if (*badptr == NUL)
10404 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010405 hash_init(&su->su_banned);
10406
10407 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010408 if (badlen != 0)
10409 su->su_badlen = badlen;
10410 else
10411 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010412 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010413 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010414
10415 if (su->su_badlen >= MAXWLEN)
10416 su->su_badlen = MAXWLEN - 1; /* just in case */
10417 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10418 (void)spell_casefold(su->su_badptr, su->su_badlen,
10419 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010420 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010421 su->su_badflags = badword_captype(su->su_badptr,
10422 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010423 if (need_cap)
10424 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010425
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010426 /* Find the default language for sound folding. We simply use the first
10427 * one in 'spelllang' that supports sound folding. That's good for when
10428 * using multiple files for one language, it's not that bad when mixing
10429 * languages (e.g., "pl,en"). */
10430 for (i = 0; i < curbuf->b_langp.ga_len; ++i)
10431 {
10432 lp = LANGP_ENTRY(curbuf->b_langp, i);
10433 if (lp->lp_sallang != NULL)
10434 {
10435 su->su_sallang = lp->lp_sallang;
10436 break;
10437 }
10438 }
10439
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010440 /* Soundfold the bad word with the default sound folding, so that we don't
10441 * have to do this many times. */
10442 if (su->su_sallang != NULL)
10443 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10444 su->su_sal_badword);
10445
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010446 /* If the word is not capitalised and spell_check() doesn't consider the
10447 * word to be bad then it might need to be capitalised. Add a suggestion
10448 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010449 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010450 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010451 {
10452 make_case_word(su->su_badword, buf, WF_ONECAP);
10453 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010454 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010455 }
10456
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010457 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +000010458 if (banbadword)
10459 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010460
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010461 /* Make a copy of 'spellsuggest', because the expression may change it. */
10462 sps_copy = vim_strsave(p_sps);
10463 if (sps_copy == NULL)
10464 return;
10465
10466 /* Loop over the items in 'spellsuggest'. */
10467 for (p = sps_copy; *p != NUL; )
10468 {
10469 copy_option_part(&p, buf, MAXPATHL, ",");
10470
10471 if (STRNCMP(buf, "expr:", 5) == 0)
10472 {
10473#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010474 /* Evaluate an expression. Skip this when called recursively,
10475 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010476 if (!expr_busy)
10477 {
10478 expr_busy = TRUE;
10479 spell_suggest_expr(su, buf + 5);
10480 expr_busy = FALSE;
10481 }
10482#endif
10483 }
10484 else if (STRNCMP(buf, "file:", 5) == 0)
10485 /* Use list of suggestions in a file. */
10486 spell_suggest_file(su, buf + 5);
10487 else
10488 {
10489 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010490 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010491 if (sps_flags & SPS_DOUBLE)
10492 do_combine = TRUE;
10493 }
10494 }
10495
10496 vim_free(sps_copy);
10497
10498 if (do_combine)
10499 /* Combine the two list of suggestions. This must be done last,
10500 * because sorting changes the order again. */
10501 score_combine(su);
10502}
10503
10504#ifdef FEAT_EVAL
10505/*
10506 * Find suggestions by evaluating expression "expr".
10507 */
10508 static void
10509spell_suggest_expr(su, expr)
10510 suginfo_T *su;
10511 char_u *expr;
10512{
10513 list_T *list;
10514 listitem_T *li;
10515 int score;
10516 char_u *p;
10517
10518 /* The work is split up in a few parts to avoid having to export
10519 * suginfo_T.
10520 * First evaluate the expression and get the resulting list. */
10521 list = eval_spell_expr(su->su_badword, expr);
10522 if (list != NULL)
10523 {
10524 /* Loop over the items in the list. */
10525 for (li = list->lv_first; li != NULL; li = li->li_next)
10526 if (li->li_tv.v_type == VAR_LIST)
10527 {
10528 /* Get the word and the score from the items. */
10529 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010530 if (score >= 0 && score <= su->su_maxscore)
10531 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10532 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010533 }
10534 list_unref(list);
10535 }
10536
Bram Moolenaar4770d092006-01-12 23:22:24 +000010537 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10538 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010539 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10540}
10541#endif
10542
10543/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010544 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010545 */
10546 static void
10547spell_suggest_file(su, fname)
10548 suginfo_T *su;
10549 char_u *fname;
10550{
10551 FILE *fd;
10552 char_u line[MAXWLEN * 2];
10553 char_u *p;
10554 int len;
10555 char_u cword[MAXWLEN];
10556
10557 /* Open the file. */
10558 fd = mch_fopen((char *)fname, "r");
10559 if (fd == NULL)
10560 {
10561 EMSG2(_(e_notopen), fname);
10562 return;
10563 }
10564
10565 /* Read it line by line. */
10566 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10567 {
10568 line_breakcheck();
10569
10570 p = vim_strchr(line, '/');
10571 if (p == NULL)
10572 continue; /* No Tab found, just skip the line. */
10573 *p++ = NUL;
10574 if (STRICMP(su->su_badword, line) == 0)
10575 {
10576 /* Match! Isolate the good word, until CR or NL. */
10577 for (len = 0; p[len] >= ' '; ++len)
10578 ;
10579 p[len] = NUL;
10580
10581 /* If the suggestion doesn't have specific case duplicate the case
10582 * of the bad word. */
10583 if (captype(p, NULL) == 0)
10584 {
10585 make_case_word(p, cword, su->su_badflags);
10586 p = cword;
10587 }
10588
10589 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010590 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010591 }
10592 }
10593
10594 fclose(fd);
10595
Bram Moolenaar4770d092006-01-12 23:22:24 +000010596 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10597 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010598 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10599}
10600
10601/*
10602 * Find suggestions for the internal method indicated by "sps_flags".
10603 */
10604 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010605spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010606 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010607 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010608{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010609 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010610 * Load the .sug file(s) that are available and not done yet.
10611 */
10612 suggest_load_files();
10613
10614 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010615 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010616 *
10617 * Set a maximum score to limit the combination of operations that is
10618 * tried.
10619 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010620 suggest_try_special(su);
10621
10622 /*
10623 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10624 * from the .aff file and inserting a space (split the word).
10625 */
10626 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010627
10628 /* For the resulting top-scorers compute the sound-a-like score. */
10629 if (sps_flags & SPS_DOUBLE)
10630 score_comp_sal(su);
10631
10632 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010633 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010634 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010635 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010636 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010637 if (sps_flags & SPS_BEST)
10638 /* Adjust the word score for the suggestions found so far for how
10639 * they sounds like. */
10640 rescore_suggestions(su);
10641
10642 /*
10643 * While going throught the soundfold tree "su_maxscore" is the score
10644 * for the soundfold word, limits the changes that are being tried,
10645 * and "su_sfmaxscore" the rescored score, which is set by
10646 * cleanup_suggestions().
10647 * First find words with a small edit distance, because this is much
10648 * faster and often already finds the top-N suggestions. If we didn't
10649 * find many suggestions try again with a higher edit distance.
10650 * "sl_sounddone" is used to avoid doing the same word twice.
10651 */
10652 suggest_try_soundalike_prep();
10653 su->su_maxscore = SCORE_SFMAX1;
10654 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010655 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010656 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10657 {
10658 /* We didn't find enough matches, try again, allowing more
10659 * changes to the soundfold word. */
10660 su->su_maxscore = SCORE_SFMAX2;
10661 suggest_try_soundalike(su);
10662 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10663 {
10664 /* Still didn't find enough matches, try again, allowing even
10665 * more changes to the soundfold word. */
10666 su->su_maxscore = SCORE_SFMAX3;
10667 suggest_try_soundalike(su);
10668 }
10669 }
10670 su->su_maxscore = su->su_sfmaxscore;
10671 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010672 }
10673
Bram Moolenaar4770d092006-01-12 23:22:24 +000010674 /* When CTRL-C was hit while searching do show the results. Only clear
10675 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010676 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010677 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010678 {
10679 (void)vgetc();
10680 got_int = FALSE;
10681 }
10682
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010683 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010684 {
10685 if (sps_flags & SPS_BEST)
10686 /* Adjust the word score for how it sounds like. */
10687 rescore_suggestions(su);
10688
Bram Moolenaar4770d092006-01-12 23:22:24 +000010689 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10690 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010691 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010692 }
10693}
10694
10695/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010696 * Load the .sug files for languages that have one and weren't loaded yet.
10697 */
10698 static void
10699suggest_load_files()
10700{
10701 langp_T *lp;
10702 int lpi;
10703 slang_T *slang;
10704 char_u *dotp;
10705 FILE *fd;
10706 char_u buf[MAXWLEN];
10707 int i;
10708 time_t timestamp;
10709 int wcount;
10710 int wordnr;
10711 garray_T ga;
10712 int c;
10713
10714 /* Do this for all languages that support sound folding. */
10715 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
10716 {
10717 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
10718 slang = lp->lp_slang;
10719 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10720 {
10721 /* Change ".spl" to ".sug" and open the file. When the file isn't
10722 * found silently skip it. Do set "sl_sugloaded" so that we
10723 * don't try again and again. */
10724 slang->sl_sugloaded = TRUE;
10725
10726 dotp = vim_strrchr(slang->sl_fname, '.');
10727 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10728 continue;
10729 STRCPY(dotp, ".sug");
Bram Moolenaar5555acc2006-04-07 21:33:12 +000010730 fd = mch_fopen((char *)slang->sl_fname, "r");
Bram Moolenaar4770d092006-01-12 23:22:24 +000010731 if (fd == NULL)
10732 goto nextone;
10733
10734 /*
10735 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10736 */
10737 for (i = 0; i < VIMSUGMAGICL; ++i)
10738 buf[i] = getc(fd); /* <fileID> */
10739 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10740 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010741 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010742 slang->sl_fname);
10743 goto nextone;
10744 }
10745 c = getc(fd); /* <versionnr> */
10746 if (c < VIMSUGVERSION)
10747 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010748 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010749 slang->sl_fname);
10750 goto nextone;
10751 }
10752 else if (c > VIMSUGVERSION)
10753 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010754 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010755 slang->sl_fname);
10756 goto nextone;
10757 }
10758
10759 /* Check the timestamp, it must be exactly the same as the one in
10760 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010761 timestamp = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010762 if (timestamp != slang->sl_sugtime)
10763 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010764 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010765 slang->sl_fname);
10766 goto nextone;
10767 }
10768
10769 /*
10770 * <SUGWORDTREE>: <wordtree>
10771 * Read the trie with the soundfolded words.
10772 */
10773 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10774 FALSE, 0) != 0)
10775 {
10776someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010777 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010778 slang->sl_fname);
10779 slang_clear_sug(slang);
10780 goto nextone;
10781 }
10782
10783 /*
10784 * <SUGTABLE>: <sugwcount> <sugline> ...
10785 *
10786 * Read the table with word numbers. We use a file buffer for
10787 * this, because it's so much like a file with lines. Makes it
10788 * possible to swap the info and save on memory use.
10789 */
10790 slang->sl_sugbuf = open_spellbuf();
10791 if (slang->sl_sugbuf == NULL)
10792 goto someerror;
10793 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010794 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010795 if (wcount < 0)
10796 goto someerror;
10797
10798 /* Read all the wordnr lists into the buffer, one NUL terminated
10799 * list per line. */
10800 ga_init2(&ga, 1, 100);
10801 for (wordnr = 0; wordnr < wcount; ++wordnr)
10802 {
10803 ga.ga_len = 0;
10804 for (;;)
10805 {
10806 c = getc(fd); /* <sugline> */
10807 if (c < 0 || ga_grow(&ga, 1) == FAIL)
10808 goto someerror;
10809 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
10810 if (c == NUL)
10811 break;
10812 }
10813 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
10814 ga.ga_data, ga.ga_len, TRUE) == FAIL)
10815 goto someerror;
10816 }
10817 ga_clear(&ga);
10818
10819 /*
10820 * Need to put word counts in the word tries, so that we can find
10821 * a word by its number.
10822 */
10823 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
10824 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
10825
10826nextone:
10827 if (fd != NULL)
10828 fclose(fd);
10829 STRCPY(dotp, ".spl");
10830 }
10831 }
10832}
10833
10834
10835/*
10836 * Fill in the wordcount fields for a trie.
10837 * Returns the total number of words.
10838 */
10839 static void
10840tree_count_words(byts, idxs)
10841 char_u *byts;
10842 idx_T *idxs;
10843{
10844 int depth;
10845 idx_T arridx[MAXWLEN];
10846 int curi[MAXWLEN];
10847 int c;
10848 idx_T n;
10849 int wordcount[MAXWLEN];
10850
10851 arridx[0] = 0;
10852 curi[0] = 1;
10853 wordcount[0] = 0;
10854 depth = 0;
10855 while (depth >= 0 && !got_int)
10856 {
10857 if (curi[depth] > byts[arridx[depth]])
10858 {
10859 /* Done all bytes at this node, go up one level. */
10860 idxs[arridx[depth]] = wordcount[depth];
10861 if (depth > 0)
10862 wordcount[depth - 1] += wordcount[depth];
10863
10864 --depth;
10865 fast_breakcheck();
10866 }
10867 else
10868 {
10869 /* Do one more byte at this node. */
10870 n = arridx[depth] + curi[depth];
10871 ++curi[depth];
10872
10873 c = byts[n];
10874 if (c == 0)
10875 {
10876 /* End of word, count it. */
10877 ++wordcount[depth];
10878
10879 /* Skip over any other NUL bytes (same word with different
10880 * flags). */
10881 while (byts[n + 1] == 0)
10882 {
10883 ++n;
10884 ++curi[depth];
10885 }
10886 }
10887 else
10888 {
10889 /* Normal char, go one level deeper to count the words. */
10890 ++depth;
10891 arridx[depth] = idxs[n];
10892 curi[depth] = 1;
10893 wordcount[depth] = 0;
10894 }
10895 }
10896 }
10897}
10898
10899/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010900 * Free the info put in "*su" by spell_find_suggest().
10901 */
10902 static void
10903spell_find_cleanup(su)
10904 suginfo_T *su;
10905{
10906 int i;
10907
10908 /* Free the suggestions. */
10909 for (i = 0; i < su->su_ga.ga_len; ++i)
10910 vim_free(SUG(su->su_ga, i).st_word);
10911 ga_clear(&su->su_ga);
10912 for (i = 0; i < su->su_sga.ga_len; ++i)
10913 vim_free(SUG(su->su_sga, i).st_word);
10914 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010915
10916 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010917 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010918}
10919
10920/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010921 * Make a copy of "word", with the first letter upper or lower cased, to
10922 * "wcopy[MAXWLEN]". "word" must not be empty.
10923 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010924 */
10925 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010926onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010927 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010928 char_u *wcopy;
10929 int upper; /* TRUE: first letter made upper case */
10930{
10931 char_u *p;
10932 int c;
10933 int l;
10934
10935 p = word;
10936#ifdef FEAT_MBYTE
10937 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010938 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010939 else
10940#endif
10941 c = *p++;
10942 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010943 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010944 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010945 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010946#ifdef FEAT_MBYTE
10947 if (has_mbyte)
10948 l = mb_char2bytes(c, wcopy);
10949 else
10950#endif
10951 {
10952 l = 1;
10953 wcopy[0] = c;
10954 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010955 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010956}
10957
10958/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010959 * Make a copy of "word" with all the letters upper cased into
10960 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010961 */
10962 static void
10963allcap_copy(word, wcopy)
10964 char_u *word;
10965 char_u *wcopy;
10966{
10967 char_u *s;
10968 char_u *d;
10969 int c;
10970
10971 d = wcopy;
10972 for (s = word; *s != NUL; )
10973 {
10974#ifdef FEAT_MBYTE
10975 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010976 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010977 else
10978#endif
10979 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000010980
10981#ifdef FEAT_MBYTE
10982 /* We only change ß to SS when we are certain latin1 is used. It
10983 * would cause weird errors in other 8-bit encodings. */
10984 if (enc_latin1like && c == 0xdf)
10985 {
10986 c = 'S';
10987 if (d - wcopy >= MAXWLEN - 1)
10988 break;
10989 *d++ = c;
10990 }
10991 else
10992#endif
10993 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010994
10995#ifdef FEAT_MBYTE
10996 if (has_mbyte)
10997 {
10998 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
10999 break;
11000 d += mb_char2bytes(c, d);
11001 }
11002 else
11003#endif
11004 {
11005 if (d - wcopy >= MAXWLEN - 1)
11006 break;
11007 *d++ = c;
11008 }
11009 }
11010 *d = NUL;
11011}
11012
11013/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000011014 * Try finding suggestions by recognizing specific situations.
11015 */
11016 static void
11017suggest_try_special(su)
11018 suginfo_T *su;
11019{
11020 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011021 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011022 int c;
11023 char_u word[MAXWLEN];
11024
11025 /*
11026 * Recognize a word that is repeated: "the the".
11027 */
11028 p = skiptowhite(su->su_fbadword);
11029 len = p - su->su_fbadword;
11030 p = skipwhite(p);
11031 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
11032 {
11033 /* Include badflags: if the badword is onecap or allcap
11034 * use that for the goodword too: "The the" -> "The". */
11035 c = su->su_fbadword[len];
11036 su->su_fbadword[len] = NUL;
11037 make_case_word(su->su_fbadword, word, su->su_badflags);
11038 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011039
11040 /* Give a soundalike score of 0, compute the score as if deleting one
11041 * character. */
11042 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011043 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011044 }
11045}
11046
11047/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011048 * Try finding suggestions by adding/removing/swapping letters.
11049 */
11050 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000011051suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011052 suginfo_T *su;
11053{
11054 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011055 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011056 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011057 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011058 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011059
11060 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000011061 * to find matches (esp. REP items). Append some more text, changing
11062 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011063 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011064 n = STRLEN(fword);
11065 p = su->su_badptr + su->su_badlen;
11066 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011067
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011068 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011069 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011070 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011071
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011072 /* If reloading a spell file fails it's still in the list but
11073 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011074 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011075 continue;
11076
Bram Moolenaar4770d092006-01-12 23:22:24 +000011077 /* Try it for this language. Will add possible suggestions. */
11078 suggest_trie_walk(su, lp, fword, FALSE);
11079 }
11080}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011081
Bram Moolenaar4770d092006-01-12 23:22:24 +000011082/* Check the maximum score, if we go over it we won't try this change. */
11083#define TRY_DEEPER(su, stack, depth, add) \
11084 (stack[depth].ts_score + (add) < su->su_maxscore)
11085
11086/*
11087 * Try finding suggestions by adding/removing/swapping letters.
11088 *
11089 * This uses a state machine. At each node in the tree we try various
11090 * operations. When trying if an operation works "depth" is increased and the
11091 * stack[] is used to store info. This allows combinations, thus insert one
11092 * character, replace one and delete another. The number of changes is
11093 * limited by su->su_maxscore.
11094 *
11095 * After implementing this I noticed an article by Kemal Oflazer that
11096 * describes something similar: "Error-tolerant Finite State Recognition with
11097 * Applications to Morphological Analysis and Spelling Correction" (1996).
11098 * The implementation in the article is simplified and requires a stack of
11099 * unknown depth. The implementation here only needs a stack depth equal to
11100 * the length of the word.
11101 *
11102 * This is also used for the sound-folded word, "soundfold" is TRUE then.
11103 * The mechanism is the same, but we find a match with a sound-folded word
11104 * that comes from one or more original words. Each of these words may be
11105 * added, this is done by add_sound_suggest().
11106 * Don't use:
11107 * the prefix tree or the keep-case tree
11108 * "su->su_badlen"
11109 * anything to do with upper and lower case
11110 * anything to do with word or non-word characters ("spell_iswordp()")
11111 * banned words
11112 * word flags (rare, region, compounding)
11113 * word splitting for now
11114 * "similar_chars()"
11115 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
11116 */
11117 static void
11118suggest_trie_walk(su, lp, fword, soundfold)
11119 suginfo_T *su;
11120 langp_T *lp;
11121 char_u *fword;
11122 int soundfold;
11123{
11124 char_u tword[MAXWLEN]; /* good word collected so far */
11125 trystate_T stack[MAXWLEN];
11126 char_u preword[MAXWLEN * 3]; /* word found with proper case;
11127 * concatanation of prefix compound
11128 * words and split word. NUL terminated
11129 * when going deeper but not when coming
11130 * back. */
11131 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
11132 trystate_T *sp;
11133 int newscore;
11134 int score;
11135 char_u *byts, *fbyts, *pbyts;
11136 idx_T *idxs, *fidxs, *pidxs;
11137 int depth;
11138 int c, c2, c3;
11139 int n = 0;
11140 int flags;
11141 garray_T *gap;
11142 idx_T arridx;
11143 int len;
11144 char_u *p;
11145 fromto_T *ftp;
11146 int fl = 0, tl;
11147 int repextra = 0; /* extra bytes in fword[] from REP item */
11148 slang_T *slang = lp->lp_slang;
11149 int fword_ends;
11150 int goodword_ends;
11151#ifdef DEBUG_TRIEWALK
11152 /* Stores the name of the change made at each level. */
11153 char_u changename[MAXWLEN][80];
11154#endif
11155 int breakcheckcount = 1000;
11156 int compound_ok;
11157
11158 /*
11159 * Go through the whole case-fold tree, try changes at each node.
11160 * "tword[]" contains the word collected from nodes in the tree.
11161 * "fword[]" the word we are trying to match with (initially the bad
11162 * word).
11163 */
11164 depth = 0;
11165 sp = &stack[0];
11166 vim_memset(sp, 0, sizeof(trystate_T));
11167 sp->ts_curi = 1;
11168
11169 if (soundfold)
11170 {
11171 /* Going through the soundfold tree. */
11172 byts = fbyts = slang->sl_sbyts;
11173 idxs = fidxs = slang->sl_sidxs;
11174 pbyts = NULL;
11175 pidxs = NULL;
11176 sp->ts_prefixdepth = PFD_NOPREFIX;
11177 sp->ts_state = STATE_START;
11178 }
11179 else
11180 {
Bram Moolenaarea424162005-06-16 21:51:00 +000011181 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011182 * When there are postponed prefixes we need to use these first. At
11183 * the end of the prefix we continue in the case-fold tree.
11184 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011185 fbyts = slang->sl_fbyts;
11186 fidxs = slang->sl_fidxs;
11187 pbyts = slang->sl_pbyts;
11188 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011189 if (pbyts != NULL)
11190 {
11191 byts = pbyts;
11192 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011193 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011194 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
11195 }
11196 else
11197 {
11198 byts = fbyts;
11199 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011200 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011201 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011202 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011203 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011204
Bram Moolenaar4770d092006-01-12 23:22:24 +000011205 /*
11206 * Loop to find all suggestions. At each round we either:
11207 * - For the current state try one operation, advance "ts_curi",
11208 * increase "depth".
11209 * - When a state is done go to the next, set "ts_state".
11210 * - When all states are tried decrease "depth".
11211 */
11212 while (depth >= 0 && !got_int)
11213 {
11214 sp = &stack[depth];
11215 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011216 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011217 case STATE_START:
11218 case STATE_NOPREFIX:
11219 /*
11220 * Start of node: Deal with NUL bytes, which means
11221 * tword[] may end here.
11222 */
11223 arridx = sp->ts_arridx; /* current node in the tree */
11224 len = byts[arridx]; /* bytes in this node */
11225 arridx += sp->ts_curi; /* index of current byte */
11226
11227 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011228 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011229 /* Skip over the NUL bytes, we use them later. */
11230 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
11231 ;
11232 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011233
Bram Moolenaar4770d092006-01-12 23:22:24 +000011234 /* Always past NUL bytes now. */
11235 n = (int)sp->ts_state;
11236 sp->ts_state = STATE_ENDNUL;
11237 sp->ts_save_badflags = su->su_badflags;
11238
11239 /* At end of a prefix or at start of prefixtree: check for
11240 * following word. */
11241 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011242 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011243 /* Set su->su_badflags to the caps type at this position.
11244 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000011245#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011246 if (has_mbyte)
11247 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
11248 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000011249#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011250 n = sp->ts_fidx;
11251 flags = badword_captype(su->su_badptr, su->su_badptr + n);
11252 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011253 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011254#ifdef DEBUG_TRIEWALK
11255 sprintf(changename[depth], "prefix");
11256#endif
11257 go_deeper(stack, depth, 0);
11258 ++depth;
11259 sp = &stack[depth];
11260 sp->ts_prefixdepth = depth - 1;
11261 byts = fbyts;
11262 idxs = fidxs;
11263 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011264
Bram Moolenaar4770d092006-01-12 23:22:24 +000011265 /* Move the prefix to preword[] with the right case
11266 * and make find_keepcap_word() works. */
11267 tword[sp->ts_twordlen] = NUL;
11268 make_case_word(tword + sp->ts_splitoff,
11269 preword + sp->ts_prewordlen, flags);
11270 sp->ts_prewordlen = STRLEN(preword);
11271 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011272 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011273 break;
11274 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011275
Bram Moolenaar4770d092006-01-12 23:22:24 +000011276 if (sp->ts_curi > len || byts[arridx] != 0)
11277 {
11278 /* Past bytes in node and/or past NUL bytes. */
11279 sp->ts_state = STATE_ENDNUL;
11280 sp->ts_save_badflags = su->su_badflags;
11281 break;
11282 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011283
Bram Moolenaar4770d092006-01-12 23:22:24 +000011284 /*
11285 * End of word in tree.
11286 */
11287 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011288
Bram Moolenaar4770d092006-01-12 23:22:24 +000011289 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011290
11291 /* Skip words with the NOSUGGEST flag. */
11292 if (flags & WF_NOSUGGEST)
11293 break;
11294
Bram Moolenaar4770d092006-01-12 23:22:24 +000011295 fword_ends = (fword[sp->ts_fidx] == NUL
11296 || (soundfold
11297 ? vim_iswhite(fword[sp->ts_fidx])
11298 : !spell_iswordp(fword + sp->ts_fidx, curbuf)));
11299 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011300
Bram Moolenaar4770d092006-01-12 23:22:24 +000011301 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000011302 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011303 {
11304 /* There was a prefix before the word. Check that the prefix
11305 * can be used with this word. */
11306 /* Count the length of the NULs in the prefix. If there are
11307 * none this must be the first try without a prefix. */
11308 n = stack[sp->ts_prefixdepth].ts_arridx;
11309 len = pbyts[n++];
11310 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
11311 ;
11312 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011313 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011314 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011315 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011316 if (c == 0)
11317 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011318
Bram Moolenaar4770d092006-01-12 23:22:24 +000011319 /* Use the WF_RARE flag for a rare prefix. */
11320 if (c & WF_RAREPFX)
11321 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011322
Bram Moolenaar4770d092006-01-12 23:22:24 +000011323 /* Tricky: when checking for both prefix and compounding
11324 * we run into the prefix flag first.
11325 * Remember that it's OK, so that we accept the prefix
11326 * when arriving at a compound flag. */
11327 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011328 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011329 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011330
Bram Moolenaar4770d092006-01-12 23:22:24 +000011331 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
11332 * appending another compound word below. */
11333 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011334 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011335 goodword_ends = FALSE;
11336 else
11337 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011338
Bram Moolenaar4770d092006-01-12 23:22:24 +000011339 p = NULL;
11340 compound_ok = TRUE;
11341 if (sp->ts_complen > sp->ts_compsplit)
11342 {
11343 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000011344 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011345 /* There was a word before this word. When there was no
11346 * change in this word (it was correct) add the first word
11347 * as a suggestion. If this word was corrected too, we
11348 * need to check if a correct word follows. */
11349 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000011350 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000011351 && STRNCMP(fword + sp->ts_splitfidx,
11352 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000011353 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011354 {
11355 preword[sp->ts_prewordlen] = NUL;
11356 newscore = score_wordcount_adj(slang, sp->ts_score,
11357 preword + sp->ts_prewordlen,
11358 sp->ts_prewordlen > 0);
11359 /* Add the suggestion if the score isn't too bad. */
11360 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000011361 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011362 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011363 newscore, 0, FALSE,
11364 lp->lp_sallang, FALSE);
11365 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000011366 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011367 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000011368 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000011369 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011370 /* There was a compound word before this word. If this
11371 * word does not support compounding then give up
11372 * (splitting is tried for the word without compound
11373 * flag). */
11374 if (((unsigned)flags >> 24) == 0
11375 || sp->ts_twordlen - sp->ts_splitoff
11376 < slang->sl_compminlen)
11377 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011378#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011379 /* For multi-byte chars check character length against
11380 * COMPOUNDMIN. */
11381 if (has_mbyte
11382 && slang->sl_compminlen > 0
11383 && mb_charlen(tword + sp->ts_splitoff)
11384 < slang->sl_compminlen)
11385 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011386#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000011387
Bram Moolenaar4770d092006-01-12 23:22:24 +000011388 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11389 compflags[sp->ts_complen + 1] = NUL;
11390 vim_strncpy(preword + sp->ts_prewordlen,
11391 tword + sp->ts_splitoff,
11392 sp->ts_twordlen - sp->ts_splitoff);
11393 p = preword;
11394 while (*skiptowhite(p) != NUL)
11395 p = skipwhite(skiptowhite(p));
11396 if (fword_ends && !can_compound(slang, p,
11397 compflags + sp->ts_compsplit))
11398 /* Compound is not allowed. But it may still be
11399 * possible if we add another (short) word. */
11400 compound_ok = FALSE;
11401
11402 /* Get pointer to last char of previous word. */
11403 p = preword + sp->ts_prewordlen;
11404 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011405 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011406 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011407
Bram Moolenaar4770d092006-01-12 23:22:24 +000011408 /*
11409 * Form the word with proper case in preword.
11410 * If there is a word from a previous split, append.
11411 * For the soundfold tree don't change the case, simply append.
11412 */
11413 if (soundfold)
11414 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11415 else if (flags & WF_KEEPCAP)
11416 /* Must find the word in the keep-case tree. */
11417 find_keepcap_word(slang, tword + sp->ts_splitoff,
11418 preword + sp->ts_prewordlen);
11419 else
11420 {
11421 /* Include badflags: If the badword is onecap or allcap
11422 * use that for the goodword too. But if the badword is
11423 * allcap and it's only one char long use onecap. */
11424 c = su->su_badflags;
11425 if ((c & WF_ALLCAP)
11426#ifdef FEAT_MBYTE
11427 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11428#else
11429 && su->su_badlen == 1
11430#endif
11431 )
11432 c = WF_ONECAP;
11433 c |= flags;
11434
11435 /* When appending a compound word after a word character don't
11436 * use Onecap. */
11437 if (p != NULL && spell_iswordp_nmw(p))
11438 c &= ~WF_ONECAP;
11439 make_case_word(tword + sp->ts_splitoff,
11440 preword + sp->ts_prewordlen, c);
11441 }
11442
11443 if (!soundfold)
11444 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011445 /* Don't use a banned word. It may appear again as a good
11446 * word, thus remember it. */
11447 if (flags & WF_BANNED)
11448 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000011449 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011450 break;
11451 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011452 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000011453 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11454 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011455 {
11456 if (slang->sl_compprog == NULL)
11457 break;
11458 /* the word so far was banned but we may try compounding */
11459 goodword_ends = FALSE;
11460 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011461 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011462
Bram Moolenaar4770d092006-01-12 23:22:24 +000011463 newscore = 0;
11464 if (!soundfold) /* soundfold words don't have flags */
11465 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011466 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011467 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011468 newscore += SCORE_REGION;
11469 if (flags & WF_RARE)
11470 newscore += SCORE_RARE;
11471
Bram Moolenaar0c405862005-06-22 22:26:26 +000011472 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011473 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011474 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011475 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011476
Bram Moolenaar4770d092006-01-12 23:22:24 +000011477 /* TODO: how about splitting in the soundfold tree? */
11478 if (fword_ends
11479 && goodword_ends
11480 && sp->ts_fidx >= sp->ts_fidxtry
11481 && compound_ok)
11482 {
11483 /* The badword also ends: add suggestions. */
11484#ifdef DEBUG_TRIEWALK
11485 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011486 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011487 int j;
11488
11489 /* print the stack of changes that brought us here */
11490 smsg("------ %s -------", fword);
11491 for (j = 0; j < depth; ++j)
11492 smsg("%s", changename[j]);
11493 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011494#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011495 if (soundfold)
11496 {
11497 /* For soundfolded words we need to find the original
11498 * words, the edit distrance and then add them. */
11499 add_sound_suggest(su, preword, sp->ts_score, lp);
11500 }
11501 else
11502 {
11503 /* Give a penalty when changing non-word char to word
11504 * char, e.g., "thes," -> "these". */
11505 p = fword + sp->ts_fidx;
11506 mb_ptr_back(fword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011507 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011508 {
11509 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011510 mb_ptr_back(preword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011511 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011512 newscore += SCORE_NONWORD;
11513 }
11514
Bram Moolenaar4770d092006-01-12 23:22:24 +000011515 /* Give a bonus to words seen before. */
11516 score = score_wordcount_adj(slang,
11517 sp->ts_score + newscore,
11518 preword + sp->ts_prewordlen,
11519 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011520
Bram Moolenaar4770d092006-01-12 23:22:24 +000011521 /* Add the suggestion if the score isn't too bad. */
11522 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011523 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011524 add_suggestion(su, &su->su_ga, preword,
11525 sp->ts_fidx - repextra,
11526 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011527
11528 if (su->su_badflags & WF_MIXCAP)
11529 {
11530 /* We really don't know if the word should be
11531 * upper or lower case, add both. */
11532 c = captype(preword, NULL);
11533 if (c == 0 || c == WF_ALLCAP)
11534 {
11535 make_case_word(tword + sp->ts_splitoff,
11536 preword + sp->ts_prewordlen,
11537 c == 0 ? WF_ALLCAP : 0);
11538
11539 add_suggestion(su, &su->su_ga, preword,
11540 sp->ts_fidx - repextra,
11541 score + SCORE_ICASE, 0, FALSE,
11542 lp->lp_sallang, FALSE);
11543 }
11544 }
11545 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011546 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011547 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011548
Bram Moolenaar4770d092006-01-12 23:22:24 +000011549 /*
11550 * Try word split and/or compounding.
11551 */
11552 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011553#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011554 /* Don't split halfway a character. */
11555 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011556#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011557 )
11558 {
11559 int try_compound;
11560 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011561
Bram Moolenaar4770d092006-01-12 23:22:24 +000011562 /* If past the end of the bad word don't try a split.
11563 * Otherwise try changing the next word. E.g., find
11564 * suggestions for "the the" where the second "the" is
11565 * different. It's done like a split.
11566 * TODO: word split for soundfold words */
11567 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11568 && !soundfold;
11569
11570 /* Get here in several situations:
11571 * 1. The word in the tree ends:
11572 * If the word allows compounding try that. Otherwise try
11573 * a split by inserting a space. For both check that a
11574 * valid words starts at fword[sp->ts_fidx].
11575 * For NOBREAK do like compounding to be able to check if
11576 * the next word is valid.
11577 * 2. The badword does end, but it was due to a change (e.g.,
11578 * a swap). No need to split, but do check that the
11579 * following word is valid.
11580 * 3. The badword and the word in the tree end. It may still
11581 * be possible to compound another (short) word.
11582 */
11583 try_compound = FALSE;
11584 if (!soundfold
11585 && slang->sl_compprog != NULL
11586 && ((unsigned)flags >> 24) != 0
11587 && sp->ts_twordlen - sp->ts_splitoff
11588 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011589#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011590 && (!has_mbyte
11591 || slang->sl_compminlen == 0
11592 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011593 >= slang->sl_compminlen)
11594#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011595 && (slang->sl_compsylmax < MAXWLEN
11596 || sp->ts_complen + 1 - sp->ts_compsplit
11597 < slang->sl_compmax)
11598 && (byte_in_str(sp->ts_complen == sp->ts_compsplit
11599 ? slang->sl_compstartflags
11600 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +000011601 ((unsigned)flags >> 24))))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011602 {
11603 try_compound = TRUE;
11604 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11605 compflags[sp->ts_complen + 1] = NUL;
11606 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011607
Bram Moolenaar4770d092006-01-12 23:22:24 +000011608 /* For NOBREAK we never try splitting, it won't make any word
11609 * valid. */
11610 if (slang->sl_nobreak)
11611 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011612
Bram Moolenaar4770d092006-01-12 23:22:24 +000011613 /* If we could add a compound word, and it's also possible to
11614 * split at this point, do the split first and set
11615 * TSF_DIDSPLIT to avoid doing it again. */
11616 else if (!fword_ends
11617 && try_compound
11618 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11619 {
11620 try_compound = FALSE;
11621 sp->ts_flags |= TSF_DIDSPLIT;
11622 --sp->ts_curi; /* do the same NUL again */
11623 compflags[sp->ts_complen] = NUL;
11624 }
11625 else
11626 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011627
Bram Moolenaar4770d092006-01-12 23:22:24 +000011628 if (try_split || try_compound)
11629 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011630 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011631 {
11632 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011633 * words so far are valid for compounding. If there
11634 * is only one word it must not have the NEEDCOMPOUND
11635 * flag. */
11636 if (sp->ts_complen == sp->ts_compsplit
11637 && (flags & WF_NEEDCOMP))
11638 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011639 p = preword;
11640 while (*skiptowhite(p) != NUL)
11641 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011642 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011643 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011644 compflags + sp->ts_compsplit))
11645 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011646
11647 if (slang->sl_nosplitsugs)
11648 newscore += SCORE_SPLIT_NO;
11649 else
11650 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011651
11652 /* Give a bonus to words seen before. */
11653 newscore = score_wordcount_adj(slang, newscore,
11654 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011655 }
11656
Bram Moolenaar4770d092006-01-12 23:22:24 +000011657 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011658 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011659 go_deeper(stack, depth, newscore);
11660#ifdef DEBUG_TRIEWALK
11661 if (!try_compound && !fword_ends)
11662 sprintf(changename[depth], "%.*s-%s: split",
11663 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11664 else
11665 sprintf(changename[depth], "%.*s-%s: compound",
11666 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11667#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011668 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011669 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011670 sp->ts_state = STATE_SPLITUNDO;
11671
11672 ++depth;
11673 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011674
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011675 /* Append a space to preword when splitting. */
11676 if (!try_compound && !fword_ends)
11677 STRCAT(preword, " ");
Bram Moolenaar5195e452005-08-19 20:32:47 +000011678 sp->ts_prewordlen = STRLEN(preword);
11679 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011680 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011681
11682 /* If the badword has a non-word character at this
11683 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011684 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011685 * character when the word ends. But only when the
11686 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011687 if (((!try_compound && !spell_iswordp_nmw(fword
11688 + sp->ts_fidx))
11689 || fword_ends)
11690 && fword[sp->ts_fidx] != NUL
11691 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011692 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011693 int l;
11694
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011695#ifdef FEAT_MBYTE
11696 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011697 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011698 else
11699#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011700 l = 1;
11701 if (fword_ends)
11702 {
11703 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011704 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011705 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011706 sp->ts_prewordlen += l;
11707 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011708 }
11709 else
11710 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11711 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011712 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011713
Bram Moolenaard12a1322005-08-21 22:08:24 +000011714 /* When compounding include compound flag in
11715 * compflags[] (already set above). When splitting we
11716 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011717 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011718 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011719 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011720 sp->ts_compsplit = sp->ts_complen;
11721 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011722
Bram Moolenaar53805d12005-08-01 07:08:33 +000011723 /* set su->su_badflags to the caps type at this
11724 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011725#ifdef FEAT_MBYTE
11726 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011727 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011728 else
11729#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011730 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011731 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011732 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011733
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011734 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011735 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011736
11737 /* If there are postponed prefixes, try these too. */
11738 if (pbyts != NULL)
11739 {
11740 byts = pbyts;
11741 idxs = pidxs;
11742 sp->ts_prefixdepth = PFD_PREFIXTREE;
11743 sp->ts_state = STATE_NOPREFIX;
11744 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011745 }
11746 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011747 }
11748 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011749
Bram Moolenaar4770d092006-01-12 23:22:24 +000011750 case STATE_SPLITUNDO:
11751 /* Undo the changes done for word split or compound word. */
11752 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011753
Bram Moolenaar4770d092006-01-12 23:22:24 +000011754 /* Continue looking for NUL bytes. */
11755 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011756
Bram Moolenaar4770d092006-01-12 23:22:24 +000011757 /* In case we went into the prefix tree. */
11758 byts = fbyts;
11759 idxs = fidxs;
11760 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011761
Bram Moolenaar4770d092006-01-12 23:22:24 +000011762 case STATE_ENDNUL:
11763 /* Past the NUL bytes in the node. */
11764 su->su_badflags = sp->ts_save_badflags;
11765 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011766#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011767 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011768#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011769 )
11770 {
11771 /* The badword ends, can't use STATE_PLAIN. */
11772 sp->ts_state = STATE_DEL;
11773 break;
11774 }
11775 sp->ts_state = STATE_PLAIN;
11776 /*FALLTHROUGH*/
11777
11778 case STATE_PLAIN:
11779 /*
11780 * Go over all possible bytes at this node, add each to tword[]
11781 * and use child node. "ts_curi" is the index.
11782 */
11783 arridx = sp->ts_arridx;
11784 if (sp->ts_curi > byts[arridx])
11785 {
11786 /* Done all bytes at this node, do next state. When still at
11787 * already changed bytes skip the other tricks. */
11788 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011789 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011790 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011791 sp->ts_state = STATE_FINAL;
11792 }
11793 else
11794 {
11795 arridx += sp->ts_curi++;
11796 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011797
Bram Moolenaar4770d092006-01-12 23:22:24 +000011798 /* Normal byte, go one level deeper. If it's not equal to the
11799 * byte in the bad word adjust the score. But don't even try
11800 * when the byte was already changed. And don't try when we
11801 * just deleted this byte, accepting it is always cheaper then
11802 * delete + substitute. */
11803 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000011804#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011805 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011806#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011807 )
11808 newscore = 0;
11809 else
11810 newscore = SCORE_SUBST;
11811 if ((newscore == 0
11812 || (sp->ts_fidx >= sp->ts_fidxtry
11813 && ((sp->ts_flags & TSF_DIDDEL) == 0
11814 || c != fword[sp->ts_delidx])))
11815 && TRY_DEEPER(su, stack, depth, newscore))
11816 {
11817 go_deeper(stack, depth, newscore);
11818#ifdef DEBUG_TRIEWALK
11819 if (newscore > 0)
11820 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
11821 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11822 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011823 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011824 sprintf(changename[depth], "%.*s-%s: accept %c",
11825 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11826 fword[sp->ts_fidx]);
11827#endif
11828 ++depth;
11829 sp = &stack[depth];
11830 ++sp->ts_fidx;
11831 tword[sp->ts_twordlen++] = c;
11832 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000011833#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011834 if (newscore == SCORE_SUBST)
11835 sp->ts_isdiff = DIFF_YES;
11836 if (has_mbyte)
11837 {
11838 /* Multi-byte characters are a bit complicated to
11839 * handle: They differ when any of the bytes differ
11840 * and then their length may also differ. */
11841 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011842 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011843 /* First byte. */
11844 sp->ts_tcharidx = 0;
11845 sp->ts_tcharlen = MB_BYTE2LEN(c);
11846 sp->ts_fcharstart = sp->ts_fidx - 1;
11847 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011848 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011849 }
11850 else if (sp->ts_isdiff == DIFF_INSERT)
11851 /* When inserting trail bytes don't advance in the
11852 * bad word. */
11853 --sp->ts_fidx;
11854 if (++sp->ts_tcharidx == sp->ts_tcharlen)
11855 {
11856 /* Last byte of character. */
11857 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000011858 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011859 /* Correct ts_fidx for the byte length of the
11860 * character (we didn't check that before). */
11861 sp->ts_fidx = sp->ts_fcharstart
11862 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000011863 fword[sp->ts_fcharstart]);
11864
Bram Moolenaar4770d092006-01-12 23:22:24 +000011865 /* For changing a composing character adjust
11866 * the score from SCORE_SUBST to
11867 * SCORE_SUBCOMP. */
11868 if (enc_utf8
11869 && utf_iscomposing(
11870 mb_ptr2char(tword
11871 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011872 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011873 && utf_iscomposing(
11874 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011875 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011876 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011877 SCORE_SUBST - SCORE_SUBCOMP;
11878
Bram Moolenaar4770d092006-01-12 23:22:24 +000011879 /* For a similar character adjust score from
11880 * SCORE_SUBST to SCORE_SIMILAR. */
11881 else if (!soundfold
11882 && slang->sl_has_map
11883 && similar_chars(slang,
11884 mb_ptr2char(tword
11885 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000011886 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000011887 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000011888 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011889 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000011890 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000011891 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011892 else if (sp->ts_isdiff == DIFF_INSERT
11893 && sp->ts_twordlen > sp->ts_tcharlen)
11894 {
11895 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
11896 c = mb_ptr2char(p);
11897 if (enc_utf8 && utf_iscomposing(c))
11898 {
11899 /* Inserting a composing char doesn't
11900 * count that much. */
11901 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
11902 }
11903 else
11904 {
11905 /* If the previous character was the same,
11906 * thus doubling a character, give a bonus
11907 * to the score. Also for the soundfold
11908 * tree (might seem illogical but does
11909 * give better scores). */
11910 mb_ptr_back(tword, p);
11911 if (c == mb_ptr2char(p))
11912 sp->ts_score -= SCORE_INS
11913 - SCORE_INSDUP;
11914 }
11915 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011916
Bram Moolenaar4770d092006-01-12 23:22:24 +000011917 /* Starting a new char, reset the length. */
11918 sp->ts_tcharlen = 0;
11919 }
Bram Moolenaarea408852005-06-25 22:49:46 +000011920 }
Bram Moolenaarea424162005-06-16 21:51:00 +000011921 else
11922#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000011923 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011924 /* If we found a similar char adjust the score.
11925 * We do this after calling go_deeper() because
11926 * it's slow. */
11927 if (newscore != 0
11928 && !soundfold
11929 && slang->sl_has_map
11930 && similar_chars(slang,
11931 c, fword[sp->ts_fidx - 1]))
11932 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000011933 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011934 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011935 }
11936 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011937
Bram Moolenaar4770d092006-01-12 23:22:24 +000011938 case STATE_DEL:
11939#ifdef FEAT_MBYTE
11940 /* When past the first byte of a multi-byte char don't try
11941 * delete/insert/swap a character. */
11942 if (has_mbyte && sp->ts_tcharlen > 0)
11943 {
11944 sp->ts_state = STATE_FINAL;
11945 break;
11946 }
11947#endif
11948 /*
11949 * Try skipping one character in the bad word (delete it).
11950 */
11951 sp->ts_state = STATE_INS_PREP;
11952 sp->ts_curi = 1;
11953 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
11954 /* Deleting a vowel at the start of a word counts less, see
11955 * soundalike_score(). */
11956 newscore = 2 * SCORE_DEL / 3;
11957 else
11958 newscore = SCORE_DEL;
11959 if (fword[sp->ts_fidx] != NUL
11960 && TRY_DEEPER(su, stack, depth, newscore))
11961 {
11962 go_deeper(stack, depth, newscore);
11963#ifdef DEBUG_TRIEWALK
11964 sprintf(changename[depth], "%.*s-%s: delete %c",
11965 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11966 fword[sp->ts_fidx]);
11967#endif
11968 ++depth;
11969
11970 /* Remember what character we deleted, so that we can avoid
11971 * inserting it again. */
11972 stack[depth].ts_flags |= TSF_DIDDEL;
11973 stack[depth].ts_delidx = sp->ts_fidx;
11974
11975 /* Advance over the character in fword[]. Give a bonus to the
11976 * score if the same character is following "nn" -> "n". It's
11977 * a bit illogical for soundfold tree but it does give better
11978 * results. */
11979#ifdef FEAT_MBYTE
11980 if (has_mbyte)
11981 {
11982 c = mb_ptr2char(fword + sp->ts_fidx);
11983 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
11984 if (enc_utf8 && utf_iscomposing(c))
11985 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
11986 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
11987 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
11988 }
11989 else
11990#endif
11991 {
11992 ++stack[depth].ts_fidx;
11993 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
11994 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
11995 }
11996 break;
11997 }
11998 /*FALLTHROUGH*/
11999
12000 case STATE_INS_PREP:
12001 if (sp->ts_flags & TSF_DIDDEL)
12002 {
12003 /* If we just deleted a byte then inserting won't make sense,
12004 * a substitute is always cheaper. */
12005 sp->ts_state = STATE_SWAP;
12006 break;
12007 }
12008
12009 /* skip over NUL bytes */
12010 n = sp->ts_arridx;
12011 for (;;)
12012 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012013 if (sp->ts_curi > byts[n])
12014 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012015 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012016 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012017 break;
12018 }
12019 if (byts[n + sp->ts_curi] != NUL)
12020 {
12021 /* Found a byte to insert. */
12022 sp->ts_state = STATE_INS;
12023 break;
12024 }
12025 ++sp->ts_curi;
12026 }
12027 break;
12028
12029 /*FALLTHROUGH*/
12030
12031 case STATE_INS:
12032 /* Insert one byte. Repeat this for each possible byte at this
12033 * node. */
12034 n = sp->ts_arridx;
12035 if (sp->ts_curi > byts[n])
12036 {
12037 /* Done all bytes at this node, go to next state. */
12038 sp->ts_state = STATE_SWAP;
12039 break;
12040 }
12041
12042 /* Do one more byte at this node, but:
12043 * - Skip NUL bytes.
12044 * - Skip the byte if it's equal to the byte in the word,
12045 * accepting that byte is always better.
12046 */
12047 n += sp->ts_curi++;
12048 c = byts[n];
12049 if (soundfold && sp->ts_twordlen == 0 && c == '*')
12050 /* Inserting a vowel at the start of a word counts less,
12051 * see soundalike_score(). */
12052 newscore = 2 * SCORE_INS / 3;
12053 else
12054 newscore = SCORE_INS;
12055 if (c != fword[sp->ts_fidx]
12056 && TRY_DEEPER(su, stack, depth, newscore))
12057 {
12058 go_deeper(stack, depth, newscore);
12059#ifdef DEBUG_TRIEWALK
12060 sprintf(changename[depth], "%.*s-%s: insert %c",
12061 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12062 c);
12063#endif
12064 ++depth;
12065 sp = &stack[depth];
12066 tword[sp->ts_twordlen++] = c;
12067 sp->ts_arridx = idxs[n];
12068#ifdef FEAT_MBYTE
12069 if (has_mbyte)
12070 {
12071 fl = MB_BYTE2LEN(c);
12072 if (fl > 1)
12073 {
12074 /* There are following bytes for the same character.
12075 * We must find all bytes before trying
12076 * delete/insert/swap/etc. */
12077 sp->ts_tcharlen = fl;
12078 sp->ts_tcharidx = 1;
12079 sp->ts_isdiff = DIFF_INSERT;
12080 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012081 }
12082 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012083 fl = 1;
12084 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000012085#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012086 {
12087 /* If the previous character was the same, thus doubling a
12088 * character, give a bonus to the score. Also for
12089 * soundfold words (illogical but does give a better
12090 * score). */
12091 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000012092 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012093 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012094 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012095 }
12096 break;
12097
12098 case STATE_SWAP:
12099 /*
12100 * Swap two bytes in the bad word: "12" -> "21".
12101 * We change "fword" here, it's changed back afterwards at
12102 * STATE_UNSWAP.
12103 */
12104 p = fword + sp->ts_fidx;
12105 c = *p;
12106 if (c == NUL)
12107 {
12108 /* End of word, can't swap or replace. */
12109 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012110 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012111 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012112
Bram Moolenaar4770d092006-01-12 23:22:24 +000012113 /* Don't swap if the first character is not a word character.
12114 * SWAP3 etc. also don't make sense then. */
12115 if (!soundfold && !spell_iswordp(p, curbuf))
12116 {
12117 sp->ts_state = STATE_REP_INI;
12118 break;
12119 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012120
Bram Moolenaar4770d092006-01-12 23:22:24 +000012121#ifdef FEAT_MBYTE
12122 if (has_mbyte)
12123 {
12124 n = mb_cptr2len(p);
12125 c = mb_ptr2char(p);
12126 if (!soundfold && !spell_iswordp(p + n, curbuf))
12127 c2 = c; /* don't swap non-word char */
12128 else
12129 c2 = mb_ptr2char(p + n);
12130 }
12131 else
12132#endif
12133 {
12134 if (!soundfold && !spell_iswordp(p + 1, curbuf))
12135 c2 = c; /* don't swap non-word char */
12136 else
12137 c2 = p[1];
12138 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012139
Bram Moolenaar4770d092006-01-12 23:22:24 +000012140 /* When characters are identical, swap won't do anything.
12141 * Also get here if the second char is not a word character. */
12142 if (c == c2)
12143 {
12144 sp->ts_state = STATE_SWAP3;
12145 break;
12146 }
12147 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
12148 {
12149 go_deeper(stack, depth, SCORE_SWAP);
12150#ifdef DEBUG_TRIEWALK
12151 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
12152 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12153 c, c2);
12154#endif
12155 sp->ts_state = STATE_UNSWAP;
12156 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012157#ifdef FEAT_MBYTE
12158 if (has_mbyte)
12159 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012160 fl = mb_char2len(c2);
12161 mch_memmove(p, p + n, fl);
12162 mb_char2bytes(c, p + fl);
12163 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012164 }
12165 else
12166#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012167 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012168 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012169 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012170 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012171 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012172 }
12173 else
12174 /* If this swap doesn't work then SWAP3 won't either. */
12175 sp->ts_state = STATE_REP_INI;
12176 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000012177
Bram Moolenaar4770d092006-01-12 23:22:24 +000012178 case STATE_UNSWAP:
12179 /* Undo the STATE_SWAP swap: "21" -> "12". */
12180 p = fword + sp->ts_fidx;
12181#ifdef FEAT_MBYTE
12182 if (has_mbyte)
12183 {
12184 n = MB_BYTE2LEN(*p);
12185 c = mb_ptr2char(p + n);
12186 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
12187 mb_char2bytes(c, p);
12188 }
12189 else
12190#endif
12191 {
12192 c = *p;
12193 *p = p[1];
12194 p[1] = c;
12195 }
12196 /*FALLTHROUGH*/
12197
12198 case STATE_SWAP3:
12199 /* Swap two bytes, skipping one: "123" -> "321". We change
12200 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
12201 p = fword + sp->ts_fidx;
12202#ifdef FEAT_MBYTE
12203 if (has_mbyte)
12204 {
12205 n = mb_cptr2len(p);
12206 c = mb_ptr2char(p);
12207 fl = mb_cptr2len(p + n);
12208 c2 = mb_ptr2char(p + n);
12209 if (!soundfold && !spell_iswordp(p + n + fl, curbuf))
12210 c3 = c; /* don't swap non-word char */
12211 else
12212 c3 = mb_ptr2char(p + n + fl);
12213 }
12214 else
12215#endif
12216 {
12217 c = *p;
12218 c2 = p[1];
12219 if (!soundfold && !spell_iswordp(p + 2, curbuf))
12220 c3 = c; /* don't swap non-word char */
12221 else
12222 c3 = p[2];
12223 }
12224
12225 /* When characters are identical: "121" then SWAP3 result is
12226 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
12227 * same as SWAP on next char: "112". Thus skip all swapping.
12228 * Also skip when c3 is NUL.
12229 * Also get here when the third character is not a word character.
12230 * Second character may any char: "a.b" -> "b.a" */
12231 if (c == c3 || c3 == NUL)
12232 {
12233 sp->ts_state = STATE_REP_INI;
12234 break;
12235 }
12236 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12237 {
12238 go_deeper(stack, depth, SCORE_SWAP3);
12239#ifdef DEBUG_TRIEWALK
12240 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
12241 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12242 c, c3);
12243#endif
12244 sp->ts_state = STATE_UNSWAP3;
12245 ++depth;
12246#ifdef FEAT_MBYTE
12247 if (has_mbyte)
12248 {
12249 tl = mb_char2len(c3);
12250 mch_memmove(p, p + n + fl, tl);
12251 mb_char2bytes(c2, p + tl);
12252 mb_char2bytes(c, p + fl + tl);
12253 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
12254 }
12255 else
12256#endif
12257 {
12258 p[0] = p[2];
12259 p[2] = c;
12260 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12261 }
12262 }
12263 else
12264 sp->ts_state = STATE_REP_INI;
12265 break;
12266
12267 case STATE_UNSWAP3:
12268 /* Undo STATE_SWAP3: "321" -> "123" */
12269 p = fword + sp->ts_fidx;
12270#ifdef FEAT_MBYTE
12271 if (has_mbyte)
12272 {
12273 n = MB_BYTE2LEN(*p);
12274 c2 = mb_ptr2char(p + n);
12275 fl = MB_BYTE2LEN(p[n]);
12276 c = mb_ptr2char(p + n + fl);
12277 tl = MB_BYTE2LEN(p[n + fl]);
12278 mch_memmove(p + fl + tl, p, n);
12279 mb_char2bytes(c, p);
12280 mb_char2bytes(c2, p + tl);
12281 p = p + tl;
12282 }
12283 else
12284#endif
12285 {
12286 c = *p;
12287 *p = p[2];
12288 p[2] = c;
12289 ++p;
12290 }
12291
12292 if (!soundfold && !spell_iswordp(p, curbuf))
12293 {
12294 /* Middle char is not a word char, skip the rotate. First and
12295 * third char were already checked at swap and swap3. */
12296 sp->ts_state = STATE_REP_INI;
12297 break;
12298 }
12299
12300 /* Rotate three characters left: "123" -> "231". We change
12301 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
12302 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12303 {
12304 go_deeper(stack, depth, SCORE_SWAP3);
12305#ifdef DEBUG_TRIEWALK
12306 p = fword + sp->ts_fidx;
12307 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
12308 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12309 p[0], p[1], p[2]);
12310#endif
12311 sp->ts_state = STATE_UNROT3L;
12312 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012313 p = fword + sp->ts_fidx;
12314#ifdef FEAT_MBYTE
12315 if (has_mbyte)
12316 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012317 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012318 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012319 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012320 fl += mb_cptr2len(p + n + fl);
12321 mch_memmove(p, p + n, fl);
12322 mb_char2bytes(c, p + fl);
12323 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012324 }
12325 else
12326#endif
12327 {
12328 c = *p;
12329 *p = p[1];
12330 p[1] = p[2];
12331 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012332 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000012333 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012334 }
12335 else
12336 sp->ts_state = STATE_REP_INI;
12337 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012338
Bram Moolenaar4770d092006-01-12 23:22:24 +000012339 case STATE_UNROT3L:
12340 /* Undo ROT3L: "231" -> "123" */
12341 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000012342#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012343 if (has_mbyte)
12344 {
12345 n = MB_BYTE2LEN(*p);
12346 n += MB_BYTE2LEN(p[n]);
12347 c = mb_ptr2char(p + n);
12348 tl = MB_BYTE2LEN(p[n]);
12349 mch_memmove(p + tl, p, n);
12350 mb_char2bytes(c, p);
12351 }
12352 else
Bram Moolenaarea424162005-06-16 21:51:00 +000012353#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012354 {
12355 c = p[2];
12356 p[2] = p[1];
12357 p[1] = *p;
12358 *p = c;
12359 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012360
Bram Moolenaar4770d092006-01-12 23:22:24 +000012361 /* Rotate three bytes right: "123" -> "312". We change "fword"
12362 * here, it's changed back afterwards at STATE_UNROT3R. */
12363 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12364 {
12365 go_deeper(stack, depth, SCORE_SWAP3);
12366#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012367 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012368 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
12369 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12370 p[0], p[1], p[2]);
12371#endif
12372 sp->ts_state = STATE_UNROT3R;
12373 ++depth;
12374 p = fword + sp->ts_fidx;
12375#ifdef FEAT_MBYTE
12376 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012377 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012378 n = mb_cptr2len(p);
12379 n += mb_cptr2len(p + n);
12380 c = mb_ptr2char(p + n);
12381 tl = mb_cptr2len(p + n);
12382 mch_memmove(p + tl, p, n);
12383 mb_char2bytes(c, p);
12384 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012385 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012386 else
12387#endif
12388 {
12389 c = p[2];
12390 p[2] = p[1];
12391 p[1] = *p;
12392 *p = c;
12393 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12394 }
12395 }
12396 else
12397 sp->ts_state = STATE_REP_INI;
12398 break;
12399
12400 case STATE_UNROT3R:
12401 /* Undo ROT3R: "312" -> "123" */
12402 p = fword + sp->ts_fidx;
12403#ifdef FEAT_MBYTE
12404 if (has_mbyte)
12405 {
12406 c = mb_ptr2char(p);
12407 tl = MB_BYTE2LEN(*p);
12408 n = MB_BYTE2LEN(p[tl]);
12409 n += MB_BYTE2LEN(p[tl + n]);
12410 mch_memmove(p, p + tl, n);
12411 mb_char2bytes(c, p + n);
12412 }
12413 else
12414#endif
12415 {
12416 c = *p;
12417 *p = p[1];
12418 p[1] = p[2];
12419 p[2] = c;
12420 }
12421 /*FALLTHROUGH*/
12422
12423 case STATE_REP_INI:
12424 /* Check if matching with REP items from the .aff file would work.
12425 * Quickly skip if:
12426 * - there are no REP items and we are not in the soundfold trie
12427 * - the score is going to be too high anyway
12428 * - already applied a REP item or swapped here */
12429 if ((lp->lp_replang == NULL && !soundfold)
12430 || sp->ts_score + SCORE_REP >= su->su_maxscore
12431 || sp->ts_fidx < sp->ts_fidxtry)
12432 {
12433 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012434 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012435 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012436
Bram Moolenaar4770d092006-01-12 23:22:24 +000012437 /* Use the first byte to quickly find the first entry that may
12438 * match. If the index is -1 there is none. */
12439 if (soundfold)
12440 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12441 else
12442 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012443
Bram Moolenaar4770d092006-01-12 23:22:24 +000012444 if (sp->ts_curi < 0)
12445 {
12446 sp->ts_state = STATE_FINAL;
12447 break;
12448 }
12449
12450 sp->ts_state = STATE_REP;
12451 /*FALLTHROUGH*/
12452
12453 case STATE_REP:
12454 /* Try matching with REP items from the .aff file. For each match
12455 * replace the characters and check if the resulting word is
12456 * valid. */
12457 p = fword + sp->ts_fidx;
12458
12459 if (soundfold)
12460 gap = &slang->sl_repsal;
12461 else
12462 gap = &lp->lp_replang->sl_rep;
12463 while (sp->ts_curi < gap->ga_len)
12464 {
12465 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12466 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012467 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012468 /* past possible matching entries */
12469 sp->ts_curi = gap->ga_len;
12470 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012471 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012472 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12473 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12474 {
12475 go_deeper(stack, depth, SCORE_REP);
12476#ifdef DEBUG_TRIEWALK
12477 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12478 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12479 ftp->ft_from, ftp->ft_to);
12480#endif
12481 /* Need to undo this afterwards. */
12482 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012483
Bram Moolenaar4770d092006-01-12 23:22:24 +000012484 /* Change the "from" to the "to" string. */
12485 ++depth;
12486 fl = STRLEN(ftp->ft_from);
12487 tl = STRLEN(ftp->ft_to);
12488 if (fl != tl)
12489 {
12490 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
12491 repextra += tl - fl;
12492 }
12493 mch_memmove(p, ftp->ft_to, tl);
12494 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12495#ifdef FEAT_MBYTE
12496 stack[depth].ts_tcharlen = 0;
12497#endif
12498 break;
12499 }
12500 }
12501
12502 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
12503 /* No (more) matches. */
12504 sp->ts_state = STATE_FINAL;
12505
12506 break;
12507
12508 case STATE_REP_UNDO:
12509 /* Undo a REP replacement and continue with the next one. */
12510 if (soundfold)
12511 gap = &slang->sl_repsal;
12512 else
12513 gap = &lp->lp_replang->sl_rep;
12514 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
12515 fl = STRLEN(ftp->ft_from);
12516 tl = STRLEN(ftp->ft_to);
12517 p = fword + sp->ts_fidx;
12518 if (fl != tl)
12519 {
12520 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
12521 repextra -= tl - fl;
12522 }
12523 mch_memmove(p, ftp->ft_from, fl);
12524 sp->ts_state = STATE_REP;
12525 break;
12526
12527 default:
12528 /* Did all possible states at this level, go up one level. */
12529 --depth;
12530
12531 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12532 {
12533 /* Continue in or go back to the prefix tree. */
12534 byts = pbyts;
12535 idxs = pidxs;
12536 }
12537
12538 /* Don't check for CTRL-C too often, it takes time. */
12539 if (--breakcheckcount == 0)
12540 {
12541 ui_breakcheck();
12542 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012543 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012544 }
12545 }
12546}
12547
Bram Moolenaar4770d092006-01-12 23:22:24 +000012548
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012549/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012550 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012551 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012552 static void
12553go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012554 trystate_T *stack;
12555 int depth;
12556 int score_add;
12557{
Bram Moolenaarea424162005-06-16 21:51:00 +000012558 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012559 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012560 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012561 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012562 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012563}
12564
Bram Moolenaar53805d12005-08-01 07:08:33 +000012565#ifdef FEAT_MBYTE
12566/*
12567 * Case-folding may change the number of bytes: Count nr of chars in
12568 * fword[flen] and return the byte length of that many chars in "word".
12569 */
12570 static int
12571nofold_len(fword, flen, word)
12572 char_u *fword;
12573 int flen;
12574 char_u *word;
12575{
12576 char_u *p;
12577 int i = 0;
12578
12579 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12580 ++i;
12581 for (p = word; i > 0; mb_ptr_adv(p))
12582 --i;
12583 return (int)(p - word);
12584}
12585#endif
12586
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012587/*
12588 * "fword" is a good word with case folded. Find the matching keep-case
12589 * words and put it in "kword".
12590 * Theoretically there could be several keep-case words that result in the
12591 * same case-folded word, but we only find one...
12592 */
12593 static void
12594find_keepcap_word(slang, fword, kword)
12595 slang_T *slang;
12596 char_u *fword;
12597 char_u *kword;
12598{
12599 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12600 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012601 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012602
12603 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012604 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012605 int round[MAXWLEN];
12606 int fwordidx[MAXWLEN];
12607 int uwordidx[MAXWLEN];
12608 int kwordlen[MAXWLEN];
12609
12610 int flen, ulen;
12611 int l;
12612 int len;
12613 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012614 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012615 char_u *p;
12616 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012617 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012618
12619 if (byts == NULL)
12620 {
12621 /* array is empty: "cannot happen" */
12622 *kword = NUL;
12623 return;
12624 }
12625
12626 /* Make an all-cap version of "fword". */
12627 allcap_copy(fword, uword);
12628
12629 /*
12630 * Each character needs to be tried both case-folded and upper-case.
12631 * All this gets very complicated if we keep in mind that changing case
12632 * may change the byte length of a multi-byte character...
12633 */
12634 depth = 0;
12635 arridx[0] = 0;
12636 round[0] = 0;
12637 fwordidx[0] = 0;
12638 uwordidx[0] = 0;
12639 kwordlen[0] = 0;
12640 while (depth >= 0)
12641 {
12642 if (fword[fwordidx[depth]] == NUL)
12643 {
12644 /* We are at the end of "fword". If the tree allows a word to end
12645 * here we have found a match. */
12646 if (byts[arridx[depth] + 1] == 0)
12647 {
12648 kword[kwordlen[depth]] = NUL;
12649 return;
12650 }
12651
12652 /* kword is getting too long, continue one level up */
12653 --depth;
12654 }
12655 else if (++round[depth] > 2)
12656 {
12657 /* tried both fold-case and upper-case character, continue one
12658 * level up */
12659 --depth;
12660 }
12661 else
12662 {
12663 /*
12664 * round[depth] == 1: Try using the folded-case character.
12665 * round[depth] == 2: Try using the upper-case character.
12666 */
12667#ifdef FEAT_MBYTE
12668 if (has_mbyte)
12669 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012670 flen = mb_cptr2len(fword + fwordidx[depth]);
12671 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012672 }
12673 else
12674#endif
12675 ulen = flen = 1;
12676 if (round[depth] == 1)
12677 {
12678 p = fword + fwordidx[depth];
12679 l = flen;
12680 }
12681 else
12682 {
12683 p = uword + uwordidx[depth];
12684 l = ulen;
12685 }
12686
12687 for (tryidx = arridx[depth]; l > 0; --l)
12688 {
12689 /* Perform a binary search in the list of accepted bytes. */
12690 len = byts[tryidx++];
12691 c = *p++;
12692 lo = tryidx;
12693 hi = tryidx + len - 1;
12694 while (lo < hi)
12695 {
12696 m = (lo + hi) / 2;
12697 if (byts[m] > c)
12698 hi = m - 1;
12699 else if (byts[m] < c)
12700 lo = m + 1;
12701 else
12702 {
12703 lo = hi = m;
12704 break;
12705 }
12706 }
12707
12708 /* Stop if there is no matching byte. */
12709 if (hi < lo || byts[lo] != c)
12710 break;
12711
12712 /* Continue at the child (if there is one). */
12713 tryidx = idxs[lo];
12714 }
12715
12716 if (l == 0)
12717 {
12718 /*
12719 * Found the matching char. Copy it to "kword" and go a
12720 * level deeper.
12721 */
12722 if (round[depth] == 1)
12723 {
12724 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12725 flen);
12726 kwordlen[depth + 1] = kwordlen[depth] + flen;
12727 }
12728 else
12729 {
12730 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12731 ulen);
12732 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12733 }
12734 fwordidx[depth + 1] = fwordidx[depth] + flen;
12735 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12736
12737 ++depth;
12738 arridx[depth] = tryidx;
12739 round[depth] = 0;
12740 }
12741 }
12742 }
12743
12744 /* Didn't find it: "cannot happen". */
12745 *kword = NUL;
12746}
12747
12748/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012749 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12750 * su->su_sga.
12751 */
12752 static void
12753score_comp_sal(su)
12754 suginfo_T *su;
12755{
12756 langp_T *lp;
12757 char_u badsound[MAXWLEN];
12758 int i;
12759 suggest_T *stp;
12760 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012761 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012762 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012763
12764 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
12765 return;
12766
12767 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012768 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012769 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012770 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012771 if (lp->lp_slang->sl_sal.ga_len > 0)
12772 {
12773 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012774 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012775
12776 for (i = 0; i < su->su_ga.ga_len; ++i)
12777 {
12778 stp = &SUG(su->su_ga, i);
12779
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012780 /* Case-fold the suggested word, sound-fold it and compute the
12781 * sound-a-like score. */
12782 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012783 if (score < SCORE_MAXMAX)
12784 {
12785 /* Add the suggestion. */
12786 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
12787 sstp->st_word = vim_strsave(stp->st_word);
12788 if (sstp->st_word != NULL)
12789 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012790 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012791 sstp->st_score = score;
12792 sstp->st_altscore = 0;
12793 sstp->st_orglen = stp->st_orglen;
12794 ++su->su_sga.ga_len;
12795 }
12796 }
12797 }
12798 break;
12799 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012800 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012801}
12802
12803/*
12804 * Combine the list of suggestions in su->su_ga and su->su_sga.
12805 * They are intwined.
12806 */
12807 static void
12808score_combine(su)
12809 suginfo_T *su;
12810{
12811 int i;
12812 int j;
12813 garray_T ga;
12814 garray_T *gap;
12815 langp_T *lp;
12816 suggest_T *stp;
12817 char_u *p;
12818 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012819 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012820 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012821 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012822
12823 /* Add the alternate score to su_ga. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012824 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012825 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012826 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012827 if (lp->lp_slang->sl_sal.ga_len > 0)
12828 {
12829 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012830 slang = lp->lp_slang;
12831 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012832
12833 for (i = 0; i < su->su_ga.ga_len; ++i)
12834 {
12835 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012836 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012837 if (stp->st_altscore == SCORE_MAXMAX)
12838 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
12839 else
12840 stp->st_score = (stp->st_score * 3
12841 + stp->st_altscore) / 4;
12842 stp->st_salscore = FALSE;
12843 }
12844 break;
12845 }
12846 }
12847
Bram Moolenaar4770d092006-01-12 23:22:24 +000012848 if (slang == NULL) /* just in case */
12849 return;
12850
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012851 /* Add the alternate score to su_sga. */
12852 for (i = 0; i < su->su_sga.ga_len; ++i)
12853 {
12854 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012855 stp->st_altscore = spell_edit_score(slang,
12856 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012857 if (stp->st_score == SCORE_MAXMAX)
12858 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
12859 else
12860 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
12861 stp->st_salscore = TRUE;
12862 }
12863
Bram Moolenaar4770d092006-01-12 23:22:24 +000012864 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
12865 * for both lists. */
12866 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012867 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012868 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012869 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
12870
12871 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
12872 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
12873 return;
12874
12875 stp = &SUG(ga, 0);
12876 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
12877 {
12878 /* round 1: get a suggestion from su_ga
12879 * round 2: get a suggestion from su_sga */
12880 for (round = 1; round <= 2; ++round)
12881 {
12882 gap = round == 1 ? &su->su_ga : &su->su_sga;
12883 if (i < gap->ga_len)
12884 {
12885 /* Don't add a word if it's already there. */
12886 p = SUG(*gap, i).st_word;
12887 for (j = 0; j < ga.ga_len; ++j)
12888 if (STRCMP(stp[j].st_word, p) == 0)
12889 break;
12890 if (j == ga.ga_len)
12891 stp[ga.ga_len++] = SUG(*gap, i);
12892 else
12893 vim_free(p);
12894 }
12895 }
12896 }
12897
12898 ga_clear(&su->su_ga);
12899 ga_clear(&su->su_sga);
12900
12901 /* Truncate the list to the number of suggestions that will be displayed. */
12902 if (ga.ga_len > su->su_maxcount)
12903 {
12904 for (i = su->su_maxcount; i < ga.ga_len; ++i)
12905 vim_free(stp[i].st_word);
12906 ga.ga_len = su->su_maxcount;
12907 }
12908
12909 su->su_ga = ga;
12910}
12911
12912/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012913 * For the goodword in "stp" compute the soundalike score compared to the
12914 * badword.
12915 */
12916 static int
12917stp_sal_score(stp, su, slang, badsound)
12918 suggest_T *stp;
12919 suginfo_T *su;
12920 slang_T *slang;
12921 char_u *badsound; /* sound-folded badword */
12922{
12923 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012924 char_u *pbad;
12925 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012926 char_u badsound2[MAXWLEN];
12927 char_u fword[MAXWLEN];
12928 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012929 char_u goodword[MAXWLEN];
12930 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012931
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012932 lendiff = (int)(su->su_badlen - stp->st_orglen);
12933 if (lendiff >= 0)
12934 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012935 else
12936 {
12937 /* soundfold the bad word with more characters following */
12938 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
12939
12940 /* When joining two words the sound often changes a lot. E.g., "t he"
12941 * sounds like "t h" while "the" sounds like "@". Avoid that by
12942 * removing the space. Don't do it when the good word also contains a
12943 * space. */
12944 if (vim_iswhite(su->su_badptr[su->su_badlen])
12945 && *skiptowhite(stp->st_word) == NUL)
12946 for (p = fword; *(p = skiptowhite(p)) != NUL; )
12947 mch_memmove(p, p + 1, STRLEN(p));
12948
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012949 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012950 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012951 }
12952
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012953 if (lendiff > 0)
12954 {
12955 /* Add part of the bad word to the good word, so that we soundfold
12956 * what replaces the bad word. */
12957 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012958 vim_strncpy(goodword + stp->st_wordlen,
12959 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012960 pgood = goodword;
12961 }
12962 else
12963 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012964
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012965 /* Sound-fold the word and compute the score for the difference. */
12966 spell_soundfold(slang, pgood, FALSE, goodsound);
12967
12968 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012969}
12970
Bram Moolenaar4770d092006-01-12 23:22:24 +000012971/* structure used to store soundfolded words that add_sound_suggest() has
12972 * handled already. */
12973typedef struct
12974{
12975 short sft_score; /* lowest score used */
12976 char_u sft_word[1]; /* soundfolded word, actually longer */
12977} sftword_T;
12978
12979static sftword_T dumsft;
12980#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
12981#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
12982
12983/*
12984 * Prepare for calling suggest_try_soundalike().
12985 */
12986 static void
12987suggest_try_soundalike_prep()
12988{
12989 langp_T *lp;
12990 int lpi;
12991 slang_T *slang;
12992
12993 /* Do this for all languages that support sound folding and for which a
12994 * .sug file has been loaded. */
12995 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
12996 {
12997 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
12998 slang = lp->lp_slang;
12999 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13000 /* prepare the hashtable used by add_sound_suggest() */
13001 hash_init(&slang->sl_sounddone);
13002 }
13003}
13004
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013005/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013006 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013007 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013008 */
13009 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000013010suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013011 suginfo_T *su;
13012{
13013 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013014 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013015 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013016 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013017
Bram Moolenaar4770d092006-01-12 23:22:24 +000013018 /* Do this for all languages that support sound folding and for which a
13019 * .sug file has been loaded. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013020 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013021 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013022 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13023 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013024 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013025 {
13026 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013027 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013028
Bram Moolenaar4770d092006-01-12 23:22:24 +000013029 /* try all kinds of inserts/deletes/swaps/etc. */
13030 /* TODO: also soundfold the next words, so that we can try joining
13031 * and splitting */
13032 suggest_trie_walk(su, lp, salword, TRUE);
13033 }
13034 }
13035}
13036
13037/*
13038 * Finish up after calling suggest_try_soundalike().
13039 */
13040 static void
13041suggest_try_soundalike_finish()
13042{
13043 langp_T *lp;
13044 int lpi;
13045 slang_T *slang;
13046 int todo;
13047 hashitem_T *hi;
13048
13049 /* Do this for all languages that support sound folding and for which a
13050 * .sug file has been loaded. */
13051 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
13052 {
13053 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13054 slang = lp->lp_slang;
13055 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13056 {
13057 /* Free the info about handled words. */
13058 todo = slang->sl_sounddone.ht_used;
13059 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
13060 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013061 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013062 vim_free(HI2SFT(hi));
13063 --todo;
13064 }
13065 hash_clear(&slang->sl_sounddone);
13066 }
13067 }
13068}
13069
13070/*
13071 * A match with a soundfolded word is found. Add the good word(s) that
13072 * produce this soundfolded word.
13073 */
13074 static void
13075add_sound_suggest(su, goodword, score, lp)
13076 suginfo_T *su;
13077 char_u *goodword;
13078 int score; /* soundfold score */
13079 langp_T *lp;
13080{
13081 slang_T *slang = lp->lp_slang; /* language for sound folding */
13082 int sfwordnr;
13083 char_u *nrline;
13084 int orgnr;
13085 char_u theword[MAXWLEN];
13086 int i;
13087 int wlen;
13088 char_u *byts;
13089 idx_T *idxs;
13090 int n;
13091 int wordcount;
13092 int wc;
13093 int goodscore;
13094 hash_T hash;
13095 hashitem_T *hi;
13096 sftword_T *sft;
13097 int bc, gc;
13098 int limit;
13099
13100 /*
13101 * It's very well possible that the same soundfold word is found several
13102 * times with different scores. Since the following is quite slow only do
13103 * the words that have a better score than before. Use a hashtable to
13104 * remember the words that have been done.
13105 */
13106 hash = hash_hash(goodword);
13107 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
13108 if (HASHITEM_EMPTY(hi))
13109 {
13110 sft = (sftword_T *)alloc(sizeof(sftword_T) + STRLEN(goodword));
13111 if (sft != NULL)
13112 {
13113 sft->sft_score = score;
13114 STRCPY(sft->sft_word, goodword);
13115 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
13116 }
13117 }
13118 else
13119 {
13120 sft = HI2SFT(hi);
13121 if (score >= sft->sft_score)
13122 return;
13123 sft->sft_score = score;
13124 }
13125
13126 /*
13127 * Find the word nr in the soundfold tree.
13128 */
13129 sfwordnr = soundfold_find(slang, goodword);
13130 if (sfwordnr < 0)
13131 {
13132 EMSG2(_(e_intern2), "add_sound_suggest()");
13133 return;
13134 }
13135
13136 /*
13137 * go over the list of good words that produce this soundfold word
13138 */
13139 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
13140 orgnr = 0;
13141 while (*nrline != NUL)
13142 {
13143 /* The wordnr was stored in a minimal nr of bytes as an offset to the
13144 * previous wordnr. */
13145 orgnr += bytes2offset(&nrline);
13146
13147 byts = slang->sl_fbyts;
13148 idxs = slang->sl_fidxs;
13149
13150 /* Lookup the word "orgnr" one of the two tries. */
13151 n = 0;
13152 wlen = 0;
13153 wordcount = 0;
13154 for (;;)
13155 {
13156 i = 1;
13157 if (wordcount == orgnr && byts[n + 1] == NUL)
13158 break; /* found end of word */
13159
13160 if (byts[n + 1] == NUL)
13161 ++wordcount;
13162
13163 /* skip over the NUL bytes */
13164 for ( ; byts[n + i] == NUL; ++i)
13165 if (i > byts[n]) /* safety check */
13166 {
13167 STRCPY(theword + wlen, "BAD");
13168 goto badword;
13169 }
13170
13171 /* One of the siblings must have the word. */
13172 for ( ; i < byts[n]; ++i)
13173 {
13174 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
13175 if (wordcount + wc > orgnr)
13176 break;
13177 wordcount += wc;
13178 }
13179
13180 theword[wlen++] = byts[n + i];
13181 n = idxs[n + i];
13182 }
13183badword:
13184 theword[wlen] = NUL;
13185
13186 /* Go over the possible flags and regions. */
13187 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
13188 {
13189 char_u cword[MAXWLEN];
13190 char_u *p;
13191 int flags = (int)idxs[n + i];
13192
Bram Moolenaare1438bb2006-03-01 22:01:55 +000013193 /* Skip words with the NOSUGGEST flag */
13194 if (flags & WF_NOSUGGEST)
13195 continue;
13196
Bram Moolenaar4770d092006-01-12 23:22:24 +000013197 if (flags & WF_KEEPCAP)
13198 {
13199 /* Must find the word in the keep-case tree. */
13200 find_keepcap_word(slang, theword, cword);
13201 p = cword;
13202 }
13203 else
13204 {
13205 flags |= su->su_badflags;
13206 if ((flags & WF_CAPMASK) != 0)
13207 {
13208 /* Need to fix case according to "flags". */
13209 make_case_word(theword, cword, flags);
13210 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013211 }
13212 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013213 p = theword;
13214 }
13215
13216 /* Add the suggestion. */
13217 if (sps_flags & SPS_DOUBLE)
13218 {
13219 /* Add the suggestion if the score isn't too bad. */
13220 if (score <= su->su_maxscore)
13221 add_suggestion(su, &su->su_sga, p, su->su_badlen,
13222 score, 0, FALSE, slang, FALSE);
13223 }
13224 else
13225 {
13226 /* Add a penalty for words in another region. */
13227 if ((flags & WF_REGION)
13228 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
13229 goodscore = SCORE_REGION;
13230 else
13231 goodscore = 0;
13232
13233 /* Add a small penalty for changing the first letter from
13234 * lower to upper case. Helps for "tath" -> "Kath", which is
13235 * less common thatn "tath" -> "path". Don't do it when the
13236 * letter is the same, that has already been counted. */
13237 gc = PTR2CHAR(p);
13238 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013239 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013240 bc = PTR2CHAR(su->su_badword);
13241 if (!SPELL_ISUPPER(bc)
13242 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
13243 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013244 }
13245
Bram Moolenaar4770d092006-01-12 23:22:24 +000013246 /* Compute the score for the good word. This only does letter
13247 * insert/delete/swap/replace. REP items are not considered,
13248 * which may make the score a bit higher.
13249 * Use a limit for the score to make it work faster. Use
13250 * MAXSCORE(), because RESCORE() will change the score.
13251 * If the limit is very high then the iterative method is
13252 * inefficient, using an array is quicker. */
13253 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
13254 if (limit > SCORE_LIMITMAX)
13255 goodscore += spell_edit_score(slang, su->su_badword, p);
13256 else
13257 goodscore += spell_edit_score_limit(slang, su->su_badword,
13258 p, limit);
13259
13260 /* When going over the limit don't bother to do the rest. */
13261 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013262 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013263 /* Give a bonus to words seen before. */
13264 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013265
Bram Moolenaar4770d092006-01-12 23:22:24 +000013266 /* Add the suggestion if the score isn't too bad. */
13267 goodscore = RESCORE(goodscore, score);
13268 if (goodscore <= su->su_sfmaxscore)
13269 add_suggestion(su, &su->su_ga, p, su->su_badlen,
13270 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013271 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013272 }
13273 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013274 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013275 }
13276}
13277
13278/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013279 * Find word "word" in fold-case tree for "slang" and return the word number.
13280 */
13281 static int
13282soundfold_find(slang, word)
13283 slang_T *slang;
13284 char_u *word;
13285{
13286 idx_T arridx = 0;
13287 int len;
13288 int wlen = 0;
13289 int c;
13290 char_u *ptr = word;
13291 char_u *byts;
13292 idx_T *idxs;
13293 int wordnr = 0;
13294
13295 byts = slang->sl_sbyts;
13296 idxs = slang->sl_sidxs;
13297
13298 for (;;)
13299 {
13300 /* First byte is the number of possible bytes. */
13301 len = byts[arridx++];
13302
13303 /* If the first possible byte is a zero the word could end here.
13304 * If the word ends we found the word. If not skip the NUL bytes. */
13305 c = ptr[wlen];
13306 if (byts[arridx] == NUL)
13307 {
13308 if (c == NUL)
13309 break;
13310
13311 /* Skip over the zeros, there can be several. */
13312 while (len > 0 && byts[arridx] == NUL)
13313 {
13314 ++arridx;
13315 --len;
13316 }
13317 if (len == 0)
13318 return -1; /* no children, word should have ended here */
13319 ++wordnr;
13320 }
13321
13322 /* If the word ends we didn't find it. */
13323 if (c == NUL)
13324 return -1;
13325
13326 /* Perform a binary search in the list of accepted bytes. */
13327 if (c == TAB) /* <Tab> is handled like <Space> */
13328 c = ' ';
13329 while (byts[arridx] < c)
13330 {
13331 /* The word count is in the first idxs[] entry of the child. */
13332 wordnr += idxs[idxs[arridx]];
13333 ++arridx;
13334 if (--len == 0) /* end of the bytes, didn't find it */
13335 return -1;
13336 }
13337 if (byts[arridx] != c) /* didn't find the byte */
13338 return -1;
13339
13340 /* Continue at the child (if there is one). */
13341 arridx = idxs[arridx];
13342 ++wlen;
13343
13344 /* One space in the good word may stand for several spaces in the
13345 * checked word. */
13346 if (c == ' ')
13347 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
13348 ++wlen;
13349 }
13350
13351 return wordnr;
13352}
13353
13354/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013355 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013356 */
13357 static void
13358make_case_word(fword, cword, flags)
13359 char_u *fword;
13360 char_u *cword;
13361 int flags;
13362{
13363 if (flags & WF_ALLCAP)
13364 /* Make it all upper-case */
13365 allcap_copy(fword, cword);
13366 else if (flags & WF_ONECAP)
13367 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013368 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013369 else
13370 /* Use goodword as-is. */
13371 STRCPY(cword, fword);
13372}
13373
Bram Moolenaarea424162005-06-16 21:51:00 +000013374/*
13375 * Use map string "map" for languages "lp".
13376 */
13377 static void
13378set_map_str(lp, map)
13379 slang_T *lp;
13380 char_u *map;
13381{
13382 char_u *p;
13383 int headc = 0;
13384 int c;
13385 int i;
13386
13387 if (*map == NUL)
13388 {
13389 lp->sl_has_map = FALSE;
13390 return;
13391 }
13392 lp->sl_has_map = TRUE;
13393
Bram Moolenaar4770d092006-01-12 23:22:24 +000013394 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000013395 for (i = 0; i < 256; ++i)
13396 lp->sl_map_array[i] = 0;
13397#ifdef FEAT_MBYTE
13398 hash_init(&lp->sl_map_hash);
13399#endif
13400
13401 /*
13402 * The similar characters are stored separated with slashes:
13403 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13404 * before the same slash. For characters above 255 sl_map_hash is used.
13405 */
13406 for (p = map; *p != NUL; )
13407 {
13408#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013409 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000013410#else
13411 c = *p++;
13412#endif
13413 if (c == '/')
13414 headc = 0;
13415 else
13416 {
13417 if (headc == 0)
13418 headc = c;
13419
13420#ifdef FEAT_MBYTE
13421 /* Characters above 255 don't fit in sl_map_array[], put them in
13422 * the hash table. Each entry is the char, a NUL the headchar and
13423 * a NUL. */
13424 if (c >= 256)
13425 {
13426 int cl = mb_char2len(c);
13427 int headcl = mb_char2len(headc);
13428 char_u *b;
13429 hash_T hash;
13430 hashitem_T *hi;
13431
13432 b = alloc((unsigned)(cl + headcl + 2));
13433 if (b == NULL)
13434 return;
13435 mb_char2bytes(c, b);
13436 b[cl] = NUL;
13437 mb_char2bytes(headc, b + cl + 1);
13438 b[cl + 1 + headcl] = NUL;
13439 hash = hash_hash(b);
13440 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13441 if (HASHITEM_EMPTY(hi))
13442 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13443 else
13444 {
13445 /* This should have been checked when generating the .spl
13446 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013447 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000013448 vim_free(b);
13449 }
13450 }
13451 else
13452#endif
13453 lp->sl_map_array[c] = headc;
13454 }
13455 }
13456}
13457
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013458/*
13459 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13460 * lines in the .aff file.
13461 */
13462 static int
13463similar_chars(slang, c1, c2)
13464 slang_T *slang;
13465 int c1;
13466 int c2;
13467{
Bram Moolenaarea424162005-06-16 21:51:00 +000013468 int m1, m2;
13469#ifdef FEAT_MBYTE
13470 char_u buf[MB_MAXBYTES];
13471 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013472
Bram Moolenaarea424162005-06-16 21:51:00 +000013473 if (c1 >= 256)
13474 {
13475 buf[mb_char2bytes(c1, buf)] = 0;
13476 hi = hash_find(&slang->sl_map_hash, buf);
13477 if (HASHITEM_EMPTY(hi))
13478 m1 = 0;
13479 else
13480 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13481 }
13482 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013483#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013484 m1 = slang->sl_map_array[c1];
13485 if (m1 == 0)
13486 return FALSE;
13487
13488
13489#ifdef FEAT_MBYTE
13490 if (c2 >= 256)
13491 {
13492 buf[mb_char2bytes(c2, buf)] = 0;
13493 hi = hash_find(&slang->sl_map_hash, buf);
13494 if (HASHITEM_EMPTY(hi))
13495 m2 = 0;
13496 else
13497 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13498 }
13499 else
13500#endif
13501 m2 = slang->sl_map_array[c2];
13502
13503 return m1 == m2;
13504}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013505
13506/*
13507 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013508 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013509 */
13510 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000013511add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
13512 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013513 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013514 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013515 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013516 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013517 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013518 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013519 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013520 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013521 int maxsf; /* su_maxscore applies to soundfold score,
13522 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013523{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013524 int goodlen; /* len of goodword changed */
13525 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013526 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013527 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013528 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013529 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013530
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013531 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13532 * "thee the" is added next to changing the first "the" the "thee". */
13533 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013534 pbad = su->su_badptr + badlenarg;
13535 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013536 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013537 goodlen = pgood - goodword;
13538 badlen = pbad - su->su_badptr;
13539 if (goodlen <= 0 || badlen <= 0)
13540 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013541 mb_ptr_back(goodword, pgood);
13542 mb_ptr_back(su->su_badptr, pbad);
13543#ifdef FEAT_MBYTE
13544 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013545 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013546 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13547 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013548 }
13549 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013550#endif
13551 if (*pgood != *pbad)
13552 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013553 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013554
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013555 if (badlen == 0 && goodlen == 0)
13556 /* goodword doesn't change anything; may happen for "the the" changing
13557 * the first "the" to itself. */
13558 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013559
Bram Moolenaar4770d092006-01-12 23:22:24 +000013560 /* Check if the word is already there. Also check the length that is
13561 * being replaced "thes," -> "these" is a different suggestion from
13562 * "thes" -> "these". */
13563 stp = &SUG(*gap, 0);
13564 for (i = gap->ga_len; --i >= 0; ++stp)
13565 if (stp->st_wordlen == goodlen
13566 && stp->st_orglen == badlen
13567 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
13568 {
13569 /*
13570 * Found it. Remember the word with the lowest score.
13571 */
13572 if (stp->st_slang == NULL)
13573 stp->st_slang = slang;
13574
13575 new_sug.st_score = score;
13576 new_sug.st_altscore = altscore;
13577 new_sug.st_had_bonus = had_bonus;
13578
13579 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013580 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013581 /* Only one of the two had the soundalike score computed.
13582 * Need to do that for the other one now, otherwise the
13583 * scores can't be compared. This happens because
13584 * suggest_try_change() doesn't compute the soundalike
13585 * word to keep it fast, while some special methods set
13586 * the soundalike score to zero. */
13587 if (had_bonus)
13588 rescore_one(su, stp);
13589 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013590 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013591 new_sug.st_word = stp->st_word;
13592 new_sug.st_wordlen = stp->st_wordlen;
13593 new_sug.st_slang = stp->st_slang;
13594 new_sug.st_orglen = badlen;
13595 rescore_one(su, &new_sug);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013596 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013597 }
13598
Bram Moolenaar4770d092006-01-12 23:22:24 +000013599 if (stp->st_score > new_sug.st_score)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013600 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013601 stp->st_score = new_sug.st_score;
13602 stp->st_altscore = new_sug.st_altscore;
13603 stp->st_had_bonus = new_sug.st_had_bonus;
13604 }
13605 break;
13606 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013607
Bram Moolenaar4770d092006-01-12 23:22:24 +000013608 if (i < 0 && ga_grow(gap, 1) == OK)
13609 {
13610 /* Add a suggestion. */
13611 stp = &SUG(*gap, gap->ga_len);
13612 stp->st_word = vim_strnsave(goodword, goodlen);
13613 if (stp->st_word != NULL)
13614 {
13615 stp->st_wordlen = goodlen;
13616 stp->st_score = score;
13617 stp->st_altscore = altscore;
13618 stp->st_had_bonus = had_bonus;
13619 stp->st_orglen = badlen;
13620 stp->st_slang = slang;
13621 ++gap->ga_len;
13622
13623 /* If we have too many suggestions now, sort the list and keep
13624 * the best suggestions. */
13625 if (gap->ga_len > SUG_MAX_COUNT(su))
13626 {
13627 if (maxsf)
13628 su->su_sfmaxscore = cleanup_suggestions(gap,
13629 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13630 else
13631 {
13632 i = su->su_maxscore;
13633 su->su_maxscore = cleanup_suggestions(gap,
13634 su->su_maxscore, SUG_CLEAN_COUNT(su));
13635 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013636 }
13637 }
13638 }
13639}
13640
13641/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013642 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13643 * for split words, such as "the the". Remove these from the list here.
13644 */
13645 static void
13646check_suggestions(su, gap)
13647 suginfo_T *su;
13648 garray_T *gap; /* either su_ga or su_sga */
13649{
13650 suggest_T *stp;
13651 int i;
13652 char_u longword[MAXWLEN + 1];
13653 int len;
13654 hlf_T attr;
13655
13656 stp = &SUG(*gap, 0);
13657 for (i = gap->ga_len - 1; i >= 0; --i)
13658 {
13659 /* Need to append what follows to check for "the the". */
13660 STRCPY(longword, stp[i].st_word);
13661 len = stp[i].st_wordlen;
13662 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13663 MAXWLEN - len);
13664 attr = HLF_COUNT;
13665 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13666 if (attr != HLF_COUNT)
13667 {
13668 /* Remove this entry. */
13669 vim_free(stp[i].st_word);
13670 --gap->ga_len;
13671 if (i < gap->ga_len)
13672 mch_memmove(stp + i, stp + i + 1,
13673 sizeof(suggest_T) * (gap->ga_len - i));
13674 }
13675 }
13676}
13677
13678
13679/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013680 * Add a word to be banned.
13681 */
13682 static void
13683add_banned(su, word)
13684 suginfo_T *su;
13685 char_u *word;
13686{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013687 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013688 hash_T hash;
13689 hashitem_T *hi;
13690
Bram Moolenaar4770d092006-01-12 23:22:24 +000013691 hash = hash_hash(word);
13692 hi = hash_lookup(&su->su_banned, word, hash);
13693 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013694 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013695 s = vim_strsave(word);
13696 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013697 hash_add_item(&su->su_banned, hi, s, hash);
13698 }
13699}
13700
13701/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013702 * Recompute the score for all suggestions if sound-folding is possible. This
13703 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013704 */
13705 static void
13706rescore_suggestions(su)
13707 suginfo_T *su;
13708{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013709 int i;
13710
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013711 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013712 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013713 rescore_one(su, &SUG(su->su_ga, i));
13714}
13715
13716/*
13717 * Recompute the score for one suggestion if sound-folding is possible.
13718 */
13719 static void
13720rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013721 suginfo_T *su;
13722 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013723{
13724 slang_T *slang = stp->st_slang;
13725 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013726 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013727
13728 /* Only rescore suggestions that have no sal score yet and do have a
13729 * language. */
13730 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13731 {
13732 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013733 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013734 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013735 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013736 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013737 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013738 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013739
13740 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013741 if (stp->st_altscore == SCORE_MAXMAX)
13742 stp->st_altscore = SCORE_BIG;
13743 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13744 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013745 }
13746}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013747
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013748static int
13749#ifdef __BORLANDC__
13750_RTLENTRYF
13751#endif
13752sug_compare __ARGS((const void *s1, const void *s2));
13753
13754/*
13755 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013756 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013757 */
13758 static int
13759#ifdef __BORLANDC__
13760_RTLENTRYF
13761#endif
13762sug_compare(s1, s2)
13763 const void *s1;
13764 const void *s2;
13765{
13766 suggest_T *p1 = (suggest_T *)s1;
13767 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013768 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013769
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013770 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013771 {
13772 n = p1->st_altscore - p2->st_altscore;
13773 if (n == 0)
13774 n = STRICMP(p1->st_word, p2->st_word);
13775 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013776 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013777}
13778
13779/*
13780 * Cleanup the suggestions:
13781 * - Sort on score.
13782 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013783 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013784 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013785 static int
13786cleanup_suggestions(gap, maxscore, keep)
13787 garray_T *gap;
13788 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013789 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013790{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013791 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013792 int i;
13793
13794 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013795 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013796
13797 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013798 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013799 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013800 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013801 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013802 gap->ga_len = keep;
13803 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013804 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013805 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013806}
13807
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013808#if defined(FEAT_EVAL) || defined(PROTO)
13809/*
13810 * Soundfold a string, for soundfold().
13811 * Result is in allocated memory, NULL for an error.
13812 */
13813 char_u *
13814eval_soundfold(word)
13815 char_u *word;
13816{
13817 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013818 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013819 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013820
13821 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13822 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013823 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013824 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013825 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013826 if (lp->lp_slang->sl_sal.ga_len > 0)
13827 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013828 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013829 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013830 return vim_strsave(sound);
13831 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013832 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013833
13834 /* No language with sound folding, return word as-is. */
13835 return vim_strsave(word);
13836}
13837#endif
13838
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013839/*
13840 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000013841 *
13842 * There are many ways to turn a word into a sound-a-like representation. The
13843 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
13844 * swedish name matching - survey and test of different algorithms" by Klas
13845 * Erikson.
13846 *
13847 * We support two methods:
13848 * 1. SOFOFROM/SOFOTO do a simple character mapping.
13849 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013850 */
13851 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013852spell_soundfold(slang, inword, folded, res)
13853 slang_T *slang;
13854 char_u *inword;
13855 int folded; /* "inword" is already case-folded */
13856 char_u *res;
13857{
13858 char_u fword[MAXWLEN];
13859 char_u *word;
13860
13861 if (slang->sl_sofo)
13862 /* SOFOFROM and SOFOTO used */
13863 spell_soundfold_sofo(slang, inword, res);
13864 else
13865 {
13866 /* SAL items used. Requires the word to be case-folded. */
13867 if (folded)
13868 word = inword;
13869 else
13870 {
13871 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
13872 word = fword;
13873 }
13874
13875#ifdef FEAT_MBYTE
13876 if (has_mbyte)
13877 spell_soundfold_wsal(slang, word, res);
13878 else
13879#endif
13880 spell_soundfold_sal(slang, word, res);
13881 }
13882}
13883
13884/*
13885 * Perform sound folding of "inword" into "res" according to SOFOFROM and
13886 * SOFOTO lines.
13887 */
13888 static void
13889spell_soundfold_sofo(slang, inword, res)
13890 slang_T *slang;
13891 char_u *inword;
13892 char_u *res;
13893{
13894 char_u *s;
13895 int ri = 0;
13896 int c;
13897
13898#ifdef FEAT_MBYTE
13899 if (has_mbyte)
13900 {
13901 int prevc = 0;
13902 int *ip;
13903
13904 /* The sl_sal_first[] table contains the translation for chars up to
13905 * 255, sl_sal the rest. */
13906 for (s = inword; *s != NUL; )
13907 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013908 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013909 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
13910 c = ' ';
13911 else if (c < 256)
13912 c = slang->sl_sal_first[c];
13913 else
13914 {
13915 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
13916 if (ip == NULL) /* empty list, can't match */
13917 c = NUL;
13918 else
13919 for (;;) /* find "c" in the list */
13920 {
13921 if (*ip == 0) /* not found */
13922 {
13923 c = NUL;
13924 break;
13925 }
13926 if (*ip == c) /* match! */
13927 {
13928 c = ip[1];
13929 break;
13930 }
13931 ip += 2;
13932 }
13933 }
13934
13935 if (c != NUL && c != prevc)
13936 {
13937 ri += mb_char2bytes(c, res + ri);
13938 if (ri + MB_MAXBYTES > MAXWLEN)
13939 break;
13940 prevc = c;
13941 }
13942 }
13943 }
13944 else
13945#endif
13946 {
13947 /* The sl_sal_first[] table contains the translation. */
13948 for (s = inword; (c = *s) != NUL; ++s)
13949 {
13950 if (vim_iswhite(c))
13951 c = ' ';
13952 else
13953 c = slang->sl_sal_first[c];
13954 if (c != NUL && (ri == 0 || res[ri - 1] != c))
13955 res[ri++] = c;
13956 }
13957 }
13958
13959 res[ri] = NUL;
13960}
13961
13962 static void
13963spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013964 slang_T *slang;
13965 char_u *inword;
13966 char_u *res;
13967{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013968 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013969 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013970 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013971 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013972 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013973 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013974 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013975 int n, k = 0;
13976 int z0;
13977 int k0;
13978 int n0;
13979 int c;
13980 int pri;
13981 int p0 = -333;
13982 int c0;
13983
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013984 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013985 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013986 if (slang->sl_rem_accents)
13987 {
13988 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013989 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013990 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013991 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013992 {
13993 *t++ = ' ';
13994 s = skipwhite(s);
13995 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013996 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013997 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013998 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013999 *t++ = *s;
14000 ++s;
14001 }
14002 }
14003 *t = NUL;
14004 }
14005 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014006 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014007
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014008 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014009
14010 /*
14011 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014012 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014013 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014014 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014015 while ((c = word[i]) != NUL)
14016 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014017 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014018 n = slang->sl_sal_first[c];
14019 z0 = 0;
14020
14021 if (n >= 0)
14022 {
14023 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014024 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014025 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014026 /* Quickly skip entries that don't match the word. Most
14027 * entries are less then three chars, optimize for that. */
14028 k = smp[n].sm_leadlen;
14029 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014030 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014031 if (word[i + 1] != s[1])
14032 continue;
14033 if (k > 2)
14034 {
14035 for (j = 2; j < k; ++j)
14036 if (word[i + j] != s[j])
14037 break;
14038 if (j < k)
14039 continue;
14040 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014041 }
14042
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014043 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014044 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014045 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014046 while (*pf != NUL && *pf != word[i + k])
14047 ++pf;
14048 if (*pf == NUL)
14049 continue;
14050 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014051 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014052 s = smp[n].sm_rules;
14053 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014054
14055 p0 = *s;
14056 k0 = k;
14057 while (*s == '-' && k > 1)
14058 {
14059 k--;
14060 s++;
14061 }
14062 if (*s == '<')
14063 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014064 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014065 {
14066 /* determine priority */
14067 pri = *s - '0';
14068 s++;
14069 }
14070 if (*s == '^' && *(s + 1) == '^')
14071 s++;
14072
14073 if (*s == NUL
14074 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014075 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014076 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014077 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014078 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014079 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014080 && spell_iswordp(word + i - 1, curbuf)
14081 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014082 {
14083 /* search for followup rules, if: */
14084 /* followup and k > 1 and NO '-' in searchstring */
14085 c0 = word[i + k - 1];
14086 n0 = slang->sl_sal_first[c0];
14087
14088 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014089 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014090 {
14091 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014092 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014093 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014094 /* Quickly skip entries that don't match the word.
14095 * */
14096 k0 = smp[n0].sm_leadlen;
14097 if (k0 > 1)
14098 {
14099 if (word[i + k] != s[1])
14100 continue;
14101 if (k0 > 2)
14102 {
14103 pf = word + i + k + 1;
14104 for (j = 2; j < k0; ++j)
14105 if (*pf++ != s[j])
14106 break;
14107 if (j < k0)
14108 continue;
14109 }
14110 }
14111 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014112
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014113 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014114 {
14115 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014116 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014117 while (*pf != NUL && *pf != word[i + k0])
14118 ++pf;
14119 if (*pf == NUL)
14120 continue;
14121 ++k0;
14122 }
14123
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014124 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014125 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014126 while (*s == '-')
14127 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014128 /* "k0" gets NOT reduced because
14129 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014130 s++;
14131 }
14132 if (*s == '<')
14133 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014134 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014135 {
14136 p0 = *s - '0';
14137 s++;
14138 }
14139
14140 if (*s == NUL
14141 /* *s == '^' cuts */
14142 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014143 && !spell_iswordp(word + i + k0,
14144 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014145 {
14146 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014147 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014148 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014149
14150 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014151 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014152 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014153 /* rule fits; stop search */
14154 break;
14155 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014156 }
14157
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014158 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014159 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014160 }
14161
14162 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014163 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014164 if (s == NULL)
14165 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014166 pf = smp[n].sm_rules;
14167 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014168 if (p0 == 1 && z == 0)
14169 {
14170 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014171 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
14172 || res[reslen - 1] == *s))
14173 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014174 z0 = 1;
14175 z = 1;
14176 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014177 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014178 {
14179 word[i + k0] = *s;
14180 k0++;
14181 s++;
14182 }
14183 if (k > k0)
14184 mch_memmove(word + i + k0, word + i + k,
14185 STRLEN(word + i + k) + 1);
14186
14187 /* new "actual letter" */
14188 c = word[i];
14189 }
14190 else
14191 {
14192 /* no '<' rule used */
14193 i += k - 1;
14194 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014195 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014196 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014197 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014198 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014199 s++;
14200 }
14201 /* new "actual letter" */
14202 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014203 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014204 {
14205 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014206 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014207 mch_memmove(word, word + i + 1,
14208 STRLEN(word + i + 1) + 1);
14209 i = 0;
14210 z0 = 1;
14211 }
14212 }
14213 break;
14214 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014215 }
14216 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014217 else if (vim_iswhite(c))
14218 {
14219 c = ' ';
14220 k = 1;
14221 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014222
14223 if (z0 == 0)
14224 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014225 if (k && !p0 && reslen < MAXWLEN && c != NUL
14226 && (!slang->sl_collapse || reslen == 0
14227 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014228 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014229 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014230
14231 i++;
14232 z = 0;
14233 k = 0;
14234 }
14235 }
14236
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014237 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014238}
14239
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014240#ifdef FEAT_MBYTE
14241/*
14242 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14243 * Multi-byte version of spell_soundfold().
14244 */
14245 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014246spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014247 slang_T *slang;
14248 char_u *inword;
14249 char_u *res;
14250{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014251 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014252 int word[MAXWLEN];
14253 int wres[MAXWLEN];
14254 int l;
14255 char_u *s;
14256 int *ws;
14257 char_u *t;
14258 int *pf;
14259 int i, j, z;
14260 int reslen;
14261 int n, k = 0;
14262 int z0;
14263 int k0;
14264 int n0;
14265 int c;
14266 int pri;
14267 int p0 = -333;
14268 int c0;
14269 int did_white = FALSE;
14270
14271 /*
14272 * Convert the multi-byte string to a wide-character string.
14273 * Remove accents, if wanted. We actually remove all non-word characters.
14274 * But keep white space.
14275 */
14276 n = 0;
14277 for (s = inword; *s != NUL; )
14278 {
14279 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014280 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014281 if (slang->sl_rem_accents)
14282 {
14283 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14284 {
14285 if (did_white)
14286 continue;
14287 c = ' ';
14288 did_white = TRUE;
14289 }
14290 else
14291 {
14292 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014293 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014294 continue;
14295 }
14296 }
14297 word[n++] = c;
14298 }
14299 word[n] = NUL;
14300
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014301 /*
14302 * This comes from Aspell phonet.cpp.
14303 * Converted from C++ to C. Added support for multi-byte chars.
14304 * Changed to keep spaces.
14305 */
14306 i = reslen = z = 0;
14307 while ((c = word[i]) != NUL)
14308 {
14309 /* Start with the first rule that has the character in the word. */
14310 n = slang->sl_sal_first[c & 0xff];
14311 z0 = 0;
14312
14313 if (n >= 0)
14314 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014315 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014316 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
14317 {
14318 /* Quickly skip entries that don't match the word. Most
14319 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014320 if (c != ws[0])
14321 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014322 k = smp[n].sm_leadlen;
14323 if (k > 1)
14324 {
14325 if (word[i + 1] != ws[1])
14326 continue;
14327 if (k > 2)
14328 {
14329 for (j = 2; j < k; ++j)
14330 if (word[i + j] != ws[j])
14331 break;
14332 if (j < k)
14333 continue;
14334 }
14335 }
14336
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014337 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014338 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014339 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014340 while (*pf != NUL && *pf != word[i + k])
14341 ++pf;
14342 if (*pf == NUL)
14343 continue;
14344 ++k;
14345 }
14346 s = smp[n].sm_rules;
14347 pri = 5; /* default priority */
14348
14349 p0 = *s;
14350 k0 = k;
14351 while (*s == '-' && k > 1)
14352 {
14353 k--;
14354 s++;
14355 }
14356 if (*s == '<')
14357 s++;
14358 if (VIM_ISDIGIT(*s))
14359 {
14360 /* determine priority */
14361 pri = *s - '0';
14362 s++;
14363 }
14364 if (*s == '^' && *(s + 1) == '^')
14365 s++;
14366
14367 if (*s == NUL
14368 || (*s == '^'
14369 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014370 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014371 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014372 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014373 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014374 && spell_iswordp_w(word + i - 1, curbuf)
14375 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014376 {
14377 /* search for followup rules, if: */
14378 /* followup and k > 1 and NO '-' in searchstring */
14379 c0 = word[i + k - 1];
14380 n0 = slang->sl_sal_first[c0 & 0xff];
14381
14382 if (slang->sl_followup && k > 1 && n0 >= 0
14383 && p0 != '-' && word[i + k] != NUL)
14384 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014385 /* Test follow-up rule for "word[i + k]"; loop over
14386 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014387 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14388 == (c0 & 0xff); ++n0)
14389 {
14390 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014391 */
14392 if (c0 != ws[0])
14393 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014394 k0 = smp[n0].sm_leadlen;
14395 if (k0 > 1)
14396 {
14397 if (word[i + k] != ws[1])
14398 continue;
14399 if (k0 > 2)
14400 {
14401 pf = word + i + k + 1;
14402 for (j = 2; j < k0; ++j)
14403 if (*pf++ != ws[j])
14404 break;
14405 if (j < k0)
14406 continue;
14407 }
14408 }
14409 k0 += k - 1;
14410
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014411 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014412 {
14413 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014414 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014415 while (*pf != NUL && *pf != word[i + k0])
14416 ++pf;
14417 if (*pf == NUL)
14418 continue;
14419 ++k0;
14420 }
14421
14422 p0 = 5;
14423 s = smp[n0].sm_rules;
14424 while (*s == '-')
14425 {
14426 /* "k0" gets NOT reduced because
14427 * "if (k0 == k)" */
14428 s++;
14429 }
14430 if (*s == '<')
14431 s++;
14432 if (VIM_ISDIGIT(*s))
14433 {
14434 p0 = *s - '0';
14435 s++;
14436 }
14437
14438 if (*s == NUL
14439 /* *s == '^' cuts */
14440 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014441 && !spell_iswordp_w(word + i + k0,
14442 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014443 {
14444 if (k0 == k)
14445 /* this is just a piece of the string */
14446 continue;
14447
14448 if (p0 < pri)
14449 /* priority too low */
14450 continue;
14451 /* rule fits; stop search */
14452 break;
14453 }
14454 }
14455
14456 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14457 == (c0 & 0xff))
14458 continue;
14459 }
14460
14461 /* replace string */
14462 ws = smp[n].sm_to_w;
14463 s = smp[n].sm_rules;
14464 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14465 if (p0 == 1 && z == 0)
14466 {
14467 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014468 if (reslen > 0 && ws != NULL && *ws != NUL
14469 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014470 || wres[reslen - 1] == *ws))
14471 reslen--;
14472 z0 = 1;
14473 z = 1;
14474 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014475 if (ws != NULL)
14476 while (*ws != NUL && word[i + k0] != NUL)
14477 {
14478 word[i + k0] = *ws;
14479 k0++;
14480 ws++;
14481 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014482 if (k > k0)
14483 mch_memmove(word + i + k0, word + i + k,
14484 sizeof(int) * (STRLEN(word + i + k) + 1));
14485
14486 /* new "actual letter" */
14487 c = word[i];
14488 }
14489 else
14490 {
14491 /* no '<' rule used */
14492 i += k - 1;
14493 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014494 if (ws != NULL)
14495 while (*ws != NUL && ws[1] != NUL
14496 && reslen < MAXWLEN)
14497 {
14498 if (reslen == 0 || wres[reslen - 1] != *ws)
14499 wres[reslen++] = *ws;
14500 ws++;
14501 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014502 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014503 if (ws == NULL)
14504 c = NUL;
14505 else
14506 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014507 if (strstr((char *)s, "^^") != NULL)
14508 {
14509 if (c != NUL)
14510 wres[reslen++] = c;
14511 mch_memmove(word, word + i + 1,
14512 sizeof(int) * (STRLEN(word + i + 1) + 1));
14513 i = 0;
14514 z0 = 1;
14515 }
14516 }
14517 break;
14518 }
14519 }
14520 }
14521 else if (vim_iswhite(c))
14522 {
14523 c = ' ';
14524 k = 1;
14525 }
14526
14527 if (z0 == 0)
14528 {
14529 if (k && !p0 && reslen < MAXWLEN && c != NUL
14530 && (!slang->sl_collapse || reslen == 0
14531 || wres[reslen - 1] != c))
14532 /* condense only double letters */
14533 wres[reslen++] = c;
14534
14535 i++;
14536 z = 0;
14537 k = 0;
14538 }
14539 }
14540
14541 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14542 l = 0;
14543 for (n = 0; n < reslen; ++n)
14544 {
14545 l += mb_char2bytes(wres[n], res + l);
14546 if (l + MB_MAXBYTES > MAXWLEN)
14547 break;
14548 }
14549 res[l] = NUL;
14550}
14551#endif
14552
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014553/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014554 * Compute a score for two sound-a-like words.
14555 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14556 * Instead of a generic loop we write out the code. That keeps it fast by
14557 * avoiding checks that will not be possible.
14558 */
14559 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014560soundalike_score(goodstart, badstart)
14561 char_u *goodstart; /* sound-folded good word */
14562 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014563{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014564 char_u *goodsound = goodstart;
14565 char_u *badsound = badstart;
14566 int goodlen;
14567 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014568 int n;
14569 char_u *pl, *ps;
14570 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014571 int score = 0;
14572
14573 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
14574 * counted so much, vowels halfway the word aren't counted at all. */
14575 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14576 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014577 if (badsound[1] == goodsound[1]
14578 || (badsound[1] != NUL
14579 && goodsound[1] != NUL
14580 && badsound[2] == goodsound[2]))
14581 {
14582 /* handle like a substitute */
14583 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014584 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014585 {
14586 score = 2 * SCORE_DEL / 3;
14587 if (*badsound == '*')
14588 ++badsound;
14589 else
14590 ++goodsound;
14591 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014592 }
14593
14594 goodlen = STRLEN(goodsound);
14595 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014596
14597 /* Return quickly if the lenghts are too different to be fixed by two
14598 * changes. */
14599 n = goodlen - badlen;
14600 if (n < -2 || n > 2)
14601 return SCORE_MAXMAX;
14602
14603 if (n > 0)
14604 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014605 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014606 ps = badsound;
14607 }
14608 else
14609 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014610 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014611 ps = goodsound;
14612 }
14613
14614 /* Skip over the identical part. */
14615 while (*pl == *ps && *pl != NUL)
14616 {
14617 ++pl;
14618 ++ps;
14619 }
14620
14621 switch (n)
14622 {
14623 case -2:
14624 case 2:
14625 /*
14626 * Must delete two characters from "pl".
14627 */
14628 ++pl; /* first delete */
14629 while (*pl == *ps)
14630 {
14631 ++pl;
14632 ++ps;
14633 }
14634 /* strings must be equal after second delete */
14635 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014636 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014637
14638 /* Failed to compare. */
14639 break;
14640
14641 case -1:
14642 case 1:
14643 /*
14644 * Minimal one delete from "pl" required.
14645 */
14646
14647 /* 1: delete */
14648 pl2 = pl + 1;
14649 ps2 = ps;
14650 while (*pl2 == *ps2)
14651 {
14652 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014653 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014654 ++pl2;
14655 ++ps2;
14656 }
14657
14658 /* 2: delete then swap, then rest must be equal */
14659 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14660 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014661 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014662
14663 /* 3: delete then substitute, then the rest must be equal */
14664 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014665 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014666
14667 /* 4: first swap then delete */
14668 if (pl[0] == ps[1] && pl[1] == ps[0])
14669 {
14670 pl2 = pl + 2; /* swap, skip two chars */
14671 ps2 = ps + 2;
14672 while (*pl2 == *ps2)
14673 {
14674 ++pl2;
14675 ++ps2;
14676 }
14677 /* delete a char and then strings must be equal */
14678 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014679 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014680 }
14681
14682 /* 5: first substitute then delete */
14683 pl2 = pl + 1; /* substitute, skip one char */
14684 ps2 = ps + 1;
14685 while (*pl2 == *ps2)
14686 {
14687 ++pl2;
14688 ++ps2;
14689 }
14690 /* delete a char and then strings must be equal */
14691 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014692 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014693
14694 /* Failed to compare. */
14695 break;
14696
14697 case 0:
14698 /*
14699 * Lenghts are equal, thus changes must result in same length: An
14700 * insert is only possible in combination with a delete.
14701 * 1: check if for identical strings
14702 */
14703 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014704 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014705
14706 /* 2: swap */
14707 if (pl[0] == ps[1] && pl[1] == ps[0])
14708 {
14709 pl2 = pl + 2; /* swap, skip two chars */
14710 ps2 = ps + 2;
14711 while (*pl2 == *ps2)
14712 {
14713 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014714 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014715 ++pl2;
14716 ++ps2;
14717 }
14718 /* 3: swap and swap again */
14719 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14720 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014721 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014722
14723 /* 4: swap and substitute */
14724 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014725 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014726 }
14727
14728 /* 5: substitute */
14729 pl2 = pl + 1;
14730 ps2 = ps + 1;
14731 while (*pl2 == *ps2)
14732 {
14733 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014734 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014735 ++pl2;
14736 ++ps2;
14737 }
14738
14739 /* 6: substitute and swap */
14740 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14741 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014742 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014743
14744 /* 7: substitute and substitute */
14745 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014746 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014747
14748 /* 8: insert then delete */
14749 pl2 = pl;
14750 ps2 = ps + 1;
14751 while (*pl2 == *ps2)
14752 {
14753 ++pl2;
14754 ++ps2;
14755 }
14756 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014757 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014758
14759 /* 9: delete then insert */
14760 pl2 = pl + 1;
14761 ps2 = ps;
14762 while (*pl2 == *ps2)
14763 {
14764 ++pl2;
14765 ++ps2;
14766 }
14767 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014768 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014769
14770 /* Failed to compare. */
14771 break;
14772 }
14773
14774 return SCORE_MAXMAX;
14775}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014776
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014777/*
14778 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014779 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014780 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000014781 * The algorithm is described by Du and Chang, 1992.
14782 * The implementation of the algorithm comes from Aspell editdist.cpp,
14783 * edit_distance(). It has been converted from C++ to C and modified to
14784 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014785 */
14786 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000014787spell_edit_score(slang, badword, goodword)
14788 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014789 char_u *badword;
14790 char_u *goodword;
14791{
14792 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014793 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014794 int j, i;
14795 int t;
14796 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014797 int pbc, pgc;
14798#ifdef FEAT_MBYTE
14799 char_u *p;
14800 int wbadword[MAXWLEN];
14801 int wgoodword[MAXWLEN];
14802
14803 if (has_mbyte)
14804 {
14805 /* Get the characters from the multi-byte strings and put them in an
14806 * int array for easy access. */
14807 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014808 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014809 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014810 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014811 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014812 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014813 }
14814 else
14815#endif
14816 {
14817 badlen = STRLEN(badword) + 1;
14818 goodlen = STRLEN(goodword) + 1;
14819 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014820
14821 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
14822#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014823 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
14824 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014825 if (cnt == NULL)
14826 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014827
14828 CNT(0, 0) = 0;
14829 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000014830 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014831
14832 for (i = 1; i <= badlen; ++i)
14833 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014834 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014835 for (j = 1; j <= goodlen; ++j)
14836 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014837#ifdef FEAT_MBYTE
14838 if (has_mbyte)
14839 {
14840 bc = wbadword[i - 1];
14841 gc = wgoodword[j - 1];
14842 }
14843 else
14844#endif
14845 {
14846 bc = badword[i - 1];
14847 gc = goodword[j - 1];
14848 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014849 if (bc == gc)
14850 CNT(i, j) = CNT(i - 1, j - 1);
14851 else
14852 {
14853 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014854 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014855 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
14856 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014857 {
14858 /* For a similar character use SCORE_SIMILAR. */
14859 if (slang != NULL
14860 && slang->sl_has_map
14861 && similar_chars(slang, gc, bc))
14862 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
14863 else
14864 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
14865 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014866
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014867 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014868 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014869#ifdef FEAT_MBYTE
14870 if (has_mbyte)
14871 {
14872 pbc = wbadword[i - 2];
14873 pgc = wgoodword[j - 2];
14874 }
14875 else
14876#endif
14877 {
14878 pbc = badword[i - 2];
14879 pgc = goodword[j - 2];
14880 }
14881 if (bc == pgc && pbc == gc)
14882 {
14883 t = SCORE_SWAP + CNT(i - 2, j - 2);
14884 if (t < CNT(i, j))
14885 CNT(i, j) = t;
14886 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014887 }
14888 t = SCORE_DEL + CNT(i - 1, j);
14889 if (t < CNT(i, j))
14890 CNT(i, j) = t;
14891 t = SCORE_INS + CNT(i, j - 1);
14892 if (t < CNT(i, j))
14893 CNT(i, j) = t;
14894 }
14895 }
14896 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014897
14898 i = CNT(badlen - 1, goodlen - 1);
14899 vim_free(cnt);
14900 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014901}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000014902
Bram Moolenaar4770d092006-01-12 23:22:24 +000014903typedef struct
14904{
14905 int badi;
14906 int goodi;
14907 int score;
14908} limitscore_T;
14909
14910/*
14911 * Like spell_edit_score(), but with a limit on the score to make it faster.
14912 * May return SCORE_MAXMAX when the score is higher than "limit".
14913 *
14914 * This uses a stack for the edits still to be tried.
14915 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
14916 * for multi-byte characters.
14917 */
14918 static int
14919spell_edit_score_limit(slang, badword, goodword, limit)
14920 slang_T *slang;
14921 char_u *badword;
14922 char_u *goodword;
14923 int limit;
14924{
14925 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
14926 int stackidx;
14927 int bi, gi;
14928 int bi2, gi2;
14929 int bc, gc;
14930 int score;
14931 int score_off;
14932 int minscore;
14933 int round;
14934
14935#ifdef FEAT_MBYTE
14936 /* Multi-byte characters require a bit more work, use a different function
14937 * to avoid testing "has_mbyte" quite often. */
14938 if (has_mbyte)
14939 return spell_edit_score_limit_w(slang, badword, goodword, limit);
14940#endif
14941
14942 /*
14943 * The idea is to go from start to end over the words. So long as
14944 * characters are equal just continue, this always gives the lowest score.
14945 * When there is a difference try several alternatives. Each alternative
14946 * increases "score" for the edit distance. Some of the alternatives are
14947 * pushed unto a stack and tried later, some are tried right away. At the
14948 * end of the word the score for one alternative is known. The lowest
14949 * possible score is stored in "minscore".
14950 */
14951 stackidx = 0;
14952 bi = 0;
14953 gi = 0;
14954 score = 0;
14955 minscore = limit + 1;
14956
14957 for (;;)
14958 {
14959 /* Skip over an equal part, score remains the same. */
14960 for (;;)
14961 {
14962 bc = badword[bi];
14963 gc = goodword[gi];
14964 if (bc != gc) /* stop at a char that's different */
14965 break;
14966 if (bc == NUL) /* both words end */
14967 {
14968 if (score < minscore)
14969 minscore = score;
14970 goto pop; /* do next alternative */
14971 }
14972 ++bi;
14973 ++gi;
14974 }
14975
14976 if (gc == NUL) /* goodword ends, delete badword chars */
14977 {
14978 do
14979 {
14980 if ((score += SCORE_DEL) >= minscore)
14981 goto pop; /* do next alternative */
14982 } while (badword[++bi] != NUL);
14983 minscore = score;
14984 }
14985 else if (bc == NUL) /* badword ends, insert badword chars */
14986 {
14987 do
14988 {
14989 if ((score += SCORE_INS) >= minscore)
14990 goto pop; /* do next alternative */
14991 } while (goodword[++gi] != NUL);
14992 minscore = score;
14993 }
14994 else /* both words continue */
14995 {
14996 /* If not close to the limit, perform a change. Only try changes
14997 * that may lead to a lower score than "minscore".
14998 * round 0: try deleting a char from badword
14999 * round 1: try inserting a char in badword */
15000 for (round = 0; round <= 1; ++round)
15001 {
15002 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15003 if (score_off < minscore)
15004 {
15005 if (score_off + SCORE_EDIT_MIN >= minscore)
15006 {
15007 /* Near the limit, rest of the words must match. We
15008 * can check that right now, no need to push an item
15009 * onto the stack. */
15010 bi2 = bi + 1 - round;
15011 gi2 = gi + round;
15012 while (goodword[gi2] == badword[bi2])
15013 {
15014 if (goodword[gi2] == NUL)
15015 {
15016 minscore = score_off;
15017 break;
15018 }
15019 ++bi2;
15020 ++gi2;
15021 }
15022 }
15023 else
15024 {
15025 /* try deleting/inserting a character later */
15026 stack[stackidx].badi = bi + 1 - round;
15027 stack[stackidx].goodi = gi + round;
15028 stack[stackidx].score = score_off;
15029 ++stackidx;
15030 }
15031 }
15032 }
15033
15034 if (score + SCORE_SWAP < minscore)
15035 {
15036 /* If swapping two characters makes a match then the
15037 * substitution is more expensive, thus there is no need to
15038 * try both. */
15039 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
15040 {
15041 /* Swap two characters, that is: skip them. */
15042 gi += 2;
15043 bi += 2;
15044 score += SCORE_SWAP;
15045 continue;
15046 }
15047 }
15048
15049 /* Substitute one character for another which is the same
15050 * thing as deleting a character from both goodword and badword.
15051 * Use a better score when there is only a case difference. */
15052 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15053 score += SCORE_ICASE;
15054 else
15055 {
15056 /* For a similar character use SCORE_SIMILAR. */
15057 if (slang != NULL
15058 && slang->sl_has_map
15059 && similar_chars(slang, gc, bc))
15060 score += SCORE_SIMILAR;
15061 else
15062 score += SCORE_SUBST;
15063 }
15064
15065 if (score < minscore)
15066 {
15067 /* Do the substitution. */
15068 ++gi;
15069 ++bi;
15070 continue;
15071 }
15072 }
15073pop:
15074 /*
15075 * Get here to try the next alternative, pop it from the stack.
15076 */
15077 if (stackidx == 0) /* stack is empty, finished */
15078 break;
15079
15080 /* pop an item from the stack */
15081 --stackidx;
15082 gi = stack[stackidx].goodi;
15083 bi = stack[stackidx].badi;
15084 score = stack[stackidx].score;
15085 }
15086
15087 /* When the score goes over "limit" it may actually be much higher.
15088 * Return a very large number to avoid going below the limit when giving a
15089 * bonus. */
15090 if (minscore > limit)
15091 return SCORE_MAXMAX;
15092 return minscore;
15093}
15094
15095#ifdef FEAT_MBYTE
15096/*
15097 * Multi-byte version of spell_edit_score_limit().
15098 * Keep it in sync with the above!
15099 */
15100 static int
15101spell_edit_score_limit_w(slang, badword, goodword, limit)
15102 slang_T *slang;
15103 char_u *badword;
15104 char_u *goodword;
15105 int limit;
15106{
15107 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15108 int stackidx;
15109 int bi, gi;
15110 int bi2, gi2;
15111 int bc, gc;
15112 int score;
15113 int score_off;
15114 int minscore;
15115 int round;
15116 char_u *p;
15117 int wbadword[MAXWLEN];
15118 int wgoodword[MAXWLEN];
15119
15120 /* Get the characters from the multi-byte strings and put them in an
15121 * int array for easy access. */
15122 bi = 0;
15123 for (p = badword; *p != NUL; )
15124 wbadword[bi++] = mb_cptr2char_adv(&p);
15125 wbadword[bi++] = 0;
15126 gi = 0;
15127 for (p = goodword; *p != NUL; )
15128 wgoodword[gi++] = mb_cptr2char_adv(&p);
15129 wgoodword[gi++] = 0;
15130
15131 /*
15132 * The idea is to go from start to end over the words. So long as
15133 * characters are equal just continue, this always gives the lowest score.
15134 * When there is a difference try several alternatives. Each alternative
15135 * increases "score" for the edit distance. Some of the alternatives are
15136 * pushed unto a stack and tried later, some are tried right away. At the
15137 * end of the word the score for one alternative is known. The lowest
15138 * possible score is stored in "minscore".
15139 */
15140 stackidx = 0;
15141 bi = 0;
15142 gi = 0;
15143 score = 0;
15144 minscore = limit + 1;
15145
15146 for (;;)
15147 {
15148 /* Skip over an equal part, score remains the same. */
15149 for (;;)
15150 {
15151 bc = wbadword[bi];
15152 gc = wgoodword[gi];
15153
15154 if (bc != gc) /* stop at a char that's different */
15155 break;
15156 if (bc == NUL) /* both words end */
15157 {
15158 if (score < minscore)
15159 minscore = score;
15160 goto pop; /* do next alternative */
15161 }
15162 ++bi;
15163 ++gi;
15164 }
15165
15166 if (gc == NUL) /* goodword ends, delete badword chars */
15167 {
15168 do
15169 {
15170 if ((score += SCORE_DEL) >= minscore)
15171 goto pop; /* do next alternative */
15172 } while (wbadword[++bi] != NUL);
15173 minscore = score;
15174 }
15175 else if (bc == NUL) /* badword ends, insert badword chars */
15176 {
15177 do
15178 {
15179 if ((score += SCORE_INS) >= minscore)
15180 goto pop; /* do next alternative */
15181 } while (wgoodword[++gi] != NUL);
15182 minscore = score;
15183 }
15184 else /* both words continue */
15185 {
15186 /* If not close to the limit, perform a change. Only try changes
15187 * that may lead to a lower score than "minscore".
15188 * round 0: try deleting a char from badword
15189 * round 1: try inserting a char in badword */
15190 for (round = 0; round <= 1; ++round)
15191 {
15192 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15193 if (score_off < minscore)
15194 {
15195 if (score_off + SCORE_EDIT_MIN >= minscore)
15196 {
15197 /* Near the limit, rest of the words must match. We
15198 * can check that right now, no need to push an item
15199 * onto the stack. */
15200 bi2 = bi + 1 - round;
15201 gi2 = gi + round;
15202 while (wgoodword[gi2] == wbadword[bi2])
15203 {
15204 if (wgoodword[gi2] == NUL)
15205 {
15206 minscore = score_off;
15207 break;
15208 }
15209 ++bi2;
15210 ++gi2;
15211 }
15212 }
15213 else
15214 {
15215 /* try deleting a character from badword later */
15216 stack[stackidx].badi = bi + 1 - round;
15217 stack[stackidx].goodi = gi + round;
15218 stack[stackidx].score = score_off;
15219 ++stackidx;
15220 }
15221 }
15222 }
15223
15224 if (score + SCORE_SWAP < minscore)
15225 {
15226 /* If swapping two characters makes a match then the
15227 * substitution is more expensive, thus there is no need to
15228 * try both. */
15229 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
15230 {
15231 /* Swap two characters, that is: skip them. */
15232 gi += 2;
15233 bi += 2;
15234 score += SCORE_SWAP;
15235 continue;
15236 }
15237 }
15238
15239 /* Substitute one character for another which is the same
15240 * thing as deleting a character from both goodword and badword.
15241 * Use a better score when there is only a case difference. */
15242 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15243 score += SCORE_ICASE;
15244 else
15245 {
15246 /* For a similar character use SCORE_SIMILAR. */
15247 if (slang != NULL
15248 && slang->sl_has_map
15249 && similar_chars(slang, gc, bc))
15250 score += SCORE_SIMILAR;
15251 else
15252 score += SCORE_SUBST;
15253 }
15254
15255 if (score < minscore)
15256 {
15257 /* Do the substitution. */
15258 ++gi;
15259 ++bi;
15260 continue;
15261 }
15262 }
15263pop:
15264 /*
15265 * Get here to try the next alternative, pop it from the stack.
15266 */
15267 if (stackidx == 0) /* stack is empty, finished */
15268 break;
15269
15270 /* pop an item from the stack */
15271 --stackidx;
15272 gi = stack[stackidx].goodi;
15273 bi = stack[stackidx].badi;
15274 score = stack[stackidx].score;
15275 }
15276
15277 /* When the score goes over "limit" it may actually be much higher.
15278 * Return a very large number to avoid going below the limit when giving a
15279 * bonus. */
15280 if (minscore > limit)
15281 return SCORE_MAXMAX;
15282 return minscore;
15283}
15284#endif
15285
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015286/*
15287 * ":spellinfo"
15288 */
15289/*ARGSUSED*/
15290 void
15291ex_spellinfo(eap)
15292 exarg_T *eap;
15293{
15294 int lpi;
15295 langp_T *lp;
15296 char_u *p;
15297
15298 if (no_spell_checking(curwin))
15299 return;
15300
15301 msg_start();
15302 for (lpi = 0; lpi < curbuf->b_langp.ga_len && !got_int; ++lpi)
15303 {
15304 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
15305 msg_puts((char_u *)"file: ");
15306 msg_puts(lp->lp_slang->sl_fname);
15307 msg_putchar('\n');
15308 p = lp->lp_slang->sl_info;
15309 if (p != NULL)
15310 {
15311 msg_puts(p);
15312 msg_putchar('\n');
15313 }
15314 }
15315 msg_end();
15316}
15317
Bram Moolenaar4770d092006-01-12 23:22:24 +000015318#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
15319#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015320#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +000015321#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
15322#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015323
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015324/*
15325 * ":spelldump"
15326 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015327 void
15328ex_spelldump(eap)
15329 exarg_T *eap;
15330{
15331 buf_T *buf = curbuf;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015332
15333 if (no_spell_checking(curwin))
15334 return;
15335
15336 /* Create a new empty buffer by splitting the window. */
15337 do_cmdline_cmd((char_u *)"new");
15338 if (!bufempty() || !buf_valid(buf))
15339 return;
15340
15341 spell_dump_compl(buf, NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
15342
15343 /* Delete the empty line that we started with. */
15344 if (curbuf->b_ml.ml_line_count > 1)
15345 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
15346
15347 redraw_later(NOT_VALID);
15348}
15349
15350/*
15351 * Go through all possible words and:
15352 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
15353 * "ic" and "dir" are not used.
15354 * 2. When "pat" is not NULL: add matching words to insert mode completion.
15355 */
15356 void
15357spell_dump_compl(buf, pat, ic, dir, dumpflags_arg)
15358 buf_T *buf; /* buffer with spell checking */
15359 char_u *pat; /* leading part of the word */
15360 int ic; /* ignore case */
15361 int *dir; /* direction for adding matches */
15362 int dumpflags_arg; /* DUMPFLAG_* */
15363{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015364 langp_T *lp;
15365 slang_T *slang;
15366 idx_T arridx[MAXWLEN];
15367 int curi[MAXWLEN];
15368 char_u word[MAXWLEN];
15369 int c;
15370 char_u *byts;
15371 idx_T *idxs;
15372 linenr_T lnum = 0;
15373 int round;
15374 int depth;
15375 int n;
15376 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015377 char_u *region_names = NULL; /* region names being used */
15378 int do_region = TRUE; /* dump region names and numbers */
15379 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015380 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015381 int dumpflags = dumpflags_arg;
15382 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015383
Bram Moolenaard0131a82006-03-04 21:46:13 +000015384 /* When ignoring case or when the pattern starts with capital pass this on
15385 * to dump_word(). */
15386 if (pat != NULL)
15387 {
15388 if (ic)
15389 dumpflags |= DUMPFLAG_ICASE;
15390 else
15391 {
15392 n = captype(pat, NULL);
15393 if (n == WF_ONECAP)
15394 dumpflags |= DUMPFLAG_ONECAP;
15395 else if (n == WF_ALLCAP
15396#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015397 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015398#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015399 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +000015400#endif
15401 )
15402 dumpflags |= DUMPFLAG_ALLCAP;
15403 }
15404 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015405
Bram Moolenaar7887d882005-07-01 22:33:52 +000015406 /* Find out if we can support regions: All languages must support the same
15407 * regions or none at all. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015408 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000015409 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015410 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000015411 p = lp->lp_slang->sl_regions;
15412 if (p[0] != 0)
15413 {
15414 if (region_names == NULL) /* first language with regions */
15415 region_names = p;
15416 else if (STRCMP(region_names, p) != 0)
15417 {
15418 do_region = FALSE; /* region names are different */
15419 break;
15420 }
15421 }
15422 }
15423
15424 if (do_region && region_names != NULL)
15425 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015426 if (pat == NULL)
15427 {
15428 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15429 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15430 }
Bram Moolenaar7887d882005-07-01 22:33:52 +000015431 }
15432 else
15433 do_region = FALSE;
15434
15435 /*
15436 * Loop over all files loaded for the entries in 'spelllang'.
15437 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015438 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015439 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015440 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015441 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015442 if (slang->sl_fbyts == NULL) /* reloading failed */
15443 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015444
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015445 if (pat == NULL)
15446 {
15447 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15448 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15449 }
15450
15451 /* When matching with a pattern and there are no prefixes only use
15452 * parts of the tree that match "pat". */
15453 if (pat != NULL && slang->sl_pbyts == NULL)
15454 patlen = STRLEN(pat);
15455 else
15456 patlen = 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015457
15458 /* round 1: case-folded tree
15459 * round 2: keep-case tree */
15460 for (round = 1; round <= 2; ++round)
15461 {
15462 if (round == 1)
15463 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015464 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015465 byts = slang->sl_fbyts;
15466 idxs = slang->sl_fidxs;
15467 }
15468 else
15469 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015470 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015471 byts = slang->sl_kbyts;
15472 idxs = slang->sl_kidxs;
15473 }
15474 if (byts == NULL)
15475 continue; /* array is empty */
15476
15477 depth = 0;
15478 arridx[0] = 0;
15479 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015480 while (depth >= 0 && !got_int
15481 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015482 {
15483 if (curi[depth] > byts[arridx[depth]])
15484 {
15485 /* Done all bytes at this node, go up one level. */
15486 --depth;
15487 line_breakcheck();
Bram Moolenaara2031822006-03-07 22:29:51 +000015488 ins_compl_check_keys(50);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015489 }
15490 else
15491 {
15492 /* Do one more byte at this node. */
15493 n = arridx[depth] + curi[depth];
15494 ++curi[depth];
15495 c = byts[n];
15496 if (c == 0)
15497 {
15498 /* End of word, deal with the word.
15499 * Don't use keep-case words in the fold-case tree,
15500 * they will appear in the keep-case tree.
15501 * Only use the word when the region matches. */
15502 flags = (int)idxs[n];
15503 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015504 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000015505 && (do_region
15506 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015507 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015508 & lp->lp_region) != 0))
15509 {
15510 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015511 if (!do_region)
15512 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015513
15514 /* Dump the basic word if there is no prefix or
15515 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015516 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015517 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015518 {
15519 dump_word(slang, word, pat, dir,
15520 dumpflags, flags, lnum);
15521 if (pat == NULL)
15522 ++lnum;
15523 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015524
15525 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015526 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015527 lnum = dump_prefixes(slang, word, pat, dir,
15528 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015529 }
15530 }
15531 else
15532 {
15533 /* Normal char, go one level deeper. */
15534 word[depth++] = c;
15535 arridx[depth] = idxs[n];
15536 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015537
15538 /* Check if this characters matches with the pattern.
15539 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +000015540 * Always ignore case here, dump_word() will check
15541 * proper case later. This isn't exactly right when
15542 * length changes for multi-byte characters with
15543 * ignore case... */
15544 if (depth <= patlen
15545 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015546 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015547 }
15548 }
15549 }
15550 }
15551 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015552}
15553
15554/*
15555 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015556 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015557 */
15558 static void
Bram Moolenaard0131a82006-03-04 21:46:13 +000015559dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015560 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015561 char_u *word;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015562 char_u *pat;
15563 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015564 int dumpflags;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015565 int wordflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015566 linenr_T lnum;
15567{
15568 int keepcap = FALSE;
15569 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015570 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015571 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000015572 char_u badword[MAXWLEN + 10];
15573 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015574 int flags = wordflags;
15575
15576 if (dumpflags & DUMPFLAG_ONECAP)
15577 flags |= WF_ONECAP;
15578 if (dumpflags & DUMPFLAG_ALLCAP)
15579 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015580
Bram Moolenaar4770d092006-01-12 23:22:24 +000015581 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015582 {
15583 /* Need to fix case according to "flags". */
15584 make_case_word(word, cword, flags);
15585 p = cword;
15586 }
15587 else
15588 {
15589 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015590 if ((dumpflags & DUMPFLAG_KEEPCASE)
15591 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015592 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015593 keepcap = TRUE;
15594 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015595 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015596
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015597 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015598 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015599 /* Add flags and regions after a slash. */
15600 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015601 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015602 STRCPY(badword, p);
15603 STRCAT(badword, "/");
15604 if (keepcap)
15605 STRCAT(badword, "=");
15606 if (flags & WF_BANNED)
15607 STRCAT(badword, "!");
15608 else if (flags & WF_RARE)
15609 STRCAT(badword, "?");
15610 if (flags & WF_REGION)
15611 for (i = 0; i < 7; ++i)
15612 if (flags & (0x10000 << i))
15613 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
15614 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015615 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015616
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015617 if (dumpflags & DUMPFLAG_COUNT)
15618 {
15619 hashitem_T *hi;
15620
15621 /* Include the word count for ":spelldump!". */
15622 hi = hash_find(&slang->sl_wordcount, tw);
15623 if (!HASHITEM_EMPTY(hi))
15624 {
15625 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15626 tw, HI2WC(hi)->wc_count);
15627 p = IObuff;
15628 }
15629 }
15630
15631 ml_append(lnum, p, (colnr_T)0, FALSE);
15632 }
Bram Moolenaard0131a82006-03-04 21:46:13 +000015633 else if (((dumpflags & DUMPFLAG_ICASE)
15634 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
15635 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015636 && ins_compl_add_infercase(p, (int)STRLEN(p),
15637 dumpflags & DUMPFLAG_ICASE,
15638 NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015639 /* if dir was BACKWARD then honor it just once */
15640 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015641}
15642
15643/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000015644 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15645 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015646 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015647 * Return the updated line number.
15648 */
15649 static linenr_T
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015650dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015651 slang_T *slang;
15652 char_u *word; /* case-folded word */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015653 char_u *pat;
15654 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015655 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015656 int flags; /* flags with prefix ID */
15657 linenr_T startlnum;
15658{
15659 idx_T arridx[MAXWLEN];
15660 int curi[MAXWLEN];
15661 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015662 char_u word_up[MAXWLEN];
15663 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015664 int c;
15665 char_u *byts;
15666 idx_T *idxs;
15667 linenr_T lnum = startlnum;
15668 int depth;
15669 int n;
15670 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015671 int i;
15672
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015673 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015674 * upper-case letter in word_up[]. */
15675 c = PTR2CHAR(word);
15676 if (SPELL_TOUPPER(c) != c)
15677 {
15678 onecap_copy(word, word_up, TRUE);
15679 has_word_up = TRUE;
15680 }
15681
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015682 byts = slang->sl_pbyts;
15683 idxs = slang->sl_pidxs;
15684 if (byts != NULL) /* array not is empty */
15685 {
15686 /*
15687 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015688 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015689 */
15690 depth = 0;
15691 arridx[0] = 0;
15692 curi[0] = 1;
15693 while (depth >= 0 && !got_int)
15694 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015695 n = arridx[depth];
15696 len = byts[n];
15697 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015698 {
15699 /* Done all bytes at this node, go up one level. */
15700 --depth;
15701 line_breakcheck();
15702 }
15703 else
15704 {
15705 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015706 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015707 ++curi[depth];
15708 c = byts[n];
15709 if (c == 0)
15710 {
15711 /* End of prefix, find out how many IDs there are. */
15712 for (i = 1; i < len; ++i)
15713 if (byts[n + i] != 0)
15714 break;
15715 curi[depth] += i - 1;
15716
Bram Moolenaar53805d12005-08-01 07:08:33 +000015717 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15718 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015719 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015720 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015721 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015722 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015723 : flags, lnum);
15724 if (lnum != 0)
15725 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015726 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000015727
15728 /* Check for prefix that matches the word when the
15729 * first letter is upper-case, but only if the prefix has
15730 * a condition. */
15731 if (has_word_up)
15732 {
15733 c = valid_word_prefix(i, n, flags, word_up, slang,
15734 TRUE);
15735 if (c != 0)
15736 {
15737 vim_strncpy(prefix + depth, word_up,
15738 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015739 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015740 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015741 : flags, lnum);
15742 if (lnum != 0)
15743 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +000015744 }
15745 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015746 }
15747 else
15748 {
15749 /* Normal char, go one level deeper. */
15750 prefix[depth++] = c;
15751 arridx[depth] = idxs[n];
15752 curi[depth] = 1;
15753 }
15754 }
15755 }
15756 }
15757
15758 return lnum;
15759}
15760
Bram Moolenaar95529562005-08-25 21:21:38 +000015761/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015762 * Move "p" to the end of word "start".
15763 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000015764 */
15765 char_u *
15766spell_to_word_end(start, buf)
15767 char_u *start;
15768 buf_T *buf;
15769{
15770 char_u *p = start;
15771
15772 while (*p != NUL && spell_iswordp(p, buf))
15773 mb_ptr_adv(p);
15774 return p;
15775}
15776
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015777#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015778/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015779 * For Insert mode completion CTRL-X s:
15780 * Find start of the word in front of column "startcol".
15781 * We don't check if it is badly spelled, with completion we can only change
15782 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015783 * Returns the column number of the word.
15784 */
15785 int
15786spell_word_start(startcol)
15787 int startcol;
15788{
15789 char_u *line;
15790 char_u *p;
15791 int col = 0;
15792
Bram Moolenaar95529562005-08-25 21:21:38 +000015793 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015794 return startcol;
15795
15796 /* Find a word character before "startcol". */
15797 line = ml_get_curline();
15798 for (p = line + startcol; p > line; )
15799 {
15800 mb_ptr_back(line, p);
15801 if (spell_iswordp_nmw(p))
15802 break;
15803 }
15804
15805 /* Go back to start of the word. */
15806 while (p > line)
15807 {
15808 col = p - line;
15809 mb_ptr_back(line, p);
15810 if (!spell_iswordp(p, curbuf))
15811 break;
15812 col = 0;
15813 }
15814
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015815 return col;
15816}
15817
15818/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000015819 * Need to check for 'spellcapcheck' now, the word is removed before
15820 * expand_spelling() is called. Therefore the ugly global variable.
15821 */
15822static int spell_expand_need_cap;
15823
15824 void
15825spell_expand_check_cap(col)
15826 colnr_T col;
15827{
15828 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
15829}
15830
15831/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015832 * Get list of spelling suggestions.
15833 * Used for Insert mode completion CTRL-X ?.
15834 * Returns the number of matches. The matches are in "matchp[]", array of
15835 * allocated strings.
15836 */
15837/*ARGSUSED*/
15838 int
15839expand_spelling(lnum, col, pat, matchp)
15840 linenr_T lnum;
15841 int col;
15842 char_u *pat;
15843 char_u ***matchp;
15844{
15845 garray_T ga;
15846
Bram Moolenaar4770d092006-01-12 23:22:24 +000015847 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015848 *matchp = ga.ga_data;
15849 return ga.ga_len;
15850}
15851#endif
15852
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000015853#endif /* FEAT_SPELL */