blob: fba3857b66c8959d91370c9ed890435f25d6bfca [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;
5962 int flag;
5963
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;
6477 continue;
6478 }
6479
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006480 /* This takes time, print a message every 10000 words. */
6481 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006482 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006483 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006484 vim_snprintf((char *)message, sizeof(message),
6485 _("line %6d, word %6d - %s"),
6486 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6487 msg_start();
6488 msg_puts_long_attr(message, 0);
6489 msg_clr_eos();
6490 msg_didout = FALSE;
6491 msg_col = 0;
6492 out_flush();
6493 }
6494
Bram Moolenaar51485f02005-06-04 21:55:20 +00006495 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006496 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006497 if (dw == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006498 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006499 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006500 if (retval == FAIL)
6501 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006502
Bram Moolenaar51485f02005-06-04 21:55:20 +00006503 hash = hash_hash(dw);
6504 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006505 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006506 {
6507 if (p_verbose > 0)
6508 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006509 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006510 else if (duplicate == 0)
6511 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6512 fname, lnum, dw);
6513 ++duplicate;
6514 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006515 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006516 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006517
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006518 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006519 store_afflist[0] = NUL;
6520 pfxlen = 0;
6521 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006522 if (afflist != NULL)
6523 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006524 /* Extract flags from the affix list. */
6525 flags |= get_affix_flags(affile, afflist);
6526
Bram Moolenaar6de68532005-08-24 22:08:48 +00006527 if (affile->af_needaffix != 0 && flag_in_afflist(
6528 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006529 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006530
6531 if (affile->af_pfxpostpone)
6532 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006533 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006534
Bram Moolenaar5195e452005-08-19 20:32:47 +00006535 if (spin->si_compflags != NULL)
6536 /* Need to store the list of compound flags with the word.
6537 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006538 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006539 }
6540
Bram Moolenaar51485f02005-06-04 21:55:20 +00006541 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006542 if (store_word(spin, dw, flags, spin->si_region,
6543 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006544 retval = FAIL;
6545
6546 if (afflist != NULL)
6547 {
6548 /* Find all matching suffixes and add the resulting words.
6549 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006550 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006551 &affile->af_suff, &affile->af_pref,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006552 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006553 retval = FAIL;
6554
6555 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006556 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006557 &affile->af_pref, NULL,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006558 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006559 retval = FAIL;
6560 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006561 }
6562
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006563 if (duplicate > 0)
6564 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006565 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006566 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6567 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006568 hash_clear(&ht);
6569
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006570 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006571 return retval;
6572}
6573
6574/*
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006575 * Check for affix flags in "afflist" that are turned into word flags.
6576 * Return WF_ flags.
6577 */
6578 static int
6579get_affix_flags(affile, afflist)
6580 afffile_T *affile;
6581 char_u *afflist;
6582{
6583 int flags = 0;
6584
6585 if (affile->af_keepcase != 0 && flag_in_afflist(
6586 affile->af_flagtype, afflist, affile->af_keepcase))
6587 flags |= WF_KEEPCAP | WF_FIXCAP;
6588 if (affile->af_rare != 0 && flag_in_afflist(
6589 affile->af_flagtype, afflist, affile->af_rare))
6590 flags |= WF_RARE;
6591 if (affile->af_bad != 0 && flag_in_afflist(
6592 affile->af_flagtype, afflist, affile->af_bad))
6593 flags |= WF_BANNED;
6594 if (affile->af_needcomp != 0 && flag_in_afflist(
6595 affile->af_flagtype, afflist, affile->af_needcomp))
6596 flags |= WF_NEEDCOMP;
6597 if (affile->af_comproot != 0 && flag_in_afflist(
6598 affile->af_flagtype, afflist, affile->af_comproot))
6599 flags |= WF_COMPROOT;
6600 if (affile->af_nosuggest != 0 && flag_in_afflist(
6601 affile->af_flagtype, afflist, affile->af_nosuggest))
6602 flags |= WF_NOSUGGEST;
6603 return flags;
6604}
6605
6606/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006607 * Get the list of prefix IDs from the affix list "afflist".
6608 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006609 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6610 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006611 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006612 static int
6613get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006614 afffile_T *affile;
6615 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006616 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006617{
6618 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006619 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006620 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006621 int id;
6622 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006623 hashitem_T *hi;
6624
Bram Moolenaar6de68532005-08-24 22:08:48 +00006625 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006626 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006627 prevp = p;
6628 if (get_affitem(affile->af_flagtype, &p) != 0)
6629 {
6630 /* A flag is a postponed prefix flag if it appears in "af_pref"
6631 * and it's ID is not zero. */
6632 vim_strncpy(key, prevp, p - prevp);
6633 hi = hash_find(&affile->af_pref, key);
6634 if (!HASHITEM_EMPTY(hi))
6635 {
6636 id = HI2AH(hi)->ah_newID;
6637 if (id != 0)
6638 store_afflist[cnt++] = id;
6639 }
6640 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006641 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006642 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006643 }
6644
Bram Moolenaar5195e452005-08-19 20:32:47 +00006645 store_afflist[cnt] = NUL;
6646 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006647}
6648
6649/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006650 * Get the list of compound IDs from the affix list "afflist" that are used
6651 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006652 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006653 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006654 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00006655get_compflags(affile, afflist, store_afflist)
6656 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006657 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006658 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006659{
6660 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006661 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006662 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006663 char_u key[AH_KEY_LEN];
6664 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006665
Bram Moolenaar6de68532005-08-24 22:08:48 +00006666 for (p = afflist; *p != NUL; )
6667 {
6668 prevp = p;
6669 if (get_affitem(affile->af_flagtype, &p) != 0)
6670 {
6671 /* A flag is a compound flag if it appears in "af_comp". */
6672 vim_strncpy(key, prevp, p - prevp);
6673 hi = hash_find(&affile->af_comp, key);
6674 if (!HASHITEM_EMPTY(hi))
6675 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6676 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006677 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006678 ++p;
6679 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006680
Bram Moolenaar5195e452005-08-19 20:32:47 +00006681 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006682}
6683
6684/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006685 * Apply affixes to a word and store the resulting words.
6686 * "ht" is the hashtable with affentry_T that need to be applied, either
6687 * prefixes or suffixes.
6688 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6689 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006690 *
6691 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006692 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006693 static int
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006694store_aff_word(spin, word, afflist, affile, ht, xht, condit, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006695 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006696 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006697 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006698 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006699 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006700 hashtab_T *ht;
6701 hashtab_T *xht;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006702 int condit; /* CONDIT_SUF et al. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006703 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006704 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006705 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
6706 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006707{
6708 int todo;
6709 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006710 affheader_T *ah;
6711 affentry_T *ae;
6712 regmatch_T regmatch;
6713 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006714 int retval = OK;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006715 int i, j;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006716 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006717 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006718 char_u *use_pfxlist;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006719 int use_pfxlen;
6720 int need_affix;
6721 char_u store_afflist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006722 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006723 size_t wordlen = STRLEN(word);
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006724 int use_condit;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006725
Bram Moolenaar51485f02005-06-04 21:55:20 +00006726 todo = ht->ht_used;
6727 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006728 {
6729 if (!HASHITEM_EMPTY(hi))
6730 {
6731 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006732 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006733
Bram Moolenaar51485f02005-06-04 21:55:20 +00006734 /* Check that the affix combines, if required, and that the word
6735 * supports this affix. */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006736 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
6737 && flag_in_afflist(affile->af_flagtype, afflist,
6738 ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006739 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006740 /* Loop over all affix entries with this name. */
6741 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006742 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006743 /* Check the condition. It's not logical to match case
6744 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006745 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006746 * Another requirement from Myspell is that the chop
6747 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006748 * For prefixes, when "PFXPOSTPONE" was used, only do
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006749 * prefixes with a chop string and/or flags.
6750 * When a previously added affix had CIRCUMFIX this one
6751 * must have it too, if it had not then this one must not
6752 * have one either. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006753 regmatch.regprog = ae->ae_prog;
6754 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006755 if ((xht != NULL || !affile->af_pfxpostpone
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006756 || ae->ae_chop != NULL
6757 || ae->ae_flags != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006758 && (ae->ae_chop == NULL
6759 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006760 && (ae->ae_prog == NULL
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006761 || vim_regexec(&regmatch, word, (colnr_T)0))
6762 && (((condit & CONDIT_CFIX) == 0)
6763 == ((condit & CONDIT_AFF) == 0
6764 || ae->ae_flags == NULL
6765 || !flag_in_afflist(affile->af_flagtype,
6766 ae->ae_flags, affile->af_circumfix))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006767 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006768 /* Match. Remove the chop and add the affix. */
6769 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006770 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006771 /* prefix: chop/add at the start of the word */
6772 if (ae->ae_add == NULL)
6773 *newword = NUL;
6774 else
6775 STRCPY(newword, ae->ae_add);
6776 p = word;
6777 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006778 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006779 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006780#ifdef FEAT_MBYTE
6781 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006782 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006783 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006784 for ( ; i > 0; --i)
6785 mb_ptr_adv(p);
6786 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006787 else
6788#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006789 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006790 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006791 STRCAT(newword, p);
6792 }
6793 else
6794 {
6795 /* suffix: chop/add at the end of the word */
6796 STRCPY(newword, word);
6797 if (ae->ae_chop != NULL)
6798 {
6799 /* Remove chop string. */
6800 p = newword + STRLEN(newword);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00006801 i = MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006802 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006803 mb_ptr_back(newword, p);
6804 *p = NUL;
6805 }
6806 if (ae->ae_add != NULL)
6807 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006808 }
6809
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006810 use_flags = flags;
6811 use_pfxlist = pfxlist;
6812 use_pfxlen = pfxlen;
6813 need_affix = FALSE;
6814 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
6815 if (ae->ae_flags != NULL)
6816 {
6817 /* Extract flags from the affix list. */
6818 use_flags |= get_affix_flags(affile, ae->ae_flags);
6819
6820 if (affile->af_needaffix != 0 && flag_in_afflist(
6821 affile->af_flagtype, ae->ae_flags,
6822 affile->af_needaffix))
6823 need_affix = TRUE;
6824
6825 /* When there is a CIRCUMFIX flag the other affix
6826 * must also have it and we don't add the word
6827 * with one affix. */
6828 if (affile->af_circumfix != 0 && flag_in_afflist(
6829 affile->af_flagtype, ae->ae_flags,
6830 affile->af_circumfix))
6831 {
6832 use_condit |= CONDIT_CFIX;
6833 if ((condit & CONDIT_CFIX) == 0)
6834 need_affix = TRUE;
6835 }
6836
6837 if (affile->af_pfxpostpone
6838 || spin->si_compflags != NULL)
6839 {
6840 if (affile->af_pfxpostpone)
6841 /* Get prefix IDS from the affix list. */
6842 use_pfxlen = get_pfxlist(affile,
6843 ae->ae_flags, store_afflist);
6844 else
6845 use_pfxlen = 0;
6846 use_pfxlist = store_afflist;
6847
6848 /* Combine the prefix IDs. Avoid adding the
6849 * same ID twice. */
6850 for (i = 0; i < pfxlen; ++i)
6851 {
6852 for (j = 0; j < use_pfxlen; ++j)
6853 if (pfxlist[i] == use_pfxlist[j])
6854 break;
6855 if (j == use_pfxlen)
6856 use_pfxlist[use_pfxlen++] = pfxlist[i];
6857 }
6858
6859 if (spin->si_compflags != NULL)
6860 /* Get compound IDS from the affix list. */
6861 get_compflags(affile, ae->ae_flags,
6862 use_pfxlist + use_pfxlen);
6863
6864 /* Combine the list of compound flags.
6865 * Concatenate them to the prefix IDs list.
6866 * Avoid adding the same ID twice. */
6867 for (i = pfxlen; pfxlist[i] != NUL; ++i)
6868 {
6869 for (j = use_pfxlen;
6870 use_pfxlist[j] != NUL; ++j)
6871 if (pfxlist[i] == use_pfxlist[j])
6872 break;
6873 if (use_pfxlist[j] == NUL)
6874 {
6875 use_pfxlist[j++] = pfxlist[i];
6876 use_pfxlist[j] = NUL;
6877 }
6878 }
6879 }
6880 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00006881
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006882 /* Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006883 * use the compound flags. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006884 if (use_pfxlist != NULL && ae->ae_compforbid)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006885 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006886 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006887 use_pfxlist = pfx_pfxlist;
6888 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006889
6890 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00006891 if (spin->si_prefroot != NULL
6892 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006893 {
6894 /* ... add a flag to indicate an affix was used. */
6895 use_flags |= WF_HAS_AFF;
6896
6897 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00006898 * affixes is not allowed. But do use the
6899 * compound flags after them. */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006900 if ((!ah->ah_combine || (condit & CONDIT_COMB))
6901 && use_pfxlist != NULL)
6902 use_pfxlist += use_pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006903 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006904
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006905 /* When compounding is supported and there is no
6906 * "COMPOUNDPERMITFLAG" then forbid compounding on the
6907 * side where the affix is applied. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006908 if (spin->si_compflags != NULL && !ae->ae_comppermit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006909 {
6910 if (xht != NULL)
6911 use_flags |= WF_NOCOMPAFT;
6912 else
6913 use_flags |= WF_NOCOMPBEF;
6914 }
6915
Bram Moolenaar51485f02005-06-04 21:55:20 +00006916 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006917 if (store_word(spin, newword, use_flags,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006918 spin->si_region, use_pfxlist,
6919 need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006920 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006921
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006922 /* When added a prefix or a first suffix and the affix
6923 * has flags may add a(nother) suffix. RECURSIVE! */
6924 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
6925 if (store_aff_word(spin, newword, ae->ae_flags,
6926 affile, &affile->af_suff, xht,
6927 use_condit & (xht == NULL
6928 ? ~0 : ~CONDIT_SUF),
Bram Moolenaar5195e452005-08-19 20:32:47 +00006929 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006930 retval = FAIL;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006931
6932 /* When added a suffix and combining is allowed also
6933 * try adding a prefix additionally. Both for the
6934 * word flags and for the affix flags. RECURSIVE! */
6935 if (xht != NULL && ah->ah_combine)
6936 {
6937 if (store_aff_word(spin, newword,
6938 afflist, affile,
6939 xht, NULL, use_condit,
6940 use_flags, use_pfxlist,
6941 pfxlen) == FAIL
6942 || (ae->ae_flags != NULL
6943 && store_aff_word(spin, newword,
6944 ae->ae_flags, affile,
6945 xht, NULL, use_condit,
6946 use_flags, use_pfxlist,
6947 pfxlen) == FAIL))
6948 retval = FAIL;
6949 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006950 }
6951 }
6952 }
6953 }
6954 }
6955
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006956 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006957}
6958
6959/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006960 * Read a file with a list of words.
6961 */
6962 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006963spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006964 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006965 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006966{
6967 FILE *fd;
6968 long lnum = 0;
6969 char_u rline[MAXLINELEN];
6970 char_u *line;
6971 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00006972 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006973 int l;
6974 int retval = OK;
6975 int did_word = FALSE;
6976 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006977 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00006978 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006979
6980 /*
6981 * Open the file.
6982 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006983 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00006984 if (fd == NULL)
6985 {
6986 EMSG2(_(e_notopen), fname);
6987 return FAIL;
6988 }
6989
Bram Moolenaar4770d092006-01-12 23:22:24 +00006990 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
6991 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006992
6993 /*
6994 * Read all the lines in the file one by one.
6995 */
6996 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
6997 {
6998 line_breakcheck();
6999 ++lnum;
7000
7001 /* Skip comment lines. */
7002 if (*rline == '#')
7003 continue;
7004
7005 /* Remove CR, LF and white space from the end. */
7006 l = STRLEN(rline);
7007 while (l > 0 && rline[l - 1] <= ' ')
7008 --l;
7009 if (l == 0)
7010 continue; /* empty or blank line */
7011 rline[l] = NUL;
7012
7013 /* Convert from "=encoding={encoding}" to 'encoding' when needed. */
7014 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007015#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00007016 if (spin->si_conv.vc_type != CONV_NONE)
7017 {
7018 pc = string_convert(&spin->si_conv, rline, NULL);
7019 if (pc == NULL)
7020 {
7021 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
7022 fname, lnum, rline);
7023 continue;
7024 }
7025 line = pc;
7026 }
7027 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00007028#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007029 {
7030 pc = NULL;
7031 line = rline;
7032 }
7033
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007034 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00007035 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007036 ++line;
7037 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007038 {
7039 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007040 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
7041 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007042 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007043 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
7044 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007045 else
7046 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007047#ifdef FEAT_MBYTE
7048 char_u *enc;
7049
Bram Moolenaar51485f02005-06-04 21:55:20 +00007050 /* Setup for conversion to 'encoding'. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007051 line += 10;
7052 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007053 if (enc != NULL && !spin->si_ascii
7054 && convert_setup(&spin->si_conv, enc,
7055 p_enc) == FAIL)
7056 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00007057 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007058 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007059 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007060#else
7061 smsg((char_u *)_("Conversion in %s not supported"), fname);
7062#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007063 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007064 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007065 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007066
Bram Moolenaar3982c542005-06-08 21:56:31 +00007067 if (STRNCMP(line, "regions=", 8) == 0)
7068 {
7069 if (spin->si_region_count > 1)
7070 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
7071 fname, lnum, line);
7072 else
7073 {
7074 line += 8;
7075 if (STRLEN(line) > 16)
7076 smsg((char_u *)_("Too many regions in %s line %d: %s"),
7077 fname, lnum, line);
7078 else
7079 {
7080 spin->si_region_count = STRLEN(line) / 2;
7081 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007082
7083 /* Adjust the mask for a word valid in all regions. */
7084 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007085 }
7086 }
7087 continue;
7088 }
7089
Bram Moolenaar7887d882005-07-01 22:33:52 +00007090 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
7091 fname, lnum, line - 1);
7092 continue;
7093 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007094
Bram Moolenaar7887d882005-07-01 22:33:52 +00007095 flags = 0;
7096 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007097
Bram Moolenaar7887d882005-07-01 22:33:52 +00007098 /* Check for flags and region after a slash. */
7099 p = vim_strchr(line, '/');
7100 if (p != NULL)
7101 {
7102 *p++ = NUL;
7103 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007104 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007105 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007106 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007107 else if (*p == '!') /* Bad, bad, wicked word. */
7108 flags |= WF_BANNED;
7109 else if (*p == '?') /* Rare word. */
7110 flags |= WF_RARE;
7111 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007112 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007113 if ((flags & WF_REGION) == 0) /* first one */
7114 regionmask = 0;
7115 flags |= WF_REGION;
7116
7117 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00007118 if (l > spin->si_region_count)
7119 {
7120 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00007121 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007122 break;
7123 }
7124 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007125 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007126 else
7127 {
7128 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
7129 fname, lnum, p);
7130 break;
7131 }
7132 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007133 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007134 }
7135
7136 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
7137 if (spin->si_ascii && has_non_ascii(line))
7138 {
7139 ++non_ascii;
7140 continue;
7141 }
7142
7143 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007144 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007145 {
7146 retval = FAIL;
7147 break;
7148 }
7149 did_word = TRUE;
7150 }
7151
7152 vim_free(pc);
7153 fclose(fd);
7154
Bram Moolenaar4770d092006-01-12 23:22:24 +00007155 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007156 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007157 vim_snprintf((char *)IObuff, IOSIZE,
7158 _("Ignored %d words with non-ASCII characters"), non_ascii);
7159 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007160 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007161
Bram Moolenaar51485f02005-06-04 21:55:20 +00007162 return retval;
7163}
7164
7165/*
7166 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007167 * This avoids calling free() for every little struct we use (and keeping
7168 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00007169 * The memory is cleared to all zeros.
7170 * Returns NULL when out of memory.
7171 */
7172 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007173getroom(spin, len, align)
7174 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007175 size_t len; /* length needed */
7176 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007177{
7178 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007179 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007180
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007181 if (align && bl != NULL)
7182 /* Round size up for alignment. On some systems structures need to be
7183 * aligned to the size of a pointer (e.g., SPARC). */
7184 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
7185 & ~(sizeof(char *) - 1);
7186
Bram Moolenaar51485f02005-06-04 21:55:20 +00007187 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
7188 {
7189 /* Allocate a block of memory. This is not freed until much later. */
7190 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
7191 if (bl == NULL)
7192 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007193 bl->sb_next = spin->si_blocks;
7194 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007195 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007196 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007197 }
7198
7199 p = bl->sb_data + bl->sb_used;
7200 bl->sb_used += len;
7201
7202 return p;
7203}
7204
7205/*
7206 * Make a copy of a string into memory allocated with getroom().
7207 */
7208 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007209getroom_save(spin, s)
7210 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007211 char_u *s;
7212{
7213 char_u *sc;
7214
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007215 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007216 if (sc != NULL)
7217 STRCPY(sc, s);
7218 return sc;
7219}
7220
7221
7222/*
7223 * Free the list of allocated sblock_T.
7224 */
7225 static void
7226free_blocks(bl)
7227 sblock_T *bl;
7228{
7229 sblock_T *next;
7230
7231 while (bl != NULL)
7232 {
7233 next = bl->sb_next;
7234 vim_free(bl);
7235 bl = next;
7236 }
7237}
7238
7239/*
7240 * Allocate the root of a word tree.
7241 */
7242 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007243wordtree_alloc(spin)
7244 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007245{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007246 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007247}
7248
7249/*
7250 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007251 * Always store it in the case-folded tree. For a keep-case word this is
7252 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
7253 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007254 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007255 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
7256 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007257 */
7258 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00007259store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007260 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007261 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007262 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007263 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007264 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007265 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007266{
7267 int len = STRLEN(word);
7268 int ct = captype(word, word + len);
7269 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007270 int res = OK;
7271 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007272
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007273 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007274 for (p = pfxlist; res == OK; ++p)
7275 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007276 if (!need_affix || (p != NULL && *p != NUL))
7277 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007278 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007279 if (p == NULL || *p == NUL)
7280 break;
7281 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007282 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007283
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007284 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00007285 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007286 for (p = pfxlist; res == OK; ++p)
7287 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007288 if (!need_affix || (p != NULL && *p != NUL))
7289 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007290 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007291 if (p == NULL || *p == NUL)
7292 break;
7293 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007294 ++spin->si_keepwcount;
7295 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007296 return res;
7297}
7298
7299/*
7300 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00007301 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007302 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007303 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007304 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007305 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007306tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007307 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007308 char_u *word;
7309 wordnode_T *root;
7310 int flags;
7311 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007312 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007313{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007314 wordnode_T *node = root;
7315 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007316 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007317 wordnode_T **prev = NULL;
7318 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007319
Bram Moolenaar51485f02005-06-04 21:55:20 +00007320 /* Add each byte of the word to the tree, including the NUL at the end. */
7321 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007322 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007323 /* When there is more than one reference to this node we need to make
7324 * a copy, so that we can modify it. Copy the whole list of siblings
7325 * (we don't optimize for a partly shared list of siblings). */
7326 if (node != NULL && node->wn_refs > 1)
7327 {
7328 --node->wn_refs;
7329 copyprev = prev;
7330 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
7331 {
7332 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007333 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007334 if (np == NULL)
7335 return FAIL;
7336 np->wn_child = copyp->wn_child;
7337 if (np->wn_child != NULL)
7338 ++np->wn_child->wn_refs; /* child gets extra ref */
7339 np->wn_byte = copyp->wn_byte;
7340 if (np->wn_byte == NUL)
7341 {
7342 np->wn_flags = copyp->wn_flags;
7343 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007344 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007345 }
7346
7347 /* Link the new node in the list, there will be one ref. */
7348 np->wn_refs = 1;
7349 *copyprev = np;
7350 copyprev = &np->wn_sibling;
7351
7352 /* Let "node" point to the head of the copied list. */
7353 if (copyp == node)
7354 node = np;
7355 }
7356 }
7357
Bram Moolenaar51485f02005-06-04 21:55:20 +00007358 /* Look for the sibling that has the same character. They are sorted
7359 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007360 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007361 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007362 while (node != NULL
7363 && (node->wn_byte < word[i]
7364 || (node->wn_byte == NUL
7365 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007366 ? node->wn_affixID < (unsigned)affixID
7367 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007368 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007369 && (spin->si_sugtree
7370 ? (node->wn_region & 0xffff) < region
7371 : node->wn_affixID
7372 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007373 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007374 prev = &node->wn_sibling;
7375 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007376 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007377 if (node == NULL
7378 || node->wn_byte != word[i]
7379 || (word[i] == NUL
7380 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007381 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007382 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007383 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007384 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007385 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007386 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007387 if (np == NULL)
7388 return FAIL;
7389 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007390
7391 /* If "node" is NULL this is a new child or the end of the sibling
7392 * list: ref count is one. Otherwise use ref count of sibling and
7393 * make ref count of sibling one (matters when inserting in front
7394 * of the list of siblings). */
7395 if (node == NULL)
7396 np->wn_refs = 1;
7397 else
7398 {
7399 np->wn_refs = node->wn_refs;
7400 node->wn_refs = 1;
7401 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007402 *prev = np;
7403 np->wn_sibling = node;
7404 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007405 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007406
Bram Moolenaar51485f02005-06-04 21:55:20 +00007407 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007408 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007409 node->wn_flags = flags;
7410 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007411 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007412 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00007413 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007414 prev = &node->wn_child;
7415 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007416 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007417#ifdef SPELL_PRINTTREE
7418 smsg("Added \"%s\"", word);
7419 spell_print_tree(root->wn_sibling);
7420#endif
7421
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007422 /* count nr of words added since last message */
7423 ++spin->si_msg_count;
7424
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007425 if (spin->si_compress_cnt > 1)
7426 {
7427 if (--spin->si_compress_cnt == 1)
7428 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007429 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007430 }
7431
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007432 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007433 * When we have allocated lots of memory we need to compress the word tree
7434 * to free up some room. But compression is slow, and we might actually
7435 * need that room, thus only compress in the following situations:
7436 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00007437 * "compress_start" blocks.
7438 * 2. When compressed before and used "compress_inc" blocks before
7439 * adding "compress_added" words (si_compress_cnt > 1).
7440 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007441 * (si_compress_cnt == 1) and the number of free nodes drops below the
7442 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007443 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007444#ifndef SPELL_PRINTTREE
7445 if (spin->si_compress_cnt == 1
7446 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00007447 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007448#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007449 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007450 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00007451 * when the freed up room has been used and another "compress_inc"
7452 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007453 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007454 spin->si_blocks_cnt -= compress_inc;
7455 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007456
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007457 if (spin->si_verbose)
7458 {
7459 msg_start();
7460 msg_puts((char_u *)_(msg_compressing));
7461 msg_clr_eos();
7462 msg_didout = FALSE;
7463 msg_col = 0;
7464 out_flush();
7465 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007466
7467 /* Compress both trees. Either they both have many nodes, which makes
7468 * compression useful, or one of them is small, which means
Bram Moolenaar4770d092006-01-12 23:22:24 +00007469 * compression goes fast. But when filling the souldfold word tree
7470 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007471 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007472 if (affixID >= 0)
7473 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007474 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007475
7476 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007477}
7478
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007479/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007480 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7481 * Sets "sps_flags".
7482 */
7483 int
7484spell_check_msm()
7485{
7486 char_u *p = p_msm;
7487 long start = 0;
7488 long inc = 0;
7489 long added = 0;
7490
7491 if (!VIM_ISDIGIT(*p))
7492 return FAIL;
7493 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7494 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7495 if (*p != ',')
7496 return FAIL;
7497 ++p;
7498 if (!VIM_ISDIGIT(*p))
7499 return FAIL;
7500 inc = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
7501 if (*p != ',')
7502 return FAIL;
7503 ++p;
7504 if (!VIM_ISDIGIT(*p))
7505 return FAIL;
7506 added = getdigits(&p) * 1024;
7507 if (*p != NUL)
7508 return FAIL;
7509
7510 if (start == 0 || inc == 0 || added == 0 || inc > start)
7511 return FAIL;
7512
7513 compress_start = start;
7514 compress_inc = inc;
7515 compress_added = added;
7516 return OK;
7517}
7518
7519
7520/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007521 * Get a wordnode_T, either from the list of previously freed nodes or
7522 * allocate a new one.
7523 */
7524 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007525get_wordnode(spin)
7526 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007527{
7528 wordnode_T *n;
7529
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007530 if (spin->si_first_free == NULL)
7531 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007532 else
7533 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007534 n = spin->si_first_free;
7535 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007536 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007537 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007538 }
7539#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007540 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007541#endif
7542 return n;
7543}
7544
7545/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007546 * Decrement the reference count on a node (which is the head of a list of
7547 * siblings). If the reference count becomes zero free the node and its
7548 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007549 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007550 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007551 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007552deref_wordnode(spin, node)
7553 spellinfo_T *spin;
7554 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007555{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007556 wordnode_T *np;
7557 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007558
7559 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007560 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007561 for (np = node; np != NULL; np = np->wn_sibling)
7562 {
7563 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007564 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007565 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007566 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007567 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007568 ++cnt; /* length field */
7569 }
7570 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007571}
7572
7573/*
7574 * Free a wordnode_T for re-use later.
7575 * Only the "wn_child" field becomes invalid.
7576 */
7577 static void
7578free_wordnode(spin, n)
7579 spellinfo_T *spin;
7580 wordnode_T *n;
7581{
7582 n->wn_child = spin->si_first_free;
7583 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007584 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007585}
7586
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007587/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007588 * Compress a tree: find tails that are identical and can be shared.
7589 */
7590 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007591wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007592 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007593 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007594{
7595 hashtab_T ht;
7596 int n;
7597 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007598 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007599
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007600 /* Skip the root itself, it's not actually used. The first sibling is the
7601 * start of the tree. */
7602 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007603 {
7604 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007605 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007606
7607#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007608 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007609#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007610 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007611 if (tot > 1000000)
7612 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007613 else if (tot == 0)
7614 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007615 else
7616 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007617 vim_snprintf((char *)IObuff, IOSIZE,
7618 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7619 n, tot, tot - n, perc);
7620 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007621 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007622#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007623 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007624#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007625 hash_clear(&ht);
7626 }
7627}
7628
7629/*
7630 * Compress a node, its siblings and its children, depth first.
7631 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007632 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007633 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007634node_compress(spin, node, ht, tot)
7635 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007636 wordnode_T *node;
7637 hashtab_T *ht;
7638 int *tot; /* total count of nodes before compressing,
7639 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007640{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007641 wordnode_T *np;
7642 wordnode_T *tp;
7643 wordnode_T *child;
7644 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007645 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007646 int len = 0;
7647 unsigned nr, n;
7648 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007649
Bram Moolenaar51485f02005-06-04 21:55:20 +00007650 /*
7651 * Go through the list of siblings. Compress each child and then try
7652 * finding an identical child to replace it.
7653 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007654 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007655 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007656 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007657 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007658 ++len;
7659 if ((child = np->wn_child) != NULL)
7660 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007661 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007662 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007663
7664 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007665 hash = hash_hash(child->wn_u1.hashkey);
7666 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007667 if (!HASHITEM_EMPTY(hi))
7668 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007669 /* There are children we encountered before with a hash value
7670 * identical to the current child. Now check if there is one
7671 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007672 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007673 if (node_equal(child, tp))
7674 {
7675 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007676 * current one. This means the current child and all
7677 * its siblings is unlinked from the tree. */
7678 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007679 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007680 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007681 break;
7682 }
7683 if (tp == NULL)
7684 {
7685 /* No other child with this hash value equals the child of
7686 * the node, add it to the linked list after the first
7687 * item. */
7688 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007689 child->wn_u2.next = tp->wn_u2.next;
7690 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007691 }
7692 }
7693 else
7694 /* No other child has this hash value, add it to the
7695 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007696 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007697 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007698 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007699 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007700
7701 /*
7702 * Make a hash key for the node and its siblings, so that we can quickly
7703 * find a lookalike node. This must be done after compressing the sibling
7704 * list, otherwise the hash key would become invalid by the compression.
7705 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007706 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007707 nr = 0;
7708 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007709 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007710 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007711 /* end node: use wn_flags, wn_region and wn_affixID */
7712 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007713 else
7714 /* byte node: use the byte value and the child pointer */
7715 n = np->wn_byte + ((long_u)np->wn_child << 8);
7716 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007717 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007718
7719 /* Avoid NUL bytes, it terminates the hash key. */
7720 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007721 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007722 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007723 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007724 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007725 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007726 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007727 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7728 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007729
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007730 /* Check for CTRL-C pressed now and then. */
7731 fast_breakcheck();
7732
Bram Moolenaar51485f02005-06-04 21:55:20 +00007733 return compressed;
7734}
7735
7736/*
7737 * Return TRUE when two nodes have identical siblings and children.
7738 */
7739 static int
7740node_equal(n1, n2)
7741 wordnode_T *n1;
7742 wordnode_T *n2;
7743{
7744 wordnode_T *p1;
7745 wordnode_T *p2;
7746
7747 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7748 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7749 if (p1->wn_byte != p2->wn_byte
7750 || (p1->wn_byte == NUL
7751 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007752 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007753 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007754 : (p1->wn_child != p2->wn_child)))
7755 break;
7756
7757 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007758}
7759
7760/*
7761 * Write a number to file "fd", MSB first, in "len" bytes.
7762 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007763 void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007764put_bytes(fd, nr, len)
7765 FILE *fd;
7766 long_u nr;
7767 int len;
7768{
7769 int i;
7770
7771 for (i = len - 1; i >= 0; --i)
7772 putc((int)(nr >> (i * 8)), fd);
7773}
7774
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007775#ifdef _MSC_VER
7776# if (_MSC_VER <= 1200)
7777/* This line is required for VC6 without the service pack. Also see the
7778 * matching #pragma below. */
7779/* # pragma optimize("", off) */
7780# endif
7781#endif
7782
Bram Moolenaar4770d092006-01-12 23:22:24 +00007783/*
7784 * Write spin->si_sugtime to file "fd".
7785 */
7786 static void
7787put_sugtime(spin, fd)
7788 spellinfo_T *spin;
7789 FILE *fd;
7790{
7791 int c;
7792 int i;
7793
7794 /* time_t can be up to 8 bytes in size, more than long_u, thus we
7795 * can't use put_bytes() here. */
7796 for (i = 7; i >= 0; --i)
7797 if (i + 1 > sizeof(time_t))
7798 /* ">>" doesn't work well when shifting more bits than avail */
7799 putc(0, fd);
7800 else
7801 {
7802 c = (unsigned)spin->si_sugtime >> (i * 8);
7803 putc(c, fd);
7804 }
7805}
7806
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007807#ifdef _MSC_VER
7808# if (_MSC_VER <= 1200)
7809/* # pragma optimize("", on) */
7810# endif
7811#endif
7812
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007813static int
7814#ifdef __BORLANDC__
7815_RTLENTRYF
7816#endif
7817rep_compare __ARGS((const void *s1, const void *s2));
7818
7819/*
7820 * Function given to qsort() to sort the REP items on "from" string.
7821 */
7822 static int
7823#ifdef __BORLANDC__
7824_RTLENTRYF
7825#endif
7826rep_compare(s1, s2)
7827 const void *s1;
7828 const void *s2;
7829{
7830 fromto_T *p1 = (fromto_T *)s1;
7831 fromto_T *p2 = (fromto_T *)s2;
7832
7833 return STRCMP(p1->ft_from, p2->ft_from);
7834}
7835
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007836/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007837 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007838 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007839 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007840 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007841write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007842 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007843 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007844{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007845 FILE *fd;
7846 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007847 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007848 wordnode_T *tree;
7849 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007850 int i;
7851 int l;
7852 garray_T *gap;
7853 fromto_T *ftp;
7854 char_u *p;
7855 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007856 int retval = OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007857
Bram Moolenaarb765d632005-06-07 21:00:02 +00007858 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007859 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007860 {
7861 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007862 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007863 }
7864
Bram Moolenaar5195e452005-08-19 20:32:47 +00007865 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007866 /* <fileID> */
7867 if (fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd) != 1)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007868 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007869 EMSG(_(e_write));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007870 retval = FAIL;
7871 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007872 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007873
Bram Moolenaar5195e452005-08-19 20:32:47 +00007874 /*
7875 * <SECTIONS>: <section> ... <sectionend>
7876 */
7877
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007878 /* SN_INFO: <infotext> */
7879 if (spin->si_info != NULL)
7880 {
7881 putc(SN_INFO, fd); /* <sectionID> */
7882 putc(0, fd); /* <sectionflags> */
7883
7884 i = STRLEN(spin->si_info);
7885 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
7886 fwrite(spin->si_info, (size_t)i, (size_t)1, fd); /* <infotext> */
7887 }
7888
Bram Moolenaar5195e452005-08-19 20:32:47 +00007889 /* SN_REGION: <regionname> ...
7890 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007891 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007892 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007893 putc(SN_REGION, fd); /* <sectionID> */
7894 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7895 l = spin->si_region_count * 2;
7896 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7897 fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
7898 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007899 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007900 }
7901 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00007902 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007903
Bram Moolenaar5195e452005-08-19 20:32:47 +00007904 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
7905 *
7906 * The table with character flags and the table for case folding.
7907 * This makes sure the same characters are recognized as word characters
7908 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007909 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007910 * 'encoding'.
7911 * Also skip this for an .add.spl file, the main spell file must contain
7912 * the table (avoids that it conflicts). File is shorter too.
7913 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007914 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007915 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007916 char_u folchars[128 * 8];
7917 int flags;
7918
Bram Moolenaard12a1322005-08-21 22:08:24 +00007919 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007920 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7921
7922 /* Form the <folchars> string first, we need to know its length. */
7923 l = 0;
7924 for (i = 128; i < 256; ++i)
7925 {
7926#ifdef FEAT_MBYTE
7927 if (has_mbyte)
7928 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
7929 else
7930#endif
7931 folchars[l++] = spelltab.st_fold[i];
7932 }
7933 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
7934
7935 fputc(128, fd); /* <charflagslen> */
7936 for (i = 128; i < 256; ++i)
7937 {
7938 flags = 0;
7939 if (spelltab.st_isw[i])
7940 flags |= CF_WORD;
7941 if (spelltab.st_isu[i])
7942 flags |= CF_UPPER;
7943 fputc(flags, fd); /* <charflags> */
7944 }
7945
7946 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
7947 fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00007948 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007949
Bram Moolenaar5195e452005-08-19 20:32:47 +00007950 /* SN_MIDWORD: <midword> */
7951 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007952 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007953 putc(SN_MIDWORD, fd); /* <sectionID> */
7954 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7955
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007956 i = STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007957 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007958 fwrite(spin->si_midword, (size_t)i, (size_t)1, fd); /* <midword> */
7959 }
7960
Bram Moolenaar5195e452005-08-19 20:32:47 +00007961 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
7962 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007963 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007964 putc(SN_PREFCOND, fd); /* <sectionID> */
7965 putc(SNF_REQUIRED, fd); /* <sectionflags> */
7966
7967 l = write_spell_prefcond(NULL, &spin->si_prefcond);
7968 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
7969
7970 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007971 }
7972
Bram Moolenaar5195e452005-08-19 20:32:47 +00007973 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00007974 * SN_SAL: <salflags> <salcount> <sal> ...
7975 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007976
Bram Moolenaar5195e452005-08-19 20:32:47 +00007977 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00007978 * round 2: SN_SAL section (unless SN_SOFO is used)
7979 * round 3: SN_REPSAL section */
7980 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007981 {
7982 if (round == 1)
7983 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007984 else if (round == 2)
7985 {
7986 /* Don't write SN_SAL when using a SN_SOFO section */
7987 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
7988 continue;
7989 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007990 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007991 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00007992 gap = &spin->si_repsal;
7993
7994 /* Don't write the section if there are no items. */
7995 if (gap->ga_len == 0)
7996 continue;
7997
7998 /* Sort the REP/REPSAL items. */
7999 if (round != 2)
8000 qsort(gap->ga_data, (size_t)gap->ga_len,
8001 sizeof(fromto_T), rep_compare);
8002
8003 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
8004 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008005
Bram Moolenaar5195e452005-08-19 20:32:47 +00008006 /* This is for making suggestions, section is not required. */
8007 putc(0, fd); /* <sectionflags> */
8008
8009 /* Compute the length of what follows. */
8010 l = 2; /* count <repcount> or <salcount> */
8011 for (i = 0; i < gap->ga_len; ++i)
8012 {
8013 ftp = &((fromto_T *)gap->ga_data)[i];
8014 l += 1 + STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
8015 l += 1 + STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
8016 }
8017 if (round == 2)
8018 ++l; /* count <salflags> */
8019 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8020
8021 if (round == 2)
8022 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008023 i = 0;
8024 if (spin->si_followup)
8025 i |= SAL_F0LLOWUP;
8026 if (spin->si_collapse)
8027 i |= SAL_COLLAPSE;
8028 if (spin->si_rem_accents)
8029 i |= SAL_REM_ACCENTS;
8030 putc(i, fd); /* <salflags> */
8031 }
8032
8033 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
8034 for (i = 0; i < gap->ga_len; ++i)
8035 {
8036 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
8037 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
8038 ftp = &((fromto_T *)gap->ga_data)[i];
8039 for (rr = 1; rr <= 2; ++rr)
8040 {
8041 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
8042 l = STRLEN(p);
8043 putc(l, fd);
8044 fwrite(p, l, (size_t)1, fd);
8045 }
8046 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008047
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008048 }
8049
Bram Moolenaar5195e452005-08-19 20:32:47 +00008050 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
8051 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008052 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8053 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008054 putc(SN_SOFO, fd); /* <sectionID> */
8055 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008056
8057 l = STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008058 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
8059 /* <sectionlen> */
8060
8061 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
8062 fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008063
8064 l = STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008065 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
8066 fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008067 }
8068
Bram Moolenaar4770d092006-01-12 23:22:24 +00008069 /* SN_WORDS: <word> ...
8070 * This is for making suggestions, section is not required. */
8071 if (spin->si_commonwords.ht_used > 0)
8072 {
8073 putc(SN_WORDS, fd); /* <sectionID> */
8074 putc(0, fd); /* <sectionflags> */
8075
8076 /* round 1: count the bytes
8077 * round 2: write the bytes */
8078 for (round = 1; round <= 2; ++round)
8079 {
8080 int todo;
8081 int len = 0;
8082 hashitem_T *hi;
8083
8084 todo = spin->si_commonwords.ht_used;
8085 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
8086 if (!HASHITEM_EMPTY(hi))
8087 {
8088 l = STRLEN(hi->hi_key) + 1;
8089 len += l;
8090 if (round == 2) /* <word> */
8091 fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
8092 --todo;
8093 }
8094 if (round == 1)
8095 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
8096 }
8097 }
8098
Bram Moolenaar5195e452005-08-19 20:32:47 +00008099 /* SN_MAP: <mapstr>
8100 * This is for making suggestions, section is not required. */
8101 if (spin->si_map.ga_len > 0)
8102 {
8103 putc(SN_MAP, fd); /* <sectionID> */
8104 putc(0, fd); /* <sectionflags> */
8105 l = spin->si_map.ga_len;
8106 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8107 fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
8108 /* <mapstr> */
8109 }
8110
Bram Moolenaar4770d092006-01-12 23:22:24 +00008111 /* SN_SUGFILE: <timestamp>
8112 * This is used to notify that a .sug file may be available and at the
8113 * same time allows for checking that a .sug file that is found matches
8114 * with this .spl file. That's because the word numbers must be exactly
8115 * right. */
8116 if (!spin->si_nosugfile
8117 && (spin->si_sal.ga_len > 0
8118 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
8119 {
8120 putc(SN_SUGFILE, fd); /* <sectionID> */
8121 putc(0, fd); /* <sectionflags> */
8122 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
8123
8124 /* Set si_sugtime and write it to the file. */
8125 spin->si_sugtime = time(NULL);
8126 put_sugtime(spin, fd); /* <timestamp> */
8127 }
8128
Bram Moolenaare1438bb2006-03-01 22:01:55 +00008129 /* SN_NOSPLITSUGS: nothing
8130 * This is used to notify that no suggestions with word splits are to be
8131 * made. */
8132 if (spin->si_nosplitsugs)
8133 {
8134 putc(SN_NOSPLITSUGS, fd); /* <sectionID> */
8135 putc(0, fd); /* <sectionflags> */
8136 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8137 }
8138
Bram Moolenaar5195e452005-08-19 20:32:47 +00008139 /* SN_COMPOUND: compound info.
8140 * We don't mark it required, when not supported all compound words will
8141 * be bad words. */
8142 if (spin->si_compflags != NULL)
8143 {
8144 putc(SN_COMPOUND, fd); /* <sectionID> */
8145 putc(0, fd); /* <sectionflags> */
8146
8147 l = STRLEN(spin->si_compflags);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008148 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8149 l += STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
8150 put_bytes(fd, (long_u)(l + 7), 4); /* <sectionlen> */
8151
Bram Moolenaar5195e452005-08-19 20:32:47 +00008152 putc(spin->si_compmax, fd); /* <compmax> */
8153 putc(spin->si_compminlen, fd); /* <compminlen> */
8154 putc(spin->si_compsylmax, fd); /* <compsylmax> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008155 putc(0, fd); /* for Vim 7.0b compatibility */
8156 putc(spin->si_compoptions, fd); /* <compoptions> */
8157 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
8158 /* <comppatcount> */
8159 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8160 {
8161 p = ((char_u **)(spin->si_comppat.ga_data))[i];
8162 putc(STRLEN(p), fd); /* <comppatlen> */
8163 fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);/* <comppattext> */
8164 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008165 /* <compflags> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008166 fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
8167 (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008168 }
8169
Bram Moolenaar78622822005-08-23 21:00:13 +00008170 /* SN_NOBREAK: NOBREAK flag */
8171 if (spin->si_nobreak)
8172 {
8173 putc(SN_NOBREAK, fd); /* <sectionID> */
8174 putc(0, fd); /* <sectionflags> */
8175
8176 /* It's empty, the precense of the section flags the feature. */
8177 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8178 }
8179
Bram Moolenaar5195e452005-08-19 20:32:47 +00008180 /* SN_SYLLABLE: syllable info.
8181 * We don't mark it required, when not supported syllables will not be
8182 * counted. */
8183 if (spin->si_syllable != NULL)
8184 {
8185 putc(SN_SYLLABLE, fd); /* <sectionID> */
8186 putc(0, fd); /* <sectionflags> */
8187
8188 l = STRLEN(spin->si_syllable);
8189 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8190 fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd); /* <syllable> */
8191 }
8192
8193 /* end of <SECTIONS> */
8194 putc(SN_END, fd); /* <sectionend> */
8195
Bram Moolenaar50cde822005-06-05 21:54:54 +00008196
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008197 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008198 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008199 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008200 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008201 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008202 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008203 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008204 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008205 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008206 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008207 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008208 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008209
Bram Moolenaar0c405862005-06-22 22:26:26 +00008210 /* Clear the index and wnode fields in the tree. */
8211 clear_node(tree);
8212
Bram Moolenaar51485f02005-06-04 21:55:20 +00008213 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00008214 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00008215 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008216 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008217
Bram Moolenaar51485f02005-06-04 21:55:20 +00008218 /* number of nodes in 4 bytes */
8219 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00008220 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008221
Bram Moolenaar51485f02005-06-04 21:55:20 +00008222 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008223 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008224 }
8225
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008226 /* Write another byte to check for errors. */
8227 if (putc(0, fd) == EOF)
8228 retval = FAIL;
8229
8230 if (fclose(fd) == EOF)
8231 retval = FAIL;
8232
8233 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008234}
8235
8236/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008237 * Clear the index and wnode fields of "node", it siblings and its
8238 * children. This is needed because they are a union with other items to save
8239 * space.
8240 */
8241 static void
8242clear_node(node)
8243 wordnode_T *node;
8244{
8245 wordnode_T *np;
8246
8247 if (node != NULL)
8248 for (np = node; np != NULL; np = np->wn_sibling)
8249 {
8250 np->wn_u1.index = 0;
8251 np->wn_u2.wnode = NULL;
8252
8253 if (np->wn_byte != NUL)
8254 clear_node(np->wn_child);
8255 }
8256}
8257
8258
8259/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008260 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008261 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00008262 * This first writes the list of possible bytes (siblings). Then for each
8263 * byte recursively write the children.
8264 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00008265 * NOTE: The code here must match the code in read_tree_node(), since
8266 * assumptions are made about the indexes (so that we don't have to write them
8267 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00008268 *
8269 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008270 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008271 static int
Bram Moolenaar0c405862005-06-22 22:26:26 +00008272put_node(fd, node, index, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008273 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008274 wordnode_T *node;
8275 int index;
8276 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008277 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008278{
Bram Moolenaar51485f02005-06-04 21:55:20 +00008279 int newindex = index;
8280 int siblingcount = 0;
8281 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008282 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008283
Bram Moolenaar51485f02005-06-04 21:55:20 +00008284 /* If "node" is zero the tree is empty. */
8285 if (node == NULL)
8286 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008287
Bram Moolenaar51485f02005-06-04 21:55:20 +00008288 /* Store the index where this node is written. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008289 node->wn_u1.index = index;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008290
8291 /* Count the number of siblings. */
8292 for (np = node; np != NULL; np = np->wn_sibling)
8293 ++siblingcount;
8294
8295 /* Write the sibling count. */
8296 if (fd != NULL)
8297 putc(siblingcount, fd); /* <siblingcount> */
8298
8299 /* Write each sibling byte and optionally extra info. */
8300 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008301 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008302 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008303 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008304 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008305 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008306 /* For a NUL byte (end of word) write the flags etc. */
8307 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008308 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008309 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008310 * associated condition nr (stored in wn_region). The
8311 * byte value is misused to store the "rare" and "not
8312 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008313 if (np->wn_flags == (short_u)PFX_FLAGS)
8314 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008315 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00008316 {
8317 putc(BY_FLAGS, fd); /* <byte> */
8318 putc(np->wn_flags, fd); /* <pflags> */
8319 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008320 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008321 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008322 }
8323 else
8324 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008325 /* For word trees we write the flag/region items. */
8326 flags = np->wn_flags;
8327 if (regionmask != 0 && np->wn_region != regionmask)
8328 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008329 if (np->wn_affixID != 0)
8330 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008331 if (flags == 0)
8332 {
8333 /* word without flags or region */
8334 putc(BY_NOFLAGS, fd); /* <byte> */
8335 }
8336 else
8337 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008338 if (np->wn_flags >= 0x100)
8339 {
8340 putc(BY_FLAGS2, fd); /* <byte> */
8341 putc(flags, fd); /* <flags> */
8342 putc((unsigned)flags >> 8, fd); /* <flags2> */
8343 }
8344 else
8345 {
8346 putc(BY_FLAGS, fd); /* <byte> */
8347 putc(flags, fd); /* <flags> */
8348 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008349 if (flags & WF_REGION)
8350 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008351 if (flags & WF_AFX)
8352 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008353 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008354 }
8355 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008356 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008357 else
8358 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008359 if (np->wn_child->wn_u1.index != 0
8360 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008361 {
8362 /* The child is written elsewhere, write the reference. */
8363 if (fd != NULL)
8364 {
8365 putc(BY_INDEX, fd); /* <byte> */
8366 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008367 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008368 }
8369 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008370 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008371 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008372 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008373
Bram Moolenaar51485f02005-06-04 21:55:20 +00008374 if (fd != NULL)
8375 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
8376 {
8377 EMSG(_(e_write));
8378 return 0;
8379 }
8380 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008381 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008382
8383 /* Space used in the array when reading: one for each sibling and one for
8384 * the count. */
8385 newindex += siblingcount + 1;
8386
8387 /* Recursively dump the children of each sibling. */
8388 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008389 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
8390 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008391 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008392
8393 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008394}
8395
8396
8397/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008398 * ":mkspell [-ascii] outfile infile ..."
8399 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008400 */
8401 void
8402ex_mkspell(eap)
8403 exarg_T *eap;
8404{
8405 int fcount;
8406 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008407 char_u *arg = eap->arg;
8408 int ascii = FALSE;
8409
8410 if (STRNCMP(arg, "-ascii", 6) == 0)
8411 {
8412 ascii = TRUE;
8413 arg = skipwhite(arg + 6);
8414 }
8415
8416 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
8417 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
8418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008419 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008420 FreeWild(fcount, fnames);
8421 }
8422}
8423
8424/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00008425 * Create the .sug file.
8426 * Uses the soundfold info in "spin".
8427 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
8428 */
8429 static void
8430spell_make_sugfile(spin, wfname)
8431 spellinfo_T *spin;
8432 char_u *wfname;
8433{
8434 char_u fname[MAXPATHL];
8435 int len;
8436 slang_T *slang;
8437 int free_slang = FALSE;
8438
8439 /*
8440 * Read back the .spl file that was written. This fills the required
8441 * info for soundfolding. This also uses less memory than the
8442 * pointer-linked version of the trie. And it avoids having two versions
8443 * of the code for the soundfolding stuff.
8444 * It might have been done already by spell_reload_one().
8445 */
8446 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
8447 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
8448 break;
8449 if (slang == NULL)
8450 {
8451 spell_message(spin, (char_u *)_("Reading back spell file..."));
8452 slang = spell_load_file(wfname, NULL, NULL, FALSE);
8453 if (slang == NULL)
8454 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008455 free_slang = TRUE;
8456 }
8457
8458 /*
8459 * Clear the info in "spin" that is used.
8460 */
8461 spin->si_blocks = NULL;
8462 spin->si_blocks_cnt = 0;
8463 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
8464 spin->si_free_count = 0;
8465 spin->si_first_free = NULL;
8466 spin->si_foldwcount = 0;
8467
8468 /*
8469 * Go through the trie of good words, soundfold each word and add it to
8470 * the soundfold trie.
8471 */
8472 spell_message(spin, (char_u *)_("Performing soundfolding..."));
8473 if (sug_filltree(spin, slang) == FAIL)
8474 goto theend;
8475
8476 /*
8477 * Create the table which links each soundfold word with a list of the
8478 * good words it may come from. Creates buffer "spin->si_spellbuf".
8479 * This also removes the wordnr from the NUL byte entries to make
8480 * compression possible.
8481 */
8482 if (sug_maketable(spin) == FAIL)
8483 goto theend;
8484
8485 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8486 (long)spin->si_spellbuf->b_ml.ml_line_count);
8487
8488 /*
8489 * Compress the soundfold trie.
8490 */
8491 spell_message(spin, (char_u *)_(msg_compressing));
8492 wordtree_compress(spin, spin->si_foldroot);
8493
8494 /*
8495 * Write the .sug file.
8496 * Make the file name by changing ".spl" to ".sug".
8497 */
8498 STRCPY(fname, wfname);
8499 len = STRLEN(fname);
8500 fname[len - 2] = 'u';
8501 fname[len - 1] = 'g';
8502 sug_write(spin, fname);
8503
8504theend:
8505 if (free_slang)
8506 slang_free(slang);
8507 free_blocks(spin->si_blocks);
8508 close_spellbuf(spin->si_spellbuf);
8509}
8510
8511/*
8512 * Build the soundfold trie for language "slang".
8513 */
8514 static int
8515sug_filltree(spin, slang)
8516 spellinfo_T *spin;
8517 slang_T *slang;
8518{
8519 char_u *byts;
8520 idx_T *idxs;
8521 int depth;
8522 idx_T arridx[MAXWLEN];
8523 int curi[MAXWLEN];
8524 char_u tword[MAXWLEN];
8525 char_u tsalword[MAXWLEN];
8526 int c;
8527 idx_T n;
8528 unsigned words_done = 0;
8529 int wordcount[MAXWLEN];
8530
8531 /* We use si_foldroot for the souldfolded trie. */
8532 spin->si_foldroot = wordtree_alloc(spin);
8533 if (spin->si_foldroot == NULL)
8534 return FAIL;
8535
8536 /* let tree_add_word() know we're adding to the soundfolded tree */
8537 spin->si_sugtree = TRUE;
8538
8539 /*
8540 * Go through the whole case-folded tree, soundfold each word and put it
8541 * in the trie.
8542 */
8543 byts = slang->sl_fbyts;
8544 idxs = slang->sl_fidxs;
8545
8546 arridx[0] = 0;
8547 curi[0] = 1;
8548 wordcount[0] = 0;
8549
8550 depth = 0;
8551 while (depth >= 0 && !got_int)
8552 {
8553 if (curi[depth] > byts[arridx[depth]])
8554 {
8555 /* Done all bytes at this node, go up one level. */
8556 idxs[arridx[depth]] = wordcount[depth];
8557 if (depth > 0)
8558 wordcount[depth - 1] += wordcount[depth];
8559
8560 --depth;
8561 line_breakcheck();
8562 }
8563 else
8564 {
8565
8566 /* Do one more byte at this node. */
8567 n = arridx[depth] + curi[depth];
8568 ++curi[depth];
8569
8570 c = byts[n];
8571 if (c == 0)
8572 {
8573 /* Sound-fold the word. */
8574 tword[depth] = NUL;
8575 spell_soundfold(slang, tword, TRUE, tsalword);
8576
8577 /* We use the "flags" field for the MSB of the wordnr,
8578 * "region" for the LSB of the wordnr. */
8579 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8580 words_done >> 16, words_done & 0xffff,
8581 0) == FAIL)
8582 return FAIL;
8583
8584 ++words_done;
8585 ++wordcount[depth];
8586
8587 /* Reset the block count each time to avoid compression
8588 * kicking in. */
8589 spin->si_blocks_cnt = 0;
8590
8591 /* Skip over any other NUL bytes (same word with different
8592 * flags). */
8593 while (byts[n + 1] == 0)
8594 {
8595 ++n;
8596 ++curi[depth];
8597 }
8598 }
8599 else
8600 {
8601 /* Normal char, go one level deeper. */
8602 tword[depth++] = c;
8603 arridx[depth] = idxs[n];
8604 curi[depth] = 1;
8605 wordcount[depth] = 0;
8606 }
8607 }
8608 }
8609
8610 smsg((char_u *)_("Total number of words: %d"), words_done);
8611
8612 return OK;
8613}
8614
8615/*
8616 * Make the table that links each word in the soundfold trie to the words it
8617 * can be produced from.
8618 * This is not unlike lines in a file, thus use a memfile to be able to access
8619 * the table efficiently.
8620 * Returns FAIL when out of memory.
8621 */
8622 static int
8623sug_maketable(spin)
8624 spellinfo_T *spin;
8625{
8626 garray_T ga;
8627 int res = OK;
8628
8629 /* Allocate a buffer, open a memline for it and create the swap file
8630 * (uses a temp file, not a .swp file). */
8631 spin->si_spellbuf = open_spellbuf();
8632 if (spin->si_spellbuf == NULL)
8633 return FAIL;
8634
8635 /* Use a buffer to store the line info, avoids allocating many small
8636 * pieces of memory. */
8637 ga_init2(&ga, 1, 100);
8638
8639 /* recursively go through the tree */
8640 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8641 res = FAIL;
8642
8643 ga_clear(&ga);
8644 return res;
8645}
8646
8647/*
8648 * Fill the table for one node and its children.
8649 * Returns the wordnr at the start of the node.
8650 * Returns -1 when out of memory.
8651 */
8652 static int
8653sug_filltable(spin, node, startwordnr, gap)
8654 spellinfo_T *spin;
8655 wordnode_T *node;
8656 int startwordnr;
8657 garray_T *gap; /* place to store line of numbers */
8658{
8659 wordnode_T *p, *np;
8660 int wordnr = startwordnr;
8661 int nr;
8662 int prev_nr;
8663
8664 for (p = node; p != NULL; p = p->wn_sibling)
8665 {
8666 if (p->wn_byte == NUL)
8667 {
8668 gap->ga_len = 0;
8669 prev_nr = 0;
8670 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8671 {
8672 if (ga_grow(gap, 10) == FAIL)
8673 return -1;
8674
8675 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8676 /* Compute the offset from the previous nr and store the
8677 * offset in a way that it takes a minimum number of bytes.
8678 * It's a bit like utf-8, but without the need to mark
8679 * following bytes. */
8680 nr -= prev_nr;
8681 prev_nr += nr;
8682 gap->ga_len += offset2bytes(nr,
8683 (char_u *)gap->ga_data + gap->ga_len);
8684 }
8685
8686 /* add the NUL byte */
8687 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8688
8689 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8690 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8691 return -1;
8692 ++wordnr;
8693
8694 /* Remove extra NUL entries, we no longer need them. We don't
8695 * bother freeing the nodes, the won't be reused anyway. */
8696 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8697 p->wn_sibling = p->wn_sibling->wn_sibling;
8698
8699 /* Clear the flags on the remaining NUL node, so that compression
8700 * works a lot better. */
8701 p->wn_flags = 0;
8702 p->wn_region = 0;
8703 }
8704 else
8705 {
8706 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8707 if (wordnr == -1)
8708 return -1;
8709 }
8710 }
8711 return wordnr;
8712}
8713
8714/*
8715 * Convert an offset into a minimal number of bytes.
8716 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8717 * bytes.
8718 */
8719 static int
8720offset2bytes(nr, buf)
8721 int nr;
8722 char_u *buf;
8723{
8724 int rem;
8725 int b1, b2, b3, b4;
8726
8727 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8728 b1 = nr % 255 + 1;
8729 rem = nr / 255;
8730 b2 = rem % 255 + 1;
8731 rem = rem / 255;
8732 b3 = rem % 255 + 1;
8733 b4 = rem / 255 + 1;
8734
8735 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8736 {
8737 buf[0] = 0xe0 + b4;
8738 buf[1] = b3;
8739 buf[2] = b2;
8740 buf[3] = b1;
8741 return 4;
8742 }
8743 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8744 {
8745 buf[0] = 0xc0 + b3;
8746 buf[1] = b2;
8747 buf[2] = b1;
8748 return 3;
8749 }
8750 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8751 {
8752 buf[0] = 0x80 + b2;
8753 buf[1] = b1;
8754 return 2;
8755 }
8756 /* 1 byte */
8757 buf[0] = b1;
8758 return 1;
8759}
8760
8761/*
8762 * Opposite of offset2bytes().
8763 * "pp" points to the bytes and is advanced over it.
8764 * Returns the offset.
8765 */
8766 static int
8767bytes2offset(pp)
8768 char_u **pp;
8769{
8770 char_u *p = *pp;
8771 int nr;
8772 int c;
8773
8774 c = *p++;
8775 if ((c & 0x80) == 0x00) /* 1 byte */
8776 {
8777 nr = c - 1;
8778 }
8779 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8780 {
8781 nr = (c & 0x3f) - 1;
8782 nr = nr * 255 + (*p++ - 1);
8783 }
8784 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8785 {
8786 nr = (c & 0x1f) - 1;
8787 nr = nr * 255 + (*p++ - 1);
8788 nr = nr * 255 + (*p++ - 1);
8789 }
8790 else /* 4 bytes */
8791 {
8792 nr = (c & 0x0f) - 1;
8793 nr = nr * 255 + (*p++ - 1);
8794 nr = nr * 255 + (*p++ - 1);
8795 nr = nr * 255 + (*p++ - 1);
8796 }
8797
8798 *pp = p;
8799 return nr;
8800}
8801
8802/*
8803 * Write the .sug file in "fname".
8804 */
8805 static void
8806sug_write(spin, fname)
8807 spellinfo_T *spin;
8808 char_u *fname;
8809{
8810 FILE *fd;
8811 wordnode_T *tree;
8812 int nodecount;
8813 int wcount;
8814 char_u *line;
8815 linenr_T lnum;
8816 int len;
8817
8818 /* Create the file. Note that an existing file is silently overwritten! */
8819 fd = mch_fopen((char *)fname, "w");
8820 if (fd == NULL)
8821 {
8822 EMSG2(_(e_notopen), fname);
8823 return;
8824 }
8825
8826 vim_snprintf((char *)IObuff, IOSIZE,
8827 _("Writing suggestion file %s ..."), fname);
8828 spell_message(spin, IObuff);
8829
8830 /*
8831 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8832 */
8833 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
8834 {
8835 EMSG(_(e_write));
8836 goto theend;
8837 }
8838 putc(VIMSUGVERSION, fd); /* <versionnr> */
8839
8840 /* Write si_sugtime to the file. */
8841 put_sugtime(spin, fd); /* <timestamp> */
8842
8843 /*
8844 * <SUGWORDTREE>
8845 */
8846 spin->si_memtot = 0;
8847 tree = spin->si_foldroot->wn_sibling;
8848
8849 /* Clear the index and wnode fields in the tree. */
8850 clear_node(tree);
8851
8852 /* Count the number of nodes. Needed to be able to allocate the
8853 * memory when reading the nodes. Also fills in index for shared
8854 * nodes. */
8855 nodecount = put_node(NULL, tree, 0, 0, FALSE);
8856
8857 /* number of nodes in 4 bytes */
8858 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
8859 spin->si_memtot += nodecount + nodecount * sizeof(int);
8860
8861 /* Write the nodes. */
8862 (void)put_node(fd, tree, 0, 0, FALSE);
8863
8864 /*
8865 * <SUGTABLE>: <sugwcount> <sugline> ...
8866 */
8867 wcount = spin->si_spellbuf->b_ml.ml_line_count;
8868 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
8869
8870 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
8871 {
8872 /* <sugline>: <sugnr> ... NUL */
8873 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
8874 len = STRLEN(line) + 1;
8875 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
8876 {
8877 EMSG(_(e_write));
8878 goto theend;
8879 }
8880 spin->si_memtot += len;
8881 }
8882
8883 /* Write another byte to check for errors. */
8884 if (putc(0, fd) == EOF)
8885 EMSG(_(e_write));
8886
8887 vim_snprintf((char *)IObuff, IOSIZE,
8888 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
8889 spell_message(spin, IObuff);
8890
8891theend:
8892 /* close the file */
8893 fclose(fd);
8894}
8895
8896/*
8897 * Open a spell buffer. This is a nameless buffer that is not in the buffer
8898 * list and only contains text lines. Can use a swapfile to reduce memory
8899 * use.
8900 * Most other fields are invalid! Esp. watch out for string options being
8901 * NULL and there is no undo info.
8902 * Returns NULL when out of memory.
8903 */
8904 static buf_T *
8905open_spellbuf()
8906{
8907 buf_T *buf;
8908
8909 buf = (buf_T *)alloc_clear(sizeof(buf_T));
8910 if (buf != NULL)
8911 {
8912 buf->b_spell = TRUE;
8913 buf->b_p_swf = TRUE; /* may create a swap file */
8914 ml_open(buf);
8915 ml_open_file(buf); /* create swap file now */
8916 }
8917 return buf;
8918}
8919
8920/*
8921 * Close the buffer used for spell info.
8922 */
8923 static void
8924close_spellbuf(buf)
8925 buf_T *buf;
8926{
8927 if (buf != NULL)
8928 {
8929 ml_close(buf, TRUE);
8930 vim_free(buf);
8931 }
8932}
8933
8934
8935/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008936 * Create a Vim spell file from one or more word lists.
8937 * "fnames[0]" is the output file name.
8938 * "fnames[fcount - 1]" is the last input file name.
8939 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
8940 * and ".spl" is appended to make the output file name.
8941 */
8942 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008943mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00008944 int fcount;
8945 char_u **fnames;
8946 int ascii; /* -ascii argument given */
8947 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008948 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00008949{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008950 char_u fname[MAXPATHL];
8951 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00008952 char_u **innames;
8953 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008954 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008955 int i;
8956 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008957 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008958 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008959 spellinfo_T spin;
8960
8961 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008962 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008963 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008964 spin.si_followup = TRUE;
8965 spin.si_rem_accents = TRUE;
8966 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008967 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008968 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
8969 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008970 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008971 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008972 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008973 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008974
Bram Moolenaarb765d632005-06-07 21:00:02 +00008975 /* default: fnames[0] is output file, following are input files */
8976 innames = &fnames[1];
8977 incount = fcount - 1;
8978
8979 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00008980 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00008981 len = STRLEN(fnames[0]);
8982 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
8983 {
8984 /* For ":mkspell path/en.latin1.add" output file is
8985 * "path/en.latin1.add.spl". */
8986 innames = &fnames[0];
8987 incount = 1;
8988 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
8989 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008990 else if (fcount == 1)
8991 {
8992 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
8993 innames = &fnames[0];
8994 incount = 1;
8995 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
8996 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
8997 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00008998 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
8999 {
9000 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009001 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009002 }
9003 else
9004 /* Name should be language, make the file name from it. */
9005 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
9006 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
9007
9008 /* Check for .ascii.spl. */
9009 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
9010 spin.si_ascii = TRUE;
9011
9012 /* Check for .add.spl. */
9013 if (strstr((char *)gettail(wfname), ".add.") != NULL)
9014 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00009015 }
9016
Bram Moolenaarb765d632005-06-07 21:00:02 +00009017 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009018 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009019 else if (vim_strchr(gettail(wfname), '_') != NULL)
9020 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009021 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009022 EMSG(_("E754: Only up to 8 regions supported"));
9023 else
9024 {
9025 /* Check for overwriting before doing things that may take a lot of
9026 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009027 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009028 {
9029 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009030 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009031 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009032 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009033 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009034 EMSG2(_(e_isadir2), wfname);
9035 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009036 }
9037
9038 /*
9039 * Init the aff and dic pointers.
9040 * Get the region names if there are more than 2 arguments.
9041 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009042 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009043 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009044 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009045
Bram Moolenaar3982c542005-06-08 21:56:31 +00009046 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009047 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009048 len = STRLEN(innames[i]);
9049 if (STRLEN(gettail(innames[i])) < 5
9050 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009051 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009052 EMSG2(_("E755: Invalid region in %s"), innames[i]);
9053 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009054 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009055 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
9056 spin.si_region_name[i * 2 + 1] =
9057 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009058 }
9059 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009060 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009061
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009062 spin.si_foldroot = wordtree_alloc(&spin);
9063 spin.si_keeproot = wordtree_alloc(&spin);
9064 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009065 if (spin.si_foldroot == NULL
9066 || spin.si_keeproot == NULL
9067 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009068 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00009069 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009070 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009071 }
9072
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009073 /* When not producing a .add.spl file clear the character table when
9074 * we encounter one in the .aff file. This means we dump the current
9075 * one in the .spl file if the .aff file doesn't define one. That's
9076 * better than guessing the contents, the table will match a
9077 * previously loaded spell file. */
9078 if (!spin.si_add)
9079 spin.si_clear_chartab = TRUE;
9080
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009081 /*
9082 * Read all the .aff and .dic files.
9083 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00009084 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009085 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009086 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009087 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009088 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009089 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009090
Bram Moolenaarb765d632005-06-07 21:00:02 +00009091 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009092 if (mch_stat((char *)fname, &st) >= 0)
9093 {
9094 /* Read the .aff file. Will init "spin->si_conv" based on the
9095 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009096 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009097 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009098 error = TRUE;
9099 else
9100 {
9101 /* Read the .dic file and store the words in the trees. */
9102 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00009103 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009104 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009105 error = TRUE;
9106 }
9107 }
9108 else
9109 {
9110 /* No .aff file, try reading the file as a word list. Store
9111 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009112 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009113 error = TRUE;
9114 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009115
Bram Moolenaarb765d632005-06-07 21:00:02 +00009116#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009117 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00009118 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009119#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009120 }
9121
Bram Moolenaar78622822005-08-23 21:00:13 +00009122 if (spin.si_compflags != NULL && spin.si_nobreak)
9123 MSG(_("Warning: both compounding and NOBREAK specified"));
9124
Bram Moolenaar4770d092006-01-12 23:22:24 +00009125 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009126 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009127 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00009128 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009129 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009130 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009131 wordtree_compress(&spin, spin.si_foldroot);
9132 wordtree_compress(&spin, spin.si_keeproot);
9133 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009134 }
9135
Bram Moolenaar4770d092006-01-12 23:22:24 +00009136 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009137 {
9138 /*
9139 * Write the info in the spell file.
9140 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009141 vim_snprintf((char *)IObuff, IOSIZE,
9142 _("Writing spell file %s ..."), wfname);
9143 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00009144
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009145 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009146
Bram Moolenaar4770d092006-01-12 23:22:24 +00009147 spell_message(&spin, (char_u *)_("Done!"));
9148 vim_snprintf((char *)IObuff, IOSIZE,
9149 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
9150 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009151
Bram Moolenaar4770d092006-01-12 23:22:24 +00009152 /*
9153 * If the file is loaded need to reload it.
9154 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009155 if (!error)
9156 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009157 }
9158
9159 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009160 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009161 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009162 ga_clear(&spin.si_sal);
9163 ga_clear(&spin.si_map);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009164 ga_clear(&spin.si_comppat);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009165 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009166 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009167
9168 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009169 for (i = 0; i < incount; ++i)
9170 if (afile[i] != NULL)
9171 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009172
9173 /* Free all the bits and pieces at once. */
9174 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009175
9176 /*
9177 * If there is soundfolding info and no NOSUGFILE item create the
9178 * .sug file with the soundfolded word trie.
9179 */
9180 if (spin.si_sugtime != 0 && !error && !got_int)
9181 spell_make_sugfile(&spin, wfname);
9182
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009183 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009184}
9185
Bram Moolenaar4770d092006-01-12 23:22:24 +00009186/*
9187 * Display a message for spell file processing when 'verbose' is set or using
9188 * ":mkspell". "str" can be IObuff.
9189 */
9190 static void
9191spell_message(spin, str)
9192 spellinfo_T *spin;
9193 char_u *str;
9194{
9195 if (spin->si_verbose || p_verbose > 2)
9196 {
9197 if (!spin->si_verbose)
9198 verbose_enter();
9199 MSG(str);
9200 out_flush();
9201 if (!spin->si_verbose)
9202 verbose_leave();
9203 }
9204}
Bram Moolenaarb765d632005-06-07 21:00:02 +00009205
9206/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009207 * ":[count]spellgood {word}"
9208 * ":[count]spellwrong {word}"
Bram Moolenaard0131a82006-03-04 21:46:13 +00009209 * ":[count]spellundo {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00009210 */
9211 void
9212ex_spell(eap)
9213 exarg_T *eap;
9214{
Bram Moolenaar7887d882005-07-01 22:33:52 +00009215 spell_add_word(eap->arg, STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaard0131a82006-03-04 21:46:13 +00009216 eap->forceit ? 0 : (int)eap->line2,
9217 eap->cmdidx == CMD_spellundo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009218}
9219
9220/*
9221 * Add "word[len]" to 'spellfile' as a good or bad word.
9222 */
9223 void
Bram Moolenaard0131a82006-03-04 21:46:13 +00009224spell_add_word(word, len, bad, index, undo)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009225 char_u *word;
9226 int len;
9227 int bad;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009228 int index; /* "zG" and "zW": zero, otherwise index in
9229 'spellfile' */
Bram Moolenaard0131a82006-03-04 21:46:13 +00009230 int undo; /* TRUE for "zug", "zuG", "zuw" and "zuW" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009231{
9232 FILE *fd;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009233 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009234 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009235 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009236 char_u fnamebuf[MAXPATHL];
9237 char_u line[MAXWLEN * 2];
9238 long fpos, fpos_next = 0;
9239 int i;
9240 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009241
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009242 if (index == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009243 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009244 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009245 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009246 int_wordlist = vim_tempname('s');
9247 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009248 return;
9249 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009250 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009251 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009252 else
9253 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009254 /* If 'spellfile' isn't set figure out a good default value. */
9255 if (*curbuf->b_p_spf == NUL)
9256 {
9257 init_spellfile();
9258 new_spf = TRUE;
9259 }
9260
9261 if (*curbuf->b_p_spf == NUL)
9262 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00009263 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009264 return;
9265 }
9266
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009267 for (spf = curbuf->b_p_spf, i = 1; *spf != NUL; ++i)
9268 {
9269 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
9270 if (i == index)
9271 break;
9272 if (*spf == NUL)
9273 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00009274 EMSGN(_("E765: 'spellfile' does not have %ld entries"), index);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009275 return;
9276 }
9277 }
9278
Bram Moolenaarb765d632005-06-07 21:00:02 +00009279 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009280 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009281 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
9282 buf = NULL;
9283 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00009284 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009285 EMSG(_(e_bufloaded));
9286 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009287 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009288
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009289 fname = fnamebuf;
9290 }
9291
Bram Moolenaard0131a82006-03-04 21:46:13 +00009292 if (bad || undo)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009293 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009294 /* When the word appears as good word we need to remove that one,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009295 * since its flags sort before the one with WF_BANNED. */
9296 fd = mch_fopen((char *)fname, "r");
9297 if (fd != NULL)
9298 {
9299 while (!vim_fgets(line, MAXWLEN * 2, fd))
9300 {
9301 fpos = fpos_next;
9302 fpos_next = ftell(fd);
9303 if (STRNCMP(word, line, len) == 0
9304 && (line[len] == '/' || line[len] < ' '))
9305 {
9306 /* Found duplicate word. Remove it by writing a '#' at
9307 * the start of the line. Mixing reading and writing
9308 * doesn't work for all systems, close the file first. */
9309 fclose(fd);
9310 fd = mch_fopen((char *)fname, "r+");
9311 if (fd == NULL)
9312 break;
9313 if (fseek(fd, fpos, SEEK_SET) == 0)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009314 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009315 fputc('#', fd);
Bram Moolenaard0131a82006-03-04 21:46:13 +00009316 if (undo)
9317 smsg((char_u *)_("Word removed from %s"), NameBuff);
9318 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009319 fseek(fd, fpos_next, SEEK_SET);
9320 }
9321 }
9322 fclose(fd);
9323 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009324 }
9325
Bram Moolenaard0131a82006-03-04 21:46:13 +00009326 if (!undo)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009327 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009328 fd = mch_fopen((char *)fname, "a");
9329 if (fd == NULL && new_spf)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009330 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009331 /* We just initialized the 'spellfile' option and can't open the
9332 * file. We may need to create the "spell" directory first. We
9333 * already checked the runtime directory is writable in
9334 * init_spellfile(). */
9335 if (!dir_of_file_exists(fname))
9336 {
9337 /* The directory doesn't exist. Try creating it and opening
9338 * the file again. */
9339 vim_mkdir(NameBuff, 0755);
9340 fd = mch_fopen((char *)fname, "a");
9341 }
9342 }
9343
9344 if (fd == NULL)
9345 EMSG2(_(e_notopen), fname);
9346 else
9347 {
9348 if (bad)
9349 fprintf(fd, "%.*s/!\n", len, word);
9350 else
9351 fprintf(fd, "%.*s\n", len, word);
9352 fclose(fd);
9353
9354 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
9355 smsg((char_u *)_("Word added to %s"), NameBuff);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009356 }
9357 }
9358
Bram Moolenaard0131a82006-03-04 21:46:13 +00009359 if (fd != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009360 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009361 /* Update the .add.spl file. */
9362 mkspell(1, &fname, FALSE, TRUE, TRUE);
9363
9364 /* If the .add file is edited somewhere, reload it. */
9365 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00009366 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009367
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009368 redraw_all_later(SOME_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009369 }
9370}
9371
9372/*
9373 * Initialize 'spellfile' for the current buffer.
9374 */
9375 static void
9376init_spellfile()
9377{
9378 char_u buf[MAXPATHL];
9379 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009380 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009381 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009382 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009383 int aspath = FALSE;
9384 char_u *lstart = curbuf->b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009385
9386 if (*curbuf->b_p_spl != NUL && curbuf->b_langp.ga_len > 0)
9387 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009388 /* Find the end of the language name. Exclude the region. If there
9389 * is a path separator remember the start of the tail. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009390 for (lend = curbuf->b_p_spl; *lend != NUL
9391 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009392 if (vim_ispathsep(*lend))
9393 {
9394 aspath = TRUE;
9395 lstart = lend + 1;
9396 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009397
9398 /* Loop over all entries in 'runtimepath'. Use the first one where we
9399 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009400 rtp = p_rtp;
9401 while (*rtp != NUL)
9402 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009403 if (aspath)
9404 /* Use directory of an entry with path, e.g., for
9405 * "/dir/lg.utf-8.spl" use "/dir". */
9406 vim_strncpy(buf, curbuf->b_p_spl, lstart - curbuf->b_p_spl - 1);
9407 else
9408 /* Copy the path from 'runtimepath' to buf[]. */
9409 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009410 if (filewritable(buf) == 2)
9411 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00009412 /* Use the first language name from 'spelllang' and the
9413 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009414 if (aspath)
9415 vim_strncpy(buf, curbuf->b_p_spl, lend - curbuf->b_p_spl);
9416 else
9417 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009418 /* Create the "spell" directory if it doesn't exist yet. */
9419 l = STRLEN(buf);
9420 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
9421 if (!filewritable(buf) != 2)
9422 vim_mkdir(buf, 0755);
9423
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009424 l = STRLEN(buf);
9425 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009426 "/%.*s", (int)(lend - lstart), lstart);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009427 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009428 l = STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009429 fname = LANGP_ENTRY(curbuf->b_langp, 0)->lp_slang->sl_fname;
9430 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
9431 fname != NULL
9432 && strstr((char *)gettail(fname), ".ascii.") != NULL
9433 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009434 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
9435 break;
9436 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009437 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009438 }
9439 }
9440}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009441
Bram Moolenaar51485f02005-06-04 21:55:20 +00009442
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009443/*
9444 * Init the chartab used for spelling for ASCII.
9445 * EBCDIC is not supported!
9446 */
9447 static void
9448clear_spell_chartab(sp)
9449 spelltab_T *sp;
9450{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009451 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009452
9453 /* Init everything to FALSE. */
9454 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
9455 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
9456 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009457 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009458 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009459 sp->st_upper[i] = i;
9460 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009461
9462 /* We include digits. A word shouldn't start with a digit, but handling
9463 * that is done separately. */
9464 for (i = '0'; i <= '9'; ++i)
9465 sp->st_isw[i] = TRUE;
9466 for (i = 'A'; i <= 'Z'; ++i)
9467 {
9468 sp->st_isw[i] = TRUE;
9469 sp->st_isu[i] = TRUE;
9470 sp->st_fold[i] = i + 0x20;
9471 }
9472 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009473 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009474 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009475 sp->st_upper[i] = i - 0x20;
9476 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009477}
9478
9479/*
9480 * Init the chartab used for spelling. Only depends on 'encoding'.
9481 * Called once while starting up and when 'encoding' changes.
9482 * The default is to use isalpha(), but the spell file should define the word
9483 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009484 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009485 */
9486 void
9487init_spell_chartab()
9488{
9489 int i;
9490
9491 did_set_spelltab = FALSE;
9492 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009493#ifdef FEAT_MBYTE
9494 if (enc_dbcs)
9495 {
9496 /* DBCS: assume double-wide characters are word characters. */
9497 for (i = 128; i <= 255; ++i)
9498 if (MB_BYTE2LEN(i) == 2)
9499 spelltab.st_isw[i] = TRUE;
9500 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009501 else if (enc_utf8)
9502 {
9503 for (i = 128; i < 256; ++i)
9504 {
9505 spelltab.st_isu[i] = utf_isupper(i);
9506 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
9507 spelltab.st_fold[i] = utf_fold(i);
9508 spelltab.st_upper[i] = utf_toupper(i);
9509 }
9510 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009511 else
9512#endif
9513 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009514 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009515 for (i = 128; i < 256; ++i)
9516 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009517 if (MB_ISUPPER(i))
9518 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009519 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009520 spelltab.st_isu[i] = TRUE;
9521 spelltab.st_fold[i] = MB_TOLOWER(i);
9522 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009523 else if (MB_ISLOWER(i))
9524 {
9525 spelltab.st_isw[i] = TRUE;
9526 spelltab.st_upper[i] = MB_TOUPPER(i);
9527 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009528 }
9529 }
9530}
9531
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009532/*
9533 * Set the spell character tables from strings in the affix file.
9534 */
9535 static int
9536set_spell_chartab(fol, low, upp)
9537 char_u *fol;
9538 char_u *low;
9539 char_u *upp;
9540{
9541 /* We build the new tables here first, so that we can compare with the
9542 * previous one. */
9543 spelltab_T new_st;
9544 char_u *pf = fol, *pl = low, *pu = upp;
9545 int f, l, u;
9546
9547 clear_spell_chartab(&new_st);
9548
9549 while (*pf != NUL)
9550 {
9551 if (*pl == NUL || *pu == NUL)
9552 {
9553 EMSG(_(e_affform));
9554 return FAIL;
9555 }
9556#ifdef FEAT_MBYTE
9557 f = mb_ptr2char_adv(&pf);
9558 l = mb_ptr2char_adv(&pl);
9559 u = mb_ptr2char_adv(&pu);
9560#else
9561 f = *pf++;
9562 l = *pl++;
9563 u = *pu++;
9564#endif
9565 /* Every character that appears is a word character. */
9566 if (f < 256)
9567 new_st.st_isw[f] = TRUE;
9568 if (l < 256)
9569 new_st.st_isw[l] = TRUE;
9570 if (u < 256)
9571 new_st.st_isw[u] = TRUE;
9572
9573 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9574 * case-folding */
9575 if (l < 256 && l != f)
9576 {
9577 if (f >= 256)
9578 {
9579 EMSG(_(e_affrange));
9580 return FAIL;
9581 }
9582 new_st.st_fold[l] = f;
9583 }
9584
9585 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009586 * case-folding, it's upper case and the "UPP" is the upper case of
9587 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009588 if (u < 256 && u != f)
9589 {
9590 if (f >= 256)
9591 {
9592 EMSG(_(e_affrange));
9593 return FAIL;
9594 }
9595 new_st.st_fold[u] = f;
9596 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009597 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009598 }
9599 }
9600
9601 if (*pl != NUL || *pu != NUL)
9602 {
9603 EMSG(_(e_affform));
9604 return FAIL;
9605 }
9606
9607 return set_spell_finish(&new_st);
9608}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009609
9610/*
9611 * Set the spell character tables from strings in the .spl file.
9612 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009613 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009614set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009615 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009616 int cnt; /* length of "flags" */
9617 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009618{
9619 /* We build the new tables here first, so that we can compare with the
9620 * previous one. */
9621 spelltab_T new_st;
9622 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009623 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009624 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009625
9626 clear_spell_chartab(&new_st);
9627
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009628 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009629 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009630 if (i < cnt)
9631 {
9632 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9633 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9634 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009635
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009636 if (*p != NUL)
9637 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009638#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009639 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009640#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009641 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009642#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009643 new_st.st_fold[i + 128] = c;
9644 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9645 new_st.st_upper[c] = i + 128;
9646 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009647 }
9648
Bram Moolenaar5195e452005-08-19 20:32:47 +00009649 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009650}
9651
9652 static int
9653set_spell_finish(new_st)
9654 spelltab_T *new_st;
9655{
9656 int i;
9657
9658 if (did_set_spelltab)
9659 {
9660 /* check that it's the same table */
9661 for (i = 0; i < 256; ++i)
9662 {
9663 if (spelltab.st_isw[i] != new_st->st_isw[i]
9664 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009665 || spelltab.st_fold[i] != new_st->st_fold[i]
9666 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009667 {
9668 EMSG(_("E763: Word characters differ between spell files"));
9669 return FAIL;
9670 }
9671 }
9672 }
9673 else
9674 {
9675 /* copy the new spelltab into the one being used */
9676 spelltab = *new_st;
9677 did_set_spelltab = TRUE;
9678 }
9679
9680 return OK;
9681}
9682
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009683/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009684 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009685 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009686 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009687 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009688 */
9689 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009690spell_iswordp(p, buf)
Bram Moolenaarea408852005-06-25 22:49:46 +00009691 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009692 buf_T *buf; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009693{
Bram Moolenaarea408852005-06-25 22:49:46 +00009694#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009695 char_u *s;
9696 int l;
9697 int c;
9698
9699 if (has_mbyte)
9700 {
9701 l = MB_BYTE2LEN(*p);
9702 s = p;
9703 if (l == 1)
9704 {
9705 /* be quick for ASCII */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009706 if (buf->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009707 {
9708 s = p + 1; /* skip a mid-word character */
9709 l = MB_BYTE2LEN(*s);
9710 }
9711 }
9712 else
9713 {
9714 c = mb_ptr2char(p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009715 if (c < 256 ? buf->b_spell_ismw[c]
9716 : (buf->b_spell_ismw_mb != NULL
9717 && vim_strchr(buf->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009718 {
9719 s = p + l;
9720 l = MB_BYTE2LEN(*s);
9721 }
9722 }
9723
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009724 c = mb_ptr2char(s);
9725 if (c > 255)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009726 return mb_get_class(s) >= 2;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009727 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009728 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009729#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009730
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009731 return spelltab.st_isw[buf->b_spell_ismw[*p] ? p[1] : p[0]];
9732}
9733
9734/*
9735 * Return TRUE if "p" points to a word character.
9736 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9737 */
9738 static int
9739spell_iswordp_nmw(p)
9740 char_u *p;
9741{
9742#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009743 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009744
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009745 if (has_mbyte)
9746 {
9747 c = mb_ptr2char(p);
9748 if (c > 255)
9749 return mb_get_class(p) >= 2;
9750 return spelltab.st_isw[c];
9751 }
9752#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009753 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009754}
9755
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009756#ifdef FEAT_MBYTE
9757/*
9758 * Return TRUE if "p" points to a word character.
9759 * Wide version of spell_iswordp().
9760 */
9761 static int
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009762spell_iswordp_w(p, buf)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009763 int *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009764 buf_T *buf;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009765{
9766 int *s;
9767
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009768 if (*p < 256 ? buf->b_spell_ismw[*p]
9769 : (buf->b_spell_ismw_mb != NULL
9770 && vim_strchr(buf->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009771 s = p + 1;
9772 else
9773 s = p;
9774
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009775 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009776 {
9777 if (enc_utf8)
9778 return utf_class(*s) >= 2;
9779 if (enc_dbcs)
9780 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
9781 return 0;
9782 }
9783 return spelltab.st_isw[*s];
9784}
9785#endif
9786
Bram Moolenaarea408852005-06-25 22:49:46 +00009787/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009788 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009789 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009790 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009791 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009792write_spell_prefcond(fd, gap)
9793 FILE *fd;
9794 garray_T *gap;
9795{
9796 int i;
9797 char_u *p;
9798 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009799 int totlen;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009800
Bram Moolenaar5195e452005-08-19 20:32:47 +00009801 if (fd != NULL)
9802 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
9803
9804 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009805
9806 for (i = 0; i < gap->ga_len; ++i)
9807 {
9808 /* <prefcond> : <condlen> <condstr> */
9809 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009810 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009811 {
9812 len = STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009813 if (fd != NULL)
9814 {
9815 fputc(len, fd);
9816 fwrite(p, (size_t)len, (size_t)1, fd);
9817 }
9818 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009819 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009820 else if (fd != NULL)
9821 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009822 }
9823
Bram Moolenaar5195e452005-08-19 20:32:47 +00009824 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009825}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009826
9827/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009828 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
9829 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009830 * When using a multi-byte 'encoding' the length may change!
9831 * Returns FAIL when something wrong.
9832 */
9833 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009834spell_casefold(str, len, buf, buflen)
9835 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009836 int len;
9837 char_u *buf;
9838 int buflen;
9839{
9840 int i;
9841
9842 if (len >= buflen)
9843 {
9844 buf[0] = NUL;
9845 return FAIL; /* result will not fit */
9846 }
9847
9848#ifdef FEAT_MBYTE
9849 if (has_mbyte)
9850 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009851 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009852 char_u *p;
9853 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009854
9855 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009856 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009857 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009858 if (outi + MB_MAXBYTES > buflen)
9859 {
9860 buf[outi] = NUL;
9861 return FAIL;
9862 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009863 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009864 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009865 }
9866 buf[outi] = NUL;
9867 }
9868 else
9869#endif
9870 {
9871 /* Be quick for non-multibyte encodings. */
9872 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009873 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009874 buf[i] = NUL;
9875 }
9876
9877 return OK;
9878}
9879
Bram Moolenaar4770d092006-01-12 23:22:24 +00009880/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009881#define SPS_BEST 1
9882#define SPS_FAST 2
9883#define SPS_DOUBLE 4
9884
Bram Moolenaar4770d092006-01-12 23:22:24 +00009885static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
9886static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009887
9888/*
9889 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009890 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009891 */
9892 int
9893spell_check_sps()
9894{
9895 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009896 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009897 char_u buf[MAXPATHL];
9898 int f;
9899
9900 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009901 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009902
9903 for (p = p_sps; *p != NUL; )
9904 {
9905 copy_option_part(&p, buf, MAXPATHL, ",");
9906
9907 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009908 if (VIM_ISDIGIT(*buf))
9909 {
9910 s = buf;
9911 sps_limit = getdigits(&s);
9912 if (*s != NUL && !VIM_ISDIGIT(*s))
9913 f = -1;
9914 }
9915 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009916 f = SPS_BEST;
9917 else if (STRCMP(buf, "fast") == 0)
9918 f = SPS_FAST;
9919 else if (STRCMP(buf, "double") == 0)
9920 f = SPS_DOUBLE;
9921 else if (STRNCMP(buf, "expr:", 5) != 0
9922 && STRNCMP(buf, "file:", 5) != 0)
9923 f = -1;
9924
9925 if (f == -1 || (sps_flags != 0 && f != 0))
9926 {
9927 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009928 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009929 return FAIL;
9930 }
9931 if (f != 0)
9932 sps_flags = f;
9933 }
9934
9935 if (sps_flags == 0)
9936 sps_flags = SPS_BEST;
9937
9938 return OK;
9939}
9940
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009941/*
9942 * "z?": Find badly spelled word under or after the cursor.
9943 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009944 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +00009945 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009946 */
9947 void
Bram Moolenaard12a1322005-08-21 22:08:24 +00009948spell_suggest(count)
9949 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009950{
9951 char_u *line;
9952 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009953 char_u wcopy[MAXWLEN + 2];
9954 char_u *p;
9955 int i;
9956 int c;
9957 suginfo_T sug;
9958 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009959 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00009960 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009961 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +00009962 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009963 int badlen = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009964
Bram Moolenaar66fa2712006-01-22 23:22:22 +00009965 if (no_spell_checking(curwin))
9966 return;
9967
9968#ifdef FEAT_VISUAL
9969 if (VIsual_active)
9970 {
9971 /* Use the Visually selected text as the bad word. But reject
9972 * a multi-line selection. */
9973 if (curwin->w_cursor.lnum != VIsual.lnum)
9974 {
9975 vim_beep();
9976 return;
9977 }
9978 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
9979 if (badlen < 0)
9980 badlen = -badlen;
9981 else
9982 curwin->w_cursor.col = VIsual.col;
9983 ++badlen;
9984 end_visual_mode();
9985 }
9986 else
9987#endif
9988 /* Find the start of the badly spelled word. */
9989 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +00009990 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009991 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00009992 /* No bad word or it starts after the cursor: use the word under the
9993 * cursor. */
9994 curwin->w_cursor = prev_cursor;
9995 line = ml_get_curline();
9996 p = line + curwin->w_cursor.col;
9997 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009998 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +00009999 mb_ptr_back(line, p);
10000 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010001 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010002 mb_ptr_adv(p);
10003
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010004 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010005 {
10006 beep_flush();
10007 return;
10008 }
10009 curwin->w_cursor.col = p - line;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010010 }
10011
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010012 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010013
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010014 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010015 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010016
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010017 line = ml_get_curline();
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010018
Bram Moolenaar5195e452005-08-19 20:32:47 +000010019 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
10020 * 'spellsuggest', whatever is smaller. */
10021 if (sps_limit > (int)Rows - 2)
10022 limit = (int)Rows - 2;
10023 else
10024 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010025 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010026 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010027
10028 if (sug.su_ga.ga_len == 0)
10029 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +000010030 else if (count > 0)
10031 {
10032 if (count > sug.su_ga.ga_len)
10033 smsg((char_u *)_("Sorry, only %ld suggestions"),
10034 (long)sug.su_ga.ga_len);
10035 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010036 else
10037 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010038 vim_free(repl_from);
10039 repl_from = NULL;
10040 vim_free(repl_to);
10041 repl_to = NULL;
10042
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010043#ifdef FEAT_RIGHTLEFT
10044 /* When 'rightleft' is set the list is drawn right-left. */
10045 cmdmsg_rl = curwin->w_p_rl;
10046 if (cmdmsg_rl)
10047 msg_col = Columns - 1;
10048#endif
10049
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010050 /* List the suggestions. */
10051 msg_start();
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010052 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010053 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
10054 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010055#ifdef FEAT_RIGHTLEFT
10056 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
10057 {
10058 /* And now the rabbit from the high hat: Avoid showing the
10059 * untranslated message rightleft. */
10060 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
10061 sug.su_badlen, sug.su_badptr);
10062 }
10063#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010064 msg_puts(IObuff);
10065 msg_clr_eos();
10066 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +000010067
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010068 msg_scroll = TRUE;
10069 for (i = 0; i < sug.su_ga.ga_len; ++i)
10070 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010071 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010072
10073 /* The suggested word may replace only part of the bad word, add
10074 * the not replaced part. */
10075 STRCPY(wcopy, stp->st_word);
10076 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010077 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010078 sug.su_badptr + stp->st_orglen,
10079 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010080 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
10081#ifdef FEAT_RIGHTLEFT
10082 if (cmdmsg_rl)
10083 rl_mirror(IObuff);
10084#endif
10085 msg_puts(IObuff);
10086
10087 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010088 msg_puts(IObuff);
10089
10090 /* The word may replace more than "su_badlen". */
10091 if (sug.su_badlen < stp->st_orglen)
10092 {
10093 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
10094 stp->st_orglen, sug.su_badptr);
10095 msg_puts(IObuff);
10096 }
10097
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010098 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010099 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010100 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010101 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010102 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010103 stp->st_salscore ? "s " : "",
10104 stp->st_score, stp->st_altscore);
10105 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010106 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +000010107 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010108#ifdef FEAT_RIGHTLEFT
10109 if (cmdmsg_rl)
10110 /* Mirror the numbers, but keep the leading space. */
10111 rl_mirror(IObuff + 1);
10112#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +000010113 msg_advance(30);
10114 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010115 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010116 msg_putchar('\n');
10117 }
10118
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010119#ifdef FEAT_RIGHTLEFT
10120 cmdmsg_rl = FALSE;
10121 msg_col = 0;
10122#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010123 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010124 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010125 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010126 selected -= lines_left;
Bram Moolenaar0fd92892006-03-09 22:27:48 +000010127 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010128 }
10129
Bram Moolenaard12a1322005-08-21 22:08:24 +000010130 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
10131 {
10132 /* Save the from and to text for :spellrepall. */
10133 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000010134 if (sug.su_badlen > stp->st_orglen)
10135 {
10136 /* Replacing less than "su_badlen", append the remainder to
10137 * repl_to. */
10138 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
10139 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
10140 sug.su_badlen - stp->st_orglen,
10141 sug.su_badptr + stp->st_orglen);
10142 repl_to = vim_strsave(IObuff);
10143 }
10144 else
10145 {
10146 /* Replacing su_badlen or more, use the whole word. */
10147 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
10148 repl_to = vim_strsave(stp->st_word);
10149 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010150
10151 /* Replace the word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010152 p = alloc(STRLEN(line) - stp->st_orglen + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010153 if (p != NULL)
10154 {
10155 c = sug.su_badptr - line;
10156 mch_memmove(p, line, c);
10157 STRCPY(p + c, stp->st_word);
10158 STRCAT(p, sug.su_badptr + stp->st_orglen);
10159 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10160 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010161
10162 /* For redo we use a change-word command. */
10163 ResetRedobuff();
10164 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +000010165 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010166 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010167 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010168
10169 /* After this "p" may be invalid. */
10170 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010171 }
10172 }
10173 else
10174 curwin->w_cursor = prev_cursor;
10175
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010176 spell_find_cleanup(&sug);
10177}
10178
10179/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010180 * Check if the word at line "lnum" column "col" is required to start with a
10181 * capital. This uses 'spellcapcheck' of the current buffer.
10182 */
10183 static int
10184check_need_cap(lnum, col)
10185 linenr_T lnum;
10186 colnr_T col;
10187{
10188 int need_cap = FALSE;
10189 char_u *line;
10190 char_u *line_copy = NULL;
10191 char_u *p;
10192 colnr_T endcol;
10193 regmatch_T regmatch;
10194
10195 if (curbuf->b_cap_prog == NULL)
10196 return FALSE;
10197
10198 line = ml_get_curline();
10199 endcol = 0;
10200 if ((int)(skipwhite(line) - line) >= (int)col)
10201 {
10202 /* At start of line, check if previous line is empty or sentence
10203 * ends there. */
10204 if (lnum == 1)
10205 need_cap = TRUE;
10206 else
10207 {
10208 line = ml_get(lnum - 1);
10209 if (*skipwhite(line) == NUL)
10210 need_cap = TRUE;
10211 else
10212 {
10213 /* Append a space in place of the line break. */
10214 line_copy = concat_str(line, (char_u *)" ");
10215 line = line_copy;
10216 endcol = STRLEN(line);
10217 }
10218 }
10219 }
10220 else
10221 endcol = col;
10222
10223 if (endcol > 0)
10224 {
10225 /* Check if sentence ends before the bad word. */
10226 regmatch.regprog = curbuf->b_cap_prog;
10227 regmatch.rm_ic = FALSE;
10228 p = line + endcol;
10229 for (;;)
10230 {
10231 mb_ptr_back(line, p);
10232 if (p == line || spell_iswordp_nmw(p))
10233 break;
10234 if (vim_regexec(&regmatch, p, 0)
10235 && regmatch.endp[0] == line + endcol)
10236 {
10237 need_cap = TRUE;
10238 break;
10239 }
10240 }
10241 }
10242
10243 vim_free(line_copy);
10244
10245 return need_cap;
10246}
10247
10248
10249/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010250 * ":spellrepall"
10251 */
10252/*ARGSUSED*/
10253 void
10254ex_spellrepall(eap)
10255 exarg_T *eap;
10256{
10257 pos_T pos = curwin->w_cursor;
10258 char_u *frompat;
10259 int addlen;
10260 char_u *line;
10261 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010262 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010263 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010264
10265 if (repl_from == NULL || repl_to == NULL)
10266 {
10267 EMSG(_("E752: No previous spell replacement"));
10268 return;
10269 }
10270 addlen = STRLEN(repl_to) - STRLEN(repl_from);
10271
10272 frompat = alloc(STRLEN(repl_from) + 7);
10273 if (frompat == NULL)
10274 return;
10275 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
10276 p_ws = FALSE;
10277
Bram Moolenaar5195e452005-08-19 20:32:47 +000010278 sub_nsubs = 0;
10279 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010280 curwin->w_cursor.lnum = 0;
10281 while (!got_int)
10282 {
10283 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP) == 0
10284 || u_save_cursor() == FAIL)
10285 break;
10286
10287 /* Only replace when the right word isn't there yet. This happens
10288 * when changing "etc" to "etc.". */
10289 line = ml_get_curline();
10290 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
10291 repl_to, STRLEN(repl_to)) != 0)
10292 {
10293 p = alloc(STRLEN(line) + addlen + 1);
10294 if (p == NULL)
10295 break;
10296 mch_memmove(p, line, curwin->w_cursor.col);
10297 STRCPY(p + curwin->w_cursor.col, repl_to);
10298 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
10299 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10300 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010301
10302 if (curwin->w_cursor.lnum != prev_lnum)
10303 {
10304 ++sub_nlines;
10305 prev_lnum = curwin->w_cursor.lnum;
10306 }
10307 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010308 }
10309 curwin->w_cursor.col += STRLEN(repl_to);
10310 }
10311
10312 p_ws = save_ws;
10313 curwin->w_cursor = pos;
10314 vim_free(frompat);
10315
Bram Moolenaar5195e452005-08-19 20:32:47 +000010316 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010317 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010318 else
10319 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010320}
10321
10322/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010323 * Find spell suggestions for "word". Return them in the growarray "*gap" as
10324 * a list of allocated strings.
10325 */
10326 void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010327spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010328 garray_T *gap;
10329 char_u *word;
10330 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010331 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010332 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010333{
10334 suginfo_T sug;
10335 int i;
10336 suggest_T *stp;
10337 char_u *wcopy;
10338
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010339 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010340
10341 /* Make room in "gap". */
10342 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010343 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010344 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010345 for (i = 0; i < sug.su_ga.ga_len; ++i)
10346 {
10347 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010348
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010349 /* The suggested word may replace only part of "word", add the not
10350 * replaced part. */
10351 wcopy = alloc(stp->st_wordlen
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010352 + STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010353 if (wcopy == NULL)
10354 break;
10355 STRCPY(wcopy, stp->st_word);
10356 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
10357 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
10358 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010359 }
10360
10361 spell_find_cleanup(&sug);
10362}
10363
10364/*
10365 * Find spell suggestions for the word at the start of "badptr".
10366 * Return the suggestions in "su->su_ga".
10367 * The maximum number of suggestions is "maxcount".
10368 * Note: does use info for the current window.
10369 * This is based on the mechanisms of Aspell, but completely reimplemented.
10370 */
10371 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010372spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010373 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010374 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010375 suginfo_T *su;
10376 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +000010377 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010378 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010379 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010380{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010381 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010382 char_u buf[MAXPATHL];
10383 char_u *p;
10384 int do_combine = FALSE;
10385 char_u *sps_copy;
10386#ifdef FEAT_EVAL
10387 static int expr_busy = FALSE;
10388#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010389 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010390 int i;
10391 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010392
10393 /*
10394 * Set the info in "*su".
10395 */
10396 vim_memset(su, 0, sizeof(suginfo_T));
10397 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10398 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010399 if (*badptr == NUL)
10400 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010401 hash_init(&su->su_banned);
10402
10403 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010404 if (badlen != 0)
10405 su->su_badlen = badlen;
10406 else
10407 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010408 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010409 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010410
10411 if (su->su_badlen >= MAXWLEN)
10412 su->su_badlen = MAXWLEN - 1; /* just in case */
10413 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10414 (void)spell_casefold(su->su_badptr, su->su_badlen,
10415 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010416 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010417 su->su_badflags = badword_captype(su->su_badptr,
10418 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010419 if (need_cap)
10420 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010421
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010422 /* Find the default language for sound folding. We simply use the first
10423 * one in 'spelllang' that supports sound folding. That's good for when
10424 * using multiple files for one language, it's not that bad when mixing
10425 * languages (e.g., "pl,en"). */
10426 for (i = 0; i < curbuf->b_langp.ga_len; ++i)
10427 {
10428 lp = LANGP_ENTRY(curbuf->b_langp, i);
10429 if (lp->lp_sallang != NULL)
10430 {
10431 su->su_sallang = lp->lp_sallang;
10432 break;
10433 }
10434 }
10435
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010436 /* Soundfold the bad word with the default sound folding, so that we don't
10437 * have to do this many times. */
10438 if (su->su_sallang != NULL)
10439 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10440 su->su_sal_badword);
10441
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010442 /* If the word is not capitalised and spell_check() doesn't consider the
10443 * word to be bad then it might need to be capitalised. Add a suggestion
10444 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010445 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010446 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010447 {
10448 make_case_word(su->su_badword, buf, WF_ONECAP);
10449 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010450 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010451 }
10452
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010453 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +000010454 if (banbadword)
10455 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010456
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010457 /* Make a copy of 'spellsuggest', because the expression may change it. */
10458 sps_copy = vim_strsave(p_sps);
10459 if (sps_copy == NULL)
10460 return;
10461
10462 /* Loop over the items in 'spellsuggest'. */
10463 for (p = sps_copy; *p != NUL; )
10464 {
10465 copy_option_part(&p, buf, MAXPATHL, ",");
10466
10467 if (STRNCMP(buf, "expr:", 5) == 0)
10468 {
10469#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010470 /* Evaluate an expression. Skip this when called recursively,
10471 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010472 if (!expr_busy)
10473 {
10474 expr_busy = TRUE;
10475 spell_suggest_expr(su, buf + 5);
10476 expr_busy = FALSE;
10477 }
10478#endif
10479 }
10480 else if (STRNCMP(buf, "file:", 5) == 0)
10481 /* Use list of suggestions in a file. */
10482 spell_suggest_file(su, buf + 5);
10483 else
10484 {
10485 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010486 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010487 if (sps_flags & SPS_DOUBLE)
10488 do_combine = TRUE;
10489 }
10490 }
10491
10492 vim_free(sps_copy);
10493
10494 if (do_combine)
10495 /* Combine the two list of suggestions. This must be done last,
10496 * because sorting changes the order again. */
10497 score_combine(su);
10498}
10499
10500#ifdef FEAT_EVAL
10501/*
10502 * Find suggestions by evaluating expression "expr".
10503 */
10504 static void
10505spell_suggest_expr(su, expr)
10506 suginfo_T *su;
10507 char_u *expr;
10508{
10509 list_T *list;
10510 listitem_T *li;
10511 int score;
10512 char_u *p;
10513
10514 /* The work is split up in a few parts to avoid having to export
10515 * suginfo_T.
10516 * First evaluate the expression and get the resulting list. */
10517 list = eval_spell_expr(su->su_badword, expr);
10518 if (list != NULL)
10519 {
10520 /* Loop over the items in the list. */
10521 for (li = list->lv_first; li != NULL; li = li->li_next)
10522 if (li->li_tv.v_type == VAR_LIST)
10523 {
10524 /* Get the word and the score from the items. */
10525 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010526 if (score >= 0 && score <= su->su_maxscore)
10527 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10528 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010529 }
10530 list_unref(list);
10531 }
10532
Bram Moolenaar4770d092006-01-12 23:22:24 +000010533 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10534 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010535 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10536}
10537#endif
10538
10539/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010540 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010541 */
10542 static void
10543spell_suggest_file(su, fname)
10544 suginfo_T *su;
10545 char_u *fname;
10546{
10547 FILE *fd;
10548 char_u line[MAXWLEN * 2];
10549 char_u *p;
10550 int len;
10551 char_u cword[MAXWLEN];
10552
10553 /* Open the file. */
10554 fd = mch_fopen((char *)fname, "r");
10555 if (fd == NULL)
10556 {
10557 EMSG2(_(e_notopen), fname);
10558 return;
10559 }
10560
10561 /* Read it line by line. */
10562 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10563 {
10564 line_breakcheck();
10565
10566 p = vim_strchr(line, '/');
10567 if (p == NULL)
10568 continue; /* No Tab found, just skip the line. */
10569 *p++ = NUL;
10570 if (STRICMP(su->su_badword, line) == 0)
10571 {
10572 /* Match! Isolate the good word, until CR or NL. */
10573 for (len = 0; p[len] >= ' '; ++len)
10574 ;
10575 p[len] = NUL;
10576
10577 /* If the suggestion doesn't have specific case duplicate the case
10578 * of the bad word. */
10579 if (captype(p, NULL) == 0)
10580 {
10581 make_case_word(p, cword, su->su_badflags);
10582 p = cword;
10583 }
10584
10585 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010586 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010587 }
10588 }
10589
10590 fclose(fd);
10591
Bram Moolenaar4770d092006-01-12 23:22:24 +000010592 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10593 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010594 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10595}
10596
10597/*
10598 * Find suggestions for the internal method indicated by "sps_flags".
10599 */
10600 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010601spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010602 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010603 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010604{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010605 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010606 * Load the .sug file(s) that are available and not done yet.
10607 */
10608 suggest_load_files();
10609
10610 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010611 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010612 *
10613 * Set a maximum score to limit the combination of operations that is
10614 * tried.
10615 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010616 suggest_try_special(su);
10617
10618 /*
10619 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10620 * from the .aff file and inserting a space (split the word).
10621 */
10622 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010623
10624 /* For the resulting top-scorers compute the sound-a-like score. */
10625 if (sps_flags & SPS_DOUBLE)
10626 score_comp_sal(su);
10627
10628 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010629 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010630 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010631 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010632 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010633 if (sps_flags & SPS_BEST)
10634 /* Adjust the word score for the suggestions found so far for how
10635 * they sounds like. */
10636 rescore_suggestions(su);
10637
10638 /*
10639 * While going throught the soundfold tree "su_maxscore" is the score
10640 * for the soundfold word, limits the changes that are being tried,
10641 * and "su_sfmaxscore" the rescored score, which is set by
10642 * cleanup_suggestions().
10643 * First find words with a small edit distance, because this is much
10644 * faster and often already finds the top-N suggestions. If we didn't
10645 * find many suggestions try again with a higher edit distance.
10646 * "sl_sounddone" is used to avoid doing the same word twice.
10647 */
10648 suggest_try_soundalike_prep();
10649 su->su_maxscore = SCORE_SFMAX1;
10650 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010651 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010652 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10653 {
10654 /* We didn't find enough matches, try again, allowing more
10655 * changes to the soundfold word. */
10656 su->su_maxscore = SCORE_SFMAX2;
10657 suggest_try_soundalike(su);
10658 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10659 {
10660 /* Still didn't find enough matches, try again, allowing even
10661 * more changes to the soundfold word. */
10662 su->su_maxscore = SCORE_SFMAX3;
10663 suggest_try_soundalike(su);
10664 }
10665 }
10666 su->su_maxscore = su->su_sfmaxscore;
10667 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010668 }
10669
Bram Moolenaar4770d092006-01-12 23:22:24 +000010670 /* When CTRL-C was hit while searching do show the results. Only clear
10671 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010672 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010673 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010674 {
10675 (void)vgetc();
10676 got_int = FALSE;
10677 }
10678
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010679 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010680 {
10681 if (sps_flags & SPS_BEST)
10682 /* Adjust the word score for how it sounds like. */
10683 rescore_suggestions(su);
10684
Bram Moolenaar4770d092006-01-12 23:22:24 +000010685 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10686 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010687 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010688 }
10689}
10690
10691/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010692 * Load the .sug files for languages that have one and weren't loaded yet.
10693 */
10694 static void
10695suggest_load_files()
10696{
10697 langp_T *lp;
10698 int lpi;
10699 slang_T *slang;
10700 char_u *dotp;
10701 FILE *fd;
10702 char_u buf[MAXWLEN];
10703 int i;
10704 time_t timestamp;
10705 int wcount;
10706 int wordnr;
10707 garray_T ga;
10708 int c;
10709
10710 /* Do this for all languages that support sound folding. */
10711 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
10712 {
10713 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
10714 slang = lp->lp_slang;
10715 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10716 {
10717 /* Change ".spl" to ".sug" and open the file. When the file isn't
10718 * found silently skip it. Do set "sl_sugloaded" so that we
10719 * don't try again and again. */
10720 slang->sl_sugloaded = TRUE;
10721
10722 dotp = vim_strrchr(slang->sl_fname, '.');
10723 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10724 continue;
10725 STRCPY(dotp, ".sug");
Bram Moolenaar5555acc2006-04-07 21:33:12 +000010726 fd = mch_fopen((char *)slang->sl_fname, "r");
Bram Moolenaar4770d092006-01-12 23:22:24 +000010727 if (fd == NULL)
10728 goto nextone;
10729
10730 /*
10731 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10732 */
10733 for (i = 0; i < VIMSUGMAGICL; ++i)
10734 buf[i] = getc(fd); /* <fileID> */
10735 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10736 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010737 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010738 slang->sl_fname);
10739 goto nextone;
10740 }
10741 c = getc(fd); /* <versionnr> */
10742 if (c < VIMSUGVERSION)
10743 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010744 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010745 slang->sl_fname);
10746 goto nextone;
10747 }
10748 else if (c > VIMSUGVERSION)
10749 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010750 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010751 slang->sl_fname);
10752 goto nextone;
10753 }
10754
10755 /* Check the timestamp, it must be exactly the same as the one in
10756 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010757 timestamp = get8c(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010758 if (timestamp != slang->sl_sugtime)
10759 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010760 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010761 slang->sl_fname);
10762 goto nextone;
10763 }
10764
10765 /*
10766 * <SUGWORDTREE>: <wordtree>
10767 * Read the trie with the soundfolded words.
10768 */
10769 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10770 FALSE, 0) != 0)
10771 {
10772someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010773 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010774 slang->sl_fname);
10775 slang_clear_sug(slang);
10776 goto nextone;
10777 }
10778
10779 /*
10780 * <SUGTABLE>: <sugwcount> <sugline> ...
10781 *
10782 * Read the table with word numbers. We use a file buffer for
10783 * this, because it's so much like a file with lines. Makes it
10784 * possible to swap the info and save on memory use.
10785 */
10786 slang->sl_sugbuf = open_spellbuf();
10787 if (slang->sl_sugbuf == NULL)
10788 goto someerror;
10789 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010790 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010791 if (wcount < 0)
10792 goto someerror;
10793
10794 /* Read all the wordnr lists into the buffer, one NUL terminated
10795 * list per line. */
10796 ga_init2(&ga, 1, 100);
10797 for (wordnr = 0; wordnr < wcount; ++wordnr)
10798 {
10799 ga.ga_len = 0;
10800 for (;;)
10801 {
10802 c = getc(fd); /* <sugline> */
10803 if (c < 0 || ga_grow(&ga, 1) == FAIL)
10804 goto someerror;
10805 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
10806 if (c == NUL)
10807 break;
10808 }
10809 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
10810 ga.ga_data, ga.ga_len, TRUE) == FAIL)
10811 goto someerror;
10812 }
10813 ga_clear(&ga);
10814
10815 /*
10816 * Need to put word counts in the word tries, so that we can find
10817 * a word by its number.
10818 */
10819 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
10820 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
10821
10822nextone:
10823 if (fd != NULL)
10824 fclose(fd);
10825 STRCPY(dotp, ".spl");
10826 }
10827 }
10828}
10829
10830
10831/*
10832 * Fill in the wordcount fields for a trie.
10833 * Returns the total number of words.
10834 */
10835 static void
10836tree_count_words(byts, idxs)
10837 char_u *byts;
10838 idx_T *idxs;
10839{
10840 int depth;
10841 idx_T arridx[MAXWLEN];
10842 int curi[MAXWLEN];
10843 int c;
10844 idx_T n;
10845 int wordcount[MAXWLEN];
10846
10847 arridx[0] = 0;
10848 curi[0] = 1;
10849 wordcount[0] = 0;
10850 depth = 0;
10851 while (depth >= 0 && !got_int)
10852 {
10853 if (curi[depth] > byts[arridx[depth]])
10854 {
10855 /* Done all bytes at this node, go up one level. */
10856 idxs[arridx[depth]] = wordcount[depth];
10857 if (depth > 0)
10858 wordcount[depth - 1] += wordcount[depth];
10859
10860 --depth;
10861 fast_breakcheck();
10862 }
10863 else
10864 {
10865 /* Do one more byte at this node. */
10866 n = arridx[depth] + curi[depth];
10867 ++curi[depth];
10868
10869 c = byts[n];
10870 if (c == 0)
10871 {
10872 /* End of word, count it. */
10873 ++wordcount[depth];
10874
10875 /* Skip over any other NUL bytes (same word with different
10876 * flags). */
10877 while (byts[n + 1] == 0)
10878 {
10879 ++n;
10880 ++curi[depth];
10881 }
10882 }
10883 else
10884 {
10885 /* Normal char, go one level deeper to count the words. */
10886 ++depth;
10887 arridx[depth] = idxs[n];
10888 curi[depth] = 1;
10889 wordcount[depth] = 0;
10890 }
10891 }
10892 }
10893}
10894
10895/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010896 * Free the info put in "*su" by spell_find_suggest().
10897 */
10898 static void
10899spell_find_cleanup(su)
10900 suginfo_T *su;
10901{
10902 int i;
10903
10904 /* Free the suggestions. */
10905 for (i = 0; i < su->su_ga.ga_len; ++i)
10906 vim_free(SUG(su->su_ga, i).st_word);
10907 ga_clear(&su->su_ga);
10908 for (i = 0; i < su->su_sga.ga_len; ++i)
10909 vim_free(SUG(su->su_sga, i).st_word);
10910 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010911
10912 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010913 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010914}
10915
10916/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010917 * Make a copy of "word", with the first letter upper or lower cased, to
10918 * "wcopy[MAXWLEN]". "word" must not be empty.
10919 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010920 */
10921 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010922onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010923 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010924 char_u *wcopy;
10925 int upper; /* TRUE: first letter made upper case */
10926{
10927 char_u *p;
10928 int c;
10929 int l;
10930
10931 p = word;
10932#ifdef FEAT_MBYTE
10933 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010934 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010935 else
10936#endif
10937 c = *p++;
10938 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010939 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010940 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010941 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010942#ifdef FEAT_MBYTE
10943 if (has_mbyte)
10944 l = mb_char2bytes(c, wcopy);
10945 else
10946#endif
10947 {
10948 l = 1;
10949 wcopy[0] = c;
10950 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000010951 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010952}
10953
10954/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010955 * Make a copy of "word" with all the letters upper cased into
10956 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010957 */
10958 static void
10959allcap_copy(word, wcopy)
10960 char_u *word;
10961 char_u *wcopy;
10962{
10963 char_u *s;
10964 char_u *d;
10965 int c;
10966
10967 d = wcopy;
10968 for (s = word; *s != NUL; )
10969 {
10970#ifdef FEAT_MBYTE
10971 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010972 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010973 else
10974#endif
10975 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000010976
10977#ifdef FEAT_MBYTE
10978 /* We only change ß to SS when we are certain latin1 is used. It
10979 * would cause weird errors in other 8-bit encodings. */
10980 if (enc_latin1like && c == 0xdf)
10981 {
10982 c = 'S';
10983 if (d - wcopy >= MAXWLEN - 1)
10984 break;
10985 *d++ = c;
10986 }
10987 else
10988#endif
10989 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010990
10991#ifdef FEAT_MBYTE
10992 if (has_mbyte)
10993 {
10994 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
10995 break;
10996 d += mb_char2bytes(c, d);
10997 }
10998 else
10999#endif
11000 {
11001 if (d - wcopy >= MAXWLEN - 1)
11002 break;
11003 *d++ = c;
11004 }
11005 }
11006 *d = NUL;
11007}
11008
11009/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000011010 * Try finding suggestions by recognizing specific situations.
11011 */
11012 static void
11013suggest_try_special(su)
11014 suginfo_T *su;
11015{
11016 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011017 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011018 int c;
11019 char_u word[MAXWLEN];
11020
11021 /*
11022 * Recognize a word that is repeated: "the the".
11023 */
11024 p = skiptowhite(su->su_fbadword);
11025 len = p - su->su_fbadword;
11026 p = skipwhite(p);
11027 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
11028 {
11029 /* Include badflags: if the badword is onecap or allcap
11030 * use that for the goodword too: "The the" -> "The". */
11031 c = su->su_fbadword[len];
11032 su->su_fbadword[len] = NUL;
11033 make_case_word(su->su_fbadword, word, su->su_badflags);
11034 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011035
11036 /* Give a soundalike score of 0, compute the score as if deleting one
11037 * character. */
11038 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011039 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011040 }
11041}
11042
11043/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011044 * Try finding suggestions by adding/removing/swapping letters.
11045 */
11046 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000011047suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011048 suginfo_T *su;
11049{
11050 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011051 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011052 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011053 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011054 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011055
11056 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000011057 * to find matches (esp. REP items). Append some more text, changing
11058 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011059 STRCPY(fword, su->su_fbadword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011060 n = STRLEN(fword);
11061 p = su->su_badptr + su->su_badlen;
11062 (void)spell_casefold(p, STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011063
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011064 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011065 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011066 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011067
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011068 /* If reloading a spell file fails it's still in the list but
11069 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011070 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011071 continue;
11072
Bram Moolenaar4770d092006-01-12 23:22:24 +000011073 /* Try it for this language. Will add possible suggestions. */
11074 suggest_trie_walk(su, lp, fword, FALSE);
11075 }
11076}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011077
Bram Moolenaar4770d092006-01-12 23:22:24 +000011078/* Check the maximum score, if we go over it we won't try this change. */
11079#define TRY_DEEPER(su, stack, depth, add) \
11080 (stack[depth].ts_score + (add) < su->su_maxscore)
11081
11082/*
11083 * Try finding suggestions by adding/removing/swapping letters.
11084 *
11085 * This uses a state machine. At each node in the tree we try various
11086 * operations. When trying if an operation works "depth" is increased and the
11087 * stack[] is used to store info. This allows combinations, thus insert one
11088 * character, replace one and delete another. The number of changes is
11089 * limited by su->su_maxscore.
11090 *
11091 * After implementing this I noticed an article by Kemal Oflazer that
11092 * describes something similar: "Error-tolerant Finite State Recognition with
11093 * Applications to Morphological Analysis and Spelling Correction" (1996).
11094 * The implementation in the article is simplified and requires a stack of
11095 * unknown depth. The implementation here only needs a stack depth equal to
11096 * the length of the word.
11097 *
11098 * This is also used for the sound-folded word, "soundfold" is TRUE then.
11099 * The mechanism is the same, but we find a match with a sound-folded word
11100 * that comes from one or more original words. Each of these words may be
11101 * added, this is done by add_sound_suggest().
11102 * Don't use:
11103 * the prefix tree or the keep-case tree
11104 * "su->su_badlen"
11105 * anything to do with upper and lower case
11106 * anything to do with word or non-word characters ("spell_iswordp()")
11107 * banned words
11108 * word flags (rare, region, compounding)
11109 * word splitting for now
11110 * "similar_chars()"
11111 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
11112 */
11113 static void
11114suggest_trie_walk(su, lp, fword, soundfold)
11115 suginfo_T *su;
11116 langp_T *lp;
11117 char_u *fword;
11118 int soundfold;
11119{
11120 char_u tword[MAXWLEN]; /* good word collected so far */
11121 trystate_T stack[MAXWLEN];
11122 char_u preword[MAXWLEN * 3]; /* word found with proper case;
11123 * concatanation of prefix compound
11124 * words and split word. NUL terminated
11125 * when going deeper but not when coming
11126 * back. */
11127 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
11128 trystate_T *sp;
11129 int newscore;
11130 int score;
11131 char_u *byts, *fbyts, *pbyts;
11132 idx_T *idxs, *fidxs, *pidxs;
11133 int depth;
11134 int c, c2, c3;
11135 int n = 0;
11136 int flags;
11137 garray_T *gap;
11138 idx_T arridx;
11139 int len;
11140 char_u *p;
11141 fromto_T *ftp;
11142 int fl = 0, tl;
11143 int repextra = 0; /* extra bytes in fword[] from REP item */
11144 slang_T *slang = lp->lp_slang;
11145 int fword_ends;
11146 int goodword_ends;
11147#ifdef DEBUG_TRIEWALK
11148 /* Stores the name of the change made at each level. */
11149 char_u changename[MAXWLEN][80];
11150#endif
11151 int breakcheckcount = 1000;
11152 int compound_ok;
11153
11154 /*
11155 * Go through the whole case-fold tree, try changes at each node.
11156 * "tword[]" contains the word collected from nodes in the tree.
11157 * "fword[]" the word we are trying to match with (initially the bad
11158 * word).
11159 */
11160 depth = 0;
11161 sp = &stack[0];
11162 vim_memset(sp, 0, sizeof(trystate_T));
11163 sp->ts_curi = 1;
11164
11165 if (soundfold)
11166 {
11167 /* Going through the soundfold tree. */
11168 byts = fbyts = slang->sl_sbyts;
11169 idxs = fidxs = slang->sl_sidxs;
11170 pbyts = NULL;
11171 pidxs = NULL;
11172 sp->ts_prefixdepth = PFD_NOPREFIX;
11173 sp->ts_state = STATE_START;
11174 }
11175 else
11176 {
Bram Moolenaarea424162005-06-16 21:51:00 +000011177 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011178 * When there are postponed prefixes we need to use these first. At
11179 * the end of the prefix we continue in the case-fold tree.
11180 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011181 fbyts = slang->sl_fbyts;
11182 fidxs = slang->sl_fidxs;
11183 pbyts = slang->sl_pbyts;
11184 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011185 if (pbyts != NULL)
11186 {
11187 byts = pbyts;
11188 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011189 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011190 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
11191 }
11192 else
11193 {
11194 byts = fbyts;
11195 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011196 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011197 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011198 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011199 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011200
Bram Moolenaar4770d092006-01-12 23:22:24 +000011201 /*
11202 * Loop to find all suggestions. At each round we either:
11203 * - For the current state try one operation, advance "ts_curi",
11204 * increase "depth".
11205 * - When a state is done go to the next, set "ts_state".
11206 * - When all states are tried decrease "depth".
11207 */
11208 while (depth >= 0 && !got_int)
11209 {
11210 sp = &stack[depth];
11211 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011212 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011213 case STATE_START:
11214 case STATE_NOPREFIX:
11215 /*
11216 * Start of node: Deal with NUL bytes, which means
11217 * tword[] may end here.
11218 */
11219 arridx = sp->ts_arridx; /* current node in the tree */
11220 len = byts[arridx]; /* bytes in this node */
11221 arridx += sp->ts_curi; /* index of current byte */
11222
11223 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011224 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011225 /* Skip over the NUL bytes, we use them later. */
11226 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
11227 ;
11228 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011229
Bram Moolenaar4770d092006-01-12 23:22:24 +000011230 /* Always past NUL bytes now. */
11231 n = (int)sp->ts_state;
11232 sp->ts_state = STATE_ENDNUL;
11233 sp->ts_save_badflags = su->su_badflags;
11234
11235 /* At end of a prefix or at start of prefixtree: check for
11236 * following word. */
11237 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011238 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011239 /* Set su->su_badflags to the caps type at this position.
11240 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000011241#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011242 if (has_mbyte)
11243 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
11244 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000011245#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011246 n = sp->ts_fidx;
11247 flags = badword_captype(su->su_badptr, su->su_badptr + n);
11248 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011249 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011250#ifdef DEBUG_TRIEWALK
11251 sprintf(changename[depth], "prefix");
11252#endif
11253 go_deeper(stack, depth, 0);
11254 ++depth;
11255 sp = &stack[depth];
11256 sp->ts_prefixdepth = depth - 1;
11257 byts = fbyts;
11258 idxs = fidxs;
11259 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011260
Bram Moolenaar4770d092006-01-12 23:22:24 +000011261 /* Move the prefix to preword[] with the right case
11262 * and make find_keepcap_word() works. */
11263 tword[sp->ts_twordlen] = NUL;
11264 make_case_word(tword + sp->ts_splitoff,
11265 preword + sp->ts_prewordlen, flags);
11266 sp->ts_prewordlen = STRLEN(preword);
11267 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011268 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011269 break;
11270 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011271
Bram Moolenaar4770d092006-01-12 23:22:24 +000011272 if (sp->ts_curi > len || byts[arridx] != 0)
11273 {
11274 /* Past bytes in node and/or past NUL bytes. */
11275 sp->ts_state = STATE_ENDNUL;
11276 sp->ts_save_badflags = su->su_badflags;
11277 break;
11278 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011279
Bram Moolenaar4770d092006-01-12 23:22:24 +000011280 /*
11281 * End of word in tree.
11282 */
11283 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011284
Bram Moolenaar4770d092006-01-12 23:22:24 +000011285 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011286
11287 /* Skip words with the NOSUGGEST flag. */
11288 if (flags & WF_NOSUGGEST)
11289 break;
11290
Bram Moolenaar4770d092006-01-12 23:22:24 +000011291 fword_ends = (fword[sp->ts_fidx] == NUL
11292 || (soundfold
11293 ? vim_iswhite(fword[sp->ts_fidx])
11294 : !spell_iswordp(fword + sp->ts_fidx, curbuf)));
11295 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011296
Bram Moolenaar4770d092006-01-12 23:22:24 +000011297 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000011298 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011299 {
11300 /* There was a prefix before the word. Check that the prefix
11301 * can be used with this word. */
11302 /* Count the length of the NULs in the prefix. If there are
11303 * none this must be the first try without a prefix. */
11304 n = stack[sp->ts_prefixdepth].ts_arridx;
11305 len = pbyts[n++];
11306 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
11307 ;
11308 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011309 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011310 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011311 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011312 if (c == 0)
11313 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011314
Bram Moolenaar4770d092006-01-12 23:22:24 +000011315 /* Use the WF_RARE flag for a rare prefix. */
11316 if (c & WF_RAREPFX)
11317 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011318
Bram Moolenaar4770d092006-01-12 23:22:24 +000011319 /* Tricky: when checking for both prefix and compounding
11320 * we run into the prefix flag first.
11321 * Remember that it's OK, so that we accept the prefix
11322 * when arriving at a compound flag. */
11323 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011324 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011325 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011326
Bram Moolenaar4770d092006-01-12 23:22:24 +000011327 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
11328 * appending another compound word below. */
11329 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011330 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011331 goodword_ends = FALSE;
11332 else
11333 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011334
Bram Moolenaar4770d092006-01-12 23:22:24 +000011335 p = NULL;
11336 compound_ok = TRUE;
11337 if (sp->ts_complen > sp->ts_compsplit)
11338 {
11339 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000011340 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011341 /* There was a word before this word. When there was no
11342 * change in this word (it was correct) add the first word
11343 * as a suggestion. If this word was corrected too, we
11344 * need to check if a correct word follows. */
11345 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000011346 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000011347 && STRNCMP(fword + sp->ts_splitfidx,
11348 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000011349 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011350 {
11351 preword[sp->ts_prewordlen] = NUL;
11352 newscore = score_wordcount_adj(slang, sp->ts_score,
11353 preword + sp->ts_prewordlen,
11354 sp->ts_prewordlen > 0);
11355 /* Add the suggestion if the score isn't too bad. */
11356 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000011357 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011358 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011359 newscore, 0, FALSE,
11360 lp->lp_sallang, FALSE);
11361 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000011362 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011363 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000011364 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000011365 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011366 /* There was a compound word before this word. If this
11367 * word does not support compounding then give up
11368 * (splitting is tried for the word without compound
11369 * flag). */
11370 if (((unsigned)flags >> 24) == 0
11371 || sp->ts_twordlen - sp->ts_splitoff
11372 < slang->sl_compminlen)
11373 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011374#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011375 /* For multi-byte chars check character length against
11376 * COMPOUNDMIN. */
11377 if (has_mbyte
11378 && slang->sl_compminlen > 0
11379 && mb_charlen(tword + sp->ts_splitoff)
11380 < slang->sl_compminlen)
11381 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011382#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000011383
Bram Moolenaar4770d092006-01-12 23:22:24 +000011384 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11385 compflags[sp->ts_complen + 1] = NUL;
11386 vim_strncpy(preword + sp->ts_prewordlen,
11387 tword + sp->ts_splitoff,
11388 sp->ts_twordlen - sp->ts_splitoff);
11389 p = preword;
11390 while (*skiptowhite(p) != NUL)
11391 p = skipwhite(skiptowhite(p));
11392 if (fword_ends && !can_compound(slang, p,
11393 compflags + sp->ts_compsplit))
11394 /* Compound is not allowed. But it may still be
11395 * possible if we add another (short) word. */
11396 compound_ok = FALSE;
11397
11398 /* Get pointer to last char of previous word. */
11399 p = preword + sp->ts_prewordlen;
11400 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011401 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011402 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011403
Bram Moolenaar4770d092006-01-12 23:22:24 +000011404 /*
11405 * Form the word with proper case in preword.
11406 * If there is a word from a previous split, append.
11407 * For the soundfold tree don't change the case, simply append.
11408 */
11409 if (soundfold)
11410 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11411 else if (flags & WF_KEEPCAP)
11412 /* Must find the word in the keep-case tree. */
11413 find_keepcap_word(slang, tword + sp->ts_splitoff,
11414 preword + sp->ts_prewordlen);
11415 else
11416 {
11417 /* Include badflags: If the badword is onecap or allcap
11418 * use that for the goodword too. But if the badword is
11419 * allcap and it's only one char long use onecap. */
11420 c = su->su_badflags;
11421 if ((c & WF_ALLCAP)
11422#ifdef FEAT_MBYTE
11423 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11424#else
11425 && su->su_badlen == 1
11426#endif
11427 )
11428 c = WF_ONECAP;
11429 c |= flags;
11430
11431 /* When appending a compound word after a word character don't
11432 * use Onecap. */
11433 if (p != NULL && spell_iswordp_nmw(p))
11434 c &= ~WF_ONECAP;
11435 make_case_word(tword + sp->ts_splitoff,
11436 preword + sp->ts_prewordlen, c);
11437 }
11438
11439 if (!soundfold)
11440 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011441 /* Don't use a banned word. It may appear again as a good
11442 * word, thus remember it. */
11443 if (flags & WF_BANNED)
11444 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000011445 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011446 break;
11447 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011448 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000011449 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11450 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011451 {
11452 if (slang->sl_compprog == NULL)
11453 break;
11454 /* the word so far was banned but we may try compounding */
11455 goodword_ends = FALSE;
11456 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011457 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011458
Bram Moolenaar4770d092006-01-12 23:22:24 +000011459 newscore = 0;
11460 if (!soundfold) /* soundfold words don't have flags */
11461 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011462 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011463 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011464 newscore += SCORE_REGION;
11465 if (flags & WF_RARE)
11466 newscore += SCORE_RARE;
11467
Bram Moolenaar0c405862005-06-22 22:26:26 +000011468 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011469 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011470 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011471 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011472
Bram Moolenaar4770d092006-01-12 23:22:24 +000011473 /* TODO: how about splitting in the soundfold tree? */
11474 if (fword_ends
11475 && goodword_ends
11476 && sp->ts_fidx >= sp->ts_fidxtry
11477 && compound_ok)
11478 {
11479 /* The badword also ends: add suggestions. */
11480#ifdef DEBUG_TRIEWALK
11481 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011482 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011483 int j;
11484
11485 /* print the stack of changes that brought us here */
11486 smsg("------ %s -------", fword);
11487 for (j = 0; j < depth; ++j)
11488 smsg("%s", changename[j]);
11489 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011490#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011491 if (soundfold)
11492 {
11493 /* For soundfolded words we need to find the original
11494 * words, the edit distrance and then add them. */
11495 add_sound_suggest(su, preword, sp->ts_score, lp);
11496 }
11497 else
11498 {
11499 /* Give a penalty when changing non-word char to word
11500 * char, e.g., "thes," -> "these". */
11501 p = fword + sp->ts_fidx;
11502 mb_ptr_back(fword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011503 if (!spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011504 {
11505 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011506 mb_ptr_back(preword, p);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011507 if (spell_iswordp(p, curbuf))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011508 newscore += SCORE_NONWORD;
11509 }
11510
Bram Moolenaar4770d092006-01-12 23:22:24 +000011511 /* Give a bonus to words seen before. */
11512 score = score_wordcount_adj(slang,
11513 sp->ts_score + newscore,
11514 preword + sp->ts_prewordlen,
11515 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011516
Bram Moolenaar4770d092006-01-12 23:22:24 +000011517 /* Add the suggestion if the score isn't too bad. */
11518 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011519 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011520 add_suggestion(su, &su->su_ga, preword,
11521 sp->ts_fidx - repextra,
11522 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011523
11524 if (su->su_badflags & WF_MIXCAP)
11525 {
11526 /* We really don't know if the word should be
11527 * upper or lower case, add both. */
11528 c = captype(preword, NULL);
11529 if (c == 0 || c == WF_ALLCAP)
11530 {
11531 make_case_word(tword + sp->ts_splitoff,
11532 preword + sp->ts_prewordlen,
11533 c == 0 ? WF_ALLCAP : 0);
11534
11535 add_suggestion(su, &su->su_ga, preword,
11536 sp->ts_fidx - repextra,
11537 score + SCORE_ICASE, 0, FALSE,
11538 lp->lp_sallang, FALSE);
11539 }
11540 }
11541 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011542 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011543 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011544
Bram Moolenaar4770d092006-01-12 23:22:24 +000011545 /*
11546 * Try word split and/or compounding.
11547 */
11548 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011549#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011550 /* Don't split halfway a character. */
11551 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011552#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011553 )
11554 {
11555 int try_compound;
11556 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011557
Bram Moolenaar4770d092006-01-12 23:22:24 +000011558 /* If past the end of the bad word don't try a split.
11559 * Otherwise try changing the next word. E.g., find
11560 * suggestions for "the the" where the second "the" is
11561 * different. It's done like a split.
11562 * TODO: word split for soundfold words */
11563 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11564 && !soundfold;
11565
11566 /* Get here in several situations:
11567 * 1. The word in the tree ends:
11568 * If the word allows compounding try that. Otherwise try
11569 * a split by inserting a space. For both check that a
11570 * valid words starts at fword[sp->ts_fidx].
11571 * For NOBREAK do like compounding to be able to check if
11572 * the next word is valid.
11573 * 2. The badword does end, but it was due to a change (e.g.,
11574 * a swap). No need to split, but do check that the
11575 * following word is valid.
11576 * 3. The badword and the word in the tree end. It may still
11577 * be possible to compound another (short) word.
11578 */
11579 try_compound = FALSE;
11580 if (!soundfold
11581 && slang->sl_compprog != NULL
11582 && ((unsigned)flags >> 24) != 0
11583 && sp->ts_twordlen - sp->ts_splitoff
11584 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011585#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011586 && (!has_mbyte
11587 || slang->sl_compminlen == 0
11588 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011589 >= slang->sl_compminlen)
11590#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011591 && (slang->sl_compsylmax < MAXWLEN
11592 || sp->ts_complen + 1 - sp->ts_compsplit
11593 < slang->sl_compmax)
11594 && (byte_in_str(sp->ts_complen == sp->ts_compsplit
11595 ? slang->sl_compstartflags
11596 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +000011597 ((unsigned)flags >> 24))))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011598 {
11599 try_compound = TRUE;
11600 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11601 compflags[sp->ts_complen + 1] = NUL;
11602 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011603
Bram Moolenaar4770d092006-01-12 23:22:24 +000011604 /* For NOBREAK we never try splitting, it won't make any word
11605 * valid. */
11606 if (slang->sl_nobreak)
11607 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011608
Bram Moolenaar4770d092006-01-12 23:22:24 +000011609 /* If we could add a compound word, and it's also possible to
11610 * split at this point, do the split first and set
11611 * TSF_DIDSPLIT to avoid doing it again. */
11612 else if (!fword_ends
11613 && try_compound
11614 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11615 {
11616 try_compound = FALSE;
11617 sp->ts_flags |= TSF_DIDSPLIT;
11618 --sp->ts_curi; /* do the same NUL again */
11619 compflags[sp->ts_complen] = NUL;
11620 }
11621 else
11622 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011623
Bram Moolenaar4770d092006-01-12 23:22:24 +000011624 if (try_split || try_compound)
11625 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011626 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011627 {
11628 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011629 * words so far are valid for compounding. If there
11630 * is only one word it must not have the NEEDCOMPOUND
11631 * flag. */
11632 if (sp->ts_complen == sp->ts_compsplit
11633 && (flags & WF_NEEDCOMP))
11634 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011635 p = preword;
11636 while (*skiptowhite(p) != NUL)
11637 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011638 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011639 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011640 compflags + sp->ts_compsplit))
11641 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011642
11643 if (slang->sl_nosplitsugs)
11644 newscore += SCORE_SPLIT_NO;
11645 else
11646 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011647
11648 /* Give a bonus to words seen before. */
11649 newscore = score_wordcount_adj(slang, newscore,
11650 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011651 }
11652
Bram Moolenaar4770d092006-01-12 23:22:24 +000011653 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011654 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011655 go_deeper(stack, depth, newscore);
11656#ifdef DEBUG_TRIEWALK
11657 if (!try_compound && !fword_ends)
11658 sprintf(changename[depth], "%.*s-%s: split",
11659 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11660 else
11661 sprintf(changename[depth], "%.*s-%s: compound",
11662 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11663#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011664 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011665 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011666 sp->ts_state = STATE_SPLITUNDO;
11667
11668 ++depth;
11669 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011670
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011671 /* Append a space to preword when splitting. */
11672 if (!try_compound && !fword_ends)
11673 STRCAT(preword, " ");
Bram Moolenaar5195e452005-08-19 20:32:47 +000011674 sp->ts_prewordlen = STRLEN(preword);
11675 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011676 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011677
11678 /* If the badword has a non-word character at this
11679 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011680 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011681 * character when the word ends. But only when the
11682 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011683 if (((!try_compound && !spell_iswordp_nmw(fword
11684 + sp->ts_fidx))
11685 || fword_ends)
11686 && fword[sp->ts_fidx] != NUL
11687 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011688 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011689 int l;
11690
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011691#ifdef FEAT_MBYTE
11692 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011693 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011694 else
11695#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011696 l = 1;
11697 if (fword_ends)
11698 {
11699 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011700 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011701 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011702 sp->ts_prewordlen += l;
11703 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011704 }
11705 else
11706 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11707 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011708 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011709
Bram Moolenaard12a1322005-08-21 22:08:24 +000011710 /* When compounding include compound flag in
11711 * compflags[] (already set above). When splitting we
11712 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011713 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011714 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011715 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011716 sp->ts_compsplit = sp->ts_complen;
11717 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011718
Bram Moolenaar53805d12005-08-01 07:08:33 +000011719 /* set su->su_badflags to the caps type at this
11720 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011721#ifdef FEAT_MBYTE
11722 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011723 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011724 else
11725#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011726 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011727 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011728 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011729
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011730 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011731 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011732
11733 /* If there are postponed prefixes, try these too. */
11734 if (pbyts != NULL)
11735 {
11736 byts = pbyts;
11737 idxs = pidxs;
11738 sp->ts_prefixdepth = PFD_PREFIXTREE;
11739 sp->ts_state = STATE_NOPREFIX;
11740 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011741 }
11742 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011743 }
11744 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011745
Bram Moolenaar4770d092006-01-12 23:22:24 +000011746 case STATE_SPLITUNDO:
11747 /* Undo the changes done for word split or compound word. */
11748 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011749
Bram Moolenaar4770d092006-01-12 23:22:24 +000011750 /* Continue looking for NUL bytes. */
11751 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011752
Bram Moolenaar4770d092006-01-12 23:22:24 +000011753 /* In case we went into the prefix tree. */
11754 byts = fbyts;
11755 idxs = fidxs;
11756 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011757
Bram Moolenaar4770d092006-01-12 23:22:24 +000011758 case STATE_ENDNUL:
11759 /* Past the NUL bytes in the node. */
11760 su->su_badflags = sp->ts_save_badflags;
11761 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011762#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011763 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011764#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011765 )
11766 {
11767 /* The badword ends, can't use STATE_PLAIN. */
11768 sp->ts_state = STATE_DEL;
11769 break;
11770 }
11771 sp->ts_state = STATE_PLAIN;
11772 /*FALLTHROUGH*/
11773
11774 case STATE_PLAIN:
11775 /*
11776 * Go over all possible bytes at this node, add each to tword[]
11777 * and use child node. "ts_curi" is the index.
11778 */
11779 arridx = sp->ts_arridx;
11780 if (sp->ts_curi > byts[arridx])
11781 {
11782 /* Done all bytes at this node, do next state. When still at
11783 * already changed bytes skip the other tricks. */
11784 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011785 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011786 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011787 sp->ts_state = STATE_FINAL;
11788 }
11789 else
11790 {
11791 arridx += sp->ts_curi++;
11792 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011793
Bram Moolenaar4770d092006-01-12 23:22:24 +000011794 /* Normal byte, go one level deeper. If it's not equal to the
11795 * byte in the bad word adjust the score. But don't even try
11796 * when the byte was already changed. And don't try when we
11797 * just deleted this byte, accepting it is always cheaper then
11798 * delete + substitute. */
11799 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000011800#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011801 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011802#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011803 )
11804 newscore = 0;
11805 else
11806 newscore = SCORE_SUBST;
11807 if ((newscore == 0
11808 || (sp->ts_fidx >= sp->ts_fidxtry
11809 && ((sp->ts_flags & TSF_DIDDEL) == 0
11810 || c != fword[sp->ts_delidx])))
11811 && TRY_DEEPER(su, stack, depth, newscore))
11812 {
11813 go_deeper(stack, depth, newscore);
11814#ifdef DEBUG_TRIEWALK
11815 if (newscore > 0)
11816 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
11817 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11818 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011819 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011820 sprintf(changename[depth], "%.*s-%s: accept %c",
11821 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11822 fword[sp->ts_fidx]);
11823#endif
11824 ++depth;
11825 sp = &stack[depth];
11826 ++sp->ts_fidx;
11827 tword[sp->ts_twordlen++] = c;
11828 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000011829#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011830 if (newscore == SCORE_SUBST)
11831 sp->ts_isdiff = DIFF_YES;
11832 if (has_mbyte)
11833 {
11834 /* Multi-byte characters are a bit complicated to
11835 * handle: They differ when any of the bytes differ
11836 * and then their length may also differ. */
11837 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011838 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011839 /* First byte. */
11840 sp->ts_tcharidx = 0;
11841 sp->ts_tcharlen = MB_BYTE2LEN(c);
11842 sp->ts_fcharstart = sp->ts_fidx - 1;
11843 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011844 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011845 }
11846 else if (sp->ts_isdiff == DIFF_INSERT)
11847 /* When inserting trail bytes don't advance in the
11848 * bad word. */
11849 --sp->ts_fidx;
11850 if (++sp->ts_tcharidx == sp->ts_tcharlen)
11851 {
11852 /* Last byte of character. */
11853 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000011854 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011855 /* Correct ts_fidx for the byte length of the
11856 * character (we didn't check that before). */
11857 sp->ts_fidx = sp->ts_fcharstart
11858 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000011859 fword[sp->ts_fcharstart]);
11860
Bram Moolenaar4770d092006-01-12 23:22:24 +000011861 /* For changing a composing character adjust
11862 * the score from SCORE_SUBST to
11863 * SCORE_SUBCOMP. */
11864 if (enc_utf8
11865 && utf_iscomposing(
11866 mb_ptr2char(tword
11867 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011868 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011869 && utf_iscomposing(
11870 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011871 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011872 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011873 SCORE_SUBST - SCORE_SUBCOMP;
11874
Bram Moolenaar4770d092006-01-12 23:22:24 +000011875 /* For a similar character adjust score from
11876 * SCORE_SUBST to SCORE_SIMILAR. */
11877 else if (!soundfold
11878 && slang->sl_has_map
11879 && similar_chars(slang,
11880 mb_ptr2char(tword
11881 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000011882 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000011883 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000011884 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011885 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000011886 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000011887 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011888 else if (sp->ts_isdiff == DIFF_INSERT
11889 && sp->ts_twordlen > sp->ts_tcharlen)
11890 {
11891 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
11892 c = mb_ptr2char(p);
11893 if (enc_utf8 && utf_iscomposing(c))
11894 {
11895 /* Inserting a composing char doesn't
11896 * count that much. */
11897 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
11898 }
11899 else
11900 {
11901 /* If the previous character was the same,
11902 * thus doubling a character, give a bonus
11903 * to the score. Also for the soundfold
11904 * tree (might seem illogical but does
11905 * give better scores). */
11906 mb_ptr_back(tword, p);
11907 if (c == mb_ptr2char(p))
11908 sp->ts_score -= SCORE_INS
11909 - SCORE_INSDUP;
11910 }
11911 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011912
Bram Moolenaar4770d092006-01-12 23:22:24 +000011913 /* Starting a new char, reset the length. */
11914 sp->ts_tcharlen = 0;
11915 }
Bram Moolenaarea408852005-06-25 22:49:46 +000011916 }
Bram Moolenaarea424162005-06-16 21:51:00 +000011917 else
11918#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000011919 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011920 /* If we found a similar char adjust the score.
11921 * We do this after calling go_deeper() because
11922 * it's slow. */
11923 if (newscore != 0
11924 && !soundfold
11925 && slang->sl_has_map
11926 && similar_chars(slang,
11927 c, fword[sp->ts_fidx - 1]))
11928 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000011929 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011930 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011931 }
11932 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011933
Bram Moolenaar4770d092006-01-12 23:22:24 +000011934 case STATE_DEL:
11935#ifdef FEAT_MBYTE
11936 /* When past the first byte of a multi-byte char don't try
11937 * delete/insert/swap a character. */
11938 if (has_mbyte && sp->ts_tcharlen > 0)
11939 {
11940 sp->ts_state = STATE_FINAL;
11941 break;
11942 }
11943#endif
11944 /*
11945 * Try skipping one character in the bad word (delete it).
11946 */
11947 sp->ts_state = STATE_INS_PREP;
11948 sp->ts_curi = 1;
11949 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
11950 /* Deleting a vowel at the start of a word counts less, see
11951 * soundalike_score(). */
11952 newscore = 2 * SCORE_DEL / 3;
11953 else
11954 newscore = SCORE_DEL;
11955 if (fword[sp->ts_fidx] != NUL
11956 && TRY_DEEPER(su, stack, depth, newscore))
11957 {
11958 go_deeper(stack, depth, newscore);
11959#ifdef DEBUG_TRIEWALK
11960 sprintf(changename[depth], "%.*s-%s: delete %c",
11961 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11962 fword[sp->ts_fidx]);
11963#endif
11964 ++depth;
11965
11966 /* Remember what character we deleted, so that we can avoid
11967 * inserting it again. */
11968 stack[depth].ts_flags |= TSF_DIDDEL;
11969 stack[depth].ts_delidx = sp->ts_fidx;
11970
11971 /* Advance over the character in fword[]. Give a bonus to the
11972 * score if the same character is following "nn" -> "n". It's
11973 * a bit illogical for soundfold tree but it does give better
11974 * results. */
11975#ifdef FEAT_MBYTE
11976 if (has_mbyte)
11977 {
11978 c = mb_ptr2char(fword + sp->ts_fidx);
11979 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
11980 if (enc_utf8 && utf_iscomposing(c))
11981 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
11982 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
11983 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
11984 }
11985 else
11986#endif
11987 {
11988 ++stack[depth].ts_fidx;
11989 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
11990 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
11991 }
11992 break;
11993 }
11994 /*FALLTHROUGH*/
11995
11996 case STATE_INS_PREP:
11997 if (sp->ts_flags & TSF_DIDDEL)
11998 {
11999 /* If we just deleted a byte then inserting won't make sense,
12000 * a substitute is always cheaper. */
12001 sp->ts_state = STATE_SWAP;
12002 break;
12003 }
12004
12005 /* skip over NUL bytes */
12006 n = sp->ts_arridx;
12007 for (;;)
12008 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012009 if (sp->ts_curi > byts[n])
12010 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012011 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012012 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012013 break;
12014 }
12015 if (byts[n + sp->ts_curi] != NUL)
12016 {
12017 /* Found a byte to insert. */
12018 sp->ts_state = STATE_INS;
12019 break;
12020 }
12021 ++sp->ts_curi;
12022 }
12023 break;
12024
12025 /*FALLTHROUGH*/
12026
12027 case STATE_INS:
12028 /* Insert one byte. Repeat this for each possible byte at this
12029 * node. */
12030 n = sp->ts_arridx;
12031 if (sp->ts_curi > byts[n])
12032 {
12033 /* Done all bytes at this node, go to next state. */
12034 sp->ts_state = STATE_SWAP;
12035 break;
12036 }
12037
12038 /* Do one more byte at this node, but:
12039 * - Skip NUL bytes.
12040 * - Skip the byte if it's equal to the byte in the word,
12041 * accepting that byte is always better.
12042 */
12043 n += sp->ts_curi++;
12044 c = byts[n];
12045 if (soundfold && sp->ts_twordlen == 0 && c == '*')
12046 /* Inserting a vowel at the start of a word counts less,
12047 * see soundalike_score(). */
12048 newscore = 2 * SCORE_INS / 3;
12049 else
12050 newscore = SCORE_INS;
12051 if (c != fword[sp->ts_fidx]
12052 && TRY_DEEPER(su, stack, depth, newscore))
12053 {
12054 go_deeper(stack, depth, newscore);
12055#ifdef DEBUG_TRIEWALK
12056 sprintf(changename[depth], "%.*s-%s: insert %c",
12057 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12058 c);
12059#endif
12060 ++depth;
12061 sp = &stack[depth];
12062 tword[sp->ts_twordlen++] = c;
12063 sp->ts_arridx = idxs[n];
12064#ifdef FEAT_MBYTE
12065 if (has_mbyte)
12066 {
12067 fl = MB_BYTE2LEN(c);
12068 if (fl > 1)
12069 {
12070 /* There are following bytes for the same character.
12071 * We must find all bytes before trying
12072 * delete/insert/swap/etc. */
12073 sp->ts_tcharlen = fl;
12074 sp->ts_tcharidx = 1;
12075 sp->ts_isdiff = DIFF_INSERT;
12076 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012077 }
12078 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012079 fl = 1;
12080 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000012081#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012082 {
12083 /* If the previous character was the same, thus doubling a
12084 * character, give a bonus to the score. Also for
12085 * soundfold words (illogical but does give a better
12086 * score). */
12087 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000012088 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012089 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012090 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012091 }
12092 break;
12093
12094 case STATE_SWAP:
12095 /*
12096 * Swap two bytes in the bad word: "12" -> "21".
12097 * We change "fword" here, it's changed back afterwards at
12098 * STATE_UNSWAP.
12099 */
12100 p = fword + sp->ts_fidx;
12101 c = *p;
12102 if (c == NUL)
12103 {
12104 /* End of word, can't swap or replace. */
12105 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012106 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012107 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012108
Bram Moolenaar4770d092006-01-12 23:22:24 +000012109 /* Don't swap if the first character is not a word character.
12110 * SWAP3 etc. also don't make sense then. */
12111 if (!soundfold && !spell_iswordp(p, curbuf))
12112 {
12113 sp->ts_state = STATE_REP_INI;
12114 break;
12115 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012116
Bram Moolenaar4770d092006-01-12 23:22:24 +000012117#ifdef FEAT_MBYTE
12118 if (has_mbyte)
12119 {
12120 n = mb_cptr2len(p);
12121 c = mb_ptr2char(p);
12122 if (!soundfold && !spell_iswordp(p + n, curbuf))
12123 c2 = c; /* don't swap non-word char */
12124 else
12125 c2 = mb_ptr2char(p + n);
12126 }
12127 else
12128#endif
12129 {
12130 if (!soundfold && !spell_iswordp(p + 1, curbuf))
12131 c2 = c; /* don't swap non-word char */
12132 else
12133 c2 = p[1];
12134 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012135
Bram Moolenaar4770d092006-01-12 23:22:24 +000012136 /* When characters are identical, swap won't do anything.
12137 * Also get here if the second char is not a word character. */
12138 if (c == c2)
12139 {
12140 sp->ts_state = STATE_SWAP3;
12141 break;
12142 }
12143 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
12144 {
12145 go_deeper(stack, depth, SCORE_SWAP);
12146#ifdef DEBUG_TRIEWALK
12147 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
12148 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12149 c, c2);
12150#endif
12151 sp->ts_state = STATE_UNSWAP;
12152 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012153#ifdef FEAT_MBYTE
12154 if (has_mbyte)
12155 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012156 fl = mb_char2len(c2);
12157 mch_memmove(p, p + n, fl);
12158 mb_char2bytes(c, p + fl);
12159 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012160 }
12161 else
12162#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012163 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012164 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012165 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012166 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012167 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012168 }
12169 else
12170 /* If this swap doesn't work then SWAP3 won't either. */
12171 sp->ts_state = STATE_REP_INI;
12172 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000012173
Bram Moolenaar4770d092006-01-12 23:22:24 +000012174 case STATE_UNSWAP:
12175 /* Undo the STATE_SWAP swap: "21" -> "12". */
12176 p = fword + sp->ts_fidx;
12177#ifdef FEAT_MBYTE
12178 if (has_mbyte)
12179 {
12180 n = MB_BYTE2LEN(*p);
12181 c = mb_ptr2char(p + n);
12182 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
12183 mb_char2bytes(c, p);
12184 }
12185 else
12186#endif
12187 {
12188 c = *p;
12189 *p = p[1];
12190 p[1] = c;
12191 }
12192 /*FALLTHROUGH*/
12193
12194 case STATE_SWAP3:
12195 /* Swap two bytes, skipping one: "123" -> "321". We change
12196 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
12197 p = fword + sp->ts_fidx;
12198#ifdef FEAT_MBYTE
12199 if (has_mbyte)
12200 {
12201 n = mb_cptr2len(p);
12202 c = mb_ptr2char(p);
12203 fl = mb_cptr2len(p + n);
12204 c2 = mb_ptr2char(p + n);
12205 if (!soundfold && !spell_iswordp(p + n + fl, curbuf))
12206 c3 = c; /* don't swap non-word char */
12207 else
12208 c3 = mb_ptr2char(p + n + fl);
12209 }
12210 else
12211#endif
12212 {
12213 c = *p;
12214 c2 = p[1];
12215 if (!soundfold && !spell_iswordp(p + 2, curbuf))
12216 c3 = c; /* don't swap non-word char */
12217 else
12218 c3 = p[2];
12219 }
12220
12221 /* When characters are identical: "121" then SWAP3 result is
12222 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
12223 * same as SWAP on next char: "112". Thus skip all swapping.
12224 * Also skip when c3 is NUL.
12225 * Also get here when the third character is not a word character.
12226 * Second character may any char: "a.b" -> "b.a" */
12227 if (c == c3 || c3 == NUL)
12228 {
12229 sp->ts_state = STATE_REP_INI;
12230 break;
12231 }
12232 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12233 {
12234 go_deeper(stack, depth, SCORE_SWAP3);
12235#ifdef DEBUG_TRIEWALK
12236 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
12237 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12238 c, c3);
12239#endif
12240 sp->ts_state = STATE_UNSWAP3;
12241 ++depth;
12242#ifdef FEAT_MBYTE
12243 if (has_mbyte)
12244 {
12245 tl = mb_char2len(c3);
12246 mch_memmove(p, p + n + fl, tl);
12247 mb_char2bytes(c2, p + tl);
12248 mb_char2bytes(c, p + fl + tl);
12249 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
12250 }
12251 else
12252#endif
12253 {
12254 p[0] = p[2];
12255 p[2] = c;
12256 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12257 }
12258 }
12259 else
12260 sp->ts_state = STATE_REP_INI;
12261 break;
12262
12263 case STATE_UNSWAP3:
12264 /* Undo STATE_SWAP3: "321" -> "123" */
12265 p = fword + sp->ts_fidx;
12266#ifdef FEAT_MBYTE
12267 if (has_mbyte)
12268 {
12269 n = MB_BYTE2LEN(*p);
12270 c2 = mb_ptr2char(p + n);
12271 fl = MB_BYTE2LEN(p[n]);
12272 c = mb_ptr2char(p + n + fl);
12273 tl = MB_BYTE2LEN(p[n + fl]);
12274 mch_memmove(p + fl + tl, p, n);
12275 mb_char2bytes(c, p);
12276 mb_char2bytes(c2, p + tl);
12277 p = p + tl;
12278 }
12279 else
12280#endif
12281 {
12282 c = *p;
12283 *p = p[2];
12284 p[2] = c;
12285 ++p;
12286 }
12287
12288 if (!soundfold && !spell_iswordp(p, curbuf))
12289 {
12290 /* Middle char is not a word char, skip the rotate. First and
12291 * third char were already checked at swap and swap3. */
12292 sp->ts_state = STATE_REP_INI;
12293 break;
12294 }
12295
12296 /* Rotate three characters left: "123" -> "231". We change
12297 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
12298 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12299 {
12300 go_deeper(stack, depth, SCORE_SWAP3);
12301#ifdef DEBUG_TRIEWALK
12302 p = fword + sp->ts_fidx;
12303 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
12304 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12305 p[0], p[1], p[2]);
12306#endif
12307 sp->ts_state = STATE_UNROT3L;
12308 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012309 p = fword + sp->ts_fidx;
12310#ifdef FEAT_MBYTE
12311 if (has_mbyte)
12312 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012313 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012314 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012315 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012316 fl += mb_cptr2len(p + n + fl);
12317 mch_memmove(p, p + n, fl);
12318 mb_char2bytes(c, p + fl);
12319 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012320 }
12321 else
12322#endif
12323 {
12324 c = *p;
12325 *p = p[1];
12326 p[1] = p[2];
12327 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012328 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000012329 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012330 }
12331 else
12332 sp->ts_state = STATE_REP_INI;
12333 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012334
Bram Moolenaar4770d092006-01-12 23:22:24 +000012335 case STATE_UNROT3L:
12336 /* Undo ROT3L: "231" -> "123" */
12337 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000012338#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012339 if (has_mbyte)
12340 {
12341 n = MB_BYTE2LEN(*p);
12342 n += MB_BYTE2LEN(p[n]);
12343 c = mb_ptr2char(p + n);
12344 tl = MB_BYTE2LEN(p[n]);
12345 mch_memmove(p + tl, p, n);
12346 mb_char2bytes(c, p);
12347 }
12348 else
Bram Moolenaarea424162005-06-16 21:51:00 +000012349#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012350 {
12351 c = p[2];
12352 p[2] = p[1];
12353 p[1] = *p;
12354 *p = c;
12355 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012356
Bram Moolenaar4770d092006-01-12 23:22:24 +000012357 /* Rotate three bytes right: "123" -> "312". We change "fword"
12358 * here, it's changed back afterwards at STATE_UNROT3R. */
12359 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12360 {
12361 go_deeper(stack, depth, SCORE_SWAP3);
12362#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012363 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012364 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
12365 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12366 p[0], p[1], p[2]);
12367#endif
12368 sp->ts_state = STATE_UNROT3R;
12369 ++depth;
12370 p = fword + sp->ts_fidx;
12371#ifdef FEAT_MBYTE
12372 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012373 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012374 n = mb_cptr2len(p);
12375 n += mb_cptr2len(p + n);
12376 c = mb_ptr2char(p + n);
12377 tl = mb_cptr2len(p + n);
12378 mch_memmove(p + tl, p, n);
12379 mb_char2bytes(c, p);
12380 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012381 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012382 else
12383#endif
12384 {
12385 c = p[2];
12386 p[2] = p[1];
12387 p[1] = *p;
12388 *p = c;
12389 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12390 }
12391 }
12392 else
12393 sp->ts_state = STATE_REP_INI;
12394 break;
12395
12396 case STATE_UNROT3R:
12397 /* Undo ROT3R: "312" -> "123" */
12398 p = fword + sp->ts_fidx;
12399#ifdef FEAT_MBYTE
12400 if (has_mbyte)
12401 {
12402 c = mb_ptr2char(p);
12403 tl = MB_BYTE2LEN(*p);
12404 n = MB_BYTE2LEN(p[tl]);
12405 n += MB_BYTE2LEN(p[tl + n]);
12406 mch_memmove(p, p + tl, n);
12407 mb_char2bytes(c, p + n);
12408 }
12409 else
12410#endif
12411 {
12412 c = *p;
12413 *p = p[1];
12414 p[1] = p[2];
12415 p[2] = c;
12416 }
12417 /*FALLTHROUGH*/
12418
12419 case STATE_REP_INI:
12420 /* Check if matching with REP items from the .aff file would work.
12421 * Quickly skip if:
12422 * - there are no REP items and we are not in the soundfold trie
12423 * - the score is going to be too high anyway
12424 * - already applied a REP item or swapped here */
12425 if ((lp->lp_replang == NULL && !soundfold)
12426 || sp->ts_score + SCORE_REP >= su->su_maxscore
12427 || sp->ts_fidx < sp->ts_fidxtry)
12428 {
12429 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012430 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012431 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012432
Bram Moolenaar4770d092006-01-12 23:22:24 +000012433 /* Use the first byte to quickly find the first entry that may
12434 * match. If the index is -1 there is none. */
12435 if (soundfold)
12436 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12437 else
12438 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012439
Bram Moolenaar4770d092006-01-12 23:22:24 +000012440 if (sp->ts_curi < 0)
12441 {
12442 sp->ts_state = STATE_FINAL;
12443 break;
12444 }
12445
12446 sp->ts_state = STATE_REP;
12447 /*FALLTHROUGH*/
12448
12449 case STATE_REP:
12450 /* Try matching with REP items from the .aff file. For each match
12451 * replace the characters and check if the resulting word is
12452 * valid. */
12453 p = fword + sp->ts_fidx;
12454
12455 if (soundfold)
12456 gap = &slang->sl_repsal;
12457 else
12458 gap = &lp->lp_replang->sl_rep;
12459 while (sp->ts_curi < gap->ga_len)
12460 {
12461 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12462 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012463 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012464 /* past possible matching entries */
12465 sp->ts_curi = gap->ga_len;
12466 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012467 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012468 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12469 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12470 {
12471 go_deeper(stack, depth, SCORE_REP);
12472#ifdef DEBUG_TRIEWALK
12473 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12474 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12475 ftp->ft_from, ftp->ft_to);
12476#endif
12477 /* Need to undo this afterwards. */
12478 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012479
Bram Moolenaar4770d092006-01-12 23:22:24 +000012480 /* Change the "from" to the "to" string. */
12481 ++depth;
12482 fl = STRLEN(ftp->ft_from);
12483 tl = STRLEN(ftp->ft_to);
12484 if (fl != tl)
12485 {
12486 mch_memmove(p + tl, p + fl, STRLEN(p + fl) + 1);
12487 repextra += tl - fl;
12488 }
12489 mch_memmove(p, ftp->ft_to, tl);
12490 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12491#ifdef FEAT_MBYTE
12492 stack[depth].ts_tcharlen = 0;
12493#endif
12494 break;
12495 }
12496 }
12497
12498 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
12499 /* No (more) matches. */
12500 sp->ts_state = STATE_FINAL;
12501
12502 break;
12503
12504 case STATE_REP_UNDO:
12505 /* Undo a REP replacement and continue with the next one. */
12506 if (soundfold)
12507 gap = &slang->sl_repsal;
12508 else
12509 gap = &lp->lp_replang->sl_rep;
12510 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
12511 fl = STRLEN(ftp->ft_from);
12512 tl = STRLEN(ftp->ft_to);
12513 p = fword + sp->ts_fidx;
12514 if (fl != tl)
12515 {
12516 mch_memmove(p + fl, p + tl, STRLEN(p + tl) + 1);
12517 repextra -= tl - fl;
12518 }
12519 mch_memmove(p, ftp->ft_from, fl);
12520 sp->ts_state = STATE_REP;
12521 break;
12522
12523 default:
12524 /* Did all possible states at this level, go up one level. */
12525 --depth;
12526
12527 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12528 {
12529 /* Continue in or go back to the prefix tree. */
12530 byts = pbyts;
12531 idxs = pidxs;
12532 }
12533
12534 /* Don't check for CTRL-C too often, it takes time. */
12535 if (--breakcheckcount == 0)
12536 {
12537 ui_breakcheck();
12538 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012539 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012540 }
12541 }
12542}
12543
Bram Moolenaar4770d092006-01-12 23:22:24 +000012544
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012545/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012546 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012547 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012548 static void
12549go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012550 trystate_T *stack;
12551 int depth;
12552 int score_add;
12553{
Bram Moolenaarea424162005-06-16 21:51:00 +000012554 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012555 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012556 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012557 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012558 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012559}
12560
Bram Moolenaar53805d12005-08-01 07:08:33 +000012561#ifdef FEAT_MBYTE
12562/*
12563 * Case-folding may change the number of bytes: Count nr of chars in
12564 * fword[flen] and return the byte length of that many chars in "word".
12565 */
12566 static int
12567nofold_len(fword, flen, word)
12568 char_u *fword;
12569 int flen;
12570 char_u *word;
12571{
12572 char_u *p;
12573 int i = 0;
12574
12575 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12576 ++i;
12577 for (p = word; i > 0; mb_ptr_adv(p))
12578 --i;
12579 return (int)(p - word);
12580}
12581#endif
12582
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012583/*
12584 * "fword" is a good word with case folded. Find the matching keep-case
12585 * words and put it in "kword".
12586 * Theoretically there could be several keep-case words that result in the
12587 * same case-folded word, but we only find one...
12588 */
12589 static void
12590find_keepcap_word(slang, fword, kword)
12591 slang_T *slang;
12592 char_u *fword;
12593 char_u *kword;
12594{
12595 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12596 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012597 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012598
12599 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012600 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012601 int round[MAXWLEN];
12602 int fwordidx[MAXWLEN];
12603 int uwordidx[MAXWLEN];
12604 int kwordlen[MAXWLEN];
12605
12606 int flen, ulen;
12607 int l;
12608 int len;
12609 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012610 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012611 char_u *p;
12612 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012613 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012614
12615 if (byts == NULL)
12616 {
12617 /* array is empty: "cannot happen" */
12618 *kword = NUL;
12619 return;
12620 }
12621
12622 /* Make an all-cap version of "fword". */
12623 allcap_copy(fword, uword);
12624
12625 /*
12626 * Each character needs to be tried both case-folded and upper-case.
12627 * All this gets very complicated if we keep in mind that changing case
12628 * may change the byte length of a multi-byte character...
12629 */
12630 depth = 0;
12631 arridx[0] = 0;
12632 round[0] = 0;
12633 fwordidx[0] = 0;
12634 uwordidx[0] = 0;
12635 kwordlen[0] = 0;
12636 while (depth >= 0)
12637 {
12638 if (fword[fwordidx[depth]] == NUL)
12639 {
12640 /* We are at the end of "fword". If the tree allows a word to end
12641 * here we have found a match. */
12642 if (byts[arridx[depth] + 1] == 0)
12643 {
12644 kword[kwordlen[depth]] = NUL;
12645 return;
12646 }
12647
12648 /* kword is getting too long, continue one level up */
12649 --depth;
12650 }
12651 else if (++round[depth] > 2)
12652 {
12653 /* tried both fold-case and upper-case character, continue one
12654 * level up */
12655 --depth;
12656 }
12657 else
12658 {
12659 /*
12660 * round[depth] == 1: Try using the folded-case character.
12661 * round[depth] == 2: Try using the upper-case character.
12662 */
12663#ifdef FEAT_MBYTE
12664 if (has_mbyte)
12665 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012666 flen = mb_cptr2len(fword + fwordidx[depth]);
12667 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012668 }
12669 else
12670#endif
12671 ulen = flen = 1;
12672 if (round[depth] == 1)
12673 {
12674 p = fword + fwordidx[depth];
12675 l = flen;
12676 }
12677 else
12678 {
12679 p = uword + uwordidx[depth];
12680 l = ulen;
12681 }
12682
12683 for (tryidx = arridx[depth]; l > 0; --l)
12684 {
12685 /* Perform a binary search in the list of accepted bytes. */
12686 len = byts[tryidx++];
12687 c = *p++;
12688 lo = tryidx;
12689 hi = tryidx + len - 1;
12690 while (lo < hi)
12691 {
12692 m = (lo + hi) / 2;
12693 if (byts[m] > c)
12694 hi = m - 1;
12695 else if (byts[m] < c)
12696 lo = m + 1;
12697 else
12698 {
12699 lo = hi = m;
12700 break;
12701 }
12702 }
12703
12704 /* Stop if there is no matching byte. */
12705 if (hi < lo || byts[lo] != c)
12706 break;
12707
12708 /* Continue at the child (if there is one). */
12709 tryidx = idxs[lo];
12710 }
12711
12712 if (l == 0)
12713 {
12714 /*
12715 * Found the matching char. Copy it to "kword" and go a
12716 * level deeper.
12717 */
12718 if (round[depth] == 1)
12719 {
12720 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12721 flen);
12722 kwordlen[depth + 1] = kwordlen[depth] + flen;
12723 }
12724 else
12725 {
12726 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12727 ulen);
12728 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12729 }
12730 fwordidx[depth + 1] = fwordidx[depth] + flen;
12731 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12732
12733 ++depth;
12734 arridx[depth] = tryidx;
12735 round[depth] = 0;
12736 }
12737 }
12738 }
12739
12740 /* Didn't find it: "cannot happen". */
12741 *kword = NUL;
12742}
12743
12744/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012745 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12746 * su->su_sga.
12747 */
12748 static void
12749score_comp_sal(su)
12750 suginfo_T *su;
12751{
12752 langp_T *lp;
12753 char_u badsound[MAXWLEN];
12754 int i;
12755 suggest_T *stp;
12756 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012757 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012758 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012759
12760 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
12761 return;
12762
12763 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012764 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012765 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012766 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012767 if (lp->lp_slang->sl_sal.ga_len > 0)
12768 {
12769 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012770 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012771
12772 for (i = 0; i < su->su_ga.ga_len; ++i)
12773 {
12774 stp = &SUG(su->su_ga, i);
12775
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012776 /* Case-fold the suggested word, sound-fold it and compute the
12777 * sound-a-like score. */
12778 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012779 if (score < SCORE_MAXMAX)
12780 {
12781 /* Add the suggestion. */
12782 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
12783 sstp->st_word = vim_strsave(stp->st_word);
12784 if (sstp->st_word != NULL)
12785 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012786 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012787 sstp->st_score = score;
12788 sstp->st_altscore = 0;
12789 sstp->st_orglen = stp->st_orglen;
12790 ++su->su_sga.ga_len;
12791 }
12792 }
12793 }
12794 break;
12795 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012796 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012797}
12798
12799/*
12800 * Combine the list of suggestions in su->su_ga and su->su_sga.
12801 * They are intwined.
12802 */
12803 static void
12804score_combine(su)
12805 suginfo_T *su;
12806{
12807 int i;
12808 int j;
12809 garray_T ga;
12810 garray_T *gap;
12811 langp_T *lp;
12812 suggest_T *stp;
12813 char_u *p;
12814 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012815 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012816 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012817 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012818
12819 /* Add the alternate score to su_ga. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012820 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012821 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000012822 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012823 if (lp->lp_slang->sl_sal.ga_len > 0)
12824 {
12825 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012826 slang = lp->lp_slang;
12827 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012828
12829 for (i = 0; i < su->su_ga.ga_len; ++i)
12830 {
12831 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012832 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012833 if (stp->st_altscore == SCORE_MAXMAX)
12834 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
12835 else
12836 stp->st_score = (stp->st_score * 3
12837 + stp->st_altscore) / 4;
12838 stp->st_salscore = FALSE;
12839 }
12840 break;
12841 }
12842 }
12843
Bram Moolenaar4770d092006-01-12 23:22:24 +000012844 if (slang == NULL) /* just in case */
12845 return;
12846
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012847 /* Add the alternate score to su_sga. */
12848 for (i = 0; i < su->su_sga.ga_len; ++i)
12849 {
12850 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012851 stp->st_altscore = spell_edit_score(slang,
12852 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012853 if (stp->st_score == SCORE_MAXMAX)
12854 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
12855 else
12856 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
12857 stp->st_salscore = TRUE;
12858 }
12859
Bram Moolenaar4770d092006-01-12 23:22:24 +000012860 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
12861 * for both lists. */
12862 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012863 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012864 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012865 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
12866
12867 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
12868 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
12869 return;
12870
12871 stp = &SUG(ga, 0);
12872 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
12873 {
12874 /* round 1: get a suggestion from su_ga
12875 * round 2: get a suggestion from su_sga */
12876 for (round = 1; round <= 2; ++round)
12877 {
12878 gap = round == 1 ? &su->su_ga : &su->su_sga;
12879 if (i < gap->ga_len)
12880 {
12881 /* Don't add a word if it's already there. */
12882 p = SUG(*gap, i).st_word;
12883 for (j = 0; j < ga.ga_len; ++j)
12884 if (STRCMP(stp[j].st_word, p) == 0)
12885 break;
12886 if (j == ga.ga_len)
12887 stp[ga.ga_len++] = SUG(*gap, i);
12888 else
12889 vim_free(p);
12890 }
12891 }
12892 }
12893
12894 ga_clear(&su->su_ga);
12895 ga_clear(&su->su_sga);
12896
12897 /* Truncate the list to the number of suggestions that will be displayed. */
12898 if (ga.ga_len > su->su_maxcount)
12899 {
12900 for (i = su->su_maxcount; i < ga.ga_len; ++i)
12901 vim_free(stp[i].st_word);
12902 ga.ga_len = su->su_maxcount;
12903 }
12904
12905 su->su_ga = ga;
12906}
12907
12908/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012909 * For the goodword in "stp" compute the soundalike score compared to the
12910 * badword.
12911 */
12912 static int
12913stp_sal_score(stp, su, slang, badsound)
12914 suggest_T *stp;
12915 suginfo_T *su;
12916 slang_T *slang;
12917 char_u *badsound; /* sound-folded badword */
12918{
12919 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012920 char_u *pbad;
12921 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012922 char_u badsound2[MAXWLEN];
12923 char_u fword[MAXWLEN];
12924 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012925 char_u goodword[MAXWLEN];
12926 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012927
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012928 lendiff = (int)(su->su_badlen - stp->st_orglen);
12929 if (lendiff >= 0)
12930 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012931 else
12932 {
12933 /* soundfold the bad word with more characters following */
12934 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
12935
12936 /* When joining two words the sound often changes a lot. E.g., "t he"
12937 * sounds like "t h" while "the" sounds like "@". Avoid that by
12938 * removing the space. Don't do it when the good word also contains a
12939 * space. */
12940 if (vim_iswhite(su->su_badptr[su->su_badlen])
12941 && *skiptowhite(stp->st_word) == NUL)
12942 for (p = fword; *(p = skiptowhite(p)) != NUL; )
12943 mch_memmove(p, p + 1, STRLEN(p));
12944
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012945 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012946 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012947 }
12948
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012949 if (lendiff > 0)
12950 {
12951 /* Add part of the bad word to the good word, so that we soundfold
12952 * what replaces the bad word. */
12953 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012954 vim_strncpy(goodword + stp->st_wordlen,
12955 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012956 pgood = goodword;
12957 }
12958 else
12959 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012960
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000012961 /* Sound-fold the word and compute the score for the difference. */
12962 spell_soundfold(slang, pgood, FALSE, goodsound);
12963
12964 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012965}
12966
Bram Moolenaar4770d092006-01-12 23:22:24 +000012967/* structure used to store soundfolded words that add_sound_suggest() has
12968 * handled already. */
12969typedef struct
12970{
12971 short sft_score; /* lowest score used */
12972 char_u sft_word[1]; /* soundfolded word, actually longer */
12973} sftword_T;
12974
12975static sftword_T dumsft;
12976#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
12977#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
12978
12979/*
12980 * Prepare for calling suggest_try_soundalike().
12981 */
12982 static void
12983suggest_try_soundalike_prep()
12984{
12985 langp_T *lp;
12986 int lpi;
12987 slang_T *slang;
12988
12989 /* Do this for all languages that support sound folding and for which a
12990 * .sug file has been loaded. */
12991 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
12992 {
12993 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
12994 slang = lp->lp_slang;
12995 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
12996 /* prepare the hashtable used by add_sound_suggest() */
12997 hash_init(&slang->sl_sounddone);
12998 }
12999}
13000
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013001/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013002 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013003 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013004 */
13005 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000013006suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013007 suginfo_T *su;
13008{
13009 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013010 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013011 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013012 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013013
Bram Moolenaar4770d092006-01-12 23:22:24 +000013014 /* Do this for all languages that support sound folding and for which a
13015 * .sug file has been loaded. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013016 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013017 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013018 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13019 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013020 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013021 {
13022 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013023 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013024
Bram Moolenaar4770d092006-01-12 23:22:24 +000013025 /* try all kinds of inserts/deletes/swaps/etc. */
13026 /* TODO: also soundfold the next words, so that we can try joining
13027 * and splitting */
13028 suggest_trie_walk(su, lp, salword, TRUE);
13029 }
13030 }
13031}
13032
13033/*
13034 * Finish up after calling suggest_try_soundalike().
13035 */
13036 static void
13037suggest_try_soundalike_finish()
13038{
13039 langp_T *lp;
13040 int lpi;
13041 slang_T *slang;
13042 int todo;
13043 hashitem_T *hi;
13044
13045 /* Do this for all languages that support sound folding and for which a
13046 * .sug file has been loaded. */
13047 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
13048 {
13049 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
13050 slang = lp->lp_slang;
13051 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13052 {
13053 /* Free the info about handled words. */
13054 todo = slang->sl_sounddone.ht_used;
13055 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
13056 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013057 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013058 vim_free(HI2SFT(hi));
13059 --todo;
13060 }
13061 hash_clear(&slang->sl_sounddone);
13062 }
13063 }
13064}
13065
13066/*
13067 * A match with a soundfolded word is found. Add the good word(s) that
13068 * produce this soundfolded word.
13069 */
13070 static void
13071add_sound_suggest(su, goodword, score, lp)
13072 suginfo_T *su;
13073 char_u *goodword;
13074 int score; /* soundfold score */
13075 langp_T *lp;
13076{
13077 slang_T *slang = lp->lp_slang; /* language for sound folding */
13078 int sfwordnr;
13079 char_u *nrline;
13080 int orgnr;
13081 char_u theword[MAXWLEN];
13082 int i;
13083 int wlen;
13084 char_u *byts;
13085 idx_T *idxs;
13086 int n;
13087 int wordcount;
13088 int wc;
13089 int goodscore;
13090 hash_T hash;
13091 hashitem_T *hi;
13092 sftword_T *sft;
13093 int bc, gc;
13094 int limit;
13095
13096 /*
13097 * It's very well possible that the same soundfold word is found several
13098 * times with different scores. Since the following is quite slow only do
13099 * the words that have a better score than before. Use a hashtable to
13100 * remember the words that have been done.
13101 */
13102 hash = hash_hash(goodword);
13103 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
13104 if (HASHITEM_EMPTY(hi))
13105 {
13106 sft = (sftword_T *)alloc(sizeof(sftword_T) + STRLEN(goodword));
13107 if (sft != NULL)
13108 {
13109 sft->sft_score = score;
13110 STRCPY(sft->sft_word, goodword);
13111 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
13112 }
13113 }
13114 else
13115 {
13116 sft = HI2SFT(hi);
13117 if (score >= sft->sft_score)
13118 return;
13119 sft->sft_score = score;
13120 }
13121
13122 /*
13123 * Find the word nr in the soundfold tree.
13124 */
13125 sfwordnr = soundfold_find(slang, goodword);
13126 if (sfwordnr < 0)
13127 {
13128 EMSG2(_(e_intern2), "add_sound_suggest()");
13129 return;
13130 }
13131
13132 /*
13133 * go over the list of good words that produce this soundfold word
13134 */
13135 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
13136 orgnr = 0;
13137 while (*nrline != NUL)
13138 {
13139 /* The wordnr was stored in a minimal nr of bytes as an offset to the
13140 * previous wordnr. */
13141 orgnr += bytes2offset(&nrline);
13142
13143 byts = slang->sl_fbyts;
13144 idxs = slang->sl_fidxs;
13145
13146 /* Lookup the word "orgnr" one of the two tries. */
13147 n = 0;
13148 wlen = 0;
13149 wordcount = 0;
13150 for (;;)
13151 {
13152 i = 1;
13153 if (wordcount == orgnr && byts[n + 1] == NUL)
13154 break; /* found end of word */
13155
13156 if (byts[n + 1] == NUL)
13157 ++wordcount;
13158
13159 /* skip over the NUL bytes */
13160 for ( ; byts[n + i] == NUL; ++i)
13161 if (i > byts[n]) /* safety check */
13162 {
13163 STRCPY(theword + wlen, "BAD");
13164 goto badword;
13165 }
13166
13167 /* One of the siblings must have the word. */
13168 for ( ; i < byts[n]; ++i)
13169 {
13170 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
13171 if (wordcount + wc > orgnr)
13172 break;
13173 wordcount += wc;
13174 }
13175
13176 theword[wlen++] = byts[n + i];
13177 n = idxs[n + i];
13178 }
13179badword:
13180 theword[wlen] = NUL;
13181
13182 /* Go over the possible flags and regions. */
13183 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
13184 {
13185 char_u cword[MAXWLEN];
13186 char_u *p;
13187 int flags = (int)idxs[n + i];
13188
Bram Moolenaare1438bb2006-03-01 22:01:55 +000013189 /* Skip words with the NOSUGGEST flag */
13190 if (flags & WF_NOSUGGEST)
13191 continue;
13192
Bram Moolenaar4770d092006-01-12 23:22:24 +000013193 if (flags & WF_KEEPCAP)
13194 {
13195 /* Must find the word in the keep-case tree. */
13196 find_keepcap_word(slang, theword, cword);
13197 p = cword;
13198 }
13199 else
13200 {
13201 flags |= su->su_badflags;
13202 if ((flags & WF_CAPMASK) != 0)
13203 {
13204 /* Need to fix case according to "flags". */
13205 make_case_word(theword, cword, flags);
13206 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013207 }
13208 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013209 p = theword;
13210 }
13211
13212 /* Add the suggestion. */
13213 if (sps_flags & SPS_DOUBLE)
13214 {
13215 /* Add the suggestion if the score isn't too bad. */
13216 if (score <= su->su_maxscore)
13217 add_suggestion(su, &su->su_sga, p, su->su_badlen,
13218 score, 0, FALSE, slang, FALSE);
13219 }
13220 else
13221 {
13222 /* Add a penalty for words in another region. */
13223 if ((flags & WF_REGION)
13224 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
13225 goodscore = SCORE_REGION;
13226 else
13227 goodscore = 0;
13228
13229 /* Add a small penalty for changing the first letter from
13230 * lower to upper case. Helps for "tath" -> "Kath", which is
13231 * less common thatn "tath" -> "path". Don't do it when the
13232 * letter is the same, that has already been counted. */
13233 gc = PTR2CHAR(p);
13234 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013235 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013236 bc = PTR2CHAR(su->su_badword);
13237 if (!SPELL_ISUPPER(bc)
13238 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
13239 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013240 }
13241
Bram Moolenaar4770d092006-01-12 23:22:24 +000013242 /* Compute the score for the good word. This only does letter
13243 * insert/delete/swap/replace. REP items are not considered,
13244 * which may make the score a bit higher.
13245 * Use a limit for the score to make it work faster. Use
13246 * MAXSCORE(), because RESCORE() will change the score.
13247 * If the limit is very high then the iterative method is
13248 * inefficient, using an array is quicker. */
13249 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
13250 if (limit > SCORE_LIMITMAX)
13251 goodscore += spell_edit_score(slang, su->su_badword, p);
13252 else
13253 goodscore += spell_edit_score_limit(slang, su->su_badword,
13254 p, limit);
13255
13256 /* When going over the limit don't bother to do the rest. */
13257 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013258 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013259 /* Give a bonus to words seen before. */
13260 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013261
Bram Moolenaar4770d092006-01-12 23:22:24 +000013262 /* Add the suggestion if the score isn't too bad. */
13263 goodscore = RESCORE(goodscore, score);
13264 if (goodscore <= su->su_sfmaxscore)
13265 add_suggestion(su, &su->su_ga, p, su->su_badlen,
13266 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013267 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013268 }
13269 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013270 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013271 }
13272}
13273
13274/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013275 * Find word "word" in fold-case tree for "slang" and return the word number.
13276 */
13277 static int
13278soundfold_find(slang, word)
13279 slang_T *slang;
13280 char_u *word;
13281{
13282 idx_T arridx = 0;
13283 int len;
13284 int wlen = 0;
13285 int c;
13286 char_u *ptr = word;
13287 char_u *byts;
13288 idx_T *idxs;
13289 int wordnr = 0;
13290
13291 byts = slang->sl_sbyts;
13292 idxs = slang->sl_sidxs;
13293
13294 for (;;)
13295 {
13296 /* First byte is the number of possible bytes. */
13297 len = byts[arridx++];
13298
13299 /* If the first possible byte is a zero the word could end here.
13300 * If the word ends we found the word. If not skip the NUL bytes. */
13301 c = ptr[wlen];
13302 if (byts[arridx] == NUL)
13303 {
13304 if (c == NUL)
13305 break;
13306
13307 /* Skip over the zeros, there can be several. */
13308 while (len > 0 && byts[arridx] == NUL)
13309 {
13310 ++arridx;
13311 --len;
13312 }
13313 if (len == 0)
13314 return -1; /* no children, word should have ended here */
13315 ++wordnr;
13316 }
13317
13318 /* If the word ends we didn't find it. */
13319 if (c == NUL)
13320 return -1;
13321
13322 /* Perform a binary search in the list of accepted bytes. */
13323 if (c == TAB) /* <Tab> is handled like <Space> */
13324 c = ' ';
13325 while (byts[arridx] < c)
13326 {
13327 /* The word count is in the first idxs[] entry of the child. */
13328 wordnr += idxs[idxs[arridx]];
13329 ++arridx;
13330 if (--len == 0) /* end of the bytes, didn't find it */
13331 return -1;
13332 }
13333 if (byts[arridx] != c) /* didn't find the byte */
13334 return -1;
13335
13336 /* Continue at the child (if there is one). */
13337 arridx = idxs[arridx];
13338 ++wlen;
13339
13340 /* One space in the good word may stand for several spaces in the
13341 * checked word. */
13342 if (c == ' ')
13343 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
13344 ++wlen;
13345 }
13346
13347 return wordnr;
13348}
13349
13350/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013351 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013352 */
13353 static void
13354make_case_word(fword, cword, flags)
13355 char_u *fword;
13356 char_u *cword;
13357 int flags;
13358{
13359 if (flags & WF_ALLCAP)
13360 /* Make it all upper-case */
13361 allcap_copy(fword, cword);
13362 else if (flags & WF_ONECAP)
13363 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013364 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013365 else
13366 /* Use goodword as-is. */
13367 STRCPY(cword, fword);
13368}
13369
Bram Moolenaarea424162005-06-16 21:51:00 +000013370/*
13371 * Use map string "map" for languages "lp".
13372 */
13373 static void
13374set_map_str(lp, map)
13375 slang_T *lp;
13376 char_u *map;
13377{
13378 char_u *p;
13379 int headc = 0;
13380 int c;
13381 int i;
13382
13383 if (*map == NUL)
13384 {
13385 lp->sl_has_map = FALSE;
13386 return;
13387 }
13388 lp->sl_has_map = TRUE;
13389
Bram Moolenaar4770d092006-01-12 23:22:24 +000013390 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000013391 for (i = 0; i < 256; ++i)
13392 lp->sl_map_array[i] = 0;
13393#ifdef FEAT_MBYTE
13394 hash_init(&lp->sl_map_hash);
13395#endif
13396
13397 /*
13398 * The similar characters are stored separated with slashes:
13399 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13400 * before the same slash. For characters above 255 sl_map_hash is used.
13401 */
13402 for (p = map; *p != NUL; )
13403 {
13404#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013405 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000013406#else
13407 c = *p++;
13408#endif
13409 if (c == '/')
13410 headc = 0;
13411 else
13412 {
13413 if (headc == 0)
13414 headc = c;
13415
13416#ifdef FEAT_MBYTE
13417 /* Characters above 255 don't fit in sl_map_array[], put them in
13418 * the hash table. Each entry is the char, a NUL the headchar and
13419 * a NUL. */
13420 if (c >= 256)
13421 {
13422 int cl = mb_char2len(c);
13423 int headcl = mb_char2len(headc);
13424 char_u *b;
13425 hash_T hash;
13426 hashitem_T *hi;
13427
13428 b = alloc((unsigned)(cl + headcl + 2));
13429 if (b == NULL)
13430 return;
13431 mb_char2bytes(c, b);
13432 b[cl] = NUL;
13433 mb_char2bytes(headc, b + cl + 1);
13434 b[cl + 1 + headcl] = NUL;
13435 hash = hash_hash(b);
13436 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13437 if (HASHITEM_EMPTY(hi))
13438 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13439 else
13440 {
13441 /* This should have been checked when generating the .spl
13442 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013443 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000013444 vim_free(b);
13445 }
13446 }
13447 else
13448#endif
13449 lp->sl_map_array[c] = headc;
13450 }
13451 }
13452}
13453
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013454/*
13455 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13456 * lines in the .aff file.
13457 */
13458 static int
13459similar_chars(slang, c1, c2)
13460 slang_T *slang;
13461 int c1;
13462 int c2;
13463{
Bram Moolenaarea424162005-06-16 21:51:00 +000013464 int m1, m2;
13465#ifdef FEAT_MBYTE
13466 char_u buf[MB_MAXBYTES];
13467 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013468
Bram Moolenaarea424162005-06-16 21:51:00 +000013469 if (c1 >= 256)
13470 {
13471 buf[mb_char2bytes(c1, buf)] = 0;
13472 hi = hash_find(&slang->sl_map_hash, buf);
13473 if (HASHITEM_EMPTY(hi))
13474 m1 = 0;
13475 else
13476 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13477 }
13478 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013479#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013480 m1 = slang->sl_map_array[c1];
13481 if (m1 == 0)
13482 return FALSE;
13483
13484
13485#ifdef FEAT_MBYTE
13486 if (c2 >= 256)
13487 {
13488 buf[mb_char2bytes(c2, buf)] = 0;
13489 hi = hash_find(&slang->sl_map_hash, buf);
13490 if (HASHITEM_EMPTY(hi))
13491 m2 = 0;
13492 else
13493 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13494 }
13495 else
13496#endif
13497 m2 = slang->sl_map_array[c2];
13498
13499 return m1 == m2;
13500}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013501
13502/*
13503 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013504 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013505 */
13506 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000013507add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
13508 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013509 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013510 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013511 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013512 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013513 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013514 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013515 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013516 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013517 int maxsf; /* su_maxscore applies to soundfold score,
13518 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013519{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013520 int goodlen; /* len of goodword changed */
13521 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013522 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013523 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013524 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013525 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013526
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013527 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13528 * "thee the" is added next to changing the first "the" the "thee". */
13529 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013530 pbad = su->su_badptr + badlenarg;
13531 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013532 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013533 goodlen = pgood - goodword;
13534 badlen = pbad - su->su_badptr;
13535 if (goodlen <= 0 || badlen <= 0)
13536 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013537 mb_ptr_back(goodword, pgood);
13538 mb_ptr_back(su->su_badptr, pbad);
13539#ifdef FEAT_MBYTE
13540 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013541 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013542 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13543 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013544 }
13545 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013546#endif
13547 if (*pgood != *pbad)
13548 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013549 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013550
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013551 if (badlen == 0 && goodlen == 0)
13552 /* goodword doesn't change anything; may happen for "the the" changing
13553 * the first "the" to itself. */
13554 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013555
Bram Moolenaar4770d092006-01-12 23:22:24 +000013556 /* Check if the word is already there. Also check the length that is
13557 * being replaced "thes," -> "these" is a different suggestion from
13558 * "thes" -> "these". */
13559 stp = &SUG(*gap, 0);
13560 for (i = gap->ga_len; --i >= 0; ++stp)
13561 if (stp->st_wordlen == goodlen
13562 && stp->st_orglen == badlen
13563 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
13564 {
13565 /*
13566 * Found it. Remember the word with the lowest score.
13567 */
13568 if (stp->st_slang == NULL)
13569 stp->st_slang = slang;
13570
13571 new_sug.st_score = score;
13572 new_sug.st_altscore = altscore;
13573 new_sug.st_had_bonus = had_bonus;
13574
13575 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013576 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013577 /* Only one of the two had the soundalike score computed.
13578 * Need to do that for the other one now, otherwise the
13579 * scores can't be compared. This happens because
13580 * suggest_try_change() doesn't compute the soundalike
13581 * word to keep it fast, while some special methods set
13582 * the soundalike score to zero. */
13583 if (had_bonus)
13584 rescore_one(su, stp);
13585 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013586 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013587 new_sug.st_word = stp->st_word;
13588 new_sug.st_wordlen = stp->st_wordlen;
13589 new_sug.st_slang = stp->st_slang;
13590 new_sug.st_orglen = badlen;
13591 rescore_one(su, &new_sug);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013592 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013593 }
13594
Bram Moolenaar4770d092006-01-12 23:22:24 +000013595 if (stp->st_score > new_sug.st_score)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013596 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013597 stp->st_score = new_sug.st_score;
13598 stp->st_altscore = new_sug.st_altscore;
13599 stp->st_had_bonus = new_sug.st_had_bonus;
13600 }
13601 break;
13602 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013603
Bram Moolenaar4770d092006-01-12 23:22:24 +000013604 if (i < 0 && ga_grow(gap, 1) == OK)
13605 {
13606 /* Add a suggestion. */
13607 stp = &SUG(*gap, gap->ga_len);
13608 stp->st_word = vim_strnsave(goodword, goodlen);
13609 if (stp->st_word != NULL)
13610 {
13611 stp->st_wordlen = goodlen;
13612 stp->st_score = score;
13613 stp->st_altscore = altscore;
13614 stp->st_had_bonus = had_bonus;
13615 stp->st_orglen = badlen;
13616 stp->st_slang = slang;
13617 ++gap->ga_len;
13618
13619 /* If we have too many suggestions now, sort the list and keep
13620 * the best suggestions. */
13621 if (gap->ga_len > SUG_MAX_COUNT(su))
13622 {
13623 if (maxsf)
13624 su->su_sfmaxscore = cleanup_suggestions(gap,
13625 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13626 else
13627 {
13628 i = su->su_maxscore;
13629 su->su_maxscore = cleanup_suggestions(gap,
13630 su->su_maxscore, SUG_CLEAN_COUNT(su));
13631 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013632 }
13633 }
13634 }
13635}
13636
13637/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013638 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13639 * for split words, such as "the the". Remove these from the list here.
13640 */
13641 static void
13642check_suggestions(su, gap)
13643 suginfo_T *su;
13644 garray_T *gap; /* either su_ga or su_sga */
13645{
13646 suggest_T *stp;
13647 int i;
13648 char_u longword[MAXWLEN + 1];
13649 int len;
13650 hlf_T attr;
13651
13652 stp = &SUG(*gap, 0);
13653 for (i = gap->ga_len - 1; i >= 0; --i)
13654 {
13655 /* Need to append what follows to check for "the the". */
13656 STRCPY(longword, stp[i].st_word);
13657 len = stp[i].st_wordlen;
13658 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13659 MAXWLEN - len);
13660 attr = HLF_COUNT;
13661 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13662 if (attr != HLF_COUNT)
13663 {
13664 /* Remove this entry. */
13665 vim_free(stp[i].st_word);
13666 --gap->ga_len;
13667 if (i < gap->ga_len)
13668 mch_memmove(stp + i, stp + i + 1,
13669 sizeof(suggest_T) * (gap->ga_len - i));
13670 }
13671 }
13672}
13673
13674
13675/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013676 * Add a word to be banned.
13677 */
13678 static void
13679add_banned(su, word)
13680 suginfo_T *su;
13681 char_u *word;
13682{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013683 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013684 hash_T hash;
13685 hashitem_T *hi;
13686
Bram Moolenaar4770d092006-01-12 23:22:24 +000013687 hash = hash_hash(word);
13688 hi = hash_lookup(&su->su_banned, word, hash);
13689 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013690 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013691 s = vim_strsave(word);
13692 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013693 hash_add_item(&su->su_banned, hi, s, hash);
13694 }
13695}
13696
13697/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013698 * Recompute the score for all suggestions if sound-folding is possible. This
13699 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013700 */
13701 static void
13702rescore_suggestions(su)
13703 suginfo_T *su;
13704{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013705 int i;
13706
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013707 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013708 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013709 rescore_one(su, &SUG(su->su_ga, i));
13710}
13711
13712/*
13713 * Recompute the score for one suggestion if sound-folding is possible.
13714 */
13715 static void
13716rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013717 suginfo_T *su;
13718 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013719{
13720 slang_T *slang = stp->st_slang;
13721 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013722 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013723
13724 /* Only rescore suggestions that have no sal score yet and do have a
13725 * language. */
13726 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13727 {
13728 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013729 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013730 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013731 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013732 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013733 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013734 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013735
13736 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013737 if (stp->st_altscore == SCORE_MAXMAX)
13738 stp->st_altscore = SCORE_BIG;
13739 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13740 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013741 }
13742}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013744static int
13745#ifdef __BORLANDC__
13746_RTLENTRYF
13747#endif
13748sug_compare __ARGS((const void *s1, const void *s2));
13749
13750/*
13751 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013752 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013753 */
13754 static int
13755#ifdef __BORLANDC__
13756_RTLENTRYF
13757#endif
13758sug_compare(s1, s2)
13759 const void *s1;
13760 const void *s2;
13761{
13762 suggest_T *p1 = (suggest_T *)s1;
13763 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013764 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013765
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013766 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013767 {
13768 n = p1->st_altscore - p2->st_altscore;
13769 if (n == 0)
13770 n = STRICMP(p1->st_word, p2->st_word);
13771 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013772 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013773}
13774
13775/*
13776 * Cleanup the suggestions:
13777 * - Sort on score.
13778 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013779 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013780 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013781 static int
13782cleanup_suggestions(gap, maxscore, keep)
13783 garray_T *gap;
13784 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013785 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013786{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013787 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013788 int i;
13789
13790 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013791 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013792
13793 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013794 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013795 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013796 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013797 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013798 gap->ga_len = keep;
13799 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013800 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013801 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013802}
13803
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013804#if defined(FEAT_EVAL) || defined(PROTO)
13805/*
13806 * Soundfold a string, for soundfold().
13807 * Result is in allocated memory, NULL for an error.
13808 */
13809 char_u *
13810eval_soundfold(word)
13811 char_u *word;
13812{
13813 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013814 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013815 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013816
13817 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13818 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013819 for (lpi = 0; lpi < curbuf->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013820 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013821 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013822 if (lp->lp_slang->sl_sal.ga_len > 0)
13823 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013824 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013825 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013826 return vim_strsave(sound);
13827 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013828 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013829
13830 /* No language with sound folding, return word as-is. */
13831 return vim_strsave(word);
13832}
13833#endif
13834
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013835/*
13836 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000013837 *
13838 * There are many ways to turn a word into a sound-a-like representation. The
13839 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
13840 * swedish name matching - survey and test of different algorithms" by Klas
13841 * Erikson.
13842 *
13843 * We support two methods:
13844 * 1. SOFOFROM/SOFOTO do a simple character mapping.
13845 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013846 */
13847 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013848spell_soundfold(slang, inword, folded, res)
13849 slang_T *slang;
13850 char_u *inword;
13851 int folded; /* "inword" is already case-folded */
13852 char_u *res;
13853{
13854 char_u fword[MAXWLEN];
13855 char_u *word;
13856
13857 if (slang->sl_sofo)
13858 /* SOFOFROM and SOFOTO used */
13859 spell_soundfold_sofo(slang, inword, res);
13860 else
13861 {
13862 /* SAL items used. Requires the word to be case-folded. */
13863 if (folded)
13864 word = inword;
13865 else
13866 {
13867 (void)spell_casefold(inword, STRLEN(inword), fword, MAXWLEN);
13868 word = fword;
13869 }
13870
13871#ifdef FEAT_MBYTE
13872 if (has_mbyte)
13873 spell_soundfold_wsal(slang, word, res);
13874 else
13875#endif
13876 spell_soundfold_sal(slang, word, res);
13877 }
13878}
13879
13880/*
13881 * Perform sound folding of "inword" into "res" according to SOFOFROM and
13882 * SOFOTO lines.
13883 */
13884 static void
13885spell_soundfold_sofo(slang, inword, res)
13886 slang_T *slang;
13887 char_u *inword;
13888 char_u *res;
13889{
13890 char_u *s;
13891 int ri = 0;
13892 int c;
13893
13894#ifdef FEAT_MBYTE
13895 if (has_mbyte)
13896 {
13897 int prevc = 0;
13898 int *ip;
13899
13900 /* The sl_sal_first[] table contains the translation for chars up to
13901 * 255, sl_sal the rest. */
13902 for (s = inword; *s != NUL; )
13903 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013904 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013905 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
13906 c = ' ';
13907 else if (c < 256)
13908 c = slang->sl_sal_first[c];
13909 else
13910 {
13911 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
13912 if (ip == NULL) /* empty list, can't match */
13913 c = NUL;
13914 else
13915 for (;;) /* find "c" in the list */
13916 {
13917 if (*ip == 0) /* not found */
13918 {
13919 c = NUL;
13920 break;
13921 }
13922 if (*ip == c) /* match! */
13923 {
13924 c = ip[1];
13925 break;
13926 }
13927 ip += 2;
13928 }
13929 }
13930
13931 if (c != NUL && c != prevc)
13932 {
13933 ri += mb_char2bytes(c, res + ri);
13934 if (ri + MB_MAXBYTES > MAXWLEN)
13935 break;
13936 prevc = c;
13937 }
13938 }
13939 }
13940 else
13941#endif
13942 {
13943 /* The sl_sal_first[] table contains the translation. */
13944 for (s = inword; (c = *s) != NUL; ++s)
13945 {
13946 if (vim_iswhite(c))
13947 c = ' ';
13948 else
13949 c = slang->sl_sal_first[c];
13950 if (c != NUL && (ri == 0 || res[ri - 1] != c))
13951 res[ri++] = c;
13952 }
13953 }
13954
13955 res[ri] = NUL;
13956}
13957
13958 static void
13959spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013960 slang_T *slang;
13961 char_u *inword;
13962 char_u *res;
13963{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013964 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013965 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013966 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013967 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013968 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013969 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013970 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013971 int n, k = 0;
13972 int z0;
13973 int k0;
13974 int n0;
13975 int c;
13976 int pri;
13977 int p0 = -333;
13978 int c0;
13979
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013980 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013981 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013982 if (slang->sl_rem_accents)
13983 {
13984 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013985 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013986 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013987 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013988 {
13989 *t++ = ' ';
13990 s = skipwhite(s);
13991 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013992 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013993 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000013994 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013995 *t++ = *s;
13996 ++s;
13997 }
13998 }
13999 *t = NUL;
14000 }
14001 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014002 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014003
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014004 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014005
14006 /*
14007 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014008 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014009 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014010 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014011 while ((c = word[i]) != NUL)
14012 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014013 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014014 n = slang->sl_sal_first[c];
14015 z0 = 0;
14016
14017 if (n >= 0)
14018 {
14019 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014020 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014021 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014022 /* Quickly skip entries that don't match the word. Most
14023 * entries are less then three chars, optimize for that. */
14024 k = smp[n].sm_leadlen;
14025 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014026 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014027 if (word[i + 1] != s[1])
14028 continue;
14029 if (k > 2)
14030 {
14031 for (j = 2; j < k; ++j)
14032 if (word[i + j] != s[j])
14033 break;
14034 if (j < k)
14035 continue;
14036 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014037 }
14038
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014039 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014040 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014041 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014042 while (*pf != NUL && *pf != word[i + k])
14043 ++pf;
14044 if (*pf == NUL)
14045 continue;
14046 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014047 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014048 s = smp[n].sm_rules;
14049 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014050
14051 p0 = *s;
14052 k0 = k;
14053 while (*s == '-' && k > 1)
14054 {
14055 k--;
14056 s++;
14057 }
14058 if (*s == '<')
14059 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014060 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014061 {
14062 /* determine priority */
14063 pri = *s - '0';
14064 s++;
14065 }
14066 if (*s == '^' && *(s + 1) == '^')
14067 s++;
14068
14069 if (*s == NUL
14070 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014071 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014072 || spell_iswordp(word + i - 1, curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014073 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014074 || (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014075 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014076 && spell_iswordp(word + i - 1, curbuf)
14077 && (!spell_iswordp(word + i + k0, curbuf))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014078 {
14079 /* search for followup rules, if: */
14080 /* followup and k > 1 and NO '-' in searchstring */
14081 c0 = word[i + k - 1];
14082 n0 = slang->sl_sal_first[c0];
14083
14084 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014085 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014086 {
14087 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014088 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014089 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014090 /* Quickly skip entries that don't match the word.
14091 * */
14092 k0 = smp[n0].sm_leadlen;
14093 if (k0 > 1)
14094 {
14095 if (word[i + k] != s[1])
14096 continue;
14097 if (k0 > 2)
14098 {
14099 pf = word + i + k + 1;
14100 for (j = 2; j < k0; ++j)
14101 if (*pf++ != s[j])
14102 break;
14103 if (j < k0)
14104 continue;
14105 }
14106 }
14107 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014108
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014109 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014110 {
14111 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014112 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014113 while (*pf != NUL && *pf != word[i + k0])
14114 ++pf;
14115 if (*pf == NUL)
14116 continue;
14117 ++k0;
14118 }
14119
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014120 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014121 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014122 while (*s == '-')
14123 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014124 /* "k0" gets NOT reduced because
14125 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014126 s++;
14127 }
14128 if (*s == '<')
14129 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014130 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014131 {
14132 p0 = *s - '0';
14133 s++;
14134 }
14135
14136 if (*s == NUL
14137 /* *s == '^' cuts */
14138 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014139 && !spell_iswordp(word + i + k0,
14140 curbuf)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014141 {
14142 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014143 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014144 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014145
14146 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014147 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014148 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014149 /* rule fits; stop search */
14150 break;
14151 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014152 }
14153
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014154 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014155 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014156 }
14157
14158 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014159 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014160 if (s == NULL)
14161 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014162 pf = smp[n].sm_rules;
14163 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014164 if (p0 == 1 && z == 0)
14165 {
14166 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014167 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
14168 || res[reslen - 1] == *s))
14169 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014170 z0 = 1;
14171 z = 1;
14172 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014173 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014174 {
14175 word[i + k0] = *s;
14176 k0++;
14177 s++;
14178 }
14179 if (k > k0)
14180 mch_memmove(word + i + k0, word + i + k,
14181 STRLEN(word + i + k) + 1);
14182
14183 /* new "actual letter" */
14184 c = word[i];
14185 }
14186 else
14187 {
14188 /* no '<' rule used */
14189 i += k - 1;
14190 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014191 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014192 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014193 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014194 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014195 s++;
14196 }
14197 /* new "actual letter" */
14198 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014199 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014200 {
14201 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014202 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014203 mch_memmove(word, word + i + 1,
14204 STRLEN(word + i + 1) + 1);
14205 i = 0;
14206 z0 = 1;
14207 }
14208 }
14209 break;
14210 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014211 }
14212 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014213 else if (vim_iswhite(c))
14214 {
14215 c = ' ';
14216 k = 1;
14217 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014218
14219 if (z0 == 0)
14220 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014221 if (k && !p0 && reslen < MAXWLEN && c != NUL
14222 && (!slang->sl_collapse || reslen == 0
14223 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014224 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014225 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014226
14227 i++;
14228 z = 0;
14229 k = 0;
14230 }
14231 }
14232
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014233 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014234}
14235
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014236#ifdef FEAT_MBYTE
14237/*
14238 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14239 * Multi-byte version of spell_soundfold().
14240 */
14241 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014242spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014243 slang_T *slang;
14244 char_u *inword;
14245 char_u *res;
14246{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014247 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014248 int word[MAXWLEN];
14249 int wres[MAXWLEN];
14250 int l;
14251 char_u *s;
14252 int *ws;
14253 char_u *t;
14254 int *pf;
14255 int i, j, z;
14256 int reslen;
14257 int n, k = 0;
14258 int z0;
14259 int k0;
14260 int n0;
14261 int c;
14262 int pri;
14263 int p0 = -333;
14264 int c0;
14265 int did_white = FALSE;
14266
14267 /*
14268 * Convert the multi-byte string to a wide-character string.
14269 * Remove accents, if wanted. We actually remove all non-word characters.
14270 * But keep white space.
14271 */
14272 n = 0;
14273 for (s = inword; *s != NUL; )
14274 {
14275 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014276 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014277 if (slang->sl_rem_accents)
14278 {
14279 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14280 {
14281 if (did_white)
14282 continue;
14283 c = ' ';
14284 did_white = TRUE;
14285 }
14286 else
14287 {
14288 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014289 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014290 continue;
14291 }
14292 }
14293 word[n++] = c;
14294 }
14295 word[n] = NUL;
14296
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014297 /*
14298 * This comes from Aspell phonet.cpp.
14299 * Converted from C++ to C. Added support for multi-byte chars.
14300 * Changed to keep spaces.
14301 */
14302 i = reslen = z = 0;
14303 while ((c = word[i]) != NUL)
14304 {
14305 /* Start with the first rule that has the character in the word. */
14306 n = slang->sl_sal_first[c & 0xff];
14307 z0 = 0;
14308
14309 if (n >= 0)
14310 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014311 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014312 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
14313 {
14314 /* Quickly skip entries that don't match the word. Most
14315 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014316 if (c != ws[0])
14317 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014318 k = smp[n].sm_leadlen;
14319 if (k > 1)
14320 {
14321 if (word[i + 1] != ws[1])
14322 continue;
14323 if (k > 2)
14324 {
14325 for (j = 2; j < k; ++j)
14326 if (word[i + j] != ws[j])
14327 break;
14328 if (j < k)
14329 continue;
14330 }
14331 }
14332
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014333 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014334 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014335 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014336 while (*pf != NUL && *pf != word[i + k])
14337 ++pf;
14338 if (*pf == NUL)
14339 continue;
14340 ++k;
14341 }
14342 s = smp[n].sm_rules;
14343 pri = 5; /* default priority */
14344
14345 p0 = *s;
14346 k0 = k;
14347 while (*s == '-' && k > 1)
14348 {
14349 k--;
14350 s++;
14351 }
14352 if (*s == '<')
14353 s++;
14354 if (VIM_ISDIGIT(*s))
14355 {
14356 /* determine priority */
14357 pri = *s - '0';
14358 s++;
14359 }
14360 if (*s == '^' && *(s + 1) == '^')
14361 s++;
14362
14363 if (*s == NUL
14364 || (*s == '^'
14365 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014366 || spell_iswordp_w(word + i - 1, curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014367 && (*(s + 1) != '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014368 || (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014369 || (*s == '$' && i > 0
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014370 && spell_iswordp_w(word + i - 1, curbuf)
14371 && (!spell_iswordp_w(word + i + k0, curbuf))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014372 {
14373 /* search for followup rules, if: */
14374 /* followup and k > 1 and NO '-' in searchstring */
14375 c0 = word[i + k - 1];
14376 n0 = slang->sl_sal_first[c0 & 0xff];
14377
14378 if (slang->sl_followup && k > 1 && n0 >= 0
14379 && p0 != '-' && word[i + k] != NUL)
14380 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014381 /* Test follow-up rule for "word[i + k]"; loop over
14382 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014383 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14384 == (c0 & 0xff); ++n0)
14385 {
14386 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014387 */
14388 if (c0 != ws[0])
14389 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014390 k0 = smp[n0].sm_leadlen;
14391 if (k0 > 1)
14392 {
14393 if (word[i + k] != ws[1])
14394 continue;
14395 if (k0 > 2)
14396 {
14397 pf = word + i + k + 1;
14398 for (j = 2; j < k0; ++j)
14399 if (*pf++ != ws[j])
14400 break;
14401 if (j < k0)
14402 continue;
14403 }
14404 }
14405 k0 += k - 1;
14406
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014407 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014408 {
14409 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014410 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014411 while (*pf != NUL && *pf != word[i + k0])
14412 ++pf;
14413 if (*pf == NUL)
14414 continue;
14415 ++k0;
14416 }
14417
14418 p0 = 5;
14419 s = smp[n0].sm_rules;
14420 while (*s == '-')
14421 {
14422 /* "k0" gets NOT reduced because
14423 * "if (k0 == k)" */
14424 s++;
14425 }
14426 if (*s == '<')
14427 s++;
14428 if (VIM_ISDIGIT(*s))
14429 {
14430 p0 = *s - '0';
14431 s++;
14432 }
14433
14434 if (*s == NUL
14435 /* *s == '^' cuts */
14436 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014437 && !spell_iswordp_w(word + i + k0,
14438 curbuf)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014439 {
14440 if (k0 == k)
14441 /* this is just a piece of the string */
14442 continue;
14443
14444 if (p0 < pri)
14445 /* priority too low */
14446 continue;
14447 /* rule fits; stop search */
14448 break;
14449 }
14450 }
14451
14452 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14453 == (c0 & 0xff))
14454 continue;
14455 }
14456
14457 /* replace string */
14458 ws = smp[n].sm_to_w;
14459 s = smp[n].sm_rules;
14460 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14461 if (p0 == 1 && z == 0)
14462 {
14463 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014464 if (reslen > 0 && ws != NULL && *ws != NUL
14465 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014466 || wres[reslen - 1] == *ws))
14467 reslen--;
14468 z0 = 1;
14469 z = 1;
14470 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014471 if (ws != NULL)
14472 while (*ws != NUL && word[i + k0] != NUL)
14473 {
14474 word[i + k0] = *ws;
14475 k0++;
14476 ws++;
14477 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014478 if (k > k0)
14479 mch_memmove(word + i + k0, word + i + k,
14480 sizeof(int) * (STRLEN(word + i + k) + 1));
14481
14482 /* new "actual letter" */
14483 c = word[i];
14484 }
14485 else
14486 {
14487 /* no '<' rule used */
14488 i += k - 1;
14489 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014490 if (ws != NULL)
14491 while (*ws != NUL && ws[1] != NUL
14492 && reslen < MAXWLEN)
14493 {
14494 if (reslen == 0 || wres[reslen - 1] != *ws)
14495 wres[reslen++] = *ws;
14496 ws++;
14497 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014498 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014499 if (ws == NULL)
14500 c = NUL;
14501 else
14502 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014503 if (strstr((char *)s, "^^") != NULL)
14504 {
14505 if (c != NUL)
14506 wres[reslen++] = c;
14507 mch_memmove(word, word + i + 1,
14508 sizeof(int) * (STRLEN(word + i + 1) + 1));
14509 i = 0;
14510 z0 = 1;
14511 }
14512 }
14513 break;
14514 }
14515 }
14516 }
14517 else if (vim_iswhite(c))
14518 {
14519 c = ' ';
14520 k = 1;
14521 }
14522
14523 if (z0 == 0)
14524 {
14525 if (k && !p0 && reslen < MAXWLEN && c != NUL
14526 && (!slang->sl_collapse || reslen == 0
14527 || wres[reslen - 1] != c))
14528 /* condense only double letters */
14529 wres[reslen++] = c;
14530
14531 i++;
14532 z = 0;
14533 k = 0;
14534 }
14535 }
14536
14537 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14538 l = 0;
14539 for (n = 0; n < reslen; ++n)
14540 {
14541 l += mb_char2bytes(wres[n], res + l);
14542 if (l + MB_MAXBYTES > MAXWLEN)
14543 break;
14544 }
14545 res[l] = NUL;
14546}
14547#endif
14548
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014549/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014550 * Compute a score for two sound-a-like words.
14551 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14552 * Instead of a generic loop we write out the code. That keeps it fast by
14553 * avoiding checks that will not be possible.
14554 */
14555 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014556soundalike_score(goodstart, badstart)
14557 char_u *goodstart; /* sound-folded good word */
14558 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014559{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014560 char_u *goodsound = goodstart;
14561 char_u *badsound = badstart;
14562 int goodlen;
14563 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014564 int n;
14565 char_u *pl, *ps;
14566 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014567 int score = 0;
14568
14569 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
14570 * counted so much, vowels halfway the word aren't counted at all. */
14571 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14572 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014573 if (badsound[1] == goodsound[1]
14574 || (badsound[1] != NUL
14575 && goodsound[1] != NUL
14576 && badsound[2] == goodsound[2]))
14577 {
14578 /* handle like a substitute */
14579 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014580 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014581 {
14582 score = 2 * SCORE_DEL / 3;
14583 if (*badsound == '*')
14584 ++badsound;
14585 else
14586 ++goodsound;
14587 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014588 }
14589
14590 goodlen = STRLEN(goodsound);
14591 badlen = STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014592
14593 /* Return quickly if the lenghts are too different to be fixed by two
14594 * changes. */
14595 n = goodlen - badlen;
14596 if (n < -2 || n > 2)
14597 return SCORE_MAXMAX;
14598
14599 if (n > 0)
14600 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014601 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014602 ps = badsound;
14603 }
14604 else
14605 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014606 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014607 ps = goodsound;
14608 }
14609
14610 /* Skip over the identical part. */
14611 while (*pl == *ps && *pl != NUL)
14612 {
14613 ++pl;
14614 ++ps;
14615 }
14616
14617 switch (n)
14618 {
14619 case -2:
14620 case 2:
14621 /*
14622 * Must delete two characters from "pl".
14623 */
14624 ++pl; /* first delete */
14625 while (*pl == *ps)
14626 {
14627 ++pl;
14628 ++ps;
14629 }
14630 /* strings must be equal after second delete */
14631 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014632 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014633
14634 /* Failed to compare. */
14635 break;
14636
14637 case -1:
14638 case 1:
14639 /*
14640 * Minimal one delete from "pl" required.
14641 */
14642
14643 /* 1: delete */
14644 pl2 = pl + 1;
14645 ps2 = ps;
14646 while (*pl2 == *ps2)
14647 {
14648 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014649 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014650 ++pl2;
14651 ++ps2;
14652 }
14653
14654 /* 2: delete then swap, then rest must be equal */
14655 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14656 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014657 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014658
14659 /* 3: delete then substitute, then the rest must be equal */
14660 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014661 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014662
14663 /* 4: first swap then delete */
14664 if (pl[0] == ps[1] && pl[1] == ps[0])
14665 {
14666 pl2 = pl + 2; /* swap, skip two chars */
14667 ps2 = ps + 2;
14668 while (*pl2 == *ps2)
14669 {
14670 ++pl2;
14671 ++ps2;
14672 }
14673 /* delete a char and then strings must be equal */
14674 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014675 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014676 }
14677
14678 /* 5: first substitute then delete */
14679 pl2 = pl + 1; /* substitute, skip one char */
14680 ps2 = ps + 1;
14681 while (*pl2 == *ps2)
14682 {
14683 ++pl2;
14684 ++ps2;
14685 }
14686 /* delete a char and then strings must be equal */
14687 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014688 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014689
14690 /* Failed to compare. */
14691 break;
14692
14693 case 0:
14694 /*
14695 * Lenghts are equal, thus changes must result in same length: An
14696 * insert is only possible in combination with a delete.
14697 * 1: check if for identical strings
14698 */
14699 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014700 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014701
14702 /* 2: swap */
14703 if (pl[0] == ps[1] && pl[1] == ps[0])
14704 {
14705 pl2 = pl + 2; /* swap, skip two chars */
14706 ps2 = ps + 2;
14707 while (*pl2 == *ps2)
14708 {
14709 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014710 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014711 ++pl2;
14712 ++ps2;
14713 }
14714 /* 3: swap and swap again */
14715 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14716 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014717 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014718
14719 /* 4: swap and substitute */
14720 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014721 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014722 }
14723
14724 /* 5: substitute */
14725 pl2 = pl + 1;
14726 ps2 = ps + 1;
14727 while (*pl2 == *ps2)
14728 {
14729 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014730 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014731 ++pl2;
14732 ++ps2;
14733 }
14734
14735 /* 6: substitute and swap */
14736 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14737 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014738 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014739
14740 /* 7: substitute and substitute */
14741 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014742 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014743
14744 /* 8: insert then delete */
14745 pl2 = pl;
14746 ps2 = ps + 1;
14747 while (*pl2 == *ps2)
14748 {
14749 ++pl2;
14750 ++ps2;
14751 }
14752 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014753 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014754
14755 /* 9: delete then insert */
14756 pl2 = pl + 1;
14757 ps2 = ps;
14758 while (*pl2 == *ps2)
14759 {
14760 ++pl2;
14761 ++ps2;
14762 }
14763 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014764 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014765
14766 /* Failed to compare. */
14767 break;
14768 }
14769
14770 return SCORE_MAXMAX;
14771}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014772
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014773/*
14774 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014775 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014776 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000014777 * The algorithm is described by Du and Chang, 1992.
14778 * The implementation of the algorithm comes from Aspell editdist.cpp,
14779 * edit_distance(). It has been converted from C++ to C and modified to
14780 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014781 */
14782 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000014783spell_edit_score(slang, badword, goodword)
14784 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014785 char_u *badword;
14786 char_u *goodword;
14787{
14788 int *cnt;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014789 int badlen, goodlen; /* lenghts including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014790 int j, i;
14791 int t;
14792 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014793 int pbc, pgc;
14794#ifdef FEAT_MBYTE
14795 char_u *p;
14796 int wbadword[MAXWLEN];
14797 int wgoodword[MAXWLEN];
14798
14799 if (has_mbyte)
14800 {
14801 /* Get the characters from the multi-byte strings and put them in an
14802 * int array for easy access. */
14803 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014804 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014805 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014806 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014807 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014808 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014809 }
14810 else
14811#endif
14812 {
14813 badlen = STRLEN(badword) + 1;
14814 goodlen = STRLEN(goodword) + 1;
14815 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014816
14817 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
14818#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014819 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
14820 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014821 if (cnt == NULL)
14822 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014823
14824 CNT(0, 0) = 0;
14825 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000014826 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014827
14828 for (i = 1; i <= badlen; ++i)
14829 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014830 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014831 for (j = 1; j <= goodlen; ++j)
14832 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014833#ifdef FEAT_MBYTE
14834 if (has_mbyte)
14835 {
14836 bc = wbadword[i - 1];
14837 gc = wgoodword[j - 1];
14838 }
14839 else
14840#endif
14841 {
14842 bc = badword[i - 1];
14843 gc = goodword[j - 1];
14844 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014845 if (bc == gc)
14846 CNT(i, j) = CNT(i - 1, j - 1);
14847 else
14848 {
14849 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014850 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014851 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
14852 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014853 {
14854 /* For a similar character use SCORE_SIMILAR. */
14855 if (slang != NULL
14856 && slang->sl_has_map
14857 && similar_chars(slang, gc, bc))
14858 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
14859 else
14860 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
14861 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014862
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014863 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014864 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014865#ifdef FEAT_MBYTE
14866 if (has_mbyte)
14867 {
14868 pbc = wbadword[i - 2];
14869 pgc = wgoodword[j - 2];
14870 }
14871 else
14872#endif
14873 {
14874 pbc = badword[i - 2];
14875 pgc = goodword[j - 2];
14876 }
14877 if (bc == pgc && pbc == gc)
14878 {
14879 t = SCORE_SWAP + CNT(i - 2, j - 2);
14880 if (t < CNT(i, j))
14881 CNT(i, j) = t;
14882 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014883 }
14884 t = SCORE_DEL + CNT(i - 1, j);
14885 if (t < CNT(i, j))
14886 CNT(i, j) = t;
14887 t = SCORE_INS + CNT(i, j - 1);
14888 if (t < CNT(i, j))
14889 CNT(i, j) = t;
14890 }
14891 }
14892 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014893
14894 i = CNT(badlen - 1, goodlen - 1);
14895 vim_free(cnt);
14896 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014897}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000014898
Bram Moolenaar4770d092006-01-12 23:22:24 +000014899typedef struct
14900{
14901 int badi;
14902 int goodi;
14903 int score;
14904} limitscore_T;
14905
14906/*
14907 * Like spell_edit_score(), but with a limit on the score to make it faster.
14908 * May return SCORE_MAXMAX when the score is higher than "limit".
14909 *
14910 * This uses a stack for the edits still to be tried.
14911 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
14912 * for multi-byte characters.
14913 */
14914 static int
14915spell_edit_score_limit(slang, badword, goodword, limit)
14916 slang_T *slang;
14917 char_u *badword;
14918 char_u *goodword;
14919 int limit;
14920{
14921 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
14922 int stackidx;
14923 int bi, gi;
14924 int bi2, gi2;
14925 int bc, gc;
14926 int score;
14927 int score_off;
14928 int minscore;
14929 int round;
14930
14931#ifdef FEAT_MBYTE
14932 /* Multi-byte characters require a bit more work, use a different function
14933 * to avoid testing "has_mbyte" quite often. */
14934 if (has_mbyte)
14935 return spell_edit_score_limit_w(slang, badword, goodword, limit);
14936#endif
14937
14938 /*
14939 * The idea is to go from start to end over the words. So long as
14940 * characters are equal just continue, this always gives the lowest score.
14941 * When there is a difference try several alternatives. Each alternative
14942 * increases "score" for the edit distance. Some of the alternatives are
14943 * pushed unto a stack and tried later, some are tried right away. At the
14944 * end of the word the score for one alternative is known. The lowest
14945 * possible score is stored in "minscore".
14946 */
14947 stackidx = 0;
14948 bi = 0;
14949 gi = 0;
14950 score = 0;
14951 minscore = limit + 1;
14952
14953 for (;;)
14954 {
14955 /* Skip over an equal part, score remains the same. */
14956 for (;;)
14957 {
14958 bc = badword[bi];
14959 gc = goodword[gi];
14960 if (bc != gc) /* stop at a char that's different */
14961 break;
14962 if (bc == NUL) /* both words end */
14963 {
14964 if (score < minscore)
14965 minscore = score;
14966 goto pop; /* do next alternative */
14967 }
14968 ++bi;
14969 ++gi;
14970 }
14971
14972 if (gc == NUL) /* goodword ends, delete badword chars */
14973 {
14974 do
14975 {
14976 if ((score += SCORE_DEL) >= minscore)
14977 goto pop; /* do next alternative */
14978 } while (badword[++bi] != NUL);
14979 minscore = score;
14980 }
14981 else if (bc == NUL) /* badword ends, insert badword chars */
14982 {
14983 do
14984 {
14985 if ((score += SCORE_INS) >= minscore)
14986 goto pop; /* do next alternative */
14987 } while (goodword[++gi] != NUL);
14988 minscore = score;
14989 }
14990 else /* both words continue */
14991 {
14992 /* If not close to the limit, perform a change. Only try changes
14993 * that may lead to a lower score than "minscore".
14994 * round 0: try deleting a char from badword
14995 * round 1: try inserting a char in badword */
14996 for (round = 0; round <= 1; ++round)
14997 {
14998 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
14999 if (score_off < minscore)
15000 {
15001 if (score_off + SCORE_EDIT_MIN >= minscore)
15002 {
15003 /* Near the limit, rest of the words must match. We
15004 * can check that right now, no need to push an item
15005 * onto the stack. */
15006 bi2 = bi + 1 - round;
15007 gi2 = gi + round;
15008 while (goodword[gi2] == badword[bi2])
15009 {
15010 if (goodword[gi2] == NUL)
15011 {
15012 minscore = score_off;
15013 break;
15014 }
15015 ++bi2;
15016 ++gi2;
15017 }
15018 }
15019 else
15020 {
15021 /* try deleting/inserting a character later */
15022 stack[stackidx].badi = bi + 1 - round;
15023 stack[stackidx].goodi = gi + round;
15024 stack[stackidx].score = score_off;
15025 ++stackidx;
15026 }
15027 }
15028 }
15029
15030 if (score + SCORE_SWAP < minscore)
15031 {
15032 /* If swapping two characters makes a match then the
15033 * substitution is more expensive, thus there is no need to
15034 * try both. */
15035 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
15036 {
15037 /* Swap two characters, that is: skip them. */
15038 gi += 2;
15039 bi += 2;
15040 score += SCORE_SWAP;
15041 continue;
15042 }
15043 }
15044
15045 /* Substitute one character for another which is the same
15046 * thing as deleting a character from both goodword and badword.
15047 * Use a better score when there is only a case difference. */
15048 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15049 score += SCORE_ICASE;
15050 else
15051 {
15052 /* For a similar character use SCORE_SIMILAR. */
15053 if (slang != NULL
15054 && slang->sl_has_map
15055 && similar_chars(slang, gc, bc))
15056 score += SCORE_SIMILAR;
15057 else
15058 score += SCORE_SUBST;
15059 }
15060
15061 if (score < minscore)
15062 {
15063 /* Do the substitution. */
15064 ++gi;
15065 ++bi;
15066 continue;
15067 }
15068 }
15069pop:
15070 /*
15071 * Get here to try the next alternative, pop it from the stack.
15072 */
15073 if (stackidx == 0) /* stack is empty, finished */
15074 break;
15075
15076 /* pop an item from the stack */
15077 --stackidx;
15078 gi = stack[stackidx].goodi;
15079 bi = stack[stackidx].badi;
15080 score = stack[stackidx].score;
15081 }
15082
15083 /* When the score goes over "limit" it may actually be much higher.
15084 * Return a very large number to avoid going below the limit when giving a
15085 * bonus. */
15086 if (minscore > limit)
15087 return SCORE_MAXMAX;
15088 return minscore;
15089}
15090
15091#ifdef FEAT_MBYTE
15092/*
15093 * Multi-byte version of spell_edit_score_limit().
15094 * Keep it in sync with the above!
15095 */
15096 static int
15097spell_edit_score_limit_w(slang, badword, goodword, limit)
15098 slang_T *slang;
15099 char_u *badword;
15100 char_u *goodword;
15101 int limit;
15102{
15103 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15104 int stackidx;
15105 int bi, gi;
15106 int bi2, gi2;
15107 int bc, gc;
15108 int score;
15109 int score_off;
15110 int minscore;
15111 int round;
15112 char_u *p;
15113 int wbadword[MAXWLEN];
15114 int wgoodword[MAXWLEN];
15115
15116 /* Get the characters from the multi-byte strings and put them in an
15117 * int array for easy access. */
15118 bi = 0;
15119 for (p = badword; *p != NUL; )
15120 wbadword[bi++] = mb_cptr2char_adv(&p);
15121 wbadword[bi++] = 0;
15122 gi = 0;
15123 for (p = goodword; *p != NUL; )
15124 wgoodword[gi++] = mb_cptr2char_adv(&p);
15125 wgoodword[gi++] = 0;
15126
15127 /*
15128 * The idea is to go from start to end over the words. So long as
15129 * characters are equal just continue, this always gives the lowest score.
15130 * When there is a difference try several alternatives. Each alternative
15131 * increases "score" for the edit distance. Some of the alternatives are
15132 * pushed unto a stack and tried later, some are tried right away. At the
15133 * end of the word the score for one alternative is known. The lowest
15134 * possible score is stored in "minscore".
15135 */
15136 stackidx = 0;
15137 bi = 0;
15138 gi = 0;
15139 score = 0;
15140 minscore = limit + 1;
15141
15142 for (;;)
15143 {
15144 /* Skip over an equal part, score remains the same. */
15145 for (;;)
15146 {
15147 bc = wbadword[bi];
15148 gc = wgoodword[gi];
15149
15150 if (bc != gc) /* stop at a char that's different */
15151 break;
15152 if (bc == NUL) /* both words end */
15153 {
15154 if (score < minscore)
15155 minscore = score;
15156 goto pop; /* do next alternative */
15157 }
15158 ++bi;
15159 ++gi;
15160 }
15161
15162 if (gc == NUL) /* goodword ends, delete badword chars */
15163 {
15164 do
15165 {
15166 if ((score += SCORE_DEL) >= minscore)
15167 goto pop; /* do next alternative */
15168 } while (wbadword[++bi] != NUL);
15169 minscore = score;
15170 }
15171 else if (bc == NUL) /* badword ends, insert badword chars */
15172 {
15173 do
15174 {
15175 if ((score += SCORE_INS) >= minscore)
15176 goto pop; /* do next alternative */
15177 } while (wgoodword[++gi] != NUL);
15178 minscore = score;
15179 }
15180 else /* both words continue */
15181 {
15182 /* If not close to the limit, perform a change. Only try changes
15183 * that may lead to a lower score than "minscore".
15184 * round 0: try deleting a char from badword
15185 * round 1: try inserting a char in badword */
15186 for (round = 0; round <= 1; ++round)
15187 {
15188 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15189 if (score_off < minscore)
15190 {
15191 if (score_off + SCORE_EDIT_MIN >= minscore)
15192 {
15193 /* Near the limit, rest of the words must match. We
15194 * can check that right now, no need to push an item
15195 * onto the stack. */
15196 bi2 = bi + 1 - round;
15197 gi2 = gi + round;
15198 while (wgoodword[gi2] == wbadword[bi2])
15199 {
15200 if (wgoodword[gi2] == NUL)
15201 {
15202 minscore = score_off;
15203 break;
15204 }
15205 ++bi2;
15206 ++gi2;
15207 }
15208 }
15209 else
15210 {
15211 /* try deleting a character from badword later */
15212 stack[stackidx].badi = bi + 1 - round;
15213 stack[stackidx].goodi = gi + round;
15214 stack[stackidx].score = score_off;
15215 ++stackidx;
15216 }
15217 }
15218 }
15219
15220 if (score + SCORE_SWAP < minscore)
15221 {
15222 /* If swapping two characters makes a match then the
15223 * substitution is more expensive, thus there is no need to
15224 * try both. */
15225 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
15226 {
15227 /* Swap two characters, that is: skip them. */
15228 gi += 2;
15229 bi += 2;
15230 score += SCORE_SWAP;
15231 continue;
15232 }
15233 }
15234
15235 /* Substitute one character for another which is the same
15236 * thing as deleting a character from both goodword and badword.
15237 * Use a better score when there is only a case difference. */
15238 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15239 score += SCORE_ICASE;
15240 else
15241 {
15242 /* For a similar character use SCORE_SIMILAR. */
15243 if (slang != NULL
15244 && slang->sl_has_map
15245 && similar_chars(slang, gc, bc))
15246 score += SCORE_SIMILAR;
15247 else
15248 score += SCORE_SUBST;
15249 }
15250
15251 if (score < minscore)
15252 {
15253 /* Do the substitution. */
15254 ++gi;
15255 ++bi;
15256 continue;
15257 }
15258 }
15259pop:
15260 /*
15261 * Get here to try the next alternative, pop it from the stack.
15262 */
15263 if (stackidx == 0) /* stack is empty, finished */
15264 break;
15265
15266 /* pop an item from the stack */
15267 --stackidx;
15268 gi = stack[stackidx].goodi;
15269 bi = stack[stackidx].badi;
15270 score = stack[stackidx].score;
15271 }
15272
15273 /* When the score goes over "limit" it may actually be much higher.
15274 * Return a very large number to avoid going below the limit when giving a
15275 * bonus. */
15276 if (minscore > limit)
15277 return SCORE_MAXMAX;
15278 return minscore;
15279}
15280#endif
15281
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015282/*
15283 * ":spellinfo"
15284 */
15285/*ARGSUSED*/
15286 void
15287ex_spellinfo(eap)
15288 exarg_T *eap;
15289{
15290 int lpi;
15291 langp_T *lp;
15292 char_u *p;
15293
15294 if (no_spell_checking(curwin))
15295 return;
15296
15297 msg_start();
15298 for (lpi = 0; lpi < curbuf->b_langp.ga_len && !got_int; ++lpi)
15299 {
15300 lp = LANGP_ENTRY(curbuf->b_langp, lpi);
15301 msg_puts((char_u *)"file: ");
15302 msg_puts(lp->lp_slang->sl_fname);
15303 msg_putchar('\n');
15304 p = lp->lp_slang->sl_info;
15305 if (p != NULL)
15306 {
15307 msg_puts(p);
15308 msg_putchar('\n');
15309 }
15310 }
15311 msg_end();
15312}
15313
Bram Moolenaar4770d092006-01-12 23:22:24 +000015314#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
15315#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015316#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +000015317#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
15318#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015319
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015320/*
15321 * ":spelldump"
15322 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015323 void
15324ex_spelldump(eap)
15325 exarg_T *eap;
15326{
15327 buf_T *buf = curbuf;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015328
15329 if (no_spell_checking(curwin))
15330 return;
15331
15332 /* Create a new empty buffer by splitting the window. */
15333 do_cmdline_cmd((char_u *)"new");
15334 if (!bufempty() || !buf_valid(buf))
15335 return;
15336
15337 spell_dump_compl(buf, NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
15338
15339 /* Delete the empty line that we started with. */
15340 if (curbuf->b_ml.ml_line_count > 1)
15341 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
15342
15343 redraw_later(NOT_VALID);
15344}
15345
15346/*
15347 * Go through all possible words and:
15348 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
15349 * "ic" and "dir" are not used.
15350 * 2. When "pat" is not NULL: add matching words to insert mode completion.
15351 */
15352 void
15353spell_dump_compl(buf, pat, ic, dir, dumpflags_arg)
15354 buf_T *buf; /* buffer with spell checking */
15355 char_u *pat; /* leading part of the word */
15356 int ic; /* ignore case */
15357 int *dir; /* direction for adding matches */
15358 int dumpflags_arg; /* DUMPFLAG_* */
15359{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015360 langp_T *lp;
15361 slang_T *slang;
15362 idx_T arridx[MAXWLEN];
15363 int curi[MAXWLEN];
15364 char_u word[MAXWLEN];
15365 int c;
15366 char_u *byts;
15367 idx_T *idxs;
15368 linenr_T lnum = 0;
15369 int round;
15370 int depth;
15371 int n;
15372 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015373 char_u *region_names = NULL; /* region names being used */
15374 int do_region = TRUE; /* dump region names and numbers */
15375 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015376 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015377 int dumpflags = dumpflags_arg;
15378 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015379
Bram Moolenaard0131a82006-03-04 21:46:13 +000015380 /* When ignoring case or when the pattern starts with capital pass this on
15381 * to dump_word(). */
15382 if (pat != NULL)
15383 {
15384 if (ic)
15385 dumpflags |= DUMPFLAG_ICASE;
15386 else
15387 {
15388 n = captype(pat, NULL);
15389 if (n == WF_ONECAP)
15390 dumpflags |= DUMPFLAG_ONECAP;
15391 else if (n == WF_ALLCAP
15392#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015393 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015394#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015395 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +000015396#endif
15397 )
15398 dumpflags |= DUMPFLAG_ALLCAP;
15399 }
15400 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015401
Bram Moolenaar7887d882005-07-01 22:33:52 +000015402 /* Find out if we can support regions: All languages must support the same
15403 * regions or none at all. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015404 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000015405 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015406 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000015407 p = lp->lp_slang->sl_regions;
15408 if (p[0] != 0)
15409 {
15410 if (region_names == NULL) /* first language with regions */
15411 region_names = p;
15412 else if (STRCMP(region_names, p) != 0)
15413 {
15414 do_region = FALSE; /* region names are different */
15415 break;
15416 }
15417 }
15418 }
15419
15420 if (do_region && region_names != NULL)
15421 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015422 if (pat == NULL)
15423 {
15424 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15425 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15426 }
Bram Moolenaar7887d882005-07-01 22:33:52 +000015427 }
15428 else
15429 do_region = FALSE;
15430
15431 /*
15432 * Loop over all files loaded for the entries in 'spelllang'.
15433 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015434 for (lpi = 0; lpi < buf->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015435 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015436 lp = LANGP_ENTRY(buf->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015437 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015438 if (slang->sl_fbyts == NULL) /* reloading failed */
15439 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015440
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015441 if (pat == NULL)
15442 {
15443 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15444 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15445 }
15446
15447 /* When matching with a pattern and there are no prefixes only use
15448 * parts of the tree that match "pat". */
15449 if (pat != NULL && slang->sl_pbyts == NULL)
15450 patlen = STRLEN(pat);
15451 else
15452 patlen = 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015453
15454 /* round 1: case-folded tree
15455 * round 2: keep-case tree */
15456 for (round = 1; round <= 2; ++round)
15457 {
15458 if (round == 1)
15459 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015460 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015461 byts = slang->sl_fbyts;
15462 idxs = slang->sl_fidxs;
15463 }
15464 else
15465 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015466 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015467 byts = slang->sl_kbyts;
15468 idxs = slang->sl_kidxs;
15469 }
15470 if (byts == NULL)
15471 continue; /* array is empty */
15472
15473 depth = 0;
15474 arridx[0] = 0;
15475 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015476 while (depth >= 0 && !got_int
15477 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015478 {
15479 if (curi[depth] > byts[arridx[depth]])
15480 {
15481 /* Done all bytes at this node, go up one level. */
15482 --depth;
15483 line_breakcheck();
Bram Moolenaara2031822006-03-07 22:29:51 +000015484 ins_compl_check_keys(50);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015485 }
15486 else
15487 {
15488 /* Do one more byte at this node. */
15489 n = arridx[depth] + curi[depth];
15490 ++curi[depth];
15491 c = byts[n];
15492 if (c == 0)
15493 {
15494 /* End of word, deal with the word.
15495 * Don't use keep-case words in the fold-case tree,
15496 * they will appear in the keep-case tree.
15497 * Only use the word when the region matches. */
15498 flags = (int)idxs[n];
15499 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015500 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000015501 && (do_region
15502 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015503 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015504 & lp->lp_region) != 0))
15505 {
15506 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015507 if (!do_region)
15508 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015509
15510 /* Dump the basic word if there is no prefix or
15511 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015512 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015513 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015514 {
15515 dump_word(slang, word, pat, dir,
15516 dumpflags, flags, lnum);
15517 if (pat == NULL)
15518 ++lnum;
15519 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015520
15521 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015522 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015523 lnum = dump_prefixes(slang, word, pat, dir,
15524 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015525 }
15526 }
15527 else
15528 {
15529 /* Normal char, go one level deeper. */
15530 word[depth++] = c;
15531 arridx[depth] = idxs[n];
15532 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015533
15534 /* Check if this characters matches with the pattern.
15535 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +000015536 * Always ignore case here, dump_word() will check
15537 * proper case later. This isn't exactly right when
15538 * length changes for multi-byte characters with
15539 * ignore case... */
15540 if (depth <= patlen
15541 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015542 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015543 }
15544 }
15545 }
15546 }
15547 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015548}
15549
15550/*
15551 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015552 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015553 */
15554 static void
Bram Moolenaard0131a82006-03-04 21:46:13 +000015555dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015556 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015557 char_u *word;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015558 char_u *pat;
15559 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015560 int dumpflags;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015561 int wordflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015562 linenr_T lnum;
15563{
15564 int keepcap = FALSE;
15565 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015566 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015567 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000015568 char_u badword[MAXWLEN + 10];
15569 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015570 int flags = wordflags;
15571
15572 if (dumpflags & DUMPFLAG_ONECAP)
15573 flags |= WF_ONECAP;
15574 if (dumpflags & DUMPFLAG_ALLCAP)
15575 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015576
Bram Moolenaar4770d092006-01-12 23:22:24 +000015577 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015578 {
15579 /* Need to fix case according to "flags". */
15580 make_case_word(word, cword, flags);
15581 p = cword;
15582 }
15583 else
15584 {
15585 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015586 if ((dumpflags & DUMPFLAG_KEEPCASE)
15587 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015588 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015589 keepcap = TRUE;
15590 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015591 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015592
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015593 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015594 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015595 /* Add flags and regions after a slash. */
15596 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015597 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015598 STRCPY(badword, p);
15599 STRCAT(badword, "/");
15600 if (keepcap)
15601 STRCAT(badword, "=");
15602 if (flags & WF_BANNED)
15603 STRCAT(badword, "!");
15604 else if (flags & WF_RARE)
15605 STRCAT(badword, "?");
15606 if (flags & WF_REGION)
15607 for (i = 0; i < 7; ++i)
15608 if (flags & (0x10000 << i))
15609 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
15610 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015611 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015612
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015613 if (dumpflags & DUMPFLAG_COUNT)
15614 {
15615 hashitem_T *hi;
15616
15617 /* Include the word count for ":spelldump!". */
15618 hi = hash_find(&slang->sl_wordcount, tw);
15619 if (!HASHITEM_EMPTY(hi))
15620 {
15621 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15622 tw, HI2WC(hi)->wc_count);
15623 p = IObuff;
15624 }
15625 }
15626
15627 ml_append(lnum, p, (colnr_T)0, FALSE);
15628 }
Bram Moolenaard0131a82006-03-04 21:46:13 +000015629 else if (((dumpflags & DUMPFLAG_ICASE)
15630 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
15631 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015632 && ins_compl_add_infercase(p, (int)STRLEN(p),
15633 dumpflags & DUMPFLAG_ICASE,
15634 NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015635 /* if dir was BACKWARD then honor it just once */
15636 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015637}
15638
15639/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000015640 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15641 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015642 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015643 * Return the updated line number.
15644 */
15645 static linenr_T
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015646dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015647 slang_T *slang;
15648 char_u *word; /* case-folded word */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015649 char_u *pat;
15650 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015651 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015652 int flags; /* flags with prefix ID */
15653 linenr_T startlnum;
15654{
15655 idx_T arridx[MAXWLEN];
15656 int curi[MAXWLEN];
15657 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015658 char_u word_up[MAXWLEN];
15659 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015660 int c;
15661 char_u *byts;
15662 idx_T *idxs;
15663 linenr_T lnum = startlnum;
15664 int depth;
15665 int n;
15666 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015667 int i;
15668
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015669 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015670 * upper-case letter in word_up[]. */
15671 c = PTR2CHAR(word);
15672 if (SPELL_TOUPPER(c) != c)
15673 {
15674 onecap_copy(word, word_up, TRUE);
15675 has_word_up = TRUE;
15676 }
15677
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015678 byts = slang->sl_pbyts;
15679 idxs = slang->sl_pidxs;
15680 if (byts != NULL) /* array not is empty */
15681 {
15682 /*
15683 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015684 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015685 */
15686 depth = 0;
15687 arridx[0] = 0;
15688 curi[0] = 1;
15689 while (depth >= 0 && !got_int)
15690 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015691 n = arridx[depth];
15692 len = byts[n];
15693 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015694 {
15695 /* Done all bytes at this node, go up one level. */
15696 --depth;
15697 line_breakcheck();
15698 }
15699 else
15700 {
15701 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015702 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015703 ++curi[depth];
15704 c = byts[n];
15705 if (c == 0)
15706 {
15707 /* End of prefix, find out how many IDs there are. */
15708 for (i = 1; i < len; ++i)
15709 if (byts[n + i] != 0)
15710 break;
15711 curi[depth] += i - 1;
15712
Bram Moolenaar53805d12005-08-01 07:08:33 +000015713 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15714 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015715 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015716 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015717 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015718 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015719 : flags, lnum);
15720 if (lnum != 0)
15721 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015722 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000015723
15724 /* Check for prefix that matches the word when the
15725 * first letter is upper-case, but only if the prefix has
15726 * a condition. */
15727 if (has_word_up)
15728 {
15729 c = valid_word_prefix(i, n, flags, word_up, slang,
15730 TRUE);
15731 if (c != 0)
15732 {
15733 vim_strncpy(prefix + depth, word_up,
15734 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015735 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015736 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015737 : flags, lnum);
15738 if (lnum != 0)
15739 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +000015740 }
15741 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015742 }
15743 else
15744 {
15745 /* Normal char, go one level deeper. */
15746 prefix[depth++] = c;
15747 arridx[depth] = idxs[n];
15748 curi[depth] = 1;
15749 }
15750 }
15751 }
15752 }
15753
15754 return lnum;
15755}
15756
Bram Moolenaar95529562005-08-25 21:21:38 +000015757/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015758 * Move "p" to the end of word "start".
15759 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000015760 */
15761 char_u *
15762spell_to_word_end(start, buf)
15763 char_u *start;
15764 buf_T *buf;
15765{
15766 char_u *p = start;
15767
15768 while (*p != NUL && spell_iswordp(p, buf))
15769 mb_ptr_adv(p);
15770 return p;
15771}
15772
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015773#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015774/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015775 * For Insert mode completion CTRL-X s:
15776 * Find start of the word in front of column "startcol".
15777 * We don't check if it is badly spelled, with completion we can only change
15778 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015779 * Returns the column number of the word.
15780 */
15781 int
15782spell_word_start(startcol)
15783 int startcol;
15784{
15785 char_u *line;
15786 char_u *p;
15787 int col = 0;
15788
Bram Moolenaar95529562005-08-25 21:21:38 +000015789 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015790 return startcol;
15791
15792 /* Find a word character before "startcol". */
15793 line = ml_get_curline();
15794 for (p = line + startcol; p > line; )
15795 {
15796 mb_ptr_back(line, p);
15797 if (spell_iswordp_nmw(p))
15798 break;
15799 }
15800
15801 /* Go back to start of the word. */
15802 while (p > line)
15803 {
15804 col = p - line;
15805 mb_ptr_back(line, p);
15806 if (!spell_iswordp(p, curbuf))
15807 break;
15808 col = 0;
15809 }
15810
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015811 return col;
15812}
15813
15814/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000015815 * Need to check for 'spellcapcheck' now, the word is removed before
15816 * expand_spelling() is called. Therefore the ugly global variable.
15817 */
15818static int spell_expand_need_cap;
15819
15820 void
15821spell_expand_check_cap(col)
15822 colnr_T col;
15823{
15824 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
15825}
15826
15827/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015828 * Get list of spelling suggestions.
15829 * Used for Insert mode completion CTRL-X ?.
15830 * Returns the number of matches. The matches are in "matchp[]", array of
15831 * allocated strings.
15832 */
15833/*ARGSUSED*/
15834 int
15835expand_spelling(lnum, col, pat, matchp)
15836 linenr_T lnum;
15837 int col;
15838 char_u *pat;
15839 char_u ***matchp;
15840{
15841 garray_T ga;
15842
Bram Moolenaar4770d092006-01-12 23:22:24 +000015843 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015844 *matchp = ga.ga_data;
15845 return ga.ga_len;
15846}
15847#endif
15848
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000015849#endif /* FEAT_SPELL */