blob: b4d6ca72de0d476dc6f949bd29368bb3cb97feea [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,
Bram Moolenaar6949d1d2008-08-25 02:14:05 +000080 * compute the maximum word score that can be used.
Bram Moolenaar4770d092006-01-12 23:22:24 +000081 */
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
Bram Moolenaar4770d092006-01-12 23:22:24 +0000314#ifndef UNIX /* it's in os_unix.h for Unix */
315# include <time.h> /* for time_t */
316#endif
317
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000318#define MAXWLEN 250 /* Assume max. word len is this many bytes.
319 Some places assume a word length fits in a
320 byte, thus it can't be above 255. */
Bram Moolenaarfc735152005-03-22 22:54:12 +0000321
Bram Moolenaare52325c2005-08-22 22:54:29 +0000322/* Type used for indexes in the word tree need to be at least 4 bytes. If int
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000323 * is 8 bytes we could use something smaller, but what? */
Bram Moolenaare52325c2005-08-22 22:54:29 +0000324#if SIZEOF_INT > 3
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000325typedef int idx_T;
326#else
327typedef long idx_T;
328#endif
329
330/* Flags used for a word. Only the lowest byte can be used, the region byte
331 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000332#define WF_REGION 0x01 /* region byte follows */
333#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
334#define WF_ALLCAP 0x04 /* word must be all capitals */
335#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000336#define WF_BANNED 0x10 /* bad word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000337#define WF_AFX 0x20 /* affix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000338#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000339#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000340
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000341/* for <flags2>, shifted up one byte to be used in wn_flags */
342#define WF_HAS_AFF 0x0100 /* word includes affix */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000343#define WF_NEEDCOMP 0x0200 /* word only valid in compound */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000344#define WF_NOSUGGEST 0x0400 /* word not to be suggested */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000345#define WF_COMPROOT 0x0800 /* already compounded word, COMPOUNDROOT */
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000346#define WF_NOCOMPBEF 0x1000 /* no compounding before this word */
347#define WF_NOCOMPAFT 0x2000 /* no compounding after this word */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000348
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000349/* only used for su_badflags */
350#define WF_MIXCAP 0x20 /* mix of upper and lower case: macaRONI */
351
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000352#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000353
Bram Moolenaar53805d12005-08-01 07:08:33 +0000354/* flags for <pflags> */
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000355#define WFP_RARE 0x01 /* rare prefix */
356#define WFP_NC 0x02 /* prefix is not combining */
357#define WFP_UP 0x04 /* to-upper prefix */
358#define WFP_COMPPERMIT 0x08 /* prefix with COMPOUNDPERMITFLAG */
359#define WFP_COMPFORBID 0x10 /* prefix with COMPOUNDFORBIDFLAG */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000360
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000361/* Flags for postponed prefixes in "sl_pidxs". Must be above affixID (one
362 * byte) and prefcondnr (two bytes). */
363#define WF_RAREPFX (WFP_RARE << 24) /* rare postponed prefix */
364#define WF_PFX_NC (WFP_NC << 24) /* non-combining postponed prefix */
365#define WF_PFX_UP (WFP_UP << 24) /* to-upper postponed prefix */
366#define WF_PFX_COMPPERMIT (WFP_COMPPERMIT << 24) /* postponed prefix with
367 * COMPOUNDPERMITFLAG */
368#define WF_PFX_COMPFORBID (WFP_COMPFORBID << 24) /* postponed prefix with
369 * COMPOUNDFORBIDFLAG */
370
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000371
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000372/* flags for <compoptions> */
373#define COMP_CHECKDUP 1 /* CHECKCOMPOUNDDUP */
374#define COMP_CHECKREP 2 /* CHECKCOMPOUNDREP */
375#define COMP_CHECKCASE 4 /* CHECKCOMPOUNDCASE */
376#define COMP_CHECKTRIPLE 8 /* CHECKCOMPOUNDTRIPLE */
377
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000378/* Special byte values for <byte>. Some are only used in the tree for
379 * postponed prefixes, some only in the other trees. This is a bit messy... */
380#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000381 * postponed prefix: no <pflags> */
382#define BY_INDEX 1 /* child is shared, index follows */
383#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
384 * postponed prefix: <pflags> follows */
385#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
386 * follow; never used in prefix tree */
387#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000388
Bram Moolenaar4770d092006-01-12 23:22:24 +0000389/* Info from "REP", "REPSAL" and "SAL" entries in ".aff" file used in si_rep,
390 * si_repsal, sl_rep, and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000391 * One replacement: from "ft_from" to "ft_to". */
392typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000393{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000394 char_u *ft_from;
395 char_u *ft_to;
396} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000397
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000398/* Info from "SAL" entries in ".aff" file used in sl_sal.
399 * The info is split for quick processing by spell_soundfold().
400 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
401typedef struct salitem_S
402{
403 char_u *sm_lead; /* leading letters */
404 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000405 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000406 char_u *sm_rules; /* rules like ^, $, priority */
407 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000408#ifdef FEAT_MBYTE
409 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000410 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000411 int *sm_to_w; /* wide character copy of "sm_to" */
412#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000413} salitem_T;
414
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000415#ifdef FEAT_MBYTE
416typedef int salfirst_T;
417#else
418typedef short salfirst_T;
419#endif
420
Bram Moolenaar5195e452005-08-19 20:32:47 +0000421/* Values for SP_*ERROR are negative, positive values are used by
422 * read_cnt_string(). */
423#define SP_TRUNCERROR -1 /* spell file truncated error */
424#define SP_FORMERROR -2 /* format error in spell file */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000425#define SP_OTHERERROR -3 /* other error while reading spell file */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000426
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000427/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000428 * Structure used to store words and other info for one language, loaded from
429 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000430 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
431 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
432 *
433 * The "byts" array stores the possible bytes in each tree node, preceded by
434 * the number of possible bytes, sorted on byte value:
435 * <len> <byte1> <byte2> ...
436 * The "idxs" array stores the index of the child node corresponding to the
437 * byte in "byts".
438 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000439 * the flags, region mask and affixID for the word. There may be several
440 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000441 */
442typedef struct slang_S slang_T;
443struct slang_S
444{
445 slang_T *sl_next; /* next language */
446 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000447 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000448 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000449
Bram Moolenaar51485f02005-06-04 21:55:20 +0000450 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000451 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000452 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000453 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000454 char_u *sl_pbyts; /* prefix tree word bytes */
455 idx_T *sl_pidxs; /* prefix tree word indexes */
456
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000457 char_u *sl_info; /* infotext string or NULL */
458
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000459 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000460
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000461 char_u *sl_midword; /* MIDWORD string or NULL */
462
Bram Moolenaar4770d092006-01-12 23:22:24 +0000463 hashtab_T sl_wordcount; /* hashtable with word count, wordcount_T */
464
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000465 int sl_compmax; /* COMPOUNDWORDMAX (default: MAXWLEN) */
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000466 int sl_compminlen; /* COMPOUNDMIN (default: 0) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000467 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000468 int sl_compoptions; /* COMP_* flags */
469 garray_T sl_comppat; /* CHECKCOMPOUNDPATTERN items */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000470 regprog_T *sl_compprog; /* COMPOUNDRULE turned into a regexp progrm
Bram Moolenaar5195e452005-08-19 20:32:47 +0000471 * (NULL when no compounding) */
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000472 char_u *sl_comprules; /* all COMPOUNDRULE concatenated (or NULL) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000473 char_u *sl_compstartflags; /* flags for first compound word */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000474 char_u *sl_compallflags; /* all flags for compound words */
Bram Moolenaar78622822005-08-23 21:00:13 +0000475 char_u sl_nobreak; /* When TRUE: no spaces between words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000476 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
477 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000478
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000479 int sl_prefixcnt; /* number of items in "sl_prefprog" */
480 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
481
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000482 garray_T sl_rep; /* list of fromto_T entries from REP lines */
483 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
484 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000485 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000486 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000487 there is none */
488 int sl_followup; /* SAL followup */
489 int sl_collapse; /* SAL collapse_result */
490 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000491 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
492 * "sl_sal_first" maps chars, when has_mbyte
493 * "sl_sal" is a list of wide char lists. */
494 garray_T sl_repsal; /* list of fromto_T entries from REPSAL lines */
495 short sl_repsal_first[256]; /* sl_rep_first for REPSAL lines */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000496 int sl_nosplitsugs; /* don't suggest splitting a word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000497
498 /* Info from the .sug file. Loaded on demand. */
499 time_t sl_sugtime; /* timestamp for .sug file */
500 char_u *sl_sbyts; /* soundfolded word bytes */
501 idx_T *sl_sidxs; /* soundfolded word indexes */
502 buf_T *sl_sugbuf; /* buffer with word number table */
503 int sl_sugloaded; /* TRUE when .sug file was loaded or failed to
504 load */
505
Bram Moolenaarea424162005-06-16 21:51:00 +0000506 int sl_has_map; /* TRUE if there is a MAP line */
507#ifdef FEAT_MBYTE
508 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
509 int sl_map_array[256]; /* MAP for first 256 chars */
510#else
511 char_u sl_map_array[256]; /* MAP for first 256 chars */
512#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000513 hashtab_T sl_sounddone; /* table with soundfolded words that have
514 handled, see add_sound_suggest() */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000515};
516
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000517/* First language that is loaded, start of the linked list of loaded
518 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000519static slang_T *first_lang = NULL;
520
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000521/* Flags used in .spl file for soundsalike flags. */
522#define SAL_F0LLOWUP 1
523#define SAL_COLLAPSE 2
524#define SAL_REM_ACCENTS 4
525
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000526/*
527 * Structure used in "b_langp", filled from 'spelllang'.
528 */
529typedef struct langp_S
530{
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000531 slang_T *lp_slang; /* info for this language */
532 slang_T *lp_sallang; /* language used for sound folding or NULL */
533 slang_T *lp_replang; /* language used for REP items or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000534 int lp_region; /* bitmask for region or REGION_ALL */
535} langp_T;
536
537#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
538
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000539#define REGION_ALL 0xff /* word valid in all regions */
540
Bram Moolenaar5195e452005-08-19 20:32:47 +0000541#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
542#define VIMSPELLMAGICL 8
543#define VIMSPELLVERSION 50
544
Bram Moolenaar4770d092006-01-12 23:22:24 +0000545#define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */
546#define VIMSUGMAGICL 6
547#define VIMSUGVERSION 1
548
Bram Moolenaar5195e452005-08-19 20:32:47 +0000549/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
550#define SN_REGION 0 /* <regionname> section */
551#define SN_CHARFLAGS 1 /* charflags section */
552#define SN_MIDWORD 2 /* <midword> section */
553#define SN_PREFCOND 3 /* <prefcond> section */
554#define SN_REP 4 /* REP items section */
555#define SN_SAL 5 /* SAL items section */
556#define SN_SOFO 6 /* soundfolding section */
557#define SN_MAP 7 /* MAP items section */
558#define SN_COMPOUND 8 /* compound words section */
559#define SN_SYLLABLE 9 /* syllable section */
Bram Moolenaar78622822005-08-23 21:00:13 +0000560#define SN_NOBREAK 10 /* NOBREAK section */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000561#define SN_SUGFILE 11 /* timestamp for .sug file */
562#define SN_REPSAL 12 /* REPSAL items section */
563#define SN_WORDS 13 /* common words */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000564#define SN_NOSPLITSUGS 14 /* don't split word for suggestions */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000565#define SN_INFO 15 /* info section */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000566#define SN_END 255 /* end of sections */
567
568#define SNF_REQUIRED 1 /* <sectionflags>: required section */
569
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000570/* Result values. Lower number is accepted over higher one. */
571#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000572#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000573#define SP_RARE 1
574#define SP_LOCAL 2
575#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000576
Bram Moolenaar7887d882005-07-01 22:33:52 +0000577/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000578static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000579
Bram Moolenaar4770d092006-01-12 23:22:24 +0000580typedef struct wordcount_S
581{
582 short_u wc_count; /* nr of times word was seen */
583 char_u wc_word[1]; /* word, actually longer */
584} wordcount_T;
585
586static wordcount_T dumwc;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000587#define WC_KEY_OFF (unsigned)(dumwc.wc_word - (char_u *)&dumwc)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000588#define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF))
589#define MAXWORDCOUNT 0xffff
590
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000591/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000592 * Information used when looking for suggestions.
593 */
594typedef struct suginfo_S
595{
596 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000597 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000598 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000599 int su_sfmaxscore; /* idem, for when doing soundfold words */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000600 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000601 char_u *su_badptr; /* start of bad word in line */
602 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000603 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000604 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
605 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000606 char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000607 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000608 slang_T *su_sallang; /* default language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000609} suginfo_T;
610
611/* One word suggestion. Used in "si_ga". */
612typedef struct suggest_S
613{
614 char_u *st_word; /* suggested word, allocated string */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000615 int st_wordlen; /* STRLEN(st_word) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000616 int st_orglen; /* length of replaced text */
617 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000618 int st_altscore; /* used when st_score compares equal */
619 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000620 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000621 slang_T *st_slang; /* language used for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000622} suggest_T;
623
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000624#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000625
Bram Moolenaar4770d092006-01-12 23:22:24 +0000626/* TRUE if a word appears in the list of banned words. */
627#define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word)))
628
Bram Moolenaar6949d1d2008-08-25 02:14:05 +0000629/* Number of suggestions kept when cleaning up. We need to keep more than
Bram Moolenaar4770d092006-01-12 23:22:24 +0000630 * what is displayed, because when rescore_suggestions() is called the score
631 * may change and wrong suggestions may be removed later. */
632#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000633
634/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
635 * of suggestions that are not going to be displayed. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000636#define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000637
638/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000639#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000640#define SCORE_SPLIT_NO 249 /* split bad word with NOSPLITSUGS */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000641#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000642#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000643#define SCORE_RARE 180 /* rare word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000644#define SCORE_SWAP 75 /* swap two characters */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000645#define SCORE_SWAP3 110 /* swap two characters in three */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000646#define SCORE_REP 65 /* REP replacement */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000647#define SCORE_SUBST 93 /* substitute a character */
648#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000649#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000650#define SCORE_DEL 94 /* delete a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000651#define SCORE_DELDUP 66 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000652#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000653#define SCORE_INS 96 /* insert a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000654#define SCORE_INSDUP 67 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000655#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000656#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000657
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000658#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000659#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
660 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000661
Bram Moolenaar4770d092006-01-12 23:22:24 +0000662#define SCORE_COMMON1 30 /* subtracted for words seen before */
663#define SCORE_COMMON2 40 /* subtracted for words often seen */
664#define SCORE_COMMON3 50 /* subtracted for words very often seen */
665#define SCORE_THRES2 10 /* word count threshold for COMMON2 */
666#define SCORE_THRES3 100 /* word count threshold for COMMON3 */
667
668/* When trying changed soundfold words it becomes slow when trying more than
669 * two changes. With less then two changes it's slightly faster but we miss a
670 * few good suggestions. In rare cases we need to try three of four changes.
671 */
672#define SCORE_SFMAX1 200 /* maximum score for first try */
673#define SCORE_SFMAX2 300 /* maximum score for second try */
674#define SCORE_SFMAX3 400 /* maximum score for third try */
675
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000676#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000677#define SCORE_MAXMAX 999999 /* accept any score */
678#define SCORE_LIMITMAX 350 /* for spell_edit_score_limit() */
679
680/* for spell_edit_score_limit() we need to know the minimum value of
681 * SCORE_ICASE, SCORE_SWAP, SCORE_DEL, SCORE_SIMILAR and SCORE_INS */
682#define SCORE_EDIT_MIN SCORE_SIMILAR
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000683
684/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000685 * Structure to store info for word matching.
686 */
687typedef struct matchinf_S
688{
689 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000690
691 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000692 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000693 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000694 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000695 char_u *mi_cend; /* char after what was used for
696 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000697
698 /* case-folded text */
699 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000700 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000701
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000702 /* for when checking word after a prefix */
703 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000704 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000705 int mi_prefcnt; /* number of entries at mi_prefarridx */
706 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000707#ifdef FEAT_MBYTE
708 int mi_cprefixlen; /* byte length of prefix in original
709 case */
710#else
711# define mi_cprefixlen mi_prefixlen /* it's the same value */
712#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000713
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000714 /* for when checking a compound word */
715 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000716 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
717 int mi_complen; /* nr of compound words used */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000718 int mi_compextra; /* nr of COMPOUNDROOT words */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000719
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000720 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000721 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000722 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200723 win_T *mi_win; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000724
725 /* for NOBREAK */
726 int mi_result2; /* "mi_resul" without following word */
727 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000728} matchinf_T;
729
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000730/*
731 * The tables used for recognizing word characters according to spelling.
732 * These are only used for the first 256 characters of 'encoding'.
733 */
734typedef struct spelltab_S
735{
736 char_u st_isw[256]; /* flags: is word char */
737 char_u st_isu[256]; /* flags: is uppercase char */
738 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000739 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000740} spelltab_T;
741
742static spelltab_T spelltab;
743static int did_set_spelltab;
744
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000745#define CF_WORD 0x01
746#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000747
748static void clear_spell_chartab __ARGS((spelltab_T *sp));
749static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200750static int spell_iswordp __ARGS((char_u *p, win_T *wp));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000751static int spell_iswordp_nmw __ARGS((char_u *p));
752#ifdef FEAT_MBYTE
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +0000753static int spell_mb_isword_class __ARGS((int cl));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200754static int spell_iswordp_w __ARGS((int *p, win_T *wp));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000755#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000756static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000757
758/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000759 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000760 */
761typedef enum
762{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000763 STATE_START = 0, /* At start of node check for NUL bytes (goodword
764 * ends); if badword ends there is a match, otherwise
765 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000766 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000767 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000768 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
769 STATE_PLAIN, /* Use each byte of the node. */
770 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000771 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000772 STATE_INS, /* Insert a byte in the bad word. */
773 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000774 STATE_UNSWAP, /* Undo swap two characters. */
775 STATE_SWAP3, /* Swap two characters over three. */
776 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000777 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000778 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000779 STATE_REP_INI, /* Prepare for using REP items. */
780 STATE_REP, /* Use matching REP items from the .aff file. */
781 STATE_REP_UNDO, /* Undo a REP item replacement. */
782 STATE_FINAL /* End of this node. */
783} state_T;
784
785/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000786 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000787 */
788typedef struct trystate_S
789{
Bram Moolenaarea424162005-06-16 21:51:00 +0000790 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000791 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000792 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000793 short ts_curi; /* index in list of child nodes */
794 char_u ts_fidx; /* index in fword[], case-folded bad word */
795 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
796 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000797 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000798 * PFD_PREFIXTREE or PFD_NOPREFIX */
799 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000800#ifdef FEAT_MBYTE
801 char_u ts_tcharlen; /* number of bytes in tword character */
802 char_u ts_tcharidx; /* current byte index in tword character */
803 char_u ts_isdiff; /* DIFF_ values */
804 char_u ts_fcharstart; /* index in fword where badword char started */
805#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000806 char_u ts_prewordlen; /* length of word in "preword[]" */
807 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000808 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000809 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000810 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000811 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000812 char_u ts_delidx; /* index in fword for char that was deleted,
813 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000814} trystate_T;
815
Bram Moolenaarea424162005-06-16 21:51:00 +0000816/* values for ts_isdiff */
817#define DIFF_NONE 0 /* no different byte (yet) */
818#define DIFF_YES 1 /* different byte found */
819#define DIFF_INSERT 2 /* inserting character */
820
Bram Moolenaard12a1322005-08-21 22:08:24 +0000821/* values for ts_flags */
822#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
823#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000824#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000825
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000826/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000827#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000828#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000829#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000830
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000831/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000832#define FIND_FOLDWORD 0 /* find word case-folded */
833#define FIND_KEEPWORD 1 /* find keep-case word */
834#define FIND_PREFIX 2 /* find word after prefix */
835#define FIND_COMPOUND 3 /* find case-folded compound word */
836#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000837
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000838static slang_T *slang_alloc __ARGS((char_u *lang));
839static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000840static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000841static void slang_clear_sug __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000842static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000843static int match_checkcompoundpattern __ARGS((char_u *ptr, int wlen, garray_T *gap));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000844static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000845static int can_be_compound __ARGS((trystate_T *sp, slang_T *slang, char_u *compflags, int flag));
846static int match_compoundrule __ARGS((slang_T *slang, char_u *compflags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000847static int valid_word_prefix __ARGS((int totprefcnt, int arridx, int flags, char_u *word, slang_T *slang, int cond_req));
Bram Moolenaard12a1322005-08-21 22:08:24 +0000848static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000849static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000850static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000851static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000852static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000853static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000854static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000855static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000856static slang_T *spell_load_file __ARGS((char_u *fname, char_u *lang, slang_T *old_lp, int silent));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000857static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000858static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
859static int read_charflags_section __ARGS((FILE *fd));
860static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000861static int read_rep_section __ARGS((FILE *fd, garray_T *gap, short *first));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000862static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000863static int read_words_section __ARGS((FILE *fd, slang_T *lp, int len));
864static void count_common_word __ARGS((slang_T *lp, char_u *word, int len, int count));
865static int score_wordcount_adj __ARGS((slang_T *slang, int score, char_u *word, int split));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000866static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
867static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000868static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000869static int init_syl_tab __ARGS((slang_T *slang));
870static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000871static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
872static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000873#ifdef FEAT_MBYTE
874static int *mb_str2wide __ARGS((char_u *s));
875#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000876static int spell_read_tree __ARGS((FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200877static idx_T read_tree_node __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, idx_T startidx, int prefixtree, int maxprefcondnr));
878static void clear_midword __ARGS((win_T *buf));
879static void use_midword __ARGS((slang_T *lp, win_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000880static int find_region __ARGS((char_u *rp, char_u *region));
881static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000882static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000883static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000884static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000885static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000886static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000887static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar66fa2712006-01-22 23:22:22 +0000888static void spell_find_suggest __ARGS((char_u *badptr, int badlen, suginfo_T *su, int maxcount, int banbadword, int need_cap, int interactive));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000889#ifdef FEAT_EVAL
890static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
891#endif
892static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000893static void spell_suggest_intern __ARGS((suginfo_T *su, int interactive));
894static void suggest_load_files __ARGS((void));
895static void tree_count_words __ARGS((char_u *byts, idx_T *idxs));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000896static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000897static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000898static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000899static void suggest_try_special __ARGS((suginfo_T *su));
900static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000901static void suggest_trie_walk __ARGS((suginfo_T *su, langp_T *lp, char_u *fword, int soundfold));
902static void go_deeper __ARGS((trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000903#ifdef FEAT_MBYTE
904static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
905#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000906static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000907static void score_comp_sal __ARGS((suginfo_T *su));
908static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000909static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000910static void suggest_try_soundalike_prep __ARGS((void));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000911static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000912static void suggest_try_soundalike_finish __ARGS((void));
913static void add_sound_suggest __ARGS((suginfo_T *su, char_u *goodword, int score, langp_T *lp));
914static int soundfold_find __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000915static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000916static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000917static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000918static void add_suggestion __ARGS((suginfo_T *su, garray_T *gap, char_u *goodword, int badlen, int score, int altscore, int had_bonus, slang_T *slang, int maxsf));
919static void check_suggestions __ARGS((suginfo_T *su, garray_T *gap));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000920static void add_banned __ARGS((suginfo_T *su, char_u *word));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000921static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000922static void rescore_one __ARGS((suginfo_T *su, suggest_T *stp));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000923static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000924static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
925static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
926static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000927#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000928static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000929#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000930static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000931static int spell_edit_score __ARGS((slang_T *slang, char_u *badword, char_u *goodword));
932static int spell_edit_score_limit __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
933#ifdef FEAT_MBYTE
934static int spell_edit_score_limit_w __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
935#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +0000936static void dump_word __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum));
937static linenr_T dump_prefixes __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T startlnum));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000938static buf_T *open_spellbuf __ARGS((void));
939static void close_spellbuf __ARGS((buf_T *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000940
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000941/*
942 * Use our own character-case definitions, because the current locale may
943 * differ from what the .spl file uses.
944 * These must not be called with negative number!
945 */
946#ifndef FEAT_MBYTE
947/* Non-multi-byte implementation. */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000948# define SPELL_TOFOLD(c) ((c) < 256 ? (int)spelltab.st_fold[c] : (c))
949# define SPELL_TOUPPER(c) ((c) < 256 ? (int)spelltab.st_upper[c] : (c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000950# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
951#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000952# if defined(HAVE_WCHAR_H)
953# include <wchar.h> /* for towupper() and towlower() */
954# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000955/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
956 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
957 * the "w" library function for characters above 255 if available. */
958# ifdef HAVE_TOWLOWER
959# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000960 : (c) < 256 ? (int)spelltab.st_fold[c] : (int)towlower(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000961# else
962# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000963 : (c) < 256 ? (int)spelltab.st_fold[c] : (c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000964# endif
965
966# ifdef HAVE_TOWUPPER
967# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000968 : (c) < 256 ? (int)spelltab.st_upper[c] : (int)towupper(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000969# else
970# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000971 : (c) < 256 ? (int)spelltab.st_upper[c] : (c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000972# endif
973
974# ifdef HAVE_ISWUPPER
975# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
976 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
977# else
978# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000979 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000980# endif
981#endif
982
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000983
984static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000985static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000986static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000987static char *e_affname = N_("Affix name too long in %s line %d: %s");
988static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
989static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +0000990static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000991
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000992/* Remember what "z?" replaced. */
993static char_u *repl_from = NULL;
994static char_u *repl_to = NULL;
995
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000996/*
997 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000998 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000999 * "*attrp" is set to the highlight index for a badly spelled word. For a
1000 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001001 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001002 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001003 * "capcol" is used to check for a Capitalised word after the end of a
1004 * sentence. If it's zero then perform the check. Return the column where to
1005 * check next, or -1 when no sentence end was found. If it's NULL then don't
1006 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001007 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001008 * Returns the length of the word in bytes, also when it's OK, so that the
1009 * caller can skip over the word.
1010 */
1011 int
Bram Moolenaar4770d092006-01-12 23:22:24 +00001012spell_check(wp, ptr, attrp, capcol, docount)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001013 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001014 char_u *ptr;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001015 hlf_T *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001016 int *capcol; /* column to check for Capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001017 int docount; /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001018{
1019 matchinf_T mi; /* Most things are put in "mi" so that it can
1020 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001021 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001022 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001023 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001024 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001025 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001026
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001027 /* A word never starts at a space or a control character. Return quickly
1028 * then, skipping over the character. */
1029 if (*ptr <= ' ')
1030 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001031
1032 /* Return here when loading language files failed. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001033 if (wp->w_s->b_langp.ga_len == 0)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001034 return 1;
1035
Bram Moolenaar5195e452005-08-19 20:32:47 +00001036 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001037
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001038 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +00001039 * 0X99FF. But always do check spelling to find "3GPP" and "11
1040 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001041 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +00001042 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001043 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
1044 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001045 else
1046 mi.mi_end = skipdigits(ptr);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001047 nrlen = (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001048 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001049
Bram Moolenaar0c405862005-06-22 22:26:26 +00001050 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001051 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +00001052 mi.mi_fend = ptr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001053 if (spell_iswordp(mi.mi_fend, wp))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001054 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001055 do
Bram Moolenaar51485f02005-06-04 21:55:20 +00001056 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001057 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001058 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp));
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001059
Bram Moolenaar860cae12010-06-05 23:22:07 +02001060 if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001061 {
1062 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00001063 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001064 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001065 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001066 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001067 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001068 if (capcol != NULL)
1069 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001070
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001071 /* We always use the characters up to the next non-word character,
1072 * also for bad words. */
1073 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001074
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001075 /* Check caps type later. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001076 mi.mi_capflags = 0;
1077 mi.mi_cend = NULL;
1078 mi.mi_win = wp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001079
Bram Moolenaar5195e452005-08-19 20:32:47 +00001080 /* case-fold the word with one non-word character, so that we can check
1081 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001082 if (*mi.mi_fend != NUL)
1083 mb_ptr_adv(mi.mi_fend);
1084
1085 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
1086 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001087 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001088
1089 /* The word is bad unless we recognize it. */
1090 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +00001091 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001092
1093 /*
1094 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001095 * We check them all, because a word may be matched longer in another
1096 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001097 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001098 for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001099 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001100 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001101
1102 /* If reloading fails the language is still in the list but everything
1103 * has been cleared. */
1104 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
1105 continue;
1106
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001107 /* Check for a matching word in case-folded words. */
1108 find_word(&mi, FIND_FOLDWORD);
1109
1110 /* Check for a matching word in keep-case words. */
1111 find_word(&mi, FIND_KEEPWORD);
1112
1113 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001114 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +00001115
1116 /* For a NOBREAK language, may want to use a word without a following
1117 * word as a backup. */
1118 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
1119 && mi.mi_result2 != SP_BAD)
1120 {
1121 mi.mi_result = mi.mi_result2;
1122 mi.mi_end = mi.mi_end2;
1123 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001124
1125 /* Count the word in the first language where it's found to be OK. */
1126 if (count_word && mi.mi_result == SP_OK)
1127 {
1128 count_common_word(mi.mi_lp->lp_slang, ptr,
1129 (int)(mi.mi_end - ptr), 1);
1130 count_word = FALSE;
1131 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001132 }
1133
1134 if (mi.mi_result != SP_OK)
1135 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001136 /* If we found a number skip over it. Allows for "42nd". Do flag
1137 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001138 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001139 {
1140 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1141 return nrlen;
1142 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001143
1144 /* When we are at a non-word character there is no error, just
1145 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001146 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001147 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001148 if (capcol != NULL && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001149 {
1150 regmatch_T regmatch;
1151
1152 /* Check for end of sentence. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001153 regmatch.regprog = wp->w_s->b_cap_prog;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001154 regmatch.rm_ic = FALSE;
1155 if (vim_regexec(&regmatch, ptr, 0))
1156 *capcol = (int)(regmatch.endp[0] - ptr);
1157 }
1158
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001159#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001160 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001161 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001162#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001163 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001164 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001165 else if (mi.mi_end == ptr)
1166 /* Always include at least one character. Required for when there
1167 * is a mixup in "midword". */
1168 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +00001169 else if (mi.mi_result == SP_BAD
Bram Moolenaar860cae12010-06-05 23:22:07 +02001170 && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak)
Bram Moolenaar78622822005-08-23 21:00:13 +00001171 {
1172 char_u *p, *fp;
1173 int save_result = mi.mi_result;
1174
1175 /* First language in 'spelllang' is NOBREAK. Find first position
1176 * at which any word would be valid. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001177 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001178 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +00001179 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001180 p = mi.mi_word;
1181 fp = mi.mi_fword;
1182 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +00001183 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001184 mb_ptr_adv(p);
1185 mb_ptr_adv(fp);
1186 if (p >= mi.mi_end)
1187 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001188 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001189 find_word(&mi, FIND_COMPOUND);
1190 if (mi.mi_result != SP_BAD)
1191 {
1192 mi.mi_end = p;
1193 break;
1194 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001195 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001196 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001197 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001198 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001199
1200 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001201 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001202 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001203 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001204 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001205 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001206 }
1207
Bram Moolenaar5195e452005-08-19 20:32:47 +00001208 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1209 {
1210 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001211 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001212 return wrongcaplen;
1213 }
1214
Bram Moolenaar51485f02005-06-04 21:55:20 +00001215 return (int)(mi.mi_end - ptr);
1216}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001217
Bram Moolenaar51485f02005-06-04 21:55:20 +00001218/*
1219 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001220 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1221 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1222 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1223 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001224 *
1225 * For a match mip->mi_result is updated.
1226 */
1227 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001228find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001229 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001230 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001231{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001232 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001233 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001234 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001235 int endidxcnt = 0;
1236 int len;
1237 int wlen = 0;
1238 int flen;
1239 int c;
1240 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001241 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001242#ifdef FEAT_MBYTE
1243 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001244#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001245 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001246 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001247 slang_T *slang = mip->mi_lp->lp_slang;
1248 unsigned flags;
1249 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001250 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001251 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001252 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001253 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001254
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001255 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001256 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001257 /* Check for word with matching case in keep-case tree. */
1258 ptr = mip->mi_word;
1259 flen = 9999; /* no case folding, always enough bytes */
1260 byts = slang->sl_kbyts;
1261 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001262
1263 if (mode == FIND_KEEPCOMPOUND)
1264 /* Skip over the previously found word(s). */
1265 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001266 }
1267 else
1268 {
1269 /* Check for case-folded in case-folded tree. */
1270 ptr = mip->mi_fword;
1271 flen = mip->mi_fwordlen; /* available case-folded bytes */
1272 byts = slang->sl_fbyts;
1273 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001274
1275 if (mode == FIND_PREFIX)
1276 {
1277 /* Skip over the prefix. */
1278 wlen = mip->mi_prefixlen;
1279 flen -= mip->mi_prefixlen;
1280 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001281 else if (mode == FIND_COMPOUND)
1282 {
1283 /* Skip over the previously found word(s). */
1284 wlen = mip->mi_compoff;
1285 flen -= mip->mi_compoff;
1286 }
1287
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001288 }
1289
Bram Moolenaar51485f02005-06-04 21:55:20 +00001290 if (byts == NULL)
1291 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001292
Bram Moolenaar51485f02005-06-04 21:55:20 +00001293 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001294 * Repeat advancing in the tree until:
1295 * - there is a byte that doesn't match,
1296 * - we reach the end of the tree,
1297 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001298 */
1299 for (;;)
1300 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001301 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001302 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001303
1304 len = byts[arridx++];
1305
1306 /* If the first possible byte is a zero the word could end here.
1307 * Remember this index, we first check for the longest word. */
1308 if (byts[arridx] == 0)
1309 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001310 if (endidxcnt == MAXWLEN)
1311 {
1312 /* Must be a corrupted spell file. */
1313 EMSG(_(e_format));
1314 return;
1315 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001316 endlen[endidxcnt] = wlen;
1317 endidx[endidxcnt++] = arridx++;
1318 --len;
1319
1320 /* Skip over the zeros, there can be several flag/region
1321 * combinations. */
1322 while (len > 0 && byts[arridx] == 0)
1323 {
1324 ++arridx;
1325 --len;
1326 }
1327 if (len == 0)
1328 break; /* no children, word must end here */
1329 }
1330
1331 /* Stop looking at end of the line. */
1332 if (ptr[wlen] == NUL)
1333 break;
1334
1335 /* Perform a binary search in the list of accepted bytes. */
1336 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001337 if (c == TAB) /* <Tab> is handled like <Space> */
1338 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001339 lo = arridx;
1340 hi = arridx + len - 1;
1341 while (lo < hi)
1342 {
1343 m = (lo + hi) / 2;
1344 if (byts[m] > c)
1345 hi = m - 1;
1346 else if (byts[m] < c)
1347 lo = m + 1;
1348 else
1349 {
1350 lo = hi = m;
1351 break;
1352 }
1353 }
1354
1355 /* Stop if there is no matching byte. */
1356 if (hi < lo || byts[lo] != c)
1357 break;
1358
1359 /* Continue at the child (if there is one). */
1360 arridx = idxs[lo];
1361 ++wlen;
1362 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001363
1364 /* One space in the good word may stand for several spaces in the
1365 * checked word. */
1366 if (c == ' ')
1367 {
1368 for (;;)
1369 {
1370 if (flen <= 0 && *mip->mi_fend != NUL)
1371 flen = fold_more(mip);
1372 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1373 break;
1374 ++wlen;
1375 --flen;
1376 }
1377 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001378 }
1379
1380 /*
1381 * Verify that one of the possible endings is valid. Try the longest
1382 * first.
1383 */
1384 while (endidxcnt > 0)
1385 {
1386 --endidxcnt;
1387 arridx = endidx[endidxcnt];
1388 wlen = endlen[endidxcnt];
1389
1390#ifdef FEAT_MBYTE
1391 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1392 continue; /* not at first byte of character */
1393#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02001394 if (spell_iswordp(ptr + wlen, mip->mi_win))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001395 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001396 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001397 continue; /* next char is a word character */
1398 word_ends = FALSE;
1399 }
1400 else
1401 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001402 /* The prefix flag is before compound flags. Once a valid prefix flag
1403 * has been found we try compound flags. */
1404 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001405
1406#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001407 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001408 {
1409 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001410 * when folding case. This can be slow, take a shortcut when the
1411 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001412 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001413 if (STRNCMP(ptr, p, wlen) != 0)
1414 {
1415 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1416 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001417 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001418 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001419 }
1420#endif
1421
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001422 /* Check flags and region. For FIND_PREFIX check the condition and
1423 * prefix ID.
1424 * Repeat this if there are more flags/region alternatives until there
1425 * is a match. */
1426 res = SP_BAD;
1427 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1428 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001429 {
1430 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001431
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001432 /* For the fold-case tree check that the case of the checked word
1433 * matches with what the word in the tree requires.
1434 * For keep-case tree the case is always right. For prefixes we
1435 * don't bother to check. */
1436 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001437 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001438 if (mip->mi_cend != mip->mi_word + wlen)
1439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001440 /* mi_capflags was set for a different word length, need
1441 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001442 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001443 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001444 }
1445
Bram Moolenaar0c405862005-06-22 22:26:26 +00001446 if (mip->mi_capflags == WF_KEEPCAP
1447 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001448 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001449 }
1450
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001451 /* When mode is FIND_PREFIX the word must support the prefix:
1452 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001453 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001454 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001455 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001456 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001457 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001458 mip->mi_word + mip->mi_cprefixlen, slang,
1459 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001460 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001461 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001462
1463 /* Use the WF_RARE flag for a rare prefix. */
1464 if (c & WF_RAREPFX)
1465 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001466 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001467 }
1468
Bram Moolenaar78622822005-08-23 21:00:13 +00001469 if (slang->sl_nobreak)
1470 {
1471 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1472 && (flags & WF_BANNED) == 0)
1473 {
1474 /* NOBREAK: found a valid following word. That's all we
1475 * need to know, so return. */
1476 mip->mi_result = SP_OK;
1477 break;
1478 }
1479 }
1480
1481 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1482 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001483 {
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00001484 /* If there is no compound flag or the word is shorter than
Bram Moolenaar5195e452005-08-19 20:32:47 +00001485 * COMPOUNDMIN reject it quickly.
1486 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001487 * that's too short... Myspell compatibility requires this
1488 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001489 if (((unsigned)flags >> 24) == 0
1490 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001491 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001492#ifdef FEAT_MBYTE
1493 /* For multi-byte chars check character length against
1494 * COMPOUNDMIN. */
1495 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001496 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001497 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1498 wlen - mip->mi_compoff) < slang->sl_compminlen)
1499 continue;
1500#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001501
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001502 /* Limit the number of compound words to COMPOUNDWORDMAX if no
Bram Moolenaare52325c2005-08-22 22:54:29 +00001503 * maximum for syllables is specified. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001504 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
1505 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +00001506 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001507 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001508
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001509 /* Don't allow compounding on a side where an affix was added,
1510 * unless COMPOUNDPERMITFLAG was used. */
1511 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
1512 continue;
1513 if (!word_ends && (flags & WF_NOCOMPAFT))
1514 continue;
1515
Bram Moolenaard12a1322005-08-21 22:08:24 +00001516 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001517 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001518 ? slang->sl_compstartflags
1519 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001520 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001521 continue;
1522
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001523 /* If there is a match with a CHECKCOMPOUNDPATTERN rule
1524 * discard the compound word. */
1525 if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat))
1526 continue;
1527
Bram Moolenaare52325c2005-08-22 22:54:29 +00001528 if (mode == FIND_COMPOUND)
1529 {
1530 int capflags;
1531
1532 /* Need to check the caps type of the appended compound
1533 * word. */
1534#ifdef FEAT_MBYTE
1535 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1536 mip->mi_compoff) != 0)
1537 {
1538 /* case folding may have changed the length */
1539 p = mip->mi_word;
1540 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1541 mb_ptr_adv(p);
1542 }
1543 else
1544#endif
1545 p = mip->mi_word + mip->mi_compoff;
1546 capflags = captype(p, mip->mi_word + wlen);
1547 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1548 && (flags & WF_FIXCAP) != 0))
1549 continue;
1550
1551 if (capflags != WF_ALLCAP)
1552 {
1553 /* When the character before the word is a word
1554 * character we do not accept a Onecap word. We do
1555 * accept a no-caps word, even when the dictionary
1556 * word specifies ONECAP. */
1557 mb_ptr_back(mip->mi_word, p);
1558 if (spell_iswordp_nmw(p)
1559 ? capflags == WF_ONECAP
1560 : (flags & WF_ONECAP) != 0
1561 && capflags != WF_ONECAP)
1562 continue;
1563 }
1564 }
1565
Bram Moolenaar5195e452005-08-19 20:32:47 +00001566 /* If the word ends the sequence of compound flags of the
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001567 * words must match with one of the COMPOUNDRULE items and
Bram Moolenaar5195e452005-08-19 20:32:47 +00001568 * the number of syllables must not be too large. */
1569 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1570 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1571 if (word_ends)
1572 {
1573 char_u fword[MAXWLEN];
1574
1575 if (slang->sl_compsylmax < MAXWLEN)
1576 {
1577 /* "fword" is only needed for checking syllables. */
1578 if (ptr == mip->mi_word)
1579 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1580 else
1581 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1582 }
1583 if (!can_compound(slang, fword, mip->mi_compflags))
1584 continue;
1585 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001586 else if (slang->sl_comprules != NULL
1587 && !match_compoundrule(slang, mip->mi_compflags))
1588 /* The compound flags collected so far do not match any
1589 * COMPOUNDRULE, discard the compounded word. */
1590 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001591 }
1592
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001593 /* Check NEEDCOMPOUND: can't use word without compounding. */
1594 else if (flags & WF_NEEDCOMP)
1595 continue;
1596
Bram Moolenaar78622822005-08-23 21:00:13 +00001597 nobreak_result = SP_OK;
1598
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001599 if (!word_ends)
1600 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001601 int save_result = mip->mi_result;
1602 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001603 langp_T *save_lp = mip->mi_lp;
1604 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001605
1606 /* Check that a valid word follows. If there is one and we
1607 * are compounding, it will set "mi_result", thus we are
1608 * always finished here. For NOBREAK we only check that a
1609 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001610 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001611 if (slang->sl_nobreak)
1612 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001613
1614 /* Find following word in case-folded tree. */
1615 mip->mi_compoff = endlen[endidxcnt];
1616#ifdef FEAT_MBYTE
1617 if (has_mbyte && mode == FIND_KEEPWORD)
1618 {
1619 /* Compute byte length in case-folded word from "wlen":
1620 * byte length in keep-case word. Length may change when
1621 * folding case. This can be slow, take a shortcut when
1622 * the case-folded word is equal to the keep-case word. */
1623 p = mip->mi_fword;
1624 if (STRNCMP(ptr, p, wlen) != 0)
1625 {
1626 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1627 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001628 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001629 }
1630 }
1631#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001632 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001633 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001634 if (flags & WF_COMPROOT)
1635 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001636
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001637 /* For NOBREAK we need to try all NOBREAK languages, at least
1638 * to find the ".add" file(s). */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001639 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001640 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001641 if (slang->sl_nobreak)
1642 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001643 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001644 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1645 || !mip->mi_lp->lp_slang->sl_nobreak)
1646 continue;
1647 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001648
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001649 find_word(mip, FIND_COMPOUND);
1650
1651 /* When NOBREAK any word that matches is OK. Otherwise we
1652 * need to find the longest match, thus try with keep-case
1653 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001654 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1655 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001656 /* Find following word in keep-case tree. */
1657 mip->mi_compoff = wlen;
1658 find_word(mip, FIND_KEEPCOMPOUND);
1659
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001660#if 0 /* Disabled, a prefix must not appear halfway a compound word,
1661 unless the COMPOUNDPERMITFLAG is used and then it can't be a
1662 postponed prefix. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001663 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1664 {
1665 /* Check for following word with prefix. */
1666 mip->mi_compoff = c;
1667 find_prefix(mip, FIND_COMPOUND);
1668 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001669#endif
Bram Moolenaar78622822005-08-23 21:00:13 +00001670 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001671
1672 if (!slang->sl_nobreak)
1673 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001674 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001675 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001676 if (flags & WF_COMPROOT)
1677 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001678 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001679
Bram Moolenaar78622822005-08-23 21:00:13 +00001680 if (slang->sl_nobreak)
1681 {
1682 nobreak_result = mip->mi_result;
1683 mip->mi_result = save_result;
1684 mip->mi_end = save_end;
1685 }
1686 else
1687 {
1688 if (mip->mi_result == SP_OK)
1689 break;
1690 continue;
1691 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001692 }
1693
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001694 if (flags & WF_BANNED)
1695 res = SP_BANNED;
1696 else if (flags & WF_REGION)
1697 {
1698 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001699 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001700 res = SP_OK;
1701 else
1702 res = SP_LOCAL;
1703 }
1704 else if (flags & WF_RARE)
1705 res = SP_RARE;
1706 else
1707 res = SP_OK;
1708
Bram Moolenaar78622822005-08-23 21:00:13 +00001709 /* Always use the longest match and the best result. For NOBREAK
1710 * we separately keep the longest match without a following good
1711 * word as a fall-back. */
1712 if (nobreak_result == SP_BAD)
1713 {
1714 if (mip->mi_result2 > res)
1715 {
1716 mip->mi_result2 = res;
1717 mip->mi_end2 = mip->mi_word + wlen;
1718 }
1719 else if (mip->mi_result2 == res
1720 && mip->mi_end2 < mip->mi_word + wlen)
1721 mip->mi_end2 = mip->mi_word + wlen;
1722 }
1723 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001724 {
1725 mip->mi_result = res;
1726 mip->mi_end = mip->mi_word + wlen;
1727 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001728 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001729 mip->mi_end = mip->mi_word + wlen;
1730
Bram Moolenaar78622822005-08-23 21:00:13 +00001731 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001732 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001733 }
1734
Bram Moolenaar78622822005-08-23 21:00:13 +00001735 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001736 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001737 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001738}
1739
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001740/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001741 * Return TRUE if there is a match between the word ptr[wlen] and
1742 * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another
1743 * word.
1744 * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the
1745 * end of ptr[wlen] and the second part matches after it.
1746 */
1747 static int
1748match_checkcompoundpattern(ptr, wlen, gap)
1749 char_u *ptr;
1750 int wlen;
1751 garray_T *gap; /* &sl_comppat */
1752{
1753 int i;
1754 char_u *p;
1755 int len;
1756
1757 for (i = 0; i + 1 < gap->ga_len; i += 2)
1758 {
1759 p = ((char_u **)gap->ga_data)[i + 1];
1760 if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0)
1761 {
1762 /* Second part matches at start of following compound word, now
1763 * check if first part matches at end of previous word. */
1764 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar19c9c762008-12-09 21:34:39 +00001765 len = (int)STRLEN(p);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001766 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0)
1767 return TRUE;
1768 }
1769 }
1770 return FALSE;
1771}
1772
1773/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001774 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1775 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001776 */
1777 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001778can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001779 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001780 char_u *word;
1781 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001782{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001783 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001784#ifdef FEAT_MBYTE
1785 char_u uflags[MAXWLEN * 2];
1786 int i;
1787#endif
1788 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001789
1790 if (slang->sl_compprog == NULL)
1791 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001792#ifdef FEAT_MBYTE
1793 if (enc_utf8)
1794 {
1795 /* Need to convert the single byte flags to utf8 characters. */
1796 p = uflags;
1797 for (i = 0; flags[i] != NUL; ++i)
1798 p += mb_char2bytes(flags[i], p);
1799 *p = NUL;
1800 p = uflags;
1801 }
1802 else
1803#endif
1804 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001805 regmatch.regprog = slang->sl_compprog;
1806 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001807 if (!vim_regexec(&regmatch, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001808 return FALSE;
1809
Bram Moolenaare52325c2005-08-22 22:54:29 +00001810 /* Count the number of syllables. This may be slow, do it last. If there
1811 * are too many syllables AND the number of compound words is above
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001812 * COMPOUNDWORDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001813 if (slang->sl_compsylmax < MAXWLEN
1814 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001815 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001816 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001817}
1818
1819/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001820 * Return TRUE when the sequence of flags in "compflags" plus "flag" can
1821 * possibly form a valid compounded word. This also checks the COMPOUNDRULE
1822 * lines if they don't contain wildcards.
1823 */
1824 static int
1825can_be_compound(sp, slang, compflags, flag)
1826 trystate_T *sp;
1827 slang_T *slang;
1828 char_u *compflags;
1829 int flag;
1830{
1831 /* If the flag doesn't appear in sl_compstartflags or sl_compallflags
1832 * then it can't possibly compound. */
1833 if (!byte_in_str(sp->ts_complen == sp->ts_compsplit
1834 ? slang->sl_compstartflags : slang->sl_compallflags, flag))
1835 return FALSE;
1836
1837 /* If there are no wildcards, we can check if the flags collected so far
1838 * possibly can form a match with COMPOUNDRULE patterns. This only
1839 * makes sense when we have two or more words. */
1840 if (slang->sl_comprules != NULL && sp->ts_complen > sp->ts_compsplit)
1841 {
1842 int v;
1843
1844 compflags[sp->ts_complen] = flag;
1845 compflags[sp->ts_complen + 1] = NUL;
1846 v = match_compoundrule(slang, compflags + sp->ts_compsplit);
1847 compflags[sp->ts_complen] = NUL;
1848 return v;
1849 }
1850
1851 return TRUE;
1852}
1853
1854
1855/*
1856 * Return TRUE if the compound flags in compflags[] match the start of any
1857 * compound rule. This is used to stop trying a compound if the flags
1858 * collected so far can't possibly match any compound rule.
1859 * Caller must check that slang->sl_comprules is not NULL.
1860 */
1861 static int
1862match_compoundrule(slang, compflags)
1863 slang_T *slang;
1864 char_u *compflags;
1865{
1866 char_u *p;
1867 int i;
1868 int c;
1869
1870 /* loop over all the COMPOUNDRULE entries */
1871 for (p = slang->sl_comprules; *p != NUL; ++p)
1872 {
1873 /* loop over the flags in the compound word we have made, match
1874 * them against the current rule entry */
1875 for (i = 0; ; ++i)
1876 {
1877 c = compflags[i];
1878 if (c == NUL)
1879 /* found a rule that matches for the flags we have so far */
1880 return TRUE;
1881 if (*p == '/' || *p == NUL)
1882 break; /* end of rule, it's too short */
1883 if (*p == '[')
1884 {
1885 int match = FALSE;
1886
1887 /* compare against all the flags in [] */
1888 ++p;
1889 while (*p != ']' && *p != NUL)
1890 if (*p++ == c)
1891 match = TRUE;
1892 if (!match)
1893 break; /* none matches */
1894 }
1895 else if (*p != c)
1896 break; /* flag of word doesn't match flag in pattern */
1897 ++p;
1898 }
1899
1900 /* Skip to the next "/", where the next pattern starts. */
1901 p = vim_strchr(p, '/');
1902 if (p == NULL)
1903 break;
1904 }
1905
1906 /* Checked all the rules and none of them match the flags, so there
1907 * can't possibly be a compound starting with these flags. */
1908 return FALSE;
1909}
1910
1911/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001912 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1913 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001914 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001915 */
1916 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001917valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001918 int totprefcnt; /* nr of prefix IDs */
1919 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001920 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001921 char_u *word;
1922 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001923 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001924{
1925 int prefcnt;
1926 int pidx;
1927 regprog_T *rp;
1928 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001929 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001930
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001931 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001932 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1933 {
1934 pidx = slang->sl_pidxs[arridx + prefcnt];
1935
1936 /* Check the prefix ID. */
1937 if (prefid != (pidx & 0xff))
1938 continue;
1939
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001940 /* Check if the prefix doesn't combine and the word already has a
1941 * suffix. */
1942 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1943 continue;
1944
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001945 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001946 * stored in the two bytes above the prefix ID byte. */
1947 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001948 if (rp != NULL)
1949 {
1950 regmatch.regprog = rp;
1951 regmatch.rm_ic = FALSE;
1952 if (!vim_regexec(&regmatch, word, 0))
1953 continue;
1954 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001955 else if (cond_req)
1956 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001957
Bram Moolenaar53805d12005-08-01 07:08:33 +00001958 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001959 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001960 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001961 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001962}
1963
1964/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001965 * Check if the word at "mip->mi_word" has a matching prefix.
1966 * If it does, then check the following word.
1967 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001968 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1969 * prefix in a compound word.
1970 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001971 * For a match mip->mi_result is updated.
1972 */
1973 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001974find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001975 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001976 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001977{
1978 idx_T arridx = 0;
1979 int len;
1980 int wlen = 0;
1981 int flen;
1982 int c;
1983 char_u *ptr;
1984 idx_T lo, hi, m;
1985 slang_T *slang = mip->mi_lp->lp_slang;
1986 char_u *byts;
1987 idx_T *idxs;
1988
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001989 byts = slang->sl_pbyts;
1990 if (byts == NULL)
1991 return; /* array is empty */
1992
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001993 /* We use the case-folded word here, since prefixes are always
1994 * case-folded. */
1995 ptr = mip->mi_fword;
1996 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001997 if (mode == FIND_COMPOUND)
1998 {
1999 /* Skip over the previously found word(s). */
2000 ptr += mip->mi_compoff;
2001 flen -= mip->mi_compoff;
2002 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002003 idxs = slang->sl_pidxs;
2004
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002005 /*
2006 * Repeat advancing in the tree until:
2007 * - there is a byte that doesn't match,
2008 * - we reach the end of the tree,
2009 * - or we reach the end of the line.
2010 */
2011 for (;;)
2012 {
2013 if (flen == 0 && *mip->mi_fend != NUL)
2014 flen = fold_more(mip);
2015
2016 len = byts[arridx++];
2017
2018 /* If the first possible byte is a zero the prefix could end here.
2019 * Check if the following word matches and supports the prefix. */
2020 if (byts[arridx] == 0)
2021 {
2022 /* There can be several prefixes with different conditions. We
2023 * try them all, since we don't know which one will give the
2024 * longest match. The word is the same each time, pass the list
2025 * of possible prefixes to find_word(). */
2026 mip->mi_prefarridx = arridx;
2027 mip->mi_prefcnt = len;
2028 while (len > 0 && byts[arridx] == 0)
2029 {
2030 ++arridx;
2031 --len;
2032 }
2033 mip->mi_prefcnt -= len;
2034
2035 /* Find the word that comes after the prefix. */
2036 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002037 if (mode == FIND_COMPOUND)
2038 /* Skip over the previously found word(s). */
2039 mip->mi_prefixlen += mip->mi_compoff;
2040
Bram Moolenaar53805d12005-08-01 07:08:33 +00002041#ifdef FEAT_MBYTE
2042 if (has_mbyte)
2043 {
2044 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00002045 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
2046 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00002047 }
2048 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00002049 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00002050#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002051 find_word(mip, FIND_PREFIX);
2052
2053
2054 if (len == 0)
2055 break; /* no children, word must end here */
2056 }
2057
2058 /* Stop looking at end of the line. */
2059 if (ptr[wlen] == NUL)
2060 break;
2061
2062 /* Perform a binary search in the list of accepted bytes. */
2063 c = ptr[wlen];
2064 lo = arridx;
2065 hi = arridx + len - 1;
2066 while (lo < hi)
2067 {
2068 m = (lo + hi) / 2;
2069 if (byts[m] > c)
2070 hi = m - 1;
2071 else if (byts[m] < c)
2072 lo = m + 1;
2073 else
2074 {
2075 lo = hi = m;
2076 break;
2077 }
2078 }
2079
2080 /* Stop if there is no matching byte. */
2081 if (hi < lo || byts[lo] != c)
2082 break;
2083
2084 /* Continue at the child (if there is one). */
2085 arridx = idxs[lo];
2086 ++wlen;
2087 --flen;
2088 }
2089}
2090
2091/*
2092 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002093 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002094 * Return the length of the folded chars in bytes.
2095 */
2096 static int
2097fold_more(mip)
2098 matchinf_T *mip;
2099{
2100 int flen;
2101 char_u *p;
2102
2103 p = mip->mi_fend;
2104 do
2105 {
2106 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002107 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002108
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002109 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002110 if (*mip->mi_fend != NUL)
2111 mb_ptr_adv(mip->mi_fend);
2112
2113 (void)spell_casefold(p, (int)(mip->mi_fend - p),
2114 mip->mi_fword + mip->mi_fwordlen,
2115 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002116 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002117 mip->mi_fwordlen += flen;
2118 return flen;
2119}
2120
2121/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002122 * Check case flags for a word. Return TRUE if the word has the requested
2123 * case.
2124 */
2125 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002126spell_valid_case(wordflags, treeflags)
2127 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002128 int treeflags; /* flags for the word in the spell tree */
2129{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002130 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002131 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002132 && ((treeflags & WF_ONECAP) == 0
2133 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002134}
2135
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002136/*
2137 * Return TRUE if spell checking is not enabled.
2138 */
2139 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00002140no_spell_checking(wp)
2141 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002142{
Bram Moolenaar860cae12010-06-05 23:22:07 +02002143 if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL
2144 || wp->w_s->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002145 {
2146 EMSG(_("E756: Spell checking is not enabled"));
2147 return TRUE;
2148 }
2149 return FALSE;
2150}
Bram Moolenaar51485f02005-06-04 21:55:20 +00002151
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002152/*
2153 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002154 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
2155 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002156 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
2157 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00002158 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002159 */
2160 int
Bram Moolenaar95529562005-08-25 21:21:38 +00002161spell_move_to(wp, dir, allwords, curline, attrp)
2162 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002163 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002164 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002165 int curline;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002166 hlf_T *attrp; /* return: attributes of bad word or NULL
2167 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002168{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002169 linenr_T lnum;
2170 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002171 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002172 char_u *line;
2173 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002174 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002175 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002176 int len;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002177# ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002178 int has_syntax = syntax_present(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002179# endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00002180 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002181 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002182 char_u *buf = NULL;
2183 int buflen = 0;
2184 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002185 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002186 int found_one = FALSE;
2187 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002188
Bram Moolenaar95529562005-08-25 21:21:38 +00002189 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00002190 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002191
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002192 /*
2193 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00002194 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002195 *
2196 * When searching backwards, we continue in the line to find the last
2197 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00002198 *
2199 * We concatenate the start of the next line, so that wrapped words work
2200 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
2201 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002202 */
Bram Moolenaar95529562005-08-25 21:21:38 +00002203 lnum = wp->w_cursor.lnum;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002204 clearpos(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002205
2206 while (!got_int)
2207 {
Bram Moolenaar95529562005-08-25 21:21:38 +00002208 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002209
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002210 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002211 if (buflen < len + MAXWLEN + 2)
2212 {
2213 vim_free(buf);
2214 buflen = len + MAXWLEN + 2;
2215 buf = alloc(buflen);
2216 if (buf == NULL)
2217 break;
2218 }
2219
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002220 /* In first line check first word for Capital. */
2221 if (lnum == 1)
2222 capcol = 0;
2223
2224 /* For checking first word with a capital skip white space. */
2225 if (capcol == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002226 capcol = (int)(skipwhite(line) - line);
2227 else if (curline && wp == curwin)
2228 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002229 /* For spellbadword(): check if first word needs a capital. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002230 col = (int)(skipwhite(line) - line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002231 if (check_need_cap(lnum, col))
2232 capcol = col;
2233
2234 /* Need to get the line again, may have looked at the previous
2235 * one. */
2236 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2237 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002238
Bram Moolenaar0c405862005-06-22 22:26:26 +00002239 /* Copy the line into "buf" and append the start of the next line if
2240 * possible. */
2241 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00002242 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00002243 spell_cat_line(buf + STRLEN(buf),
2244 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002245
2246 p = buf + skip;
2247 endp = buf + len;
2248 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002249 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002250 /* When searching backward don't search after the cursor. Unless
2251 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002252 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00002253 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002254 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00002255 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002256 break;
2257
2258 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002259 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002260 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002261
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002262 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002263 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002264 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002265 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002266 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002267 /* When searching forward only accept a bad word after
2268 * the cursor. */
2269 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002270 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00002271 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002272 && (wrapped
2273 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00002274 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002275 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002276 {
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002277# ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00002278 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002279 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002280 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002281 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00002282 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00002283 if (!can_spell)
2284 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002285 }
2286 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002287#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002288 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002289
Bram Moolenaar51485f02005-06-04 21:55:20 +00002290 if (can_spell)
2291 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00002292 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002293 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002294 found_pos.col = (int)(p - buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002295#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00002296 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002297#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002298 if (dir == FORWARD)
2299 {
2300 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00002301 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002302 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002303 if (attrp != NULL)
2304 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002305 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002306 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002307 else if (curline)
2308 /* Insert mode completion: put cursor after
2309 * the bad word. */
2310 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002311 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002312 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002313 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00002314 else
2315 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002316 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002317 }
2318
Bram Moolenaar51485f02005-06-04 21:55:20 +00002319 /* advance to character after the word */
2320 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002321 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002322 }
2323
Bram Moolenaar5195e452005-08-19 20:32:47 +00002324 if (dir == BACKWARD && found_pos.lnum != 0)
2325 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002326 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00002327 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002328 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002329 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002330 }
2331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002332 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002333 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002334
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002335 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002336 if (dir == BACKWARD)
2337 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002338 /* If we are back at the starting line and searched it again there
2339 * is no match, give up. */
2340 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002341 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002342
2343 if (lnum > 1)
2344 --lnum;
2345 else if (!p_ws)
2346 break; /* at first line and 'nowrapscan' */
2347 else
2348 {
2349 /* Wrap around to the end of the buffer. May search the
2350 * starting line again and accept the last match. */
2351 lnum = wp->w_buffer->b_ml.ml_line_count;
2352 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002353 if (!shortmess(SHM_SEARCH))
2354 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002355 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002356 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002357 }
2358 else
2359 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002360 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2361 ++lnum;
2362 else if (!p_ws)
2363 break; /* at first line and 'nowrapscan' */
2364 else
2365 {
2366 /* Wrap around to the start of the buffer. May search the
2367 * starting line again and accept the first match. */
2368 lnum = 1;
2369 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002370 if (!shortmess(SHM_SEARCH))
2371 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002372 }
2373
2374 /* If we are back at the starting line and there is no match then
2375 * give up. */
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00002376 if (lnum == wp->w_cursor.lnum && (!found_one || wrapped))
Bram Moolenaar0c405862005-06-22 22:26:26 +00002377 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002378
2379 /* Skip the characters at the start of the next line that were
2380 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002381 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002382 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002383 else
2384 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002385
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002386 /* Capcol skips over the inserted space. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002387 --capcol;
2388
2389 /* But after empty line check first word in next line */
2390 if (*skipwhite(line) == NUL)
2391 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002392 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002393
2394 line_breakcheck();
2395 }
2396
Bram Moolenaar0c405862005-06-22 22:26:26 +00002397 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002398 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002399}
2400
2401/*
2402 * For spell checking: concatenate the start of the following line "line" into
2403 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00002404 * Keep the blanks at the start of the next line, this is used in win_line()
2405 * to skip those bytes if the word was OK.
Bram Moolenaar0c405862005-06-22 22:26:26 +00002406 */
2407 void
2408spell_cat_line(buf, line, maxlen)
2409 char_u *buf;
2410 char_u *line;
2411 int maxlen;
2412{
2413 char_u *p;
2414 int n;
2415
2416 p = skipwhite(line);
2417 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2418 p = skipwhite(p + 1);
2419
2420 if (*p != NUL)
2421 {
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00002422 /* Only worth concatenating if there is something else than spaces to
2423 * concatenate. */
2424 n = (int)(p - line) + 1;
2425 if (n < maxlen - 1)
2426 {
2427 vim_memset(buf, ' ', n);
2428 vim_strncpy(buf + n, p, maxlen - 1 - n);
2429 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00002430 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002431}
2432
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002433/*
2434 * Structure used for the cookie argument of do_in_runtimepath().
2435 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002436typedef struct spelload_S
2437{
2438 char_u sl_lang[MAXWLEN + 1]; /* language name */
2439 slang_T *sl_slang; /* resulting slang_T struct */
2440 int sl_nobreak; /* NOBREAK language found */
2441} spelload_T;
2442
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002443/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002444 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002445 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002446 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002447 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002448spell_load_lang(lang)
2449 char_u *lang;
2450{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002451 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002452 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002453 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002454#ifdef FEAT_AUTOCMD
2455 int round;
2456#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002457
Bram Moolenaarb765d632005-06-07 21:00:02 +00002458 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002459 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002460 STRCPY(sl.sl_lang, lang);
2461 sl.sl_slang = NULL;
2462 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002463
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002464#ifdef FEAT_AUTOCMD
2465 /* We may retry when no spell file is found for the language, an
2466 * autocommand may load it then. */
2467 for (round = 1; round <= 2; ++round)
2468#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002469 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002470 /*
2471 * Find the first spell file for "lang" in 'runtimepath' and load it.
2472 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002473 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002474 "spell/%s.%s.spl", lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002475 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002476
2477 if (r == FAIL && *sl.sl_lang != NUL)
2478 {
2479 /* Try loading the ASCII version. */
2480 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
2481 "spell/%s.ascii.spl", lang);
2482 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
2483
2484#ifdef FEAT_AUTOCMD
2485 if (r == FAIL && *sl.sl_lang != NUL && round == 1
2486 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
2487 curbuf->b_fname, FALSE, curbuf))
2488 continue;
2489 break;
2490#endif
2491 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002492#ifdef FEAT_AUTOCMD
2493 break;
2494#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002495 }
2496
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002497 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002498 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002499 smsg((char_u *)_("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2500 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002501 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002502 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002503 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002504 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002505 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002506 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002507 }
2508}
2509
2510/*
2511 * Return the encoding used for spell checking: Use 'encoding', except that we
2512 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2513 */
2514 static char_u *
2515spell_enc()
2516{
2517
2518#ifdef FEAT_MBYTE
2519 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2520 return p_enc;
2521#endif
2522 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002523}
2524
2525/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002526 * Get the name of the .spl file for the internal wordlist into
2527 * "fname[MAXPATHL]".
2528 */
2529 static void
2530int_wordlist_spl(fname)
2531 char_u *fname;
2532{
2533 vim_snprintf((char *)fname, MAXPATHL, "%s.%s.spl",
2534 int_wordlist, spell_enc());
2535}
2536
2537/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002538 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002539 * Caller must fill "sl_next".
2540 */
2541 static slang_T *
2542slang_alloc(lang)
2543 char_u *lang;
2544{
2545 slang_T *lp;
2546
Bram Moolenaar51485f02005-06-04 21:55:20 +00002547 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002548 if (lp != NULL)
2549 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002550 if (lang != NULL)
2551 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002552 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002553 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002554 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002555 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002556 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002557 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002558
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002559 return lp;
2560}
2561
2562/*
2563 * Free the contents of an slang_T and the structure itself.
2564 */
2565 static void
2566slang_free(lp)
2567 slang_T *lp;
2568{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002569 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002570 vim_free(lp->sl_fname);
2571 slang_clear(lp);
2572 vim_free(lp);
2573}
2574
2575/*
2576 * Clear an slang_T so that the file can be reloaded.
2577 */
2578 static void
2579slang_clear(lp)
2580 slang_T *lp;
2581{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002582 garray_T *gap;
2583 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002584 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002585 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002586 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002587
Bram Moolenaar51485f02005-06-04 21:55:20 +00002588 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002589 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002590 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002591 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002592 vim_free(lp->sl_pbyts);
2593 lp->sl_pbyts = NULL;
2594
Bram Moolenaar51485f02005-06-04 21:55:20 +00002595 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002596 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002597 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002598 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002599 vim_free(lp->sl_pidxs);
2600 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002601
Bram Moolenaar4770d092006-01-12 23:22:24 +00002602 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002603 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002604 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2605 while (gap->ga_len > 0)
2606 {
2607 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2608 vim_free(ftp->ft_from);
2609 vim_free(ftp->ft_to);
2610 }
2611 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002612 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002613
2614 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002615 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002616 {
2617 /* "ga_len" is set to 1 without adding an item for latin1 */
2618 if (gap->ga_data != NULL)
2619 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2620 for (i = 0; i < gap->ga_len; ++i)
2621 vim_free(((int **)gap->ga_data)[i]);
2622 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002623 else
2624 /* SAL items: free salitem_T items */
2625 while (gap->ga_len > 0)
2626 {
2627 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2628 vim_free(smp->sm_lead);
2629 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2630 vim_free(smp->sm_to);
2631#ifdef FEAT_MBYTE
2632 vim_free(smp->sm_lead_w);
2633 vim_free(smp->sm_oneof_w);
2634 vim_free(smp->sm_to_w);
2635#endif
2636 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002637 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002638
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002639 for (i = 0; i < lp->sl_prefixcnt; ++i)
2640 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002641 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002642 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002643 lp->sl_prefprog = NULL;
2644
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002645 vim_free(lp->sl_info);
2646 lp->sl_info = NULL;
2647
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002648 vim_free(lp->sl_midword);
2649 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002650
Bram Moolenaar5195e452005-08-19 20:32:47 +00002651 vim_free(lp->sl_compprog);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00002652 vim_free(lp->sl_comprules);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002653 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002654 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002655 lp->sl_compprog = NULL;
Bram Moolenaar9f94b052008-11-30 20:12:46 +00002656 lp->sl_comprules = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002657 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002658 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002659
2660 vim_free(lp->sl_syllable);
2661 lp->sl_syllable = NULL;
2662 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002663
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002664 ga_clear_strings(&lp->sl_comppat);
2665
Bram Moolenaar4770d092006-01-12 23:22:24 +00002666 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2667 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002668
Bram Moolenaar4770d092006-01-12 23:22:24 +00002669#ifdef FEAT_MBYTE
2670 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002671#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002672
Bram Moolenaar4770d092006-01-12 23:22:24 +00002673 /* Clear info from .sug file. */
2674 slang_clear_sug(lp);
2675
Bram Moolenaar5195e452005-08-19 20:32:47 +00002676 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002677 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002678 lp->sl_compsylmax = MAXWLEN;
2679 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002680}
2681
2682/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002683 * Clear the info from the .sug file in "lp".
2684 */
2685 static void
2686slang_clear_sug(lp)
2687 slang_T *lp;
2688{
2689 vim_free(lp->sl_sbyts);
2690 lp->sl_sbyts = NULL;
2691 vim_free(lp->sl_sidxs);
2692 lp->sl_sidxs = NULL;
2693 close_spellbuf(lp->sl_sugbuf);
2694 lp->sl_sugbuf = NULL;
2695 lp->sl_sugloaded = FALSE;
2696 lp->sl_sugtime = 0;
2697}
2698
2699/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002700 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002701 * Invoked through do_in_runtimepath().
2702 */
2703 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002704spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002705 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002706 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002707{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002708 spelload_T *slp = (spelload_T *)cookie;
2709 slang_T *slang;
2710
2711 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2712 if (slang != NULL)
2713 {
2714 /* When a previously loaded file has NOBREAK also use it for the
2715 * ".add" files. */
2716 if (slp->sl_nobreak && slang->sl_add)
2717 slang->sl_nobreak = TRUE;
2718 else if (slang->sl_nobreak)
2719 slp->sl_nobreak = TRUE;
2720
2721 slp->sl_slang = slang;
2722 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002723}
2724
2725/*
2726 * Load one spell file and store the info into a slang_T.
2727 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00002728 * This is invoked in three ways:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002729 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2730 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2731 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2732 * points to the existing slang_T.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002733 * - Just after writing a .spl file; it's read back to produce the .sug file.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002734 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
2735 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002736 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002737 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002738 static slang_T *
2739spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002740 char_u *fname;
2741 char_u *lang;
2742 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002743 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002744{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002745 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002746 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002747 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002748 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002749 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002750 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002751 char_u *save_sourcing_name = sourcing_name;
2752 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002753 slang_T *lp = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002754 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002755 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002756
Bram Moolenaarb765d632005-06-07 21:00:02 +00002757 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002758 if (fd == NULL)
2759 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002760 if (!silent)
2761 EMSG2(_(e_notopen), fname);
2762 else if (p_verbose > 2)
2763 {
2764 verbose_enter();
2765 smsg((char_u *)e_notopen, fname);
2766 verbose_leave();
2767 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002768 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002769 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002770 if (p_verbose > 2)
2771 {
2772 verbose_enter();
2773 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2774 verbose_leave();
2775 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002776
Bram Moolenaarb765d632005-06-07 21:00:02 +00002777 if (old_lp == NULL)
2778 {
2779 lp = slang_alloc(lang);
2780 if (lp == NULL)
2781 goto endFAIL;
2782
2783 /* Remember the file name, used to reload the file when it's updated. */
2784 lp->sl_fname = vim_strsave(fname);
2785 if (lp->sl_fname == NULL)
2786 goto endFAIL;
2787
2788 /* Check for .add.spl. */
2789 lp->sl_add = strstr((char *)gettail(fname), ".add.") != NULL;
2790 }
2791 else
2792 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002793
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002794 /* Set sourcing_name, so that error messages mention the file name. */
2795 sourcing_name = fname;
2796 sourcing_lnum = 0;
2797
Bram Moolenaar4770d092006-01-12 23:22:24 +00002798 /*
2799 * <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002800 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002801 for (i = 0; i < VIMSPELLMAGICL; ++i)
2802 buf[i] = getc(fd); /* <fileID> */
2803 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2804 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002805 EMSG(_("E757: This does not look like a spell file"));
2806 goto endFAIL;
2807 }
2808 c = getc(fd); /* <versionnr> */
2809 if (c < VIMSPELLVERSION)
2810 {
2811 EMSG(_("E771: Old spell file, needs to be updated"));
2812 goto endFAIL;
2813 }
2814 else if (c > VIMSPELLVERSION)
2815 {
2816 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002817 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002818 }
2819
Bram Moolenaar5195e452005-08-19 20:32:47 +00002820
2821 /*
2822 * <SECTIONS>: <section> ... <sectionend>
2823 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2824 */
2825 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002826 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002827 n = getc(fd); /* <sectionID> or <sectionend> */
2828 if (n == SN_END)
2829 break;
2830 c = getc(fd); /* <sectionflags> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002831 len = get4c(fd); /* <sectionlen> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002832 if (len < 0)
2833 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002834
Bram Moolenaar5195e452005-08-19 20:32:47 +00002835 res = 0;
2836 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002837 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002838 case SN_INFO:
2839 lp->sl_info = read_string(fd, len); /* <infotext> */
2840 if (lp->sl_info == NULL)
2841 goto endFAIL;
2842 break;
2843
Bram Moolenaar5195e452005-08-19 20:32:47 +00002844 case SN_REGION:
2845 res = read_region_section(fd, lp, len);
2846 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002847
Bram Moolenaar5195e452005-08-19 20:32:47 +00002848 case SN_CHARFLAGS:
2849 res = read_charflags_section(fd);
2850 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002851
Bram Moolenaar5195e452005-08-19 20:32:47 +00002852 case SN_MIDWORD:
2853 lp->sl_midword = read_string(fd, len); /* <midword> */
2854 if (lp->sl_midword == NULL)
2855 goto endFAIL;
2856 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002857
Bram Moolenaar5195e452005-08-19 20:32:47 +00002858 case SN_PREFCOND:
2859 res = read_prefcond_section(fd, lp);
2860 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002861
Bram Moolenaar5195e452005-08-19 20:32:47 +00002862 case SN_REP:
Bram Moolenaar4770d092006-01-12 23:22:24 +00002863 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
2864 break;
2865
2866 case SN_REPSAL:
2867 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002868 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002869
Bram Moolenaar5195e452005-08-19 20:32:47 +00002870 case SN_SAL:
2871 res = read_sal_section(fd, lp);
2872 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002873
Bram Moolenaar5195e452005-08-19 20:32:47 +00002874 case SN_SOFO:
2875 res = read_sofo_section(fd, lp);
2876 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002877
Bram Moolenaar5195e452005-08-19 20:32:47 +00002878 case SN_MAP:
2879 p = read_string(fd, len); /* <mapstr> */
2880 if (p == NULL)
2881 goto endFAIL;
2882 set_map_str(lp, p);
2883 vim_free(p);
2884 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002885
Bram Moolenaar4770d092006-01-12 23:22:24 +00002886 case SN_WORDS:
2887 res = read_words_section(fd, lp, len);
2888 break;
2889
2890 case SN_SUGFILE:
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002891 lp->sl_sugtime = get8ctime(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002892 break;
2893
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002894 case SN_NOSPLITSUGS:
2895 lp->sl_nosplitsugs = TRUE; /* <timestamp> */
2896 break;
2897
Bram Moolenaar5195e452005-08-19 20:32:47 +00002898 case SN_COMPOUND:
2899 res = read_compound(fd, lp, len);
2900 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002901
Bram Moolenaar78622822005-08-23 21:00:13 +00002902 case SN_NOBREAK:
2903 lp->sl_nobreak = TRUE;
2904 break;
2905
Bram Moolenaar5195e452005-08-19 20:32:47 +00002906 case SN_SYLLABLE:
2907 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2908 if (lp->sl_syllable == NULL)
2909 goto endFAIL;
2910 if (init_syl_tab(lp) == FAIL)
2911 goto endFAIL;
2912 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002913
Bram Moolenaar5195e452005-08-19 20:32:47 +00002914 default:
2915 /* Unsupported section. When it's required give an error
2916 * message. When it's not required skip the contents. */
2917 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002918 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002919 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002920 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002921 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002922 while (--len >= 0)
2923 if (getc(fd) < 0)
2924 goto truncerr;
2925 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002926 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002927someerror:
Bram Moolenaar5195e452005-08-19 20:32:47 +00002928 if (res == SP_FORMERROR)
2929 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002930 EMSG(_(e_format));
2931 goto endFAIL;
2932 }
2933 if (res == SP_TRUNCERROR)
2934 {
2935truncerr:
2936 EMSG(_(e_spell_trunc));
2937 goto endFAIL;
2938 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002939 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002940 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002941 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002942
Bram Moolenaar4770d092006-01-12 23:22:24 +00002943 /* <LWORDTREE> */
2944 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
2945 if (res != 0)
2946 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002947
Bram Moolenaar4770d092006-01-12 23:22:24 +00002948 /* <KWORDTREE> */
2949 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
2950 if (res != 0)
2951 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002952
Bram Moolenaar4770d092006-01-12 23:22:24 +00002953 /* <PREFIXTREE> */
2954 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
2955 lp->sl_prefixcnt);
2956 if (res != 0)
2957 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002958
Bram Moolenaarb765d632005-06-07 21:00:02 +00002959 /* For a new file link it in the list of spell files. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002960 if (old_lp == NULL && lang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002961 {
2962 lp->sl_next = first_lang;
2963 first_lang = lp;
2964 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002965
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002966 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002967
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002968endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002969 if (lang != NULL)
2970 /* truncating the name signals the error to spell_load_lang() */
2971 *lang = NUL;
2972 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002973 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002974 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002975
2976endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002977 if (fd != NULL)
2978 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002979 sourcing_name = save_sourcing_name;
2980 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002981
2982 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002983}
2984
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002985/*
2986 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00002987 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002988 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002989 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
2990 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002991 */
2992 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002993read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002994 FILE *fd;
2995 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002996 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002997{
2998 int cnt = 0;
2999 int i;
3000 char_u *str;
3001
3002 /* read the length bytes, MSB first */
3003 for (i = 0; i < cnt_bytes; ++i)
3004 cnt = (cnt << 8) + getc(fd);
3005 if (cnt < 0)
3006 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00003007 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003008 return NULL;
3009 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003010 *cntp = cnt;
3011 if (cnt == 0)
3012 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003013
Bram Moolenaar5195e452005-08-19 20:32:47 +00003014 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003015 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003016 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003017 return str;
3018}
3019
Bram Moolenaar7887d882005-07-01 22:33:52 +00003020/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003021 * Read SN_REGION: <regionname> ...
3022 * Return SP_*ERROR flags.
3023 */
3024 static int
3025read_region_section(fd, lp, len)
3026 FILE *fd;
3027 slang_T *lp;
3028 int len;
3029{
3030 int i;
3031
3032 if (len > 16)
3033 return SP_FORMERROR;
3034 for (i = 0; i < len; ++i)
3035 lp->sl_regions[i] = getc(fd); /* <regionname> */
3036 lp->sl_regions[len] = NUL;
3037 return 0;
3038}
3039
3040/*
3041 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
3042 * <folcharslen> <folchars>
3043 * Return SP_*ERROR flags.
3044 */
3045 static int
3046read_charflags_section(fd)
3047 FILE *fd;
3048{
3049 char_u *flags;
3050 char_u *fol;
3051 int flagslen, follen;
3052
3053 /* <charflagslen> <charflags> */
3054 flags = read_cnt_string(fd, 1, &flagslen);
3055 if (flagslen < 0)
3056 return flagslen;
3057
3058 /* <folcharslen> <folchars> */
3059 fol = read_cnt_string(fd, 2, &follen);
3060 if (follen < 0)
3061 {
3062 vim_free(flags);
3063 return follen;
3064 }
3065
3066 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
3067 if (flags != NULL && fol != NULL)
3068 set_spell_charflags(flags, flagslen, fol);
3069
3070 vim_free(flags);
3071 vim_free(fol);
3072
3073 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
3074 if ((flags == NULL) != (fol == NULL))
3075 return SP_FORMERROR;
3076 return 0;
3077}
3078
3079/*
3080 * Read SN_PREFCOND section.
3081 * Return SP_*ERROR flags.
3082 */
3083 static int
3084read_prefcond_section(fd, lp)
3085 FILE *fd;
3086 slang_T *lp;
3087{
3088 int cnt;
3089 int i;
3090 int n;
3091 char_u *p;
3092 char_u buf[MAXWLEN + 1];
3093
3094 /* <prefcondcnt> <prefcond> ... */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003095 cnt = get2c(fd); /* <prefcondcnt> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003096 if (cnt <= 0)
3097 return SP_FORMERROR;
3098
3099 lp->sl_prefprog = (regprog_T **)alloc_clear(
3100 (unsigned)sizeof(regprog_T *) * cnt);
3101 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003102 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003103 lp->sl_prefixcnt = cnt;
3104
3105 for (i = 0; i < cnt; ++i)
3106 {
3107 /* <prefcond> : <condlen> <condstr> */
3108 n = getc(fd); /* <condlen> */
3109 if (n < 0 || n >= MAXWLEN)
3110 return SP_FORMERROR;
3111
3112 /* When <condlen> is zero we have an empty condition. Otherwise
3113 * compile the regexp program used to check for the condition. */
3114 if (n > 0)
3115 {
3116 buf[0] = '^'; /* always match at one position only */
3117 p = buf + 1;
3118 while (n-- > 0)
3119 *p++ = getc(fd); /* <condstr> */
3120 *p = NUL;
3121 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
3122 }
3123 }
3124 return 0;
3125}
3126
3127/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003128 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
Bram Moolenaar5195e452005-08-19 20:32:47 +00003129 * Return SP_*ERROR flags.
3130 */
3131 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00003132read_rep_section(fd, gap, first)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003133 FILE *fd;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003134 garray_T *gap;
3135 short *first;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003136{
3137 int cnt;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003138 fromto_T *ftp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003139 int i;
3140
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003141 cnt = get2c(fd); /* <repcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003142 if (cnt < 0)
3143 return SP_TRUNCERROR;
3144
Bram Moolenaar5195e452005-08-19 20:32:47 +00003145 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003146 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003147
3148 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3149 for (; gap->ga_len < cnt; ++gap->ga_len)
3150 {
3151 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
3152 ftp->ft_from = read_cnt_string(fd, 1, &i);
3153 if (i < 0)
3154 return i;
3155 if (i == 0)
3156 return SP_FORMERROR;
3157 ftp->ft_to = read_cnt_string(fd, 1, &i);
3158 if (i <= 0)
3159 {
3160 vim_free(ftp->ft_from);
3161 if (i < 0)
3162 return i;
3163 return SP_FORMERROR;
3164 }
3165 }
3166
3167 /* Fill the first-index table. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003168 for (i = 0; i < 256; ++i)
3169 first[i] = -1;
3170 for (i = 0; i < gap->ga_len; ++i)
3171 {
3172 ftp = &((fromto_T *)gap->ga_data)[i];
3173 if (first[*ftp->ft_from] == -1)
3174 first[*ftp->ft_from] = i;
3175 }
3176 return 0;
3177}
3178
3179/*
3180 * Read SN_SAL section: <salflags> <salcount> <sal> ...
3181 * Return SP_*ERROR flags.
3182 */
3183 static int
3184read_sal_section(fd, slang)
3185 FILE *fd;
3186 slang_T *slang;
3187{
3188 int i;
3189 int cnt;
3190 garray_T *gap;
3191 salitem_T *smp;
3192 int ccnt;
3193 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003194 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003195
3196 slang->sl_sofo = FALSE;
3197
3198 i = getc(fd); /* <salflags> */
3199 if (i & SAL_F0LLOWUP)
3200 slang->sl_followup = TRUE;
3201 if (i & SAL_COLLAPSE)
3202 slang->sl_collapse = TRUE;
3203 if (i & SAL_REM_ACCENTS)
3204 slang->sl_rem_accents = TRUE;
3205
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003206 cnt = get2c(fd); /* <salcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003207 if (cnt < 0)
3208 return SP_TRUNCERROR;
3209
3210 gap = &slang->sl_sal;
3211 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003212 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003213 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003214
3215 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3216 for (; gap->ga_len < cnt; ++gap->ga_len)
3217 {
3218 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3219 ccnt = getc(fd); /* <salfromlen> */
3220 if (ccnt < 0)
3221 return SP_TRUNCERROR;
3222 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003223 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003224 smp->sm_lead = p;
3225
3226 /* Read up to the first special char into sm_lead. */
3227 for (i = 0; i < ccnt; ++i)
3228 {
3229 c = getc(fd); /* <salfrom> */
3230 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
3231 break;
3232 *p++ = c;
3233 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003234 smp->sm_leadlen = (int)(p - smp->sm_lead);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003235 *p++ = NUL;
3236
3237 /* Put (abc) chars in sm_oneof, if any. */
3238 if (c == '(')
3239 {
3240 smp->sm_oneof = p;
3241 for (++i; i < ccnt; ++i)
3242 {
3243 c = getc(fd); /* <salfrom> */
3244 if (c == ')')
3245 break;
3246 *p++ = c;
3247 }
3248 *p++ = NUL;
3249 if (++i < ccnt)
3250 c = getc(fd);
3251 }
3252 else
3253 smp->sm_oneof = NULL;
3254
3255 /* Any following chars go in sm_rules. */
3256 smp->sm_rules = p;
3257 if (i < ccnt)
3258 /* store the char we got while checking for end of sm_lead */
3259 *p++ = c;
3260 for (++i; i < ccnt; ++i)
3261 *p++ = getc(fd); /* <salfrom> */
3262 *p++ = NUL;
3263
3264 /* <saltolen> <salto> */
3265 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
3266 if (ccnt < 0)
3267 {
3268 vim_free(smp->sm_lead);
3269 return ccnt;
3270 }
3271
3272#ifdef FEAT_MBYTE
3273 if (has_mbyte)
3274 {
3275 /* convert the multi-byte strings to wide char strings */
3276 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3277 smp->sm_leadlen = mb_charlen(smp->sm_lead);
3278 if (smp->sm_oneof == NULL)
3279 smp->sm_oneof_w = NULL;
3280 else
3281 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
3282 if (smp->sm_to == NULL)
3283 smp->sm_to_w = NULL;
3284 else
3285 smp->sm_to_w = mb_str2wide(smp->sm_to);
3286 if (smp->sm_lead_w == NULL
3287 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
3288 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
3289 {
3290 vim_free(smp->sm_lead);
3291 vim_free(smp->sm_to);
3292 vim_free(smp->sm_lead_w);
3293 vim_free(smp->sm_oneof_w);
3294 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003295 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003296 }
3297 }
3298#endif
3299 }
3300
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003301 if (gap->ga_len > 0)
3302 {
3303 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
3304 * that we need to check the index every time. */
3305 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3306 if ((p = alloc(1)) == NULL)
3307 return SP_OTHERERROR;
3308 p[0] = NUL;
3309 smp->sm_lead = p;
3310 smp->sm_leadlen = 0;
3311 smp->sm_oneof = NULL;
3312 smp->sm_rules = p;
3313 smp->sm_to = NULL;
3314#ifdef FEAT_MBYTE
3315 if (has_mbyte)
3316 {
3317 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3318 smp->sm_leadlen = 0;
3319 smp->sm_oneof_w = NULL;
3320 smp->sm_to_w = NULL;
3321 }
3322#endif
3323 ++gap->ga_len;
3324 }
3325
Bram Moolenaar5195e452005-08-19 20:32:47 +00003326 /* Fill the first-index table. */
3327 set_sal_first(slang);
3328
3329 return 0;
3330}
3331
3332/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003333 * Read SN_WORDS: <word> ...
3334 * Return SP_*ERROR flags.
3335 */
3336 static int
3337read_words_section(fd, lp, len)
3338 FILE *fd;
3339 slang_T *lp;
3340 int len;
3341{
3342 int done = 0;
3343 int i;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003344 int c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003345 char_u word[MAXWLEN];
3346
3347 while (done < len)
3348 {
3349 /* Read one word at a time. */
3350 for (i = 0; ; ++i)
3351 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00003352 c = getc(fd);
3353 if (c == EOF)
3354 return SP_TRUNCERROR;
3355 word[i] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003356 if (word[i] == NUL)
3357 break;
3358 if (i == MAXWLEN - 1)
3359 return SP_FORMERROR;
3360 }
3361
3362 /* Init the count to 10. */
3363 count_common_word(lp, word, -1, 10);
3364 done += i + 1;
3365 }
3366 return 0;
3367}
3368
3369/*
3370 * Add a word to the hashtable of common words.
3371 * If it's already there then the counter is increased.
3372 */
3373 static void
3374count_common_word(lp, word, len, count)
3375 slang_T *lp;
3376 char_u *word;
3377 int len; /* word length, -1 for upto NUL */
3378 int count; /* 1 to count once, 10 to init */
3379{
3380 hash_T hash;
3381 hashitem_T *hi;
3382 wordcount_T *wc;
3383 char_u buf[MAXWLEN];
3384 char_u *p;
3385
3386 if (len == -1)
3387 p = word;
3388 else
3389 {
3390 vim_strncpy(buf, word, len);
3391 p = buf;
3392 }
3393
3394 hash = hash_hash(p);
3395 hi = hash_lookup(&lp->sl_wordcount, p, hash);
3396 if (HASHITEM_EMPTY(hi))
3397 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003398 wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003399 if (wc == NULL)
3400 return;
3401 STRCPY(wc->wc_word, p);
3402 wc->wc_count = count;
3403 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
3404 }
3405 else
3406 {
3407 wc = HI2WC(hi);
3408 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
3409 wc->wc_count = MAXWORDCOUNT;
3410 }
3411}
3412
3413/*
3414 * Adjust the score of common words.
3415 */
3416 static int
3417score_wordcount_adj(slang, score, word, split)
3418 slang_T *slang;
3419 int score;
3420 char_u *word;
3421 int split; /* word was split, less bonus */
3422{
3423 hashitem_T *hi;
3424 wordcount_T *wc;
3425 int bonus;
3426 int newscore;
3427
3428 hi = hash_find(&slang->sl_wordcount, word);
3429 if (!HASHITEM_EMPTY(hi))
3430 {
3431 wc = HI2WC(hi);
3432 if (wc->wc_count < SCORE_THRES2)
3433 bonus = SCORE_COMMON1;
3434 else if (wc->wc_count < SCORE_THRES3)
3435 bonus = SCORE_COMMON2;
3436 else
3437 bonus = SCORE_COMMON3;
3438 if (split)
3439 newscore = score - bonus / 2;
3440 else
3441 newscore = score - bonus;
3442 if (newscore < 0)
3443 return 0;
3444 return newscore;
3445 }
3446 return score;
3447}
3448
3449/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003450 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
3451 * Return SP_*ERROR flags.
3452 */
3453 static int
3454read_sofo_section(fd, slang)
3455 FILE *fd;
3456 slang_T *slang;
3457{
3458 int cnt;
3459 char_u *from, *to;
3460 int res;
3461
3462 slang->sl_sofo = TRUE;
3463
3464 /* <sofofromlen> <sofofrom> */
3465 from = read_cnt_string(fd, 2, &cnt);
3466 if (cnt < 0)
3467 return cnt;
3468
3469 /* <sofotolen> <sofoto> */
3470 to = read_cnt_string(fd, 2, &cnt);
3471 if (cnt < 0)
3472 {
3473 vim_free(from);
3474 return cnt;
3475 }
3476
3477 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
3478 if (from != NULL && to != NULL)
3479 res = set_sofo(slang, from, to);
3480 else if (from != NULL || to != NULL)
3481 res = SP_FORMERROR; /* only one of two strings is an error */
3482 else
3483 res = 0;
3484
3485 vim_free(from);
3486 vim_free(to);
3487 return res;
3488}
3489
3490/*
3491 * Read the compound section from the .spl file:
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003492 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +00003493 * Returns SP_*ERROR flags.
3494 */
3495 static int
3496read_compound(fd, slang, len)
3497 FILE *fd;
3498 slang_T *slang;
3499 int len;
3500{
3501 int todo = len;
3502 int c;
3503 int atstart;
3504 char_u *pat;
3505 char_u *pp;
3506 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003507 char_u *ap;
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003508 char_u *crp;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003509 int cnt;
3510 garray_T *gap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003511
3512 if (todo < 2)
3513 return SP_FORMERROR; /* need at least two bytes */
3514
3515 --todo;
3516 c = getc(fd); /* <compmax> */
3517 if (c < 2)
3518 c = MAXWLEN;
3519 slang->sl_compmax = c;
3520
3521 --todo;
3522 c = getc(fd); /* <compminlen> */
3523 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003524 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003525 slang->sl_compminlen = c;
3526
3527 --todo;
3528 c = getc(fd); /* <compsylmax> */
3529 if (c < 1)
3530 c = MAXWLEN;
3531 slang->sl_compsylmax = c;
3532
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003533 c = getc(fd); /* <compoptions> */
3534 if (c != 0)
3535 ungetc(c, fd); /* be backwards compatible with Vim 7.0b */
3536 else
3537 {
3538 --todo;
3539 c = getc(fd); /* only use the lower byte for now */
3540 --todo;
3541 slang->sl_compoptions = c;
3542
3543 gap = &slang->sl_comppat;
3544 c = get2c(fd); /* <comppatcount> */
3545 todo -= 2;
3546 ga_init2(gap, sizeof(char_u *), c);
3547 if (ga_grow(gap, c) == OK)
3548 while (--c >= 0)
3549 {
3550 ((char_u **)(gap->ga_data))[gap->ga_len++] =
3551 read_cnt_string(fd, 1, &cnt);
3552 /* <comppatlen> <comppattext> */
3553 if (cnt < 0)
3554 return cnt;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003555 todo -= cnt + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003556 }
3557 }
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003558 if (todo < 0)
3559 return SP_FORMERROR;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003560
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003561 /* Turn the COMPOUNDRULE items into a regexp pattern:
Bram Moolenaar5195e452005-08-19 20:32:47 +00003562 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003563 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3564 * Conversion to utf-8 may double the size. */
3565 c = todo * 2 + 7;
3566#ifdef FEAT_MBYTE
3567 if (enc_utf8)
3568 c += todo * 2;
3569#endif
3570 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003571 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003572 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003573
Bram Moolenaard12a1322005-08-21 22:08:24 +00003574 /* We also need a list of all flags that can appear at the start and one
3575 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003576 cp = alloc(todo + 1);
3577 if (cp == NULL)
3578 {
3579 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003580 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003581 }
3582 slang->sl_compstartflags = cp;
3583 *cp = NUL;
3584
Bram Moolenaard12a1322005-08-21 22:08:24 +00003585 ap = alloc(todo + 1);
3586 if (ap == NULL)
3587 {
3588 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003589 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003590 }
3591 slang->sl_compallflags = ap;
3592 *ap = NUL;
3593
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003594 /* And a list of all patterns in their original form, for checking whether
3595 * compounding may work in match_compoundrule(). This is freed when we
3596 * encounter a wildcard, the check doesn't work then. */
3597 crp = alloc(todo + 1);
3598 slang->sl_comprules = crp;
3599
Bram Moolenaar5195e452005-08-19 20:32:47 +00003600 pp = pat;
3601 *pp++ = '^';
3602 *pp++ = '\\';
3603 *pp++ = '(';
3604
3605 atstart = 1;
3606 while (todo-- > 0)
3607 {
3608 c = getc(fd); /* <compflags> */
Bram Moolenaara7241f52008-06-24 20:39:31 +00003609 if (c == EOF)
3610 {
3611 vim_free(pat);
3612 return SP_TRUNCERROR;
3613 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00003614
3615 /* Add all flags to "sl_compallflags". */
3616 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003617 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003618 {
3619 *ap++ = c;
3620 *ap = NUL;
3621 }
3622
Bram Moolenaar5195e452005-08-19 20:32:47 +00003623 if (atstart != 0)
3624 {
3625 /* At start of item: copy flags to "sl_compstartflags". For a
3626 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3627 if (c == '[')
3628 atstart = 2;
3629 else if (c == ']')
3630 atstart = 0;
3631 else
3632 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003633 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003634 {
3635 *cp++ = c;
3636 *cp = NUL;
3637 }
3638 if (atstart == 1)
3639 atstart = 0;
3640 }
3641 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003642
3643 /* Copy flag to "sl_comprules", unless we run into a wildcard. */
3644 if (crp != NULL)
3645 {
3646 if (c == '+' || c == '*')
3647 {
3648 vim_free(slang->sl_comprules);
3649 slang->sl_comprules = NULL;
3650 crp = NULL;
3651 }
3652 else
3653 *crp++ = c;
3654 }
3655
Bram Moolenaar5195e452005-08-19 20:32:47 +00003656 if (c == '/') /* slash separates two items */
3657 {
3658 *pp++ = '\\';
3659 *pp++ = '|';
3660 atstart = 1;
3661 }
3662 else /* normal char, "[abc]" and '*' are copied as-is */
3663 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003664 if (c == '+' || c == '~')
Bram Moolenaar5195e452005-08-19 20:32:47 +00003665 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003666#ifdef FEAT_MBYTE
3667 if (enc_utf8)
3668 pp += mb_char2bytes(c, pp);
3669 else
3670#endif
3671 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003672 }
3673 }
3674
3675 *pp++ = '\\';
3676 *pp++ = ')';
3677 *pp++ = '$';
3678 *pp = NUL;
3679
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003680 if (crp != NULL)
3681 *crp = NUL;
3682
Bram Moolenaar5195e452005-08-19 20:32:47 +00003683 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3684 vim_free(pat);
3685 if (slang->sl_compprog == NULL)
3686 return SP_FORMERROR;
3687
3688 return 0;
3689}
3690
Bram Moolenaar6de68532005-08-24 22:08:48 +00003691/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003692 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003693 * Like strchr() but independent of locale.
3694 */
3695 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003696byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003697 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003698 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003699{
3700 char_u *p;
3701
3702 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003703 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003704 return TRUE;
3705 return FALSE;
3706}
3707
Bram Moolenaar5195e452005-08-19 20:32:47 +00003708#define SY_MAXLEN 30
3709typedef struct syl_item_S
3710{
3711 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3712 int sy_len;
3713} syl_item_T;
3714
3715/*
3716 * Truncate "slang->sl_syllable" at the first slash and put the following items
3717 * in "slang->sl_syl_items".
3718 */
3719 static int
3720init_syl_tab(slang)
3721 slang_T *slang;
3722{
3723 char_u *p;
3724 char_u *s;
3725 int l;
3726 syl_item_T *syl;
3727
3728 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3729 p = vim_strchr(slang->sl_syllable, '/');
3730 while (p != NULL)
3731 {
3732 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003733 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003734 break;
3735 s = p;
3736 p = vim_strchr(p, '/');
3737 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003738 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003739 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003740 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003741 if (l >= SY_MAXLEN)
3742 return SP_FORMERROR;
3743 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003744 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003745 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3746 + slang->sl_syl_items.ga_len++;
3747 vim_strncpy(syl->sy_chars, s, l);
3748 syl->sy_len = l;
3749 }
3750 return OK;
3751}
3752
3753/*
3754 * Count the number of syllables in "word".
3755 * When "word" contains spaces the syllables after the last space are counted.
3756 * Returns zero if syllables are not defines.
3757 */
3758 static int
3759count_syllables(slang, word)
3760 slang_T *slang;
3761 char_u *word;
3762{
3763 int cnt = 0;
3764 int skip = FALSE;
3765 char_u *p;
3766 int len;
3767 int i;
3768 syl_item_T *syl;
3769 int c;
3770
3771 if (slang->sl_syllable == NULL)
3772 return 0;
3773
3774 for (p = word; *p != NUL; p += len)
3775 {
3776 /* When running into a space reset counter. */
3777 if (*p == ' ')
3778 {
3779 len = 1;
3780 cnt = 0;
3781 continue;
3782 }
3783
3784 /* Find longest match of syllable items. */
3785 len = 0;
3786 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3787 {
3788 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3789 if (syl->sy_len > len
3790 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3791 len = syl->sy_len;
3792 }
3793 if (len != 0) /* found a match, count syllable */
3794 {
3795 ++cnt;
3796 skip = FALSE;
3797 }
3798 else
3799 {
3800 /* No recognized syllable item, at least a syllable char then? */
3801#ifdef FEAT_MBYTE
3802 c = mb_ptr2char(p);
3803 len = (*mb_ptr2len)(p);
3804#else
3805 c = *p;
3806 len = 1;
3807#endif
3808 if (vim_strchr(slang->sl_syllable, c) == NULL)
3809 skip = FALSE; /* No, search for next syllable */
3810 else if (!skip)
3811 {
3812 ++cnt; /* Yes, count it */
3813 skip = TRUE; /* don't count following syllable chars */
3814 }
3815 }
3816 }
3817 return cnt;
3818}
3819
3820/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003821 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003822 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003823 */
3824 static int
3825set_sofo(lp, from, to)
3826 slang_T *lp;
3827 char_u *from;
3828 char_u *to;
3829{
3830 int i;
3831
3832#ifdef FEAT_MBYTE
3833 garray_T *gap;
3834 char_u *s;
3835 char_u *p;
3836 int c;
3837 int *inp;
3838
3839 if (has_mbyte)
3840 {
3841 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3842 * characters. The index is the low byte of the character.
3843 * The list contains from-to pairs with a terminating NUL.
3844 * sl_sal_first[] is used for latin1 "from" characters. */
3845 gap = &lp->sl_sal;
3846 ga_init2(gap, sizeof(int *), 1);
3847 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003848 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003849 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3850 gap->ga_len = 256;
3851
3852 /* First count the number of items for each list. Temporarily use
3853 * sl_sal_first[] for this. */
3854 for (p = from, s = to; *p != NUL && *s != NUL; )
3855 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003856 c = mb_cptr2char_adv(&p);
3857 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003858 if (c >= 256)
3859 ++lp->sl_sal_first[c & 0xff];
3860 }
3861 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003862 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003863
3864 /* Allocate the lists. */
3865 for (i = 0; i < 256; ++i)
3866 if (lp->sl_sal_first[i] > 0)
3867 {
3868 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3869 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003870 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003871 ((int **)gap->ga_data)[i] = (int *)p;
3872 *(int *)p = 0;
3873 }
3874
3875 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3876 * list. */
3877 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3878 for (p = from, s = to; *p != NUL && *s != NUL; )
3879 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003880 c = mb_cptr2char_adv(&p);
3881 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003882 if (c >= 256)
3883 {
3884 /* Append the from-to chars at the end of the list with
3885 * the low byte. */
3886 inp = ((int **)gap->ga_data)[c & 0xff];
3887 while (*inp != 0)
3888 ++inp;
3889 *inp++ = c; /* from char */
3890 *inp++ = i; /* to char */
3891 *inp++ = NUL; /* NUL at the end */
3892 }
3893 else
3894 /* mapping byte to char is done in sl_sal_first[] */
3895 lp->sl_sal_first[c] = i;
3896 }
3897 }
3898 else
3899#endif
3900 {
3901 /* mapping bytes to bytes is done in sl_sal_first[] */
3902 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003903 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003904
3905 for (i = 0; to[i] != NUL; ++i)
3906 lp->sl_sal_first[from[i]] = to[i];
3907 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3908 }
3909
Bram Moolenaar5195e452005-08-19 20:32:47 +00003910 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003911}
3912
3913/*
3914 * Fill the first-index table for "lp".
3915 */
3916 static void
3917set_sal_first(lp)
3918 slang_T *lp;
3919{
3920 salfirst_T *sfirst;
3921 int i;
3922 salitem_T *smp;
3923 int c;
3924 garray_T *gap = &lp->sl_sal;
3925
3926 sfirst = lp->sl_sal_first;
3927 for (i = 0; i < 256; ++i)
3928 sfirst[i] = -1;
3929 smp = (salitem_T *)gap->ga_data;
3930 for (i = 0; i < gap->ga_len; ++i)
3931 {
3932#ifdef FEAT_MBYTE
3933 if (has_mbyte)
3934 /* Use the lowest byte of the first character. For latin1 it's
3935 * the character, for other encodings it should differ for most
3936 * characters. */
3937 c = *smp[i].sm_lead_w & 0xff;
3938 else
3939#endif
3940 c = *smp[i].sm_lead;
3941 if (sfirst[c] == -1)
3942 {
3943 sfirst[c] = i;
3944#ifdef FEAT_MBYTE
3945 if (has_mbyte)
3946 {
3947 int n;
3948
3949 /* Make sure all entries with this byte are following each
3950 * other. Move the ones that are in the wrong position. Do
3951 * keep the same ordering! */
3952 while (i + 1 < gap->ga_len
3953 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3954 /* Skip over entry with same index byte. */
3955 ++i;
3956
3957 for (n = 1; i + n < gap->ga_len; ++n)
3958 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3959 {
3960 salitem_T tsal;
3961
3962 /* Move entry with same index byte after the entries
3963 * we already found. */
3964 ++i;
3965 --n;
3966 tsal = smp[i + n];
3967 mch_memmove(smp + i + 1, smp + i,
3968 sizeof(salitem_T) * n);
3969 smp[i] = tsal;
3970 }
3971 }
3972#endif
3973 }
3974 }
3975}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003976
Bram Moolenaara1ba8112005-06-28 23:23:32 +00003977#ifdef FEAT_MBYTE
3978/*
3979 * Turn a multi-byte string into a wide character string.
3980 * Return it in allocated memory (NULL for out-of-memory)
3981 */
3982 static int *
3983mb_str2wide(s)
3984 char_u *s;
3985{
3986 int *res;
3987 char_u *p;
3988 int i = 0;
3989
3990 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
3991 if (res != NULL)
3992 {
3993 for (p = s; *p != NUL; )
3994 res[i++] = mb_ptr2char_adv(&p);
3995 res[i] = NUL;
3996 }
3997 return res;
3998}
3999#endif
4000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004001/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00004002 * Read a tree from the .spl or .sug file.
4003 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
4004 * This is skipped when the tree has zero length.
4005 * Returns zero when OK, SP_ value for an error.
4006 */
4007 static int
4008spell_read_tree(fd, bytsp, idxsp, prefixtree, prefixcnt)
4009 FILE *fd;
4010 char_u **bytsp;
4011 idx_T **idxsp;
4012 int prefixtree; /* TRUE for the prefix tree */
4013 int prefixcnt; /* when "prefixtree" is TRUE: prefix count */
4014{
4015 int len;
4016 int idx;
4017 char_u *bp;
4018 idx_T *ip;
4019
4020 /* The tree size was computed when writing the file, so that we can
4021 * allocate it as one long block. <nodecount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004022 len = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004023 if (len < 0)
4024 return SP_TRUNCERROR;
4025 if (len > 0)
4026 {
4027 /* Allocate the byte array. */
4028 bp = lalloc((long_u)len, TRUE);
4029 if (bp == NULL)
4030 return SP_OTHERERROR;
4031 *bytsp = bp;
4032
4033 /* Allocate the index array. */
4034 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
4035 if (ip == NULL)
4036 return SP_OTHERERROR;
4037 *idxsp = ip;
4038
4039 /* Recursively read the tree and store it in the array. */
4040 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
4041 if (idx < 0)
4042 return idx;
4043 }
4044 return 0;
4045}
4046
4047/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004048 * Read one row of siblings from the spell file and store it in the byte array
4049 * "byts" and index array "idxs". Recursively read the children.
4050 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00004051 * NOTE: The code here must match put_node()!
Bram Moolenaar51485f02005-06-04 21:55:20 +00004052 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00004053 * Returns the index (>= 0) following the siblings.
4054 * Returns SP_TRUNCERROR if the file is shorter than expected.
4055 * Returns SP_FORMERROR if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004056 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004057 static idx_T
Bram Moolenaar4770d092006-01-12 23:22:24 +00004058read_tree_node(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004059 FILE *fd;
4060 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004061 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004062 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004063 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004064 int prefixtree; /* TRUE for reading PREFIXTREE */
4065 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004066{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004067 int len;
4068 int i;
4069 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004070 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004071 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004072 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004073#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004074
Bram Moolenaar51485f02005-06-04 21:55:20 +00004075 len = getc(fd); /* <siblingcount> */
4076 if (len <= 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004077 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004078
4079 if (startidx + len >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004080 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004081 byts[idx++] = len;
4082
4083 /* Read the byte values, flag/region bytes and shared indexes. */
4084 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004085 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004086 c = getc(fd); /* <byte> */
4087 if (c < 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004088 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004089 if (c <= BY_SPECIAL)
4090 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004091 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004092 {
4093 /* No flags, all regions. */
4094 idxs[idx] = 0;
4095 c = 0;
4096 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004097 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004098 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004099 if (prefixtree)
4100 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004101 /* Read the optional pflags byte, the prefix ID and the
4102 * condition nr. In idxs[] store the prefix ID in the low
4103 * byte, the condition index shifted up 8 bits, the flags
4104 * shifted up 24 bits. */
4105 if (c == BY_FLAGS)
4106 c = getc(fd) << 24; /* <pflags> */
4107 else
4108 c = 0;
4109
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004110 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004111
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004112 n = get2c(fd); /* <prefcondnr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004113 if (n >= maxprefcondnr)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004114 return SP_FORMERROR;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004115 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004116 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004117 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004118 {
4119 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004120 * idxs[] the flags go in the low two bytes, region above
4121 * that and prefix ID above the region. */
4122 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004123 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004124 if (c2 == BY_FLAGS2)
4125 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004126 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004127 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004128 if (c & WF_AFX)
4129 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004130 }
4131
Bram Moolenaar51485f02005-06-04 21:55:20 +00004132 idxs[idx] = c;
4133 c = 0;
4134 }
4135 else /* c == BY_INDEX */
4136 {
4137 /* <nodeidx> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004138 n = get3c(fd);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004139 if (n < 0 || n >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004140 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004141 idxs[idx] = n + SHARED_MASK;
4142 c = getc(fd); /* <xbyte> */
4143 }
4144 }
4145 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004146 }
4147
Bram Moolenaar51485f02005-06-04 21:55:20 +00004148 /* Recursively read the children for non-shared siblings.
4149 * Skip the end-of-word ones (zero byte value) and the shared ones (and
4150 * remove SHARED_MASK) */
4151 for (i = 1; i <= len; ++i)
4152 if (byts[startidx + i] != 0)
4153 {
4154 if (idxs[startidx + i] & SHARED_MASK)
4155 idxs[startidx + i] &= ~SHARED_MASK;
4156 else
4157 {
4158 idxs[startidx + i] = idx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004159 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004160 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004161 if (idx < 0)
4162 break;
4163 }
4164 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004165
Bram Moolenaar51485f02005-06-04 21:55:20 +00004166 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004167}
4168
4169/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02004170 * Parse 'spelllang' and set w_s->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004171 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004172 */
4173 char_u *
Bram Moolenaar860cae12010-06-05 23:22:07 +02004174did_set_spelllang(wp)
4175 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004176{
4177 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004178 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004179 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00004180 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004181 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004182 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004183 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004184 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004185 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004186 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004187 int len;
4188 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004189 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004190 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004191 char_u *use_region = NULL;
4192 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004193 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004194 int i, j;
4195 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004196 static int recursive = FALSE;
4197 char_u *ret_msg = NULL;
4198 char_u *spl_copy;
4199
4200 /* We don't want to do this recursively. May happen when a language is
4201 * not available and the SpellFileMissing autocommand opens a new buffer
4202 * in which 'spell' is set. */
4203 if (recursive)
4204 return NULL;
4205 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004206
4207 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004208 clear_midword(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004209
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004210 /* Make a copy of 'spellang', the SpellFileMissing autocommands may change
4211 * it under our fingers. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004212 spl_copy = vim_strsave(wp->w_s->b_p_spl);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004213 if (spl_copy == NULL)
4214 goto theend;
4215
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004216 /* loop over comma separated language names. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004217 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004218 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004219 /* Get one language name. */
4220 copy_option_part(&splp, lang, MAXWLEN, ",");
Bram Moolenaar5482f332005-04-17 20:18:43 +00004221 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004222 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004223
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004224 /* If the name ends in ".spl" use it as the name of the spell file.
4225 * If there is a region name let "region" point to it and remove it
4226 * from the name. */
4227 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
4228 {
4229 filename = TRUE;
4230
Bram Moolenaarb6356332005-07-18 21:40:44 +00004231 /* Locate a region and remove it from the file name. */
4232 p = vim_strchr(gettail(lang), '_');
4233 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
4234 && !ASCII_ISALPHA(p[3]))
4235 {
4236 vim_strncpy(region_cp, p + 1, 2);
4237 mch_memmove(p, p + 3, len - (p - lang) - 2);
4238 len -= 3;
4239 region = region_cp;
4240 }
4241 else
4242 dont_use_region = TRUE;
4243
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004244 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004245 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4246 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004247 break;
4248 }
4249 else
4250 {
4251 filename = FALSE;
4252 if (len > 3 && lang[len - 3] == '_')
4253 {
4254 region = lang + len - 2;
4255 len -= 3;
4256 lang[len] = NUL;
4257 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004258 else
4259 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004260
4261 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004262 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4263 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004264 break;
4265 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004266
Bram Moolenaarb6356332005-07-18 21:40:44 +00004267 if (region != NULL)
4268 {
4269 /* If the region differs from what was used before then don't
4270 * use it for 'spellfile'. */
4271 if (use_region != NULL && STRCMP(region, use_region) != 0)
4272 dont_use_region = TRUE;
4273 use_region = region;
4274 }
4275
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004276 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004277 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004278 {
4279 if (filename)
4280 (void)spell_load_file(lang, lang, NULL, FALSE);
4281 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004282 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004283 spell_load_lang(lang);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004284#ifdef FEAT_AUTOCMD
4285 /* SpellFileMissing autocommands may do anything, including
4286 * destroying the buffer we are using... */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004287 if (!buf_valid(wp->w_buffer))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004288 {
4289 ret_msg = (char_u *)"E797: SpellFileMissing autocommand deleted buffer";
4290 goto theend;
4291 }
4292#endif
4293 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004294 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004295
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004296 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004297 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004298 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004299 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4300 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4301 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004302 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004303 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004304 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004305 {
4306 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004307 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004308 if (c == REGION_ALL)
4309 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004310 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004311 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004312 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004313 /* This addition file is for other regions. */
4314 region_mask = 0;
4315 }
4316 else
4317 /* This is probably an error. Give a warning and
4318 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004319 smsg((char_u *)
4320 _("Warning: region %s not supported"),
4321 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004322 }
4323 else
4324 region_mask = 1 << c;
4325 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004326
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004327 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004328 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004329 if (ga_grow(&ga, 1) == FAIL)
4330 {
4331 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004332 ret_msg = e_outofmem;
4333 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004334 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004335 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004336 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4337 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004338 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004339 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004340 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004341 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004342 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004343 }
4344
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004345 /* round 0: load int_wordlist, if possible.
4346 * round 1: load first name in 'spellfile'.
4347 * round 2: load second name in 'spellfile.
4348 * etc. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004349 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004350 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004351 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004352 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004353 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004354 /* Internal wordlist, if there is one. */
4355 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004356 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004357 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004358 }
4359 else
4360 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004361 /* One entry in 'spellfile'. */
4362 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4363 STRCAT(spf_name, ".spl");
4364
4365 /* If it was already found above then skip it. */
4366 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004367 {
4368 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4369 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004370 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004371 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004372 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004373 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004374 }
4375
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004376 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004377 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4378 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004379 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004380 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004381 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004382 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004383 * region name, the region is ignored otherwise. for int_wordlist
4384 * use an arbitrary name. */
4385 if (round == 0)
4386 STRCPY(lang, "internal wordlist");
4387 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00004388 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004389 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004390 p = vim_strchr(lang, '.');
4391 if (p != NULL)
4392 *p = NUL; /* truncate at ".encoding.add" */
4393 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004394 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004395
4396 /* If one of the languages has NOBREAK we assume the addition
4397 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004398 if (slang != NULL && nobreak)
4399 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004400 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004401 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004402 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004403 region_mask = REGION_ALL;
4404 if (use_region != NULL && !dont_use_region)
4405 {
4406 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004407 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004408 if (c != REGION_ALL)
4409 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004410 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004411 /* This spell file is for other regions. */
4412 region_mask = 0;
4413 }
4414
4415 if (region_mask != 0)
4416 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004417 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4418 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4419 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004420 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4421 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004422 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004423 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004424 }
4425 }
4426
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004427 /* Everything is fine, store the new b_langp value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004428 ga_clear(&wp->w_s->b_langp);
4429 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004430
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004431 /* For each language figure out what language to use for sound folding and
4432 * REP items. If the language doesn't support it itself use another one
4433 * with the same name. E.g. for "en-math" use "en". */
4434 for (i = 0; i < ga.ga_len; ++i)
4435 {
4436 lp = LANGP_ENTRY(ga, i);
4437
4438 /* sound folding */
4439 if (lp->lp_slang->sl_sal.ga_len > 0)
4440 /* language does sound folding itself */
4441 lp->lp_sallang = lp->lp_slang;
4442 else
4443 /* find first similar language that does sound folding */
4444 for (j = 0; j < ga.ga_len; ++j)
4445 {
4446 lp2 = LANGP_ENTRY(ga, j);
4447 if (lp2->lp_slang->sl_sal.ga_len > 0
4448 && STRNCMP(lp->lp_slang->sl_name,
4449 lp2->lp_slang->sl_name, 2) == 0)
4450 {
4451 lp->lp_sallang = lp2->lp_slang;
4452 break;
4453 }
4454 }
4455
4456 /* REP items */
4457 if (lp->lp_slang->sl_rep.ga_len > 0)
4458 /* language has REP items itself */
4459 lp->lp_replang = lp->lp_slang;
4460 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004461 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004462 for (j = 0; j < ga.ga_len; ++j)
4463 {
4464 lp2 = LANGP_ENTRY(ga, j);
4465 if (lp2->lp_slang->sl_rep.ga_len > 0
4466 && STRNCMP(lp->lp_slang->sl_name,
4467 lp2->lp_slang->sl_name, 2) == 0)
4468 {
4469 lp->lp_replang = lp2->lp_slang;
4470 break;
4471 }
4472 }
4473 }
4474
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004475theend:
4476 vim_free(spl_copy);
4477 recursive = FALSE;
4478 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004479}
4480
4481/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004482 * Clear the midword characters for buffer "buf".
4483 */
4484 static void
Bram Moolenaar860cae12010-06-05 23:22:07 +02004485clear_midword(wp)
4486 win_T *wp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004487{
Bram Moolenaar860cae12010-06-05 23:22:07 +02004488 vim_memset(wp->w_s->b_spell_ismw, 0, 256);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004489#ifdef FEAT_MBYTE
Bram Moolenaar860cae12010-06-05 23:22:07 +02004490 vim_free(wp->w_s->b_spell_ismw_mb);
4491 wp->w_s->b_spell_ismw_mb = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004492#endif
4493}
4494
4495/*
4496 * Use the "sl_midword" field of language "lp" for buffer "buf".
4497 * They add up to any currently used midword characters.
4498 */
4499 static void
Bram Moolenaar860cae12010-06-05 23:22:07 +02004500use_midword(lp, wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004501 slang_T *lp;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004502 win_T *wp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004503{
4504 char_u *p;
4505
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004506 if (lp->sl_midword == NULL) /* there aren't any */
4507 return;
4508
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004509 for (p = lp->sl_midword; *p != NUL; )
4510#ifdef FEAT_MBYTE
4511 if (has_mbyte)
4512 {
4513 int c, l, n;
4514 char_u *bp;
4515
4516 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004517 l = (*mb_ptr2len)(p);
4518 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004519 wp->w_s->b_spell_ismw[c] = TRUE;
4520 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004521 /* First multi-byte char in "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004522 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004523 else
4524 {
4525 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004526 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
4527 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004528 if (bp != NULL)
4529 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004530 vim_free(wp->w_s->b_spell_ismw_mb);
4531 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004532 vim_strncpy(bp + n, p, l);
4533 }
4534 }
4535 p += l;
4536 }
4537 else
4538#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02004539 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004540}
4541
4542/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004543 * Find the region "region[2]" in "rp" (points to "sl_regions").
4544 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00004545 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004546 */
4547 static int
4548find_region(rp, region)
4549 char_u *rp;
4550 char_u *region;
4551{
4552 int i;
4553
4554 for (i = 0; ; i += 2)
4555 {
4556 if (rp[i] == NUL)
4557 return REGION_ALL;
4558 if (rp[i] == region[0] && rp[i + 1] == region[1])
4559 break;
4560 }
4561 return i / 2;
4562}
4563
4564/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004565 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004566 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00004567 * Word WF_ONECAP
4568 * W WORD WF_ALLCAP
4569 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004570 */
4571 static int
4572captype(word, end)
4573 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004574 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004575{
4576 char_u *p;
4577 int c;
4578 int firstcap;
4579 int allcap;
4580 int past_second = FALSE; /* past second word char */
4581
4582 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004583 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004584 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004585 return 0; /* only non-word characters, illegal word */
4586#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004587 if (has_mbyte)
4588 c = mb_ptr2char_adv(&p);
4589 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004590#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004591 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004592 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004593
4594 /*
4595 * Need to check all letters to find a word with mixed upper/lower.
4596 * But a word with an upper char only at start is a ONECAP.
4597 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004598 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004599 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004600 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004601 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004602 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004603 {
4604 /* UUl -> KEEPCAP */
4605 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004606 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004607 allcap = FALSE;
4608 }
4609 else if (!allcap)
4610 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004611 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004612 past_second = TRUE;
4613 }
4614
4615 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004616 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004617 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004618 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004619 return 0;
4620}
4621
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004622/*
4623 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4624 * capital. So that make_case_word() can turn WOrd into Word.
4625 * Add ALLCAP for "WOrD".
4626 */
4627 static int
4628badword_captype(word, end)
4629 char_u *word;
4630 char_u *end;
4631{
4632 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004633 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004634 int l, u;
4635 int first;
4636 char_u *p;
4637
4638 if (flags & WF_KEEPCAP)
4639 {
4640 /* Count the number of UPPER and lower case letters. */
4641 l = u = 0;
4642 first = FALSE;
4643 for (p = word; p < end; mb_ptr_adv(p))
4644 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004645 c = PTR2CHAR(p);
4646 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004647 {
4648 ++u;
4649 if (p == word)
4650 first = TRUE;
4651 }
4652 else
4653 ++l;
4654 }
4655
4656 /* If there are more UPPER than lower case letters suggest an
4657 * ALLCAP word. Otherwise, if the first letter is UPPER then
4658 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4659 * require three upper case letters. */
4660 if (u > l && u > 2)
4661 flags |= WF_ALLCAP;
4662 else if (first)
4663 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004664
4665 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4666 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004667 }
4668 return flags;
4669}
4670
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004671# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4672/*
4673 * Free all languages.
4674 */
4675 void
4676spell_free_all()
4677{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004678 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004679 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004680 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004681
Bram Moolenaar860cae12010-06-05 23:22:07 +02004682 /* Go through all buffers and handle 'spelllang'. */ //<VN>
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004683 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004684 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004685
4686 while (first_lang != NULL)
4687 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004688 slang = first_lang;
4689 first_lang = slang->sl_next;
4690 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004691 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004692
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004693 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004694 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004695 /* Delete the internal wordlist and its .spl file */
4696 mch_remove(int_wordlist);
4697 int_wordlist_spl(fname);
4698 mch_remove(fname);
4699 vim_free(int_wordlist);
4700 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004701 }
4702
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004703 init_spell_chartab();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004704
4705 vim_free(repl_to);
4706 repl_to = NULL;
4707 vim_free(repl_from);
4708 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004709}
4710# endif
4711
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004712# if defined(FEAT_MBYTE) || defined(PROTO)
4713/*
4714 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004715 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004716 */
4717 void
4718spell_reload()
4719{
Bram Moolenaar3982c542005-06-08 21:56:31 +00004720 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004721
Bram Moolenaarea408852005-06-25 22:49:46 +00004722 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004723 init_spell_chartab();
4724
4725 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004726 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004727
4728 /* Go through all buffers and handle 'spelllang'. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004729 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004730 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004731 /* Only load the wordlists when 'spelllang' is set and there is a
4732 * window for this buffer in which 'spell' is set. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004733 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004734 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004735 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004736 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004737 (void)did_set_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004738# ifdef FEAT_WINDOWS
4739 break;
4740# endif
4741 }
4742 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004743 }
4744}
4745# endif
4746
Bram Moolenaarb765d632005-06-07 21:00:02 +00004747/*
4748 * Reload the spell file "fname" if it's loaded.
4749 */
4750 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004751spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004752 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004753 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004754{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004755 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004756 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004757
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004758 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004759 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004760 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004761 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004762 slang_clear(slang);
4763 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004764 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004765 slang_clear(slang);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004766 redraw_all_later(SOME_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004767 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004768 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004769 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004770
4771 /* When "zg" was used and the file wasn't loaded yet, should redo
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004772 * 'spelllang' to load it now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004773 if (added_word && !didit)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004774 did_set_spelllang(curwin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004775}
4776
4777
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004778/*
4779 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004780 */
4781
Bram Moolenaar51485f02005-06-04 21:55:20 +00004782#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004783 and .dic file. */
4784/*
4785 * Main structure to store the contents of a ".aff" file.
4786 */
4787typedef struct afffile_S
4788{
4789 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004790 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004791 unsigned af_rare; /* RARE ID for rare word */
4792 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004793 unsigned af_bad; /* BAD ID for banned word */
4794 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004795 unsigned af_circumfix; /* CIRCUMFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004796 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004797 unsigned af_comproot; /* COMPOUNDROOT ID */
4798 unsigned af_compforbid; /* COMPOUNDFORBIDFLAG ID */
4799 unsigned af_comppermit; /* COMPOUNDPERMITFLAG ID */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004800 unsigned af_nosuggest; /* NOSUGGEST ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004801 int af_pfxpostpone; /* postpone prefixes without chop string and
4802 without flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004803 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4804 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004805 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004806} afffile_T;
4807
Bram Moolenaar6de68532005-08-24 22:08:48 +00004808#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004809#define AFT_LONG 1 /* flags are two characters */
4810#define AFT_CAPLONG 2 /* flags are one or two characters */
4811#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004812
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004813typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004814/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4815struct affentry_S
4816{
4817 affentry_T *ae_next; /* next affix with same name/number */
4818 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4819 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004820 char_u *ae_flags; /* flags on the affix (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004821 char_u *ae_cond; /* condition (NULL for ".") */
4822 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004823 char ae_compforbid; /* COMPOUNDFORBIDFLAG found */
4824 char ae_comppermit; /* COMPOUNDPERMITFLAG found */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004825};
4826
Bram Moolenaar6de68532005-08-24 22:08:48 +00004827#ifdef FEAT_MBYTE
4828# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4829#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004830# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004831#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004832
Bram Moolenaar51485f02005-06-04 21:55:20 +00004833/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4834typedef struct affheader_S
4835{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004836 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4837 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4838 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004839 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004840 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004841 affentry_T *ah_first; /* first affix entry */
4842} affheader_T;
4843
4844#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4845
Bram Moolenaar6de68532005-08-24 22:08:48 +00004846/* Flag used in compound items. */
4847typedef struct compitem_S
4848{
4849 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4850 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4851 int ci_newID; /* affix ID after renumbering. */
4852} compitem_T;
4853
4854#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4855
Bram Moolenaar51485f02005-06-04 21:55:20 +00004856/*
4857 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004858 * the need to keep track of each allocated thing, everything is freed all at
4859 * once after ":mkspell" is done.
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00004860 * Note: "sb_next" must be just before "sb_data" to make sure the alignment of
4861 * "sb_data" is correct for systems where pointers must be aligned on
4862 * pointer-size boundaries and sizeof(pointer) > sizeof(int) (e.g., Sparc).
Bram Moolenaar51485f02005-06-04 21:55:20 +00004863 */
4864#define SBLOCKSIZE 16000 /* size of sb_data */
4865typedef struct sblock_S sblock_T;
4866struct sblock_S
4867{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004868 int sb_used; /* nr of bytes already in use */
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00004869 sblock_T *sb_next; /* next block in list */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004870 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004871};
4872
4873/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004874 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004875 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004876typedef struct wordnode_S wordnode_T;
4877struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004878{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004879 union /* shared to save space */
4880 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004881 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004882 int index; /* index in written nodes (valid after first
4883 round) */
4884 } wn_u1;
4885 union /* shared to save space */
4886 {
4887 wordnode_T *next; /* next node with same hash key */
4888 wordnode_T *wnode; /* parent node that will write this node */
4889 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004890 wordnode_T *wn_child; /* child (next byte in word) */
4891 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4892 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004893 int wn_refs; /* Nr. of references to this node. Only
4894 relevant for first node in a list of
4895 siblings, in following siblings it is
4896 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004897 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004898
4899 /* Info for when "wn_byte" is NUL.
4900 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4901 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4902 * "wn_region" the LSW of the wordnr. */
4903 char_u wn_affixID; /* supported/required prefix ID or 0 */
4904 short_u wn_flags; /* WF_ flags */
4905 short wn_region; /* region mask */
4906
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004907#ifdef SPELL_PRINTTREE
4908 int wn_nr; /* sequence nr for printing */
4909#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004910};
4911
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004912#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4913
Bram Moolenaar51485f02005-06-04 21:55:20 +00004914#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004915
Bram Moolenaar51485f02005-06-04 21:55:20 +00004916/*
4917 * Info used while reading the spell files.
4918 */
4919typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004920{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004921 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004922 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004923
Bram Moolenaar51485f02005-06-04 21:55:20 +00004924 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004925 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004926
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004927 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004928
Bram Moolenaar4770d092006-01-12 23:22:24 +00004929 long si_sugtree; /* creating the soundfolding trie */
4930
Bram Moolenaar51485f02005-06-04 21:55:20 +00004931 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004932 long si_blocks_cnt; /* memory blocks allocated */
4933 long si_compress_cnt; /* words to add before lowering
4934 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004935 wordnode_T *si_first_free; /* List of nodes that have been freed during
4936 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004937 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004938#ifdef SPELL_PRINTTREE
4939 int si_wordnode_nr; /* sequence nr for nodes */
4940#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004941 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004942
Bram Moolenaar51485f02005-06-04 21:55:20 +00004943 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004944 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004945 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004946 int si_region; /* region mask */
4947 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004948 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004949 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004950 int si_msg_count; /* number of words added since last message */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004951 char_u *si_info; /* info text chars or NULL */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004952 int si_region_count; /* number of regions supported (1 when there
4953 are no regions) */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004954 char_u si_region_name[16]; /* region names; used only if
4955 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004956
4957 garray_T si_rep; /* list of fromto_T entries from REP lines */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004958 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004959 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004960 char_u *si_sofofr; /* SOFOFROM text */
4961 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004962 int si_nosugfile; /* NOSUGFILE item found */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004963 int si_nosplitsugs; /* NOSPLITSUGS item found */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004964 int si_followup; /* soundsalike: ? */
4965 int si_collapse; /* soundsalike: ? */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004966 hashtab_T si_commonwords; /* hashtable for common words */
4967 time_t si_sugtime; /* timestamp for .sug file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004968 int si_rem_accents; /* soundsalike: remove accents */
4969 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004970 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004971 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004972 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004973 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004974 int si_compoptions; /* COMP_ flags */
4975 garray_T si_comppat; /* CHECKCOMPOUNDPATTERN items, each stored as
4976 a string */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004977 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00004978 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004979 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004980 garray_T si_prefcond; /* table with conditions for postponed
4981 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004982 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004983 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004984} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004985
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004986static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9f94b052008-11-30 20:12:46 +00004987static int is_aff_rule __ARGS((char_u **items, int itemcnt, char *rulename, int mincount));
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004988static void aff_process_flags __ARGS((afffile_T *affile, affentry_T *entry));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004989static int spell_info_item __ARGS((char_u *s));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004990static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
4991static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
4992static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004993static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00004994static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
4995static void aff_check_number __ARGS((int spinval, int affval, char *name));
4996static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004997static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004998static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
4999static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00005000static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00005001static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005002static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005003static int get_affix_flags __ARGS((afffile_T *affile, char_u *afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00005004static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00005005static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005006static 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 +00005007static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
5008static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
5009static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00005010static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005011static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00005012static 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 +00005013static 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 +00005014static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005015static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005016static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
5017static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
5018static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00005019static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005020static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00005021static void clear_node __ARGS((wordnode_T *node));
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005022static int put_node __ARGS((FILE *fd, wordnode_T *node, int idx, int regionmask, int prefixtree));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005023static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname));
5024static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang));
5025static int sug_maketable __ARGS((spellinfo_T *spin));
5026static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap));
5027static int offset2bytes __ARGS((int nr, char_u *buf));
5028static int bytes2offset __ARGS((char_u **pp));
5029static void sug_write __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005030static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005031static void spell_message __ARGS((spellinfo_T *spin, char_u *str));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005032static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005033
Bram Moolenaar53805d12005-08-01 07:08:33 +00005034/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
5035 * but it must be negative to indicate the prefix tree to tree_add_word().
5036 * Use a negative number with the lower 8 bits zero. */
5037#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005038
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005039/* flags for "condit" argument of store_aff_word() */
5040#define CONDIT_COMB 1 /* affix must combine */
5041#define CONDIT_CFIX 2 /* affix must have CIRCUMFIX flag */
5042#define CONDIT_SUF 4 /* add a suffix for matching flags */
5043#define CONDIT_AFF 8 /* word already has an affix */
5044
Bram Moolenaar5195e452005-08-19 20:32:47 +00005045/*
5046 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
5047 */
5048static long compress_start = 30000; /* memory / SBLOCKSIZE */
5049static long compress_inc = 100; /* memory / SBLOCKSIZE */
5050static long compress_added = 500000; /* word count */
5051
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005052#ifdef SPELL_PRINTTREE
5053/*
5054 * For debugging the tree code: print the current tree in a (more or less)
5055 * readable format, so that we can see what happens when adding a word and/or
5056 * compressing the tree.
5057 * Based on code from Olaf Seibert.
5058 */
5059#define PRINTLINESIZE 1000
5060#define PRINTWIDTH 6
5061
5062#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
5063 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
5064
5065static char line1[PRINTLINESIZE];
5066static char line2[PRINTLINESIZE];
5067static char line3[PRINTLINESIZE];
5068
5069 static void
5070spell_clear_flags(wordnode_T *node)
5071{
5072 wordnode_T *np;
5073
5074 for (np = node; np != NULL; np = np->wn_sibling)
5075 {
5076 np->wn_u1.index = FALSE;
5077 spell_clear_flags(np->wn_child);
5078 }
5079}
5080
5081 static void
5082spell_print_node(wordnode_T *node, int depth)
5083{
5084 if (node->wn_u1.index)
5085 {
5086 /* Done this node before, print the reference. */
5087 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
5088 PRINTSOME(line2, depth, " ", 0, 0);
5089 PRINTSOME(line3, depth, " ", 0, 0);
5090 msg(line1);
5091 msg(line2);
5092 msg(line3);
5093 }
5094 else
5095 {
5096 node->wn_u1.index = TRUE;
5097
5098 if (node->wn_byte != NUL)
5099 {
5100 if (node->wn_child != NULL)
5101 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
5102 else
5103 /* Cannot happen? */
5104 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
5105 }
5106 else
5107 PRINTSOME(line1, depth, " $ ", 0, 0);
5108
5109 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
5110
5111 if (node->wn_sibling != NULL)
5112 PRINTSOME(line3, depth, " | ", 0, 0);
5113 else
5114 PRINTSOME(line3, depth, " ", 0, 0);
5115
5116 if (node->wn_byte == NUL)
5117 {
5118 msg(line1);
5119 msg(line2);
5120 msg(line3);
5121 }
5122
5123 /* do the children */
5124 if (node->wn_byte != NUL && node->wn_child != NULL)
5125 spell_print_node(node->wn_child, depth + 1);
5126
5127 /* do the siblings */
5128 if (node->wn_sibling != NULL)
5129 {
5130 /* get rid of all parent details except | */
5131 STRCPY(line1, line3);
5132 STRCPY(line2, line3);
5133 spell_print_node(node->wn_sibling, depth);
5134 }
5135 }
5136}
5137
5138 static void
5139spell_print_tree(wordnode_T *root)
5140{
5141 if (root != NULL)
5142 {
5143 /* Clear the "wn_u1.index" fields, used to remember what has been
5144 * done. */
5145 spell_clear_flags(root);
5146
5147 /* Recursively print the tree. */
5148 spell_print_node(root, 0);
5149 }
5150}
5151#endif /* SPELL_PRINTTREE */
5152
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005153/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005154 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00005155 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005156 */
5157 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005158spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005159 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005160 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005161{
5162 FILE *fd;
5163 afffile_T *aff;
5164 char_u rline[MAXLINELEN];
5165 char_u *line;
5166 char_u *pc = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005167#define MAXITEMCNT 30
Bram Moolenaar8db73182005-06-17 21:51:16 +00005168 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005169 int itemcnt;
5170 char_u *p;
5171 int lnum = 0;
5172 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005173 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005174 int aff_todo = 0;
5175 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005176 char_u *low = NULL;
5177 char_u *fol = NULL;
5178 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005179 int do_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005180 int do_repsal;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005181 int do_sal;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005182 int do_mapline;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005183 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005184 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005185 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005186 int compminlen = 0; /* COMPOUNDMIN value */
5187 int compsylmax = 0; /* COMPOUNDSYLMAX value */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005188 int compoptions = 0; /* COMP_ flags */
5189 int compmax = 0; /* COMPOUNDWORDMAX value */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005190 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDRULE
Bram Moolenaar6de68532005-08-24 22:08:48 +00005191 concatenated */
5192 char_u *midword = NULL; /* MIDWORD value */
5193 char_u *syllable = NULL; /* SYLLABLE value */
5194 char_u *sofofrom = NULL; /* SOFOFROM value */
5195 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005196
Bram Moolenaar51485f02005-06-04 21:55:20 +00005197 /*
5198 * Open the file.
5199 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005200 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005201 if (fd == NULL)
5202 {
5203 EMSG2(_(e_notopen), fname);
5204 return NULL;
5205 }
5206
Bram Moolenaar4770d092006-01-12 23:22:24 +00005207 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
5208 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005209
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005210 /* Only do REP lines when not done in another .aff file already. */
5211 do_rep = spin->si_rep.ga_len == 0;
5212
Bram Moolenaar4770d092006-01-12 23:22:24 +00005213 /* Only do REPSAL lines when not done in another .aff file already. */
5214 do_repsal = spin->si_repsal.ga_len == 0;
5215
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005216 /* Only do SAL lines when not done in another .aff file already. */
5217 do_sal = spin->si_sal.ga_len == 0;
5218
5219 /* Only do MAP lines when not done in another .aff file already. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00005220 do_mapline = spin->si_map.ga_len == 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005221
Bram Moolenaar51485f02005-06-04 21:55:20 +00005222 /*
5223 * Allocate and init the afffile_T structure.
5224 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005225 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005226 if (aff == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005227 {
5228 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005229 return NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005230 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005231 hash_init(&aff->af_pref);
5232 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005233 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005234
5235 /*
5236 * Read all the lines in the file one by one.
5237 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005238 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005239 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005240 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005241 ++lnum;
5242
5243 /* Skip comment lines. */
5244 if (*rline == '#')
5245 continue;
5246
5247 /* Convert from "SET" to 'encoding' when needed. */
5248 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005249#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005250 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005251 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005252 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005253 if (pc == NULL)
5254 {
5255 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5256 fname, lnum, rline);
5257 continue;
5258 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005259 line = pc;
5260 }
5261 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005262#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005263 {
5264 pc = NULL;
5265 line = rline;
5266 }
5267
5268 /* Split the line up in white separated items. Put a NUL after each
5269 * item. */
5270 itemcnt = 0;
5271 for (p = line; ; )
5272 {
5273 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
5274 ++p;
5275 if (*p == NUL)
5276 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00005277 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005278 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005279 items[itemcnt++] = p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005280 /* A few items have arbitrary text argument, don't split them. */
5281 if (itemcnt == 2 && spell_info_item(items[0]))
5282 while (*p >= ' ' || *p == TAB) /* skip until CR/NL */
5283 ++p;
5284 else
5285 while (*p > ' ') /* skip until white space or CR/NL */
5286 ++p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005287 if (*p == NUL)
5288 break;
5289 *p++ = NUL;
5290 }
5291
5292 /* Handle non-empty lines. */
5293 if (itemcnt > 0)
5294 {
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005295 if (is_aff_rule(items, itemcnt, "SET", 2) && aff->af_enc == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005296 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005297#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005298 /* Setup for conversion from "ENC" to 'encoding'. */
5299 aff->af_enc = enc_canonize(items[1]);
5300 if (aff->af_enc != NULL && !spin->si_ascii
5301 && convert_setup(&spin->si_conv, aff->af_enc,
5302 p_enc) == FAIL)
5303 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
5304 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005305 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005306#else
5307 smsg((char_u *)_("Conversion in %s not supported"), fname);
5308#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005309 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005310 else if (is_aff_rule(items, itemcnt, "FLAG", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005311 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005312 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005313 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005314 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005315 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005316 aff->af_flagtype = AFT_NUM;
5317 else if (STRCMP(items[1], "caplong") == 0)
5318 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005319 else
5320 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
5321 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00005322 if (aff->af_rare != 0
5323 || aff->af_keepcase != 0
5324 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005325 || aff->af_needaffix != 0
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005326 || aff->af_circumfix != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005327 || aff->af_needcomp != 0
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005328 || aff->af_comproot != 0
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005329 || aff->af_nosuggest != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005330 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00005331 || aff->af_suff.ht_used > 0
5332 || aff->af_pref.ht_used > 0)
5333 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
5334 fname, lnum, items[1]);
5335 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005336 else if (spell_info_item(items[0]))
5337 {
5338 p = (char_u *)getroom(spin,
5339 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
5340 + STRLEN(items[0])
5341 + STRLEN(items[1]) + 3, FALSE);
5342 if (p != NULL)
5343 {
5344 if (spin->si_info != NULL)
5345 {
5346 STRCPY(p, spin->si_info);
5347 STRCAT(p, "\n");
5348 }
5349 STRCAT(p, items[0]);
5350 STRCAT(p, " ");
5351 STRCAT(p, items[1]);
5352 spin->si_info = p;
5353 }
5354 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005355 else if (is_aff_rule(items, itemcnt, "MIDWORD", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005356 && midword == NULL)
5357 {
5358 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005359 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005360 else if (is_aff_rule(items, itemcnt, "TRY", 2))
Bram Moolenaar51485f02005-06-04 21:55:20 +00005361 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005362 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005363 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005364 /* TODO: remove "RAR" later */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005365 else if ((is_aff_rule(items, itemcnt, "RAR", 2)
5366 || is_aff_rule(items, itemcnt, "RARE", 2))
5367 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005368 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005369 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005370 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005371 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005372 /* TODO: remove "KEP" later */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005373 else if ((is_aff_rule(items, itemcnt, "KEP", 2)
5374 || is_aff_rule(items, itemcnt, "KEEPCASE", 2))
Bram Moolenaar371baa92005-12-29 22:43:53 +00005375 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005376 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005377 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005378 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005379 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005380 else if ((is_aff_rule(items, itemcnt, "BAD", 2)
5381 || is_aff_rule(items, itemcnt, "FORBIDDENWORD", 2))
5382 && aff->af_bad == 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005383 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005384 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5385 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005386 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005387 else if (is_aff_rule(items, itemcnt, "NEEDAFFIX", 2)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005388 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005389 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005390 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5391 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005392 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005393 else if (is_aff_rule(items, itemcnt, "CIRCUMFIX", 2)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005394 && aff->af_circumfix == 0)
5395 {
5396 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
5397 fname, lnum);
5398 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005399 else if (is_aff_rule(items, itemcnt, "NOSUGGEST", 2)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005400 && aff->af_nosuggest == 0)
5401 {
5402 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
5403 fname, lnum);
5404 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005405 else if ((is_aff_rule(items, itemcnt, "NEEDCOMPOUND", 2)
5406 || is_aff_rule(items, itemcnt, "ONLYINCOMPOUND", 2))
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005407 && aff->af_needcomp == 0)
5408 {
5409 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5410 fname, lnum);
5411 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005412 else if (is_aff_rule(items, itemcnt, "COMPOUNDROOT", 2)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005413 && aff->af_comproot == 0)
5414 {
5415 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
5416 fname, lnum);
5417 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005418 else if (is_aff_rule(items, itemcnt, "COMPOUNDFORBIDFLAG", 2)
5419 && aff->af_compforbid == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005420 {
5421 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
5422 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005423 if (aff->af_pref.ht_used > 0)
5424 smsg((char_u *)_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
5425 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005426 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005427 else if (is_aff_rule(items, itemcnt, "COMPOUNDPERMITFLAG", 2)
5428 && aff->af_comppermit == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005429 {
5430 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
5431 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005432 if (aff->af_pref.ht_used > 0)
5433 smsg((char_u *)_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
5434 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005435 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005436 else if (is_aff_rule(items, itemcnt, "COMPOUNDFLAG", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005437 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005438 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005439 /* Turn flag "c" into COMPOUNDRULE compatible string "c+",
Bram Moolenaar6de68532005-08-24 22:08:48 +00005440 * "Na" into "Na+", "1234" into "1234+". */
5441 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005442 if (p != NULL)
5443 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005444 STRCPY(p, items[1]);
5445 STRCAT(p, "+");
5446 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005447 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005448 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005449 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULES", 2))
5450 {
5451 /* We don't use the count, but do check that it's a number and
5452 * not COMPOUNDRULE mistyped. */
5453 if (atoi((char *)items[1]) == 0)
5454 smsg((char_u *)_("Wrong COMPOUNDRULES value in %s line %d: %s"),
5455 fname, lnum, items[1]);
5456 }
5457 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULE", 2))
Bram Moolenaar5195e452005-08-19 20:32:47 +00005458 {
5459 /* Concatenate this string to previously defined ones, using a
5460 * slash to separate them. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005461 l = (int)STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005462 if (compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005463 l += (int)STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005464 p = getroom(spin, l, FALSE);
5465 if (p != NULL)
5466 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005467 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005468 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005469 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005470 STRCAT(p, "/");
5471 }
5472 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005473 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005474 }
5475 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005476 else if (is_aff_rule(items, itemcnt, "COMPOUNDWORDMAX", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005477 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005478 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005479 compmax = atoi((char *)items[1]);
5480 if (compmax == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005481 smsg((char_u *)_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar5195e452005-08-19 20:32:47 +00005482 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005483 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005484 else if (is_aff_rule(items, itemcnt, "COMPOUNDMIN", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005485 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005486 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005487 compminlen = atoi((char *)items[1]);
5488 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005489 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5490 fname, lnum, items[1]);
5491 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005492 else if (is_aff_rule(items, itemcnt, "COMPOUNDSYLMAX", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005493 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005494 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005495 compsylmax = atoi((char *)items[1]);
5496 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005497 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5498 fname, lnum, items[1]);
5499 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005500 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDDUP", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005501 {
5502 compoptions |= COMP_CHECKDUP;
5503 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005504 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDREP", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005505 {
5506 compoptions |= COMP_CHECKREP;
5507 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005508 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDCASE", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005509 {
5510 compoptions |= COMP_CHECKCASE;
5511 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005512 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDTRIPLE", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005513 {
5514 compoptions |= COMP_CHECKTRIPLE;
5515 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005516 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 2))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005517 {
5518 if (atoi((char *)items[1]) == 0)
5519 smsg((char_u *)_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
5520 fname, lnum, items[1]);
5521 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005522 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 3))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005523 {
5524 garray_T *gap = &spin->si_comppat;
5525 int i;
5526
5527 /* Only add the couple if it isn't already there. */
5528 for (i = 0; i < gap->ga_len - 1; i += 2)
5529 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
5530 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
5531 items[2]) == 0)
5532 break;
5533 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
5534 {
5535 ((char_u **)(gap->ga_data))[gap->ga_len++]
5536 = getroom_save(spin, items[1]);
5537 ((char_u **)(gap->ga_data))[gap->ga_len++]
5538 = getroom_save(spin, items[2]);
5539 }
5540 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005541 else if (is_aff_rule(items, itemcnt, "SYLLABLE", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005542 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005543 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005544 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005545 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005546 else if (is_aff_rule(items, itemcnt, "NOBREAK", 1))
Bram Moolenaar78622822005-08-23 21:00:13 +00005547 {
5548 spin->si_nobreak = TRUE;
5549 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005550 else if (is_aff_rule(items, itemcnt, "NOSPLITSUGS", 1))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005551 {
5552 spin->si_nosplitsugs = TRUE;
5553 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005554 else if (is_aff_rule(items, itemcnt, "NOSUGFILE", 1))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005555 {
5556 spin->si_nosugfile = TRUE;
5557 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005558 else if (is_aff_rule(items, itemcnt, "PFXPOSTPONE", 1))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005559 {
5560 aff->af_pfxpostpone = TRUE;
5561 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005562 else if ((STRCMP(items[0], "PFX") == 0
5563 || STRCMP(items[0], "SFX") == 0)
5564 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005565 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005566 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005567 int lasti = 4;
5568 char_u key[AH_KEY_LEN];
5569
5570 if (*items[0] == 'P')
5571 tp = &aff->af_pref;
5572 else
5573 tp = &aff->af_suff;
5574
5575 /* Myspell allows the same affix name to be used multiple
5576 * times. The affix files that do this have an undocumented
5577 * "S" flag on all but the last block, thus we check for that
5578 * and store it in ah_follows. */
5579 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5580 hi = hash_find(tp, key);
5581 if (!HASHITEM_EMPTY(hi))
5582 {
5583 cur_aff = HI2AH(hi);
5584 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5585 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5586 fname, lnum, items[1]);
5587 if (!cur_aff->ah_follows)
5588 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5589 fname, lnum, items[1]);
5590 }
5591 else
5592 {
5593 /* New affix letter. */
5594 cur_aff = (affheader_T *)getroom(spin,
5595 sizeof(affheader_T), TRUE);
5596 if (cur_aff == NULL)
5597 break;
5598 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5599 fname, lnum);
5600 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5601 break;
5602 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00005603 || cur_aff->ah_flag == aff->af_rare
5604 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005605 || cur_aff->ah_flag == aff->af_needaffix
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005606 || cur_aff->ah_flag == aff->af_circumfix
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005607 || cur_aff->ah_flag == aff->af_nosuggest
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005608 || cur_aff->ah_flag == aff->af_needcomp
5609 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005610 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 +00005611 fname, lnum, items[1]);
5612 STRCPY(cur_aff->ah_key, items[1]);
5613 hash_add(tp, cur_aff->ah_key);
5614
5615 cur_aff->ah_combine = (*items[2] == 'Y');
5616 }
5617
5618 /* Check for the "S" flag, which apparently means that another
5619 * block with the same affix name is following. */
5620 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5621 {
5622 ++lasti;
5623 cur_aff->ah_follows = TRUE;
5624 }
5625 else
5626 cur_aff->ah_follows = FALSE;
5627
Bram Moolenaar8db73182005-06-17 21:51:16 +00005628 /* Myspell allows extra text after the item, but that might
5629 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00005630 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005631 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005632
Bram Moolenaar95529562005-08-25 21:21:38 +00005633 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005634 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5635 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005636
Bram Moolenaar95529562005-08-25 21:21:38 +00005637 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005638 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005639 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005640 {
5641 /* Use a new number in the .spl file later, to be able
5642 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005643 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005644 cur_aff->ah_newID = ++spin->si_newprefID;
5645
5646 /* We only really use ah_newID if the prefix is
5647 * postponed. We know that only after handling all
5648 * the items. */
5649 did_postpone_prefix = FALSE;
5650 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005651 else
5652 /* Did use the ID in a previous block. */
5653 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005654 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005655
Bram Moolenaar51485f02005-06-04 21:55:20 +00005656 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005657 }
5658 else if ((STRCMP(items[0], "PFX") == 0
5659 || STRCMP(items[0], "SFX") == 0)
5660 && aff_todo > 0
5661 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005662 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005663 {
5664 affentry_T *aff_entry;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005665 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005666 int lasti = 5;
5667
Bram Moolenaar8db73182005-06-17 21:51:16 +00005668 /* Myspell allows extra text after the item, but that might
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005669 * mean mistakes go unnoticed. Require a comment-starter.
5670 * Hunspell uses a "-" item. */
5671 if (itemcnt > lasti && *items[lasti] != '#'
5672 && (STRCMP(items[lasti], "-") != 0
5673 || itemcnt != lasti + 1))
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005674 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005675
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005676 /* New item for an affix letter. */
5677 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005678 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005679 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005680 if (aff_entry == NULL)
5681 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005682
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005683 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005684 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005685 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005686 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005687 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005688
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005689 /* Recognize flags on the affix: abcd/XYZ */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005690 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
5691 if (aff_entry->ae_flags != NULL)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005692 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005693 *aff_entry->ae_flags++ = NUL;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005694 aff_process_flags(aff, aff_entry);
5695 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005696 }
5697
Bram Moolenaar51485f02005-06-04 21:55:20 +00005698 /* Don't use an affix entry with non-ASCII characters when
5699 * "spin->si_ascii" is TRUE. */
5700 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005701 || has_non_ascii(aff_entry->ae_add)))
5702 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00005703 aff_entry->ae_next = cur_aff->ah_first;
5704 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005705
5706 if (STRCMP(items[4], ".") != 0)
5707 {
5708 char_u buf[MAXLINELEN];
5709
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005710 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005711 if (*items[0] == 'P')
5712 sprintf((char *)buf, "^%s", items[4]);
5713 else
5714 sprintf((char *)buf, "%s$", items[4]);
5715 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005716 RE_MAGIC + RE_STRING + RE_STRICT);
5717 if (aff_entry->ae_prog == NULL)
5718 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5719 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005720 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005721
5722 /* For postponed prefixes we need an entry in si_prefcond
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005723 * for the condition. Use an existing one if possible.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005724 * Can't be done for an affix with flags, ignoring
5725 * COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005726 if (*items[0] == 'P' && aff->af_pfxpostpone
5727 && aff_entry->ae_flags == NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005728 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005729 /* When the chop string is one lower-case letter and
5730 * the add string ends in the upper-case letter we set
5731 * the "upper" flag, clear "ae_chop" and remove the
5732 * letters from "ae_add". The condition must either
5733 * be empty or start with the same letter. */
5734 if (aff_entry->ae_chop != NULL
5735 && aff_entry->ae_add != NULL
5736#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005737 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00005738 aff_entry->ae_chop)] == NUL
5739#else
5740 && aff_entry->ae_chop[1] == NUL
5741#endif
5742 )
5743 {
5744 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005745
Bram Moolenaar53805d12005-08-01 07:08:33 +00005746 c = PTR2CHAR(aff_entry->ae_chop);
5747 c_up = SPELL_TOUPPER(c);
5748 if (c_up != c
5749 && (aff_entry->ae_cond == NULL
5750 || PTR2CHAR(aff_entry->ae_cond) == c))
5751 {
5752 p = aff_entry->ae_add
5753 + STRLEN(aff_entry->ae_add);
5754 mb_ptr_back(aff_entry->ae_add, p);
5755 if (PTR2CHAR(p) == c_up)
5756 {
5757 upper = TRUE;
5758 aff_entry->ae_chop = NULL;
5759 *p = NUL;
5760
5761 /* The condition is matched with the
5762 * actual word, thus must check for the
5763 * upper-case letter. */
5764 if (aff_entry->ae_cond != NULL)
5765 {
5766 char_u buf[MAXLINELEN];
5767#ifdef FEAT_MBYTE
5768 if (has_mbyte)
5769 {
5770 onecap_copy(items[4], buf, TRUE);
5771 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005772 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005773 }
5774 else
5775#endif
5776 *aff_entry->ae_cond = c_up;
5777 if (aff_entry->ae_cond != NULL)
5778 {
5779 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005780 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005781 vim_free(aff_entry->ae_prog);
5782 aff_entry->ae_prog = vim_regcomp(
5783 buf, RE_MAGIC + RE_STRING);
5784 }
5785 }
5786 }
5787 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005788 }
5789
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005790 if (aff_entry->ae_chop == NULL
5791 && aff_entry->ae_flags == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005792 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005793 int idx;
5794 char_u **pp;
5795 int n;
5796
Bram Moolenaar6de68532005-08-24 22:08:48 +00005797 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005798 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5799 --idx)
5800 {
5801 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5802 if (str_equal(p, aff_entry->ae_cond))
5803 break;
5804 }
5805 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5806 {
5807 /* Not found, add a new condition. */
5808 idx = spin->si_prefcond.ga_len++;
5809 pp = ((char_u **)spin->si_prefcond.ga_data)
5810 + idx;
5811 if (aff_entry->ae_cond == NULL)
5812 *pp = NULL;
5813 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005814 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005815 aff_entry->ae_cond);
5816 }
5817
5818 /* Add the prefix to the prefix tree. */
5819 if (aff_entry->ae_add == NULL)
5820 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005821 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005822 p = aff_entry->ae_add;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005823
Bram Moolenaar53805d12005-08-01 07:08:33 +00005824 /* PFX_FLAGS is a negative number, so that
5825 * tree_add_word() knows this is the prefix tree. */
5826 n = PFX_FLAGS;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005827 if (!cur_aff->ah_combine)
5828 n |= WFP_NC;
5829 if (upper)
5830 n |= WFP_UP;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005831 if (aff_entry->ae_comppermit)
5832 n |= WFP_COMPPERMIT;
5833 if (aff_entry->ae_compforbid)
5834 n |= WFP_COMPFORBID;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005835 tree_add_word(spin, p, spin->si_prefroot, n,
5836 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005837 did_postpone_prefix = TRUE;
5838 }
5839
5840 /* Didn't actually use ah_newID, backup si_newprefID. */
5841 if (aff_todo == 0 && !did_postpone_prefix)
5842 {
5843 --spin->si_newprefID;
5844 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005845 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005846 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005847 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005848 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005849 else if (is_aff_rule(items, itemcnt, "FOL", 2) && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005850 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005851 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005852 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005853 else if (is_aff_rule(items, itemcnt, "LOW", 2) && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005854 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005855 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005856 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005857 else if (is_aff_rule(items, itemcnt, "UPP", 2) && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005858 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005859 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005860 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005861 else if (is_aff_rule(items, itemcnt, "REP", 2)
5862 || is_aff_rule(items, itemcnt, "REPSAL", 2))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005863 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005864 /* Ignore REP/REPSAL count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005865 if (!isdigit(*items[1]))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005866 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005867 fname, lnum);
5868 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005869 else if ((STRCMP(items[0], "REP") == 0
5870 || STRCMP(items[0], "REPSAL") == 0)
5871 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005872 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005873 /* REP/REPSAL item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005874 /* Myspell ignores extra arguments, we require it starts with
5875 * # to detect mistakes. */
5876 if (itemcnt > 3 && items[3][0] != '#')
5877 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005878 if (items[0][3] == 'S' ? do_repsal : do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005879 {
5880 /* Replace underscore with space (can't include a space
5881 * directly). */
5882 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5883 if (*p == '_')
5884 *p = ' ';
5885 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5886 if (*p == '_')
5887 *p = ' ';
Bram Moolenaar4770d092006-01-12 23:22:24 +00005888 add_fromto(spin, items[0][3] == 'S'
5889 ? &spin->si_repsal
5890 : &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005891 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005892 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005893 else if (is_aff_rule(items, itemcnt, "MAP", 2))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005894 {
5895 /* MAP item or count */
5896 if (!found_map)
5897 {
5898 /* First line contains the count. */
5899 found_map = TRUE;
5900 if (!isdigit(*items[1]))
5901 smsg((char_u *)_("Expected MAP count in %s line %d"),
5902 fname, lnum);
5903 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005904 else if (do_mapline)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005905 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005906 int c;
5907
5908 /* Check that every character appears only once. */
5909 for (p = items[1]; *p != NUL; )
5910 {
5911#ifdef FEAT_MBYTE
5912 c = mb_ptr2char_adv(&p);
5913#else
5914 c = *p++;
5915#endif
5916 if ((spin->si_map.ga_len > 0
5917 && vim_strchr(spin->si_map.ga_data, c)
5918 != NULL)
5919 || vim_strchr(p, c) != NULL)
5920 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5921 fname, lnum);
5922 }
5923
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005924 /* We simply concatenate all the MAP strings, separated by
5925 * slashes. */
5926 ga_concat(&spin->si_map, items[1]);
5927 ga_append(&spin->si_map, '/');
5928 }
5929 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005930 /* Accept "SAL from to" and "SAL from to #comment". */
5931 else if (is_aff_rule(items, itemcnt, "SAL", 3))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005932 {
5933 if (do_sal)
5934 {
5935 /* SAL item (sounds-a-like)
5936 * Either one of the known keys or a from-to pair. */
5937 if (STRCMP(items[1], "followup") == 0)
5938 spin->si_followup = sal_to_bool(items[2]);
5939 else if (STRCMP(items[1], "collapse_result") == 0)
5940 spin->si_collapse = sal_to_bool(items[2]);
5941 else if (STRCMP(items[1], "remove_accents") == 0)
5942 spin->si_rem_accents = sal_to_bool(items[2]);
5943 else
5944 /* when "to" is "_" it means empty */
5945 add_fromto(spin, &spin->si_sal, items[1],
5946 STRCMP(items[2], "_") == 0 ? (char_u *)""
5947 : items[2]);
5948 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005949 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005950 else if (is_aff_rule(items, itemcnt, "SOFOFROM", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005951 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005952 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005953 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005954 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005955 else if (is_aff_rule(items, itemcnt, "SOFOTO", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005956 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005957 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005958 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005959 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005960 else if (STRCMP(items[0], "COMMON") == 0)
5961 {
5962 int i;
5963
5964 for (i = 1; i < itemcnt; ++i)
5965 {
5966 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
5967 items[i])))
5968 {
5969 p = vim_strsave(items[i]);
5970 if (p == NULL)
5971 break;
5972 hash_add(&spin->si_commonwords, p);
5973 }
5974 }
5975 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00005976 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005977 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005978 fname, lnum, items[0]);
5979 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005980 }
5981
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005982 if (fol != NULL || low != NULL || upp != NULL)
5983 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00005984 if (spin->si_clear_chartab)
5985 {
5986 /* Clear the char type tables, don't want to use any of the
5987 * currently used spell properties. */
5988 init_spell_chartab();
5989 spin->si_clear_chartab = FALSE;
5990 }
5991
Bram Moolenaar3982c542005-06-08 21:56:31 +00005992 /*
5993 * Don't write a word table for an ASCII file, so that we don't check
5994 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005995 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00005996 * mb_get_class(), the list of chars in the file will be incomplete.
5997 */
5998 if (!spin->si_ascii
5999#ifdef FEAT_MBYTE
6000 && !enc_utf8
6001#endif
6002 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006003 {
6004 if (fol == NULL || low == NULL || upp == NULL)
6005 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
6006 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00006007 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006008 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006009
6010 vim_free(fol);
6011 vim_free(low);
6012 vim_free(upp);
6013 }
6014
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006015 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006016 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006017 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006018 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
Bram Moolenaar6de68532005-08-24 22:08:48 +00006019 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006020 }
6021
Bram Moolenaar6de68532005-08-24 22:08:48 +00006022 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006023 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006024 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
6025 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006026 }
6027
Bram Moolenaar6de68532005-08-24 22:08:48 +00006028 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006029 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006030 if (syllable == NULL)
6031 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
6032 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
6033 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006034 }
6035
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006036 if (compoptions != 0)
6037 {
6038 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
6039 spin->si_compoptions |= compoptions;
6040 }
6041
Bram Moolenaar6de68532005-08-24 22:08:48 +00006042 if (compflags != NULL)
6043 process_compflags(spin, aff, compflags);
6044
6045 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006046 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006047 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006048 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006049 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006050 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006051 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006052 else
Bram Moolenaar6949d1d2008-08-25 02:14:05 +00006053 MSG(_("Too many postponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006054 }
6055
Bram Moolenaar6de68532005-08-24 22:08:48 +00006056 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006057 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006058 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
6059 spin->si_syllable = syllable;
6060 }
6061
6062 if (sofofrom != NULL || sofoto != NULL)
6063 {
6064 if (sofofrom == NULL || sofoto == NULL)
6065 smsg((char_u *)_("Missing SOFO%s line in %s"),
6066 sofofrom == NULL ? "FROM" : "TO", fname);
6067 else if (spin->si_sal.ga_len > 0)
6068 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006069 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00006070 {
6071 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
6072 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
6073 spin->si_sofofr = sofofrom;
6074 spin->si_sofoto = sofoto;
6075 }
6076 }
6077
6078 if (midword != NULL)
6079 {
6080 aff_check_string(spin->si_midword, midword, "MIDWORD");
6081 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006082 }
6083
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006084 vim_free(pc);
6085 fclose(fd);
6086 return aff;
6087}
6088
6089/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00006090 * Return TRUE when items[0] equals "rulename", there are "mincount" items or
6091 * a comment is following after item "mincount".
6092 */
6093 static int
6094is_aff_rule(items, itemcnt, rulename, mincount)
6095 char_u **items;
6096 int itemcnt;
6097 char *rulename;
6098 int mincount;
6099{
6100 return (STRCMP(items[0], rulename) == 0
6101 && (itemcnt == mincount
6102 || (itemcnt > mincount && items[mincount][0] == '#')));
6103}
6104
6105/*
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006106 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
6107 * ae_flags to ae_comppermit and ae_compforbid.
6108 */
6109 static void
6110aff_process_flags(affile, entry)
6111 afffile_T *affile;
6112 affentry_T *entry;
6113{
6114 char_u *p;
6115 char_u *prevp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006116 unsigned flag;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006117
6118 if (entry->ae_flags != NULL
6119 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
6120 {
6121 for (p = entry->ae_flags; *p != NUL; )
6122 {
6123 prevp = p;
6124 flag = get_affitem(affile->af_flagtype, &p);
6125 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
6126 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00006127 STRMOVE(prevp, p);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006128 p = prevp;
6129 if (flag == affile->af_comppermit)
6130 entry->ae_comppermit = TRUE;
6131 else
6132 entry->ae_compforbid = TRUE;
6133 }
6134 if (affile->af_flagtype == AFT_NUM && *p == ',')
6135 ++p;
6136 }
6137 if (*entry->ae_flags == NUL)
6138 entry->ae_flags = NULL; /* nothing left */
6139 }
6140}
6141
6142/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006143 * Return TRUE if "s" is the name of an info item in the affix file.
6144 */
6145 static int
6146spell_info_item(s)
6147 char_u *s;
6148{
6149 return STRCMP(s, "NAME") == 0
6150 || STRCMP(s, "HOME") == 0
6151 || STRCMP(s, "VERSION") == 0
6152 || STRCMP(s, "AUTHOR") == 0
6153 || STRCMP(s, "EMAIL") == 0
6154 || STRCMP(s, "COPYRIGHT") == 0;
6155}
6156
6157/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006158 * Turn an affix flag name into a number, according to the FLAG type.
6159 * returns zero for failure.
6160 */
6161 static unsigned
6162affitem2flag(flagtype, item, fname, lnum)
6163 int flagtype;
6164 char_u *item;
6165 char_u *fname;
6166 int lnum;
6167{
6168 unsigned res;
6169 char_u *p = item;
6170
6171 res = get_affitem(flagtype, &p);
6172 if (res == 0)
6173 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006174 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006175 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
6176 fname, lnum, item);
6177 else
6178 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
6179 fname, lnum, item);
6180 }
6181 if (*p != NUL)
6182 {
6183 smsg((char_u *)_(e_affname), fname, lnum, item);
6184 return 0;
6185 }
6186
6187 return res;
6188}
6189
6190/*
6191 * Get one affix name from "*pp" and advance the pointer.
6192 * Returns zero for an error, still advances the pointer then.
6193 */
6194 static unsigned
6195get_affitem(flagtype, pp)
6196 int flagtype;
6197 char_u **pp;
6198{
6199 int res;
6200
Bram Moolenaar95529562005-08-25 21:21:38 +00006201 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006202 {
6203 if (!VIM_ISDIGIT(**pp))
6204 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006205 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006206 return 0;
6207 }
6208 res = getdigits(pp);
6209 }
6210 else
6211 {
6212#ifdef FEAT_MBYTE
6213 res = mb_ptr2char_adv(pp);
6214#else
6215 res = *(*pp)++;
6216#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006217 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00006218 && res >= 'A' && res <= 'Z'))
6219 {
6220 if (**pp == NUL)
6221 return 0;
6222#ifdef FEAT_MBYTE
6223 res = mb_ptr2char_adv(pp) + (res << 16);
6224#else
6225 res = *(*pp)++ + (res << 16);
6226#endif
6227 }
6228 }
6229 return res;
6230}
6231
6232/*
6233 * Process the "compflags" string used in an affix file and append it to
6234 * spin->si_compflags.
6235 * The processing involves changing the affix names to ID numbers, so that
6236 * they fit in one byte.
6237 */
6238 static void
6239process_compflags(spin, aff, compflags)
6240 spellinfo_T *spin;
6241 afffile_T *aff;
6242 char_u *compflags;
6243{
6244 char_u *p;
6245 char_u *prevp;
6246 unsigned flag;
6247 compitem_T *ci;
6248 int id;
6249 int len;
6250 char_u *tp;
6251 char_u key[AH_KEY_LEN];
6252 hashitem_T *hi;
6253
6254 /* Make room for the old and the new compflags, concatenated with a / in
6255 * between. Processing it makes it shorter, but we don't know by how
6256 * much, thus allocate the maximum. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006257 len = (int)STRLEN(compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006258 if (spin->si_compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006259 len += (int)STRLEN(spin->si_compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006260 p = getroom(spin, len, FALSE);
6261 if (p == NULL)
6262 return;
6263 if (spin->si_compflags != NULL)
6264 {
6265 STRCPY(p, spin->si_compflags);
6266 STRCAT(p, "/");
6267 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00006268 spin->si_compflags = p;
6269 tp = p + STRLEN(p);
6270
6271 for (p = compflags; *p != NUL; )
6272 {
6273 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
6274 /* Copy non-flag characters directly. */
6275 *tp++ = *p++;
6276 else
6277 {
6278 /* First get the flag number, also checks validity. */
6279 prevp = p;
6280 flag = get_affitem(aff->af_flagtype, &p);
6281 if (flag != 0)
6282 {
6283 /* Find the flag in the hashtable. If it was used before, use
6284 * the existing ID. Otherwise add a new entry. */
6285 vim_strncpy(key, prevp, p - prevp);
6286 hi = hash_find(&aff->af_comp, key);
6287 if (!HASHITEM_EMPTY(hi))
6288 id = HI2CI(hi)->ci_newID;
6289 else
6290 {
6291 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
6292 if (ci == NULL)
6293 break;
6294 STRCPY(ci->ci_key, key);
6295 ci->ci_flag = flag;
6296 /* Avoid using a flag ID that has a special meaning in a
6297 * regexp (also inside []). */
6298 do
6299 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006300 check_renumber(spin);
6301 id = spin->si_newcompID--;
6302 } while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006303 ci->ci_newID = id;
6304 hash_add(&aff->af_comp, ci->ci_key);
6305 }
6306 *tp++ = id;
6307 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006308 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006309 ++p;
6310 }
6311 }
6312
6313 *tp = NUL;
6314}
6315
6316/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006317 * Check that the new IDs for postponed affixes and compounding don't overrun
6318 * each other. We have almost 255 available, but start at 0-127 to avoid
6319 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
6320 * When that is used up an error message is given.
6321 */
6322 static void
6323check_renumber(spin)
6324 spellinfo_T *spin;
6325{
6326 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
6327 {
6328 spin->si_newprefID = 127;
6329 spin->si_newcompID = 255;
6330 }
6331}
6332
6333/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006334 * Return TRUE if flag "flag" appears in affix list "afflist".
6335 */
6336 static int
6337flag_in_afflist(flagtype, afflist, flag)
6338 int flagtype;
6339 char_u *afflist;
6340 unsigned flag;
6341{
6342 char_u *p;
6343 unsigned n;
6344
6345 switch (flagtype)
6346 {
6347 case AFT_CHAR:
6348 return vim_strchr(afflist, flag) != NULL;
6349
Bram Moolenaar95529562005-08-25 21:21:38 +00006350 case AFT_CAPLONG:
6351 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006352 for (p = afflist; *p != NUL; )
6353 {
6354#ifdef FEAT_MBYTE
6355 n = mb_ptr2char_adv(&p);
6356#else
6357 n = *p++;
6358#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006359 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00006360 && *p != NUL)
6361#ifdef FEAT_MBYTE
6362 n = mb_ptr2char_adv(&p) + (n << 16);
6363#else
6364 n = *p++ + (n << 16);
6365#endif
6366 if (n == flag)
6367 return TRUE;
6368 }
6369 break;
6370
Bram Moolenaar95529562005-08-25 21:21:38 +00006371 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006372 for (p = afflist; *p != NUL; )
6373 {
6374 n = getdigits(&p);
6375 if (n == flag)
6376 return TRUE;
6377 if (*p != NUL) /* skip over comma */
6378 ++p;
6379 }
6380 break;
6381 }
6382 return FALSE;
6383}
6384
6385/*
6386 * Give a warning when "spinval" and "affval" numbers are set and not the same.
6387 */
6388 static void
6389aff_check_number(spinval, affval, name)
6390 int spinval;
6391 int affval;
6392 char *name;
6393{
6394 if (spinval != 0 && spinval != affval)
6395 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6396}
6397
6398/*
6399 * Give a warning when "spinval" and "affval" strings are set and not the same.
6400 */
6401 static void
6402aff_check_string(spinval, affval, name)
6403 char_u *spinval;
6404 char_u *affval;
6405 char *name;
6406{
6407 if (spinval != NULL && STRCMP(spinval, affval) != 0)
6408 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6409}
6410
6411/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006412 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
6413 * NULL as equal.
6414 */
6415 static int
6416str_equal(s1, s2)
6417 char_u *s1;
6418 char_u *s2;
6419{
6420 if (s1 == NULL || s2 == NULL)
6421 return s1 == s2;
6422 return STRCMP(s1, s2) == 0;
6423}
6424
6425/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006426 * Add a from-to item to "gap". Used for REP and SAL items.
6427 * They are stored case-folded.
6428 */
6429 static void
6430add_fromto(spin, gap, from, to)
6431 spellinfo_T *spin;
6432 garray_T *gap;
6433 char_u *from;
6434 char_u *to;
6435{
6436 fromto_T *ftp;
6437 char_u word[MAXWLEN];
6438
6439 if (ga_grow(gap, 1) == OK)
6440 {
6441 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006442 (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006443 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006444 (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006445 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006446 ++gap->ga_len;
6447 }
6448}
6449
6450/*
6451 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6452 */
6453 static int
6454sal_to_bool(s)
6455 char_u *s;
6456{
6457 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
6458}
6459
6460/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00006461 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6462 * When "s" is NULL FALSE is returned.
6463 */
6464 static int
6465has_non_ascii(s)
6466 char_u *s;
6467{
6468 char_u *p;
6469
6470 if (s != NULL)
6471 for (p = s; *p != NUL; ++p)
6472 if (*p >= 128)
6473 return TRUE;
6474 return FALSE;
6475}
6476
6477/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006478 * Free the structure filled by spell_read_aff().
6479 */
6480 static void
6481spell_free_aff(aff)
6482 afffile_T *aff;
6483{
6484 hashtab_T *ht;
6485 hashitem_T *hi;
6486 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006487 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006488 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006489
6490 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006491
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006492 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006493 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
6494 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006495 todo = (int)ht->ht_used;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006496 for (hi = ht->ht_array; todo > 0; ++hi)
6497 {
6498 if (!HASHITEM_EMPTY(hi))
6499 {
6500 --todo;
6501 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006502 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
6503 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006504 }
6505 }
6506 if (ht == &aff->af_suff)
6507 break;
6508 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006509
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006510 hash_clear(&aff->af_pref);
6511 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006512 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006513}
6514
6515/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006516 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006517 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006518 */
6519 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006520spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006521 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006522 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006523 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006524{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006525 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006526 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006527 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006528 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006529 char_u store_afflist[MAXWLEN];
6530 int pfxlen;
6531 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006532 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006533 char_u *pc;
6534 char_u *w;
6535 int l;
6536 hash_T hash;
6537 hashitem_T *hi;
6538 FILE *fd;
6539 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006540 int non_ascii = 0;
6541 int retval = OK;
6542 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006543 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006544 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006545
Bram Moolenaar51485f02005-06-04 21:55:20 +00006546 /*
6547 * Open the file.
6548 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006549 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006550 if (fd == NULL)
6551 {
6552 EMSG2(_(e_notopen), fname);
6553 return FAIL;
6554 }
6555
Bram Moolenaar51485f02005-06-04 21:55:20 +00006556 /* The hashtable is only used to detect duplicated words. */
6557 hash_init(&ht);
6558
Bram Moolenaar4770d092006-01-12 23:22:24 +00006559 vim_snprintf((char *)IObuff, IOSIZE,
6560 _("Reading dictionary file %s ..."), fname);
6561 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006562
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006563 /* start with a message for the first line */
6564 spin->si_msg_count = 999999;
6565
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006566 /* Read and ignore the first line: word count. */
6567 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006568 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006569 EMSG2(_("E760: No word count in %s"), fname);
6570
6571 /*
6572 * Read all the lines in the file one by one.
6573 * The words are converted to 'encoding' here, before being added to
6574 * the hashtable.
6575 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006576 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006577 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006578 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006579 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00006580 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006581 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006582
Bram Moolenaar51485f02005-06-04 21:55:20 +00006583 /* Remove CR, LF and white space from the end. White space halfway
6584 * the word is kept to allow e.g., "et al.". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006585 l = (int)STRLEN(line);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006586 while (l > 0 && line[l - 1] <= ' ')
6587 --l;
6588 if (l == 0)
6589 continue; /* empty line */
6590 line[l] = NUL;
6591
Bram Moolenaarb765d632005-06-07 21:00:02 +00006592#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006593 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006594 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006595 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006596 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006597 if (pc == NULL)
6598 {
6599 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6600 fname, lnum, line);
6601 continue;
6602 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006603 w = pc;
6604 }
6605 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006606#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006607 {
6608 pc = NULL;
6609 w = line;
6610 }
6611
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006612 /* Truncate the word at the "/", set "afflist" to what follows.
6613 * Replace "\/" by "/" and "\\" by "\". */
6614 afflist = NULL;
6615 for (p = w; *p != NUL; mb_ptr_adv(p))
6616 {
6617 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
Bram Moolenaara7241f52008-06-24 20:39:31 +00006618 STRMOVE(p, p + 1);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006619 else if (*p == '/')
6620 {
6621 *p = NUL;
6622 afflist = p + 1;
6623 break;
6624 }
6625 }
6626
6627 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6628 if (spin->si_ascii && has_non_ascii(w))
6629 {
6630 ++non_ascii;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006631 vim_free(pc);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006632 continue;
6633 }
6634
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006635 /* This takes time, print a message every 10000 words. */
6636 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006637 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006638 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006639 vim_snprintf((char *)message, sizeof(message),
6640 _("line %6d, word %6d - %s"),
6641 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6642 msg_start();
6643 msg_puts_long_attr(message, 0);
6644 msg_clr_eos();
6645 msg_didout = FALSE;
6646 msg_col = 0;
6647 out_flush();
6648 }
6649
Bram Moolenaar51485f02005-06-04 21:55:20 +00006650 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006651 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006652 if (dw == NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006653 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006654 retval = FAIL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006655 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006656 break;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006657 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006658
Bram Moolenaar51485f02005-06-04 21:55:20 +00006659 hash = hash_hash(dw);
6660 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006661 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006662 {
6663 if (p_verbose > 0)
6664 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006665 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006666 else if (duplicate == 0)
6667 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6668 fname, lnum, dw);
6669 ++duplicate;
6670 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006671 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006672 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006673
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006674 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006675 store_afflist[0] = NUL;
6676 pfxlen = 0;
6677 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006678 if (afflist != NULL)
6679 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006680 /* Extract flags from the affix list. */
6681 flags |= get_affix_flags(affile, afflist);
6682
Bram Moolenaar6de68532005-08-24 22:08:48 +00006683 if (affile->af_needaffix != 0 && flag_in_afflist(
6684 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006685 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006686
6687 if (affile->af_pfxpostpone)
6688 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006689 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006690
Bram Moolenaar5195e452005-08-19 20:32:47 +00006691 if (spin->si_compflags != NULL)
6692 /* Need to store the list of compound flags with the word.
6693 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006694 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006695 }
6696
Bram Moolenaar51485f02005-06-04 21:55:20 +00006697 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006698 if (store_word(spin, dw, flags, spin->si_region,
6699 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006700 retval = FAIL;
6701
6702 if (afflist != NULL)
6703 {
6704 /* Find all matching suffixes and add the resulting words.
6705 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006706 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006707 &affile->af_suff, &affile->af_pref,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006708 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006709 retval = FAIL;
6710
6711 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006712 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006713 &affile->af_pref, NULL,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006714 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006715 retval = FAIL;
6716 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006717
6718 vim_free(pc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006719 }
6720
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006721 if (duplicate > 0)
6722 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006723 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006724 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6725 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006726 hash_clear(&ht);
6727
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006728 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006729 return retval;
6730}
6731
6732/*
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006733 * Check for affix flags in "afflist" that are turned into word flags.
6734 * Return WF_ flags.
6735 */
6736 static int
6737get_affix_flags(affile, afflist)
6738 afffile_T *affile;
6739 char_u *afflist;
6740{
6741 int flags = 0;
6742
6743 if (affile->af_keepcase != 0 && flag_in_afflist(
6744 affile->af_flagtype, afflist, affile->af_keepcase))
6745 flags |= WF_KEEPCAP | WF_FIXCAP;
6746 if (affile->af_rare != 0 && flag_in_afflist(
6747 affile->af_flagtype, afflist, affile->af_rare))
6748 flags |= WF_RARE;
6749 if (affile->af_bad != 0 && flag_in_afflist(
6750 affile->af_flagtype, afflist, affile->af_bad))
6751 flags |= WF_BANNED;
6752 if (affile->af_needcomp != 0 && flag_in_afflist(
6753 affile->af_flagtype, afflist, affile->af_needcomp))
6754 flags |= WF_NEEDCOMP;
6755 if (affile->af_comproot != 0 && flag_in_afflist(
6756 affile->af_flagtype, afflist, affile->af_comproot))
6757 flags |= WF_COMPROOT;
6758 if (affile->af_nosuggest != 0 && flag_in_afflist(
6759 affile->af_flagtype, afflist, affile->af_nosuggest))
6760 flags |= WF_NOSUGGEST;
6761 return flags;
6762}
6763
6764/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006765 * Get the list of prefix IDs from the affix list "afflist".
6766 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006767 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6768 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006769 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006770 static int
6771get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006772 afffile_T *affile;
6773 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006774 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006775{
6776 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006777 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006778 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006779 int id;
6780 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006781 hashitem_T *hi;
6782
Bram Moolenaar6de68532005-08-24 22:08:48 +00006783 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006784 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006785 prevp = p;
6786 if (get_affitem(affile->af_flagtype, &p) != 0)
6787 {
6788 /* A flag is a postponed prefix flag if it appears in "af_pref"
6789 * and it's ID is not zero. */
6790 vim_strncpy(key, prevp, p - prevp);
6791 hi = hash_find(&affile->af_pref, key);
6792 if (!HASHITEM_EMPTY(hi))
6793 {
6794 id = HI2AH(hi)->ah_newID;
6795 if (id != 0)
6796 store_afflist[cnt++] = id;
6797 }
6798 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006799 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006800 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006801 }
6802
Bram Moolenaar5195e452005-08-19 20:32:47 +00006803 store_afflist[cnt] = NUL;
6804 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006805}
6806
6807/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006808 * Get the list of compound IDs from the affix list "afflist" that are used
6809 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006810 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006811 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006812 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00006813get_compflags(affile, afflist, store_afflist)
6814 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006815 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006816 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006817{
6818 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006819 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006820 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006821 char_u key[AH_KEY_LEN];
6822 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006823
Bram Moolenaar6de68532005-08-24 22:08:48 +00006824 for (p = afflist; *p != NUL; )
6825 {
6826 prevp = p;
6827 if (get_affitem(affile->af_flagtype, &p) != 0)
6828 {
6829 /* A flag is a compound flag if it appears in "af_comp". */
6830 vim_strncpy(key, prevp, p - prevp);
6831 hi = hash_find(&affile->af_comp, key);
6832 if (!HASHITEM_EMPTY(hi))
6833 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6834 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006835 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006836 ++p;
6837 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006838
Bram Moolenaar5195e452005-08-19 20:32:47 +00006839 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006840}
6841
6842/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006843 * Apply affixes to a word and store the resulting words.
6844 * "ht" is the hashtable with affentry_T that need to be applied, either
6845 * prefixes or suffixes.
6846 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6847 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006848 *
6849 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006850 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006851 static int
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006852store_aff_word(spin, word, afflist, affile, ht, xht, condit, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006853 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006854 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006855 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006856 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006857 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006858 hashtab_T *ht;
6859 hashtab_T *xht;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006860 int condit; /* CONDIT_SUF et al. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006861 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006862 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006863 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
6864 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006865{
6866 int todo;
6867 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006868 affheader_T *ah;
6869 affentry_T *ae;
6870 regmatch_T regmatch;
6871 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006872 int retval = OK;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006873 int i, j;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006874 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006875 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006876 char_u *use_pfxlist;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006877 int use_pfxlen;
6878 int need_affix;
6879 char_u store_afflist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006880 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006881 size_t wordlen = STRLEN(word);
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006882 int use_condit;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006883
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006884 todo = (int)ht->ht_used;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006885 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006886 {
6887 if (!HASHITEM_EMPTY(hi))
6888 {
6889 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006890 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006891
Bram Moolenaar51485f02005-06-04 21:55:20 +00006892 /* Check that the affix combines, if required, and that the word
6893 * supports this affix. */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006894 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
6895 && flag_in_afflist(affile->af_flagtype, afflist,
6896 ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006897 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006898 /* Loop over all affix entries with this name. */
6899 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006900 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006901 /* Check the condition. It's not logical to match case
6902 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006903 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006904 * Another requirement from Myspell is that the chop
6905 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006906 * For prefixes, when "PFXPOSTPONE" was used, only do
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006907 * prefixes with a chop string and/or flags.
6908 * When a previously added affix had CIRCUMFIX this one
6909 * must have it too, if it had not then this one must not
6910 * have one either. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006911 regmatch.regprog = ae->ae_prog;
6912 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006913 if ((xht != NULL || !affile->af_pfxpostpone
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006914 || ae->ae_chop != NULL
6915 || ae->ae_flags != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006916 && (ae->ae_chop == NULL
6917 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006918 && (ae->ae_prog == NULL
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006919 || vim_regexec(&regmatch, word, (colnr_T)0))
6920 && (((condit & CONDIT_CFIX) == 0)
6921 == ((condit & CONDIT_AFF) == 0
6922 || ae->ae_flags == NULL
6923 || !flag_in_afflist(affile->af_flagtype,
6924 ae->ae_flags, affile->af_circumfix))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006925 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006926 /* Match. Remove the chop and add the affix. */
6927 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006928 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006929 /* prefix: chop/add at the start of the word */
6930 if (ae->ae_add == NULL)
6931 *newword = NUL;
6932 else
6933 STRCPY(newword, ae->ae_add);
6934 p = word;
6935 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006936 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006937 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006938#ifdef FEAT_MBYTE
6939 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006940 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006941 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006942 for ( ; i > 0; --i)
6943 mb_ptr_adv(p);
6944 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006945 else
6946#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006947 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006948 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006949 STRCAT(newword, p);
6950 }
6951 else
6952 {
6953 /* suffix: chop/add at the end of the word */
6954 STRCPY(newword, word);
6955 if (ae->ae_chop != NULL)
6956 {
6957 /* Remove chop string. */
6958 p = newword + STRLEN(newword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006959 i = (int)MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006960 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006961 mb_ptr_back(newword, p);
6962 *p = NUL;
6963 }
6964 if (ae->ae_add != NULL)
6965 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006966 }
6967
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006968 use_flags = flags;
6969 use_pfxlist = pfxlist;
6970 use_pfxlen = pfxlen;
6971 need_affix = FALSE;
6972 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
6973 if (ae->ae_flags != NULL)
6974 {
6975 /* Extract flags from the affix list. */
6976 use_flags |= get_affix_flags(affile, ae->ae_flags);
6977
6978 if (affile->af_needaffix != 0 && flag_in_afflist(
6979 affile->af_flagtype, ae->ae_flags,
6980 affile->af_needaffix))
6981 need_affix = TRUE;
6982
6983 /* When there is a CIRCUMFIX flag the other affix
6984 * must also have it and we don't add the word
6985 * with one affix. */
6986 if (affile->af_circumfix != 0 && flag_in_afflist(
6987 affile->af_flagtype, ae->ae_flags,
6988 affile->af_circumfix))
6989 {
6990 use_condit |= CONDIT_CFIX;
6991 if ((condit & CONDIT_CFIX) == 0)
6992 need_affix = TRUE;
6993 }
6994
6995 if (affile->af_pfxpostpone
6996 || spin->si_compflags != NULL)
6997 {
6998 if (affile->af_pfxpostpone)
6999 /* Get prefix IDS from the affix list. */
7000 use_pfxlen = get_pfxlist(affile,
7001 ae->ae_flags, store_afflist);
7002 else
7003 use_pfxlen = 0;
7004 use_pfxlist = store_afflist;
7005
7006 /* Combine the prefix IDs. Avoid adding the
7007 * same ID twice. */
7008 for (i = 0; i < pfxlen; ++i)
7009 {
7010 for (j = 0; j < use_pfxlen; ++j)
7011 if (pfxlist[i] == use_pfxlist[j])
7012 break;
7013 if (j == use_pfxlen)
7014 use_pfxlist[use_pfxlen++] = pfxlist[i];
7015 }
7016
7017 if (spin->si_compflags != NULL)
7018 /* Get compound IDS from the affix list. */
7019 get_compflags(affile, ae->ae_flags,
7020 use_pfxlist + use_pfxlen);
7021
7022 /* Combine the list of compound flags.
7023 * Concatenate them to the prefix IDs list.
7024 * Avoid adding the same ID twice. */
7025 for (i = pfxlen; pfxlist[i] != NUL; ++i)
7026 {
7027 for (j = use_pfxlen;
7028 use_pfxlist[j] != NUL; ++j)
7029 if (pfxlist[i] == use_pfxlist[j])
7030 break;
7031 if (use_pfxlist[j] == NUL)
7032 {
7033 use_pfxlist[j++] = pfxlist[i];
7034 use_pfxlist[j] = NUL;
7035 }
7036 }
7037 }
7038 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007039
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007040 /* Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007041 * use the compound flags. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007042 if (use_pfxlist != NULL && ae->ae_compforbid)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007043 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007044 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007045 use_pfxlist = pfx_pfxlist;
7046 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007047
7048 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00007049 if (spin->si_prefroot != NULL
7050 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007051 {
7052 /* ... add a flag to indicate an affix was used. */
7053 use_flags |= WF_HAS_AFF;
7054
7055 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00007056 * affixes is not allowed. But do use the
7057 * compound flags after them. */
Bram Moolenaar18144c82006-04-12 21:52:12 +00007058 if (!ah->ah_combine && use_pfxlist != NULL)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007059 use_pfxlist += use_pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007060 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007061
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007062 /* When compounding is supported and there is no
7063 * "COMPOUNDPERMITFLAG" then forbid compounding on the
7064 * side where the affix is applied. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007065 if (spin->si_compflags != NULL && !ae->ae_comppermit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007066 {
7067 if (xht != NULL)
7068 use_flags |= WF_NOCOMPAFT;
7069 else
7070 use_flags |= WF_NOCOMPBEF;
7071 }
7072
Bram Moolenaar51485f02005-06-04 21:55:20 +00007073 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007074 if (store_word(spin, newword, use_flags,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007075 spin->si_region, use_pfxlist,
7076 need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007077 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007078
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007079 /* When added a prefix or a first suffix and the affix
7080 * has flags may add a(nother) suffix. RECURSIVE! */
7081 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
7082 if (store_aff_word(spin, newword, ae->ae_flags,
7083 affile, &affile->af_suff, xht,
7084 use_condit & (xht == NULL
7085 ? ~0 : ~CONDIT_SUF),
Bram Moolenaar5195e452005-08-19 20:32:47 +00007086 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007087 retval = FAIL;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007088
7089 /* When added a suffix and combining is allowed also
7090 * try adding a prefix additionally. Both for the
7091 * word flags and for the affix flags. RECURSIVE! */
7092 if (xht != NULL && ah->ah_combine)
7093 {
7094 if (store_aff_word(spin, newword,
7095 afflist, affile,
7096 xht, NULL, use_condit,
7097 use_flags, use_pfxlist,
7098 pfxlen) == FAIL
7099 || (ae->ae_flags != NULL
7100 && store_aff_word(spin, newword,
7101 ae->ae_flags, affile,
7102 xht, NULL, use_condit,
7103 use_flags, use_pfxlist,
7104 pfxlen) == FAIL))
7105 retval = FAIL;
7106 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007107 }
7108 }
7109 }
7110 }
7111 }
7112
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007113 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007114}
7115
7116/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007117 * Read a file with a list of words.
7118 */
7119 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007120spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007121 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007122 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007123{
7124 FILE *fd;
7125 long lnum = 0;
7126 char_u rline[MAXLINELEN];
7127 char_u *line;
7128 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007129 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007130 int l;
7131 int retval = OK;
7132 int did_word = FALSE;
7133 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007134 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007135 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007136
7137 /*
7138 * Open the file.
7139 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007140 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007141 if (fd == NULL)
7142 {
7143 EMSG2(_(e_notopen), fname);
7144 return FAIL;
7145 }
7146
Bram Moolenaar4770d092006-01-12 23:22:24 +00007147 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
7148 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007149
7150 /*
7151 * Read all the lines in the file one by one.
7152 */
7153 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
7154 {
7155 line_breakcheck();
7156 ++lnum;
7157
7158 /* Skip comment lines. */
7159 if (*rline == '#')
7160 continue;
7161
7162 /* Remove CR, LF and white space from the end. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007163 l = (int)STRLEN(rline);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007164 while (l > 0 && rline[l - 1] <= ' ')
7165 --l;
7166 if (l == 0)
7167 continue; /* empty or blank line */
7168 rline[l] = NUL;
7169
Bram Moolenaar9c102382006-05-03 21:26:49 +00007170 /* Convert from "/encoding={encoding}" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007171 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007172#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00007173 if (spin->si_conv.vc_type != CONV_NONE)
7174 {
7175 pc = string_convert(&spin->si_conv, rline, NULL);
7176 if (pc == NULL)
7177 {
7178 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
7179 fname, lnum, rline);
7180 continue;
7181 }
7182 line = pc;
7183 }
7184 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00007185#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007186 {
7187 pc = NULL;
7188 line = rline;
7189 }
7190
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007191 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00007192 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007193 ++line;
7194 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007195 {
7196 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007197 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
7198 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007199 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007200 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
7201 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007202 else
7203 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007204#ifdef FEAT_MBYTE
7205 char_u *enc;
7206
Bram Moolenaar51485f02005-06-04 21:55:20 +00007207 /* Setup for conversion to 'encoding'. */
Bram Moolenaar9c102382006-05-03 21:26:49 +00007208 line += 9;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007209 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007210 if (enc != NULL && !spin->si_ascii
7211 && convert_setup(&spin->si_conv, enc,
7212 p_enc) == FAIL)
7213 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00007214 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007215 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007216 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007217#else
7218 smsg((char_u *)_("Conversion in %s not supported"), fname);
7219#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007220 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007221 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007222 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007223
Bram Moolenaar3982c542005-06-08 21:56:31 +00007224 if (STRNCMP(line, "regions=", 8) == 0)
7225 {
7226 if (spin->si_region_count > 1)
7227 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
7228 fname, lnum, line);
7229 else
7230 {
7231 line += 8;
7232 if (STRLEN(line) > 16)
7233 smsg((char_u *)_("Too many regions in %s line %d: %s"),
7234 fname, lnum, line);
7235 else
7236 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007237 spin->si_region_count = (int)STRLEN(line) / 2;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007238 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007239
7240 /* Adjust the mask for a word valid in all regions. */
7241 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007242 }
7243 }
7244 continue;
7245 }
7246
Bram Moolenaar7887d882005-07-01 22:33:52 +00007247 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
7248 fname, lnum, line - 1);
7249 continue;
7250 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007251
Bram Moolenaar7887d882005-07-01 22:33:52 +00007252 flags = 0;
7253 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007254
Bram Moolenaar7887d882005-07-01 22:33:52 +00007255 /* Check for flags and region after a slash. */
7256 p = vim_strchr(line, '/');
7257 if (p != NULL)
7258 {
7259 *p++ = NUL;
7260 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007261 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007262 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007263 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007264 else if (*p == '!') /* Bad, bad, wicked word. */
7265 flags |= WF_BANNED;
7266 else if (*p == '?') /* Rare word. */
7267 flags |= WF_RARE;
7268 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007269 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007270 if ((flags & WF_REGION) == 0) /* first one */
7271 regionmask = 0;
7272 flags |= WF_REGION;
7273
7274 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00007275 if (l > spin->si_region_count)
7276 {
7277 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00007278 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007279 break;
7280 }
7281 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007282 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007283 else
7284 {
7285 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
7286 fname, lnum, p);
7287 break;
7288 }
7289 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007290 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007291 }
7292
7293 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
7294 if (spin->si_ascii && has_non_ascii(line))
7295 {
7296 ++non_ascii;
7297 continue;
7298 }
7299
7300 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007301 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007302 {
7303 retval = FAIL;
7304 break;
7305 }
7306 did_word = TRUE;
7307 }
7308
7309 vim_free(pc);
7310 fclose(fd);
7311
Bram Moolenaar4770d092006-01-12 23:22:24 +00007312 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007313 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007314 vim_snprintf((char *)IObuff, IOSIZE,
7315 _("Ignored %d words with non-ASCII characters"), non_ascii);
7316 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007317 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007318
Bram Moolenaar51485f02005-06-04 21:55:20 +00007319 return retval;
7320}
7321
7322/*
7323 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007324 * This avoids calling free() for every little struct we use (and keeping
7325 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00007326 * The memory is cleared to all zeros.
7327 * Returns NULL when out of memory.
7328 */
7329 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007330getroom(spin, len, align)
7331 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007332 size_t len; /* length needed */
7333 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007334{
7335 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007336 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007337
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007338 if (align && bl != NULL)
7339 /* Round size up for alignment. On some systems structures need to be
7340 * aligned to the size of a pointer (e.g., SPARC). */
7341 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
7342 & ~(sizeof(char *) - 1);
7343
Bram Moolenaar51485f02005-06-04 21:55:20 +00007344 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
7345 {
7346 /* Allocate a block of memory. This is not freed until much later. */
7347 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
7348 if (bl == NULL)
7349 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007350 bl->sb_next = spin->si_blocks;
7351 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007352 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007353 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007354 }
7355
7356 p = bl->sb_data + bl->sb_used;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007357 bl->sb_used += (int)len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007358
7359 return p;
7360}
7361
7362/*
7363 * Make a copy of a string into memory allocated with getroom().
7364 */
7365 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007366getroom_save(spin, s)
7367 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007368 char_u *s;
7369{
7370 char_u *sc;
7371
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007372 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007373 if (sc != NULL)
7374 STRCPY(sc, s);
7375 return sc;
7376}
7377
7378
7379/*
7380 * Free the list of allocated sblock_T.
7381 */
7382 static void
7383free_blocks(bl)
7384 sblock_T *bl;
7385{
7386 sblock_T *next;
7387
7388 while (bl != NULL)
7389 {
7390 next = bl->sb_next;
7391 vim_free(bl);
7392 bl = next;
7393 }
7394}
7395
7396/*
7397 * Allocate the root of a word tree.
7398 */
7399 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007400wordtree_alloc(spin)
7401 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007402{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007403 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007404}
7405
7406/*
7407 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007408 * Always store it in the case-folded tree. For a keep-case word this is
7409 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
7410 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007411 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007412 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
7413 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007414 */
7415 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00007416store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007417 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007418 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007419 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007420 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007421 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007422 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007423{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007424 int len = (int)STRLEN(word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007425 int ct = captype(word, word + len);
7426 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007427 int res = OK;
7428 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007429
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007430 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007431 for (p = pfxlist; res == OK; ++p)
7432 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007433 if (!need_affix || (p != NULL && *p != NUL))
7434 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007435 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007436 if (p == NULL || *p == NUL)
7437 break;
7438 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007439 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007440
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007441 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00007442 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007443 for (p = pfxlist; res == OK; ++p)
7444 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007445 if (!need_affix || (p != NULL && *p != NUL))
7446 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007447 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007448 if (p == NULL || *p == NUL)
7449 break;
7450 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007451 ++spin->si_keepwcount;
7452 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007453 return res;
7454}
7455
7456/*
7457 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00007458 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007459 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007460 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007461 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007462 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007463tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007464 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007465 char_u *word;
7466 wordnode_T *root;
7467 int flags;
7468 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007469 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007470{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007471 wordnode_T *node = root;
7472 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007473 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007474 wordnode_T **prev = NULL;
7475 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007476
Bram Moolenaar51485f02005-06-04 21:55:20 +00007477 /* Add each byte of the word to the tree, including the NUL at the end. */
7478 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007479 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007480 /* When there is more than one reference to this node we need to make
7481 * a copy, so that we can modify it. Copy the whole list of siblings
7482 * (we don't optimize for a partly shared list of siblings). */
7483 if (node != NULL && node->wn_refs > 1)
7484 {
7485 --node->wn_refs;
7486 copyprev = prev;
7487 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
7488 {
7489 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007490 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007491 if (np == NULL)
7492 return FAIL;
7493 np->wn_child = copyp->wn_child;
7494 if (np->wn_child != NULL)
7495 ++np->wn_child->wn_refs; /* child gets extra ref */
7496 np->wn_byte = copyp->wn_byte;
7497 if (np->wn_byte == NUL)
7498 {
7499 np->wn_flags = copyp->wn_flags;
7500 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007501 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007502 }
7503
7504 /* Link the new node in the list, there will be one ref. */
7505 np->wn_refs = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007506 if (copyprev != NULL)
7507 *copyprev = np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007508 copyprev = &np->wn_sibling;
7509
7510 /* Let "node" point to the head of the copied list. */
7511 if (copyp == node)
7512 node = np;
7513 }
7514 }
7515
Bram Moolenaar51485f02005-06-04 21:55:20 +00007516 /* Look for the sibling that has the same character. They are sorted
7517 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007518 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007519 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007520 while (node != NULL
7521 && (node->wn_byte < word[i]
7522 || (node->wn_byte == NUL
7523 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007524 ? node->wn_affixID < (unsigned)affixID
7525 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007526 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007527 && (spin->si_sugtree
7528 ? (node->wn_region & 0xffff) < region
7529 : node->wn_affixID
7530 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007531 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007532 prev = &node->wn_sibling;
7533 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007534 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007535 if (node == NULL
7536 || node->wn_byte != word[i]
7537 || (word[i] == NUL
7538 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007539 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007540 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007541 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007542 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007543 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007544 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007545 if (np == NULL)
7546 return FAIL;
7547 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007548
7549 /* If "node" is NULL this is a new child or the end of the sibling
7550 * list: ref count is one. Otherwise use ref count of sibling and
7551 * make ref count of sibling one (matters when inserting in front
7552 * of the list of siblings). */
7553 if (node == NULL)
7554 np->wn_refs = 1;
7555 else
7556 {
7557 np->wn_refs = node->wn_refs;
7558 node->wn_refs = 1;
7559 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007560 *prev = np;
7561 np->wn_sibling = node;
7562 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007563 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007564
Bram Moolenaar51485f02005-06-04 21:55:20 +00007565 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007566 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007567 node->wn_flags = flags;
7568 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007569 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007570 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00007571 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007572 prev = &node->wn_child;
7573 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007574 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007575#ifdef SPELL_PRINTTREE
7576 smsg("Added \"%s\"", word);
7577 spell_print_tree(root->wn_sibling);
7578#endif
7579
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007580 /* count nr of words added since last message */
7581 ++spin->si_msg_count;
7582
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007583 if (spin->si_compress_cnt > 1)
7584 {
7585 if (--spin->si_compress_cnt == 1)
7586 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007587 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007588 }
7589
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007590 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007591 * When we have allocated lots of memory we need to compress the word tree
7592 * to free up some room. But compression is slow, and we might actually
7593 * need that room, thus only compress in the following situations:
7594 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00007595 * "compress_start" blocks.
7596 * 2. When compressed before and used "compress_inc" blocks before
7597 * adding "compress_added" words (si_compress_cnt > 1).
7598 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007599 * (si_compress_cnt == 1) and the number of free nodes drops below the
7600 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007601 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007602#ifndef SPELL_PRINTTREE
7603 if (spin->si_compress_cnt == 1
7604 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00007605 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007606#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007607 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007608 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00007609 * when the freed up room has been used and another "compress_inc"
7610 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007611 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007612 spin->si_blocks_cnt -= compress_inc;
7613 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007614
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007615 if (spin->si_verbose)
7616 {
7617 msg_start();
7618 msg_puts((char_u *)_(msg_compressing));
7619 msg_clr_eos();
7620 msg_didout = FALSE;
7621 msg_col = 0;
7622 out_flush();
7623 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007624
7625 /* Compress both trees. Either they both have many nodes, which makes
7626 * compression useful, or one of them is small, which means
Bram Moolenaar4770d092006-01-12 23:22:24 +00007627 * compression goes fast. But when filling the souldfold word tree
7628 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007629 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007630 if (affixID >= 0)
7631 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007632 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007633
7634 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007635}
7636
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007637/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007638 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7639 * Sets "sps_flags".
7640 */
7641 int
7642spell_check_msm()
7643{
7644 char_u *p = p_msm;
7645 long start = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007646 long incr = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007647 long added = 0;
7648
7649 if (!VIM_ISDIGIT(*p))
7650 return FAIL;
7651 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7652 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7653 if (*p != ',')
7654 return FAIL;
7655 ++p;
7656 if (!VIM_ISDIGIT(*p))
7657 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007658 incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007659 if (*p != ',')
7660 return FAIL;
7661 ++p;
7662 if (!VIM_ISDIGIT(*p))
7663 return FAIL;
7664 added = getdigits(&p) * 1024;
7665 if (*p != NUL)
7666 return FAIL;
7667
Bram Moolenaar89d40322006-08-29 15:30:07 +00007668 if (start == 0 || incr == 0 || added == 0 || incr > start)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007669 return FAIL;
7670
7671 compress_start = start;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007672 compress_inc = incr;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007673 compress_added = added;
7674 return OK;
7675}
7676
7677
7678/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007679 * Get a wordnode_T, either from the list of previously freed nodes or
7680 * allocate a new one.
7681 */
7682 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007683get_wordnode(spin)
7684 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007685{
7686 wordnode_T *n;
7687
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007688 if (spin->si_first_free == NULL)
7689 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007690 else
7691 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007692 n = spin->si_first_free;
7693 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007694 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007695 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007696 }
7697#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007698 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007699#endif
7700 return n;
7701}
7702
7703/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007704 * Decrement the reference count on a node (which is the head of a list of
7705 * siblings). If the reference count becomes zero free the node and its
7706 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007707 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007708 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007709 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007710deref_wordnode(spin, node)
7711 spellinfo_T *spin;
7712 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007713{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007714 wordnode_T *np;
7715 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007716
7717 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007718 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007719 for (np = node; np != NULL; np = np->wn_sibling)
7720 {
7721 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007722 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007723 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007724 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007725 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007726 ++cnt; /* length field */
7727 }
7728 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007729}
7730
7731/*
7732 * Free a wordnode_T for re-use later.
7733 * Only the "wn_child" field becomes invalid.
7734 */
7735 static void
7736free_wordnode(spin, n)
7737 spellinfo_T *spin;
7738 wordnode_T *n;
7739{
7740 n->wn_child = spin->si_first_free;
7741 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007742 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007743}
7744
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007745/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007746 * Compress a tree: find tails that are identical and can be shared.
7747 */
7748 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007749wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007750 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007751 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007752{
7753 hashtab_T ht;
7754 int n;
7755 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007756 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007757
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007758 /* Skip the root itself, it's not actually used. The first sibling is the
7759 * start of the tree. */
7760 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007761 {
7762 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007763 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007764
7765#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007766 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007767#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007768 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007769 if (tot > 1000000)
7770 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007771 else if (tot == 0)
7772 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007773 else
7774 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007775 vim_snprintf((char *)IObuff, IOSIZE,
7776 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7777 n, tot, tot - n, perc);
7778 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007779 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007780#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007781 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007782#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007783 hash_clear(&ht);
7784 }
7785}
7786
7787/*
7788 * Compress a node, its siblings and its children, depth first.
7789 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007790 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007791 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007792node_compress(spin, node, ht, tot)
7793 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007794 wordnode_T *node;
7795 hashtab_T *ht;
7796 int *tot; /* total count of nodes before compressing,
7797 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007798{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007799 wordnode_T *np;
7800 wordnode_T *tp;
7801 wordnode_T *child;
7802 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007803 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007804 int len = 0;
7805 unsigned nr, n;
7806 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007807
Bram Moolenaar51485f02005-06-04 21:55:20 +00007808 /*
7809 * Go through the list of siblings. Compress each child and then try
7810 * finding an identical child to replace it.
7811 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007812 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007813 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007814 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007815 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007816 ++len;
7817 if ((child = np->wn_child) != NULL)
7818 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007819 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007820 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007821
7822 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007823 hash = hash_hash(child->wn_u1.hashkey);
7824 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007825 if (!HASHITEM_EMPTY(hi))
7826 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007827 /* There are children we encountered before with a hash value
7828 * identical to the current child. Now check if there is one
7829 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007830 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007831 if (node_equal(child, tp))
7832 {
7833 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007834 * current one. This means the current child and all
7835 * its siblings is unlinked from the tree. */
7836 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007837 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007838 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007839 break;
7840 }
7841 if (tp == NULL)
7842 {
7843 /* No other child with this hash value equals the child of
7844 * the node, add it to the linked list after the first
7845 * item. */
7846 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007847 child->wn_u2.next = tp->wn_u2.next;
7848 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007849 }
7850 }
7851 else
7852 /* No other child has this hash value, add it to the
7853 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007854 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007855 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007856 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007857 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007858
7859 /*
7860 * Make a hash key for the node and its siblings, so that we can quickly
7861 * find a lookalike node. This must be done after compressing the sibling
7862 * list, otherwise the hash key would become invalid by the compression.
7863 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007864 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007865 nr = 0;
7866 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007867 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007868 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007869 /* end node: use wn_flags, wn_region and wn_affixID */
7870 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007871 else
7872 /* byte node: use the byte value and the child pointer */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007873 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
Bram Moolenaar51485f02005-06-04 21:55:20 +00007874 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007875 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007876
7877 /* Avoid NUL bytes, it terminates the hash key. */
7878 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007879 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007880 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007881 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007882 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007883 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007884 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007885 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7886 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007887
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007888 /* Check for CTRL-C pressed now and then. */
7889 fast_breakcheck();
7890
Bram Moolenaar51485f02005-06-04 21:55:20 +00007891 return compressed;
7892}
7893
7894/*
7895 * Return TRUE when two nodes have identical siblings and children.
7896 */
7897 static int
7898node_equal(n1, n2)
7899 wordnode_T *n1;
7900 wordnode_T *n2;
7901{
7902 wordnode_T *p1;
7903 wordnode_T *p2;
7904
7905 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7906 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7907 if (p1->wn_byte != p2->wn_byte
7908 || (p1->wn_byte == NUL
7909 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007910 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007911 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007912 : (p1->wn_child != p2->wn_child)))
7913 break;
7914
7915 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007916}
7917
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007918static int
7919#ifdef __BORLANDC__
7920_RTLENTRYF
7921#endif
7922rep_compare __ARGS((const void *s1, const void *s2));
7923
7924/*
7925 * Function given to qsort() to sort the REP items on "from" string.
7926 */
7927 static int
7928#ifdef __BORLANDC__
7929_RTLENTRYF
7930#endif
7931rep_compare(s1, s2)
7932 const void *s1;
7933 const void *s2;
7934{
7935 fromto_T *p1 = (fromto_T *)s1;
7936 fromto_T *p2 = (fromto_T *)s2;
7937
7938 return STRCMP(p1->ft_from, p2->ft_from);
7939}
7940
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007941/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007942 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007943 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007944 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007945 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007946write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007947 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007948 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007949{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007950 FILE *fd;
7951 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007952 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007953 wordnode_T *tree;
7954 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007955 int i;
7956 int l;
7957 garray_T *gap;
7958 fromto_T *ftp;
7959 char_u *p;
7960 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007961 int retval = OK;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00007962 size_t fwv = 1; /* collect return value of fwrite() to avoid
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00007963 warnings from picky compiler */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007964
Bram Moolenaarb765d632005-06-07 21:00:02 +00007965 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007966 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007967 {
7968 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007969 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007970 }
7971
Bram Moolenaar5195e452005-08-19 20:32:47 +00007972 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007973 /* <fileID> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00007974 fwv &= fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd);
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00007975 if (fwv != (size_t)1)
7976 /* Catch first write error, don't try writing more. */
7977 goto theend;
7978
Bram Moolenaar5195e452005-08-19 20:32:47 +00007979 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007980
Bram Moolenaar5195e452005-08-19 20:32:47 +00007981 /*
7982 * <SECTIONS>: <section> ... <sectionend>
7983 */
7984
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007985 /* SN_INFO: <infotext> */
7986 if (spin->si_info != NULL)
7987 {
7988 putc(SN_INFO, fd); /* <sectionID> */
7989 putc(0, fd); /* <sectionflags> */
7990
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007991 i = (int)STRLEN(spin->si_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007992 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00007993 fwv &= fwrite(spin->si_info, (size_t)i, (size_t)1, fd); /* <infotext> */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007994 }
7995
Bram Moolenaar5195e452005-08-19 20:32:47 +00007996 /* SN_REGION: <regionname> ...
7997 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007998 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007999 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008000 putc(SN_REGION, fd); /* <sectionID> */
8001 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8002 l = spin->si_region_count * 2;
8003 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008004 fwv &= fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008005 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00008006 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008007 }
8008 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00008009 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008010
Bram Moolenaar5195e452005-08-19 20:32:47 +00008011 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
8012 *
8013 * The table with character flags and the table for case folding.
8014 * This makes sure the same characters are recognized as word characters
8015 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008016 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008017 * 'encoding'.
8018 * Also skip this for an .add.spl file, the main spell file must contain
8019 * the table (avoids that it conflicts). File is shorter too.
8020 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008021 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008022 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008023 char_u folchars[128 * 8];
8024 int flags;
8025
Bram Moolenaard12a1322005-08-21 22:08:24 +00008026 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008027 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8028
8029 /* Form the <folchars> string first, we need to know its length. */
8030 l = 0;
8031 for (i = 128; i < 256; ++i)
8032 {
8033#ifdef FEAT_MBYTE
8034 if (has_mbyte)
8035 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
8036 else
8037#endif
8038 folchars[l++] = spelltab.st_fold[i];
8039 }
8040 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
8041
8042 fputc(128, fd); /* <charflagslen> */
8043 for (i = 128; i < 256; ++i)
8044 {
8045 flags = 0;
8046 if (spelltab.st_isw[i])
8047 flags |= CF_WORD;
8048 if (spelltab.st_isu[i])
8049 flags |= CF_UPPER;
8050 fputc(flags, fd); /* <charflags> */
8051 }
8052
8053 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008054 fwv &= fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008055 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008056
Bram Moolenaar5195e452005-08-19 20:32:47 +00008057 /* SN_MIDWORD: <midword> */
8058 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008059 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008060 putc(SN_MIDWORD, fd); /* <sectionID> */
8061 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8062
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008063 i = (int)STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008064 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008065 fwv &= fwrite(spin->si_midword, (size_t)i, (size_t)1, fd);
8066 /* <midword> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008067 }
8068
Bram Moolenaar5195e452005-08-19 20:32:47 +00008069 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
8070 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008071 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008072 putc(SN_PREFCOND, fd); /* <sectionID> */
8073 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8074
8075 l = write_spell_prefcond(NULL, &spin->si_prefcond);
8076 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8077
8078 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008079 }
8080
Bram Moolenaar5195e452005-08-19 20:32:47 +00008081 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00008082 * SN_SAL: <salflags> <salcount> <sal> ...
8083 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008084
Bram Moolenaar5195e452005-08-19 20:32:47 +00008085 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00008086 * round 2: SN_SAL section (unless SN_SOFO is used)
8087 * round 3: SN_REPSAL section */
8088 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008089 {
8090 if (round == 1)
8091 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008092 else if (round == 2)
8093 {
8094 /* Don't write SN_SAL when using a SN_SOFO section */
8095 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8096 continue;
8097 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008098 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008099 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00008100 gap = &spin->si_repsal;
8101
8102 /* Don't write the section if there are no items. */
8103 if (gap->ga_len == 0)
8104 continue;
8105
8106 /* Sort the REP/REPSAL items. */
8107 if (round != 2)
8108 qsort(gap->ga_data, (size_t)gap->ga_len,
8109 sizeof(fromto_T), rep_compare);
8110
8111 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
8112 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008113
Bram Moolenaar5195e452005-08-19 20:32:47 +00008114 /* This is for making suggestions, section is not required. */
8115 putc(0, fd); /* <sectionflags> */
8116
8117 /* Compute the length of what follows. */
8118 l = 2; /* count <repcount> or <salcount> */
8119 for (i = 0; i < gap->ga_len; ++i)
8120 {
8121 ftp = &((fromto_T *)gap->ga_data)[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008122 l += 1 + (int)STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
8123 l += 1 + (int)STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008124 }
8125 if (round == 2)
8126 ++l; /* count <salflags> */
8127 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8128
8129 if (round == 2)
8130 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008131 i = 0;
8132 if (spin->si_followup)
8133 i |= SAL_F0LLOWUP;
8134 if (spin->si_collapse)
8135 i |= SAL_COLLAPSE;
8136 if (spin->si_rem_accents)
8137 i |= SAL_REM_ACCENTS;
8138 putc(i, fd); /* <salflags> */
8139 }
8140
8141 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
8142 for (i = 0; i < gap->ga_len; ++i)
8143 {
8144 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
8145 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
8146 ftp = &((fromto_T *)gap->ga_data)[i];
8147 for (rr = 1; rr <= 2; ++rr)
8148 {
8149 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008150 l = (int)STRLEN(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008151 putc(l, fd);
Bram Moolenaar9bf13612008-11-29 19:11:40 +00008152 if (l > 0)
8153 fwv &= fwrite(p, l, (size_t)1, fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008154 }
8155 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008156
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008157 }
8158
Bram Moolenaar5195e452005-08-19 20:32:47 +00008159 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
8160 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008161 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8162 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008163 putc(SN_SOFO, fd); /* <sectionID> */
8164 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008165
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008166 l = (int)STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008167 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
8168 /* <sectionlen> */
8169
8170 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008171 fwv &= fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008172
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008173 l = (int)STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008174 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008175 fwv &= fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008176 }
8177
Bram Moolenaar4770d092006-01-12 23:22:24 +00008178 /* SN_WORDS: <word> ...
8179 * This is for making suggestions, section is not required. */
8180 if (spin->si_commonwords.ht_used > 0)
8181 {
8182 putc(SN_WORDS, fd); /* <sectionID> */
8183 putc(0, fd); /* <sectionflags> */
8184
8185 /* round 1: count the bytes
8186 * round 2: write the bytes */
8187 for (round = 1; round <= 2; ++round)
8188 {
8189 int todo;
8190 int len = 0;
8191 hashitem_T *hi;
8192
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008193 todo = (int)spin->si_commonwords.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008194 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
8195 if (!HASHITEM_EMPTY(hi))
8196 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008197 l = (int)STRLEN(hi->hi_key) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008198 len += l;
8199 if (round == 2) /* <word> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008200 fwv &= fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008201 --todo;
8202 }
8203 if (round == 1)
8204 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
8205 }
8206 }
8207
Bram Moolenaar5195e452005-08-19 20:32:47 +00008208 /* SN_MAP: <mapstr>
8209 * This is for making suggestions, section is not required. */
8210 if (spin->si_map.ga_len > 0)
8211 {
8212 putc(SN_MAP, fd); /* <sectionID> */
8213 putc(0, fd); /* <sectionflags> */
8214 l = spin->si_map.ga_len;
8215 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008216 fwv &= fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008217 /* <mapstr> */
8218 }
8219
Bram Moolenaar4770d092006-01-12 23:22:24 +00008220 /* SN_SUGFILE: <timestamp>
8221 * This is used to notify that a .sug file may be available and at the
8222 * same time allows for checking that a .sug file that is found matches
8223 * with this .spl file. That's because the word numbers must be exactly
8224 * right. */
8225 if (!spin->si_nosugfile
8226 && (spin->si_sal.ga_len > 0
8227 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
8228 {
8229 putc(SN_SUGFILE, fd); /* <sectionID> */
8230 putc(0, fd); /* <sectionflags> */
8231 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
8232
8233 /* Set si_sugtime and write it to the file. */
8234 spin->si_sugtime = time(NULL);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02008235 put_time(fd, spin->si_sugtime); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008236 }
8237
Bram Moolenaare1438bb2006-03-01 22:01:55 +00008238 /* SN_NOSPLITSUGS: nothing
8239 * This is used to notify that no suggestions with word splits are to be
8240 * made. */
8241 if (spin->si_nosplitsugs)
8242 {
8243 putc(SN_NOSPLITSUGS, fd); /* <sectionID> */
8244 putc(0, fd); /* <sectionflags> */
8245 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8246 }
8247
Bram Moolenaar5195e452005-08-19 20:32:47 +00008248 /* SN_COMPOUND: compound info.
8249 * We don't mark it required, when not supported all compound words will
8250 * be bad words. */
8251 if (spin->si_compflags != NULL)
8252 {
8253 putc(SN_COMPOUND, fd); /* <sectionID> */
8254 putc(0, fd); /* <sectionflags> */
8255
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008256 l = (int)STRLEN(spin->si_compflags);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008257 for (i = 0; i < spin->si_comppat.ga_len; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008258 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008259 put_bytes(fd, (long_u)(l + 7), 4); /* <sectionlen> */
8260
Bram Moolenaar5195e452005-08-19 20:32:47 +00008261 putc(spin->si_compmax, fd); /* <compmax> */
8262 putc(spin->si_compminlen, fd); /* <compminlen> */
8263 putc(spin->si_compsylmax, fd); /* <compsylmax> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008264 putc(0, fd); /* for Vim 7.0b compatibility */
8265 putc(spin->si_compoptions, fd); /* <compoptions> */
8266 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
8267 /* <comppatcount> */
8268 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8269 {
8270 p = ((char_u **)(spin->si_comppat.ga_data))[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008271 putc((int)STRLEN(p), fd); /* <comppatlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008272 fwv &= fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);
8273 /* <comppattext> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008274 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008275 /* <compflags> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008276 fwv &= fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008277 (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008278 }
8279
Bram Moolenaar78622822005-08-23 21:00:13 +00008280 /* SN_NOBREAK: NOBREAK flag */
8281 if (spin->si_nobreak)
8282 {
8283 putc(SN_NOBREAK, fd); /* <sectionID> */
8284 putc(0, fd); /* <sectionflags> */
8285
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008286 /* It's empty, the presence of the section flags the feature. */
Bram Moolenaar78622822005-08-23 21:00:13 +00008287 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8288 }
8289
Bram Moolenaar5195e452005-08-19 20:32:47 +00008290 /* SN_SYLLABLE: syllable info.
8291 * We don't mark it required, when not supported syllables will not be
8292 * counted. */
8293 if (spin->si_syllable != NULL)
8294 {
8295 putc(SN_SYLLABLE, fd); /* <sectionID> */
8296 putc(0, fd); /* <sectionflags> */
8297
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008298 l = (int)STRLEN(spin->si_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008299 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008300 fwv &= fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd);
8301 /* <syllable> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008302 }
8303
8304 /* end of <SECTIONS> */
8305 putc(SN_END, fd); /* <sectionend> */
8306
Bram Moolenaar50cde822005-06-05 21:54:54 +00008307
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008308 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008309 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008310 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008311 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008312 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008313 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008314 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008315 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008316 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008317 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008318 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008319 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008320
Bram Moolenaar0c405862005-06-22 22:26:26 +00008321 /* Clear the index and wnode fields in the tree. */
8322 clear_node(tree);
8323
Bram Moolenaar51485f02005-06-04 21:55:20 +00008324 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00008325 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00008326 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008327 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008328
Bram Moolenaar51485f02005-06-04 21:55:20 +00008329 /* number of nodes in 4 bytes */
8330 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00008331 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008332
Bram Moolenaar51485f02005-06-04 21:55:20 +00008333 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008334 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008335 }
8336
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008337 /* Write another byte to check for errors (file system full). */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008338 if (putc(0, fd) == EOF)
8339 retval = FAIL;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008340theend:
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008341 if (fclose(fd) == EOF)
8342 retval = FAIL;
8343
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008344 if (fwv != (size_t)1)
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008345 retval = FAIL;
8346 if (retval == FAIL)
8347 EMSG(_(e_write));
8348
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008349 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008350}
8351
8352/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008353 * Clear the index and wnode fields of "node", it siblings and its
8354 * children. This is needed because they are a union with other items to save
8355 * space.
8356 */
8357 static void
8358clear_node(node)
8359 wordnode_T *node;
8360{
8361 wordnode_T *np;
8362
8363 if (node != NULL)
8364 for (np = node; np != NULL; np = np->wn_sibling)
8365 {
8366 np->wn_u1.index = 0;
8367 np->wn_u2.wnode = NULL;
8368
8369 if (np->wn_byte != NUL)
8370 clear_node(np->wn_child);
8371 }
8372}
8373
8374
8375/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008376 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008377 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00008378 * This first writes the list of possible bytes (siblings). Then for each
8379 * byte recursively write the children.
8380 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00008381 * NOTE: The code here must match the code in read_tree_node(), since
8382 * assumptions are made about the indexes (so that we don't have to write them
8383 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00008384 *
8385 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008386 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008387 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00008388put_node(fd, node, idx, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008389 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008390 wordnode_T *node;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008391 int idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008392 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008393 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008394{
Bram Moolenaar89d40322006-08-29 15:30:07 +00008395 int newindex = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008396 int siblingcount = 0;
8397 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008398 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008399
Bram Moolenaar51485f02005-06-04 21:55:20 +00008400 /* If "node" is zero the tree is empty. */
8401 if (node == NULL)
8402 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008403
Bram Moolenaar51485f02005-06-04 21:55:20 +00008404 /* Store the index where this node is written. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008405 node->wn_u1.index = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008406
8407 /* Count the number of siblings. */
8408 for (np = node; np != NULL; np = np->wn_sibling)
8409 ++siblingcount;
8410
8411 /* Write the sibling count. */
8412 if (fd != NULL)
8413 putc(siblingcount, fd); /* <siblingcount> */
8414
8415 /* Write each sibling byte and optionally extra info. */
8416 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008417 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008418 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008419 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008420 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008421 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008422 /* For a NUL byte (end of word) write the flags etc. */
8423 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008424 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008425 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008426 * associated condition nr (stored in wn_region). The
8427 * byte value is misused to store the "rare" and "not
8428 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008429 if (np->wn_flags == (short_u)PFX_FLAGS)
8430 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008431 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00008432 {
8433 putc(BY_FLAGS, fd); /* <byte> */
8434 putc(np->wn_flags, fd); /* <pflags> */
8435 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008436 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008437 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008438 }
8439 else
8440 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008441 /* For word trees we write the flag/region items. */
8442 flags = np->wn_flags;
8443 if (regionmask != 0 && np->wn_region != regionmask)
8444 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008445 if (np->wn_affixID != 0)
8446 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008447 if (flags == 0)
8448 {
8449 /* word without flags or region */
8450 putc(BY_NOFLAGS, fd); /* <byte> */
8451 }
8452 else
8453 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008454 if (np->wn_flags >= 0x100)
8455 {
8456 putc(BY_FLAGS2, fd); /* <byte> */
8457 putc(flags, fd); /* <flags> */
8458 putc((unsigned)flags >> 8, fd); /* <flags2> */
8459 }
8460 else
8461 {
8462 putc(BY_FLAGS, fd); /* <byte> */
8463 putc(flags, fd); /* <flags> */
8464 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008465 if (flags & WF_REGION)
8466 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008467 if (flags & WF_AFX)
8468 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008469 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008470 }
8471 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008472 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008473 else
8474 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008475 if (np->wn_child->wn_u1.index != 0
8476 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008477 {
8478 /* The child is written elsewhere, write the reference. */
8479 if (fd != NULL)
8480 {
8481 putc(BY_INDEX, fd); /* <byte> */
8482 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008483 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008484 }
8485 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008486 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008487 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008488 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008489
Bram Moolenaar51485f02005-06-04 21:55:20 +00008490 if (fd != NULL)
8491 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
8492 {
8493 EMSG(_(e_write));
8494 return 0;
8495 }
8496 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008497 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008498
8499 /* Space used in the array when reading: one for each sibling and one for
8500 * the count. */
8501 newindex += siblingcount + 1;
8502
8503 /* Recursively dump the children of each sibling. */
8504 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008505 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
8506 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008507 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008508
8509 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008510}
8511
8512
8513/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008514 * ":mkspell [-ascii] outfile infile ..."
8515 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008516 */
8517 void
8518ex_mkspell(eap)
8519 exarg_T *eap;
8520{
8521 int fcount;
8522 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008523 char_u *arg = eap->arg;
8524 int ascii = FALSE;
8525
8526 if (STRNCMP(arg, "-ascii", 6) == 0)
8527 {
8528 ascii = TRUE;
8529 arg = skipwhite(arg + 6);
8530 }
8531
8532 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
8533 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
8534 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008535 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008536 FreeWild(fcount, fnames);
8537 }
8538}
8539
8540/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00008541 * Create the .sug file.
8542 * Uses the soundfold info in "spin".
8543 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
8544 */
8545 static void
8546spell_make_sugfile(spin, wfname)
8547 spellinfo_T *spin;
8548 char_u *wfname;
8549{
8550 char_u fname[MAXPATHL];
8551 int len;
8552 slang_T *slang;
8553 int free_slang = FALSE;
8554
8555 /*
8556 * Read back the .spl file that was written. This fills the required
8557 * info for soundfolding. This also uses less memory than the
8558 * pointer-linked version of the trie. And it avoids having two versions
8559 * of the code for the soundfolding stuff.
8560 * It might have been done already by spell_reload_one().
8561 */
8562 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
8563 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
8564 break;
8565 if (slang == NULL)
8566 {
8567 spell_message(spin, (char_u *)_("Reading back spell file..."));
8568 slang = spell_load_file(wfname, NULL, NULL, FALSE);
8569 if (slang == NULL)
8570 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008571 free_slang = TRUE;
8572 }
8573
8574 /*
8575 * Clear the info in "spin" that is used.
8576 */
8577 spin->si_blocks = NULL;
8578 spin->si_blocks_cnt = 0;
8579 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
8580 spin->si_free_count = 0;
8581 spin->si_first_free = NULL;
8582 spin->si_foldwcount = 0;
8583
8584 /*
8585 * Go through the trie of good words, soundfold each word and add it to
8586 * the soundfold trie.
8587 */
8588 spell_message(spin, (char_u *)_("Performing soundfolding..."));
8589 if (sug_filltree(spin, slang) == FAIL)
8590 goto theend;
8591
8592 /*
8593 * Create the table which links each soundfold word with a list of the
8594 * good words it may come from. Creates buffer "spin->si_spellbuf".
8595 * This also removes the wordnr from the NUL byte entries to make
8596 * compression possible.
8597 */
8598 if (sug_maketable(spin) == FAIL)
8599 goto theend;
8600
8601 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8602 (long)spin->si_spellbuf->b_ml.ml_line_count);
8603
8604 /*
8605 * Compress the soundfold trie.
8606 */
8607 spell_message(spin, (char_u *)_(msg_compressing));
8608 wordtree_compress(spin, spin->si_foldroot);
8609
8610 /*
8611 * Write the .sug file.
8612 * Make the file name by changing ".spl" to ".sug".
8613 */
8614 STRCPY(fname, wfname);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008615 len = (int)STRLEN(fname);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008616 fname[len - 2] = 'u';
8617 fname[len - 1] = 'g';
8618 sug_write(spin, fname);
8619
8620theend:
8621 if (free_slang)
8622 slang_free(slang);
8623 free_blocks(spin->si_blocks);
8624 close_spellbuf(spin->si_spellbuf);
8625}
8626
8627/*
8628 * Build the soundfold trie for language "slang".
8629 */
8630 static int
8631sug_filltree(spin, slang)
8632 spellinfo_T *spin;
8633 slang_T *slang;
8634{
8635 char_u *byts;
8636 idx_T *idxs;
8637 int depth;
8638 idx_T arridx[MAXWLEN];
8639 int curi[MAXWLEN];
8640 char_u tword[MAXWLEN];
8641 char_u tsalword[MAXWLEN];
8642 int c;
8643 idx_T n;
8644 unsigned words_done = 0;
8645 int wordcount[MAXWLEN];
8646
8647 /* We use si_foldroot for the souldfolded trie. */
8648 spin->si_foldroot = wordtree_alloc(spin);
8649 if (spin->si_foldroot == NULL)
8650 return FAIL;
8651
8652 /* let tree_add_word() know we're adding to the soundfolded tree */
8653 spin->si_sugtree = TRUE;
8654
8655 /*
8656 * Go through the whole case-folded tree, soundfold each word and put it
8657 * in the trie.
8658 */
8659 byts = slang->sl_fbyts;
8660 idxs = slang->sl_fidxs;
8661
8662 arridx[0] = 0;
8663 curi[0] = 1;
8664 wordcount[0] = 0;
8665
8666 depth = 0;
8667 while (depth >= 0 && !got_int)
8668 {
8669 if (curi[depth] > byts[arridx[depth]])
8670 {
8671 /* Done all bytes at this node, go up one level. */
8672 idxs[arridx[depth]] = wordcount[depth];
8673 if (depth > 0)
8674 wordcount[depth - 1] += wordcount[depth];
8675
8676 --depth;
8677 line_breakcheck();
8678 }
8679 else
8680 {
8681
8682 /* Do one more byte at this node. */
8683 n = arridx[depth] + curi[depth];
8684 ++curi[depth];
8685
8686 c = byts[n];
8687 if (c == 0)
8688 {
8689 /* Sound-fold the word. */
8690 tword[depth] = NUL;
8691 spell_soundfold(slang, tword, TRUE, tsalword);
8692
8693 /* We use the "flags" field for the MSB of the wordnr,
8694 * "region" for the LSB of the wordnr. */
8695 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8696 words_done >> 16, words_done & 0xffff,
8697 0) == FAIL)
8698 return FAIL;
8699
8700 ++words_done;
8701 ++wordcount[depth];
8702
8703 /* Reset the block count each time to avoid compression
8704 * kicking in. */
8705 spin->si_blocks_cnt = 0;
8706
8707 /* Skip over any other NUL bytes (same word with different
8708 * flags). */
8709 while (byts[n + 1] == 0)
8710 {
8711 ++n;
8712 ++curi[depth];
8713 }
8714 }
8715 else
8716 {
8717 /* Normal char, go one level deeper. */
8718 tword[depth++] = c;
8719 arridx[depth] = idxs[n];
8720 curi[depth] = 1;
8721 wordcount[depth] = 0;
8722 }
8723 }
8724 }
8725
8726 smsg((char_u *)_("Total number of words: %d"), words_done);
8727
8728 return OK;
8729}
8730
8731/*
8732 * Make the table that links each word in the soundfold trie to the words it
8733 * can be produced from.
8734 * This is not unlike lines in a file, thus use a memfile to be able to access
8735 * the table efficiently.
8736 * Returns FAIL when out of memory.
8737 */
8738 static int
8739sug_maketable(spin)
8740 spellinfo_T *spin;
8741{
8742 garray_T ga;
8743 int res = OK;
8744
8745 /* Allocate a buffer, open a memline for it and create the swap file
8746 * (uses a temp file, not a .swp file). */
8747 spin->si_spellbuf = open_spellbuf();
8748 if (spin->si_spellbuf == NULL)
8749 return FAIL;
8750
8751 /* Use a buffer to store the line info, avoids allocating many small
8752 * pieces of memory. */
8753 ga_init2(&ga, 1, 100);
8754
8755 /* recursively go through the tree */
8756 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8757 res = FAIL;
8758
8759 ga_clear(&ga);
8760 return res;
8761}
8762
8763/*
8764 * Fill the table for one node and its children.
8765 * Returns the wordnr at the start of the node.
8766 * Returns -1 when out of memory.
8767 */
8768 static int
8769sug_filltable(spin, node, startwordnr, gap)
8770 spellinfo_T *spin;
8771 wordnode_T *node;
8772 int startwordnr;
8773 garray_T *gap; /* place to store line of numbers */
8774{
8775 wordnode_T *p, *np;
8776 int wordnr = startwordnr;
8777 int nr;
8778 int prev_nr;
8779
8780 for (p = node; p != NULL; p = p->wn_sibling)
8781 {
8782 if (p->wn_byte == NUL)
8783 {
8784 gap->ga_len = 0;
8785 prev_nr = 0;
8786 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8787 {
8788 if (ga_grow(gap, 10) == FAIL)
8789 return -1;
8790
8791 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8792 /* Compute the offset from the previous nr and store the
8793 * offset in a way that it takes a minimum number of bytes.
8794 * It's a bit like utf-8, but without the need to mark
8795 * following bytes. */
8796 nr -= prev_nr;
8797 prev_nr += nr;
8798 gap->ga_len += offset2bytes(nr,
8799 (char_u *)gap->ga_data + gap->ga_len);
8800 }
8801
8802 /* add the NUL byte */
8803 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8804
8805 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8806 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8807 return -1;
8808 ++wordnr;
8809
8810 /* Remove extra NUL entries, we no longer need them. We don't
8811 * bother freeing the nodes, the won't be reused anyway. */
8812 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8813 p->wn_sibling = p->wn_sibling->wn_sibling;
8814
8815 /* Clear the flags on the remaining NUL node, so that compression
8816 * works a lot better. */
8817 p->wn_flags = 0;
8818 p->wn_region = 0;
8819 }
8820 else
8821 {
8822 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8823 if (wordnr == -1)
8824 return -1;
8825 }
8826 }
8827 return wordnr;
8828}
8829
8830/*
8831 * Convert an offset into a minimal number of bytes.
8832 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8833 * bytes.
8834 */
8835 static int
8836offset2bytes(nr, buf)
8837 int nr;
8838 char_u *buf;
8839{
8840 int rem;
8841 int b1, b2, b3, b4;
8842
8843 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8844 b1 = nr % 255 + 1;
8845 rem = nr / 255;
8846 b2 = rem % 255 + 1;
8847 rem = rem / 255;
8848 b3 = rem % 255 + 1;
8849 b4 = rem / 255 + 1;
8850
8851 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8852 {
8853 buf[0] = 0xe0 + b4;
8854 buf[1] = b3;
8855 buf[2] = b2;
8856 buf[3] = b1;
8857 return 4;
8858 }
8859 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8860 {
8861 buf[0] = 0xc0 + b3;
8862 buf[1] = b2;
8863 buf[2] = b1;
8864 return 3;
8865 }
8866 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8867 {
8868 buf[0] = 0x80 + b2;
8869 buf[1] = b1;
8870 return 2;
8871 }
8872 /* 1 byte */
8873 buf[0] = b1;
8874 return 1;
8875}
8876
8877/*
8878 * Opposite of offset2bytes().
8879 * "pp" points to the bytes and is advanced over it.
8880 * Returns the offset.
8881 */
8882 static int
8883bytes2offset(pp)
8884 char_u **pp;
8885{
8886 char_u *p = *pp;
8887 int nr;
8888 int c;
8889
8890 c = *p++;
8891 if ((c & 0x80) == 0x00) /* 1 byte */
8892 {
8893 nr = c - 1;
8894 }
8895 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8896 {
8897 nr = (c & 0x3f) - 1;
8898 nr = nr * 255 + (*p++ - 1);
8899 }
8900 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8901 {
8902 nr = (c & 0x1f) - 1;
8903 nr = nr * 255 + (*p++ - 1);
8904 nr = nr * 255 + (*p++ - 1);
8905 }
8906 else /* 4 bytes */
8907 {
8908 nr = (c & 0x0f) - 1;
8909 nr = nr * 255 + (*p++ - 1);
8910 nr = nr * 255 + (*p++ - 1);
8911 nr = nr * 255 + (*p++ - 1);
8912 }
8913
8914 *pp = p;
8915 return nr;
8916}
8917
8918/*
8919 * Write the .sug file in "fname".
8920 */
8921 static void
8922sug_write(spin, fname)
8923 spellinfo_T *spin;
8924 char_u *fname;
8925{
8926 FILE *fd;
8927 wordnode_T *tree;
8928 int nodecount;
8929 int wcount;
8930 char_u *line;
8931 linenr_T lnum;
8932 int len;
8933
8934 /* Create the file. Note that an existing file is silently overwritten! */
8935 fd = mch_fopen((char *)fname, "w");
8936 if (fd == NULL)
8937 {
8938 EMSG2(_(e_notopen), fname);
8939 return;
8940 }
8941
8942 vim_snprintf((char *)IObuff, IOSIZE,
8943 _("Writing suggestion file %s ..."), fname);
8944 spell_message(spin, IObuff);
8945
8946 /*
8947 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8948 */
8949 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
8950 {
8951 EMSG(_(e_write));
8952 goto theend;
8953 }
8954 putc(VIMSUGVERSION, fd); /* <versionnr> */
8955
8956 /* Write si_sugtime to the file. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02008957 put_time(fd, spin->si_sugtime); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008958
8959 /*
8960 * <SUGWORDTREE>
8961 */
8962 spin->si_memtot = 0;
8963 tree = spin->si_foldroot->wn_sibling;
8964
8965 /* Clear the index and wnode fields in the tree. */
8966 clear_node(tree);
8967
8968 /* Count the number of nodes. Needed to be able to allocate the
8969 * memory when reading the nodes. Also fills in index for shared
8970 * nodes. */
8971 nodecount = put_node(NULL, tree, 0, 0, FALSE);
8972
8973 /* number of nodes in 4 bytes */
8974 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
8975 spin->si_memtot += nodecount + nodecount * sizeof(int);
8976
8977 /* Write the nodes. */
8978 (void)put_node(fd, tree, 0, 0, FALSE);
8979
8980 /*
8981 * <SUGTABLE>: <sugwcount> <sugline> ...
8982 */
8983 wcount = spin->si_spellbuf->b_ml.ml_line_count;
8984 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
8985
8986 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
8987 {
8988 /* <sugline>: <sugnr> ... NUL */
8989 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008990 len = (int)STRLEN(line) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008991 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
8992 {
8993 EMSG(_(e_write));
8994 goto theend;
8995 }
8996 spin->si_memtot += len;
8997 }
8998
8999 /* Write another byte to check for errors. */
9000 if (putc(0, fd) == EOF)
9001 EMSG(_(e_write));
9002
9003 vim_snprintf((char *)IObuff, IOSIZE,
9004 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
9005 spell_message(spin, IObuff);
9006
9007theend:
9008 /* close the file */
9009 fclose(fd);
9010}
9011
9012/*
9013 * Open a spell buffer. This is a nameless buffer that is not in the buffer
9014 * list and only contains text lines. Can use a swapfile to reduce memory
9015 * use.
9016 * Most other fields are invalid! Esp. watch out for string options being
9017 * NULL and there is no undo info.
9018 * Returns NULL when out of memory.
9019 */
9020 static buf_T *
9021open_spellbuf()
9022{
9023 buf_T *buf;
9024
9025 buf = (buf_T *)alloc_clear(sizeof(buf_T));
9026 if (buf != NULL)
9027 {
9028 buf->b_spell = TRUE;
9029 buf->b_p_swf = TRUE; /* may create a swap file */
9030 ml_open(buf);
9031 ml_open_file(buf); /* create swap file now */
9032 }
9033 return buf;
9034}
9035
9036/*
9037 * Close the buffer used for spell info.
9038 */
9039 static void
9040close_spellbuf(buf)
9041 buf_T *buf;
9042{
9043 if (buf != NULL)
9044 {
9045 ml_close(buf, TRUE);
9046 vim_free(buf);
9047 }
9048}
9049
9050
9051/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00009052 * Create a Vim spell file from one or more word lists.
9053 * "fnames[0]" is the output file name.
9054 * "fnames[fcount - 1]" is the last input file name.
9055 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
9056 * and ".spl" is appended to make the output file name.
9057 */
9058 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009059mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009060 int fcount;
9061 char_u **fnames;
9062 int ascii; /* -ascii argument given */
9063 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009064 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009065{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009066 char_u fname[MAXPATHL];
9067 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00009068 char_u **innames;
9069 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009070 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009071 int i;
9072 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009073 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00009074 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009075 spellinfo_T spin;
9076
9077 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009078 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009079 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009080 spin.si_followup = TRUE;
9081 spin.si_rem_accents = TRUE;
9082 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009083 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009084 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
9085 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009086 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009087 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009088 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009089 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009090
Bram Moolenaarb765d632005-06-07 21:00:02 +00009091 /* default: fnames[0] is output file, following are input files */
9092 innames = &fnames[1];
9093 incount = fcount - 1;
9094
9095 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00009096 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009097 len = (int)STRLEN(fnames[0]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009098 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
9099 {
9100 /* For ":mkspell path/en.latin1.add" output file is
9101 * "path/en.latin1.add.spl". */
9102 innames = &fnames[0];
9103 incount = 1;
9104 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
9105 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009106 else if (fcount == 1)
9107 {
9108 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
9109 innames = &fnames[0];
9110 incount = 1;
9111 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
9112 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
9113 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009114 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
9115 {
9116 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009117 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009118 }
9119 else
9120 /* Name should be language, make the file name from it. */
9121 vim_snprintf((char *)wfname, sizeof(wfname), "%s.%s.spl", fnames[0],
9122 spin.si_ascii ? (char_u *)"ascii" : spell_enc());
9123
9124 /* Check for .ascii.spl. */
9125 if (strstr((char *)gettail(wfname), ".ascii.") != NULL)
9126 spin.si_ascii = TRUE;
9127
9128 /* Check for .add.spl. */
9129 if (strstr((char *)gettail(wfname), ".add.") != NULL)
9130 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00009131 }
9132
Bram Moolenaarb765d632005-06-07 21:00:02 +00009133 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009134 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009135 else if (vim_strchr(gettail(wfname), '_') != NULL)
9136 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009137 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009138 EMSG(_("E754: Only up to 8 regions supported"));
9139 else
9140 {
9141 /* Check for overwriting before doing things that may take a lot of
9142 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009143 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009144 {
9145 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009146 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009147 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009148 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009149 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009150 EMSG2(_(e_isadir2), wfname);
9151 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009152 }
9153
9154 /*
9155 * Init the aff and dic pointers.
9156 * Get the region names if there are more than 2 arguments.
9157 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009158 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009159 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009160 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009161
Bram Moolenaar3982c542005-06-08 21:56:31 +00009162 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009163 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009164 len = (int)STRLEN(innames[i]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009165 if (STRLEN(gettail(innames[i])) < 5
9166 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009167 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009168 EMSG2(_("E755: Invalid region in %s"), innames[i]);
9169 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009170 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009171 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
9172 spin.si_region_name[i * 2 + 1] =
9173 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009174 }
9175 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009176 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009177
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009178 spin.si_foldroot = wordtree_alloc(&spin);
9179 spin.si_keeproot = wordtree_alloc(&spin);
9180 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009181 if (spin.si_foldroot == NULL
9182 || spin.si_keeproot == NULL
9183 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009184 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00009185 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009186 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009187 }
9188
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009189 /* When not producing a .add.spl file clear the character table when
9190 * we encounter one in the .aff file. This means we dump the current
9191 * one in the .spl file if the .aff file doesn't define one. That's
9192 * better than guessing the contents, the table will match a
9193 * previously loaded spell file. */
9194 if (!spin.si_add)
9195 spin.si_clear_chartab = TRUE;
9196
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009197 /*
9198 * Read all the .aff and .dic files.
9199 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00009200 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009201 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009202 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009203 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009204 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009205 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009206
Bram Moolenaarb765d632005-06-07 21:00:02 +00009207 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009208 if (mch_stat((char *)fname, &st) >= 0)
9209 {
9210 /* Read the .aff file. Will init "spin->si_conv" based on the
9211 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009212 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009213 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009214 error = TRUE;
9215 else
9216 {
9217 /* Read the .dic file and store the words in the trees. */
9218 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00009219 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009220 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009221 error = TRUE;
9222 }
9223 }
9224 else
9225 {
9226 /* No .aff file, try reading the file as a word list. Store
9227 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009228 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009229 error = TRUE;
9230 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009231
Bram Moolenaarb765d632005-06-07 21:00:02 +00009232#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009233 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00009234 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009235#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009236 }
9237
Bram Moolenaar78622822005-08-23 21:00:13 +00009238 if (spin.si_compflags != NULL && spin.si_nobreak)
9239 MSG(_("Warning: both compounding and NOBREAK specified"));
9240
Bram Moolenaar4770d092006-01-12 23:22:24 +00009241 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009242 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009243 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00009244 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009245 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009246 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009247 wordtree_compress(&spin, spin.si_foldroot);
9248 wordtree_compress(&spin, spin.si_keeproot);
9249 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009250 }
9251
Bram Moolenaar4770d092006-01-12 23:22:24 +00009252 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009253 {
9254 /*
9255 * Write the info in the spell file.
9256 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009257 vim_snprintf((char *)IObuff, IOSIZE,
9258 _("Writing spell file %s ..."), wfname);
9259 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00009260
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009261 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009262
Bram Moolenaar4770d092006-01-12 23:22:24 +00009263 spell_message(&spin, (char_u *)_("Done!"));
9264 vim_snprintf((char *)IObuff, IOSIZE,
9265 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
9266 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009267
Bram Moolenaar4770d092006-01-12 23:22:24 +00009268 /*
9269 * If the file is loaded need to reload it.
9270 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009271 if (!error)
9272 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009273 }
9274
9275 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009276 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009277 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009278 ga_clear(&spin.si_sal);
9279 ga_clear(&spin.si_map);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009280 ga_clear(&spin.si_comppat);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009281 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009282 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009283
9284 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009285 for (i = 0; i < incount; ++i)
9286 if (afile[i] != NULL)
9287 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009288
9289 /* Free all the bits and pieces at once. */
9290 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009291
9292 /*
9293 * If there is soundfolding info and no NOSUGFILE item create the
9294 * .sug file with the soundfolded word trie.
9295 */
9296 if (spin.si_sugtime != 0 && !error && !got_int)
9297 spell_make_sugfile(&spin, wfname);
9298
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009299 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009300}
9301
Bram Moolenaar4770d092006-01-12 23:22:24 +00009302/*
9303 * Display a message for spell file processing when 'verbose' is set or using
9304 * ":mkspell". "str" can be IObuff.
9305 */
9306 static void
9307spell_message(spin, str)
9308 spellinfo_T *spin;
9309 char_u *str;
9310{
9311 if (spin->si_verbose || p_verbose > 2)
9312 {
9313 if (!spin->si_verbose)
9314 verbose_enter();
9315 MSG(str);
9316 out_flush();
9317 if (!spin->si_verbose)
9318 verbose_leave();
9319 }
9320}
Bram Moolenaarb765d632005-06-07 21:00:02 +00009321
9322/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009323 * ":[count]spellgood {word}"
9324 * ":[count]spellwrong {word}"
Bram Moolenaard0131a82006-03-04 21:46:13 +00009325 * ":[count]spellundo {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00009326 */
9327 void
9328ex_spell(eap)
9329 exarg_T *eap;
9330{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009331 spell_add_word(eap->arg, (int)STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaard0131a82006-03-04 21:46:13 +00009332 eap->forceit ? 0 : (int)eap->line2,
9333 eap->cmdidx == CMD_spellundo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009334}
9335
9336/*
9337 * Add "word[len]" to 'spellfile' as a good or bad word.
9338 */
9339 void
Bram Moolenaar89d40322006-08-29 15:30:07 +00009340spell_add_word(word, len, bad, idx, undo)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009341 char_u *word;
9342 int len;
9343 int bad;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009344 int idx; /* "zG" and "zW": zero, otherwise index in
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009345 'spellfile' */
Bram Moolenaard0131a82006-03-04 21:46:13 +00009346 int undo; /* TRUE for "zug", "zuG", "zuw" and "zuW" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009347{
Bram Moolenaara3917072006-09-14 08:48:14 +00009348 FILE *fd = NULL;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009349 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009350 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009351 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009352 char_u fnamebuf[MAXPATHL];
9353 char_u line[MAXWLEN * 2];
9354 long fpos, fpos_next = 0;
9355 int i;
9356 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009357
Bram Moolenaar89d40322006-08-29 15:30:07 +00009358 if (idx == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009359 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009360 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009361 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009362 int_wordlist = vim_tempname('s');
9363 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009364 return;
9365 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009366 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009367 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009368 else
9369 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009370 /* If 'spellfile' isn't set figure out a good default value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009371 if (*curwin->w_s->b_p_spf == NUL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009372 {
9373 init_spellfile();
9374 new_spf = TRUE;
9375 }
9376
Bram Moolenaar860cae12010-06-05 23:22:07 +02009377 if (*curwin->w_s->b_p_spf == NUL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009378 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00009379 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009380 return;
9381 }
9382
Bram Moolenaar860cae12010-06-05 23:22:07 +02009383 for (spf = curwin->w_s->b_p_spf, i = 1; *spf != NUL; ++i)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009384 {
9385 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
Bram Moolenaar89d40322006-08-29 15:30:07 +00009386 if (i == idx)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009387 break;
9388 if (*spf == NUL)
9389 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009390 EMSGN(_("E765: 'spellfile' does not have %ld entries"), idx);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009391 return;
9392 }
9393 }
9394
Bram Moolenaarb765d632005-06-07 21:00:02 +00009395 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009396 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009397 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
9398 buf = NULL;
9399 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00009400 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009401 EMSG(_(e_bufloaded));
9402 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009403 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009404
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009405 fname = fnamebuf;
9406 }
9407
Bram Moolenaard0131a82006-03-04 21:46:13 +00009408 if (bad || undo)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009409 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009410 /* When the word appears as good word we need to remove that one,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009411 * since its flags sort before the one with WF_BANNED. */
9412 fd = mch_fopen((char *)fname, "r");
9413 if (fd != NULL)
9414 {
9415 while (!vim_fgets(line, MAXWLEN * 2, fd))
9416 {
9417 fpos = fpos_next;
9418 fpos_next = ftell(fd);
9419 if (STRNCMP(word, line, len) == 0
9420 && (line[len] == '/' || line[len] < ' '))
9421 {
9422 /* Found duplicate word. Remove it by writing a '#' at
9423 * the start of the line. Mixing reading and writing
9424 * doesn't work for all systems, close the file first. */
9425 fclose(fd);
9426 fd = mch_fopen((char *)fname, "r+");
9427 if (fd == NULL)
9428 break;
9429 if (fseek(fd, fpos, SEEK_SET) == 0)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009430 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009431 fputc('#', fd);
Bram Moolenaard0131a82006-03-04 21:46:13 +00009432 if (undo)
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009433 {
9434 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009435 smsg((char_u *)_("Word removed from %s"), NameBuff);
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009436 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00009437 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009438 fseek(fd, fpos_next, SEEK_SET);
9439 }
9440 }
9441 fclose(fd);
9442 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009443 }
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009444
9445 if (!undo)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009446 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009447 fd = mch_fopen((char *)fname, "a");
9448 if (fd == NULL && new_spf)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009449 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009450 char_u *p;
9451
Bram Moolenaard0131a82006-03-04 21:46:13 +00009452 /* We just initialized the 'spellfile' option and can't open the
9453 * file. We may need to create the "spell" directory first. We
9454 * already checked the runtime directory is writable in
9455 * init_spellfile(). */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009456 if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009457 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009458 int c = *p;
9459
Bram Moolenaard0131a82006-03-04 21:46:13 +00009460 /* The directory doesn't exist. Try creating it and opening
9461 * the file again. */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009462 *p = NUL;
9463 vim_mkdir(fname, 0755);
9464 *p = c;
Bram Moolenaard0131a82006-03-04 21:46:13 +00009465 fd = mch_fopen((char *)fname, "a");
9466 }
9467 }
9468
9469 if (fd == NULL)
9470 EMSG2(_(e_notopen), fname);
9471 else
9472 {
9473 if (bad)
9474 fprintf(fd, "%.*s/!\n", len, word);
9475 else
9476 fprintf(fd, "%.*s\n", len, word);
9477 fclose(fd);
9478
9479 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
9480 smsg((char_u *)_("Word added to %s"), NameBuff);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009481 }
9482 }
9483
Bram Moolenaard0131a82006-03-04 21:46:13 +00009484 if (fd != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009485 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009486 /* Update the .add.spl file. */
9487 mkspell(1, &fname, FALSE, TRUE, TRUE);
9488
9489 /* If the .add file is edited somewhere, reload it. */
9490 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00009491 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009492
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009493 redraw_all_later(SOME_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009494 }
9495}
9496
9497/*
9498 * Initialize 'spellfile' for the current buffer.
9499 */
9500 static void
9501init_spellfile()
9502{
9503 char_u buf[MAXPATHL];
9504 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009505 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009506 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009507 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009508 int aspath = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009509 char_u *lstart = curbuf->b_s.b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009510
Bram Moolenaar860cae12010-06-05 23:22:07 +02009511 if (*curwin->w_s->b_p_spl != NUL && curwin->w_s->b_langp.ga_len > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009512 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009513 /* Find the end of the language name. Exclude the region. If there
9514 * is a path separator remember the start of the tail. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009515 for (lend = curwin->w_s->b_p_spl; *lend != NUL
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009516 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009517 if (vim_ispathsep(*lend))
9518 {
9519 aspath = TRUE;
9520 lstart = lend + 1;
9521 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009522
9523 /* Loop over all entries in 'runtimepath'. Use the first one where we
9524 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009525 rtp = p_rtp;
9526 while (*rtp != NUL)
9527 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009528 if (aspath)
9529 /* Use directory of an entry with path, e.g., for
9530 * "/dir/lg.utf-8.spl" use "/dir". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009531 vim_strncpy(buf, curbuf->b_s.b_p_spl, lstart - curbuf->b_s.b_p_spl - 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009532 else
9533 /* Copy the path from 'runtimepath' to buf[]. */
9534 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009535 if (filewritable(buf) == 2)
9536 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00009537 /* Use the first language name from 'spelllang' and the
9538 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009539 if (aspath)
Bram Moolenaar860cae12010-06-05 23:22:07 +02009540 vim_strncpy(buf, curbuf->b_s.b_p_spl, lend - curbuf->b_s.b_p_spl);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009541 else
9542 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009543 /* Create the "spell" directory if it doesn't exist yet. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009544 l = (int)STRLEN(buf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009545 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
9546 if (!filewritable(buf) != 2)
9547 vim_mkdir(buf, 0755);
9548
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009549 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009550 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009551 "/%.*s", (int)(lend - lstart), lstart);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009552 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009553 l = (int)STRLEN(buf);
Bram Moolenaar860cae12010-06-05 23:22:07 +02009554 fname = LANGP_ENTRY(curwin->w_s->b_langp, 0)->lp_slang->sl_fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009555 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
9556 fname != NULL
9557 && strstr((char *)gettail(fname), ".ascii.") != NULL
9558 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009559 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
9560 break;
9561 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009562 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009563 }
9564 }
9565}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009566
Bram Moolenaar51485f02005-06-04 21:55:20 +00009567
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009568/*
9569 * Init the chartab used for spelling for ASCII.
9570 * EBCDIC is not supported!
9571 */
9572 static void
9573clear_spell_chartab(sp)
9574 spelltab_T *sp;
9575{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009576 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009577
9578 /* Init everything to FALSE. */
9579 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
9580 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
9581 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009582 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009583 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009584 sp->st_upper[i] = i;
9585 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009586
9587 /* We include digits. A word shouldn't start with a digit, but handling
9588 * that is done separately. */
9589 for (i = '0'; i <= '9'; ++i)
9590 sp->st_isw[i] = TRUE;
9591 for (i = 'A'; i <= 'Z'; ++i)
9592 {
9593 sp->st_isw[i] = TRUE;
9594 sp->st_isu[i] = TRUE;
9595 sp->st_fold[i] = i + 0x20;
9596 }
9597 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009598 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009599 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009600 sp->st_upper[i] = i - 0x20;
9601 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009602}
9603
9604/*
9605 * Init the chartab used for spelling. Only depends on 'encoding'.
9606 * Called once while starting up and when 'encoding' changes.
9607 * The default is to use isalpha(), but the spell file should define the word
9608 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009609 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009610 */
9611 void
9612init_spell_chartab()
9613{
9614 int i;
9615
9616 did_set_spelltab = FALSE;
9617 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009618#ifdef FEAT_MBYTE
9619 if (enc_dbcs)
9620 {
9621 /* DBCS: assume double-wide characters are word characters. */
9622 for (i = 128; i <= 255; ++i)
9623 if (MB_BYTE2LEN(i) == 2)
9624 spelltab.st_isw[i] = TRUE;
9625 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009626 else if (enc_utf8)
9627 {
9628 for (i = 128; i < 256; ++i)
9629 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02009630 int f = utf_fold(i);
9631 int u = utf_toupper(i);
9632
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009633 spelltab.st_isu[i] = utf_isupper(i);
9634 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02009635 /* The folded/upper-cased value is different between latin1 and
9636 * utf8 for 0xb5, causing E763 for no good reason. Use the latin1
9637 * value for utf-8 to avoid this. */
9638 spelltab.st_fold[i] = (f < 256) ? f : i;
9639 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009640 }
9641 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009642 else
9643#endif
9644 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009645 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009646 for (i = 128; i < 256; ++i)
9647 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009648 if (MB_ISUPPER(i))
9649 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009650 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009651 spelltab.st_isu[i] = TRUE;
9652 spelltab.st_fold[i] = MB_TOLOWER(i);
9653 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009654 else if (MB_ISLOWER(i))
9655 {
9656 spelltab.st_isw[i] = TRUE;
9657 spelltab.st_upper[i] = MB_TOUPPER(i);
9658 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009659 }
9660 }
9661}
9662
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009663/*
9664 * Set the spell character tables from strings in the affix file.
9665 */
9666 static int
9667set_spell_chartab(fol, low, upp)
9668 char_u *fol;
9669 char_u *low;
9670 char_u *upp;
9671{
9672 /* We build the new tables here first, so that we can compare with the
9673 * previous one. */
9674 spelltab_T new_st;
9675 char_u *pf = fol, *pl = low, *pu = upp;
9676 int f, l, u;
9677
9678 clear_spell_chartab(&new_st);
9679
9680 while (*pf != NUL)
9681 {
9682 if (*pl == NUL || *pu == NUL)
9683 {
9684 EMSG(_(e_affform));
9685 return FAIL;
9686 }
9687#ifdef FEAT_MBYTE
9688 f = mb_ptr2char_adv(&pf);
9689 l = mb_ptr2char_adv(&pl);
9690 u = mb_ptr2char_adv(&pu);
9691#else
9692 f = *pf++;
9693 l = *pl++;
9694 u = *pu++;
9695#endif
9696 /* Every character that appears is a word character. */
9697 if (f < 256)
9698 new_st.st_isw[f] = TRUE;
9699 if (l < 256)
9700 new_st.st_isw[l] = TRUE;
9701 if (u < 256)
9702 new_st.st_isw[u] = TRUE;
9703
9704 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9705 * case-folding */
9706 if (l < 256 && l != f)
9707 {
9708 if (f >= 256)
9709 {
9710 EMSG(_(e_affrange));
9711 return FAIL;
9712 }
9713 new_st.st_fold[l] = f;
9714 }
9715
9716 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009717 * case-folding, it's upper case and the "UPP" is the upper case of
9718 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009719 if (u < 256 && u != f)
9720 {
9721 if (f >= 256)
9722 {
9723 EMSG(_(e_affrange));
9724 return FAIL;
9725 }
9726 new_st.st_fold[u] = f;
9727 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009728 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009729 }
9730 }
9731
9732 if (*pl != NUL || *pu != NUL)
9733 {
9734 EMSG(_(e_affform));
9735 return FAIL;
9736 }
9737
9738 return set_spell_finish(&new_st);
9739}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009740
9741/*
9742 * Set the spell character tables from strings in the .spl file.
9743 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009744 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009745set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009746 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009747 int cnt; /* length of "flags" */
9748 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009749{
9750 /* We build the new tables here first, so that we can compare with the
9751 * previous one. */
9752 spelltab_T new_st;
9753 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009754 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009755 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009756
9757 clear_spell_chartab(&new_st);
9758
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009759 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009760 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009761 if (i < cnt)
9762 {
9763 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9764 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9765 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009766
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009767 if (*p != NUL)
9768 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009769#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009770 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009771#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009772 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009773#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009774 new_st.st_fold[i + 128] = c;
9775 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9776 new_st.st_upper[c] = i + 128;
9777 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009778 }
9779
Bram Moolenaar5195e452005-08-19 20:32:47 +00009780 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009781}
9782
9783 static int
9784set_spell_finish(new_st)
9785 spelltab_T *new_st;
9786{
9787 int i;
9788
9789 if (did_set_spelltab)
9790 {
9791 /* check that it's the same table */
9792 for (i = 0; i < 256; ++i)
9793 {
9794 if (spelltab.st_isw[i] != new_st->st_isw[i]
9795 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009796 || spelltab.st_fold[i] != new_st->st_fold[i]
9797 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009798 {
9799 EMSG(_("E763: Word characters differ between spell files"));
9800 return FAIL;
9801 }
9802 }
9803 }
9804 else
9805 {
9806 /* copy the new spelltab into the one being used */
9807 spelltab = *new_st;
9808 did_set_spelltab = TRUE;
9809 }
9810
9811 return OK;
9812}
9813
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009814/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009815 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009816 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009817 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009818 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009819 */
9820 static int
Bram Moolenaar860cae12010-06-05 23:22:07 +02009821spell_iswordp(p, wp)
Bram Moolenaarea408852005-06-25 22:49:46 +00009822 char_u *p;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009823 win_T *wp; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009824{
Bram Moolenaarea408852005-06-25 22:49:46 +00009825#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009826 char_u *s;
9827 int l;
9828 int c;
9829
9830 if (has_mbyte)
9831 {
9832 l = MB_BYTE2LEN(*p);
9833 s = p;
9834 if (l == 1)
9835 {
9836 /* be quick for ASCII */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009837 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009838 {
9839 s = p + 1; /* skip a mid-word character */
9840 l = MB_BYTE2LEN(*s);
9841 }
9842 }
9843 else
9844 {
9845 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02009846 if (c < 256 ? wp->w_s->b_spell_ismw[c]
9847 : (wp->w_s->b_spell_ismw_mb != NULL
9848 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009849 {
9850 s = p + l;
9851 l = MB_BYTE2LEN(*s);
9852 }
9853 }
9854
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009855 c = mb_ptr2char(s);
9856 if (c > 255)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009857 return spell_mb_isword_class(mb_get_class(s));
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009858 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009859 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009860#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009861
Bram Moolenaar860cae12010-06-05 23:22:07 +02009862 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009863}
9864
9865/*
9866 * Return TRUE if "p" points to a word character.
9867 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9868 */
9869 static int
9870spell_iswordp_nmw(p)
9871 char_u *p;
9872{
9873#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009874 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009875
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009876 if (has_mbyte)
9877 {
9878 c = mb_ptr2char(p);
9879 if (c > 255)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009880 return spell_mb_isword_class(mb_get_class(p));
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009881 return spelltab.st_isw[c];
9882 }
9883#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009884 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009885}
9886
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009887#ifdef FEAT_MBYTE
9888/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009889 * Return TRUE if word class indicates a word character.
9890 * Only for characters above 255.
9891 * Unicode subscript and superscript are not considered word characters.
9892 */
9893 static int
9894spell_mb_isword_class(cl)
9895 int cl;
9896{
9897 return cl >= 2 && cl != 0x2070 && cl != 0x2080;
9898}
9899
9900/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009901 * Return TRUE if "p" points to a word character.
9902 * Wide version of spell_iswordp().
9903 */
9904 static int
Bram Moolenaar860cae12010-06-05 23:22:07 +02009905spell_iswordp_w(p, wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009906 int *p;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009907 win_T *wp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009908{
9909 int *s;
9910
Bram Moolenaar860cae12010-06-05 23:22:07 +02009911 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
9912 : (wp->w_s->b_spell_ismw_mb != NULL
9913 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009914 s = p + 1;
9915 else
9916 s = p;
9917
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009918 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009919 {
9920 if (enc_utf8)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009921 return spell_mb_isword_class(utf_class(*s));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009922 if (enc_dbcs)
9923 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
9924 return 0;
9925 }
9926 return spelltab.st_isw[*s];
9927}
9928#endif
9929
Bram Moolenaarea408852005-06-25 22:49:46 +00009930/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009931 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009932 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009933 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009934 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009935write_spell_prefcond(fd, gap)
9936 FILE *fd;
9937 garray_T *gap;
9938{
9939 int i;
9940 char_u *p;
9941 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009942 int totlen;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00009943 size_t x = 1; /* collect return value of fwrite() */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009944
Bram Moolenaar5195e452005-08-19 20:32:47 +00009945 if (fd != NULL)
9946 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
9947
9948 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009949
9950 for (i = 0; i < gap->ga_len; ++i)
9951 {
9952 /* <prefcond> : <condlen> <condstr> */
9953 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009954 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009955 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009956 len = (int)STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009957 if (fd != NULL)
9958 {
9959 fputc(len, fd);
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00009960 x &= fwrite(p, (size_t)len, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009961 }
9962 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009963 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009964 else if (fd != NULL)
9965 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009966 }
9967
Bram Moolenaar5195e452005-08-19 20:32:47 +00009968 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009969}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009970
9971/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009972 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
9973 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009974 * When using a multi-byte 'encoding' the length may change!
9975 * Returns FAIL when something wrong.
9976 */
9977 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009978spell_casefold(str, len, buf, buflen)
9979 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009980 int len;
9981 char_u *buf;
9982 int buflen;
9983{
9984 int i;
9985
9986 if (len >= buflen)
9987 {
9988 buf[0] = NUL;
9989 return FAIL; /* result will not fit */
9990 }
9991
9992#ifdef FEAT_MBYTE
9993 if (has_mbyte)
9994 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009995 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009996 char_u *p;
9997 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009998
9999 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010000 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010001 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010002 if (outi + MB_MAXBYTES > buflen)
10003 {
10004 buf[outi] = NUL;
10005 return FAIL;
10006 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010007 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010008 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010009 }
10010 buf[outi] = NUL;
10011 }
10012 else
10013#endif
10014 {
10015 /* Be quick for non-multibyte encodings. */
10016 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010017 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010018 buf[i] = NUL;
10019 }
10020
10021 return OK;
10022}
10023
Bram Moolenaar4770d092006-01-12 23:22:24 +000010024/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010025#define SPS_BEST 1
10026#define SPS_FAST 2
10027#define SPS_DOUBLE 4
10028
Bram Moolenaar4770d092006-01-12 23:22:24 +000010029static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
10030static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010031
10032/*
10033 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +000010034 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010035 */
10036 int
10037spell_check_sps()
10038{
10039 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010040 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010041 char_u buf[MAXPATHL];
10042 int f;
10043
10044 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010045 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010046
10047 for (p = p_sps; *p != NUL; )
10048 {
10049 copy_option_part(&p, buf, MAXPATHL, ",");
10050
10051 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010052 if (VIM_ISDIGIT(*buf))
10053 {
10054 s = buf;
10055 sps_limit = getdigits(&s);
10056 if (*s != NUL && !VIM_ISDIGIT(*s))
10057 f = -1;
10058 }
10059 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010060 f = SPS_BEST;
10061 else if (STRCMP(buf, "fast") == 0)
10062 f = SPS_FAST;
10063 else if (STRCMP(buf, "double") == 0)
10064 f = SPS_DOUBLE;
10065 else if (STRNCMP(buf, "expr:", 5) != 0
10066 && STRNCMP(buf, "file:", 5) != 0)
10067 f = -1;
10068
10069 if (f == -1 || (sps_flags != 0 && f != 0))
10070 {
10071 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010072 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010073 return FAIL;
10074 }
10075 if (f != 0)
10076 sps_flags = f;
10077 }
10078
10079 if (sps_flags == 0)
10080 sps_flags = SPS_BEST;
10081
10082 return OK;
10083}
10084
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010085/*
10086 * "z?": Find badly spelled word under or after the cursor.
10087 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010088 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +000010089 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010090 */
10091 void
Bram Moolenaard12a1322005-08-21 22:08:24 +000010092spell_suggest(count)
10093 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010094{
10095 char_u *line;
10096 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010097 char_u wcopy[MAXWLEN + 2];
10098 char_u *p;
10099 int i;
10100 int c;
10101 suginfo_T sug;
10102 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010103 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010104 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010105 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010106 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010107 int badlen = 0;
Bram Moolenaarb2450162009-07-22 09:04:20 +000010108 int msg_scroll_save = msg_scroll;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010109
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010110 if (no_spell_checking(curwin))
10111 return;
10112
10113#ifdef FEAT_VISUAL
10114 if (VIsual_active)
10115 {
10116 /* Use the Visually selected text as the bad word. But reject
10117 * a multi-line selection. */
10118 if (curwin->w_cursor.lnum != VIsual.lnum)
10119 {
10120 vim_beep();
10121 return;
10122 }
10123 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
10124 if (badlen < 0)
10125 badlen = -badlen;
10126 else
10127 curwin->w_cursor.col = VIsual.col;
10128 ++badlen;
10129 end_visual_mode();
10130 }
10131 else
10132#endif
10133 /* Find the start of the badly spelled word. */
10134 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +000010135 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010136 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010137 /* No bad word or it starts after the cursor: use the word under the
10138 * cursor. */
10139 curwin->w_cursor = prev_cursor;
10140 line = ml_get_curline();
10141 p = line + curwin->w_cursor.col;
10142 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010143 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010144 mb_ptr_back(line, p);
10145 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010146 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010147 mb_ptr_adv(p);
10148
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010149 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010150 {
10151 beep_flush();
10152 return;
10153 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010154 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010155 }
10156
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010157 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010158
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010159 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010160 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010161
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010162 /* Make a copy of current line since autocommands may free the line. */
10163 line = vim_strsave(ml_get_curline());
10164 if (line == NULL)
10165 goto skip;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010166
Bram Moolenaar5195e452005-08-19 20:32:47 +000010167 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
10168 * 'spellsuggest', whatever is smaller. */
10169 if (sps_limit > (int)Rows - 2)
10170 limit = (int)Rows - 2;
10171 else
10172 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010173 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010174 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010175
10176 if (sug.su_ga.ga_len == 0)
10177 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +000010178 else if (count > 0)
10179 {
10180 if (count > sug.su_ga.ga_len)
10181 smsg((char_u *)_("Sorry, only %ld suggestions"),
10182 (long)sug.su_ga.ga_len);
10183 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010184 else
10185 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010186 vim_free(repl_from);
10187 repl_from = NULL;
10188 vim_free(repl_to);
10189 repl_to = NULL;
10190
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010191#ifdef FEAT_RIGHTLEFT
10192 /* When 'rightleft' is set the list is drawn right-left. */
10193 cmdmsg_rl = curwin->w_p_rl;
10194 if (cmdmsg_rl)
10195 msg_col = Columns - 1;
10196#endif
10197
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010198 /* List the suggestions. */
10199 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000010200 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010201 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010202 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
10203 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010204#ifdef FEAT_RIGHTLEFT
10205 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
10206 {
10207 /* And now the rabbit from the high hat: Avoid showing the
10208 * untranslated message rightleft. */
10209 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
10210 sug.su_badlen, sug.su_badptr);
10211 }
10212#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010213 msg_puts(IObuff);
10214 msg_clr_eos();
10215 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +000010216
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010217 msg_scroll = TRUE;
10218 for (i = 0; i < sug.su_ga.ga_len; ++i)
10219 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010220 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010221
10222 /* The suggested word may replace only part of the bad word, add
10223 * the not replaced part. */
10224 STRCPY(wcopy, stp->st_word);
10225 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010226 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010227 sug.su_badptr + stp->st_orglen,
10228 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010229 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
10230#ifdef FEAT_RIGHTLEFT
10231 if (cmdmsg_rl)
10232 rl_mirror(IObuff);
10233#endif
10234 msg_puts(IObuff);
10235
10236 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010237 msg_puts(IObuff);
10238
10239 /* The word may replace more than "su_badlen". */
10240 if (sug.su_badlen < stp->st_orglen)
10241 {
10242 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
10243 stp->st_orglen, sug.su_badptr);
10244 msg_puts(IObuff);
10245 }
10246
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010247 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010248 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010249 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010250 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010251 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010252 stp->st_salscore ? "s " : "",
10253 stp->st_score, stp->st_altscore);
10254 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010255 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +000010256 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010257#ifdef FEAT_RIGHTLEFT
10258 if (cmdmsg_rl)
10259 /* Mirror the numbers, but keep the leading space. */
10260 rl_mirror(IObuff + 1);
10261#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +000010262 msg_advance(30);
10263 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010264 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010265 msg_putchar('\n');
10266 }
10267
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010268#ifdef FEAT_RIGHTLEFT
10269 cmdmsg_rl = FALSE;
10270 msg_col = 0;
10271#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010272 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010273 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010274 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010275 selected -= lines_left;
Bram Moolenaarb2450162009-07-22 09:04:20 +000010276 lines_left = Rows; /* avoid more prompt */
10277 /* don't delay for 'smd' in normal_cmd() */
10278 msg_scroll = msg_scroll_save;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010279 }
10280
Bram Moolenaard12a1322005-08-21 22:08:24 +000010281 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
10282 {
10283 /* Save the from and to text for :spellrepall. */
10284 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000010285 if (sug.su_badlen > stp->st_orglen)
10286 {
10287 /* Replacing less than "su_badlen", append the remainder to
10288 * repl_to. */
10289 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
10290 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
10291 sug.su_badlen - stp->st_orglen,
10292 sug.su_badptr + stp->st_orglen);
10293 repl_to = vim_strsave(IObuff);
10294 }
10295 else
10296 {
10297 /* Replacing su_badlen or more, use the whole word. */
10298 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
10299 repl_to = vim_strsave(stp->st_word);
10300 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010301
10302 /* Replace the word. */
Bram Moolenaarb2450162009-07-22 09:04:20 +000010303 p = alloc((unsigned)STRLEN(line) - stp->st_orglen
10304 + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010305 if (p != NULL)
10306 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010307 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010308 mch_memmove(p, line, c);
10309 STRCPY(p + c, stp->st_word);
10310 STRCAT(p, sug.su_badptr + stp->st_orglen);
10311 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10312 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010313
10314 /* For redo we use a change-word command. */
10315 ResetRedobuff();
10316 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +000010317 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010318 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010319 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010320
10321 /* After this "p" may be invalid. */
10322 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010323 }
10324 }
10325 else
10326 curwin->w_cursor = prev_cursor;
10327
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010328 spell_find_cleanup(&sug);
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010329skip:
10330 vim_free(line);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010331}
10332
10333/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010334 * Check if the word at line "lnum" column "col" is required to start with a
10335 * capital. This uses 'spellcapcheck' of the current buffer.
10336 */
10337 static int
10338check_need_cap(lnum, col)
10339 linenr_T lnum;
10340 colnr_T col;
10341{
10342 int need_cap = FALSE;
10343 char_u *line;
10344 char_u *line_copy = NULL;
10345 char_u *p;
10346 colnr_T endcol;
10347 regmatch_T regmatch;
10348
Bram Moolenaar860cae12010-06-05 23:22:07 +020010349 if (curwin->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010350 return FALSE;
10351
10352 line = ml_get_curline();
10353 endcol = 0;
10354 if ((int)(skipwhite(line) - line) >= (int)col)
10355 {
10356 /* At start of line, check if previous line is empty or sentence
10357 * ends there. */
10358 if (lnum == 1)
10359 need_cap = TRUE;
10360 else
10361 {
10362 line = ml_get(lnum - 1);
10363 if (*skipwhite(line) == NUL)
10364 need_cap = TRUE;
10365 else
10366 {
10367 /* Append a space in place of the line break. */
10368 line_copy = concat_str(line, (char_u *)" ");
10369 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010370 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010371 }
10372 }
10373 }
10374 else
10375 endcol = col;
10376
10377 if (endcol > 0)
10378 {
10379 /* Check if sentence ends before the bad word. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010380 regmatch.regprog = curwin->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010381 regmatch.rm_ic = FALSE;
10382 p = line + endcol;
10383 for (;;)
10384 {
10385 mb_ptr_back(line, p);
10386 if (p == line || spell_iswordp_nmw(p))
10387 break;
10388 if (vim_regexec(&regmatch, p, 0)
10389 && regmatch.endp[0] == line + endcol)
10390 {
10391 need_cap = TRUE;
10392 break;
10393 }
10394 }
10395 }
10396
10397 vim_free(line_copy);
10398
10399 return need_cap;
10400}
10401
10402
10403/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010404 * ":spellrepall"
10405 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010406 void
10407ex_spellrepall(eap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000010408 exarg_T *eap UNUSED;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010409{
10410 pos_T pos = curwin->w_cursor;
10411 char_u *frompat;
10412 int addlen;
10413 char_u *line;
10414 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010415 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010416 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010417
10418 if (repl_from == NULL || repl_to == NULL)
10419 {
10420 EMSG(_("E752: No previous spell replacement"));
10421 return;
10422 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010423 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010424
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010425 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010426 if (frompat == NULL)
10427 return;
10428 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
10429 p_ws = FALSE;
10430
Bram Moolenaar5195e452005-08-19 20:32:47 +000010431 sub_nsubs = 0;
10432 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010433 curwin->w_cursor.lnum = 0;
10434 while (!got_int)
10435 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +000010436 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010437 || u_save_cursor() == FAIL)
10438 break;
10439
10440 /* Only replace when the right word isn't there yet. This happens
10441 * when changing "etc" to "etc.". */
10442 line = ml_get_curline();
10443 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
10444 repl_to, STRLEN(repl_to)) != 0)
10445 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010446 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010447 if (p == NULL)
10448 break;
10449 mch_memmove(p, line, curwin->w_cursor.col);
10450 STRCPY(p + curwin->w_cursor.col, repl_to);
10451 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
10452 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10453 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010454
10455 if (curwin->w_cursor.lnum != prev_lnum)
10456 {
10457 ++sub_nlines;
10458 prev_lnum = curwin->w_cursor.lnum;
10459 }
10460 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010461 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010462 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010463 }
10464
10465 p_ws = save_ws;
10466 curwin->w_cursor = pos;
10467 vim_free(frompat);
10468
Bram Moolenaar5195e452005-08-19 20:32:47 +000010469 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010470 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010471 else
10472 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010473}
10474
10475/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010476 * Find spell suggestions for "word". Return them in the growarray "*gap" as
10477 * a list of allocated strings.
10478 */
10479 void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010480spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010481 garray_T *gap;
10482 char_u *word;
10483 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010484 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010485 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010486{
10487 suginfo_T sug;
10488 int i;
10489 suggest_T *stp;
10490 char_u *wcopy;
10491
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010492 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010493
10494 /* Make room in "gap". */
10495 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010496 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010497 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010498 for (i = 0; i < sug.su_ga.ga_len; ++i)
10499 {
10500 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010501
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010502 /* The suggested word may replace only part of "word", add the not
10503 * replaced part. */
10504 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010505 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010506 if (wcopy == NULL)
10507 break;
10508 STRCPY(wcopy, stp->st_word);
10509 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
10510 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
10511 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010512 }
10513
10514 spell_find_cleanup(&sug);
10515}
10516
10517/*
10518 * Find spell suggestions for the word at the start of "badptr".
10519 * Return the suggestions in "su->su_ga".
10520 * The maximum number of suggestions is "maxcount".
10521 * Note: does use info for the current window.
10522 * This is based on the mechanisms of Aspell, but completely reimplemented.
10523 */
10524 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010525spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010526 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010527 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010528 suginfo_T *su;
10529 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +000010530 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010531 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010532 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010533{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010534 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010535 char_u buf[MAXPATHL];
10536 char_u *p;
10537 int do_combine = FALSE;
10538 char_u *sps_copy;
10539#ifdef FEAT_EVAL
10540 static int expr_busy = FALSE;
10541#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010542 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010543 int i;
10544 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010545
10546 /*
10547 * Set the info in "*su".
10548 */
10549 vim_memset(su, 0, sizeof(suginfo_T));
10550 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10551 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010552 if (*badptr == NUL)
10553 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010554 hash_init(&su->su_banned);
10555
10556 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010557 if (badlen != 0)
10558 su->su_badlen = badlen;
10559 else
10560 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010561 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010562 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010563
10564 if (su->su_badlen >= MAXWLEN)
10565 su->su_badlen = MAXWLEN - 1; /* just in case */
10566 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10567 (void)spell_casefold(su->su_badptr, su->su_badlen,
10568 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010569 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010570 su->su_badflags = badword_captype(su->su_badptr,
10571 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010572 if (need_cap)
10573 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010574
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010575 /* Find the default language for sound folding. We simply use the first
10576 * one in 'spelllang' that supports sound folding. That's good for when
10577 * using multiple files for one language, it's not that bad when mixing
10578 * languages (e.g., "pl,en"). */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010579 for (i = 0; i < curbuf->b_s.b_langp.ga_len; ++i)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010580 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020010581 lp = LANGP_ENTRY(curbuf->b_s.b_langp, i);
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010582 if (lp->lp_sallang != NULL)
10583 {
10584 su->su_sallang = lp->lp_sallang;
10585 break;
10586 }
10587 }
10588
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010589 /* Soundfold the bad word with the default sound folding, so that we don't
10590 * have to do this many times. */
10591 if (su->su_sallang != NULL)
10592 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10593 su->su_sal_badword);
10594
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010595 /* If the word is not capitalised and spell_check() doesn't consider the
10596 * word to be bad then it might need to be capitalised. Add a suggestion
10597 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010598 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010599 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010600 {
10601 make_case_word(su->su_badword, buf, WF_ONECAP);
10602 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010603 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010604 }
10605
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010606 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +000010607 if (banbadword)
10608 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010609
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010610 /* Make a copy of 'spellsuggest', because the expression may change it. */
10611 sps_copy = vim_strsave(p_sps);
10612 if (sps_copy == NULL)
10613 return;
10614
10615 /* Loop over the items in 'spellsuggest'. */
10616 for (p = sps_copy; *p != NUL; )
10617 {
10618 copy_option_part(&p, buf, MAXPATHL, ",");
10619
10620 if (STRNCMP(buf, "expr:", 5) == 0)
10621 {
10622#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010623 /* Evaluate an expression. Skip this when called recursively,
10624 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010625 if (!expr_busy)
10626 {
10627 expr_busy = TRUE;
10628 spell_suggest_expr(su, buf + 5);
10629 expr_busy = FALSE;
10630 }
10631#endif
10632 }
10633 else if (STRNCMP(buf, "file:", 5) == 0)
10634 /* Use list of suggestions in a file. */
10635 spell_suggest_file(su, buf + 5);
10636 else
10637 {
10638 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010639 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010640 if (sps_flags & SPS_DOUBLE)
10641 do_combine = TRUE;
10642 }
10643 }
10644
10645 vim_free(sps_copy);
10646
10647 if (do_combine)
10648 /* Combine the two list of suggestions. This must be done last,
10649 * because sorting changes the order again. */
10650 score_combine(su);
10651}
10652
10653#ifdef FEAT_EVAL
10654/*
10655 * Find suggestions by evaluating expression "expr".
10656 */
10657 static void
10658spell_suggest_expr(su, expr)
10659 suginfo_T *su;
10660 char_u *expr;
10661{
10662 list_T *list;
10663 listitem_T *li;
10664 int score;
10665 char_u *p;
10666
10667 /* The work is split up in a few parts to avoid having to export
10668 * suginfo_T.
10669 * First evaluate the expression and get the resulting list. */
10670 list = eval_spell_expr(su->su_badword, expr);
10671 if (list != NULL)
10672 {
10673 /* Loop over the items in the list. */
10674 for (li = list->lv_first; li != NULL; li = li->li_next)
10675 if (li->li_tv.v_type == VAR_LIST)
10676 {
10677 /* Get the word and the score from the items. */
10678 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010679 if (score >= 0 && score <= su->su_maxscore)
10680 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10681 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010682 }
10683 list_unref(list);
10684 }
10685
Bram Moolenaar4770d092006-01-12 23:22:24 +000010686 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10687 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010688 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10689}
10690#endif
10691
10692/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010693 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010694 */
10695 static void
10696spell_suggest_file(su, fname)
10697 suginfo_T *su;
10698 char_u *fname;
10699{
10700 FILE *fd;
10701 char_u line[MAXWLEN * 2];
10702 char_u *p;
10703 int len;
10704 char_u cword[MAXWLEN];
10705
10706 /* Open the file. */
10707 fd = mch_fopen((char *)fname, "r");
10708 if (fd == NULL)
10709 {
10710 EMSG2(_(e_notopen), fname);
10711 return;
10712 }
10713
10714 /* Read it line by line. */
10715 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10716 {
10717 line_breakcheck();
10718
10719 p = vim_strchr(line, '/');
10720 if (p == NULL)
10721 continue; /* No Tab found, just skip the line. */
10722 *p++ = NUL;
10723 if (STRICMP(su->su_badword, line) == 0)
10724 {
10725 /* Match! Isolate the good word, until CR or NL. */
10726 for (len = 0; p[len] >= ' '; ++len)
10727 ;
10728 p[len] = NUL;
10729
10730 /* If the suggestion doesn't have specific case duplicate the case
10731 * of the bad word. */
10732 if (captype(p, NULL) == 0)
10733 {
10734 make_case_word(p, cword, su->su_badflags);
10735 p = cword;
10736 }
10737
10738 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010739 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010740 }
10741 }
10742
10743 fclose(fd);
10744
Bram Moolenaar4770d092006-01-12 23:22:24 +000010745 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10746 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010747 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10748}
10749
10750/*
10751 * Find suggestions for the internal method indicated by "sps_flags".
10752 */
10753 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010754spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010755 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010756 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010757{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010758 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010759 * Load the .sug file(s) that are available and not done yet.
10760 */
10761 suggest_load_files();
10762
10763 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010764 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010765 *
10766 * Set a maximum score to limit the combination of operations that is
10767 * tried.
10768 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010769 suggest_try_special(su);
10770
10771 /*
10772 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10773 * from the .aff file and inserting a space (split the word).
10774 */
10775 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010776
10777 /* For the resulting top-scorers compute the sound-a-like score. */
10778 if (sps_flags & SPS_DOUBLE)
10779 score_comp_sal(su);
10780
10781 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010782 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010783 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010784 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010785 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010786 if (sps_flags & SPS_BEST)
10787 /* Adjust the word score for the suggestions found so far for how
10788 * they sounds like. */
10789 rescore_suggestions(su);
10790
10791 /*
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010792 * While going through the soundfold tree "su_maxscore" is the score
Bram Moolenaar4770d092006-01-12 23:22:24 +000010793 * for the soundfold word, limits the changes that are being tried,
10794 * and "su_sfmaxscore" the rescored score, which is set by
10795 * cleanup_suggestions().
10796 * First find words with a small edit distance, because this is much
10797 * faster and often already finds the top-N suggestions. If we didn't
10798 * find many suggestions try again with a higher edit distance.
10799 * "sl_sounddone" is used to avoid doing the same word twice.
10800 */
10801 suggest_try_soundalike_prep();
10802 su->su_maxscore = SCORE_SFMAX1;
10803 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010804 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010805 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10806 {
10807 /* We didn't find enough matches, try again, allowing more
10808 * changes to the soundfold word. */
10809 su->su_maxscore = SCORE_SFMAX2;
10810 suggest_try_soundalike(su);
10811 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10812 {
10813 /* Still didn't find enough matches, try again, allowing even
10814 * more changes to the soundfold word. */
10815 su->su_maxscore = SCORE_SFMAX3;
10816 suggest_try_soundalike(su);
10817 }
10818 }
10819 su->su_maxscore = su->su_sfmaxscore;
10820 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010821 }
10822
Bram Moolenaar4770d092006-01-12 23:22:24 +000010823 /* When CTRL-C was hit while searching do show the results. Only clear
10824 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010825 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010826 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010827 {
10828 (void)vgetc();
10829 got_int = FALSE;
10830 }
10831
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010832 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010833 {
10834 if (sps_flags & SPS_BEST)
10835 /* Adjust the word score for how it sounds like. */
10836 rescore_suggestions(su);
10837
Bram Moolenaar4770d092006-01-12 23:22:24 +000010838 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10839 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010840 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010841 }
10842}
10843
10844/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010845 * Load the .sug files for languages that have one and weren't loaded yet.
10846 */
10847 static void
10848suggest_load_files()
10849{
10850 langp_T *lp;
10851 int lpi;
10852 slang_T *slang;
10853 char_u *dotp;
10854 FILE *fd;
10855 char_u buf[MAXWLEN];
10856 int i;
10857 time_t timestamp;
10858 int wcount;
10859 int wordnr;
10860 garray_T ga;
10861 int c;
10862
10863 /* Do this for all languages that support sound folding. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010864 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010865 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020010866 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010867 slang = lp->lp_slang;
10868 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10869 {
10870 /* Change ".spl" to ".sug" and open the file. When the file isn't
10871 * found silently skip it. Do set "sl_sugloaded" so that we
10872 * don't try again and again. */
10873 slang->sl_sugloaded = TRUE;
10874
10875 dotp = vim_strrchr(slang->sl_fname, '.');
10876 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10877 continue;
10878 STRCPY(dotp, ".sug");
Bram Moolenaar5555acc2006-04-07 21:33:12 +000010879 fd = mch_fopen((char *)slang->sl_fname, "r");
Bram Moolenaar4770d092006-01-12 23:22:24 +000010880 if (fd == NULL)
10881 goto nextone;
10882
10883 /*
10884 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10885 */
10886 for (i = 0; i < VIMSUGMAGICL; ++i)
10887 buf[i] = getc(fd); /* <fileID> */
10888 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10889 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010890 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010891 slang->sl_fname);
10892 goto nextone;
10893 }
10894 c = getc(fd); /* <versionnr> */
10895 if (c < VIMSUGVERSION)
10896 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010897 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010898 slang->sl_fname);
10899 goto nextone;
10900 }
10901 else if (c > VIMSUGVERSION)
10902 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010903 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010904 slang->sl_fname);
10905 goto nextone;
10906 }
10907
10908 /* Check the timestamp, it must be exactly the same as the one in
10909 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +020010910 timestamp = get8ctime(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010911 if (timestamp != slang->sl_sugtime)
10912 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010913 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010914 slang->sl_fname);
10915 goto nextone;
10916 }
10917
10918 /*
10919 * <SUGWORDTREE>: <wordtree>
10920 * Read the trie with the soundfolded words.
10921 */
10922 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10923 FALSE, 0) != 0)
10924 {
10925someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010926 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010927 slang->sl_fname);
10928 slang_clear_sug(slang);
10929 goto nextone;
10930 }
10931
10932 /*
10933 * <SUGTABLE>: <sugwcount> <sugline> ...
10934 *
10935 * Read the table with word numbers. We use a file buffer for
10936 * this, because it's so much like a file with lines. Makes it
10937 * possible to swap the info and save on memory use.
10938 */
10939 slang->sl_sugbuf = open_spellbuf();
10940 if (slang->sl_sugbuf == NULL)
10941 goto someerror;
10942 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010943 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010944 if (wcount < 0)
10945 goto someerror;
10946
10947 /* Read all the wordnr lists into the buffer, one NUL terminated
10948 * list per line. */
10949 ga_init2(&ga, 1, 100);
10950 for (wordnr = 0; wordnr < wcount; ++wordnr)
10951 {
10952 ga.ga_len = 0;
10953 for (;;)
10954 {
10955 c = getc(fd); /* <sugline> */
10956 if (c < 0 || ga_grow(&ga, 1) == FAIL)
10957 goto someerror;
10958 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
10959 if (c == NUL)
10960 break;
10961 }
10962 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
10963 ga.ga_data, ga.ga_len, TRUE) == FAIL)
10964 goto someerror;
10965 }
10966 ga_clear(&ga);
10967
10968 /*
10969 * Need to put word counts in the word tries, so that we can find
10970 * a word by its number.
10971 */
10972 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
10973 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
10974
10975nextone:
10976 if (fd != NULL)
10977 fclose(fd);
10978 STRCPY(dotp, ".spl");
10979 }
10980 }
10981}
10982
10983
10984/*
10985 * Fill in the wordcount fields for a trie.
10986 * Returns the total number of words.
10987 */
10988 static void
10989tree_count_words(byts, idxs)
10990 char_u *byts;
10991 idx_T *idxs;
10992{
10993 int depth;
10994 idx_T arridx[MAXWLEN];
10995 int curi[MAXWLEN];
10996 int c;
10997 idx_T n;
10998 int wordcount[MAXWLEN];
10999
11000 arridx[0] = 0;
11001 curi[0] = 1;
11002 wordcount[0] = 0;
11003 depth = 0;
11004 while (depth >= 0 && !got_int)
11005 {
11006 if (curi[depth] > byts[arridx[depth]])
11007 {
11008 /* Done all bytes at this node, go up one level. */
11009 idxs[arridx[depth]] = wordcount[depth];
11010 if (depth > 0)
11011 wordcount[depth - 1] += wordcount[depth];
11012
11013 --depth;
11014 fast_breakcheck();
11015 }
11016 else
11017 {
11018 /* Do one more byte at this node. */
11019 n = arridx[depth] + curi[depth];
11020 ++curi[depth];
11021
11022 c = byts[n];
11023 if (c == 0)
11024 {
11025 /* End of word, count it. */
11026 ++wordcount[depth];
11027
11028 /* Skip over any other NUL bytes (same word with different
11029 * flags). */
11030 while (byts[n + 1] == 0)
11031 {
11032 ++n;
11033 ++curi[depth];
11034 }
11035 }
11036 else
11037 {
11038 /* Normal char, go one level deeper to count the words. */
11039 ++depth;
11040 arridx[depth] = idxs[n];
11041 curi[depth] = 1;
11042 wordcount[depth] = 0;
11043 }
11044 }
11045 }
11046}
11047
11048/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011049 * Free the info put in "*su" by spell_find_suggest().
11050 */
11051 static void
11052spell_find_cleanup(su)
11053 suginfo_T *su;
11054{
11055 int i;
11056
11057 /* Free the suggestions. */
11058 for (i = 0; i < su->su_ga.ga_len; ++i)
11059 vim_free(SUG(su->su_ga, i).st_word);
11060 ga_clear(&su->su_ga);
11061 for (i = 0; i < su->su_sga.ga_len; ++i)
11062 vim_free(SUG(su->su_sga, i).st_word);
11063 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011064
11065 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011066 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011067}
11068
11069/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011070 * Make a copy of "word", with the first letter upper or lower cased, to
11071 * "wcopy[MAXWLEN]". "word" must not be empty.
11072 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011073 */
11074 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011075onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011076 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011077 char_u *wcopy;
11078 int upper; /* TRUE: first letter made upper case */
11079{
11080 char_u *p;
11081 int c;
11082 int l;
11083
11084 p = word;
11085#ifdef FEAT_MBYTE
11086 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011087 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011088 else
11089#endif
11090 c = *p++;
11091 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011092 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011093 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011094 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011095#ifdef FEAT_MBYTE
11096 if (has_mbyte)
11097 l = mb_char2bytes(c, wcopy);
11098 else
11099#endif
11100 {
11101 l = 1;
11102 wcopy[0] = c;
11103 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011104 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011105}
11106
11107/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011108 * Make a copy of "word" with all the letters upper cased into
11109 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011110 */
11111 static void
11112allcap_copy(word, wcopy)
11113 char_u *word;
11114 char_u *wcopy;
11115{
11116 char_u *s;
11117 char_u *d;
11118 int c;
11119
11120 d = wcopy;
11121 for (s = word; *s != NUL; )
11122 {
11123#ifdef FEAT_MBYTE
11124 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011125 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011126 else
11127#endif
11128 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000011129
11130#ifdef FEAT_MBYTE
11131 /* We only change ß to SS when we are certain latin1 is used. It
11132 * would cause weird errors in other 8-bit encodings. */
11133 if (enc_latin1like && c == 0xdf)
11134 {
11135 c = 'S';
11136 if (d - wcopy >= MAXWLEN - 1)
11137 break;
11138 *d++ = c;
11139 }
11140 else
11141#endif
11142 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011143
11144#ifdef FEAT_MBYTE
11145 if (has_mbyte)
11146 {
11147 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
11148 break;
11149 d += mb_char2bytes(c, d);
11150 }
11151 else
11152#endif
11153 {
11154 if (d - wcopy >= MAXWLEN - 1)
11155 break;
11156 *d++ = c;
11157 }
11158 }
11159 *d = NUL;
11160}
11161
11162/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000011163 * Try finding suggestions by recognizing specific situations.
11164 */
11165 static void
11166suggest_try_special(su)
11167 suginfo_T *su;
11168{
11169 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011170 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011171 int c;
11172 char_u word[MAXWLEN];
11173
11174 /*
11175 * Recognize a word that is repeated: "the the".
11176 */
11177 p = skiptowhite(su->su_fbadword);
11178 len = p - su->su_fbadword;
11179 p = skipwhite(p);
11180 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
11181 {
11182 /* Include badflags: if the badword is onecap or allcap
11183 * use that for the goodword too: "The the" -> "The". */
11184 c = su->su_fbadword[len];
11185 su->su_fbadword[len] = NUL;
11186 make_case_word(su->su_fbadword, word, su->su_badflags);
11187 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011188
11189 /* Give a soundalike score of 0, compute the score as if deleting one
11190 * character. */
11191 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011192 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011193 }
11194}
11195
11196/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011197 * Try finding suggestions by adding/removing/swapping letters.
11198 */
11199 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000011200suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011201 suginfo_T *su;
11202{
11203 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011204 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011205 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011206 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011207 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011208
11209 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000011210 * to find matches (esp. REP items). Append some more text, changing
11211 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011212 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011213 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011214 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011215 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011216
Bram Moolenaar860cae12010-06-05 23:22:07 +020011217 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011218 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020011219 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011220
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011221 /* If reloading a spell file fails it's still in the list but
11222 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011223 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011224 continue;
11225
Bram Moolenaar4770d092006-01-12 23:22:24 +000011226 /* Try it for this language. Will add possible suggestions. */
11227 suggest_trie_walk(su, lp, fword, FALSE);
11228 }
11229}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011230
Bram Moolenaar4770d092006-01-12 23:22:24 +000011231/* Check the maximum score, if we go over it we won't try this change. */
11232#define TRY_DEEPER(su, stack, depth, add) \
11233 (stack[depth].ts_score + (add) < su->su_maxscore)
11234
11235/*
11236 * Try finding suggestions by adding/removing/swapping letters.
11237 *
11238 * This uses a state machine. At each node in the tree we try various
11239 * operations. When trying if an operation works "depth" is increased and the
11240 * stack[] is used to store info. This allows combinations, thus insert one
11241 * character, replace one and delete another. The number of changes is
11242 * limited by su->su_maxscore.
11243 *
11244 * After implementing this I noticed an article by Kemal Oflazer that
11245 * describes something similar: "Error-tolerant Finite State Recognition with
11246 * Applications to Morphological Analysis and Spelling Correction" (1996).
11247 * The implementation in the article is simplified and requires a stack of
11248 * unknown depth. The implementation here only needs a stack depth equal to
11249 * the length of the word.
11250 *
11251 * This is also used for the sound-folded word, "soundfold" is TRUE then.
11252 * The mechanism is the same, but we find a match with a sound-folded word
11253 * that comes from one or more original words. Each of these words may be
11254 * added, this is done by add_sound_suggest().
11255 * Don't use:
11256 * the prefix tree or the keep-case tree
11257 * "su->su_badlen"
11258 * anything to do with upper and lower case
11259 * anything to do with word or non-word characters ("spell_iswordp()")
11260 * banned words
11261 * word flags (rare, region, compounding)
11262 * word splitting for now
11263 * "similar_chars()"
11264 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
11265 */
11266 static void
11267suggest_trie_walk(su, lp, fword, soundfold)
11268 suginfo_T *su;
11269 langp_T *lp;
11270 char_u *fword;
11271 int soundfold;
11272{
11273 char_u tword[MAXWLEN]; /* good word collected so far */
11274 trystate_T stack[MAXWLEN];
11275 char_u preword[MAXWLEN * 3]; /* word found with proper case;
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010011276 * concatenation of prefix compound
Bram Moolenaar4770d092006-01-12 23:22:24 +000011277 * words and split word. NUL terminated
11278 * when going deeper but not when coming
11279 * back. */
11280 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
11281 trystate_T *sp;
11282 int newscore;
11283 int score;
11284 char_u *byts, *fbyts, *pbyts;
11285 idx_T *idxs, *fidxs, *pidxs;
11286 int depth;
11287 int c, c2, c3;
11288 int n = 0;
11289 int flags;
11290 garray_T *gap;
11291 idx_T arridx;
11292 int len;
11293 char_u *p;
11294 fromto_T *ftp;
11295 int fl = 0, tl;
11296 int repextra = 0; /* extra bytes in fword[] from REP item */
11297 slang_T *slang = lp->lp_slang;
11298 int fword_ends;
11299 int goodword_ends;
11300#ifdef DEBUG_TRIEWALK
11301 /* Stores the name of the change made at each level. */
11302 char_u changename[MAXWLEN][80];
11303#endif
11304 int breakcheckcount = 1000;
11305 int compound_ok;
11306
11307 /*
11308 * Go through the whole case-fold tree, try changes at each node.
11309 * "tword[]" contains the word collected from nodes in the tree.
11310 * "fword[]" the word we are trying to match with (initially the bad
11311 * word).
11312 */
11313 depth = 0;
11314 sp = &stack[0];
11315 vim_memset(sp, 0, sizeof(trystate_T));
11316 sp->ts_curi = 1;
11317
11318 if (soundfold)
11319 {
11320 /* Going through the soundfold tree. */
11321 byts = fbyts = slang->sl_sbyts;
11322 idxs = fidxs = slang->sl_sidxs;
11323 pbyts = NULL;
11324 pidxs = NULL;
11325 sp->ts_prefixdepth = PFD_NOPREFIX;
11326 sp->ts_state = STATE_START;
11327 }
11328 else
11329 {
Bram Moolenaarea424162005-06-16 21:51:00 +000011330 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011331 * When there are postponed prefixes we need to use these first. At
11332 * the end of the prefix we continue in the case-fold tree.
11333 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011334 fbyts = slang->sl_fbyts;
11335 fidxs = slang->sl_fidxs;
11336 pbyts = slang->sl_pbyts;
11337 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011338 if (pbyts != NULL)
11339 {
11340 byts = pbyts;
11341 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011342 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011343 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
11344 }
11345 else
11346 {
11347 byts = fbyts;
11348 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011349 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011350 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011351 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011352 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011353
Bram Moolenaar4770d092006-01-12 23:22:24 +000011354 /*
11355 * Loop to find all suggestions. At each round we either:
11356 * - For the current state try one operation, advance "ts_curi",
11357 * increase "depth".
11358 * - When a state is done go to the next, set "ts_state".
11359 * - When all states are tried decrease "depth".
11360 */
11361 while (depth >= 0 && !got_int)
11362 {
11363 sp = &stack[depth];
11364 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011365 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011366 case STATE_START:
11367 case STATE_NOPREFIX:
11368 /*
11369 * Start of node: Deal with NUL bytes, which means
11370 * tword[] may end here.
11371 */
11372 arridx = sp->ts_arridx; /* current node in the tree */
11373 len = byts[arridx]; /* bytes in this node */
11374 arridx += sp->ts_curi; /* index of current byte */
11375
11376 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011377 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011378 /* Skip over the NUL bytes, we use them later. */
11379 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
11380 ;
11381 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011382
Bram Moolenaar4770d092006-01-12 23:22:24 +000011383 /* Always past NUL bytes now. */
11384 n = (int)sp->ts_state;
11385 sp->ts_state = STATE_ENDNUL;
11386 sp->ts_save_badflags = su->su_badflags;
11387
11388 /* At end of a prefix or at start of prefixtree: check for
11389 * following word. */
11390 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011391 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011392 /* Set su->su_badflags to the caps type at this position.
11393 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000011394#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011395 if (has_mbyte)
11396 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
11397 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000011398#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011399 n = sp->ts_fidx;
11400 flags = badword_captype(su->su_badptr, su->su_badptr + n);
11401 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011402 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011403#ifdef DEBUG_TRIEWALK
11404 sprintf(changename[depth], "prefix");
11405#endif
11406 go_deeper(stack, depth, 0);
11407 ++depth;
11408 sp = &stack[depth];
11409 sp->ts_prefixdepth = depth - 1;
11410 byts = fbyts;
11411 idxs = fidxs;
11412 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011413
Bram Moolenaar4770d092006-01-12 23:22:24 +000011414 /* Move the prefix to preword[] with the right case
11415 * and make find_keepcap_word() works. */
11416 tword[sp->ts_twordlen] = NUL;
11417 make_case_word(tword + sp->ts_splitoff,
11418 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011419 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011420 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011421 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011422 break;
11423 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011424
Bram Moolenaar4770d092006-01-12 23:22:24 +000011425 if (sp->ts_curi > len || byts[arridx] != 0)
11426 {
11427 /* Past bytes in node and/or past NUL bytes. */
11428 sp->ts_state = STATE_ENDNUL;
11429 sp->ts_save_badflags = su->su_badflags;
11430 break;
11431 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011432
Bram Moolenaar4770d092006-01-12 23:22:24 +000011433 /*
11434 * End of word in tree.
11435 */
11436 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011437
Bram Moolenaar4770d092006-01-12 23:22:24 +000011438 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011439
11440 /* Skip words with the NOSUGGEST flag. */
11441 if (flags & WF_NOSUGGEST)
11442 break;
11443
Bram Moolenaar4770d092006-01-12 23:22:24 +000011444 fword_ends = (fword[sp->ts_fidx] == NUL
11445 || (soundfold
11446 ? vim_iswhite(fword[sp->ts_fidx])
Bram Moolenaar860cae12010-06-05 23:22:07 +020011447 : !spell_iswordp(fword + sp->ts_fidx, curwin)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000011448 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011449
Bram Moolenaar4770d092006-01-12 23:22:24 +000011450 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000011451 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011452 {
11453 /* There was a prefix before the word. Check that the prefix
11454 * can be used with this word. */
11455 /* Count the length of the NULs in the prefix. If there are
11456 * none this must be the first try without a prefix. */
11457 n = stack[sp->ts_prefixdepth].ts_arridx;
11458 len = pbyts[n++];
11459 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
11460 ;
11461 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011462 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011463 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011464 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011465 if (c == 0)
11466 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011467
Bram Moolenaar4770d092006-01-12 23:22:24 +000011468 /* Use the WF_RARE flag for a rare prefix. */
11469 if (c & WF_RAREPFX)
11470 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011471
Bram Moolenaar4770d092006-01-12 23:22:24 +000011472 /* Tricky: when checking for both prefix and compounding
11473 * we run into the prefix flag first.
11474 * Remember that it's OK, so that we accept the prefix
11475 * when arriving at a compound flag. */
11476 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011477 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011478 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011479
Bram Moolenaar4770d092006-01-12 23:22:24 +000011480 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
11481 * appending another compound word below. */
11482 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011483 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011484 goodword_ends = FALSE;
11485 else
11486 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011487
Bram Moolenaar4770d092006-01-12 23:22:24 +000011488 p = NULL;
11489 compound_ok = TRUE;
11490 if (sp->ts_complen > sp->ts_compsplit)
11491 {
11492 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000011493 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011494 /* There was a word before this word. When there was no
11495 * change in this word (it was correct) add the first word
11496 * as a suggestion. If this word was corrected too, we
11497 * need to check if a correct word follows. */
11498 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000011499 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000011500 && STRNCMP(fword + sp->ts_splitfidx,
11501 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000011502 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011503 {
11504 preword[sp->ts_prewordlen] = NUL;
11505 newscore = score_wordcount_adj(slang, sp->ts_score,
11506 preword + sp->ts_prewordlen,
11507 sp->ts_prewordlen > 0);
11508 /* Add the suggestion if the score isn't too bad. */
11509 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000011510 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011511 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011512 newscore, 0, FALSE,
11513 lp->lp_sallang, FALSE);
11514 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000011515 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011516 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000011517 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000011518 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011519 /* There was a compound word before this word. If this
11520 * word does not support compounding then give up
11521 * (splitting is tried for the word without compound
11522 * flag). */
11523 if (((unsigned)flags >> 24) == 0
11524 || sp->ts_twordlen - sp->ts_splitoff
11525 < slang->sl_compminlen)
11526 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011527#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011528 /* For multi-byte chars check character length against
11529 * COMPOUNDMIN. */
11530 if (has_mbyte
11531 && slang->sl_compminlen > 0
11532 && mb_charlen(tword + sp->ts_splitoff)
11533 < slang->sl_compminlen)
11534 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011535#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000011536
Bram Moolenaar4770d092006-01-12 23:22:24 +000011537 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11538 compflags[sp->ts_complen + 1] = NUL;
11539 vim_strncpy(preword + sp->ts_prewordlen,
11540 tword + sp->ts_splitoff,
11541 sp->ts_twordlen - sp->ts_splitoff);
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011542
11543 /* Verify CHECKCOMPOUNDPATTERN rules. */
11544 if (match_checkcompoundpattern(preword, sp->ts_prewordlen,
11545 &slang->sl_comppat))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011546 compound_ok = FALSE;
11547
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011548 if (compound_ok)
11549 {
11550 p = preword;
11551 while (*skiptowhite(p) != NUL)
11552 p = skipwhite(skiptowhite(p));
11553 if (fword_ends && !can_compound(slang, p,
11554 compflags + sp->ts_compsplit))
11555 /* Compound is not allowed. But it may still be
11556 * possible if we add another (short) word. */
11557 compound_ok = FALSE;
11558 }
11559
Bram Moolenaar4770d092006-01-12 23:22:24 +000011560 /* Get pointer to last char of previous word. */
11561 p = preword + sp->ts_prewordlen;
11562 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011563 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011564 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011565
Bram Moolenaar4770d092006-01-12 23:22:24 +000011566 /*
11567 * Form the word with proper case in preword.
11568 * If there is a word from a previous split, append.
11569 * For the soundfold tree don't change the case, simply append.
11570 */
11571 if (soundfold)
11572 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11573 else if (flags & WF_KEEPCAP)
11574 /* Must find the word in the keep-case tree. */
11575 find_keepcap_word(slang, tword + sp->ts_splitoff,
11576 preword + sp->ts_prewordlen);
11577 else
11578 {
11579 /* Include badflags: If the badword is onecap or allcap
11580 * use that for the goodword too. But if the badword is
11581 * allcap and it's only one char long use onecap. */
11582 c = su->su_badflags;
11583 if ((c & WF_ALLCAP)
11584#ifdef FEAT_MBYTE
11585 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11586#else
11587 && su->su_badlen == 1
11588#endif
11589 )
11590 c = WF_ONECAP;
11591 c |= flags;
11592
11593 /* When appending a compound word after a word character don't
11594 * use Onecap. */
11595 if (p != NULL && spell_iswordp_nmw(p))
11596 c &= ~WF_ONECAP;
11597 make_case_word(tword + sp->ts_splitoff,
11598 preword + sp->ts_prewordlen, c);
11599 }
11600
11601 if (!soundfold)
11602 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011603 /* Don't use a banned word. It may appear again as a good
11604 * word, thus remember it. */
11605 if (flags & WF_BANNED)
11606 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000011607 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011608 break;
11609 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011610 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000011611 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11612 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011613 {
11614 if (slang->sl_compprog == NULL)
11615 break;
11616 /* the word so far was banned but we may try compounding */
11617 goodword_ends = FALSE;
11618 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011619 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011620
Bram Moolenaar4770d092006-01-12 23:22:24 +000011621 newscore = 0;
11622 if (!soundfold) /* soundfold words don't have flags */
11623 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011624 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011625 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011626 newscore += SCORE_REGION;
11627 if (flags & WF_RARE)
11628 newscore += SCORE_RARE;
11629
Bram Moolenaar0c405862005-06-22 22:26:26 +000011630 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011631 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011632 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011633 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011634
Bram Moolenaar4770d092006-01-12 23:22:24 +000011635 /* TODO: how about splitting in the soundfold tree? */
11636 if (fword_ends
11637 && goodword_ends
11638 && sp->ts_fidx >= sp->ts_fidxtry
11639 && compound_ok)
11640 {
11641 /* The badword also ends: add suggestions. */
11642#ifdef DEBUG_TRIEWALK
11643 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011644 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011645 int j;
11646
11647 /* print the stack of changes that brought us here */
11648 smsg("------ %s -------", fword);
11649 for (j = 0; j < depth; ++j)
11650 smsg("%s", changename[j]);
11651 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011652#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011653 if (soundfold)
11654 {
11655 /* For soundfolded words we need to find the original
Bram Moolenaarf711faf2007-05-10 16:48:19 +000011656 * words, the edit distance and then add them. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011657 add_sound_suggest(su, preword, sp->ts_score, lp);
11658 }
11659 else
11660 {
11661 /* Give a penalty when changing non-word char to word
11662 * char, e.g., "thes," -> "these". */
11663 p = fword + sp->ts_fidx;
11664 mb_ptr_back(fword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020011665 if (!spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011666 {
11667 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011668 mb_ptr_back(preword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020011669 if (spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011670 newscore += SCORE_NONWORD;
11671 }
11672
Bram Moolenaar4770d092006-01-12 23:22:24 +000011673 /* Give a bonus to words seen before. */
11674 score = score_wordcount_adj(slang,
11675 sp->ts_score + newscore,
11676 preword + sp->ts_prewordlen,
11677 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011678
Bram Moolenaar4770d092006-01-12 23:22:24 +000011679 /* Add the suggestion if the score isn't too bad. */
11680 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011681 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011682 add_suggestion(su, &su->su_ga, preword,
11683 sp->ts_fidx - repextra,
11684 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011685
11686 if (su->su_badflags & WF_MIXCAP)
11687 {
11688 /* We really don't know if the word should be
11689 * upper or lower case, add both. */
11690 c = captype(preword, NULL);
11691 if (c == 0 || c == WF_ALLCAP)
11692 {
11693 make_case_word(tword + sp->ts_splitoff,
11694 preword + sp->ts_prewordlen,
11695 c == 0 ? WF_ALLCAP : 0);
11696
11697 add_suggestion(su, &su->su_ga, preword,
11698 sp->ts_fidx - repextra,
11699 score + SCORE_ICASE, 0, FALSE,
11700 lp->lp_sallang, FALSE);
11701 }
11702 }
11703 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011704 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011705 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011706
Bram Moolenaar4770d092006-01-12 23:22:24 +000011707 /*
11708 * Try word split and/or compounding.
11709 */
11710 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011711#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011712 /* Don't split halfway a character. */
11713 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011714#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011715 )
11716 {
11717 int try_compound;
11718 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011719
Bram Moolenaar4770d092006-01-12 23:22:24 +000011720 /* If past the end of the bad word don't try a split.
11721 * Otherwise try changing the next word. E.g., find
11722 * suggestions for "the the" where the second "the" is
11723 * different. It's done like a split.
11724 * TODO: word split for soundfold words */
11725 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11726 && !soundfold;
11727
11728 /* Get here in several situations:
11729 * 1. The word in the tree ends:
11730 * If the word allows compounding try that. Otherwise try
11731 * a split by inserting a space. For both check that a
11732 * valid words starts at fword[sp->ts_fidx].
11733 * For NOBREAK do like compounding to be able to check if
11734 * the next word is valid.
11735 * 2. The badword does end, but it was due to a change (e.g.,
11736 * a swap). No need to split, but do check that the
11737 * following word is valid.
11738 * 3. The badword and the word in the tree end. It may still
11739 * be possible to compound another (short) word.
11740 */
11741 try_compound = FALSE;
11742 if (!soundfold
11743 && slang->sl_compprog != NULL
11744 && ((unsigned)flags >> 24) != 0
11745 && sp->ts_twordlen - sp->ts_splitoff
11746 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011747#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011748 && (!has_mbyte
11749 || slang->sl_compminlen == 0
11750 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011751 >= slang->sl_compminlen)
11752#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011753 && (slang->sl_compsylmax < MAXWLEN
11754 || sp->ts_complen + 1 - sp->ts_compsplit
11755 < slang->sl_compmax)
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011756 && (can_be_compound(sp, slang,
11757 compflags, ((unsigned)flags >> 24))))
11758
Bram Moolenaar4770d092006-01-12 23:22:24 +000011759 {
11760 try_compound = TRUE;
11761 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11762 compflags[sp->ts_complen + 1] = NUL;
11763 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011764
Bram Moolenaar4770d092006-01-12 23:22:24 +000011765 /* For NOBREAK we never try splitting, it won't make any word
11766 * valid. */
11767 if (slang->sl_nobreak)
11768 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011769
Bram Moolenaar4770d092006-01-12 23:22:24 +000011770 /* If we could add a compound word, and it's also possible to
11771 * split at this point, do the split first and set
11772 * TSF_DIDSPLIT to avoid doing it again. */
11773 else if (!fword_ends
11774 && try_compound
11775 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11776 {
11777 try_compound = FALSE;
11778 sp->ts_flags |= TSF_DIDSPLIT;
11779 --sp->ts_curi; /* do the same NUL again */
11780 compflags[sp->ts_complen] = NUL;
11781 }
11782 else
11783 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011784
Bram Moolenaar4770d092006-01-12 23:22:24 +000011785 if (try_split || try_compound)
11786 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011787 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011788 {
11789 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011790 * words so far are valid for compounding. If there
11791 * is only one word it must not have the NEEDCOMPOUND
11792 * flag. */
11793 if (sp->ts_complen == sp->ts_compsplit
11794 && (flags & WF_NEEDCOMP))
11795 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011796 p = preword;
11797 while (*skiptowhite(p) != NUL)
11798 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011799 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011800 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011801 compflags + sp->ts_compsplit))
11802 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011803
11804 if (slang->sl_nosplitsugs)
11805 newscore += SCORE_SPLIT_NO;
11806 else
11807 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011808
11809 /* Give a bonus to words seen before. */
11810 newscore = score_wordcount_adj(slang, newscore,
11811 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011812 }
11813
Bram Moolenaar4770d092006-01-12 23:22:24 +000011814 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011815 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011816 go_deeper(stack, depth, newscore);
11817#ifdef DEBUG_TRIEWALK
11818 if (!try_compound && !fword_ends)
11819 sprintf(changename[depth], "%.*s-%s: split",
11820 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11821 else
11822 sprintf(changename[depth], "%.*s-%s: compound",
11823 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11824#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011825 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011826 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011827 sp->ts_state = STATE_SPLITUNDO;
11828
11829 ++depth;
11830 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011831
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011832 /* Append a space to preword when splitting. */
11833 if (!try_compound && !fword_ends)
11834 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011835 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011836 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011837 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011838
11839 /* If the badword has a non-word character at this
11840 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011841 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011842 * character when the word ends. But only when the
11843 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011844 if (((!try_compound && !spell_iswordp_nmw(fword
11845 + sp->ts_fidx))
11846 || fword_ends)
11847 && fword[sp->ts_fidx] != NUL
11848 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011849 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011850 int l;
11851
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011852#ifdef FEAT_MBYTE
11853 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011854 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011855 else
11856#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011857 l = 1;
11858 if (fword_ends)
11859 {
11860 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011861 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011862 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011863 sp->ts_prewordlen += l;
11864 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011865 }
11866 else
11867 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11868 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011869 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011870
Bram Moolenaard12a1322005-08-21 22:08:24 +000011871 /* When compounding include compound flag in
11872 * compflags[] (already set above). When splitting we
11873 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011874 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011875 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011876 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011877 sp->ts_compsplit = sp->ts_complen;
11878 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011879
Bram Moolenaar53805d12005-08-01 07:08:33 +000011880 /* set su->su_badflags to the caps type at this
11881 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011882#ifdef FEAT_MBYTE
11883 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011884 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011885 else
11886#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011887 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011888 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011889 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011890
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011891 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011892 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011893
11894 /* If there are postponed prefixes, try these too. */
11895 if (pbyts != NULL)
11896 {
11897 byts = pbyts;
11898 idxs = pidxs;
11899 sp->ts_prefixdepth = PFD_PREFIXTREE;
11900 sp->ts_state = STATE_NOPREFIX;
11901 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011902 }
11903 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011904 }
11905 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011906
Bram Moolenaar4770d092006-01-12 23:22:24 +000011907 case STATE_SPLITUNDO:
11908 /* Undo the changes done for word split or compound word. */
11909 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011910
Bram Moolenaar4770d092006-01-12 23:22:24 +000011911 /* Continue looking for NUL bytes. */
11912 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011913
Bram Moolenaar4770d092006-01-12 23:22:24 +000011914 /* In case we went into the prefix tree. */
11915 byts = fbyts;
11916 idxs = fidxs;
11917 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011918
Bram Moolenaar4770d092006-01-12 23:22:24 +000011919 case STATE_ENDNUL:
11920 /* Past the NUL bytes in the node. */
11921 su->su_badflags = sp->ts_save_badflags;
11922 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011923#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011924 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011925#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011926 )
11927 {
11928 /* The badword ends, can't use STATE_PLAIN. */
11929 sp->ts_state = STATE_DEL;
11930 break;
11931 }
11932 sp->ts_state = STATE_PLAIN;
11933 /*FALLTHROUGH*/
11934
11935 case STATE_PLAIN:
11936 /*
11937 * Go over all possible bytes at this node, add each to tword[]
11938 * and use child node. "ts_curi" is the index.
11939 */
11940 arridx = sp->ts_arridx;
11941 if (sp->ts_curi > byts[arridx])
11942 {
11943 /* Done all bytes at this node, do next state. When still at
11944 * already changed bytes skip the other tricks. */
11945 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011946 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011947 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011948 sp->ts_state = STATE_FINAL;
11949 }
11950 else
11951 {
11952 arridx += sp->ts_curi++;
11953 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011954
Bram Moolenaar4770d092006-01-12 23:22:24 +000011955 /* Normal byte, go one level deeper. If it's not equal to the
11956 * byte in the bad word adjust the score. But don't even try
11957 * when the byte was already changed. And don't try when we
11958 * just deleted this byte, accepting it is always cheaper then
11959 * delete + substitute. */
11960 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000011961#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011962 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011963#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011964 )
11965 newscore = 0;
11966 else
11967 newscore = SCORE_SUBST;
11968 if ((newscore == 0
11969 || (sp->ts_fidx >= sp->ts_fidxtry
11970 && ((sp->ts_flags & TSF_DIDDEL) == 0
11971 || c != fword[sp->ts_delidx])))
11972 && TRY_DEEPER(su, stack, depth, newscore))
11973 {
11974 go_deeper(stack, depth, newscore);
11975#ifdef DEBUG_TRIEWALK
11976 if (newscore > 0)
11977 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
11978 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11979 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011980 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011981 sprintf(changename[depth], "%.*s-%s: accept %c",
11982 sp->ts_twordlen, tword, fword + sp->ts_fidx,
11983 fword[sp->ts_fidx]);
11984#endif
11985 ++depth;
11986 sp = &stack[depth];
11987 ++sp->ts_fidx;
11988 tword[sp->ts_twordlen++] = c;
11989 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000011990#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011991 if (newscore == SCORE_SUBST)
11992 sp->ts_isdiff = DIFF_YES;
11993 if (has_mbyte)
11994 {
11995 /* Multi-byte characters are a bit complicated to
11996 * handle: They differ when any of the bytes differ
11997 * and then their length may also differ. */
11998 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011999 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012000 /* First byte. */
12001 sp->ts_tcharidx = 0;
12002 sp->ts_tcharlen = MB_BYTE2LEN(c);
12003 sp->ts_fcharstart = sp->ts_fidx - 1;
12004 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000012005 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012006 }
12007 else if (sp->ts_isdiff == DIFF_INSERT)
12008 /* When inserting trail bytes don't advance in the
12009 * bad word. */
12010 --sp->ts_fidx;
12011 if (++sp->ts_tcharidx == sp->ts_tcharlen)
12012 {
12013 /* Last byte of character. */
12014 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000012015 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012016 /* Correct ts_fidx for the byte length of the
12017 * character (we didn't check that before). */
12018 sp->ts_fidx = sp->ts_fcharstart
12019 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000012020 fword[sp->ts_fcharstart]);
12021
Bram Moolenaar4770d092006-01-12 23:22:24 +000012022 /* For changing a composing character adjust
12023 * the score from SCORE_SUBST to
12024 * SCORE_SUBCOMP. */
12025 if (enc_utf8
12026 && utf_iscomposing(
12027 mb_ptr2char(tword
12028 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012029 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012030 && utf_iscomposing(
12031 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012032 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012033 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012034 SCORE_SUBST - SCORE_SUBCOMP;
12035
Bram Moolenaar4770d092006-01-12 23:22:24 +000012036 /* For a similar character adjust score from
12037 * SCORE_SUBST to SCORE_SIMILAR. */
12038 else if (!soundfold
12039 && slang->sl_has_map
12040 && similar_chars(slang,
12041 mb_ptr2char(tword
12042 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000012043 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000012044 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000012045 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012046 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000012047 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000012048 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012049 else if (sp->ts_isdiff == DIFF_INSERT
12050 && sp->ts_twordlen > sp->ts_tcharlen)
12051 {
12052 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
12053 c = mb_ptr2char(p);
12054 if (enc_utf8 && utf_iscomposing(c))
12055 {
12056 /* Inserting a composing char doesn't
12057 * count that much. */
12058 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
12059 }
12060 else
12061 {
12062 /* If the previous character was the same,
12063 * thus doubling a character, give a bonus
12064 * to the score. Also for the soundfold
12065 * tree (might seem illogical but does
12066 * give better scores). */
12067 mb_ptr_back(tword, p);
12068 if (c == mb_ptr2char(p))
12069 sp->ts_score -= SCORE_INS
12070 - SCORE_INSDUP;
12071 }
12072 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012073
Bram Moolenaar4770d092006-01-12 23:22:24 +000012074 /* Starting a new char, reset the length. */
12075 sp->ts_tcharlen = 0;
12076 }
Bram Moolenaarea408852005-06-25 22:49:46 +000012077 }
Bram Moolenaarea424162005-06-16 21:51:00 +000012078 else
12079#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000012080 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012081 /* If we found a similar char adjust the score.
12082 * We do this after calling go_deeper() because
12083 * it's slow. */
12084 if (newscore != 0
12085 && !soundfold
12086 && slang->sl_has_map
12087 && similar_chars(slang,
12088 c, fword[sp->ts_fidx - 1]))
12089 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000012090 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012091 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012092 }
12093 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012094
Bram Moolenaar4770d092006-01-12 23:22:24 +000012095 case STATE_DEL:
12096#ifdef FEAT_MBYTE
12097 /* When past the first byte of a multi-byte char don't try
12098 * delete/insert/swap a character. */
12099 if (has_mbyte && sp->ts_tcharlen > 0)
12100 {
12101 sp->ts_state = STATE_FINAL;
12102 break;
12103 }
12104#endif
12105 /*
12106 * Try skipping one character in the bad word (delete it).
12107 */
12108 sp->ts_state = STATE_INS_PREP;
12109 sp->ts_curi = 1;
12110 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
12111 /* Deleting a vowel at the start of a word counts less, see
12112 * soundalike_score(). */
12113 newscore = 2 * SCORE_DEL / 3;
12114 else
12115 newscore = SCORE_DEL;
12116 if (fword[sp->ts_fidx] != NUL
12117 && TRY_DEEPER(su, stack, depth, newscore))
12118 {
12119 go_deeper(stack, depth, newscore);
12120#ifdef DEBUG_TRIEWALK
12121 sprintf(changename[depth], "%.*s-%s: delete %c",
12122 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12123 fword[sp->ts_fidx]);
12124#endif
12125 ++depth;
12126
12127 /* Remember what character we deleted, so that we can avoid
12128 * inserting it again. */
12129 stack[depth].ts_flags |= TSF_DIDDEL;
12130 stack[depth].ts_delidx = sp->ts_fidx;
12131
12132 /* Advance over the character in fword[]. Give a bonus to the
12133 * score if the same character is following "nn" -> "n". It's
12134 * a bit illogical for soundfold tree but it does give better
12135 * results. */
12136#ifdef FEAT_MBYTE
12137 if (has_mbyte)
12138 {
12139 c = mb_ptr2char(fword + sp->ts_fidx);
12140 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
12141 if (enc_utf8 && utf_iscomposing(c))
12142 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
12143 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
12144 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12145 }
12146 else
12147#endif
12148 {
12149 ++stack[depth].ts_fidx;
12150 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
12151 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12152 }
12153 break;
12154 }
12155 /*FALLTHROUGH*/
12156
12157 case STATE_INS_PREP:
12158 if (sp->ts_flags & TSF_DIDDEL)
12159 {
12160 /* If we just deleted a byte then inserting won't make sense,
12161 * a substitute is always cheaper. */
12162 sp->ts_state = STATE_SWAP;
12163 break;
12164 }
12165
12166 /* skip over NUL bytes */
12167 n = sp->ts_arridx;
12168 for (;;)
12169 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012170 if (sp->ts_curi > byts[n])
12171 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012172 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012173 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012174 break;
12175 }
12176 if (byts[n + sp->ts_curi] != NUL)
12177 {
12178 /* Found a byte to insert. */
12179 sp->ts_state = STATE_INS;
12180 break;
12181 }
12182 ++sp->ts_curi;
12183 }
12184 break;
12185
12186 /*FALLTHROUGH*/
12187
12188 case STATE_INS:
12189 /* Insert one byte. Repeat this for each possible byte at this
12190 * node. */
12191 n = sp->ts_arridx;
12192 if (sp->ts_curi > byts[n])
12193 {
12194 /* Done all bytes at this node, go to next state. */
12195 sp->ts_state = STATE_SWAP;
12196 break;
12197 }
12198
12199 /* Do one more byte at this node, but:
12200 * - Skip NUL bytes.
12201 * - Skip the byte if it's equal to the byte in the word,
12202 * accepting that byte is always better.
12203 */
12204 n += sp->ts_curi++;
12205 c = byts[n];
12206 if (soundfold && sp->ts_twordlen == 0 && c == '*')
12207 /* Inserting a vowel at the start of a word counts less,
12208 * see soundalike_score(). */
12209 newscore = 2 * SCORE_INS / 3;
12210 else
12211 newscore = SCORE_INS;
12212 if (c != fword[sp->ts_fidx]
12213 && TRY_DEEPER(su, stack, depth, newscore))
12214 {
12215 go_deeper(stack, depth, newscore);
12216#ifdef DEBUG_TRIEWALK
12217 sprintf(changename[depth], "%.*s-%s: insert %c",
12218 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12219 c);
12220#endif
12221 ++depth;
12222 sp = &stack[depth];
12223 tword[sp->ts_twordlen++] = c;
12224 sp->ts_arridx = idxs[n];
12225#ifdef FEAT_MBYTE
12226 if (has_mbyte)
12227 {
12228 fl = MB_BYTE2LEN(c);
12229 if (fl > 1)
12230 {
12231 /* There are following bytes for the same character.
12232 * We must find all bytes before trying
12233 * delete/insert/swap/etc. */
12234 sp->ts_tcharlen = fl;
12235 sp->ts_tcharidx = 1;
12236 sp->ts_isdiff = DIFF_INSERT;
12237 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012238 }
12239 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012240 fl = 1;
12241 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000012242#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012243 {
12244 /* If the previous character was the same, thus doubling a
12245 * character, give a bonus to the score. Also for
12246 * soundfold words (illogical but does give a better
12247 * score). */
12248 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000012249 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012250 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012251 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012252 }
12253 break;
12254
12255 case STATE_SWAP:
12256 /*
12257 * Swap two bytes in the bad word: "12" -> "21".
12258 * We change "fword" here, it's changed back afterwards at
12259 * STATE_UNSWAP.
12260 */
12261 p = fword + sp->ts_fidx;
12262 c = *p;
12263 if (c == NUL)
12264 {
12265 /* End of word, can't swap or replace. */
12266 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012267 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012268 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012269
Bram Moolenaar4770d092006-01-12 23:22:24 +000012270 /* Don't swap if the first character is not a word character.
12271 * SWAP3 etc. also don't make sense then. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020012272 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012273 {
12274 sp->ts_state = STATE_REP_INI;
12275 break;
12276 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012277
Bram Moolenaar4770d092006-01-12 23:22:24 +000012278#ifdef FEAT_MBYTE
12279 if (has_mbyte)
12280 {
12281 n = mb_cptr2len(p);
12282 c = mb_ptr2char(p);
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012283 if (p[n] == NUL)
12284 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +020012285 else if (!soundfold && !spell_iswordp(p + n, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012286 c2 = c; /* don't swap non-word char */
12287 else
12288 c2 = mb_ptr2char(p + n);
12289 }
12290 else
12291#endif
12292 {
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012293 if (p[1] == NUL)
12294 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +020012295 else if (!soundfold && !spell_iswordp(p + 1, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012296 c2 = c; /* don't swap non-word char */
12297 else
12298 c2 = p[1];
12299 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012300
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012301 /* When the second character is NUL we can't swap. */
12302 if (c2 == NUL)
12303 {
12304 sp->ts_state = STATE_REP_INI;
12305 break;
12306 }
12307
Bram Moolenaar4770d092006-01-12 23:22:24 +000012308 /* When characters are identical, swap won't do anything.
12309 * Also get here if the second char is not a word character. */
12310 if (c == c2)
12311 {
12312 sp->ts_state = STATE_SWAP3;
12313 break;
12314 }
12315 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
12316 {
12317 go_deeper(stack, depth, SCORE_SWAP);
12318#ifdef DEBUG_TRIEWALK
12319 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
12320 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12321 c, c2);
12322#endif
12323 sp->ts_state = STATE_UNSWAP;
12324 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012325#ifdef FEAT_MBYTE
12326 if (has_mbyte)
12327 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012328 fl = mb_char2len(c2);
12329 mch_memmove(p, p + n, fl);
12330 mb_char2bytes(c, p + fl);
12331 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012332 }
12333 else
12334#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012335 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012336 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012337 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012338 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012339 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012340 }
12341 else
12342 /* If this swap doesn't work then SWAP3 won't either. */
12343 sp->ts_state = STATE_REP_INI;
12344 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000012345
Bram Moolenaar4770d092006-01-12 23:22:24 +000012346 case STATE_UNSWAP:
12347 /* Undo the STATE_SWAP swap: "21" -> "12". */
12348 p = fword + sp->ts_fidx;
12349#ifdef FEAT_MBYTE
12350 if (has_mbyte)
12351 {
12352 n = MB_BYTE2LEN(*p);
12353 c = mb_ptr2char(p + n);
12354 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
12355 mb_char2bytes(c, p);
12356 }
12357 else
12358#endif
12359 {
12360 c = *p;
12361 *p = p[1];
12362 p[1] = c;
12363 }
12364 /*FALLTHROUGH*/
12365
12366 case STATE_SWAP3:
12367 /* Swap two bytes, skipping one: "123" -> "321". We change
12368 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
12369 p = fword + sp->ts_fidx;
12370#ifdef FEAT_MBYTE
12371 if (has_mbyte)
12372 {
12373 n = mb_cptr2len(p);
12374 c = mb_ptr2char(p);
12375 fl = mb_cptr2len(p + n);
12376 c2 = mb_ptr2char(p + n);
Bram Moolenaar860cae12010-06-05 23:22:07 +020012377 if (!soundfold && !spell_iswordp(p + n + fl, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012378 c3 = c; /* don't swap non-word char */
12379 else
12380 c3 = mb_ptr2char(p + n + fl);
12381 }
12382 else
12383#endif
12384 {
12385 c = *p;
12386 c2 = p[1];
Bram Moolenaar860cae12010-06-05 23:22:07 +020012387 if (!soundfold && !spell_iswordp(p + 2, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012388 c3 = c; /* don't swap non-word char */
12389 else
12390 c3 = p[2];
12391 }
12392
12393 /* When characters are identical: "121" then SWAP3 result is
12394 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
12395 * same as SWAP on next char: "112". Thus skip all swapping.
12396 * Also skip when c3 is NUL.
12397 * Also get here when the third character is not a word character.
12398 * Second character may any char: "a.b" -> "b.a" */
12399 if (c == c3 || c3 == NUL)
12400 {
12401 sp->ts_state = STATE_REP_INI;
12402 break;
12403 }
12404 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12405 {
12406 go_deeper(stack, depth, SCORE_SWAP3);
12407#ifdef DEBUG_TRIEWALK
12408 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
12409 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12410 c, c3);
12411#endif
12412 sp->ts_state = STATE_UNSWAP3;
12413 ++depth;
12414#ifdef FEAT_MBYTE
12415 if (has_mbyte)
12416 {
12417 tl = mb_char2len(c3);
12418 mch_memmove(p, p + n + fl, tl);
12419 mb_char2bytes(c2, p + tl);
12420 mb_char2bytes(c, p + fl + tl);
12421 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
12422 }
12423 else
12424#endif
12425 {
12426 p[0] = p[2];
12427 p[2] = c;
12428 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12429 }
12430 }
12431 else
12432 sp->ts_state = STATE_REP_INI;
12433 break;
12434
12435 case STATE_UNSWAP3:
12436 /* Undo STATE_SWAP3: "321" -> "123" */
12437 p = fword + sp->ts_fidx;
12438#ifdef FEAT_MBYTE
12439 if (has_mbyte)
12440 {
12441 n = MB_BYTE2LEN(*p);
12442 c2 = mb_ptr2char(p + n);
12443 fl = MB_BYTE2LEN(p[n]);
12444 c = mb_ptr2char(p + n + fl);
12445 tl = MB_BYTE2LEN(p[n + fl]);
12446 mch_memmove(p + fl + tl, p, n);
12447 mb_char2bytes(c, p);
12448 mb_char2bytes(c2, p + tl);
12449 p = p + tl;
12450 }
12451 else
12452#endif
12453 {
12454 c = *p;
12455 *p = p[2];
12456 p[2] = c;
12457 ++p;
12458 }
12459
Bram Moolenaar860cae12010-06-05 23:22:07 +020012460 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012461 {
12462 /* Middle char is not a word char, skip the rotate. First and
12463 * third char were already checked at swap and swap3. */
12464 sp->ts_state = STATE_REP_INI;
12465 break;
12466 }
12467
12468 /* Rotate three characters left: "123" -> "231". We change
12469 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
12470 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12471 {
12472 go_deeper(stack, depth, SCORE_SWAP3);
12473#ifdef DEBUG_TRIEWALK
12474 p = fword + sp->ts_fidx;
12475 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
12476 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12477 p[0], p[1], p[2]);
12478#endif
12479 sp->ts_state = STATE_UNROT3L;
12480 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012481 p = fword + sp->ts_fidx;
12482#ifdef FEAT_MBYTE
12483 if (has_mbyte)
12484 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012485 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012486 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012487 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012488 fl += mb_cptr2len(p + n + fl);
12489 mch_memmove(p, p + n, fl);
12490 mb_char2bytes(c, p + fl);
12491 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012492 }
12493 else
12494#endif
12495 {
12496 c = *p;
12497 *p = p[1];
12498 p[1] = p[2];
12499 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012500 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000012501 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012502 }
12503 else
12504 sp->ts_state = STATE_REP_INI;
12505 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012506
Bram Moolenaar4770d092006-01-12 23:22:24 +000012507 case STATE_UNROT3L:
12508 /* Undo ROT3L: "231" -> "123" */
12509 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000012510#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012511 if (has_mbyte)
12512 {
12513 n = MB_BYTE2LEN(*p);
12514 n += MB_BYTE2LEN(p[n]);
12515 c = mb_ptr2char(p + n);
12516 tl = MB_BYTE2LEN(p[n]);
12517 mch_memmove(p + tl, p, n);
12518 mb_char2bytes(c, p);
12519 }
12520 else
Bram Moolenaarea424162005-06-16 21:51:00 +000012521#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012522 {
12523 c = p[2];
12524 p[2] = p[1];
12525 p[1] = *p;
12526 *p = c;
12527 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012528
Bram Moolenaar4770d092006-01-12 23:22:24 +000012529 /* Rotate three bytes right: "123" -> "312". We change "fword"
12530 * here, it's changed back afterwards at STATE_UNROT3R. */
12531 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12532 {
12533 go_deeper(stack, depth, SCORE_SWAP3);
12534#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012535 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012536 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
12537 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12538 p[0], p[1], p[2]);
12539#endif
12540 sp->ts_state = STATE_UNROT3R;
12541 ++depth;
12542 p = fword + sp->ts_fidx;
12543#ifdef FEAT_MBYTE
12544 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012545 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012546 n = mb_cptr2len(p);
12547 n += mb_cptr2len(p + n);
12548 c = mb_ptr2char(p + n);
12549 tl = mb_cptr2len(p + n);
12550 mch_memmove(p + tl, p, n);
12551 mb_char2bytes(c, p);
12552 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012553 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012554 else
12555#endif
12556 {
12557 c = p[2];
12558 p[2] = p[1];
12559 p[1] = *p;
12560 *p = c;
12561 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12562 }
12563 }
12564 else
12565 sp->ts_state = STATE_REP_INI;
12566 break;
12567
12568 case STATE_UNROT3R:
12569 /* Undo ROT3R: "312" -> "123" */
12570 p = fword + sp->ts_fidx;
12571#ifdef FEAT_MBYTE
12572 if (has_mbyte)
12573 {
12574 c = mb_ptr2char(p);
12575 tl = MB_BYTE2LEN(*p);
12576 n = MB_BYTE2LEN(p[tl]);
12577 n += MB_BYTE2LEN(p[tl + n]);
12578 mch_memmove(p, p + tl, n);
12579 mb_char2bytes(c, p + n);
12580 }
12581 else
12582#endif
12583 {
12584 c = *p;
12585 *p = p[1];
12586 p[1] = p[2];
12587 p[2] = c;
12588 }
12589 /*FALLTHROUGH*/
12590
12591 case STATE_REP_INI:
12592 /* Check if matching with REP items from the .aff file would work.
12593 * Quickly skip if:
12594 * - there are no REP items and we are not in the soundfold trie
12595 * - the score is going to be too high anyway
12596 * - already applied a REP item or swapped here */
12597 if ((lp->lp_replang == NULL && !soundfold)
12598 || sp->ts_score + SCORE_REP >= su->su_maxscore
12599 || sp->ts_fidx < sp->ts_fidxtry)
12600 {
12601 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012602 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012603 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012604
Bram Moolenaar4770d092006-01-12 23:22:24 +000012605 /* Use the first byte to quickly find the first entry that may
12606 * match. If the index is -1 there is none. */
12607 if (soundfold)
12608 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12609 else
12610 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012611
Bram Moolenaar4770d092006-01-12 23:22:24 +000012612 if (sp->ts_curi < 0)
12613 {
12614 sp->ts_state = STATE_FINAL;
12615 break;
12616 }
12617
12618 sp->ts_state = STATE_REP;
12619 /*FALLTHROUGH*/
12620
12621 case STATE_REP:
12622 /* Try matching with REP items from the .aff file. For each match
12623 * replace the characters and check if the resulting word is
12624 * valid. */
12625 p = fword + sp->ts_fidx;
12626
12627 if (soundfold)
12628 gap = &slang->sl_repsal;
12629 else
12630 gap = &lp->lp_replang->sl_rep;
12631 while (sp->ts_curi < gap->ga_len)
12632 {
12633 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12634 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012635 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012636 /* past possible matching entries */
12637 sp->ts_curi = gap->ga_len;
12638 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012639 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012640 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12641 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12642 {
12643 go_deeper(stack, depth, SCORE_REP);
12644#ifdef DEBUG_TRIEWALK
12645 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12646 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12647 ftp->ft_from, ftp->ft_to);
12648#endif
12649 /* Need to undo this afterwards. */
12650 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012651
Bram Moolenaar4770d092006-01-12 23:22:24 +000012652 /* Change the "from" to the "to" string. */
12653 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012654 fl = (int)STRLEN(ftp->ft_from);
12655 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012656 if (fl != tl)
12657 {
Bram Moolenaara7241f52008-06-24 20:39:31 +000012658 STRMOVE(p + tl, p + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012659 repextra += tl - fl;
12660 }
12661 mch_memmove(p, ftp->ft_to, tl);
12662 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12663#ifdef FEAT_MBYTE
12664 stack[depth].ts_tcharlen = 0;
12665#endif
12666 break;
12667 }
12668 }
12669
12670 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
12671 /* No (more) matches. */
12672 sp->ts_state = STATE_FINAL;
12673
12674 break;
12675
12676 case STATE_REP_UNDO:
12677 /* Undo a REP replacement and continue with the next one. */
12678 if (soundfold)
12679 gap = &slang->sl_repsal;
12680 else
12681 gap = &lp->lp_replang->sl_rep;
12682 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012683 fl = (int)STRLEN(ftp->ft_from);
12684 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012685 p = fword + sp->ts_fidx;
12686 if (fl != tl)
12687 {
Bram Moolenaara7241f52008-06-24 20:39:31 +000012688 STRMOVE(p + fl, p + tl);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012689 repextra -= tl - fl;
12690 }
12691 mch_memmove(p, ftp->ft_from, fl);
12692 sp->ts_state = STATE_REP;
12693 break;
12694
12695 default:
12696 /* Did all possible states at this level, go up one level. */
12697 --depth;
12698
12699 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12700 {
12701 /* Continue in or go back to the prefix tree. */
12702 byts = pbyts;
12703 idxs = pidxs;
12704 }
12705
12706 /* Don't check for CTRL-C too often, it takes time. */
12707 if (--breakcheckcount == 0)
12708 {
12709 ui_breakcheck();
12710 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012711 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012712 }
12713 }
12714}
12715
Bram Moolenaar4770d092006-01-12 23:22:24 +000012716
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012717/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012718 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012719 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012720 static void
12721go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012722 trystate_T *stack;
12723 int depth;
12724 int score_add;
12725{
Bram Moolenaarea424162005-06-16 21:51:00 +000012726 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012727 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012728 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012729 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012730 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012731}
12732
Bram Moolenaar53805d12005-08-01 07:08:33 +000012733#ifdef FEAT_MBYTE
12734/*
12735 * Case-folding may change the number of bytes: Count nr of chars in
12736 * fword[flen] and return the byte length of that many chars in "word".
12737 */
12738 static int
12739nofold_len(fword, flen, word)
12740 char_u *fword;
12741 int flen;
12742 char_u *word;
12743{
12744 char_u *p;
12745 int i = 0;
12746
12747 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12748 ++i;
12749 for (p = word; i > 0; mb_ptr_adv(p))
12750 --i;
12751 return (int)(p - word);
12752}
12753#endif
12754
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012755/*
12756 * "fword" is a good word with case folded. Find the matching keep-case
12757 * words and put it in "kword".
12758 * Theoretically there could be several keep-case words that result in the
12759 * same case-folded word, but we only find one...
12760 */
12761 static void
12762find_keepcap_word(slang, fword, kword)
12763 slang_T *slang;
12764 char_u *fword;
12765 char_u *kword;
12766{
12767 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12768 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012769 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012770
12771 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012772 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012773 int round[MAXWLEN];
12774 int fwordidx[MAXWLEN];
12775 int uwordidx[MAXWLEN];
12776 int kwordlen[MAXWLEN];
12777
12778 int flen, ulen;
12779 int l;
12780 int len;
12781 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012782 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012783 char_u *p;
12784 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012785 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012786
12787 if (byts == NULL)
12788 {
12789 /* array is empty: "cannot happen" */
12790 *kword = NUL;
12791 return;
12792 }
12793
12794 /* Make an all-cap version of "fword". */
12795 allcap_copy(fword, uword);
12796
12797 /*
12798 * Each character needs to be tried both case-folded and upper-case.
12799 * All this gets very complicated if we keep in mind that changing case
12800 * may change the byte length of a multi-byte character...
12801 */
12802 depth = 0;
12803 arridx[0] = 0;
12804 round[0] = 0;
12805 fwordidx[0] = 0;
12806 uwordidx[0] = 0;
12807 kwordlen[0] = 0;
12808 while (depth >= 0)
12809 {
12810 if (fword[fwordidx[depth]] == NUL)
12811 {
12812 /* We are at the end of "fword". If the tree allows a word to end
12813 * here we have found a match. */
12814 if (byts[arridx[depth] + 1] == 0)
12815 {
12816 kword[kwordlen[depth]] = NUL;
12817 return;
12818 }
12819
12820 /* kword is getting too long, continue one level up */
12821 --depth;
12822 }
12823 else if (++round[depth] > 2)
12824 {
12825 /* tried both fold-case and upper-case character, continue one
12826 * level up */
12827 --depth;
12828 }
12829 else
12830 {
12831 /*
12832 * round[depth] == 1: Try using the folded-case character.
12833 * round[depth] == 2: Try using the upper-case character.
12834 */
12835#ifdef FEAT_MBYTE
12836 if (has_mbyte)
12837 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012838 flen = mb_cptr2len(fword + fwordidx[depth]);
12839 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012840 }
12841 else
12842#endif
12843 ulen = flen = 1;
12844 if (round[depth] == 1)
12845 {
12846 p = fword + fwordidx[depth];
12847 l = flen;
12848 }
12849 else
12850 {
12851 p = uword + uwordidx[depth];
12852 l = ulen;
12853 }
12854
12855 for (tryidx = arridx[depth]; l > 0; --l)
12856 {
12857 /* Perform a binary search in the list of accepted bytes. */
12858 len = byts[tryidx++];
12859 c = *p++;
12860 lo = tryidx;
12861 hi = tryidx + len - 1;
12862 while (lo < hi)
12863 {
12864 m = (lo + hi) / 2;
12865 if (byts[m] > c)
12866 hi = m - 1;
12867 else if (byts[m] < c)
12868 lo = m + 1;
12869 else
12870 {
12871 lo = hi = m;
12872 break;
12873 }
12874 }
12875
12876 /* Stop if there is no matching byte. */
12877 if (hi < lo || byts[lo] != c)
12878 break;
12879
12880 /* Continue at the child (if there is one). */
12881 tryidx = idxs[lo];
12882 }
12883
12884 if (l == 0)
12885 {
12886 /*
12887 * Found the matching char. Copy it to "kword" and go a
12888 * level deeper.
12889 */
12890 if (round[depth] == 1)
12891 {
12892 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12893 flen);
12894 kwordlen[depth + 1] = kwordlen[depth] + flen;
12895 }
12896 else
12897 {
12898 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12899 ulen);
12900 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12901 }
12902 fwordidx[depth + 1] = fwordidx[depth] + flen;
12903 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12904
12905 ++depth;
12906 arridx[depth] = tryidx;
12907 round[depth] = 0;
12908 }
12909 }
12910 }
12911
12912 /* Didn't find it: "cannot happen". */
12913 *kword = NUL;
12914}
12915
12916/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012917 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12918 * su->su_sga.
12919 */
12920 static void
12921score_comp_sal(su)
12922 suginfo_T *su;
12923{
12924 langp_T *lp;
12925 char_u badsound[MAXWLEN];
12926 int i;
12927 suggest_T *stp;
12928 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012929 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012930 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012931
12932 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
12933 return;
12934
12935 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020012936 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012937 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020012938 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012939 if (lp->lp_slang->sl_sal.ga_len > 0)
12940 {
12941 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012942 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012943
12944 for (i = 0; i < su->su_ga.ga_len; ++i)
12945 {
12946 stp = &SUG(su->su_ga, i);
12947
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012948 /* Case-fold the suggested word, sound-fold it and compute the
12949 * sound-a-like score. */
12950 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012951 if (score < SCORE_MAXMAX)
12952 {
12953 /* Add the suggestion. */
12954 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
12955 sstp->st_word = vim_strsave(stp->st_word);
12956 if (sstp->st_word != NULL)
12957 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012958 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012959 sstp->st_score = score;
12960 sstp->st_altscore = 0;
12961 sstp->st_orglen = stp->st_orglen;
12962 ++su->su_sga.ga_len;
12963 }
12964 }
12965 }
12966 break;
12967 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012968 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012969}
12970
12971/*
12972 * Combine the list of suggestions in su->su_ga and su->su_sga.
12973 * They are intwined.
12974 */
12975 static void
12976score_combine(su)
12977 suginfo_T *su;
12978{
12979 int i;
12980 int j;
12981 garray_T ga;
12982 garray_T *gap;
12983 langp_T *lp;
12984 suggest_T *stp;
12985 char_u *p;
12986 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012987 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012988 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012989 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012990
12991 /* Add the alternate score to su_ga. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020012992 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012993 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020012994 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012995 if (lp->lp_slang->sl_sal.ga_len > 0)
12996 {
12997 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012998 slang = lp->lp_slang;
12999 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013000
13001 for (i = 0; i < su->su_ga.ga_len; ++i)
13002 {
13003 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013004 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013005 if (stp->st_altscore == SCORE_MAXMAX)
13006 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
13007 else
13008 stp->st_score = (stp->st_score * 3
13009 + stp->st_altscore) / 4;
13010 stp->st_salscore = FALSE;
13011 }
13012 break;
13013 }
13014 }
13015
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013016 if (slang == NULL) /* Using "double" without sound folding. */
13017 {
13018 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
13019 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013020 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013021 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013022
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013023 /* Add the alternate score to su_sga. */
13024 for (i = 0; i < su->su_sga.ga_len; ++i)
13025 {
13026 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013027 stp->st_altscore = spell_edit_score(slang,
13028 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013029 if (stp->st_score == SCORE_MAXMAX)
13030 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
13031 else
13032 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
13033 stp->st_salscore = TRUE;
13034 }
13035
Bram Moolenaar4770d092006-01-12 23:22:24 +000013036 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
13037 * for both lists. */
13038 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013039 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013040 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013041 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
13042
13043 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
13044 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
13045 return;
13046
13047 stp = &SUG(ga, 0);
13048 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
13049 {
13050 /* round 1: get a suggestion from su_ga
13051 * round 2: get a suggestion from su_sga */
13052 for (round = 1; round <= 2; ++round)
13053 {
13054 gap = round == 1 ? &su->su_ga : &su->su_sga;
13055 if (i < gap->ga_len)
13056 {
13057 /* Don't add a word if it's already there. */
13058 p = SUG(*gap, i).st_word;
13059 for (j = 0; j < ga.ga_len; ++j)
13060 if (STRCMP(stp[j].st_word, p) == 0)
13061 break;
13062 if (j == ga.ga_len)
13063 stp[ga.ga_len++] = SUG(*gap, i);
13064 else
13065 vim_free(p);
13066 }
13067 }
13068 }
13069
13070 ga_clear(&su->su_ga);
13071 ga_clear(&su->su_sga);
13072
13073 /* Truncate the list to the number of suggestions that will be displayed. */
13074 if (ga.ga_len > su->su_maxcount)
13075 {
13076 for (i = su->su_maxcount; i < ga.ga_len; ++i)
13077 vim_free(stp[i].st_word);
13078 ga.ga_len = su->su_maxcount;
13079 }
13080
13081 su->su_ga = ga;
13082}
13083
13084/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013085 * For the goodword in "stp" compute the soundalike score compared to the
13086 * badword.
13087 */
13088 static int
13089stp_sal_score(stp, su, slang, badsound)
13090 suggest_T *stp;
13091 suginfo_T *su;
13092 slang_T *slang;
13093 char_u *badsound; /* sound-folded badword */
13094{
13095 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013096 char_u *pbad;
13097 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013098 char_u badsound2[MAXWLEN];
13099 char_u fword[MAXWLEN];
13100 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013101 char_u goodword[MAXWLEN];
13102 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013103
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013104 lendiff = (int)(su->su_badlen - stp->st_orglen);
13105 if (lendiff >= 0)
13106 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013107 else
13108 {
13109 /* soundfold the bad word with more characters following */
13110 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
13111
13112 /* When joining two words the sound often changes a lot. E.g., "t he"
13113 * sounds like "t h" while "the" sounds like "@". Avoid that by
13114 * removing the space. Don't do it when the good word also contains a
13115 * space. */
13116 if (vim_iswhite(su->su_badptr[su->su_badlen])
13117 && *skiptowhite(stp->st_word) == NUL)
13118 for (p = fword; *(p = skiptowhite(p)) != NUL; )
Bram Moolenaara7241f52008-06-24 20:39:31 +000013119 STRMOVE(p, p + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013120
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013121 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013122 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013123 }
13124
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013125 if (lendiff > 0)
13126 {
13127 /* Add part of the bad word to the good word, so that we soundfold
13128 * what replaces the bad word. */
13129 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013130 vim_strncpy(goodword + stp->st_wordlen,
13131 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013132 pgood = goodword;
13133 }
13134 else
13135 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013136
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013137 /* Sound-fold the word and compute the score for the difference. */
13138 spell_soundfold(slang, pgood, FALSE, goodsound);
13139
13140 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013141}
13142
Bram Moolenaar4770d092006-01-12 23:22:24 +000013143/* structure used to store soundfolded words that add_sound_suggest() has
13144 * handled already. */
13145typedef struct
13146{
13147 short sft_score; /* lowest score used */
13148 char_u sft_word[1]; /* soundfolded word, actually longer */
13149} sftword_T;
13150
13151static sftword_T dumsft;
13152#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
13153#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
13154
13155/*
13156 * Prepare for calling suggest_try_soundalike().
13157 */
13158 static void
13159suggest_try_soundalike_prep()
13160{
13161 langp_T *lp;
13162 int lpi;
13163 slang_T *slang;
13164
13165 /* Do this for all languages that support sound folding and for which a
13166 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013167 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013168 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013169 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013170 slang = lp->lp_slang;
13171 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13172 /* prepare the hashtable used by add_sound_suggest() */
13173 hash_init(&slang->sl_sounddone);
13174 }
13175}
13176
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013177/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013178 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013179 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013180 */
13181 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000013182suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013183 suginfo_T *su;
13184{
13185 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013186 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013187 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013188 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013189
Bram Moolenaar4770d092006-01-12 23:22:24 +000013190 /* Do this for all languages that support sound folding and for which a
13191 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013192 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013193 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013194 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013195 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013196 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013197 {
13198 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013199 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013200
Bram Moolenaar4770d092006-01-12 23:22:24 +000013201 /* try all kinds of inserts/deletes/swaps/etc. */
13202 /* TODO: also soundfold the next words, so that we can try joining
13203 * and splitting */
13204 suggest_trie_walk(su, lp, salword, TRUE);
13205 }
13206 }
13207}
13208
13209/*
13210 * Finish up after calling suggest_try_soundalike().
13211 */
13212 static void
13213suggest_try_soundalike_finish()
13214{
13215 langp_T *lp;
13216 int lpi;
13217 slang_T *slang;
13218 int todo;
13219 hashitem_T *hi;
13220
13221 /* Do this for all languages that support sound folding and for which a
13222 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013223 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013224 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013225 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013226 slang = lp->lp_slang;
13227 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13228 {
13229 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013230 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013231 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
13232 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013233 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013234 vim_free(HI2SFT(hi));
13235 --todo;
13236 }
Bram Moolenaar6417da62007-03-08 13:49:53 +000013237
13238 /* Clear the hashtable, it may also be used by another region. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013239 hash_clear(&slang->sl_sounddone);
Bram Moolenaar6417da62007-03-08 13:49:53 +000013240 hash_init(&slang->sl_sounddone);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013241 }
13242 }
13243}
13244
13245/*
13246 * A match with a soundfolded word is found. Add the good word(s) that
13247 * produce this soundfolded word.
13248 */
13249 static void
13250add_sound_suggest(su, goodword, score, lp)
13251 suginfo_T *su;
13252 char_u *goodword;
13253 int score; /* soundfold score */
13254 langp_T *lp;
13255{
13256 slang_T *slang = lp->lp_slang; /* language for sound folding */
13257 int sfwordnr;
13258 char_u *nrline;
13259 int orgnr;
13260 char_u theword[MAXWLEN];
13261 int i;
13262 int wlen;
13263 char_u *byts;
13264 idx_T *idxs;
13265 int n;
13266 int wordcount;
13267 int wc;
13268 int goodscore;
13269 hash_T hash;
13270 hashitem_T *hi;
13271 sftword_T *sft;
13272 int bc, gc;
13273 int limit;
13274
13275 /*
13276 * It's very well possible that the same soundfold word is found several
13277 * times with different scores. Since the following is quite slow only do
13278 * the words that have a better score than before. Use a hashtable to
13279 * remember the words that have been done.
13280 */
13281 hash = hash_hash(goodword);
13282 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
13283 if (HASHITEM_EMPTY(hi))
13284 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013285 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
13286 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000013287 if (sft != NULL)
13288 {
13289 sft->sft_score = score;
13290 STRCPY(sft->sft_word, goodword);
13291 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
13292 }
13293 }
13294 else
13295 {
13296 sft = HI2SFT(hi);
13297 if (score >= sft->sft_score)
13298 return;
13299 sft->sft_score = score;
13300 }
13301
13302 /*
13303 * Find the word nr in the soundfold tree.
13304 */
13305 sfwordnr = soundfold_find(slang, goodword);
13306 if (sfwordnr < 0)
13307 {
13308 EMSG2(_(e_intern2), "add_sound_suggest()");
13309 return;
13310 }
13311
13312 /*
13313 * go over the list of good words that produce this soundfold word
13314 */
13315 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
13316 orgnr = 0;
13317 while (*nrline != NUL)
13318 {
13319 /* The wordnr was stored in a minimal nr of bytes as an offset to the
13320 * previous wordnr. */
13321 orgnr += bytes2offset(&nrline);
13322
13323 byts = slang->sl_fbyts;
13324 idxs = slang->sl_fidxs;
13325
13326 /* Lookup the word "orgnr" one of the two tries. */
13327 n = 0;
13328 wlen = 0;
13329 wordcount = 0;
13330 for (;;)
13331 {
13332 i = 1;
13333 if (wordcount == orgnr && byts[n + 1] == NUL)
13334 break; /* found end of word */
13335
13336 if (byts[n + 1] == NUL)
13337 ++wordcount;
13338
13339 /* skip over the NUL bytes */
13340 for ( ; byts[n + i] == NUL; ++i)
13341 if (i > byts[n]) /* safety check */
13342 {
13343 STRCPY(theword + wlen, "BAD");
13344 goto badword;
13345 }
13346
13347 /* One of the siblings must have the word. */
13348 for ( ; i < byts[n]; ++i)
13349 {
13350 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
13351 if (wordcount + wc > orgnr)
13352 break;
13353 wordcount += wc;
13354 }
13355
13356 theword[wlen++] = byts[n + i];
13357 n = idxs[n + i];
13358 }
13359badword:
13360 theword[wlen] = NUL;
13361
13362 /* Go over the possible flags and regions. */
13363 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
13364 {
13365 char_u cword[MAXWLEN];
13366 char_u *p;
13367 int flags = (int)idxs[n + i];
13368
Bram Moolenaare1438bb2006-03-01 22:01:55 +000013369 /* Skip words with the NOSUGGEST flag */
13370 if (flags & WF_NOSUGGEST)
13371 continue;
13372
Bram Moolenaar4770d092006-01-12 23:22:24 +000013373 if (flags & WF_KEEPCAP)
13374 {
13375 /* Must find the word in the keep-case tree. */
13376 find_keepcap_word(slang, theword, cword);
13377 p = cword;
13378 }
13379 else
13380 {
13381 flags |= su->su_badflags;
13382 if ((flags & WF_CAPMASK) != 0)
13383 {
13384 /* Need to fix case according to "flags". */
13385 make_case_word(theword, cword, flags);
13386 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013387 }
13388 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013389 p = theword;
13390 }
13391
13392 /* Add the suggestion. */
13393 if (sps_flags & SPS_DOUBLE)
13394 {
13395 /* Add the suggestion if the score isn't too bad. */
13396 if (score <= su->su_maxscore)
13397 add_suggestion(su, &su->su_sga, p, su->su_badlen,
13398 score, 0, FALSE, slang, FALSE);
13399 }
13400 else
13401 {
13402 /* Add a penalty for words in another region. */
13403 if ((flags & WF_REGION)
13404 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
13405 goodscore = SCORE_REGION;
13406 else
13407 goodscore = 0;
13408
13409 /* Add a small penalty for changing the first letter from
13410 * lower to upper case. Helps for "tath" -> "Kath", which is
13411 * less common thatn "tath" -> "path". Don't do it when the
13412 * letter is the same, that has already been counted. */
13413 gc = PTR2CHAR(p);
13414 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013415 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013416 bc = PTR2CHAR(su->su_badword);
13417 if (!SPELL_ISUPPER(bc)
13418 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
13419 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013420 }
13421
Bram Moolenaar4770d092006-01-12 23:22:24 +000013422 /* Compute the score for the good word. This only does letter
13423 * insert/delete/swap/replace. REP items are not considered,
13424 * which may make the score a bit higher.
13425 * Use a limit for the score to make it work faster. Use
13426 * MAXSCORE(), because RESCORE() will change the score.
13427 * If the limit is very high then the iterative method is
13428 * inefficient, using an array is quicker. */
13429 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
13430 if (limit > SCORE_LIMITMAX)
13431 goodscore += spell_edit_score(slang, su->su_badword, p);
13432 else
13433 goodscore += spell_edit_score_limit(slang, su->su_badword,
13434 p, limit);
13435
13436 /* When going over the limit don't bother to do the rest. */
13437 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013438 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013439 /* Give a bonus to words seen before. */
13440 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013441
Bram Moolenaar4770d092006-01-12 23:22:24 +000013442 /* Add the suggestion if the score isn't too bad. */
13443 goodscore = RESCORE(goodscore, score);
13444 if (goodscore <= su->su_sfmaxscore)
13445 add_suggestion(su, &su->su_ga, p, su->su_badlen,
13446 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013447 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013448 }
13449 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013450 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013451 }
13452}
13453
13454/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013455 * Find word "word" in fold-case tree for "slang" and return the word number.
13456 */
13457 static int
13458soundfold_find(slang, word)
13459 slang_T *slang;
13460 char_u *word;
13461{
13462 idx_T arridx = 0;
13463 int len;
13464 int wlen = 0;
13465 int c;
13466 char_u *ptr = word;
13467 char_u *byts;
13468 idx_T *idxs;
13469 int wordnr = 0;
13470
13471 byts = slang->sl_sbyts;
13472 idxs = slang->sl_sidxs;
13473
13474 for (;;)
13475 {
13476 /* First byte is the number of possible bytes. */
13477 len = byts[arridx++];
13478
13479 /* If the first possible byte is a zero the word could end here.
13480 * If the word ends we found the word. If not skip the NUL bytes. */
13481 c = ptr[wlen];
13482 if (byts[arridx] == NUL)
13483 {
13484 if (c == NUL)
13485 break;
13486
13487 /* Skip over the zeros, there can be several. */
13488 while (len > 0 && byts[arridx] == NUL)
13489 {
13490 ++arridx;
13491 --len;
13492 }
13493 if (len == 0)
13494 return -1; /* no children, word should have ended here */
13495 ++wordnr;
13496 }
13497
13498 /* If the word ends we didn't find it. */
13499 if (c == NUL)
13500 return -1;
13501
13502 /* Perform a binary search in the list of accepted bytes. */
13503 if (c == TAB) /* <Tab> is handled like <Space> */
13504 c = ' ';
13505 while (byts[arridx] < c)
13506 {
13507 /* The word count is in the first idxs[] entry of the child. */
13508 wordnr += idxs[idxs[arridx]];
13509 ++arridx;
13510 if (--len == 0) /* end of the bytes, didn't find it */
13511 return -1;
13512 }
13513 if (byts[arridx] != c) /* didn't find the byte */
13514 return -1;
13515
13516 /* Continue at the child (if there is one). */
13517 arridx = idxs[arridx];
13518 ++wlen;
13519
13520 /* One space in the good word may stand for several spaces in the
13521 * checked word. */
13522 if (c == ' ')
13523 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
13524 ++wlen;
13525 }
13526
13527 return wordnr;
13528}
13529
13530/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013531 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013532 */
13533 static void
13534make_case_word(fword, cword, flags)
13535 char_u *fword;
13536 char_u *cword;
13537 int flags;
13538{
13539 if (flags & WF_ALLCAP)
13540 /* Make it all upper-case */
13541 allcap_copy(fword, cword);
13542 else if (flags & WF_ONECAP)
13543 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013544 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013545 else
13546 /* Use goodword as-is. */
13547 STRCPY(cword, fword);
13548}
13549
Bram Moolenaarea424162005-06-16 21:51:00 +000013550/*
13551 * Use map string "map" for languages "lp".
13552 */
13553 static void
13554set_map_str(lp, map)
13555 slang_T *lp;
13556 char_u *map;
13557{
13558 char_u *p;
13559 int headc = 0;
13560 int c;
13561 int i;
13562
13563 if (*map == NUL)
13564 {
13565 lp->sl_has_map = FALSE;
13566 return;
13567 }
13568 lp->sl_has_map = TRUE;
13569
Bram Moolenaar4770d092006-01-12 23:22:24 +000013570 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000013571 for (i = 0; i < 256; ++i)
13572 lp->sl_map_array[i] = 0;
13573#ifdef FEAT_MBYTE
13574 hash_init(&lp->sl_map_hash);
13575#endif
13576
13577 /*
13578 * The similar characters are stored separated with slashes:
13579 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13580 * before the same slash. For characters above 255 sl_map_hash is used.
13581 */
13582 for (p = map; *p != NUL; )
13583 {
13584#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013585 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000013586#else
13587 c = *p++;
13588#endif
13589 if (c == '/')
13590 headc = 0;
13591 else
13592 {
13593 if (headc == 0)
13594 headc = c;
13595
13596#ifdef FEAT_MBYTE
13597 /* Characters above 255 don't fit in sl_map_array[], put them in
13598 * the hash table. Each entry is the char, a NUL the headchar and
13599 * a NUL. */
13600 if (c >= 256)
13601 {
13602 int cl = mb_char2len(c);
13603 int headcl = mb_char2len(headc);
13604 char_u *b;
13605 hash_T hash;
13606 hashitem_T *hi;
13607
13608 b = alloc((unsigned)(cl + headcl + 2));
13609 if (b == NULL)
13610 return;
13611 mb_char2bytes(c, b);
13612 b[cl] = NUL;
13613 mb_char2bytes(headc, b + cl + 1);
13614 b[cl + 1 + headcl] = NUL;
13615 hash = hash_hash(b);
13616 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13617 if (HASHITEM_EMPTY(hi))
13618 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13619 else
13620 {
13621 /* This should have been checked when generating the .spl
13622 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013623 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000013624 vim_free(b);
13625 }
13626 }
13627 else
13628#endif
13629 lp->sl_map_array[c] = headc;
13630 }
13631 }
13632}
13633
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013634/*
13635 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13636 * lines in the .aff file.
13637 */
13638 static int
13639similar_chars(slang, c1, c2)
13640 slang_T *slang;
13641 int c1;
13642 int c2;
13643{
Bram Moolenaarea424162005-06-16 21:51:00 +000013644 int m1, m2;
13645#ifdef FEAT_MBYTE
13646 char_u buf[MB_MAXBYTES];
13647 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013648
Bram Moolenaarea424162005-06-16 21:51:00 +000013649 if (c1 >= 256)
13650 {
13651 buf[mb_char2bytes(c1, buf)] = 0;
13652 hi = hash_find(&slang->sl_map_hash, buf);
13653 if (HASHITEM_EMPTY(hi))
13654 m1 = 0;
13655 else
13656 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13657 }
13658 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013659#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013660 m1 = slang->sl_map_array[c1];
13661 if (m1 == 0)
13662 return FALSE;
13663
13664
13665#ifdef FEAT_MBYTE
13666 if (c2 >= 256)
13667 {
13668 buf[mb_char2bytes(c2, buf)] = 0;
13669 hi = hash_find(&slang->sl_map_hash, buf);
13670 if (HASHITEM_EMPTY(hi))
13671 m2 = 0;
13672 else
13673 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13674 }
13675 else
13676#endif
13677 m2 = slang->sl_map_array[c2];
13678
13679 return m1 == m2;
13680}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013681
13682/*
13683 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013684 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013685 */
13686 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000013687add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
13688 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013689 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013690 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013691 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013692 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013693 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013694 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013695 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013696 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013697 int maxsf; /* su_maxscore applies to soundfold score,
13698 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013699{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013700 int goodlen; /* len of goodword changed */
13701 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013702 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013703 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013704 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013705 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013706
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013707 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13708 * "thee the" is added next to changing the first "the" the "thee". */
13709 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013710 pbad = su->su_badptr + badlenarg;
13711 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013712 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013713 goodlen = (int)(pgood - goodword);
13714 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013715 if (goodlen <= 0 || badlen <= 0)
13716 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013717 mb_ptr_back(goodword, pgood);
13718 mb_ptr_back(su->su_badptr, pbad);
13719#ifdef FEAT_MBYTE
13720 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013721 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013722 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13723 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013724 }
13725 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013726#endif
13727 if (*pgood != *pbad)
13728 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013729 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013730
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013731 if (badlen == 0 && goodlen == 0)
13732 /* goodword doesn't change anything; may happen for "the the" changing
13733 * the first "the" to itself. */
13734 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013735
Bram Moolenaar89d40322006-08-29 15:30:07 +000013736 if (gap->ga_len == 0)
13737 i = -1;
13738 else
13739 {
13740 /* Check if the word is already there. Also check the length that is
13741 * being replaced "thes," -> "these" is a different suggestion from
13742 * "thes" -> "these". */
13743 stp = &SUG(*gap, 0);
13744 for (i = gap->ga_len; --i >= 0; ++stp)
13745 if (stp->st_wordlen == goodlen
13746 && stp->st_orglen == badlen
13747 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013748 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013749 /*
13750 * Found it. Remember the word with the lowest score.
13751 */
13752 if (stp->st_slang == NULL)
13753 stp->st_slang = slang;
13754
13755 new_sug.st_score = score;
13756 new_sug.st_altscore = altscore;
13757 new_sug.st_had_bonus = had_bonus;
13758
13759 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013760 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013761 /* Only one of the two had the soundalike score computed.
13762 * Need to do that for the other one now, otherwise the
13763 * scores can't be compared. This happens because
13764 * suggest_try_change() doesn't compute the soundalike
13765 * word to keep it fast, while some special methods set
13766 * the soundalike score to zero. */
13767 if (had_bonus)
13768 rescore_one(su, stp);
13769 else
13770 {
13771 new_sug.st_word = stp->st_word;
13772 new_sug.st_wordlen = stp->st_wordlen;
13773 new_sug.st_slang = stp->st_slang;
13774 new_sug.st_orglen = badlen;
13775 rescore_one(su, &new_sug);
13776 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013777 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013778
Bram Moolenaar89d40322006-08-29 15:30:07 +000013779 if (stp->st_score > new_sug.st_score)
13780 {
13781 stp->st_score = new_sug.st_score;
13782 stp->st_altscore = new_sug.st_altscore;
13783 stp->st_had_bonus = new_sug.st_had_bonus;
13784 }
13785 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013786 }
Bram Moolenaar89d40322006-08-29 15:30:07 +000013787 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013788
Bram Moolenaar4770d092006-01-12 23:22:24 +000013789 if (i < 0 && ga_grow(gap, 1) == OK)
13790 {
13791 /* Add a suggestion. */
13792 stp = &SUG(*gap, gap->ga_len);
13793 stp->st_word = vim_strnsave(goodword, goodlen);
13794 if (stp->st_word != NULL)
13795 {
13796 stp->st_wordlen = goodlen;
13797 stp->st_score = score;
13798 stp->st_altscore = altscore;
13799 stp->st_had_bonus = had_bonus;
13800 stp->st_orglen = badlen;
13801 stp->st_slang = slang;
13802 ++gap->ga_len;
13803
13804 /* If we have too many suggestions now, sort the list and keep
13805 * the best suggestions. */
13806 if (gap->ga_len > SUG_MAX_COUNT(su))
13807 {
13808 if (maxsf)
13809 su->su_sfmaxscore = cleanup_suggestions(gap,
13810 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13811 else
13812 {
13813 i = su->su_maxscore;
13814 su->su_maxscore = cleanup_suggestions(gap,
13815 su->su_maxscore, SUG_CLEAN_COUNT(su));
13816 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013817 }
13818 }
13819 }
13820}
13821
13822/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013823 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13824 * for split words, such as "the the". Remove these from the list here.
13825 */
13826 static void
13827check_suggestions(su, gap)
13828 suginfo_T *su;
13829 garray_T *gap; /* either su_ga or su_sga */
13830{
13831 suggest_T *stp;
13832 int i;
13833 char_u longword[MAXWLEN + 1];
13834 int len;
13835 hlf_T attr;
13836
13837 stp = &SUG(*gap, 0);
13838 for (i = gap->ga_len - 1; i >= 0; --i)
13839 {
13840 /* Need to append what follows to check for "the the". */
13841 STRCPY(longword, stp[i].st_word);
13842 len = stp[i].st_wordlen;
13843 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13844 MAXWLEN - len);
13845 attr = HLF_COUNT;
13846 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13847 if (attr != HLF_COUNT)
13848 {
13849 /* Remove this entry. */
13850 vim_free(stp[i].st_word);
13851 --gap->ga_len;
13852 if (i < gap->ga_len)
13853 mch_memmove(stp + i, stp + i + 1,
13854 sizeof(suggest_T) * (gap->ga_len - i));
13855 }
13856 }
13857}
13858
13859
13860/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013861 * Add a word to be banned.
13862 */
13863 static void
13864add_banned(su, word)
13865 suginfo_T *su;
13866 char_u *word;
13867{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013868 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013869 hash_T hash;
13870 hashitem_T *hi;
13871
Bram Moolenaar4770d092006-01-12 23:22:24 +000013872 hash = hash_hash(word);
13873 hi = hash_lookup(&su->su_banned, word, hash);
13874 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013875 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013876 s = vim_strsave(word);
13877 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013878 hash_add_item(&su->su_banned, hi, s, hash);
13879 }
13880}
13881
13882/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013883 * Recompute the score for all suggestions if sound-folding is possible. This
13884 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013885 */
13886 static void
13887rescore_suggestions(su)
13888 suginfo_T *su;
13889{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013890 int i;
13891
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013892 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013893 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013894 rescore_one(su, &SUG(su->su_ga, i));
13895}
13896
13897/*
13898 * Recompute the score for one suggestion if sound-folding is possible.
13899 */
13900 static void
13901rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013902 suginfo_T *su;
13903 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013904{
13905 slang_T *slang = stp->st_slang;
13906 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013907 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013908
13909 /* Only rescore suggestions that have no sal score yet and do have a
13910 * language. */
13911 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13912 {
13913 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013914 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013915 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013916 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013917 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013918 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013919 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013920
13921 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013922 if (stp->st_altscore == SCORE_MAXMAX)
13923 stp->st_altscore = SCORE_BIG;
13924 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13925 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013926 }
13927}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013928
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013929static int
13930#ifdef __BORLANDC__
13931_RTLENTRYF
13932#endif
13933sug_compare __ARGS((const void *s1, const void *s2));
13934
13935/*
13936 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013937 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013938 */
13939 static int
13940#ifdef __BORLANDC__
13941_RTLENTRYF
13942#endif
13943sug_compare(s1, s2)
13944 const void *s1;
13945 const void *s2;
13946{
13947 suggest_T *p1 = (suggest_T *)s1;
13948 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013949 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013950
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013951 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013952 {
13953 n = p1->st_altscore - p2->st_altscore;
13954 if (n == 0)
13955 n = STRICMP(p1->st_word, p2->st_word);
13956 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013957 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013958}
13959
13960/*
13961 * Cleanup the suggestions:
13962 * - Sort on score.
13963 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013964 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013965 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013966 static int
13967cleanup_suggestions(gap, maxscore, keep)
13968 garray_T *gap;
13969 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013970 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013971{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013972 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013973 int i;
13974
13975 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013976 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013977
13978 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013979 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013980 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013981 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013982 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013983 gap->ga_len = keep;
13984 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013985 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013986 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013987}
13988
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013989#if defined(FEAT_EVAL) || defined(PROTO)
13990/*
13991 * Soundfold a string, for soundfold().
13992 * Result is in allocated memory, NULL for an error.
13993 */
13994 char_u *
13995eval_soundfold(word)
13996 char_u *word;
13997{
13998 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000013999 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014000 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014001
Bram Moolenaar860cae12010-06-05 23:22:07 +020014002 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014003 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020014004 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014005 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020014006 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014007 if (lp->lp_slang->sl_sal.ga_len > 0)
14008 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014009 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014010 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014011 return vim_strsave(sound);
14012 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014013 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014014
14015 /* No language with sound folding, return word as-is. */
14016 return vim_strsave(word);
14017}
14018#endif
14019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014020/*
14021 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000014022 *
14023 * There are many ways to turn a word into a sound-a-like representation. The
14024 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
14025 * swedish name matching - survey and test of different algorithms" by Klas
14026 * Erikson.
14027 *
14028 * We support two methods:
14029 * 1. SOFOFROM/SOFOTO do a simple character mapping.
14030 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014031 */
14032 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014033spell_soundfold(slang, inword, folded, res)
14034 slang_T *slang;
14035 char_u *inword;
14036 int folded; /* "inword" is already case-folded */
14037 char_u *res;
14038{
14039 char_u fword[MAXWLEN];
14040 char_u *word;
14041
14042 if (slang->sl_sofo)
14043 /* SOFOFROM and SOFOTO used */
14044 spell_soundfold_sofo(slang, inword, res);
14045 else
14046 {
14047 /* SAL items used. Requires the word to be case-folded. */
14048 if (folded)
14049 word = inword;
14050 else
14051 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014052 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014053 word = fword;
14054 }
14055
14056#ifdef FEAT_MBYTE
14057 if (has_mbyte)
14058 spell_soundfold_wsal(slang, word, res);
14059 else
14060#endif
14061 spell_soundfold_sal(slang, word, res);
14062 }
14063}
14064
14065/*
14066 * Perform sound folding of "inword" into "res" according to SOFOFROM and
14067 * SOFOTO lines.
14068 */
14069 static void
14070spell_soundfold_sofo(slang, inword, res)
14071 slang_T *slang;
14072 char_u *inword;
14073 char_u *res;
14074{
14075 char_u *s;
14076 int ri = 0;
14077 int c;
14078
14079#ifdef FEAT_MBYTE
14080 if (has_mbyte)
14081 {
14082 int prevc = 0;
14083 int *ip;
14084
14085 /* The sl_sal_first[] table contains the translation for chars up to
14086 * 255, sl_sal the rest. */
14087 for (s = inword; *s != NUL; )
14088 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014089 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014090 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14091 c = ' ';
14092 else if (c < 256)
14093 c = slang->sl_sal_first[c];
14094 else
14095 {
14096 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
14097 if (ip == NULL) /* empty list, can't match */
14098 c = NUL;
14099 else
14100 for (;;) /* find "c" in the list */
14101 {
14102 if (*ip == 0) /* not found */
14103 {
14104 c = NUL;
14105 break;
14106 }
14107 if (*ip == c) /* match! */
14108 {
14109 c = ip[1];
14110 break;
14111 }
14112 ip += 2;
14113 }
14114 }
14115
14116 if (c != NUL && c != prevc)
14117 {
14118 ri += mb_char2bytes(c, res + ri);
14119 if (ri + MB_MAXBYTES > MAXWLEN)
14120 break;
14121 prevc = c;
14122 }
14123 }
14124 }
14125 else
14126#endif
14127 {
14128 /* The sl_sal_first[] table contains the translation. */
14129 for (s = inword; (c = *s) != NUL; ++s)
14130 {
14131 if (vim_iswhite(c))
14132 c = ' ';
14133 else
14134 c = slang->sl_sal_first[c];
14135 if (c != NUL && (ri == 0 || res[ri - 1] != c))
14136 res[ri++] = c;
14137 }
14138 }
14139
14140 res[ri] = NUL;
14141}
14142
14143 static void
14144spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014145 slang_T *slang;
14146 char_u *inword;
14147 char_u *res;
14148{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014149 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014150 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014151 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014152 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014153 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014154 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014155 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014156 int n, k = 0;
14157 int z0;
14158 int k0;
14159 int n0;
14160 int c;
14161 int pri;
14162 int p0 = -333;
14163 int c0;
14164
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014165 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014166 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014167 if (slang->sl_rem_accents)
14168 {
14169 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014170 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014171 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014172 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014173 {
14174 *t++ = ' ';
14175 s = skipwhite(s);
14176 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014177 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014178 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014179 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014180 *t++ = *s;
14181 ++s;
14182 }
14183 }
14184 *t = NUL;
14185 }
14186 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014187 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014188
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014189 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014190
14191 /*
14192 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014193 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014194 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014195 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014196 while ((c = word[i]) != NUL)
14197 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014198 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014199 n = slang->sl_sal_first[c];
14200 z0 = 0;
14201
14202 if (n >= 0)
14203 {
14204 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014205 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014206 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014207 /* Quickly skip entries that don't match the word. Most
14208 * entries are less then three chars, optimize for that. */
14209 k = smp[n].sm_leadlen;
14210 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014211 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014212 if (word[i + 1] != s[1])
14213 continue;
14214 if (k > 2)
14215 {
14216 for (j = 2; j < k; ++j)
14217 if (word[i + j] != s[j])
14218 break;
14219 if (j < k)
14220 continue;
14221 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014222 }
14223
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014224 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014225 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014226 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014227 while (*pf != NUL && *pf != word[i + k])
14228 ++pf;
14229 if (*pf == NUL)
14230 continue;
14231 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014232 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014233 s = smp[n].sm_rules;
14234 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014235
14236 p0 = *s;
14237 k0 = k;
14238 while (*s == '-' && k > 1)
14239 {
14240 k--;
14241 s++;
14242 }
14243 if (*s == '<')
14244 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014245 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014246 {
14247 /* determine priority */
14248 pri = *s - '0';
14249 s++;
14250 }
14251 if (*s == '^' && *(s + 1) == '^')
14252 s++;
14253
14254 if (*s == NUL
14255 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014256 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +020014257 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014258 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +020014259 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014260 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +020014261 && spell_iswordp(word + i - 1, curwin)
14262 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014263 {
14264 /* search for followup rules, if: */
14265 /* followup and k > 1 and NO '-' in searchstring */
14266 c0 = word[i + k - 1];
14267 n0 = slang->sl_sal_first[c0];
14268
14269 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014270 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014271 {
14272 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014273 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014274 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014275 /* Quickly skip entries that don't match the word.
14276 * */
14277 k0 = smp[n0].sm_leadlen;
14278 if (k0 > 1)
14279 {
14280 if (word[i + k] != s[1])
14281 continue;
14282 if (k0 > 2)
14283 {
14284 pf = word + i + k + 1;
14285 for (j = 2; j < k0; ++j)
14286 if (*pf++ != s[j])
14287 break;
14288 if (j < k0)
14289 continue;
14290 }
14291 }
14292 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014293
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014294 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014295 {
14296 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014297 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014298 while (*pf != NUL && *pf != word[i + k0])
14299 ++pf;
14300 if (*pf == NUL)
14301 continue;
14302 ++k0;
14303 }
14304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014305 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014306 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014307 while (*s == '-')
14308 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014309 /* "k0" gets NOT reduced because
14310 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014311 s++;
14312 }
14313 if (*s == '<')
14314 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014315 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014316 {
14317 p0 = *s - '0';
14318 s++;
14319 }
14320
14321 if (*s == NUL
14322 /* *s == '^' cuts */
14323 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014324 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +020014325 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014326 {
14327 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014328 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014329 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014330
14331 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014332 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014333 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014334 /* rule fits; stop search */
14335 break;
14336 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014337 }
14338
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014339 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014340 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014341 }
14342
14343 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014344 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014345 if (s == NULL)
14346 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014347 pf = smp[n].sm_rules;
14348 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014349 if (p0 == 1 && z == 0)
14350 {
14351 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014352 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
14353 || res[reslen - 1] == *s))
14354 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014355 z0 = 1;
14356 z = 1;
14357 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014358 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014359 {
14360 word[i + k0] = *s;
14361 k0++;
14362 s++;
14363 }
14364 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +000014365 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014366
14367 /* new "actual letter" */
14368 c = word[i];
14369 }
14370 else
14371 {
14372 /* no '<' rule used */
14373 i += k - 1;
14374 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014375 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014376 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014377 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014378 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014379 s++;
14380 }
14381 /* new "actual letter" */
14382 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014383 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014384 {
14385 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014386 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +000014387 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014388 i = 0;
14389 z0 = 1;
14390 }
14391 }
14392 break;
14393 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014394 }
14395 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014396 else if (vim_iswhite(c))
14397 {
14398 c = ' ';
14399 k = 1;
14400 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014401
14402 if (z0 == 0)
14403 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014404 if (k && !p0 && reslen < MAXWLEN && c != NUL
14405 && (!slang->sl_collapse || reslen == 0
14406 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014407 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014408 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014409
14410 i++;
14411 z = 0;
14412 k = 0;
14413 }
14414 }
14415
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014416 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014417}
14418
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014419#ifdef FEAT_MBYTE
14420/*
14421 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14422 * Multi-byte version of spell_soundfold().
14423 */
14424 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014425spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014426 slang_T *slang;
14427 char_u *inword;
14428 char_u *res;
14429{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014430 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014431 int word[MAXWLEN];
14432 int wres[MAXWLEN];
14433 int l;
14434 char_u *s;
14435 int *ws;
14436 char_u *t;
14437 int *pf;
14438 int i, j, z;
14439 int reslen;
14440 int n, k = 0;
14441 int z0;
14442 int k0;
14443 int n0;
14444 int c;
14445 int pri;
14446 int p0 = -333;
14447 int c0;
14448 int did_white = FALSE;
14449
14450 /*
14451 * Convert the multi-byte string to a wide-character string.
14452 * Remove accents, if wanted. We actually remove all non-word characters.
14453 * But keep white space.
14454 */
14455 n = 0;
14456 for (s = inword; *s != NUL; )
14457 {
14458 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014459 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014460 if (slang->sl_rem_accents)
14461 {
14462 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14463 {
14464 if (did_white)
14465 continue;
14466 c = ' ';
14467 did_white = TRUE;
14468 }
14469 else
14470 {
14471 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014472 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014473 continue;
14474 }
14475 }
14476 word[n++] = c;
14477 }
14478 word[n] = NUL;
14479
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014480 /*
14481 * This comes from Aspell phonet.cpp.
14482 * Converted from C++ to C. Added support for multi-byte chars.
14483 * Changed to keep spaces.
14484 */
14485 i = reslen = z = 0;
14486 while ((c = word[i]) != NUL)
14487 {
14488 /* Start with the first rule that has the character in the word. */
14489 n = slang->sl_sal_first[c & 0xff];
14490 z0 = 0;
14491
14492 if (n >= 0)
14493 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014494 /* check all rules for the same index byte */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014495 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff); ++n)
14496 {
14497 /* Quickly skip entries that don't match the word. Most
14498 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014499 if (c != ws[0])
14500 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014501 k = smp[n].sm_leadlen;
14502 if (k > 1)
14503 {
14504 if (word[i + 1] != ws[1])
14505 continue;
14506 if (k > 2)
14507 {
14508 for (j = 2; j < k; ++j)
14509 if (word[i + j] != ws[j])
14510 break;
14511 if (j < k)
14512 continue;
14513 }
14514 }
14515
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014516 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014517 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014518 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014519 while (*pf != NUL && *pf != word[i + k])
14520 ++pf;
14521 if (*pf == NUL)
14522 continue;
14523 ++k;
14524 }
14525 s = smp[n].sm_rules;
14526 pri = 5; /* default priority */
14527
14528 p0 = *s;
14529 k0 = k;
14530 while (*s == '-' && k > 1)
14531 {
14532 k--;
14533 s++;
14534 }
14535 if (*s == '<')
14536 s++;
14537 if (VIM_ISDIGIT(*s))
14538 {
14539 /* determine priority */
14540 pri = *s - '0';
14541 s++;
14542 }
14543 if (*s == '^' && *(s + 1) == '^')
14544 s++;
14545
14546 if (*s == NUL
14547 || (*s == '^'
14548 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +020014549 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014550 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +020014551 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014552 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +020014553 && spell_iswordp_w(word + i - 1, curwin)
14554 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014555 {
14556 /* search for followup rules, if: */
14557 /* followup and k > 1 and NO '-' in searchstring */
14558 c0 = word[i + k - 1];
14559 n0 = slang->sl_sal_first[c0 & 0xff];
14560
14561 if (slang->sl_followup && k > 1 && n0 >= 0
14562 && p0 != '-' && word[i + k] != NUL)
14563 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014564 /* Test follow-up rule for "word[i + k]"; loop over
14565 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014566 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14567 == (c0 & 0xff); ++n0)
14568 {
14569 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014570 */
14571 if (c0 != ws[0])
14572 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014573 k0 = smp[n0].sm_leadlen;
14574 if (k0 > 1)
14575 {
14576 if (word[i + k] != ws[1])
14577 continue;
14578 if (k0 > 2)
14579 {
14580 pf = word + i + k + 1;
14581 for (j = 2; j < k0; ++j)
14582 if (*pf++ != ws[j])
14583 break;
14584 if (j < k0)
14585 continue;
14586 }
14587 }
14588 k0 += k - 1;
14589
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014590 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014591 {
14592 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014593 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014594 while (*pf != NUL && *pf != word[i + k0])
14595 ++pf;
14596 if (*pf == NUL)
14597 continue;
14598 ++k0;
14599 }
14600
14601 p0 = 5;
14602 s = smp[n0].sm_rules;
14603 while (*s == '-')
14604 {
14605 /* "k0" gets NOT reduced because
14606 * "if (k0 == k)" */
14607 s++;
14608 }
14609 if (*s == '<')
14610 s++;
14611 if (VIM_ISDIGIT(*s))
14612 {
14613 p0 = *s - '0';
14614 s++;
14615 }
14616
14617 if (*s == NUL
14618 /* *s == '^' cuts */
14619 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014620 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +020014621 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014622 {
14623 if (k0 == k)
14624 /* this is just a piece of the string */
14625 continue;
14626
14627 if (p0 < pri)
14628 /* priority too low */
14629 continue;
14630 /* rule fits; stop search */
14631 break;
14632 }
14633 }
14634
14635 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14636 == (c0 & 0xff))
14637 continue;
14638 }
14639
14640 /* replace string */
14641 ws = smp[n].sm_to_w;
14642 s = smp[n].sm_rules;
14643 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14644 if (p0 == 1 && z == 0)
14645 {
14646 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014647 if (reslen > 0 && ws != NULL && *ws != NUL
14648 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014649 || wres[reslen - 1] == *ws))
14650 reslen--;
14651 z0 = 1;
14652 z = 1;
14653 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014654 if (ws != NULL)
14655 while (*ws != NUL && word[i + k0] != NUL)
14656 {
14657 word[i + k0] = *ws;
14658 k0++;
14659 ws++;
14660 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014661 if (k > k0)
14662 mch_memmove(word + i + k0, word + i + k,
14663 sizeof(int) * (STRLEN(word + i + k) + 1));
14664
14665 /* new "actual letter" */
14666 c = word[i];
14667 }
14668 else
14669 {
14670 /* no '<' rule used */
14671 i += k - 1;
14672 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014673 if (ws != NULL)
14674 while (*ws != NUL && ws[1] != NUL
14675 && reslen < MAXWLEN)
14676 {
14677 if (reslen == 0 || wres[reslen - 1] != *ws)
14678 wres[reslen++] = *ws;
14679 ws++;
14680 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014681 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014682 if (ws == NULL)
14683 c = NUL;
14684 else
14685 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014686 if (strstr((char *)s, "^^") != NULL)
14687 {
14688 if (c != NUL)
14689 wres[reslen++] = c;
14690 mch_memmove(word, word + i + 1,
14691 sizeof(int) * (STRLEN(word + i + 1) + 1));
14692 i = 0;
14693 z0 = 1;
14694 }
14695 }
14696 break;
14697 }
14698 }
14699 }
14700 else if (vim_iswhite(c))
14701 {
14702 c = ' ';
14703 k = 1;
14704 }
14705
14706 if (z0 == 0)
14707 {
14708 if (k && !p0 && reslen < MAXWLEN && c != NUL
14709 && (!slang->sl_collapse || reslen == 0
14710 || wres[reslen - 1] != c))
14711 /* condense only double letters */
14712 wres[reslen++] = c;
14713
14714 i++;
14715 z = 0;
14716 k = 0;
14717 }
14718 }
14719
14720 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14721 l = 0;
14722 for (n = 0; n < reslen; ++n)
14723 {
14724 l += mb_char2bytes(wres[n], res + l);
14725 if (l + MB_MAXBYTES > MAXWLEN)
14726 break;
14727 }
14728 res[l] = NUL;
14729}
14730#endif
14731
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014732/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014733 * Compute a score for two sound-a-like words.
14734 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14735 * Instead of a generic loop we write out the code. That keeps it fast by
14736 * avoiding checks that will not be possible.
14737 */
14738 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014739soundalike_score(goodstart, badstart)
14740 char_u *goodstart; /* sound-folded good word */
14741 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014742{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014743 char_u *goodsound = goodstart;
14744 char_u *badsound = badstart;
14745 int goodlen;
14746 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014747 int n;
14748 char_u *pl, *ps;
14749 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014750 int score = 0;
14751
14752 /* adding/inserting "*" at the start (word starts with vowel) shouldn't be
14753 * counted so much, vowels halfway the word aren't counted at all. */
14754 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14755 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000014756 if (badsound[1] == goodsound[1]
14757 || (badsound[1] != NUL
14758 && goodsound[1] != NUL
14759 && badsound[2] == goodsound[2]))
14760 {
14761 /* handle like a substitute */
14762 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014763 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014764 {
14765 score = 2 * SCORE_DEL / 3;
14766 if (*badsound == '*')
14767 ++badsound;
14768 else
14769 ++goodsound;
14770 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014771 }
14772
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014773 goodlen = (int)STRLEN(goodsound);
14774 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014775
Bram Moolenaarf711faf2007-05-10 16:48:19 +000014776 /* Return quickly if the lengths are too different to be fixed by two
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014777 * changes. */
14778 n = goodlen - badlen;
14779 if (n < -2 || n > 2)
14780 return SCORE_MAXMAX;
14781
14782 if (n > 0)
14783 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014784 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014785 ps = badsound;
14786 }
14787 else
14788 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014789 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014790 ps = goodsound;
14791 }
14792
14793 /* Skip over the identical part. */
14794 while (*pl == *ps && *pl != NUL)
14795 {
14796 ++pl;
14797 ++ps;
14798 }
14799
14800 switch (n)
14801 {
14802 case -2:
14803 case 2:
14804 /*
14805 * Must delete two characters from "pl".
14806 */
14807 ++pl; /* first delete */
14808 while (*pl == *ps)
14809 {
14810 ++pl;
14811 ++ps;
14812 }
14813 /* strings must be equal after second delete */
14814 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014815 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014816
14817 /* Failed to compare. */
14818 break;
14819
14820 case -1:
14821 case 1:
14822 /*
14823 * Minimal one delete from "pl" required.
14824 */
14825
14826 /* 1: delete */
14827 pl2 = pl + 1;
14828 ps2 = ps;
14829 while (*pl2 == *ps2)
14830 {
14831 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014832 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014833 ++pl2;
14834 ++ps2;
14835 }
14836
14837 /* 2: delete then swap, then rest must be equal */
14838 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14839 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014840 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014841
14842 /* 3: delete then substitute, then the rest must be equal */
14843 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014844 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014845
14846 /* 4: first swap then delete */
14847 if (pl[0] == ps[1] && pl[1] == ps[0])
14848 {
14849 pl2 = pl + 2; /* swap, skip two chars */
14850 ps2 = ps + 2;
14851 while (*pl2 == *ps2)
14852 {
14853 ++pl2;
14854 ++ps2;
14855 }
14856 /* delete a char and then strings must be equal */
14857 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014858 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014859 }
14860
14861 /* 5: first substitute then delete */
14862 pl2 = pl + 1; /* substitute, skip one char */
14863 ps2 = ps + 1;
14864 while (*pl2 == *ps2)
14865 {
14866 ++pl2;
14867 ++ps2;
14868 }
14869 /* delete a char and then strings must be equal */
14870 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014871 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014872
14873 /* Failed to compare. */
14874 break;
14875
14876 case 0:
14877 /*
Bram Moolenaar6ae167a2009-02-11 16:58:49 +000014878 * Lengths are equal, thus changes must result in same length: An
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014879 * insert is only possible in combination with a delete.
14880 * 1: check if for identical strings
14881 */
14882 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014883 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014884
14885 /* 2: swap */
14886 if (pl[0] == ps[1] && pl[1] == ps[0])
14887 {
14888 pl2 = pl + 2; /* swap, skip two chars */
14889 ps2 = ps + 2;
14890 while (*pl2 == *ps2)
14891 {
14892 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014893 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014894 ++pl2;
14895 ++ps2;
14896 }
14897 /* 3: swap and swap again */
14898 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14899 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014900 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014901
14902 /* 4: swap and substitute */
14903 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014904 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014905 }
14906
14907 /* 5: substitute */
14908 pl2 = pl + 1;
14909 ps2 = ps + 1;
14910 while (*pl2 == *ps2)
14911 {
14912 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014913 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014914 ++pl2;
14915 ++ps2;
14916 }
14917
14918 /* 6: substitute and swap */
14919 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14920 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014921 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014922
14923 /* 7: substitute and substitute */
14924 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014925 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014926
14927 /* 8: insert then delete */
14928 pl2 = pl;
14929 ps2 = ps + 1;
14930 while (*pl2 == *ps2)
14931 {
14932 ++pl2;
14933 ++ps2;
14934 }
14935 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014936 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014937
14938 /* 9: delete then insert */
14939 pl2 = pl + 1;
14940 ps2 = ps;
14941 while (*pl2 == *ps2)
14942 {
14943 ++pl2;
14944 ++ps2;
14945 }
14946 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014947 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014948
14949 /* Failed to compare. */
14950 break;
14951 }
14952
14953 return SCORE_MAXMAX;
14954}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014955
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014956/*
14957 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014958 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014959 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000014960 * The algorithm is described by Du and Chang, 1992.
14961 * The implementation of the algorithm comes from Aspell editdist.cpp,
14962 * edit_distance(). It has been converted from C++ to C and modified to
14963 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014964 */
14965 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000014966spell_edit_score(slang, badword, goodword)
14967 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014968 char_u *badword;
14969 char_u *goodword;
14970{
14971 int *cnt;
Bram Moolenaarf711faf2007-05-10 16:48:19 +000014972 int badlen, goodlen; /* lengths including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014973 int j, i;
14974 int t;
14975 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014976 int pbc, pgc;
14977#ifdef FEAT_MBYTE
14978 char_u *p;
14979 int wbadword[MAXWLEN];
14980 int wgoodword[MAXWLEN];
14981
14982 if (has_mbyte)
14983 {
14984 /* Get the characters from the multi-byte strings and put them in an
14985 * int array for easy access. */
14986 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014987 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014988 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014989 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014990 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000014991 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014992 }
14993 else
14994#endif
14995 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014996 badlen = (int)STRLEN(badword) + 1;
14997 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014998 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014999
15000 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
15001#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015002 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
15003 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015004 if (cnt == NULL)
15005 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015006
15007 CNT(0, 0) = 0;
15008 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015009 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015010
15011 for (i = 1; i <= badlen; ++i)
15012 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015013 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015014 for (j = 1; j <= goodlen; ++j)
15015 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015016#ifdef FEAT_MBYTE
15017 if (has_mbyte)
15018 {
15019 bc = wbadword[i - 1];
15020 gc = wgoodword[j - 1];
15021 }
15022 else
15023#endif
15024 {
15025 bc = badword[i - 1];
15026 gc = goodword[j - 1];
15027 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015028 if (bc == gc)
15029 CNT(i, j) = CNT(i - 1, j - 1);
15030 else
15031 {
15032 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015033 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015034 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
15035 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000015036 {
15037 /* For a similar character use SCORE_SIMILAR. */
15038 if (slang != NULL
15039 && slang->sl_has_map
15040 && similar_chars(slang, gc, bc))
15041 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
15042 else
15043 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
15044 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015045
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015046 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015047 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015048#ifdef FEAT_MBYTE
15049 if (has_mbyte)
15050 {
15051 pbc = wbadword[i - 2];
15052 pgc = wgoodword[j - 2];
15053 }
15054 else
15055#endif
15056 {
15057 pbc = badword[i - 2];
15058 pgc = goodword[j - 2];
15059 }
15060 if (bc == pgc && pbc == gc)
15061 {
15062 t = SCORE_SWAP + CNT(i - 2, j - 2);
15063 if (t < CNT(i, j))
15064 CNT(i, j) = t;
15065 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015066 }
15067 t = SCORE_DEL + CNT(i - 1, j);
15068 if (t < CNT(i, j))
15069 CNT(i, j) = t;
15070 t = SCORE_INS + CNT(i, j - 1);
15071 if (t < CNT(i, j))
15072 CNT(i, j) = t;
15073 }
15074 }
15075 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015076
15077 i = CNT(badlen - 1, goodlen - 1);
15078 vim_free(cnt);
15079 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015080}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000015081
Bram Moolenaar4770d092006-01-12 23:22:24 +000015082typedef struct
15083{
15084 int badi;
15085 int goodi;
15086 int score;
15087} limitscore_T;
15088
15089/*
15090 * Like spell_edit_score(), but with a limit on the score to make it faster.
15091 * May return SCORE_MAXMAX when the score is higher than "limit".
15092 *
15093 * This uses a stack for the edits still to be tried.
15094 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
15095 * for multi-byte characters.
15096 */
15097 static int
15098spell_edit_score_limit(slang, badword, goodword, limit)
15099 slang_T *slang;
15100 char_u *badword;
15101 char_u *goodword;
15102 int limit;
15103{
15104 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15105 int stackidx;
15106 int bi, gi;
15107 int bi2, gi2;
15108 int bc, gc;
15109 int score;
15110 int score_off;
15111 int minscore;
15112 int round;
15113
15114#ifdef FEAT_MBYTE
15115 /* Multi-byte characters require a bit more work, use a different function
15116 * to avoid testing "has_mbyte" quite often. */
15117 if (has_mbyte)
15118 return spell_edit_score_limit_w(slang, badword, goodword, limit);
15119#endif
15120
15121 /*
15122 * The idea is to go from start to end over the words. So long as
15123 * characters are equal just continue, this always gives the lowest score.
15124 * When there is a difference try several alternatives. Each alternative
15125 * increases "score" for the edit distance. Some of the alternatives are
15126 * pushed unto a stack and tried later, some are tried right away. At the
15127 * end of the word the score for one alternative is known. The lowest
15128 * possible score is stored in "minscore".
15129 */
15130 stackidx = 0;
15131 bi = 0;
15132 gi = 0;
15133 score = 0;
15134 minscore = limit + 1;
15135
15136 for (;;)
15137 {
15138 /* Skip over an equal part, score remains the same. */
15139 for (;;)
15140 {
15141 bc = badword[bi];
15142 gc = goodword[gi];
15143 if (bc != gc) /* stop at a char that's different */
15144 break;
15145 if (bc == NUL) /* both words end */
15146 {
15147 if (score < minscore)
15148 minscore = score;
15149 goto pop; /* do next alternative */
15150 }
15151 ++bi;
15152 ++gi;
15153 }
15154
15155 if (gc == NUL) /* goodword ends, delete badword chars */
15156 {
15157 do
15158 {
15159 if ((score += SCORE_DEL) >= minscore)
15160 goto pop; /* do next alternative */
15161 } while (badword[++bi] != NUL);
15162 minscore = score;
15163 }
15164 else if (bc == NUL) /* badword ends, insert badword chars */
15165 {
15166 do
15167 {
15168 if ((score += SCORE_INS) >= minscore)
15169 goto pop; /* do next alternative */
15170 } while (goodword[++gi] != NUL);
15171 minscore = score;
15172 }
15173 else /* both words continue */
15174 {
15175 /* If not close to the limit, perform a change. Only try changes
15176 * that may lead to a lower score than "minscore".
15177 * round 0: try deleting a char from badword
15178 * round 1: try inserting a char in badword */
15179 for (round = 0; round <= 1; ++round)
15180 {
15181 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15182 if (score_off < minscore)
15183 {
15184 if (score_off + SCORE_EDIT_MIN >= minscore)
15185 {
15186 /* Near the limit, rest of the words must match. We
15187 * can check that right now, no need to push an item
15188 * onto the stack. */
15189 bi2 = bi + 1 - round;
15190 gi2 = gi + round;
15191 while (goodword[gi2] == badword[bi2])
15192 {
15193 if (goodword[gi2] == NUL)
15194 {
15195 minscore = score_off;
15196 break;
15197 }
15198 ++bi2;
15199 ++gi2;
15200 }
15201 }
15202 else
15203 {
15204 /* try deleting/inserting a character later */
15205 stack[stackidx].badi = bi + 1 - round;
15206 stack[stackidx].goodi = gi + round;
15207 stack[stackidx].score = score_off;
15208 ++stackidx;
15209 }
15210 }
15211 }
15212
15213 if (score + SCORE_SWAP < minscore)
15214 {
15215 /* If swapping two characters makes a match then the
15216 * substitution is more expensive, thus there is no need to
15217 * try both. */
15218 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
15219 {
15220 /* Swap two characters, that is: skip them. */
15221 gi += 2;
15222 bi += 2;
15223 score += SCORE_SWAP;
15224 continue;
15225 }
15226 }
15227
15228 /* Substitute one character for another which is the same
15229 * thing as deleting a character from both goodword and badword.
15230 * Use a better score when there is only a case difference. */
15231 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15232 score += SCORE_ICASE;
15233 else
15234 {
15235 /* For a similar character use SCORE_SIMILAR. */
15236 if (slang != NULL
15237 && slang->sl_has_map
15238 && similar_chars(slang, gc, bc))
15239 score += SCORE_SIMILAR;
15240 else
15241 score += SCORE_SUBST;
15242 }
15243
15244 if (score < minscore)
15245 {
15246 /* Do the substitution. */
15247 ++gi;
15248 ++bi;
15249 continue;
15250 }
15251 }
15252pop:
15253 /*
15254 * Get here to try the next alternative, pop it from the stack.
15255 */
15256 if (stackidx == 0) /* stack is empty, finished */
15257 break;
15258
15259 /* pop an item from the stack */
15260 --stackidx;
15261 gi = stack[stackidx].goodi;
15262 bi = stack[stackidx].badi;
15263 score = stack[stackidx].score;
15264 }
15265
15266 /* When the score goes over "limit" it may actually be much higher.
15267 * Return a very large number to avoid going below the limit when giving a
15268 * bonus. */
15269 if (minscore > limit)
15270 return SCORE_MAXMAX;
15271 return minscore;
15272}
15273
15274#ifdef FEAT_MBYTE
15275/*
15276 * Multi-byte version of spell_edit_score_limit().
15277 * Keep it in sync with the above!
15278 */
15279 static int
15280spell_edit_score_limit_w(slang, badword, goodword, limit)
15281 slang_T *slang;
15282 char_u *badword;
15283 char_u *goodword;
15284 int limit;
15285{
15286 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15287 int stackidx;
15288 int bi, gi;
15289 int bi2, gi2;
15290 int bc, gc;
15291 int score;
15292 int score_off;
15293 int minscore;
15294 int round;
15295 char_u *p;
15296 int wbadword[MAXWLEN];
15297 int wgoodword[MAXWLEN];
15298
15299 /* Get the characters from the multi-byte strings and put them in an
15300 * int array for easy access. */
15301 bi = 0;
15302 for (p = badword; *p != NUL; )
15303 wbadword[bi++] = mb_cptr2char_adv(&p);
15304 wbadword[bi++] = 0;
15305 gi = 0;
15306 for (p = goodword; *p != NUL; )
15307 wgoodword[gi++] = mb_cptr2char_adv(&p);
15308 wgoodword[gi++] = 0;
15309
15310 /*
15311 * The idea is to go from start to end over the words. So long as
15312 * characters are equal just continue, this always gives the lowest score.
15313 * When there is a difference try several alternatives. Each alternative
15314 * increases "score" for the edit distance. Some of the alternatives are
15315 * pushed unto a stack and tried later, some are tried right away. At the
15316 * end of the word the score for one alternative is known. The lowest
15317 * possible score is stored in "minscore".
15318 */
15319 stackidx = 0;
15320 bi = 0;
15321 gi = 0;
15322 score = 0;
15323 minscore = limit + 1;
15324
15325 for (;;)
15326 {
15327 /* Skip over an equal part, score remains the same. */
15328 for (;;)
15329 {
15330 bc = wbadword[bi];
15331 gc = wgoodword[gi];
15332
15333 if (bc != gc) /* stop at a char that's different */
15334 break;
15335 if (bc == NUL) /* both words end */
15336 {
15337 if (score < minscore)
15338 minscore = score;
15339 goto pop; /* do next alternative */
15340 }
15341 ++bi;
15342 ++gi;
15343 }
15344
15345 if (gc == NUL) /* goodword ends, delete badword chars */
15346 {
15347 do
15348 {
15349 if ((score += SCORE_DEL) >= minscore)
15350 goto pop; /* do next alternative */
15351 } while (wbadword[++bi] != NUL);
15352 minscore = score;
15353 }
15354 else if (bc == NUL) /* badword ends, insert badword chars */
15355 {
15356 do
15357 {
15358 if ((score += SCORE_INS) >= minscore)
15359 goto pop; /* do next alternative */
15360 } while (wgoodword[++gi] != NUL);
15361 minscore = score;
15362 }
15363 else /* both words continue */
15364 {
15365 /* If not close to the limit, perform a change. Only try changes
15366 * that may lead to a lower score than "minscore".
15367 * round 0: try deleting a char from badword
15368 * round 1: try inserting a char in badword */
15369 for (round = 0; round <= 1; ++round)
15370 {
15371 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15372 if (score_off < minscore)
15373 {
15374 if (score_off + SCORE_EDIT_MIN >= minscore)
15375 {
15376 /* Near the limit, rest of the words must match. We
15377 * can check that right now, no need to push an item
15378 * onto the stack. */
15379 bi2 = bi + 1 - round;
15380 gi2 = gi + round;
15381 while (wgoodword[gi2] == wbadword[bi2])
15382 {
15383 if (wgoodword[gi2] == NUL)
15384 {
15385 minscore = score_off;
15386 break;
15387 }
15388 ++bi2;
15389 ++gi2;
15390 }
15391 }
15392 else
15393 {
15394 /* try deleting a character from badword later */
15395 stack[stackidx].badi = bi + 1 - round;
15396 stack[stackidx].goodi = gi + round;
15397 stack[stackidx].score = score_off;
15398 ++stackidx;
15399 }
15400 }
15401 }
15402
15403 if (score + SCORE_SWAP < minscore)
15404 {
15405 /* If swapping two characters makes a match then the
15406 * substitution is more expensive, thus there is no need to
15407 * try both. */
15408 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
15409 {
15410 /* Swap two characters, that is: skip them. */
15411 gi += 2;
15412 bi += 2;
15413 score += SCORE_SWAP;
15414 continue;
15415 }
15416 }
15417
15418 /* Substitute one character for another which is the same
15419 * thing as deleting a character from both goodword and badword.
15420 * Use a better score when there is only a case difference. */
15421 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15422 score += SCORE_ICASE;
15423 else
15424 {
15425 /* For a similar character use SCORE_SIMILAR. */
15426 if (slang != NULL
15427 && slang->sl_has_map
15428 && similar_chars(slang, gc, bc))
15429 score += SCORE_SIMILAR;
15430 else
15431 score += SCORE_SUBST;
15432 }
15433
15434 if (score < minscore)
15435 {
15436 /* Do the substitution. */
15437 ++gi;
15438 ++bi;
15439 continue;
15440 }
15441 }
15442pop:
15443 /*
15444 * Get here to try the next alternative, pop it from the stack.
15445 */
15446 if (stackidx == 0) /* stack is empty, finished */
15447 break;
15448
15449 /* pop an item from the stack */
15450 --stackidx;
15451 gi = stack[stackidx].goodi;
15452 bi = stack[stackidx].badi;
15453 score = stack[stackidx].score;
15454 }
15455
15456 /* When the score goes over "limit" it may actually be much higher.
15457 * Return a very large number to avoid going below the limit when giving a
15458 * bonus. */
15459 if (minscore > limit)
15460 return SCORE_MAXMAX;
15461 return minscore;
15462}
15463#endif
15464
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015465/*
15466 * ":spellinfo"
15467 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015468 void
15469ex_spellinfo(eap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000015470 exarg_T *eap UNUSED;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015471{
15472 int lpi;
15473 langp_T *lp;
15474 char_u *p;
15475
15476 if (no_spell_checking(curwin))
15477 return;
15478
15479 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +020015480 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015481 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015482 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015483 msg_puts((char_u *)"file: ");
15484 msg_puts(lp->lp_slang->sl_fname);
15485 msg_putchar('\n');
15486 p = lp->lp_slang->sl_info;
15487 if (p != NULL)
15488 {
15489 msg_puts(p);
15490 msg_putchar('\n');
15491 }
15492 }
15493 msg_end();
15494}
15495
Bram Moolenaar4770d092006-01-12 23:22:24 +000015496#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
15497#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015498#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +000015499#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
15500#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015501
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015502/*
15503 * ":spelldump"
15504 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015505 void
15506ex_spelldump(eap)
15507 exarg_T *eap;
15508{
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015509 if (no_spell_checking(curwin))
15510 return;
15511
15512 /* Create a new empty buffer by splitting the window. */
15513 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar860cae12010-06-05 23:22:07 +020015514 if (!bufempty() || !buf_valid(curbuf))
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015515 return;
15516
Bram Moolenaar860cae12010-06-05 23:22:07 +020015517 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015518
15519 /* Delete the empty line that we started with. */
15520 if (curbuf->b_ml.ml_line_count > 1)
15521 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
15522
15523 redraw_later(NOT_VALID);
15524}
15525
15526/*
15527 * Go through all possible words and:
15528 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
15529 * "ic" and "dir" are not used.
15530 * 2. When "pat" is not NULL: add matching words to insert mode completion.
15531 */
15532 void
Bram Moolenaar860cae12010-06-05 23:22:07 +020015533spell_dump_compl(pat, ic, dir, dumpflags_arg)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015534 char_u *pat; /* leading part of the word */
15535 int ic; /* ignore case */
15536 int *dir; /* direction for adding matches */
15537 int dumpflags_arg; /* DUMPFLAG_* */
15538{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015539 langp_T *lp;
15540 slang_T *slang;
15541 idx_T arridx[MAXWLEN];
15542 int curi[MAXWLEN];
15543 char_u word[MAXWLEN];
15544 int c;
15545 char_u *byts;
15546 idx_T *idxs;
15547 linenr_T lnum = 0;
15548 int round;
15549 int depth;
15550 int n;
15551 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015552 char_u *region_names = NULL; /* region names being used */
15553 int do_region = TRUE; /* dump region names and numbers */
15554 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015555 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015556 int dumpflags = dumpflags_arg;
15557 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015558
Bram Moolenaard0131a82006-03-04 21:46:13 +000015559 /* When ignoring case or when the pattern starts with capital pass this on
15560 * to dump_word(). */
15561 if (pat != NULL)
15562 {
15563 if (ic)
15564 dumpflags |= DUMPFLAG_ICASE;
15565 else
15566 {
15567 n = captype(pat, NULL);
15568 if (n == WF_ONECAP)
15569 dumpflags |= DUMPFLAG_ONECAP;
15570 else if (n == WF_ALLCAP
15571#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015572 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015573#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015574 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +000015575#endif
15576 )
15577 dumpflags |= DUMPFLAG_ALLCAP;
15578 }
15579 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015580
Bram Moolenaar7887d882005-07-01 22:33:52 +000015581 /* Find out if we can support regions: All languages must support the same
15582 * regions or none at all. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020015583 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000015584 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015585 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000015586 p = lp->lp_slang->sl_regions;
15587 if (p[0] != 0)
15588 {
15589 if (region_names == NULL) /* first language with regions */
15590 region_names = p;
15591 else if (STRCMP(region_names, p) != 0)
15592 {
15593 do_region = FALSE; /* region names are different */
15594 break;
15595 }
15596 }
15597 }
15598
15599 if (do_region && region_names != NULL)
15600 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015601 if (pat == NULL)
15602 {
15603 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15604 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15605 }
Bram Moolenaar7887d882005-07-01 22:33:52 +000015606 }
15607 else
15608 do_region = FALSE;
15609
15610 /*
15611 * Loop over all files loaded for the entries in 'spelllang'.
15612 */
Bram Moolenaar860cae12010-06-05 23:22:07 +020015613 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015614 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015615 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015616 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015617 if (slang->sl_fbyts == NULL) /* reloading failed */
15618 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015619
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015620 if (pat == NULL)
15621 {
15622 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15623 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15624 }
15625
15626 /* When matching with a pattern and there are no prefixes only use
15627 * parts of the tree that match "pat". */
15628 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015629 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015630 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015631 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015632
15633 /* round 1: case-folded tree
15634 * round 2: keep-case tree */
15635 for (round = 1; round <= 2; ++round)
15636 {
15637 if (round == 1)
15638 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015639 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015640 byts = slang->sl_fbyts;
15641 idxs = slang->sl_fidxs;
15642 }
15643 else
15644 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015645 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015646 byts = slang->sl_kbyts;
15647 idxs = slang->sl_kidxs;
15648 }
15649 if (byts == NULL)
15650 continue; /* array is empty */
15651
15652 depth = 0;
15653 arridx[0] = 0;
15654 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015655 while (depth >= 0 && !got_int
15656 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015657 {
15658 if (curi[depth] > byts[arridx[depth]])
15659 {
15660 /* Done all bytes at this node, go up one level. */
15661 --depth;
15662 line_breakcheck();
Bram Moolenaara2031822006-03-07 22:29:51 +000015663 ins_compl_check_keys(50);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015664 }
15665 else
15666 {
15667 /* Do one more byte at this node. */
15668 n = arridx[depth] + curi[depth];
15669 ++curi[depth];
15670 c = byts[n];
15671 if (c == 0)
15672 {
15673 /* End of word, deal with the word.
15674 * Don't use keep-case words in the fold-case tree,
15675 * they will appear in the keep-case tree.
15676 * Only use the word when the region matches. */
15677 flags = (int)idxs[n];
15678 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015679 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000015680 && (do_region
15681 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015682 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015683 & lp->lp_region) != 0))
15684 {
15685 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015686 if (!do_region)
15687 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015688
15689 /* Dump the basic word if there is no prefix or
15690 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015691 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015692 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015693 {
15694 dump_word(slang, word, pat, dir,
15695 dumpflags, flags, lnum);
15696 if (pat == NULL)
15697 ++lnum;
15698 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015699
15700 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015701 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015702 lnum = dump_prefixes(slang, word, pat, dir,
15703 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015704 }
15705 }
15706 else
15707 {
15708 /* Normal char, go one level deeper. */
15709 word[depth++] = c;
15710 arridx[depth] = idxs[n];
15711 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015712
15713 /* Check if this characters matches with the pattern.
15714 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +000015715 * Always ignore case here, dump_word() will check
15716 * proper case later. This isn't exactly right when
15717 * length changes for multi-byte characters with
15718 * ignore case... */
15719 if (depth <= patlen
15720 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015721 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015722 }
15723 }
15724 }
15725 }
15726 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015727}
15728
15729/*
15730 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015731 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015732 */
15733 static void
Bram Moolenaard0131a82006-03-04 21:46:13 +000015734dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015735 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015736 char_u *word;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015737 char_u *pat;
15738 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015739 int dumpflags;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015740 int wordflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015741 linenr_T lnum;
15742{
15743 int keepcap = FALSE;
15744 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015745 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015746 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000015747 char_u badword[MAXWLEN + 10];
15748 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015749 int flags = wordflags;
15750
15751 if (dumpflags & DUMPFLAG_ONECAP)
15752 flags |= WF_ONECAP;
15753 if (dumpflags & DUMPFLAG_ALLCAP)
15754 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015755
Bram Moolenaar4770d092006-01-12 23:22:24 +000015756 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015757 {
15758 /* Need to fix case according to "flags". */
15759 make_case_word(word, cword, flags);
15760 p = cword;
15761 }
15762 else
15763 {
15764 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015765 if ((dumpflags & DUMPFLAG_KEEPCASE)
15766 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015767 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015768 keepcap = TRUE;
15769 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015770 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015771
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015772 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015773 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015774 /* Add flags and regions after a slash. */
15775 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015776 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015777 STRCPY(badword, p);
15778 STRCAT(badword, "/");
15779 if (keepcap)
15780 STRCAT(badword, "=");
15781 if (flags & WF_BANNED)
15782 STRCAT(badword, "!");
15783 else if (flags & WF_RARE)
15784 STRCAT(badword, "?");
15785 if (flags & WF_REGION)
15786 for (i = 0; i < 7; ++i)
15787 if (flags & (0x10000 << i))
15788 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
15789 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015790 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015791
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015792 if (dumpflags & DUMPFLAG_COUNT)
15793 {
15794 hashitem_T *hi;
15795
15796 /* Include the word count for ":spelldump!". */
15797 hi = hash_find(&slang->sl_wordcount, tw);
15798 if (!HASHITEM_EMPTY(hi))
15799 {
15800 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15801 tw, HI2WC(hi)->wc_count);
15802 p = IObuff;
15803 }
15804 }
15805
15806 ml_append(lnum, p, (colnr_T)0, FALSE);
15807 }
Bram Moolenaard0131a82006-03-04 21:46:13 +000015808 else if (((dumpflags & DUMPFLAG_ICASE)
15809 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
15810 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015811 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaare8c3a142006-08-29 14:30:35 +000015812 p_ic, NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015813 /* if dir was BACKWARD then honor it just once */
15814 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015815}
15816
15817/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000015818 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15819 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015820 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015821 * Return the updated line number.
15822 */
15823 static linenr_T
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015824dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015825 slang_T *slang;
15826 char_u *word; /* case-folded word */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015827 char_u *pat;
15828 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015829 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015830 int flags; /* flags with prefix ID */
15831 linenr_T startlnum;
15832{
15833 idx_T arridx[MAXWLEN];
15834 int curi[MAXWLEN];
15835 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015836 char_u word_up[MAXWLEN];
15837 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015838 int c;
15839 char_u *byts;
15840 idx_T *idxs;
15841 linenr_T lnum = startlnum;
15842 int depth;
15843 int n;
15844 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015845 int i;
15846
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015847 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015848 * upper-case letter in word_up[]. */
15849 c = PTR2CHAR(word);
15850 if (SPELL_TOUPPER(c) != c)
15851 {
15852 onecap_copy(word, word_up, TRUE);
15853 has_word_up = TRUE;
15854 }
15855
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015856 byts = slang->sl_pbyts;
15857 idxs = slang->sl_pidxs;
15858 if (byts != NULL) /* array not is empty */
15859 {
15860 /*
15861 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015862 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015863 */
15864 depth = 0;
15865 arridx[0] = 0;
15866 curi[0] = 1;
15867 while (depth >= 0 && !got_int)
15868 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015869 n = arridx[depth];
15870 len = byts[n];
15871 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015872 {
15873 /* Done all bytes at this node, go up one level. */
15874 --depth;
15875 line_breakcheck();
15876 }
15877 else
15878 {
15879 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015880 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015881 ++curi[depth];
15882 c = byts[n];
15883 if (c == 0)
15884 {
15885 /* End of prefix, find out how many IDs there are. */
15886 for (i = 1; i < len; ++i)
15887 if (byts[n + i] != 0)
15888 break;
15889 curi[depth] += i - 1;
15890
Bram Moolenaar53805d12005-08-01 07:08:33 +000015891 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15892 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015893 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015894 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015895 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015896 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015897 : flags, lnum);
15898 if (lnum != 0)
15899 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015900 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000015901
15902 /* Check for prefix that matches the word when the
15903 * first letter is upper-case, but only if the prefix has
15904 * a condition. */
15905 if (has_word_up)
15906 {
15907 c = valid_word_prefix(i, n, flags, word_up, slang,
15908 TRUE);
15909 if (c != 0)
15910 {
15911 vim_strncpy(prefix + depth, word_up,
15912 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015913 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015914 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015915 : flags, lnum);
15916 if (lnum != 0)
15917 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +000015918 }
15919 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015920 }
15921 else
15922 {
15923 /* Normal char, go one level deeper. */
15924 prefix[depth++] = c;
15925 arridx[depth] = idxs[n];
15926 curi[depth] = 1;
15927 }
15928 }
15929 }
15930 }
15931
15932 return lnum;
15933}
15934
Bram Moolenaar95529562005-08-25 21:21:38 +000015935/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015936 * Move "p" to the end of word "start".
15937 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000015938 */
15939 char_u *
Bram Moolenaar860cae12010-06-05 23:22:07 +020015940spell_to_word_end(start, win)
Bram Moolenaar95529562005-08-25 21:21:38 +000015941 char_u *start;
Bram Moolenaar860cae12010-06-05 23:22:07 +020015942 win_T *win;
Bram Moolenaar95529562005-08-25 21:21:38 +000015943{
15944 char_u *p = start;
15945
Bram Moolenaar860cae12010-06-05 23:22:07 +020015946 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar95529562005-08-25 21:21:38 +000015947 mb_ptr_adv(p);
15948 return p;
15949}
15950
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015951#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015952/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015953 * For Insert mode completion CTRL-X s:
15954 * Find start of the word in front of column "startcol".
15955 * We don't check if it is badly spelled, with completion we can only change
15956 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015957 * Returns the column number of the word.
15958 */
15959 int
15960spell_word_start(startcol)
15961 int startcol;
15962{
15963 char_u *line;
15964 char_u *p;
15965 int col = 0;
15966
Bram Moolenaar95529562005-08-25 21:21:38 +000015967 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015968 return startcol;
15969
15970 /* Find a word character before "startcol". */
15971 line = ml_get_curline();
15972 for (p = line + startcol; p > line; )
15973 {
15974 mb_ptr_back(line, p);
15975 if (spell_iswordp_nmw(p))
15976 break;
15977 }
15978
15979 /* Go back to start of the word. */
15980 while (p > line)
15981 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015982 col = (int)(p - line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015983 mb_ptr_back(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020015984 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015985 break;
15986 col = 0;
15987 }
15988
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015989 return col;
15990}
15991
15992/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000015993 * Need to check for 'spellcapcheck' now, the word is removed before
15994 * expand_spelling() is called. Therefore the ugly global variable.
15995 */
15996static int spell_expand_need_cap;
15997
15998 void
15999spell_expand_check_cap(col)
16000 colnr_T col;
16001{
16002 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
16003}
16004
16005/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016006 * Get list of spelling suggestions.
16007 * Used for Insert mode completion CTRL-X ?.
16008 * Returns the number of matches. The matches are in "matchp[]", array of
16009 * allocated strings.
16010 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016011 int
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000016012expand_spelling(lnum, pat, matchp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000016013 linenr_T lnum UNUSED;
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016014 char_u *pat;
16015 char_u ***matchp;
16016{
16017 garray_T ga;
16018
Bram Moolenaar4770d092006-01-12 23:22:24 +000016019 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016020 *matchp = ga.ga_data;
16021 return ga.ga_len;
16022}
16023#endif
16024
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000016025#endif /* FEAT_SPELL */