blob: 6716a6ca818aa1bb0d6ae28a265c77155fa36d20 [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
Bram Moolenaar56f78042010-12-08 17:09:32 +0100330#ifdef VMS
331# define SPL_FNAME_TMPL "%s_%s.spl"
332# define SPL_FNAME_ADD "_add."
333# define SPL_FNAME_ASCII "_ascii."
334#else
335# define SPL_FNAME_TMPL "%s.%s.spl"
336# define SPL_FNAME_ADD ".add."
337# define SPL_FNAME_ASCII ".ascii."
338#endif
339
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000340/* Flags used for a word. Only the lowest byte can be used, the region byte
341 * comes above it. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000342#define WF_REGION 0x01 /* region byte follows */
343#define WF_ONECAP 0x02 /* word with one capital (or all capitals) */
344#define WF_ALLCAP 0x04 /* word must be all capitals */
345#define WF_RARE 0x08 /* rare word */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000346#define WF_BANNED 0x10 /* bad word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000347#define WF_AFX 0x20 /* affix ID follows */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000348#define WF_FIXCAP 0x40 /* keep-case word, allcap not allowed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000349#define WF_KEEPCAP 0x80 /* keep-case word */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000350
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000351/* for <flags2>, shifted up one byte to be used in wn_flags */
352#define WF_HAS_AFF 0x0100 /* word includes affix */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +0000353#define WF_NEEDCOMP 0x0200 /* word only valid in compound */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000354#define WF_NOSUGGEST 0x0400 /* word not to be suggested */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000355#define WF_COMPROOT 0x0800 /* already compounded word, COMPOUNDROOT */
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000356#define WF_NOCOMPBEF 0x1000 /* no compounding before this word */
357#define WF_NOCOMPAFT 0x2000 /* no compounding after this word */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000358
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000359/* only used for su_badflags */
360#define WF_MIXCAP 0x20 /* mix of upper and lower case: macaRONI */
361
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000362#define WF_CAPMASK (WF_ONECAP | WF_ALLCAP | WF_KEEPCAP | WF_FIXCAP)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000363
Bram Moolenaar53805d12005-08-01 07:08:33 +0000364/* flags for <pflags> */
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000365#define WFP_RARE 0x01 /* rare prefix */
366#define WFP_NC 0x02 /* prefix is not combining */
367#define WFP_UP 0x04 /* to-upper prefix */
368#define WFP_COMPPERMIT 0x08 /* prefix with COMPOUNDPERMITFLAG */
369#define WFP_COMPFORBID 0x10 /* prefix with COMPOUNDFORBIDFLAG */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000370
Bram Moolenaar5555acc2006-04-07 21:33:12 +0000371/* Flags for postponed prefixes in "sl_pidxs". Must be above affixID (one
372 * byte) and prefcondnr (two bytes). */
373#define WF_RAREPFX (WFP_RARE << 24) /* rare postponed prefix */
374#define WF_PFX_NC (WFP_NC << 24) /* non-combining postponed prefix */
375#define WF_PFX_UP (WFP_UP << 24) /* to-upper postponed prefix */
376#define WF_PFX_COMPPERMIT (WFP_COMPPERMIT << 24) /* postponed prefix with
377 * COMPOUNDPERMITFLAG */
378#define WF_PFX_COMPFORBID (WFP_COMPFORBID << 24) /* postponed prefix with
379 * COMPOUNDFORBIDFLAG */
380
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000381
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000382/* flags for <compoptions> */
383#define COMP_CHECKDUP 1 /* CHECKCOMPOUNDDUP */
384#define COMP_CHECKREP 2 /* CHECKCOMPOUNDREP */
385#define COMP_CHECKCASE 4 /* CHECKCOMPOUNDCASE */
386#define COMP_CHECKTRIPLE 8 /* CHECKCOMPOUNDTRIPLE */
387
Bram Moolenaardfb9ac02005-07-05 21:36:03 +0000388/* Special byte values for <byte>. Some are only used in the tree for
389 * postponed prefixes, some only in the other trees. This is a bit messy... */
390#define BY_NOFLAGS 0 /* end of word without flags or region; for
Bram Moolenaar53805d12005-08-01 07:08:33 +0000391 * postponed prefix: no <pflags> */
392#define BY_INDEX 1 /* child is shared, index follows */
393#define BY_FLAGS 2 /* end of word, <flags> byte follows; for
394 * postponed prefix: <pflags> follows */
395#define BY_FLAGS2 3 /* end of word, <flags> and <flags2> bytes
396 * follow; never used in prefix tree */
397#define BY_SPECIAL BY_FLAGS2 /* highest special byte value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000398
Bram Moolenaar4770d092006-01-12 23:22:24 +0000399/* Info from "REP", "REPSAL" and "SAL" entries in ".aff" file used in si_rep,
400 * si_repsal, sl_rep, and si_sal. Not for sl_sal!
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000401 * One replacement: from "ft_from" to "ft_to". */
402typedef struct fromto_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000403{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000404 char_u *ft_from;
405 char_u *ft_to;
406} fromto_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000407
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000408/* Info from "SAL" entries in ".aff" file used in sl_sal.
409 * The info is split for quick processing by spell_soundfold().
410 * Note that "sm_oneof" and "sm_rules" point into sm_lead. */
411typedef struct salitem_S
412{
413 char_u *sm_lead; /* leading letters */
414 int sm_leadlen; /* length of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000415 char_u *sm_oneof; /* letters from () or NULL */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000416 char_u *sm_rules; /* rules like ^, $, priority */
417 char_u *sm_to; /* replacement. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000418#ifdef FEAT_MBYTE
419 int *sm_lead_w; /* wide character copy of "sm_lead" */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000420 int *sm_oneof_w; /* wide character copy of "sm_oneof" */
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000421 int *sm_to_w; /* wide character copy of "sm_to" */
422#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000423} salitem_T;
424
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000425#ifdef FEAT_MBYTE
426typedef int salfirst_T;
427#else
428typedef short salfirst_T;
429#endif
430
Bram Moolenaar5195e452005-08-19 20:32:47 +0000431/* Values for SP_*ERROR are negative, positive values are used by
432 * read_cnt_string(). */
433#define SP_TRUNCERROR -1 /* spell file truncated error */
434#define SP_FORMERROR -2 /* format error in spell file */
Bram Moolenaar6de68532005-08-24 22:08:48 +0000435#define SP_OTHERERROR -3 /* other error while reading spell file */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000436
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000437/*
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000438 * Structure used to store words and other info for one language, loaded from
439 * a .spl file.
Bram Moolenaar51485f02005-06-04 21:55:20 +0000440 * The main access is through the tree in "sl_fbyts/sl_fidxs", storing the
441 * case-folded words. "sl_kbyts/sl_kidxs" is for keep-case words.
442 *
443 * The "byts" array stores the possible bytes in each tree node, preceded by
444 * the number of possible bytes, sorted on byte value:
445 * <len> <byte1> <byte2> ...
446 * The "idxs" array stores the index of the child node corresponding to the
447 * byte in "byts".
448 * Exception: when the byte is zero, the word may end here and "idxs" holds
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000449 * the flags, region mask and affixID for the word. There may be several
450 * zeros in sequence for alternative flag/region/affixID combinations.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000451 */
452typedef struct slang_S slang_T;
453struct slang_S
454{
455 slang_T *sl_next; /* next language */
456 char_u *sl_name; /* language name "en", "en.rare", "nl", etc. */
Bram Moolenaarb765d632005-06-07 21:00:02 +0000457 char_u *sl_fname; /* name of .spl file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000458 int sl_add; /* TRUE if it's a .add file. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000459
Bram Moolenaar51485f02005-06-04 21:55:20 +0000460 char_u *sl_fbyts; /* case-folded word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000461 idx_T *sl_fidxs; /* case-folded word indexes */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000462 char_u *sl_kbyts; /* keep-case word bytes */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000463 idx_T *sl_kidxs; /* keep-case word indexes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000464 char_u *sl_pbyts; /* prefix tree word bytes */
465 idx_T *sl_pidxs; /* prefix tree word indexes */
466
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000467 char_u *sl_info; /* infotext string or NULL */
468
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000469 char_u sl_regions[17]; /* table with up to 8 region names plus NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000470
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000471 char_u *sl_midword; /* MIDWORD string or NULL */
472
Bram Moolenaar4770d092006-01-12 23:22:24 +0000473 hashtab_T sl_wordcount; /* hashtable with word count, wordcount_T */
474
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000475 int sl_compmax; /* COMPOUNDWORDMAX (default: MAXWLEN) */
Bram Moolenaarda2303d2005-08-30 21:55:26 +0000476 int sl_compminlen; /* COMPOUNDMIN (default: 0) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000477 int sl_compsylmax; /* COMPOUNDSYLMAX (default: MAXWLEN) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000478 int sl_compoptions; /* COMP_* flags */
479 garray_T sl_comppat; /* CHECKCOMPOUNDPATTERN items */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000480 regprog_T *sl_compprog; /* COMPOUNDRULE turned into a regexp progrm
Bram Moolenaar5195e452005-08-19 20:32:47 +0000481 * (NULL when no compounding) */
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000482 char_u *sl_comprules; /* all COMPOUNDRULE concatenated (or NULL) */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000483 char_u *sl_compstartflags; /* flags for first compound word */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000484 char_u *sl_compallflags; /* all flags for compound words */
Bram Moolenaar78622822005-08-23 21:00:13 +0000485 char_u sl_nobreak; /* When TRUE: no spaces between words */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000486 char_u *sl_syllable; /* SYLLABLE repeatable chars or NULL */
487 garray_T sl_syl_items; /* syllable items */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000488
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000489 int sl_prefixcnt; /* number of items in "sl_prefprog" */
490 regprog_T **sl_prefprog; /* table with regprogs for prefixes */
491
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000492 garray_T sl_rep; /* list of fromto_T entries from REP lines */
493 short sl_rep_first[256]; /* indexes where byte first appears, -1 if
494 there is none */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000495 garray_T sl_sal; /* list of salitem_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000496 salfirst_T sl_sal_first[256]; /* indexes where byte first appears, -1 if
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000497 there is none */
498 int sl_followup; /* SAL followup */
499 int sl_collapse; /* SAL collapse_result */
500 int sl_rem_accents; /* SAL remove_accents */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000501 int sl_sofo; /* SOFOFROM and SOFOTO instead of SAL items:
502 * "sl_sal_first" maps chars, when has_mbyte
503 * "sl_sal" is a list of wide char lists. */
504 garray_T sl_repsal; /* list of fromto_T entries from REPSAL lines */
505 short sl_repsal_first[256]; /* sl_rep_first for REPSAL lines */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000506 int sl_nosplitsugs; /* don't suggest splitting a word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000507
508 /* Info from the .sug file. Loaded on demand. */
509 time_t sl_sugtime; /* timestamp for .sug file */
510 char_u *sl_sbyts; /* soundfolded word bytes */
511 idx_T *sl_sidxs; /* soundfolded word indexes */
512 buf_T *sl_sugbuf; /* buffer with word number table */
513 int sl_sugloaded; /* TRUE when .sug file was loaded or failed to
514 load */
515
Bram Moolenaarea424162005-06-16 21:51:00 +0000516 int sl_has_map; /* TRUE if there is a MAP line */
517#ifdef FEAT_MBYTE
518 hashtab_T sl_map_hash; /* MAP for multi-byte chars */
519 int sl_map_array[256]; /* MAP for first 256 chars */
520#else
521 char_u sl_map_array[256]; /* MAP for first 256 chars */
522#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000523 hashtab_T sl_sounddone; /* table with soundfolded words that have
524 handled, see add_sound_suggest() */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000525};
526
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000527/* First language that is loaded, start of the linked list of loaded
528 * languages. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000529static slang_T *first_lang = NULL;
530
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000531/* Flags used in .spl file for soundsalike flags. */
532#define SAL_F0LLOWUP 1
533#define SAL_COLLAPSE 2
534#define SAL_REM_ACCENTS 4
535
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000536/*
537 * Structure used in "b_langp", filled from 'spelllang'.
538 */
539typedef struct langp_S
540{
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000541 slang_T *lp_slang; /* info for this language */
542 slang_T *lp_sallang; /* language used for sound folding or NULL */
543 slang_T *lp_replang; /* language used for REP items or NULL */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000544 int lp_region; /* bitmask for region or REGION_ALL */
545} langp_T;
546
547#define LANGP_ENTRY(ga, i) (((langp_T *)(ga).ga_data) + (i))
548
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000549#define REGION_ALL 0xff /* word valid in all regions */
550
Bram Moolenaar5195e452005-08-19 20:32:47 +0000551#define VIMSPELLMAGIC "VIMspell" /* string at start of Vim spell file */
552#define VIMSPELLMAGICL 8
553#define VIMSPELLVERSION 50
554
Bram Moolenaar4770d092006-01-12 23:22:24 +0000555#define VIMSUGMAGIC "VIMsug" /* string at start of Vim .sug file */
556#define VIMSUGMAGICL 6
557#define VIMSUGVERSION 1
558
Bram Moolenaar5195e452005-08-19 20:32:47 +0000559/* Section IDs. Only renumber them when VIMSPELLVERSION changes! */
560#define SN_REGION 0 /* <regionname> section */
561#define SN_CHARFLAGS 1 /* charflags section */
562#define SN_MIDWORD 2 /* <midword> section */
563#define SN_PREFCOND 3 /* <prefcond> section */
564#define SN_REP 4 /* REP items section */
565#define SN_SAL 5 /* SAL items section */
566#define SN_SOFO 6 /* soundfolding section */
567#define SN_MAP 7 /* MAP items section */
568#define SN_COMPOUND 8 /* compound words section */
569#define SN_SYLLABLE 9 /* syllable section */
Bram Moolenaar78622822005-08-23 21:00:13 +0000570#define SN_NOBREAK 10 /* NOBREAK section */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000571#define SN_SUGFILE 11 /* timestamp for .sug file */
572#define SN_REPSAL 12 /* REPSAL items section */
573#define SN_WORDS 13 /* common words */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000574#define SN_NOSPLITSUGS 14 /* don't split word for suggestions */
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000575#define SN_INFO 15 /* info section */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000576#define SN_END 255 /* end of sections */
577
578#define SNF_REQUIRED 1 /* <sectionflags>: required section */
579
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000580/* Result values. Lower number is accepted over higher one. */
581#define SP_BANNED -1
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000582#define SP_OK 0
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000583#define SP_RARE 1
584#define SP_LOCAL 2
585#define SP_BAD 3
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000586
Bram Moolenaar7887d882005-07-01 22:33:52 +0000587/* file used for "zG" and "zW" */
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000588static char_u *int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +0000589
Bram Moolenaar4770d092006-01-12 23:22:24 +0000590typedef struct wordcount_S
591{
592 short_u wc_count; /* nr of times word was seen */
593 char_u wc_word[1]; /* word, actually longer */
594} wordcount_T;
595
596static wordcount_T dumwc;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000597#define WC_KEY_OFF (unsigned)(dumwc.wc_word - (char_u *)&dumwc)
Bram Moolenaar4770d092006-01-12 23:22:24 +0000598#define HI2WC(hi) ((wordcount_T *)((hi)->hi_key - WC_KEY_OFF))
599#define MAXWORDCOUNT 0xffff
600
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000601/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000602 * Information used when looking for suggestions.
603 */
604typedef struct suginfo_S
605{
606 garray_T su_ga; /* suggestions, contains "suggest_T" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000607 int su_maxcount; /* max. number of suggestions displayed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000608 int su_maxscore; /* maximum score for adding to su_ga */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000609 int su_sfmaxscore; /* idem, for when doing soundfold words */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000610 garray_T su_sga; /* like su_ga, sound-folded scoring */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000611 char_u *su_badptr; /* start of bad word in line */
612 int su_badlen; /* length of detected bad word in line */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000613 int su_badflags; /* caps flags for bad word */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000614 char_u su_badword[MAXWLEN]; /* bad word truncated at su_badlen */
615 char_u su_fbadword[MAXWLEN]; /* su_badword case-folded */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000616 char_u su_sal_badword[MAXWLEN]; /* su_badword soundfolded */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000617 hashtab_T su_banned; /* table with banned words */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000618 slang_T *su_sallang; /* default language for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000619} suginfo_T;
620
621/* One word suggestion. Used in "si_ga". */
622typedef struct suggest_S
623{
624 char_u *st_word; /* suggested word, allocated string */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000625 int st_wordlen; /* STRLEN(st_word) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000626 int st_orglen; /* length of replaced text */
627 int st_score; /* lower is better */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000628 int st_altscore; /* used when st_score compares equal */
629 int st_salscore; /* st_score is for soundalike */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000630 int st_had_bonus; /* bonus already included in score */
Bram Moolenaar8b96d642005-09-05 22:05:30 +0000631 slang_T *st_slang; /* language used for sound folding */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000632} suggest_T;
633
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000634#define SUG(ga, i) (((suggest_T *)(ga).ga_data)[i])
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000635
Bram Moolenaar4770d092006-01-12 23:22:24 +0000636/* TRUE if a word appears in the list of banned words. */
637#define WAS_BANNED(su, word) (!HASHITEM_EMPTY(hash_find(&su->su_banned, word)))
638
Bram Moolenaar6949d1d2008-08-25 02:14:05 +0000639/* Number of suggestions kept when cleaning up. We need to keep more than
Bram Moolenaar4770d092006-01-12 23:22:24 +0000640 * what is displayed, because when rescore_suggestions() is called the score
641 * may change and wrong suggestions may be removed later. */
642#define SUG_CLEAN_COUNT(su) ((su)->su_maxcount < 130 ? 150 : (su)->su_maxcount + 20)
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000643
644/* Threshold for sorting and cleaning up suggestions. Don't want to keep lots
645 * of suggestions that are not going to be displayed. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000646#define SUG_MAX_COUNT(su) (SUG_CLEAN_COUNT(su) + 50)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000647
648/* score for various changes */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000649#define SCORE_SPLIT 149 /* split bad word */
Bram Moolenaare1438bb2006-03-01 22:01:55 +0000650#define SCORE_SPLIT_NO 249 /* split bad word with NOSPLITSUGS */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000651#define SCORE_ICASE 52 /* slightly different case */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000652#define SCORE_REGION 200 /* word is for different region */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000653#define SCORE_RARE 180 /* rare word */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000654#define SCORE_SWAP 75 /* swap two characters */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000655#define SCORE_SWAP3 110 /* swap two characters in three */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000656#define SCORE_REP 65 /* REP replacement */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000657#define SCORE_SUBST 93 /* substitute a character */
658#define SCORE_SIMILAR 33 /* substitute a similar character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000659#define SCORE_SUBCOMP 33 /* substitute a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000660#define SCORE_DEL 94 /* delete a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000661#define SCORE_DELDUP 66 /* delete a duplicated character */
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +0000662#define SCORE_DELCOMP 28 /* delete a composing character */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000663#define SCORE_INS 96 /* insert a character */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000664#define SCORE_INSDUP 67 /* insert a duplicate character */
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000665#define SCORE_INSCOMP 30 /* insert a composing character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +0000666#define SCORE_NONWORD 103 /* change non-word to word char */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000667
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000668#define SCORE_FILE 30 /* suggestion from a file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000669#define SCORE_MAXINIT 350 /* Initial maximum score: higher == slower.
670 * 350 allows for about three changes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000671
Bram Moolenaar4770d092006-01-12 23:22:24 +0000672#define SCORE_COMMON1 30 /* subtracted for words seen before */
673#define SCORE_COMMON2 40 /* subtracted for words often seen */
674#define SCORE_COMMON3 50 /* subtracted for words very often seen */
675#define SCORE_THRES2 10 /* word count threshold for COMMON2 */
676#define SCORE_THRES3 100 /* word count threshold for COMMON3 */
677
678/* When trying changed soundfold words it becomes slow when trying more than
679 * two changes. With less then two changes it's slightly faster but we miss a
680 * few good suggestions. In rare cases we need to try three of four changes.
681 */
682#define SCORE_SFMAX1 200 /* maximum score for first try */
683#define SCORE_SFMAX2 300 /* maximum score for second try */
684#define SCORE_SFMAX3 400 /* maximum score for third try */
685
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000686#define SCORE_BIG SCORE_INS * 3 /* big difference */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000687#define SCORE_MAXMAX 999999 /* accept any score */
688#define SCORE_LIMITMAX 350 /* for spell_edit_score_limit() */
689
690/* for spell_edit_score_limit() we need to know the minimum value of
691 * SCORE_ICASE, SCORE_SWAP, SCORE_DEL, SCORE_SIMILAR and SCORE_INS */
692#define SCORE_EDIT_MIN SCORE_SIMILAR
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000693
694/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000695 * Structure to store info for word matching.
696 */
697typedef struct matchinf_S
698{
699 langp_T *mi_lp; /* info for language and region */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000700
701 /* pointers to original text to be checked */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000702 char_u *mi_word; /* start of word being checked */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000703 char_u *mi_end; /* end of matching word so far */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000704 char_u *mi_fend; /* next char to be added to mi_fword */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000705 char_u *mi_cend; /* char after what was used for
706 mi_capflags */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000707
708 /* case-folded text */
709 char_u mi_fword[MAXWLEN + 1]; /* mi_word case-folded */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000710 int mi_fwordlen; /* nr of valid bytes in mi_fword */
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000711
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000712 /* for when checking word after a prefix */
713 int mi_prefarridx; /* index in sl_pidxs with list of
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000714 affixID/condition */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000715 int mi_prefcnt; /* number of entries at mi_prefarridx */
716 int mi_prefixlen; /* byte length of prefix */
Bram Moolenaar53805d12005-08-01 07:08:33 +0000717#ifdef FEAT_MBYTE
718 int mi_cprefixlen; /* byte length of prefix in original
719 case */
720#else
721# define mi_cprefixlen mi_prefixlen /* it's the same value */
722#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000723
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000724 /* for when checking a compound word */
725 int mi_compoff; /* start of following word offset */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000726 char_u mi_compflags[MAXWLEN]; /* flags for compound words used */
727 int mi_complen; /* nr of compound words used */
Bram Moolenaar899dddf2006-03-26 21:06:50 +0000728 int mi_compextra; /* nr of COMPOUNDROOT words */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000729
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +0000730 /* others */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000731 int mi_result; /* result so far: SP_BAD, SP_OK, etc. */
Bram Moolenaar51485f02005-06-04 21:55:20 +0000732 int mi_capflags; /* WF_ONECAP WF_ALLCAP WF_KEEPCAP */
Bram Moolenaar860cae12010-06-05 23:22:07 +0200733 win_T *mi_win; /* buffer being checked */
Bram Moolenaar78622822005-08-23 21:00:13 +0000734
735 /* for NOBREAK */
736 int mi_result2; /* "mi_resul" without following word */
737 char_u *mi_end2; /* "mi_end" without following word */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000738} matchinf_T;
739
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000740/*
741 * The tables used for recognizing word characters according to spelling.
742 * These are only used for the first 256 characters of 'encoding'.
743 */
744typedef struct spelltab_S
745{
746 char_u st_isw[256]; /* flags: is word char */
747 char_u st_isu[256]; /* flags: is uppercase char */
748 char_u st_fold[256]; /* chars: folded case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000749 char_u st_upper[256]; /* chars: upper case */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000750} spelltab_T;
751
752static spelltab_T spelltab;
753static int did_set_spelltab;
754
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000755#define CF_WORD 0x01
756#define CF_UPPER 0x02
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000757
758static void clear_spell_chartab __ARGS((spelltab_T *sp));
759static int set_spell_finish __ARGS((spelltab_T *new_st));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200760static int spell_iswordp __ARGS((char_u *p, win_T *wp));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000761static int spell_iswordp_nmw __ARGS((char_u *p));
762#ifdef FEAT_MBYTE
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +0000763static int spell_mb_isword_class __ARGS((int cl));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200764static int spell_iswordp_w __ARGS((int *p, win_T *wp));
Bram Moolenaar9c96f592005-06-30 21:52:39 +0000765#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000766static int write_spell_prefcond __ARGS((FILE *fd, garray_T *gap));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000767
768/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000769 * For finding suggestions: At each node in the tree these states are tried:
Bram Moolenaarea424162005-06-16 21:51:00 +0000770 */
771typedef enum
772{
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000773 STATE_START = 0, /* At start of node check for NUL bytes (goodword
774 * ends); if badword ends there is a match, otherwise
775 * try splitting word. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000776 STATE_NOPREFIX, /* try without prefix */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000777 STATE_SPLITUNDO, /* Undo splitting. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000778 STATE_ENDNUL, /* Past NUL bytes at start of the node. */
779 STATE_PLAIN, /* Use each byte of the node. */
780 STATE_DEL, /* Delete a byte from the bad word. */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000781 STATE_INS_PREP, /* Prepare for inserting bytes. */
Bram Moolenaarea424162005-06-16 21:51:00 +0000782 STATE_INS, /* Insert a byte in the bad word. */
783 STATE_SWAP, /* Swap two bytes. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000784 STATE_UNSWAP, /* Undo swap two characters. */
785 STATE_SWAP3, /* Swap two characters over three. */
786 STATE_UNSWAP3, /* Undo Swap two characters over three. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000787 STATE_UNROT3L, /* Undo rotate three characters left */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000788 STATE_UNROT3R, /* Undo rotate three characters right */
Bram Moolenaarea424162005-06-16 21:51:00 +0000789 STATE_REP_INI, /* Prepare for using REP items. */
790 STATE_REP, /* Use matching REP items from the .aff file. */
791 STATE_REP_UNDO, /* Undo a REP item replacement. */
792 STATE_FINAL /* End of this node. */
793} state_T;
794
795/*
Bram Moolenaar0c405862005-06-22 22:26:26 +0000796 * Struct to keep the state at each level in suggest_try_change().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000797 */
798typedef struct trystate_S
799{
Bram Moolenaarea424162005-06-16 21:51:00 +0000800 state_T ts_state; /* state at this level, STATE_ */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000801 int ts_score; /* score */
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000802 idx_T ts_arridx; /* index in tree array, start of node */
Bram Moolenaarea424162005-06-16 21:51:00 +0000803 short ts_curi; /* index in list of child nodes */
804 char_u ts_fidx; /* index in fword[], case-folded bad word */
805 char_u ts_fidxtry; /* ts_fidx at which bytes may be changed */
806 char_u ts_twordlen; /* valid length of tword[] */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000807 char_u ts_prefixdepth; /* stack depth for end of prefix or
Bram Moolenaard12a1322005-08-21 22:08:24 +0000808 * PFD_PREFIXTREE or PFD_NOPREFIX */
809 char_u ts_flags; /* TSF_ flags */
Bram Moolenaarea424162005-06-16 21:51:00 +0000810#ifdef FEAT_MBYTE
811 char_u ts_tcharlen; /* number of bytes in tword character */
812 char_u ts_tcharidx; /* current byte index in tword character */
813 char_u ts_isdiff; /* DIFF_ values */
814 char_u ts_fcharstart; /* index in fword where badword char started */
815#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +0000816 char_u ts_prewordlen; /* length of word in "preword[]" */
817 char_u ts_splitoff; /* index in "tword" after last split */
Bram Moolenaar78622822005-08-23 21:00:13 +0000818 char_u ts_splitfidx; /* "ts_fidx" at word split */
Bram Moolenaar5195e452005-08-19 20:32:47 +0000819 char_u ts_complen; /* nr of compound words used */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000820 char_u ts_compsplit; /* index for "compflags" where word was spit */
Bram Moolenaar0c405862005-06-22 22:26:26 +0000821 char_u ts_save_badflags; /* su_badflags saved here */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000822 char_u ts_delidx; /* index in fword for char that was deleted,
823 valid when "ts_flags" has TSF_DIDDEL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000824} trystate_T;
825
Bram Moolenaarea424162005-06-16 21:51:00 +0000826/* values for ts_isdiff */
827#define DIFF_NONE 0 /* no different byte (yet) */
828#define DIFF_YES 1 /* different byte found */
829#define DIFF_INSERT 2 /* inserting character */
830
Bram Moolenaard12a1322005-08-21 22:08:24 +0000831/* values for ts_flags */
832#define TSF_PREFIXOK 1 /* already checked that prefix is OK */
833#define TSF_DIDSPLIT 2 /* tried split at this point */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000834#define TSF_DIDDEL 4 /* did a delete, "ts_delidx" has index */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000835
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000836/* special values ts_prefixdepth */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000837#define PFD_NOPREFIX 0xff /* not using prefixes */
Bram Moolenaard12a1322005-08-21 22:08:24 +0000838#define PFD_PREFIXTREE 0xfe /* walking through the prefix tree */
Bram Moolenaar4770d092006-01-12 23:22:24 +0000839#define PFD_NOTSPECIAL 0xfd /* highest value that's not special */
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000840
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000841/* mode values for find_word */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000842#define FIND_FOLDWORD 0 /* find word case-folded */
843#define FIND_KEEPWORD 1 /* find keep-case word */
844#define FIND_PREFIX 2 /* find word after prefix */
845#define FIND_COMPOUND 3 /* find case-folded compound word */
846#define FIND_KEEPCOMPOUND 4 /* find keep-case compound word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000847
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000848static slang_T *slang_alloc __ARGS((char_u *lang));
849static void slang_free __ARGS((slang_T *lp));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000850static void slang_clear __ARGS((slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000851static void slang_clear_sug __ARGS((slang_T *lp));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000852static void find_word __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000853static int match_checkcompoundpattern __ARGS((char_u *ptr, int wlen, garray_T *gap));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000854static int can_compound __ARGS((slang_T *slang, char_u *word, char_u *flags));
Bram Moolenaar9f94b052008-11-30 20:12:46 +0000855static int can_be_compound __ARGS((trystate_T *sp, slang_T *slang, char_u *compflags, int flag));
856static int match_compoundrule __ARGS((slang_T *slang, char_u *compflags));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000857static 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 +0000858static void find_prefix __ARGS((matchinf_T *mip, int mode));
Bram Moolenaar1d73c882005-06-19 22:48:47 +0000859static int fold_more __ARGS((matchinf_T *mip));
Bram Moolenaar0dc065e2005-07-04 22:49:24 +0000860static int spell_valid_case __ARGS((int wordflags, int treeflags));
Bram Moolenaar95529562005-08-25 21:21:38 +0000861static int no_spell_checking __ARGS((win_T *wp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000862static void spell_load_lang __ARGS((char_u *lang));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000863static char_u *spell_enc __ARGS((void));
Bram Moolenaarf9184a12005-07-02 23:10:47 +0000864static void int_wordlist_spl __ARGS((char_u *fname));
Bram Moolenaarb765d632005-06-07 21:00:02 +0000865static void spell_load_cb __ARGS((char_u *fname, void *cookie));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000866static 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 +0000867static char_u *read_cnt_string __ARGS((FILE *fd, int cnt_bytes, int *lenp));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000868static int read_region_section __ARGS((FILE *fd, slang_T *slang, int len));
869static int read_charflags_section __ARGS((FILE *fd));
870static int read_prefcond_section __ARGS((FILE *fd, slang_T *lp));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000871static int read_rep_section __ARGS((FILE *fd, garray_T *gap, short *first));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000872static int read_sal_section __ARGS((FILE *fd, slang_T *slang));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000873static int read_words_section __ARGS((FILE *fd, slang_T *lp, int len));
874static void count_common_word __ARGS((slang_T *lp, char_u *word, int len, int count));
875static int score_wordcount_adj __ARGS((slang_T *slang, int score, char_u *word, int split));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000876static int read_sofo_section __ARGS((FILE *fd, slang_T *slang));
877static int read_compound __ARGS((FILE *fd, slang_T *slang, int len));
Bram Moolenaar6de68532005-08-24 22:08:48 +0000878static int byte_in_str __ARGS((char_u *str, int byte));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000879static int init_syl_tab __ARGS((slang_T *slang));
880static int count_syllables __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar7887d882005-07-01 22:33:52 +0000881static int set_sofo __ARGS((slang_T *lp, char_u *from, char_u *to));
882static void set_sal_first __ARGS((slang_T *lp));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000883#ifdef FEAT_MBYTE
884static int *mb_str2wide __ARGS((char_u *s));
885#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +0000886static int spell_read_tree __ARGS((FILE *fd, char_u **bytsp, idx_T **idxsp, int prefixtree, int prefixcnt));
Bram Moolenaar860cae12010-06-05 23:22:07 +0200887static idx_T read_tree_node __ARGS((FILE *fd, char_u *byts, idx_T *idxs, int maxidx, idx_T startidx, int prefixtree, int maxprefcondnr));
888static void clear_midword __ARGS((win_T *buf));
889static void use_midword __ARGS((slang_T *lp, win_T *buf));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000890static int find_region __ARGS((char_u *rp, char_u *region));
891static int captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000892static int badword_captype __ARGS((char_u *word, char_u *end));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000893static void spell_reload_one __ARGS((char_u *fname, int added_word));
Bram Moolenaar5195e452005-08-19 20:32:47 +0000894static void set_spell_charflags __ARGS((char_u *flags, int cnt, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000895static int set_spell_chartab __ARGS((char_u *fol, char_u *low, char_u *upp));
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000896static int spell_casefold __ARGS((char_u *p, int len, char_u *buf, int buflen));
Bram Moolenaar8b59de92005-08-11 19:59:29 +0000897static int check_need_cap __ARGS((linenr_T lnum, colnr_T col));
Bram Moolenaar66fa2712006-01-22 23:22:22 +0000898static 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 +0000899#ifdef FEAT_EVAL
900static void spell_suggest_expr __ARGS((suginfo_T *su, char_u *expr));
901#endif
902static void spell_suggest_file __ARGS((suginfo_T *su, char_u *fname));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000903static void spell_suggest_intern __ARGS((suginfo_T *su, int interactive));
904static void suggest_load_files __ARGS((void));
905static void tree_count_words __ARGS((char_u *byts, idx_T *idxs));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000906static void spell_find_cleanup __ARGS((suginfo_T *su));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000907static void onecap_copy __ARGS((char_u *word, char_u *wcopy, int upper));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000908static void allcap_copy __ARGS((char_u *word, char_u *wcopy));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000909static void suggest_try_special __ARGS((suginfo_T *su));
910static void suggest_try_change __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000911static void suggest_trie_walk __ARGS((suginfo_T *su, langp_T *lp, char_u *fword, int soundfold));
912static void go_deeper __ARGS((trystate_T *stack, int depth, int score_add));
Bram Moolenaar53805d12005-08-01 07:08:33 +0000913#ifdef FEAT_MBYTE
914static int nofold_len __ARGS((char_u *fword, int flen, char_u *word));
915#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000916static void find_keepcap_word __ARGS((slang_T *slang, char_u *fword, char_u *kword));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000917static void score_comp_sal __ARGS((suginfo_T *su));
918static void score_combine __ARGS((suginfo_T *su));
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000919static int stp_sal_score __ARGS((suggest_T *stp, suginfo_T *su, slang_T *slang, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000920static void suggest_try_soundalike_prep __ARGS((void));
Bram Moolenaar0c405862005-06-22 22:26:26 +0000921static void suggest_try_soundalike __ARGS((suginfo_T *su));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000922static void suggest_try_soundalike_finish __ARGS((void));
923static void add_sound_suggest __ARGS((suginfo_T *su, char_u *goodword, int score, langp_T *lp));
924static int soundfold_find __ARGS((slang_T *slang, char_u *word));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000925static void make_case_word __ARGS((char_u *fword, char_u *cword, int flags));
Bram Moolenaarea424162005-06-16 21:51:00 +0000926static void set_map_str __ARGS((slang_T *lp, char_u *map));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000927static int similar_chars __ARGS((slang_T *slang, int c1, int c2));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000928static 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));
929static void check_suggestions __ARGS((suginfo_T *su, garray_T *gap));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000930static void add_banned __ARGS((suginfo_T *su, char_u *word));
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000931static void rescore_suggestions __ARGS((suginfo_T *su));
Bram Moolenaar482aaeb2005-09-29 18:26:07 +0000932static void rescore_one __ARGS((suginfo_T *su, suggest_T *stp));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000933static int cleanup_suggestions __ARGS((garray_T *gap, int maxscore, int keep));
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000934static void spell_soundfold __ARGS((slang_T *slang, char_u *inword, int folded, char_u *res));
935static void spell_soundfold_sofo __ARGS((slang_T *slang, char_u *inword, char_u *res));
936static void spell_soundfold_sal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000937#ifdef FEAT_MBYTE
Bram Moolenaar42eeac32005-06-29 22:40:58 +0000938static void spell_soundfold_wsal __ARGS((slang_T *slang, char_u *inword, char_u *res));
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000939#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000940static int soundalike_score __ARGS((char_u *goodsound, char_u *badsound));
Bram Moolenaar4770d092006-01-12 23:22:24 +0000941static int spell_edit_score __ARGS((slang_T *slang, char_u *badword, char_u *goodword));
942static int spell_edit_score_limit __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
943#ifdef FEAT_MBYTE
944static int spell_edit_score_limit_w __ARGS((slang_T *slang, char_u *badword, char_u *goodword, int limit));
945#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +0000946static void dump_word __ARGS((slang_T *slang, char_u *word, char_u *pat, int *dir, int round, int flags, linenr_T lnum));
947static 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 +0000948static buf_T *open_spellbuf __ARGS((void));
949static void close_spellbuf __ARGS((buf_T *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000950
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000951/*
952 * Use our own character-case definitions, because the current locale may
953 * differ from what the .spl file uses.
954 * These must not be called with negative number!
955 */
956#ifndef FEAT_MBYTE
957/* Non-multi-byte implementation. */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000958# define SPELL_TOFOLD(c) ((c) < 256 ? (int)spelltab.st_fold[c] : (c))
959# define SPELL_TOUPPER(c) ((c) < 256 ? (int)spelltab.st_upper[c] : (c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000960# define SPELL_ISUPPER(c) ((c) < 256 ? spelltab.st_isu[c] : FALSE)
961#else
Bram Moolenaarcfc7d632005-07-28 22:28:16 +0000962# if defined(HAVE_WCHAR_H)
963# include <wchar.h> /* for towupper() and towlower() */
964# endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000965/* Multi-byte implementation. For Unicode we can call utf_*(), but don't do
966 * that for ASCII, because we don't want to use 'casemap' here. Otherwise use
967 * the "w" library function for characters above 255 if available. */
968# ifdef HAVE_TOWLOWER
969# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000970 : (c) < 256 ? (int)spelltab.st_fold[c] : (int)towlower(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000971# else
972# define SPELL_TOFOLD(c) (enc_utf8 && (c) >= 128 ? utf_fold(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000973 : (c) < 256 ? (int)spelltab.st_fold[c] : (c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000974# endif
975
976# ifdef HAVE_TOWUPPER
977# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000978 : (c) < 256 ? (int)spelltab.st_upper[c] : (int)towupper(c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000979# else
980# define SPELL_TOUPPER(c) (enc_utf8 && (c) >= 128 ? utf_toupper(c) \
Bram Moolenaar2c4278f2009-05-17 11:33:22 +0000981 : (c) < 256 ? (int)spelltab.st_upper[c] : (c))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000982# endif
983
984# ifdef HAVE_ISWUPPER
985# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
986 : (c) < 256 ? spelltab.st_isu[c] : iswupper(c))
987# else
988# define SPELL_ISUPPER(c) (enc_utf8 && (c) >= 128 ? utf_isupper(c) \
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000989 : (c) < 256 ? spelltab.st_isu[c] : (FALSE))
Bram Moolenaar9f30f502005-06-14 22:01:04 +0000990# endif
991#endif
992
Bram Moolenaarcfc6c432005-06-06 21:50:35 +0000993
994static char *e_format = N_("E759: Format error in spell file");
Bram Moolenaar7887d882005-07-01 22:33:52 +0000995static char *e_spell_trunc = N_("E758: Truncated spell file");
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +0000996static char *e_afftrailing = N_("Trailing text in %s line %d: %s");
Bram Moolenaar6de68532005-08-24 22:08:48 +0000997static char *e_affname = N_("Affix name too long in %s line %d: %s");
998static char *e_affform = N_("E761: Format error in affix file FOL, LOW or UPP");
999static char *e_affrange = N_("E762: Character in FOL, LOW or UPP is out of range");
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00001000static char *msg_compressing = N_("Compressing word tree...");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001001
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001002/* Remember what "z?" replaced. */
1003static char_u *repl_from = NULL;
1004static char_u *repl_to = NULL;
1005
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001006/*
1007 * Main spell-checking function.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001008 * "ptr" points to a character that could be the start of a word.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001009 * "*attrp" is set to the highlight index for a badly spelled word. For a
1010 * non-word or when it's OK it remains unchanged.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001011 * This must only be called when 'spelllang' is not empty.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001012 *
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001013 * "capcol" is used to check for a Capitalised word after the end of a
1014 * sentence. If it's zero then perform the check. Return the column where to
1015 * check next, or -1 when no sentence end was found. If it's NULL then don't
1016 * worry.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001017 *
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001018 * Returns the length of the word in bytes, also when it's OK, so that the
1019 * caller can skip over the word.
1020 */
1021 int
Bram Moolenaar4770d092006-01-12 23:22:24 +00001022spell_check(wp, ptr, attrp, capcol, docount)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001023 win_T *wp; /* current window */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001024 char_u *ptr;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001025 hlf_T *attrp;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001026 int *capcol; /* column to check for Capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001027 int docount; /* count good words */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001028{
1029 matchinf_T mi; /* Most things are put in "mi" so that it can
1030 be passed to functions quickly. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001031 int nrlen = 0; /* found a number first */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001032 int c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001033 int wrongcaplen = 0;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001034 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +00001035 int count_word = docount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001036
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001037 /* A word never starts at a space or a control character. Return quickly
1038 * then, skipping over the character. */
1039 if (*ptr <= ' ')
1040 return 1;
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001041
1042 /* Return here when loading language files failed. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001043 if (wp->w_s->b_langp.ga_len == 0)
Bram Moolenaara226a6d2006-02-26 23:59:20 +00001044 return 1;
1045
Bram Moolenaar5195e452005-08-19 20:32:47 +00001046 vim_memset(&mi, 0, sizeof(matchinf_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001047
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001048 /* A number is always OK. Also skip hexadecimal numbers 0xFF99 and
Bram Moolenaar43abc522005-12-10 20:15:02 +00001049 * 0X99FF. But always do check spelling to find "3GPP" and "11
1050 * julifeest". */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001051 if (*ptr >= '0' && *ptr <= '9')
Bram Moolenaar51485f02005-06-04 21:55:20 +00001052 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00001053 if (*ptr == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
1054 mi.mi_end = skiphex(ptr + 2);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001055 else
1056 mi.mi_end = skipdigits(ptr);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001057 nrlen = (int)(mi.mi_end - ptr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001058 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001059
Bram Moolenaar0c405862005-06-22 22:26:26 +00001060 /* Find the normal end of the word (until the next non-word character). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001061 mi.mi_word = ptr;
Bram Moolenaar43abc522005-12-10 20:15:02 +00001062 mi.mi_fend = ptr;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001063 if (spell_iswordp(mi.mi_fend, wp))
Bram Moolenaar51485f02005-06-04 21:55:20 +00001064 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001065 do
Bram Moolenaar51485f02005-06-04 21:55:20 +00001066 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001067 mb_ptr_adv(mi.mi_fend);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001068 } while (*mi.mi_fend != NUL && spell_iswordp(mi.mi_fend, wp));
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001069
Bram Moolenaar860cae12010-06-05 23:22:07 +02001070 if (capcol != NULL && *capcol == 0 && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001071 {
1072 /* Check word starting with capital letter. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00001073 c = PTR2CHAR(ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001074 if (!SPELL_ISUPPER(c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001075 wrongcaplen = (int)(mi.mi_fend - ptr);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001076 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001077 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001078 if (capcol != NULL)
1079 *capcol = -1;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001080
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001081 /* We always use the characters up to the next non-word character,
1082 * also for bad words. */
1083 mi.mi_end = mi.mi_fend;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001084
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001085 /* Check caps type later. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001086 mi.mi_capflags = 0;
1087 mi.mi_cend = NULL;
1088 mi.mi_win = wp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001089
Bram Moolenaar5195e452005-08-19 20:32:47 +00001090 /* case-fold the word with one non-word character, so that we can check
1091 * for the word end. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001092 if (*mi.mi_fend != NUL)
1093 mb_ptr_adv(mi.mi_fend);
1094
1095 (void)spell_casefold(ptr, (int)(mi.mi_fend - ptr), mi.mi_fword,
1096 MAXWLEN + 1);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001097 mi.mi_fwordlen = (int)STRLEN(mi.mi_fword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001098
1099 /* The word is bad unless we recognize it. */
1100 mi.mi_result = SP_BAD;
Bram Moolenaar78622822005-08-23 21:00:13 +00001101 mi.mi_result2 = SP_BAD;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001102
1103 /*
1104 * Loop over the languages specified in 'spelllang'.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001105 * We check them all, because a word may be matched longer in another
1106 * language.
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001107 */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001108 for (lpi = 0; lpi < wp->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001109 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001110 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, lpi);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001111
1112 /* If reloading fails the language is still in the list but everything
1113 * has been cleared. */
1114 if (mi.mi_lp->lp_slang->sl_fidxs == NULL)
1115 continue;
1116
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001117 /* Check for a matching word in case-folded words. */
1118 find_word(&mi, FIND_FOLDWORD);
1119
1120 /* Check for a matching word in keep-case words. */
1121 find_word(&mi, FIND_KEEPWORD);
1122
1123 /* Check for matching prefixes. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001124 find_prefix(&mi, FIND_FOLDWORD);
Bram Moolenaar78622822005-08-23 21:00:13 +00001125
1126 /* For a NOBREAK language, may want to use a word without a following
1127 * word as a backup. */
1128 if (mi.mi_lp->lp_slang->sl_nobreak && mi.mi_result == SP_BAD
1129 && mi.mi_result2 != SP_BAD)
1130 {
1131 mi.mi_result = mi.mi_result2;
1132 mi.mi_end = mi.mi_end2;
1133 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00001134
1135 /* Count the word in the first language where it's found to be OK. */
1136 if (count_word && mi.mi_result == SP_OK)
1137 {
1138 count_common_word(mi.mi_lp->lp_slang, ptr,
1139 (int)(mi.mi_end - ptr), 1);
1140 count_word = FALSE;
1141 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001142 }
1143
1144 if (mi.mi_result != SP_OK)
1145 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001146 /* If we found a number skip over it. Allows for "42nd". Do flag
1147 * rare and local words, e.g., "3GPP". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001148 if (nrlen > 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00001149 {
1150 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
1151 return nrlen;
1152 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001153
1154 /* When we are at a non-word character there is no error, just
1155 * skip over the character (try looking for a word after it). */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001156 else if (!spell_iswordp_nmw(ptr))
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00001157 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001158 if (capcol != NULL && wp->w_s->b_cap_prog != NULL)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001159 {
1160 regmatch_T regmatch;
1161
1162 /* Check for end of sentence. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001163 regmatch.regprog = wp->w_s->b_cap_prog;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00001164 regmatch.rm_ic = FALSE;
1165 if (vim_regexec(&regmatch, ptr, 0))
1166 *capcol = (int)(regmatch.endp[0] - ptr);
1167 }
1168
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001169#ifdef FEAT_MBYTE
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001170 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001171 return (*mb_ptr2len)(ptr);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001172#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001173 return 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001174 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001175 else if (mi.mi_end == ptr)
1176 /* Always include at least one character. Required for when there
1177 * is a mixup in "midword". */
1178 mb_ptr_adv(mi.mi_end);
Bram Moolenaar78622822005-08-23 21:00:13 +00001179 else if (mi.mi_result == SP_BAD
Bram Moolenaar860cae12010-06-05 23:22:07 +02001180 && LANGP_ENTRY(wp->w_s->b_langp, 0)->lp_slang->sl_nobreak)
Bram Moolenaar78622822005-08-23 21:00:13 +00001181 {
1182 char_u *p, *fp;
1183 int save_result = mi.mi_result;
1184
1185 /* First language in 'spelllang' is NOBREAK. Find first position
1186 * at which any word would be valid. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001187 mi.mi_lp = LANGP_ENTRY(wp->w_s->b_langp, 0);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001188 if (mi.mi_lp->lp_slang->sl_fidxs != NULL)
Bram Moolenaar78622822005-08-23 21:00:13 +00001189 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001190 p = mi.mi_word;
1191 fp = mi.mi_fword;
1192 for (;;)
Bram Moolenaar78622822005-08-23 21:00:13 +00001193 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001194 mb_ptr_adv(p);
1195 mb_ptr_adv(fp);
1196 if (p >= mi.mi_end)
1197 break;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001198 mi.mi_compoff = (int)(fp - mi.mi_fword);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001199 find_word(&mi, FIND_COMPOUND);
1200 if (mi.mi_result != SP_BAD)
1201 {
1202 mi.mi_end = p;
1203 break;
1204 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001205 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001206 mi.mi_result = save_result;
Bram Moolenaar78622822005-08-23 21:00:13 +00001207 }
Bram Moolenaar78622822005-08-23 21:00:13 +00001208 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001209
1210 if (mi.mi_result == SP_BAD || mi.mi_result == SP_BANNED)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001211 *attrp = HLF_SPB;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001212 else if (mi.mi_result == SP_RARE)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001213 *attrp = HLF_SPR;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00001214 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001215 *attrp = HLF_SPL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001216 }
1217
Bram Moolenaar5195e452005-08-19 20:32:47 +00001218 if (wrongcaplen > 0 && (mi.mi_result == SP_OK || mi.mi_result == SP_RARE))
1219 {
1220 /* Report SpellCap only when the word isn't badly spelled. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001221 *attrp = HLF_SPC;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001222 return wrongcaplen;
1223 }
1224
Bram Moolenaar51485f02005-06-04 21:55:20 +00001225 return (int)(mi.mi_end - ptr);
1226}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001227
Bram Moolenaar51485f02005-06-04 21:55:20 +00001228/*
1229 * Check if the word at "mip->mi_word" is in the tree.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001230 * When "mode" is FIND_FOLDWORD check in fold-case word tree.
1231 * When "mode" is FIND_KEEPWORD check in keep-case word tree.
1232 * When "mode" is FIND_PREFIX check for word after prefix in fold-case word
1233 * tree.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001234 *
1235 * For a match mip->mi_result is updated.
1236 */
1237 static void
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001238find_word(mip, mode)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001239 matchinf_T *mip;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001240 int mode;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001241{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001242 idx_T arridx = 0;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001243 int endlen[MAXWLEN]; /* length at possible word endings */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001244 idx_T endidx[MAXWLEN]; /* possible word endings */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001245 int endidxcnt = 0;
1246 int len;
1247 int wlen = 0;
1248 int flen;
1249 int c;
1250 char_u *ptr;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001251 idx_T lo, hi, m;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001252#ifdef FEAT_MBYTE
1253 char_u *s;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001254#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +00001255 char_u *p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001256 int res = SP_BAD;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001257 slang_T *slang = mip->mi_lp->lp_slang;
1258 unsigned flags;
1259 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001260 idx_T *idxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001261 int word_ends;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001262 int prefix_found;
Bram Moolenaar78622822005-08-23 21:00:13 +00001263 int nobreak_result;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001264
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001265 if (mode == FIND_KEEPWORD || mode == FIND_KEEPCOMPOUND)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001266 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001267 /* Check for word with matching case in keep-case tree. */
1268 ptr = mip->mi_word;
1269 flen = 9999; /* no case folding, always enough bytes */
1270 byts = slang->sl_kbyts;
1271 idxs = slang->sl_kidxs;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001272
1273 if (mode == FIND_KEEPCOMPOUND)
1274 /* Skip over the previously found word(s). */
1275 wlen += mip->mi_compoff;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001276 }
1277 else
1278 {
1279 /* Check for case-folded in case-folded tree. */
1280 ptr = mip->mi_fword;
1281 flen = mip->mi_fwordlen; /* available case-folded bytes */
1282 byts = slang->sl_fbyts;
1283 idxs = slang->sl_fidxs;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001284
1285 if (mode == FIND_PREFIX)
1286 {
1287 /* Skip over the prefix. */
1288 wlen = mip->mi_prefixlen;
1289 flen -= mip->mi_prefixlen;
1290 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001291 else if (mode == FIND_COMPOUND)
1292 {
1293 /* Skip over the previously found word(s). */
1294 wlen = mip->mi_compoff;
1295 flen -= mip->mi_compoff;
1296 }
1297
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001298 }
1299
Bram Moolenaar51485f02005-06-04 21:55:20 +00001300 if (byts == NULL)
1301 return; /* array is empty */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001302
Bram Moolenaar51485f02005-06-04 21:55:20 +00001303 /*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001304 * Repeat advancing in the tree until:
1305 * - there is a byte that doesn't match,
1306 * - we reach the end of the tree,
1307 * - or we reach the end of the line.
Bram Moolenaar51485f02005-06-04 21:55:20 +00001308 */
1309 for (;;)
1310 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00001311 if (flen <= 0 && *mip->mi_fend != NUL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001312 flen = fold_more(mip);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001313
1314 len = byts[arridx++];
1315
1316 /* If the first possible byte is a zero the word could end here.
1317 * Remember this index, we first check for the longest word. */
1318 if (byts[arridx] == 0)
1319 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00001320 if (endidxcnt == MAXWLEN)
1321 {
1322 /* Must be a corrupted spell file. */
1323 EMSG(_(e_format));
1324 return;
1325 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001326 endlen[endidxcnt] = wlen;
1327 endidx[endidxcnt++] = arridx++;
1328 --len;
1329
1330 /* Skip over the zeros, there can be several flag/region
1331 * combinations. */
1332 while (len > 0 && byts[arridx] == 0)
1333 {
1334 ++arridx;
1335 --len;
1336 }
1337 if (len == 0)
1338 break; /* no children, word must end here */
1339 }
1340
1341 /* Stop looking at end of the line. */
1342 if (ptr[wlen] == NUL)
1343 break;
1344
1345 /* Perform a binary search in the list of accepted bytes. */
1346 c = ptr[wlen];
Bram Moolenaar0c405862005-06-22 22:26:26 +00001347 if (c == TAB) /* <Tab> is handled like <Space> */
1348 c = ' ';
Bram Moolenaar51485f02005-06-04 21:55:20 +00001349 lo = arridx;
1350 hi = arridx + len - 1;
1351 while (lo < hi)
1352 {
1353 m = (lo + hi) / 2;
1354 if (byts[m] > c)
1355 hi = m - 1;
1356 else if (byts[m] < c)
1357 lo = m + 1;
1358 else
1359 {
1360 lo = hi = m;
1361 break;
1362 }
1363 }
1364
1365 /* Stop if there is no matching byte. */
1366 if (hi < lo || byts[lo] != c)
1367 break;
1368
1369 /* Continue at the child (if there is one). */
1370 arridx = idxs[lo];
1371 ++wlen;
1372 --flen;
Bram Moolenaar0c405862005-06-22 22:26:26 +00001373
1374 /* One space in the good word may stand for several spaces in the
1375 * checked word. */
1376 if (c == ' ')
1377 {
1378 for (;;)
1379 {
1380 if (flen <= 0 && *mip->mi_fend != NUL)
1381 flen = fold_more(mip);
1382 if (ptr[wlen] != ' ' && ptr[wlen] != TAB)
1383 break;
1384 ++wlen;
1385 --flen;
1386 }
1387 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001388 }
1389
1390 /*
1391 * Verify that one of the possible endings is valid. Try the longest
1392 * first.
1393 */
1394 while (endidxcnt > 0)
1395 {
1396 --endidxcnt;
1397 arridx = endidx[endidxcnt];
1398 wlen = endlen[endidxcnt];
1399
1400#ifdef FEAT_MBYTE
1401 if ((*mb_head_off)(ptr, ptr + wlen) > 0)
1402 continue; /* not at first byte of character */
1403#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02001404 if (spell_iswordp(ptr + wlen, mip->mi_win))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001405 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001406 if (slang->sl_compprog == NULL && !slang->sl_nobreak)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001407 continue; /* next char is a word character */
1408 word_ends = FALSE;
1409 }
1410 else
1411 word_ends = TRUE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001412 /* The prefix flag is before compound flags. Once a valid prefix flag
1413 * has been found we try compound flags. */
1414 prefix_found = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001415
1416#ifdef FEAT_MBYTE
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001417 if (mode != FIND_KEEPWORD && has_mbyte)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001418 {
1419 /* Compute byte length in original word, length may change
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001420 * when folding case. This can be slow, take a shortcut when the
1421 * case-folded word is equal to the keep-case word. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001422 p = mip->mi_word;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001423 if (STRNCMP(ptr, p, wlen) != 0)
1424 {
1425 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1426 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001427 wlen = (int)(p - mip->mi_word);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001428 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00001429 }
1430#endif
1431
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001432 /* Check flags and region. For FIND_PREFIX check the condition and
1433 * prefix ID.
1434 * Repeat this if there are more flags/region alternatives until there
1435 * is a match. */
1436 res = SP_BAD;
1437 for (len = byts[arridx - 1]; len > 0 && byts[arridx] == 0;
1438 --len, ++arridx)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001439 {
1440 flags = idxs[arridx];
Bram Moolenaar9f30f502005-06-14 22:01:04 +00001441
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001442 /* For the fold-case tree check that the case of the checked word
1443 * matches with what the word in the tree requires.
1444 * For keep-case tree the case is always right. For prefixes we
1445 * don't bother to check. */
1446 if (mode == FIND_FOLDWORD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001447 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00001448 if (mip->mi_cend != mip->mi_word + wlen)
1449 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001450 /* mi_capflags was set for a different word length, need
1451 * to do it again. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00001452 mip->mi_cend = mip->mi_word + wlen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001453 mip->mi_capflags = captype(mip->mi_word, mip->mi_cend);
Bram Moolenaar51485f02005-06-04 21:55:20 +00001454 }
1455
Bram Moolenaar0c405862005-06-22 22:26:26 +00001456 if (mip->mi_capflags == WF_KEEPCAP
1457 || !spell_valid_case(mip->mi_capflags, flags))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001458 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001459 }
1460
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001461 /* When mode is FIND_PREFIX the word must support the prefix:
1462 * check the prefix ID and the condition. Do that for the list at
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001463 * mip->mi_prefarridx that find_prefix() filled. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00001464 else if (mode == FIND_PREFIX && !prefix_found)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001465 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001466 c = valid_word_prefix(mip->mi_prefcnt, mip->mi_prefarridx,
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001467 flags,
Bram Moolenaar53805d12005-08-01 07:08:33 +00001468 mip->mi_word + mip->mi_cprefixlen, slang,
1469 FALSE);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001470 if (c == 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001471 continue;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001472
1473 /* Use the WF_RARE flag for a rare prefix. */
1474 if (c & WF_RAREPFX)
1475 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001476 prefix_found = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001477 }
1478
Bram Moolenaar78622822005-08-23 21:00:13 +00001479 if (slang->sl_nobreak)
1480 {
1481 if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND)
1482 && (flags & WF_BANNED) == 0)
1483 {
1484 /* NOBREAK: found a valid following word. That's all we
1485 * need to know, so return. */
1486 mip->mi_result = SP_OK;
1487 break;
1488 }
1489 }
1490
1491 else if ((mode == FIND_COMPOUND || mode == FIND_KEEPCOMPOUND
1492 || !word_ends))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001493 {
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00001494 /* If there is no compound flag or the word is shorter than
Bram Moolenaar5195e452005-08-19 20:32:47 +00001495 * COMPOUNDMIN reject it quickly.
1496 * Makes you wonder why someone puts a compound flag on a word
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001497 * that's too short... Myspell compatibility requires this
1498 * anyway. */
Bram Moolenaare52325c2005-08-22 22:54:29 +00001499 if (((unsigned)flags >> 24) == 0
1500 || wlen - mip->mi_compoff < slang->sl_compminlen)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001501 continue;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001502#ifdef FEAT_MBYTE
1503 /* For multi-byte chars check character length against
1504 * COMPOUNDMIN. */
1505 if (has_mbyte
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001506 && slang->sl_compminlen > 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001507 && mb_charlen_len(mip->mi_word + mip->mi_compoff,
1508 wlen - mip->mi_compoff) < slang->sl_compminlen)
1509 continue;
1510#endif
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001511
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001512 /* Limit the number of compound words to COMPOUNDWORDMAX if no
Bram Moolenaare52325c2005-08-22 22:54:29 +00001513 * maximum for syllables is specified. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001514 if (!word_ends && mip->mi_complen + mip->mi_compextra + 2
1515 > slang->sl_compmax
Bram Moolenaare52325c2005-08-22 22:54:29 +00001516 && slang->sl_compsylmax == MAXWLEN)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001517 continue;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001518
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001519 /* Don't allow compounding on a side where an affix was added,
1520 * unless COMPOUNDPERMITFLAG was used. */
1521 if (mip->mi_complen > 0 && (flags & WF_NOCOMPBEF))
1522 continue;
1523 if (!word_ends && (flags & WF_NOCOMPAFT))
1524 continue;
1525
Bram Moolenaard12a1322005-08-21 22:08:24 +00001526 /* Quickly check if compounding is possible with this flag. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00001527 if (!byte_in_str(mip->mi_complen == 0
Bram Moolenaard12a1322005-08-21 22:08:24 +00001528 ? slang->sl_compstartflags
1529 : slang->sl_compallflags,
Bram Moolenaar6de68532005-08-24 22:08:48 +00001530 ((unsigned)flags >> 24)))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001531 continue;
1532
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001533 /* If there is a match with a CHECKCOMPOUNDPATTERN rule
1534 * discard the compound word. */
1535 if (match_checkcompoundpattern(ptr, wlen, &slang->sl_comppat))
1536 continue;
1537
Bram Moolenaare52325c2005-08-22 22:54:29 +00001538 if (mode == FIND_COMPOUND)
1539 {
1540 int capflags;
1541
1542 /* Need to check the caps type of the appended compound
1543 * word. */
1544#ifdef FEAT_MBYTE
1545 if (has_mbyte && STRNCMP(ptr, mip->mi_word,
1546 mip->mi_compoff) != 0)
1547 {
1548 /* case folding may have changed the length */
1549 p = mip->mi_word;
1550 for (s = ptr; s < ptr + mip->mi_compoff; mb_ptr_adv(s))
1551 mb_ptr_adv(p);
1552 }
1553 else
1554#endif
1555 p = mip->mi_word + mip->mi_compoff;
1556 capflags = captype(p, mip->mi_word + wlen);
1557 if (capflags == WF_KEEPCAP || (capflags == WF_ALLCAP
1558 && (flags & WF_FIXCAP) != 0))
1559 continue;
1560
1561 if (capflags != WF_ALLCAP)
1562 {
1563 /* When the character before the word is a word
1564 * character we do not accept a Onecap word. We do
1565 * accept a no-caps word, even when the dictionary
1566 * word specifies ONECAP. */
1567 mb_ptr_back(mip->mi_word, p);
1568 if (spell_iswordp_nmw(p)
1569 ? capflags == WF_ONECAP
1570 : (flags & WF_ONECAP) != 0
1571 && capflags != WF_ONECAP)
1572 continue;
1573 }
1574 }
1575
Bram Moolenaar5195e452005-08-19 20:32:47 +00001576 /* If the word ends the sequence of compound flags of the
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001577 * words must match with one of the COMPOUNDRULE items and
Bram Moolenaar5195e452005-08-19 20:32:47 +00001578 * the number of syllables must not be too large. */
1579 mip->mi_compflags[mip->mi_complen] = ((unsigned)flags >> 24);
1580 mip->mi_compflags[mip->mi_complen + 1] = NUL;
1581 if (word_ends)
1582 {
1583 char_u fword[MAXWLEN];
1584
1585 if (slang->sl_compsylmax < MAXWLEN)
1586 {
1587 /* "fword" is only needed for checking syllables. */
1588 if (ptr == mip->mi_word)
1589 (void)spell_casefold(ptr, wlen, fword, MAXWLEN);
1590 else
1591 vim_strncpy(fword, ptr, endlen[endidxcnt]);
1592 }
1593 if (!can_compound(slang, fword, mip->mi_compflags))
1594 continue;
1595 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001596 else if (slang->sl_comprules != NULL
1597 && !match_compoundrule(slang, mip->mi_compflags))
1598 /* The compound flags collected so far do not match any
1599 * COMPOUNDRULE, discard the compounded word. */
1600 continue;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001601 }
1602
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00001603 /* Check NEEDCOMPOUND: can't use word without compounding. */
1604 else if (flags & WF_NEEDCOMP)
1605 continue;
1606
Bram Moolenaar78622822005-08-23 21:00:13 +00001607 nobreak_result = SP_OK;
1608
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001609 if (!word_ends)
1610 {
Bram Moolenaar78622822005-08-23 21:00:13 +00001611 int save_result = mip->mi_result;
1612 char_u *save_end = mip->mi_end;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001613 langp_T *save_lp = mip->mi_lp;
1614 int lpi;
Bram Moolenaar78622822005-08-23 21:00:13 +00001615
1616 /* Check that a valid word follows. If there is one and we
1617 * are compounding, it will set "mi_result", thus we are
1618 * always finished here. For NOBREAK we only check that a
1619 * valid word follows.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001620 * Recursive! */
Bram Moolenaar78622822005-08-23 21:00:13 +00001621 if (slang->sl_nobreak)
1622 mip->mi_result = SP_BAD;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001623
1624 /* Find following word in case-folded tree. */
1625 mip->mi_compoff = endlen[endidxcnt];
1626#ifdef FEAT_MBYTE
1627 if (has_mbyte && mode == FIND_KEEPWORD)
1628 {
1629 /* Compute byte length in case-folded word from "wlen":
1630 * byte length in keep-case word. Length may change when
1631 * folding case. This can be slow, take a shortcut when
1632 * the case-folded word is equal to the keep-case word. */
1633 p = mip->mi_fword;
1634 if (STRNCMP(ptr, p, wlen) != 0)
1635 {
1636 for (s = ptr; s < ptr + wlen; mb_ptr_adv(s))
1637 mb_ptr_adv(p);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001638 mip->mi_compoff = (int)(p - mip->mi_fword);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001639 }
1640 }
1641#endif
Bram Moolenaard12a1322005-08-21 22:08:24 +00001642 c = mip->mi_compoff;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001643 ++mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001644 if (flags & WF_COMPROOT)
1645 ++mip->mi_compextra;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001646
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001647 /* For NOBREAK we need to try all NOBREAK languages, at least
1648 * to find the ".add" file(s). */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001649 for (lpi = 0; lpi < mip->mi_win->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar78622822005-08-23 21:00:13 +00001650 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001651 if (slang->sl_nobreak)
1652 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02001653 mip->mi_lp = LANGP_ENTRY(mip->mi_win->w_s->b_langp, lpi);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001654 if (mip->mi_lp->lp_slang->sl_fidxs == NULL
1655 || !mip->mi_lp->lp_slang->sl_nobreak)
1656 continue;
1657 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00001658
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001659 find_word(mip, FIND_COMPOUND);
1660
1661 /* When NOBREAK any word that matches is OK. Otherwise we
1662 * need to find the longest match, thus try with keep-case
1663 * and prefix too. */
Bram Moolenaar78622822005-08-23 21:00:13 +00001664 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1665 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001666 /* Find following word in keep-case tree. */
1667 mip->mi_compoff = wlen;
1668 find_word(mip, FIND_KEEPCOMPOUND);
1669
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001670#if 0 /* Disabled, a prefix must not appear halfway a compound word,
1671 unless the COMPOUNDPERMITFLAG is used and then it can't be a
1672 postponed prefix. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001673 if (!slang->sl_nobreak || mip->mi_result == SP_BAD)
1674 {
1675 /* Check for following word with prefix. */
1676 mip->mi_compoff = c;
1677 find_prefix(mip, FIND_COMPOUND);
1678 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001679#endif
Bram Moolenaar78622822005-08-23 21:00:13 +00001680 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001681
1682 if (!slang->sl_nobreak)
1683 break;
Bram Moolenaar78622822005-08-23 21:00:13 +00001684 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00001685 --mip->mi_complen;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001686 if (flags & WF_COMPROOT)
1687 --mip->mi_compextra;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00001688 mip->mi_lp = save_lp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001689
Bram Moolenaar78622822005-08-23 21:00:13 +00001690 if (slang->sl_nobreak)
1691 {
1692 nobreak_result = mip->mi_result;
1693 mip->mi_result = save_result;
1694 mip->mi_end = save_end;
1695 }
1696 else
1697 {
1698 if (mip->mi_result == SP_OK)
1699 break;
1700 continue;
1701 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001702 }
1703
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001704 if (flags & WF_BANNED)
1705 res = SP_BANNED;
1706 else if (flags & WF_REGION)
1707 {
1708 /* Check region. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001709 if ((mip->mi_lp->lp_region & (flags >> 16)) != 0)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001710 res = SP_OK;
1711 else
1712 res = SP_LOCAL;
1713 }
1714 else if (flags & WF_RARE)
1715 res = SP_RARE;
1716 else
1717 res = SP_OK;
1718
Bram Moolenaar78622822005-08-23 21:00:13 +00001719 /* Always use the longest match and the best result. For NOBREAK
1720 * we separately keep the longest match without a following good
1721 * word as a fall-back. */
1722 if (nobreak_result == SP_BAD)
1723 {
1724 if (mip->mi_result2 > res)
1725 {
1726 mip->mi_result2 = res;
1727 mip->mi_end2 = mip->mi_word + wlen;
1728 }
1729 else if (mip->mi_result2 == res
1730 && mip->mi_end2 < mip->mi_word + wlen)
1731 mip->mi_end2 = mip->mi_word + wlen;
1732 }
1733 else if (mip->mi_result > res)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001734 {
1735 mip->mi_result = res;
1736 mip->mi_end = mip->mi_word + wlen;
1737 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001738 else if (mip->mi_result == res && mip->mi_end < mip->mi_word + wlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001739 mip->mi_end = mip->mi_word + wlen;
1740
Bram Moolenaar78622822005-08-23 21:00:13 +00001741 if (mip->mi_result == SP_OK)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001742 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001743 }
1744
Bram Moolenaar78622822005-08-23 21:00:13 +00001745 if (mip->mi_result == SP_OK)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001746 break;
Bram Moolenaar51485f02005-06-04 21:55:20 +00001747 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001748}
1749
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001750/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001751 * Return TRUE if there is a match between the word ptr[wlen] and
1752 * CHECKCOMPOUNDPATTERN rules, assuming that we will concatenate with another
1753 * word.
1754 * A match means that the first part of CHECKCOMPOUNDPATTERN matches at the
1755 * end of ptr[wlen] and the second part matches after it.
1756 */
1757 static int
1758match_checkcompoundpattern(ptr, wlen, gap)
1759 char_u *ptr;
1760 int wlen;
1761 garray_T *gap; /* &sl_comppat */
1762{
1763 int i;
1764 char_u *p;
1765 int len;
1766
1767 for (i = 0; i + 1 < gap->ga_len; i += 2)
1768 {
1769 p = ((char_u **)gap->ga_data)[i + 1];
1770 if (STRNCMP(ptr + wlen, p, STRLEN(p)) == 0)
1771 {
1772 /* Second part matches at start of following compound word, now
1773 * check if first part matches at end of previous word. */
1774 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar19c9c762008-12-09 21:34:39 +00001775 len = (int)STRLEN(p);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001776 if (len <= wlen && STRNCMP(ptr + wlen - len, p, len) == 0)
1777 return TRUE;
1778 }
1779 }
1780 return FALSE;
1781}
1782
1783/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00001784 * Return TRUE if "flags" is a valid sequence of compound flags and "word"
1785 * does not have too many syllables.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001786 */
1787 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00001788can_compound(slang, word, flags)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001789 slang_T *slang;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001790 char_u *word;
1791 char_u *flags;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001792{
Bram Moolenaar5195e452005-08-19 20:32:47 +00001793 regmatch_T regmatch;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001794#ifdef FEAT_MBYTE
1795 char_u uflags[MAXWLEN * 2];
1796 int i;
1797#endif
1798 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001799
1800 if (slang->sl_compprog == NULL)
1801 return FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001802#ifdef FEAT_MBYTE
1803 if (enc_utf8)
1804 {
1805 /* Need to convert the single byte flags to utf8 characters. */
1806 p = uflags;
1807 for (i = 0; flags[i] != NUL; ++i)
1808 p += mb_char2bytes(flags[i], p);
1809 *p = NUL;
1810 p = uflags;
1811 }
1812 else
1813#endif
1814 p = flags;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001815 regmatch.regprog = slang->sl_compprog;
1816 regmatch.rm_ic = FALSE;
Bram Moolenaar6de68532005-08-24 22:08:48 +00001817 if (!vim_regexec(&regmatch, p, 0))
Bram Moolenaar5195e452005-08-19 20:32:47 +00001818 return FALSE;
1819
Bram Moolenaare52325c2005-08-22 22:54:29 +00001820 /* Count the number of syllables. This may be slow, do it last. If there
1821 * are too many syllables AND the number of compound words is above
Bram Moolenaar899dddf2006-03-26 21:06:50 +00001822 * COMPOUNDWORDMAX then compounding is not allowed. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00001823 if (slang->sl_compsylmax < MAXWLEN
1824 && count_syllables(slang, word) > slang->sl_compsylmax)
Bram Moolenaar6de68532005-08-24 22:08:48 +00001825 return (int)STRLEN(flags) < slang->sl_compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00001826 return TRUE;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00001827}
1828
1829/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00001830 * Return TRUE when the sequence of flags in "compflags" plus "flag" can
1831 * possibly form a valid compounded word. This also checks the COMPOUNDRULE
1832 * lines if they don't contain wildcards.
1833 */
1834 static int
1835can_be_compound(sp, slang, compflags, flag)
1836 trystate_T *sp;
1837 slang_T *slang;
1838 char_u *compflags;
1839 int flag;
1840{
1841 /* If the flag doesn't appear in sl_compstartflags or sl_compallflags
1842 * then it can't possibly compound. */
1843 if (!byte_in_str(sp->ts_complen == sp->ts_compsplit
1844 ? slang->sl_compstartflags : slang->sl_compallflags, flag))
1845 return FALSE;
1846
1847 /* If there are no wildcards, we can check if the flags collected so far
1848 * possibly can form a match with COMPOUNDRULE patterns. This only
1849 * makes sense when we have two or more words. */
1850 if (slang->sl_comprules != NULL && sp->ts_complen > sp->ts_compsplit)
1851 {
1852 int v;
1853
1854 compflags[sp->ts_complen] = flag;
1855 compflags[sp->ts_complen + 1] = NUL;
1856 v = match_compoundrule(slang, compflags + sp->ts_compsplit);
1857 compflags[sp->ts_complen] = NUL;
1858 return v;
1859 }
1860
1861 return TRUE;
1862}
1863
1864
1865/*
1866 * Return TRUE if the compound flags in compflags[] match the start of any
1867 * compound rule. This is used to stop trying a compound if the flags
1868 * collected so far can't possibly match any compound rule.
1869 * Caller must check that slang->sl_comprules is not NULL.
1870 */
1871 static int
1872match_compoundrule(slang, compflags)
1873 slang_T *slang;
1874 char_u *compflags;
1875{
1876 char_u *p;
1877 int i;
1878 int c;
1879
1880 /* loop over all the COMPOUNDRULE entries */
1881 for (p = slang->sl_comprules; *p != NUL; ++p)
1882 {
1883 /* loop over the flags in the compound word we have made, match
1884 * them against the current rule entry */
1885 for (i = 0; ; ++i)
1886 {
1887 c = compflags[i];
1888 if (c == NUL)
1889 /* found a rule that matches for the flags we have so far */
1890 return TRUE;
1891 if (*p == '/' || *p == NUL)
1892 break; /* end of rule, it's too short */
1893 if (*p == '[')
1894 {
1895 int match = FALSE;
1896
1897 /* compare against all the flags in [] */
1898 ++p;
1899 while (*p != ']' && *p != NUL)
1900 if (*p++ == c)
1901 match = TRUE;
1902 if (!match)
1903 break; /* none matches */
1904 }
1905 else if (*p != c)
1906 break; /* flag of word doesn't match flag in pattern */
1907 ++p;
1908 }
1909
1910 /* Skip to the next "/", where the next pattern starts. */
1911 p = vim_strchr(p, '/');
1912 if (p == NULL)
1913 break;
1914 }
1915
1916 /* Checked all the rules and none of them match the flags, so there
1917 * can't possibly be a compound starting with these flags. */
1918 return FALSE;
1919}
1920
1921/*
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001922 * Return non-zero if the prefix indicated by "arridx" matches with the prefix
1923 * ID in "flags" for the word "word".
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001924 * The WF_RAREPFX flag is included in the return value for a rare prefix.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001925 */
1926 static int
Bram Moolenaar53805d12005-08-01 07:08:33 +00001927valid_word_prefix(totprefcnt, arridx, flags, word, slang, cond_req)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001928 int totprefcnt; /* nr of prefix IDs */
1929 int arridx; /* idx in sl_pidxs[] */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001930 int flags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001931 char_u *word;
1932 slang_T *slang;
Bram Moolenaar53805d12005-08-01 07:08:33 +00001933 int cond_req; /* only use prefixes with a condition */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001934{
1935 int prefcnt;
1936 int pidx;
1937 regprog_T *rp;
1938 regmatch_T regmatch;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001939 int prefid;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001940
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001941 prefid = (unsigned)flags >> 24;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001942 for (prefcnt = totprefcnt - 1; prefcnt >= 0; --prefcnt)
1943 {
1944 pidx = slang->sl_pidxs[arridx + prefcnt];
1945
1946 /* Check the prefix ID. */
1947 if (prefid != (pidx & 0xff))
1948 continue;
1949
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00001950 /* Check if the prefix doesn't combine and the word already has a
1951 * suffix. */
1952 if ((flags & WF_HAS_AFF) && (pidx & WF_PFX_NC))
1953 continue;
1954
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001955 /* Check the condition, if there is one. The condition index is
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001956 * stored in the two bytes above the prefix ID byte. */
1957 rp = slang->sl_prefprog[((unsigned)pidx >> 8) & 0xffff];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001958 if (rp != NULL)
1959 {
1960 regmatch.regprog = rp;
1961 regmatch.rm_ic = FALSE;
1962 if (!vim_regexec(&regmatch, word, 0))
1963 continue;
1964 }
Bram Moolenaar53805d12005-08-01 07:08:33 +00001965 else if (cond_req)
1966 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001967
Bram Moolenaar53805d12005-08-01 07:08:33 +00001968 /* It's a match! Return the WF_ flags. */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001969 return pidx;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001970 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00001971 return 0;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00001972}
1973
1974/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001975 * Check if the word at "mip->mi_word" has a matching prefix.
1976 * If it does, then check the following word.
1977 *
Bram Moolenaard12a1322005-08-21 22:08:24 +00001978 * If "mode" is "FIND_COMPOUND" then do the same after another word, find a
1979 * prefix in a compound word.
1980 *
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001981 * For a match mip->mi_result is updated.
1982 */
1983 static void
Bram Moolenaard12a1322005-08-21 22:08:24 +00001984find_prefix(mip, mode)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001985 matchinf_T *mip;
Bram Moolenaard12a1322005-08-21 22:08:24 +00001986 int mode;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00001987{
1988 idx_T arridx = 0;
1989 int len;
1990 int wlen = 0;
1991 int flen;
1992 int c;
1993 char_u *ptr;
1994 idx_T lo, hi, m;
1995 slang_T *slang = mip->mi_lp->lp_slang;
1996 char_u *byts;
1997 idx_T *idxs;
1998
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001999 byts = slang->sl_pbyts;
2000 if (byts == NULL)
2001 return; /* array is empty */
2002
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002003 /* We use the case-folded word here, since prefixes are always
2004 * case-folded. */
2005 ptr = mip->mi_fword;
2006 flen = mip->mi_fwordlen; /* available case-folded bytes */
Bram Moolenaard12a1322005-08-21 22:08:24 +00002007 if (mode == FIND_COMPOUND)
2008 {
2009 /* Skip over the previously found word(s). */
2010 ptr += mip->mi_compoff;
2011 flen -= mip->mi_compoff;
2012 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002013 idxs = slang->sl_pidxs;
2014
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002015 /*
2016 * Repeat advancing in the tree until:
2017 * - there is a byte that doesn't match,
2018 * - we reach the end of the tree,
2019 * - or we reach the end of the line.
2020 */
2021 for (;;)
2022 {
2023 if (flen == 0 && *mip->mi_fend != NUL)
2024 flen = fold_more(mip);
2025
2026 len = byts[arridx++];
2027
2028 /* If the first possible byte is a zero the prefix could end here.
2029 * Check if the following word matches and supports the prefix. */
2030 if (byts[arridx] == 0)
2031 {
2032 /* There can be several prefixes with different conditions. We
2033 * try them all, since we don't know which one will give the
2034 * longest match. The word is the same each time, pass the list
2035 * of possible prefixes to find_word(). */
2036 mip->mi_prefarridx = arridx;
2037 mip->mi_prefcnt = len;
2038 while (len > 0 && byts[arridx] == 0)
2039 {
2040 ++arridx;
2041 --len;
2042 }
2043 mip->mi_prefcnt -= len;
2044
2045 /* Find the word that comes after the prefix. */
2046 mip->mi_prefixlen = wlen;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002047 if (mode == FIND_COMPOUND)
2048 /* Skip over the previously found word(s). */
2049 mip->mi_prefixlen += mip->mi_compoff;
2050
Bram Moolenaar53805d12005-08-01 07:08:33 +00002051#ifdef FEAT_MBYTE
2052 if (has_mbyte)
2053 {
2054 /* Case-folded length may differ from original length. */
Bram Moolenaard12a1322005-08-21 22:08:24 +00002055 mip->mi_cprefixlen = nofold_len(mip->mi_fword,
2056 mip->mi_prefixlen, mip->mi_word);
Bram Moolenaar53805d12005-08-01 07:08:33 +00002057 }
2058 else
Bram Moolenaard12a1322005-08-21 22:08:24 +00002059 mip->mi_cprefixlen = mip->mi_prefixlen;
Bram Moolenaar53805d12005-08-01 07:08:33 +00002060#endif
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002061 find_word(mip, FIND_PREFIX);
2062
2063
2064 if (len == 0)
2065 break; /* no children, word must end here */
2066 }
2067
2068 /* Stop looking at end of the line. */
2069 if (ptr[wlen] == NUL)
2070 break;
2071
2072 /* Perform a binary search in the list of accepted bytes. */
2073 c = ptr[wlen];
2074 lo = arridx;
2075 hi = arridx + len - 1;
2076 while (lo < hi)
2077 {
2078 m = (lo + hi) / 2;
2079 if (byts[m] > c)
2080 hi = m - 1;
2081 else if (byts[m] < c)
2082 lo = m + 1;
2083 else
2084 {
2085 lo = hi = m;
2086 break;
2087 }
2088 }
2089
2090 /* Stop if there is no matching byte. */
2091 if (hi < lo || byts[lo] != c)
2092 break;
2093
2094 /* Continue at the child (if there is one). */
2095 arridx = idxs[lo];
2096 ++wlen;
2097 --flen;
2098 }
2099}
2100
2101/*
2102 * Need to fold at least one more character. Do until next non-word character
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002103 * for efficiency. Include the non-word character too.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002104 * Return the length of the folded chars in bytes.
2105 */
2106 static int
2107fold_more(mip)
2108 matchinf_T *mip;
2109{
2110 int flen;
2111 char_u *p;
2112
2113 p = mip->mi_fend;
2114 do
2115 {
2116 mb_ptr_adv(mip->mi_fend);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002117 } while (*mip->mi_fend != NUL && spell_iswordp(mip->mi_fend, mip->mi_win));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002118
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002119 /* Include the non-word character so that we can check for the word end. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002120 if (*mip->mi_fend != NUL)
2121 mb_ptr_adv(mip->mi_fend);
2122
2123 (void)spell_casefold(p, (int)(mip->mi_fend - p),
2124 mip->mi_fword + mip->mi_fwordlen,
2125 MAXWLEN - mip->mi_fwordlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002126 flen = (int)STRLEN(mip->mi_fword + mip->mi_fwordlen);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002127 mip->mi_fwordlen += flen;
2128 return flen;
2129}
2130
2131/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002132 * Check case flags for a word. Return TRUE if the word has the requested
2133 * case.
2134 */
2135 static int
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002136spell_valid_case(wordflags, treeflags)
2137 int wordflags; /* flags for the checked word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002138 int treeflags; /* flags for the word in the spell tree */
2139{
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002140 return ((wordflags == WF_ALLCAP && (treeflags & WF_FIXCAP) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002141 || ((treeflags & (WF_ALLCAP | WF_KEEPCAP)) == 0
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002142 && ((treeflags & WF_ONECAP) == 0
2143 || (wordflags & WF_ONECAP) != 0)));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002144}
2145
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002146/*
2147 * Return TRUE if spell checking is not enabled.
2148 */
2149 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00002150no_spell_checking(wp)
2151 win_T *wp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002152{
Bram Moolenaar860cae12010-06-05 23:22:07 +02002153 if (!wp->w_p_spell || *wp->w_s->b_p_spl == NUL
2154 || wp->w_s->b_langp.ga_len == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00002155 {
2156 EMSG(_("E756: Spell checking is not enabled"));
2157 return TRUE;
2158 }
2159 return FALSE;
2160}
Bram Moolenaar51485f02005-06-04 21:55:20 +00002161
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002162/*
2163 * Move to next spell error.
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002164 * "curline" is FALSE for "[s", "]s", "[S" and "]S".
2165 * "curline" is TRUE to find word under/after cursor in the same line.
Bram Moolenaar5195e452005-08-19 20:32:47 +00002166 * For Insert mode completion "dir" is BACKWARD and "curline" is TRUE: move
2167 * to after badly spelled word before the cursor.
Bram Moolenaar6de68532005-08-24 22:08:48 +00002168 * Return 0 if not found, length of the badly spelled word otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002169 */
2170 int
Bram Moolenaar95529562005-08-25 21:21:38 +00002171spell_move_to(wp, dir, allwords, curline, attrp)
2172 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002173 int dir; /* FORWARD or BACKWARD */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002174 int allwords; /* TRUE for "[s"/"]s", FALSE for "[S"/"]S" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002175 int curline;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002176 hlf_T *attrp; /* return: attributes of bad word or NULL
2177 (only when "dir" is FORWARD) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002178{
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002179 linenr_T lnum;
2180 pos_T found_pos;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002181 int found_len = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002182 char_u *line;
2183 char_u *p;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002184 char_u *endp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002185 hlf_T attr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002186 int len;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002187# ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002188 int has_syntax = syntax_present(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002189# endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00002190 int col;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002191 int can_spell;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002192 char_u *buf = NULL;
2193 int buflen = 0;
2194 int skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002195 int capcol = -1;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002196 int found_one = FALSE;
2197 int wrapped = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002198
Bram Moolenaar95529562005-08-25 21:21:38 +00002199 if (no_spell_checking(wp))
Bram Moolenaar6de68532005-08-24 22:08:48 +00002200 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002201
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002202 /*
2203 * Start looking for bad word at the start of the line, because we can't
Bram Moolenaar86ca6e32006-03-29 21:06:37 +00002204 * start halfway a word, we don't know where it starts or ends.
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002205 *
2206 * When searching backwards, we continue in the line to find the last
2207 * bad word (in the cursor line: before the cursor).
Bram Moolenaar0c405862005-06-22 22:26:26 +00002208 *
2209 * We concatenate the start of the next line, so that wrapped words work
2210 * (e.g. "et<line-break>cetera"). Doesn't work when searching backwards
2211 * though...
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002212 */
Bram Moolenaar95529562005-08-25 21:21:38 +00002213 lnum = wp->w_cursor.lnum;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002214 clearpos(&found_pos);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002215
2216 while (!got_int)
2217 {
Bram Moolenaar95529562005-08-25 21:21:38 +00002218 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002219
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002220 len = (int)STRLEN(line);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002221 if (buflen < len + MAXWLEN + 2)
2222 {
2223 vim_free(buf);
2224 buflen = len + MAXWLEN + 2;
2225 buf = alloc(buflen);
2226 if (buf == NULL)
2227 break;
2228 }
2229
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002230 /* In first line check first word for Capital. */
2231 if (lnum == 1)
2232 capcol = 0;
2233
2234 /* For checking first word with a capital skip white space. */
2235 if (capcol == 0)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002236 capcol = (int)(skipwhite(line) - line);
2237 else if (curline && wp == curwin)
2238 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002239 /* For spellbadword(): check if first word needs a capital. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002240 col = (int)(skipwhite(line) - line);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002241 if (check_need_cap(lnum, col))
2242 capcol = col;
2243
2244 /* Need to get the line again, may have looked at the previous
2245 * one. */
2246 line = ml_get_buf(wp->w_buffer, lnum, FALSE);
2247 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002248
Bram Moolenaar0c405862005-06-22 22:26:26 +00002249 /* Copy the line into "buf" and append the start of the next line if
2250 * possible. */
2251 STRCPY(buf, line);
Bram Moolenaar95529562005-08-25 21:21:38 +00002252 if (lnum < wp->w_buffer->b_ml.ml_line_count)
Bram Moolenaar5dd95a12006-05-13 12:09:24 +00002253 spell_cat_line(buf + STRLEN(buf),
2254 ml_get_buf(wp->w_buffer, lnum + 1, FALSE), MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002255
2256 p = buf + skip;
2257 endp = buf + len;
2258 while (p < endp)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002259 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002260 /* When searching backward don't search after the cursor. Unless
2261 * we wrapped around the end of the buffer. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00002262 if (dir == BACKWARD
Bram Moolenaar95529562005-08-25 21:21:38 +00002263 && lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002264 && !wrapped
Bram Moolenaar95529562005-08-25 21:21:38 +00002265 && (colnr_T)(p - buf) >= wp->w_cursor.col)
Bram Moolenaar51485f02005-06-04 21:55:20 +00002266 break;
2267
2268 /* start of word */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002269 attr = HLF_COUNT;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002270 len = spell_check(wp, p, &attr, &capcol, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00002271
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002272 if (attr != HLF_COUNT)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002273 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002274 /* We found a bad word. Check the attribute. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002275 if (allwords || attr == HLF_SPB)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002276 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00002277 /* When searching forward only accept a bad word after
2278 * the cursor. */
2279 if (dir == BACKWARD
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002280 || lnum != wp->w_cursor.lnum
Bram Moolenaar95529562005-08-25 21:21:38 +00002281 || (lnum == wp->w_cursor.lnum
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002282 && (wrapped
2283 || (colnr_T)(curline ? p - buf + len
Bram Moolenaar0c405862005-06-22 22:26:26 +00002284 : p - buf)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002285 > wp->w_cursor.col)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002286 {
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002287# ifdef FEAT_SYN_HL
Bram Moolenaar51485f02005-06-04 21:55:20 +00002288 if (has_syntax)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002289 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002290 col = (int)(p - buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002291 (void)syn_get_id(wp, lnum, (colnr_T)col,
Bram Moolenaar56cefaf2008-01-12 15:47:10 +00002292 FALSE, &can_spell, FALSE);
Bram Moolenaard68071d2006-05-02 22:08:30 +00002293 if (!can_spell)
2294 attr = HLF_COUNT;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002295 }
2296 else
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002297#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002298 can_spell = TRUE;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002299
Bram Moolenaar51485f02005-06-04 21:55:20 +00002300 if (can_spell)
2301 {
Bram Moolenaard68071d2006-05-02 22:08:30 +00002302 found_one = TRUE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002303 found_pos.lnum = lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002304 found_pos.col = (int)(p - buf);
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002305#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar51485f02005-06-04 21:55:20 +00002306 found_pos.coladd = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002307#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00002308 if (dir == FORWARD)
2309 {
2310 /* No need to search further. */
Bram Moolenaar95529562005-08-25 21:21:38 +00002311 wp->w_cursor = found_pos;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002312 vim_free(buf);
Bram Moolenaar95529562005-08-25 21:21:38 +00002313 if (attrp != NULL)
2314 *attrp = attr;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002315 return len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002316 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002317 else if (curline)
2318 /* Insert mode completion: put cursor after
2319 * the bad word. */
2320 found_pos.col += len;
Bram Moolenaar6de68532005-08-24 22:08:48 +00002321 found_len = len;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002322 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002323 }
Bram Moolenaard68071d2006-05-02 22:08:30 +00002324 else
2325 found_one = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002326 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002327 }
2328
Bram Moolenaar51485f02005-06-04 21:55:20 +00002329 /* advance to character after the word */
2330 p += len;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002331 capcol -= len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002332 }
2333
Bram Moolenaar5195e452005-08-19 20:32:47 +00002334 if (dir == BACKWARD && found_pos.lnum != 0)
2335 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002336 /* Use the last match in the line (before the cursor). */
Bram Moolenaar95529562005-08-25 21:21:38 +00002337 wp->w_cursor = found_pos;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002338 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002339 return found_len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002340 }
2341
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002342 if (curline)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002343 break; /* only check cursor line */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002344
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002345 /* Advance to next line. */
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002346 if (dir == BACKWARD)
2347 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002348 /* If we are back at the starting line and searched it again there
2349 * is no match, give up. */
2350 if (lnum == wp->w_cursor.lnum && wrapped)
Bram Moolenaar0c405862005-06-22 22:26:26 +00002351 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002352
2353 if (lnum > 1)
2354 --lnum;
2355 else if (!p_ws)
2356 break; /* at first line and 'nowrapscan' */
2357 else
2358 {
2359 /* Wrap around to the end of the buffer. May search the
2360 * starting line again and accept the last match. */
2361 lnum = wp->w_buffer->b_ml.ml_line_count;
2362 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002363 if (!shortmess(SHM_SEARCH))
2364 give_warning((char_u *)_(top_bot_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002365 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002366 capcol = -1;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002367 }
2368 else
2369 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002370 if (lnum < wp->w_buffer->b_ml.ml_line_count)
2371 ++lnum;
2372 else if (!p_ws)
2373 break; /* at first line and 'nowrapscan' */
2374 else
2375 {
2376 /* Wrap around to the start of the buffer. May search the
2377 * starting line again and accept the first match. */
2378 lnum = 1;
2379 wrapped = TRUE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00002380 if (!shortmess(SHM_SEARCH))
2381 give_warning((char_u *)_(bot_top_msg), TRUE);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002382 }
2383
2384 /* If we are back at the starting line and there is no match then
2385 * give up. */
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00002386 if (lnum == wp->w_cursor.lnum && (!found_one || wrapped))
Bram Moolenaar0c405862005-06-22 22:26:26 +00002387 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002388
2389 /* Skip the characters at the start of the next line that were
2390 * included in a match crossing line boundaries. */
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002391 if (attr == HLF_COUNT)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002392 skip = (int)(p - endp);
Bram Moolenaar0c405862005-06-22 22:26:26 +00002393 else
2394 skip = 0;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002395
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002396 /* Capcol skips over the inserted space. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002397 --capcol;
2398
2399 /* But after empty line check first word in next line */
2400 if (*skipwhite(line) == NUL)
2401 capcol = 0;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00002402 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002403
2404 line_breakcheck();
2405 }
2406
Bram Moolenaar0c405862005-06-22 22:26:26 +00002407 vim_free(buf);
Bram Moolenaar6de68532005-08-24 22:08:48 +00002408 return 0;
Bram Moolenaar0c405862005-06-22 22:26:26 +00002409}
2410
2411/*
2412 * For spell checking: concatenate the start of the following line "line" into
2413 * "buf", blanking-out special characters. Copy less then "maxlen" bytes.
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00002414 * Keep the blanks at the start of the next line, this is used in win_line()
2415 * to skip those bytes if the word was OK.
Bram Moolenaar0c405862005-06-22 22:26:26 +00002416 */
2417 void
2418spell_cat_line(buf, line, maxlen)
2419 char_u *buf;
2420 char_u *line;
2421 int maxlen;
2422{
2423 char_u *p;
2424 int n;
2425
2426 p = skipwhite(line);
2427 while (vim_strchr((char_u *)"*#/\"\t", *p) != NULL)
2428 p = skipwhite(p + 1);
2429
2430 if (*p != NUL)
2431 {
Bram Moolenaar6a5d2ac2008-04-01 15:14:36 +00002432 /* Only worth concatenating if there is something else than spaces to
2433 * concatenate. */
2434 n = (int)(p - line) + 1;
2435 if (n < maxlen - 1)
2436 {
2437 vim_memset(buf, ' ', n);
2438 vim_strncpy(buf + n, p, maxlen - 1 - n);
2439 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00002440 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002441}
2442
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002443/*
2444 * Structure used for the cookie argument of do_in_runtimepath().
2445 */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002446typedef struct spelload_S
2447{
2448 char_u sl_lang[MAXWLEN + 1]; /* language name */
2449 slang_T *sl_slang; /* resulting slang_T struct */
2450 int sl_nobreak; /* NOBREAK language found */
2451} spelload_T;
2452
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002453/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002454 * Load word list(s) for "lang" from Vim spell file(s).
Bram Moolenaarb765d632005-06-07 21:00:02 +00002455 * "lang" must be the language without the region: e.g., "en".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002456 */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002457 static void
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002458spell_load_lang(lang)
2459 char_u *lang;
2460{
Bram Moolenaarb765d632005-06-07 21:00:02 +00002461 char_u fname_enc[85];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002462 int r;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002463 spelload_T sl;
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002464#ifdef FEAT_AUTOCMD
2465 int round;
2466#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002467
Bram Moolenaarb765d632005-06-07 21:00:02 +00002468 /* Copy the language name to pass it to spell_load_cb() as a cookie.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002469 * It's truncated when an error is detected. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002470 STRCPY(sl.sl_lang, lang);
2471 sl.sl_slang = NULL;
2472 sl.sl_nobreak = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002473
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002474#ifdef FEAT_AUTOCMD
2475 /* We may retry when no spell file is found for the language, an
2476 * autocommand may load it then. */
2477 for (round = 1; round <= 2; ++round)
2478#endif
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002479 {
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002480 /*
2481 * Find the first spell file for "lang" in 'runtimepath' and load it.
2482 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002483 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01002484#ifdef VMS
2485 "spell/%s_%s.spl",
2486#else
2487 "spell/%s.%s.spl",
2488#endif
2489 lang, spell_enc());
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002490 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002491
2492 if (r == FAIL && *sl.sl_lang != NUL)
2493 {
2494 /* Try loading the ASCII version. */
2495 vim_snprintf((char *)fname_enc, sizeof(fname_enc) - 5,
Bram Moolenaar56f78042010-12-08 17:09:32 +01002496#ifdef VMS
2497 "spell/%s_ascii.spl",
2498#else
2499 "spell/%s.ascii.spl",
2500#endif
2501 lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002502 r = do_in_runtimepath(fname_enc, FALSE, spell_load_cb, &sl);
2503
2504#ifdef FEAT_AUTOCMD
2505 if (r == FAIL && *sl.sl_lang != NUL && round == 1
2506 && apply_autocmds(EVENT_SPELLFILEMISSING, lang,
2507 curbuf->b_fname, FALSE, curbuf))
2508 continue;
2509 break;
2510#endif
2511 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002512#ifdef FEAT_AUTOCMD
2513 break;
2514#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002515 }
2516
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002517 if (r == FAIL)
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002518 {
Bram Moolenaar56f78042010-12-08 17:09:32 +01002519 smsg((char_u *)
2520#ifdef VMS
2521 _("Warning: Cannot find word list \"%s_%s.spl\" or \"%s_ascii.spl\""),
2522#else
2523 _("Warning: Cannot find word list \"%s.%s.spl\" or \"%s.ascii.spl\""),
2524#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002525 lang, spell_enc(), lang);
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00002526 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002527 else if (sl.sl_slang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002528 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002529 /* At least one file was loaded, now load ALL the additions. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002530 STRCPY(fname_enc + STRLEN(fname_enc) - 3, "add.spl");
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002531 do_in_runtimepath(fname_enc, TRUE, spell_load_cb, &sl);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002532 }
2533}
2534
2535/*
2536 * Return the encoding used for spell checking: Use 'encoding', except that we
2537 * use "latin1" for "latin9". And limit to 60 characters (just in case).
2538 */
2539 static char_u *
2540spell_enc()
2541{
2542
2543#ifdef FEAT_MBYTE
2544 if (STRLEN(p_enc) < 60 && STRCMP(p_enc, "iso-8859-15") != 0)
2545 return p_enc;
2546#endif
2547 return (char_u *)"latin1";
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002548}
2549
2550/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002551 * Get the name of the .spl file for the internal wordlist into
2552 * "fname[MAXPATHL]".
2553 */
2554 static void
2555int_wordlist_spl(fname)
2556 char_u *fname;
2557{
Bram Moolenaar56f78042010-12-08 17:09:32 +01002558 vim_snprintf((char *)fname, MAXPATHL, SPL_FNAME_TMPL,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00002559 int_wordlist, spell_enc());
2560}
2561
2562/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002563 * Allocate a new slang_T for language "lang". "lang" can be NULL.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002564 * Caller must fill "sl_next".
2565 */
2566 static slang_T *
2567slang_alloc(lang)
2568 char_u *lang;
2569{
2570 slang_T *lp;
2571
Bram Moolenaar51485f02005-06-04 21:55:20 +00002572 lp = (slang_T *)alloc_clear(sizeof(slang_T));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002573 if (lp != NULL)
2574 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002575 if (lang != NULL)
2576 lp->sl_name = vim_strsave(lang);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002577 ga_init2(&lp->sl_rep, sizeof(fromto_T), 10);
Bram Moolenaar4770d092006-01-12 23:22:24 +00002578 ga_init2(&lp->sl_repsal, sizeof(fromto_T), 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002579 lp->sl_compmax = MAXWLEN;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002580 lp->sl_compsylmax = MAXWLEN;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002581 hash_init(&lp->sl_wordcount);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002582 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002583
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002584 return lp;
2585}
2586
2587/*
2588 * Free the contents of an slang_T and the structure itself.
2589 */
2590 static void
2591slang_free(lp)
2592 slang_T *lp;
2593{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002594 vim_free(lp->sl_name);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002595 vim_free(lp->sl_fname);
2596 slang_clear(lp);
2597 vim_free(lp);
2598}
2599
2600/*
2601 * Clear an slang_T so that the file can be reloaded.
2602 */
2603 static void
2604slang_clear(lp)
2605 slang_T *lp;
2606{
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002607 garray_T *gap;
2608 fromto_T *ftp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002609 salitem_T *smp;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002610 int i;
Bram Moolenaar4770d092006-01-12 23:22:24 +00002611 int round;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002612
Bram Moolenaar51485f02005-06-04 21:55:20 +00002613 vim_free(lp->sl_fbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002614 lp->sl_fbyts = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002615 vim_free(lp->sl_kbyts);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002616 lp->sl_kbyts = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002617 vim_free(lp->sl_pbyts);
2618 lp->sl_pbyts = NULL;
2619
Bram Moolenaar51485f02005-06-04 21:55:20 +00002620 vim_free(lp->sl_fidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002621 lp->sl_fidxs = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002622 vim_free(lp->sl_kidxs);
Bram Moolenaarb765d632005-06-07 21:00:02 +00002623 lp->sl_kidxs = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002624 vim_free(lp->sl_pidxs);
2625 lp->sl_pidxs = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002626
Bram Moolenaar4770d092006-01-12 23:22:24 +00002627 for (round = 1; round <= 2; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002628 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00002629 gap = round == 1 ? &lp->sl_rep : &lp->sl_repsal;
2630 while (gap->ga_len > 0)
2631 {
2632 ftp = &((fromto_T *)gap->ga_data)[--gap->ga_len];
2633 vim_free(ftp->ft_from);
2634 vim_free(ftp->ft_to);
2635 }
2636 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002637 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002638
2639 gap = &lp->sl_sal;
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002640 if (lp->sl_sofo)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002641 {
2642 /* "ga_len" is set to 1 without adding an item for latin1 */
2643 if (gap->ga_data != NULL)
2644 /* SOFOFROM and SOFOTO items: free lists of wide characters. */
2645 for (i = 0; i < gap->ga_len; ++i)
2646 vim_free(((int **)gap->ga_data)[i]);
2647 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002648 else
2649 /* SAL items: free salitem_T items */
2650 while (gap->ga_len > 0)
2651 {
2652 smp = &((salitem_T *)gap->ga_data)[--gap->ga_len];
2653 vim_free(smp->sm_lead);
2654 /* Don't free sm_oneof and sm_rules, they point into sm_lead. */
2655 vim_free(smp->sm_to);
2656#ifdef FEAT_MBYTE
2657 vim_free(smp->sm_lead_w);
2658 vim_free(smp->sm_oneof_w);
2659 vim_free(smp->sm_to_w);
2660#endif
2661 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002662 ga_clear(gap);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002663
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002664 for (i = 0; i < lp->sl_prefixcnt; ++i)
2665 vim_free(lp->sl_prefprog[i]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002666 lp->sl_prefixcnt = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002667 vim_free(lp->sl_prefprog);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002668 lp->sl_prefprog = NULL;
2669
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002670 vim_free(lp->sl_info);
2671 lp->sl_info = NULL;
2672
Bram Moolenaar9c96f592005-06-30 21:52:39 +00002673 vim_free(lp->sl_midword);
2674 lp->sl_midword = NULL;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002675
Bram Moolenaar5195e452005-08-19 20:32:47 +00002676 vim_free(lp->sl_compprog);
Bram Moolenaar9f94b052008-11-30 20:12:46 +00002677 vim_free(lp->sl_comprules);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002678 vim_free(lp->sl_compstartflags);
Bram Moolenaard12a1322005-08-21 22:08:24 +00002679 vim_free(lp->sl_compallflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002680 lp->sl_compprog = NULL;
Bram Moolenaar9f94b052008-11-30 20:12:46 +00002681 lp->sl_comprules = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002682 lp->sl_compstartflags = NULL;
Bram Moolenaard12a1322005-08-21 22:08:24 +00002683 lp->sl_compallflags = NULL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002684
2685 vim_free(lp->sl_syllable);
2686 lp->sl_syllable = NULL;
2687 ga_clear(&lp->sl_syl_items);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002688
Bram Moolenaar899dddf2006-03-26 21:06:50 +00002689 ga_clear_strings(&lp->sl_comppat);
2690
Bram Moolenaar4770d092006-01-12 23:22:24 +00002691 hash_clear_all(&lp->sl_wordcount, WC_KEY_OFF);
2692 hash_init(&lp->sl_wordcount);
Bram Moolenaarea424162005-06-16 21:51:00 +00002693
Bram Moolenaar4770d092006-01-12 23:22:24 +00002694#ifdef FEAT_MBYTE
2695 hash_clear_all(&lp->sl_map_hash, 0);
Bram Moolenaarea424162005-06-16 21:51:00 +00002696#endif
Bram Moolenaar5195e452005-08-19 20:32:47 +00002697
Bram Moolenaar4770d092006-01-12 23:22:24 +00002698 /* Clear info from .sug file. */
2699 slang_clear_sug(lp);
2700
Bram Moolenaar5195e452005-08-19 20:32:47 +00002701 lp->sl_compmax = MAXWLEN;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002702 lp->sl_compminlen = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002703 lp->sl_compsylmax = MAXWLEN;
2704 lp->sl_regions[0] = NUL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002705}
2706
2707/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00002708 * Clear the info from the .sug file in "lp".
2709 */
2710 static void
2711slang_clear_sug(lp)
2712 slang_T *lp;
2713{
2714 vim_free(lp->sl_sbyts);
2715 lp->sl_sbyts = NULL;
2716 vim_free(lp->sl_sidxs);
2717 lp->sl_sidxs = NULL;
2718 close_spellbuf(lp->sl_sugbuf);
2719 lp->sl_sugbuf = NULL;
2720 lp->sl_sugloaded = FALSE;
2721 lp->sl_sugtime = 0;
2722}
2723
2724/*
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002725 * Load one spell file and store the info into a slang_T.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002726 * Invoked through do_in_runtimepath().
2727 */
2728 static void
Bram Moolenaarb765d632005-06-07 21:00:02 +00002729spell_load_cb(fname, cookie)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002730 char_u *fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002731 void *cookie;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002732{
Bram Moolenaarda2303d2005-08-30 21:55:26 +00002733 spelload_T *slp = (spelload_T *)cookie;
2734 slang_T *slang;
2735
2736 slang = spell_load_file(fname, slp->sl_lang, NULL, FALSE);
2737 if (slang != NULL)
2738 {
2739 /* When a previously loaded file has NOBREAK also use it for the
2740 * ".add" files. */
2741 if (slp->sl_nobreak && slang->sl_add)
2742 slang->sl_nobreak = TRUE;
2743 else if (slang->sl_nobreak)
2744 slp->sl_nobreak = TRUE;
2745
2746 slp->sl_slang = slang;
2747 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002748}
2749
2750/*
2751 * Load one spell file and store the info into a slang_T.
2752 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00002753 * This is invoked in three ways:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002754 * - From spell_load_cb() to load a spell file for the first time. "lang" is
2755 * the language name, "old_lp" is NULL. Will allocate an slang_T.
2756 * - To reload a spell file that was changed. "lang" is NULL and "old_lp"
2757 * points to the existing slang_T.
Bram Moolenaar4770d092006-01-12 23:22:24 +00002758 * - Just after writing a .spl file; it's read back to produce the .sug file.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002759 * "old_lp" is NULL and "lang" is NULL. Will allocate an slang_T.
2760 *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002761 * Returns the slang_T the spell file was loaded into. NULL for error.
Bram Moolenaarb765d632005-06-07 21:00:02 +00002762 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002763 static slang_T *
2764spell_load_file(fname, lang, old_lp, silent)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002765 char_u *fname;
2766 char_u *lang;
2767 slang_T *old_lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002768 int silent; /* no error if file doesn't exist */
Bram Moolenaarb765d632005-06-07 21:00:02 +00002769{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002770 FILE *fd;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002771 char_u buf[VIMSPELLMAGICL];
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002772 char_u *p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002773 int i;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002774 int n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002775 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002776 char_u *save_sourcing_name = sourcing_name;
2777 linenr_T save_sourcing_lnum = sourcing_lnum;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002778 slang_T *lp = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002779 int c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00002780 int res;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002781
Bram Moolenaarb765d632005-06-07 21:00:02 +00002782 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002783 if (fd == NULL)
2784 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002785 if (!silent)
2786 EMSG2(_(e_notopen), fname);
2787 else if (p_verbose > 2)
2788 {
2789 verbose_enter();
2790 smsg((char_u *)e_notopen, fname);
2791 verbose_leave();
2792 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002793 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002794 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00002795 if (p_verbose > 2)
2796 {
2797 verbose_enter();
2798 smsg((char_u *)_("Reading spell file \"%s\""), fname);
2799 verbose_leave();
2800 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002801
Bram Moolenaarb765d632005-06-07 21:00:02 +00002802 if (old_lp == NULL)
2803 {
2804 lp = slang_alloc(lang);
2805 if (lp == NULL)
2806 goto endFAIL;
2807
2808 /* Remember the file name, used to reload the file when it's updated. */
2809 lp->sl_fname = vim_strsave(fname);
2810 if (lp->sl_fname == NULL)
2811 goto endFAIL;
2812
Bram Moolenaar56f78042010-12-08 17:09:32 +01002813 /* Check for .add.spl (_add.spl for VMS). */
2814 lp->sl_add = strstr((char *)gettail(fname), SPL_FNAME_ADD) != NULL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00002815 }
2816 else
2817 lp = old_lp;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002818
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002819 /* Set sourcing_name, so that error messages mention the file name. */
2820 sourcing_name = fname;
2821 sourcing_lnum = 0;
2822
Bram Moolenaar4770d092006-01-12 23:22:24 +00002823 /*
2824 * <HEADER>: <fileID>
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002825 */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002826 for (i = 0; i < VIMSPELLMAGICL; ++i)
2827 buf[i] = getc(fd); /* <fileID> */
2828 if (STRNCMP(buf, VIMSPELLMAGIC, VIMSPELLMAGICL) != 0)
2829 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002830 EMSG(_("E757: This does not look like a spell file"));
2831 goto endFAIL;
2832 }
2833 c = getc(fd); /* <versionnr> */
2834 if (c < VIMSPELLVERSION)
2835 {
2836 EMSG(_("E771: Old spell file, needs to be updated"));
2837 goto endFAIL;
2838 }
2839 else if (c > VIMSPELLVERSION)
2840 {
2841 EMSG(_("E772: Spell file is for newer version of Vim"));
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002842 goto endFAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002843 }
2844
Bram Moolenaar5195e452005-08-19 20:32:47 +00002845
2846 /*
2847 * <SECTIONS>: <section> ... <sectionend>
2848 * <section>: <sectionID> <sectionflags> <sectionlen> (section contents)
2849 */
2850 for (;;)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002851 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002852 n = getc(fd); /* <sectionID> or <sectionend> */
2853 if (n == SN_END)
2854 break;
2855 c = getc(fd); /* <sectionflags> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00002856 len = get4c(fd); /* <sectionlen> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00002857 if (len < 0)
2858 goto truncerr;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002859
Bram Moolenaar5195e452005-08-19 20:32:47 +00002860 res = 0;
2861 switch (n)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002862 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002863 case SN_INFO:
2864 lp->sl_info = read_string(fd, len); /* <infotext> */
2865 if (lp->sl_info == NULL)
2866 goto endFAIL;
2867 break;
2868
Bram Moolenaar5195e452005-08-19 20:32:47 +00002869 case SN_REGION:
2870 res = read_region_section(fd, lp, len);
2871 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002872
Bram Moolenaar5195e452005-08-19 20:32:47 +00002873 case SN_CHARFLAGS:
2874 res = read_charflags_section(fd);
2875 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002876
Bram Moolenaar5195e452005-08-19 20:32:47 +00002877 case SN_MIDWORD:
2878 lp->sl_midword = read_string(fd, len); /* <midword> */
2879 if (lp->sl_midword == NULL)
2880 goto endFAIL;
2881 break;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002882
Bram Moolenaar5195e452005-08-19 20:32:47 +00002883 case SN_PREFCOND:
2884 res = read_prefcond_section(fd, lp);
2885 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002886
Bram Moolenaar5195e452005-08-19 20:32:47 +00002887 case SN_REP:
Bram Moolenaar4770d092006-01-12 23:22:24 +00002888 res = read_rep_section(fd, &lp->sl_rep, lp->sl_rep_first);
2889 break;
2890
2891 case SN_REPSAL:
2892 res = read_rep_section(fd, &lp->sl_repsal, lp->sl_repsal_first);
Bram Moolenaar5195e452005-08-19 20:32:47 +00002893 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002894
Bram Moolenaar5195e452005-08-19 20:32:47 +00002895 case SN_SAL:
2896 res = read_sal_section(fd, lp);
2897 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002898
Bram Moolenaar5195e452005-08-19 20:32:47 +00002899 case SN_SOFO:
2900 res = read_sofo_section(fd, lp);
2901 break;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00002902
Bram Moolenaar5195e452005-08-19 20:32:47 +00002903 case SN_MAP:
2904 p = read_string(fd, len); /* <mapstr> */
2905 if (p == NULL)
2906 goto endFAIL;
2907 set_map_str(lp, p);
2908 vim_free(p);
2909 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002910
Bram Moolenaar4770d092006-01-12 23:22:24 +00002911 case SN_WORDS:
2912 res = read_words_section(fd, lp, len);
2913 break;
2914
2915 case SN_SUGFILE:
Bram Moolenaarcdf04202010-05-29 15:11:47 +02002916 lp->sl_sugtime = get8ctime(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00002917 break;
2918
Bram Moolenaare1438bb2006-03-01 22:01:55 +00002919 case SN_NOSPLITSUGS:
2920 lp->sl_nosplitsugs = TRUE; /* <timestamp> */
2921 break;
2922
Bram Moolenaar5195e452005-08-19 20:32:47 +00002923 case SN_COMPOUND:
2924 res = read_compound(fd, lp, len);
2925 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002926
Bram Moolenaar78622822005-08-23 21:00:13 +00002927 case SN_NOBREAK:
2928 lp->sl_nobreak = TRUE;
2929 break;
2930
Bram Moolenaar5195e452005-08-19 20:32:47 +00002931 case SN_SYLLABLE:
2932 lp->sl_syllable = read_string(fd, len); /* <syllable> */
2933 if (lp->sl_syllable == NULL)
2934 goto endFAIL;
2935 if (init_syl_tab(lp) == FAIL)
2936 goto endFAIL;
2937 break;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002938
Bram Moolenaar5195e452005-08-19 20:32:47 +00002939 default:
2940 /* Unsupported section. When it's required give an error
2941 * message. When it's not required skip the contents. */
2942 if (c & SNF_REQUIRED)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002943 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002944 EMSG(_("E770: Unsupported section in spell file"));
Bram Moolenaar42eeac32005-06-29 22:40:58 +00002945 goto endFAIL;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002946 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00002947 while (--len >= 0)
2948 if (getc(fd) < 0)
2949 goto truncerr;
2950 break;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00002951 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00002952someerror:
Bram Moolenaar5195e452005-08-19 20:32:47 +00002953 if (res == SP_FORMERROR)
2954 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00002955 EMSG(_(e_format));
2956 goto endFAIL;
2957 }
2958 if (res == SP_TRUNCERROR)
2959 {
2960truncerr:
2961 EMSG(_(e_spell_trunc));
2962 goto endFAIL;
2963 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00002964 if (res == SP_OTHERERROR)
Bram Moolenaar5195e452005-08-19 20:32:47 +00002965 goto endFAIL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00002966 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002967
Bram Moolenaar4770d092006-01-12 23:22:24 +00002968 /* <LWORDTREE> */
2969 res = spell_read_tree(fd, &lp->sl_fbyts, &lp->sl_fidxs, FALSE, 0);
2970 if (res != 0)
2971 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002972
Bram Moolenaar4770d092006-01-12 23:22:24 +00002973 /* <KWORDTREE> */
2974 res = spell_read_tree(fd, &lp->sl_kbyts, &lp->sl_kidxs, FALSE, 0);
2975 if (res != 0)
2976 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002977
Bram Moolenaar4770d092006-01-12 23:22:24 +00002978 /* <PREFIXTREE> */
2979 res = spell_read_tree(fd, &lp->sl_pbyts, &lp->sl_pidxs, TRUE,
2980 lp->sl_prefixcnt);
2981 if (res != 0)
2982 goto someerror;
Bram Moolenaar51485f02005-06-04 21:55:20 +00002983
Bram Moolenaarb765d632005-06-07 21:00:02 +00002984 /* For a new file link it in the list of spell files. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00002985 if (old_lp == NULL && lang != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00002986 {
2987 lp->sl_next = first_lang;
2988 first_lang = lp;
2989 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002990
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002991 goto endOK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00002992
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00002993endFAIL:
Bram Moolenaarb765d632005-06-07 21:00:02 +00002994 if (lang != NULL)
2995 /* truncating the name signals the error to spell_load_lang() */
2996 *lang = NUL;
2997 if (lp != NULL && old_lp == NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00002998 slang_free(lp);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00002999 lp = NULL;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00003000
3001endOK:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003002 if (fd != NULL)
3003 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003004 sourcing_name = save_sourcing_name;
3005 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003006
3007 return lp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003008}
3009
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003010/*
3011 * Read a length field from "fd" in "cnt_bytes" bytes.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003012 * Allocate memory, read the string into it and add a NUL at the end.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003013 * Returns NULL when the count is zero.
Bram Moolenaar5195e452005-08-19 20:32:47 +00003014 * Sets "*cntp" to SP_*ERROR when there is an error, length of the result
3015 * otherwise.
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003016 */
3017 static char_u *
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003018read_cnt_string(fd, cnt_bytes, cntp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003019 FILE *fd;
3020 int cnt_bytes;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003021 int *cntp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003022{
3023 int cnt = 0;
3024 int i;
3025 char_u *str;
3026
3027 /* read the length bytes, MSB first */
3028 for (i = 0; i < cnt_bytes; ++i)
3029 cnt = (cnt << 8) + getc(fd);
3030 if (cnt < 0)
3031 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00003032 *cntp = SP_TRUNCERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003033 return NULL;
3034 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00003035 *cntp = cnt;
3036 if (cnt == 0)
3037 return NULL; /* nothing to read, return NULL */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003038
Bram Moolenaar5195e452005-08-19 20:32:47 +00003039 str = read_string(fd, cnt);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003040 if (str == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003041 *cntp = SP_OTHERERROR;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00003042 return str;
3043}
3044
Bram Moolenaar7887d882005-07-01 22:33:52 +00003045/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003046 * Read SN_REGION: <regionname> ...
3047 * Return SP_*ERROR flags.
3048 */
3049 static int
3050read_region_section(fd, lp, len)
3051 FILE *fd;
3052 slang_T *lp;
3053 int len;
3054{
3055 int i;
3056
3057 if (len > 16)
3058 return SP_FORMERROR;
3059 for (i = 0; i < len; ++i)
3060 lp->sl_regions[i] = getc(fd); /* <regionname> */
3061 lp->sl_regions[len] = NUL;
3062 return 0;
3063}
3064
3065/*
3066 * Read SN_CHARFLAGS section: <charflagslen> <charflags>
3067 * <folcharslen> <folchars>
3068 * Return SP_*ERROR flags.
3069 */
3070 static int
3071read_charflags_section(fd)
3072 FILE *fd;
3073{
3074 char_u *flags;
3075 char_u *fol;
3076 int flagslen, follen;
3077
3078 /* <charflagslen> <charflags> */
3079 flags = read_cnt_string(fd, 1, &flagslen);
3080 if (flagslen < 0)
3081 return flagslen;
3082
3083 /* <folcharslen> <folchars> */
3084 fol = read_cnt_string(fd, 2, &follen);
3085 if (follen < 0)
3086 {
3087 vim_free(flags);
3088 return follen;
3089 }
3090
3091 /* Set the word-char flags and fill SPELL_ISUPPER() table. */
3092 if (flags != NULL && fol != NULL)
3093 set_spell_charflags(flags, flagslen, fol);
3094
3095 vim_free(flags);
3096 vim_free(fol);
3097
3098 /* When <charflagslen> is zero then <fcharlen> must also be zero. */
3099 if ((flags == NULL) != (fol == NULL))
3100 return SP_FORMERROR;
3101 return 0;
3102}
3103
3104/*
3105 * Read SN_PREFCOND section.
3106 * Return SP_*ERROR flags.
3107 */
3108 static int
3109read_prefcond_section(fd, lp)
3110 FILE *fd;
3111 slang_T *lp;
3112{
3113 int cnt;
3114 int i;
3115 int n;
3116 char_u *p;
3117 char_u buf[MAXWLEN + 1];
3118
3119 /* <prefcondcnt> <prefcond> ... */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003120 cnt = get2c(fd); /* <prefcondcnt> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003121 if (cnt <= 0)
3122 return SP_FORMERROR;
3123
3124 lp->sl_prefprog = (regprog_T **)alloc_clear(
3125 (unsigned)sizeof(regprog_T *) * cnt);
3126 if (lp->sl_prefprog == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003127 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003128 lp->sl_prefixcnt = cnt;
3129
3130 for (i = 0; i < cnt; ++i)
3131 {
3132 /* <prefcond> : <condlen> <condstr> */
3133 n = getc(fd); /* <condlen> */
3134 if (n < 0 || n >= MAXWLEN)
3135 return SP_FORMERROR;
3136
3137 /* When <condlen> is zero we have an empty condition. Otherwise
3138 * compile the regexp program used to check for the condition. */
3139 if (n > 0)
3140 {
3141 buf[0] = '^'; /* always match at one position only */
3142 p = buf + 1;
3143 while (n-- > 0)
3144 *p++ = getc(fd); /* <condstr> */
3145 *p = NUL;
3146 lp->sl_prefprog[i] = vim_regcomp(buf, RE_MAGIC + RE_STRING);
3147 }
3148 }
3149 return 0;
3150}
3151
3152/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003153 * Read REP or REPSAL items section from "fd": <repcount> <rep> ...
Bram Moolenaar5195e452005-08-19 20:32:47 +00003154 * Return SP_*ERROR flags.
3155 */
3156 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +00003157read_rep_section(fd, gap, first)
Bram Moolenaar5195e452005-08-19 20:32:47 +00003158 FILE *fd;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003159 garray_T *gap;
3160 short *first;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003161{
3162 int cnt;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003163 fromto_T *ftp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003164 int i;
3165
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003166 cnt = get2c(fd); /* <repcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003167 if (cnt < 0)
3168 return SP_TRUNCERROR;
3169
Bram Moolenaar5195e452005-08-19 20:32:47 +00003170 if (ga_grow(gap, cnt) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003171 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003172
3173 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
3174 for (; gap->ga_len < cnt; ++gap->ga_len)
3175 {
3176 ftp = &((fromto_T *)gap->ga_data)[gap->ga_len];
3177 ftp->ft_from = read_cnt_string(fd, 1, &i);
3178 if (i < 0)
3179 return i;
3180 if (i == 0)
3181 return SP_FORMERROR;
3182 ftp->ft_to = read_cnt_string(fd, 1, &i);
3183 if (i <= 0)
3184 {
3185 vim_free(ftp->ft_from);
3186 if (i < 0)
3187 return i;
3188 return SP_FORMERROR;
3189 }
3190 }
3191
3192 /* Fill the first-index table. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003193 for (i = 0; i < 256; ++i)
3194 first[i] = -1;
3195 for (i = 0; i < gap->ga_len; ++i)
3196 {
3197 ftp = &((fromto_T *)gap->ga_data)[i];
3198 if (first[*ftp->ft_from] == -1)
3199 first[*ftp->ft_from] = i;
3200 }
3201 return 0;
3202}
3203
3204/*
3205 * Read SN_SAL section: <salflags> <salcount> <sal> ...
3206 * Return SP_*ERROR flags.
3207 */
3208 static int
3209read_sal_section(fd, slang)
3210 FILE *fd;
3211 slang_T *slang;
3212{
3213 int i;
3214 int cnt;
3215 garray_T *gap;
3216 salitem_T *smp;
3217 int ccnt;
3218 char_u *p;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003219 int c = NUL;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003220
3221 slang->sl_sofo = FALSE;
3222
3223 i = getc(fd); /* <salflags> */
3224 if (i & SAL_F0LLOWUP)
3225 slang->sl_followup = TRUE;
3226 if (i & SAL_COLLAPSE)
3227 slang->sl_collapse = TRUE;
3228 if (i & SAL_REM_ACCENTS)
3229 slang->sl_rem_accents = TRUE;
3230
Bram Moolenaarb388adb2006-02-28 23:50:17 +00003231 cnt = get2c(fd); /* <salcount> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003232 if (cnt < 0)
3233 return SP_TRUNCERROR;
3234
3235 gap = &slang->sl_sal;
3236 ga_init2(gap, sizeof(salitem_T), 10);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003237 if (ga_grow(gap, cnt + 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003238 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003239
3240 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
3241 for (; gap->ga_len < cnt; ++gap->ga_len)
3242 {
3243 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3244 ccnt = getc(fd); /* <salfromlen> */
3245 if (ccnt < 0)
3246 return SP_TRUNCERROR;
3247 if ((p = alloc(ccnt + 2)) == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003248 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003249 smp->sm_lead = p;
3250
3251 /* Read up to the first special char into sm_lead. */
3252 for (i = 0; i < ccnt; ++i)
3253 {
3254 c = getc(fd); /* <salfrom> */
3255 if (vim_strchr((char_u *)"0123456789(-<^$", c) != NULL)
3256 break;
3257 *p++ = c;
3258 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003259 smp->sm_leadlen = (int)(p - smp->sm_lead);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003260 *p++ = NUL;
3261
3262 /* Put (abc) chars in sm_oneof, if any. */
3263 if (c == '(')
3264 {
3265 smp->sm_oneof = p;
3266 for (++i; i < ccnt; ++i)
3267 {
3268 c = getc(fd); /* <salfrom> */
3269 if (c == ')')
3270 break;
3271 *p++ = c;
3272 }
3273 *p++ = NUL;
3274 if (++i < ccnt)
3275 c = getc(fd);
3276 }
3277 else
3278 smp->sm_oneof = NULL;
3279
3280 /* Any following chars go in sm_rules. */
3281 smp->sm_rules = p;
3282 if (i < ccnt)
3283 /* store the char we got while checking for end of sm_lead */
3284 *p++ = c;
3285 for (++i; i < ccnt; ++i)
3286 *p++ = getc(fd); /* <salfrom> */
3287 *p++ = NUL;
3288
3289 /* <saltolen> <salto> */
3290 smp->sm_to = read_cnt_string(fd, 1, &ccnt);
3291 if (ccnt < 0)
3292 {
3293 vim_free(smp->sm_lead);
3294 return ccnt;
3295 }
3296
3297#ifdef FEAT_MBYTE
3298 if (has_mbyte)
3299 {
3300 /* convert the multi-byte strings to wide char strings */
3301 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3302 smp->sm_leadlen = mb_charlen(smp->sm_lead);
3303 if (smp->sm_oneof == NULL)
3304 smp->sm_oneof_w = NULL;
3305 else
3306 smp->sm_oneof_w = mb_str2wide(smp->sm_oneof);
3307 if (smp->sm_to == NULL)
3308 smp->sm_to_w = NULL;
3309 else
3310 smp->sm_to_w = mb_str2wide(smp->sm_to);
3311 if (smp->sm_lead_w == NULL
3312 || (smp->sm_oneof_w == NULL && smp->sm_oneof != NULL)
3313 || (smp->sm_to_w == NULL && smp->sm_to != NULL))
3314 {
3315 vim_free(smp->sm_lead);
3316 vim_free(smp->sm_to);
3317 vim_free(smp->sm_lead_w);
3318 vim_free(smp->sm_oneof_w);
3319 vim_free(smp->sm_to_w);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003320 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003321 }
3322 }
3323#endif
3324 }
3325
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +00003326 if (gap->ga_len > 0)
3327 {
3328 /* Add one extra entry to mark the end with an empty sm_lead. Avoids
3329 * that we need to check the index every time. */
3330 smp = &((salitem_T *)gap->ga_data)[gap->ga_len];
3331 if ((p = alloc(1)) == NULL)
3332 return SP_OTHERERROR;
3333 p[0] = NUL;
3334 smp->sm_lead = p;
3335 smp->sm_leadlen = 0;
3336 smp->sm_oneof = NULL;
3337 smp->sm_rules = p;
3338 smp->sm_to = NULL;
3339#ifdef FEAT_MBYTE
3340 if (has_mbyte)
3341 {
3342 smp->sm_lead_w = mb_str2wide(smp->sm_lead);
3343 smp->sm_leadlen = 0;
3344 smp->sm_oneof_w = NULL;
3345 smp->sm_to_w = NULL;
3346 }
3347#endif
3348 ++gap->ga_len;
3349 }
3350
Bram Moolenaar5195e452005-08-19 20:32:47 +00003351 /* Fill the first-index table. */
3352 set_sal_first(slang);
3353
3354 return 0;
3355}
3356
3357/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00003358 * Read SN_WORDS: <word> ...
3359 * Return SP_*ERROR flags.
3360 */
3361 static int
3362read_words_section(fd, lp, len)
3363 FILE *fd;
3364 slang_T *lp;
3365 int len;
3366{
3367 int done = 0;
3368 int i;
Bram Moolenaara7241f52008-06-24 20:39:31 +00003369 int c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003370 char_u word[MAXWLEN];
3371
3372 while (done < len)
3373 {
3374 /* Read one word at a time. */
3375 for (i = 0; ; ++i)
3376 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00003377 c = getc(fd);
3378 if (c == EOF)
3379 return SP_TRUNCERROR;
3380 word[i] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +00003381 if (word[i] == NUL)
3382 break;
3383 if (i == MAXWLEN - 1)
3384 return SP_FORMERROR;
3385 }
3386
3387 /* Init the count to 10. */
3388 count_common_word(lp, word, -1, 10);
3389 done += i + 1;
3390 }
3391 return 0;
3392}
3393
3394/*
3395 * Add a word to the hashtable of common words.
3396 * If it's already there then the counter is increased.
3397 */
3398 static void
3399count_common_word(lp, word, len, count)
3400 slang_T *lp;
3401 char_u *word;
3402 int len; /* word length, -1 for upto NUL */
3403 int count; /* 1 to count once, 10 to init */
3404{
3405 hash_T hash;
3406 hashitem_T *hi;
3407 wordcount_T *wc;
3408 char_u buf[MAXWLEN];
3409 char_u *p;
3410
3411 if (len == -1)
3412 p = word;
3413 else
3414 {
3415 vim_strncpy(buf, word, len);
3416 p = buf;
3417 }
3418
3419 hash = hash_hash(p);
3420 hi = hash_lookup(&lp->sl_wordcount, p, hash);
3421 if (HASHITEM_EMPTY(hi))
3422 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003423 wc = (wordcount_T *)alloc((unsigned)(sizeof(wordcount_T) + STRLEN(p)));
Bram Moolenaar4770d092006-01-12 23:22:24 +00003424 if (wc == NULL)
3425 return;
3426 STRCPY(wc->wc_word, p);
3427 wc->wc_count = count;
3428 hash_add_item(&lp->sl_wordcount, hi, wc->wc_word, hash);
3429 }
3430 else
3431 {
3432 wc = HI2WC(hi);
3433 if ((wc->wc_count += count) < (unsigned)count) /* check for overflow */
3434 wc->wc_count = MAXWORDCOUNT;
3435 }
3436}
3437
3438/*
3439 * Adjust the score of common words.
3440 */
3441 static int
3442score_wordcount_adj(slang, score, word, split)
3443 slang_T *slang;
3444 int score;
3445 char_u *word;
3446 int split; /* word was split, less bonus */
3447{
3448 hashitem_T *hi;
3449 wordcount_T *wc;
3450 int bonus;
3451 int newscore;
3452
3453 hi = hash_find(&slang->sl_wordcount, word);
3454 if (!HASHITEM_EMPTY(hi))
3455 {
3456 wc = HI2WC(hi);
3457 if (wc->wc_count < SCORE_THRES2)
3458 bonus = SCORE_COMMON1;
3459 else if (wc->wc_count < SCORE_THRES3)
3460 bonus = SCORE_COMMON2;
3461 else
3462 bonus = SCORE_COMMON3;
3463 if (split)
3464 newscore = score - bonus / 2;
3465 else
3466 newscore = score - bonus;
3467 if (newscore < 0)
3468 return 0;
3469 return newscore;
3470 }
3471 return score;
3472}
3473
3474/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00003475 * SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
3476 * Return SP_*ERROR flags.
3477 */
3478 static int
3479read_sofo_section(fd, slang)
3480 FILE *fd;
3481 slang_T *slang;
3482{
3483 int cnt;
3484 char_u *from, *to;
3485 int res;
3486
3487 slang->sl_sofo = TRUE;
3488
3489 /* <sofofromlen> <sofofrom> */
3490 from = read_cnt_string(fd, 2, &cnt);
3491 if (cnt < 0)
3492 return cnt;
3493
3494 /* <sofotolen> <sofoto> */
3495 to = read_cnt_string(fd, 2, &cnt);
3496 if (cnt < 0)
3497 {
3498 vim_free(from);
3499 return cnt;
3500 }
3501
3502 /* Store the info in slang->sl_sal and/or slang->sl_sal_first. */
3503 if (from != NULL && to != NULL)
3504 res = set_sofo(slang, from, to);
3505 else if (from != NULL || to != NULL)
3506 res = SP_FORMERROR; /* only one of two strings is an error */
3507 else
3508 res = 0;
3509
3510 vim_free(from);
3511 vim_free(to);
3512 return res;
3513}
3514
3515/*
3516 * Read the compound section from the .spl file:
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003517 * <compmax> <compminlen> <compsylmax> <compoptions> <compflags>
Bram Moolenaar5195e452005-08-19 20:32:47 +00003518 * Returns SP_*ERROR flags.
3519 */
3520 static int
3521read_compound(fd, slang, len)
3522 FILE *fd;
3523 slang_T *slang;
3524 int len;
3525{
3526 int todo = len;
3527 int c;
3528 int atstart;
3529 char_u *pat;
3530 char_u *pp;
3531 char_u *cp;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003532 char_u *ap;
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003533 char_u *crp;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003534 int cnt;
3535 garray_T *gap;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003536
3537 if (todo < 2)
3538 return SP_FORMERROR; /* need at least two bytes */
3539
3540 --todo;
3541 c = getc(fd); /* <compmax> */
3542 if (c < 2)
3543 c = MAXWLEN;
3544 slang->sl_compmax = c;
3545
3546 --todo;
3547 c = getc(fd); /* <compminlen> */
3548 if (c < 1)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00003549 c = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003550 slang->sl_compminlen = c;
3551
3552 --todo;
3553 c = getc(fd); /* <compsylmax> */
3554 if (c < 1)
3555 c = MAXWLEN;
3556 slang->sl_compsylmax = c;
3557
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003558 c = getc(fd); /* <compoptions> */
3559 if (c != 0)
3560 ungetc(c, fd); /* be backwards compatible with Vim 7.0b */
3561 else
3562 {
3563 --todo;
3564 c = getc(fd); /* only use the lower byte for now */
3565 --todo;
3566 slang->sl_compoptions = c;
3567
3568 gap = &slang->sl_comppat;
3569 c = get2c(fd); /* <comppatcount> */
3570 todo -= 2;
3571 ga_init2(gap, sizeof(char_u *), c);
3572 if (ga_grow(gap, c) == OK)
3573 while (--c >= 0)
3574 {
3575 ((char_u **)(gap->ga_data))[gap->ga_len++] =
3576 read_cnt_string(fd, 1, &cnt);
3577 /* <comppatlen> <comppattext> */
3578 if (cnt < 0)
3579 return cnt;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003580 todo -= cnt + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003581 }
3582 }
Bram Moolenaar5555acc2006-04-07 21:33:12 +00003583 if (todo < 0)
3584 return SP_FORMERROR;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003585
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003586 /* Turn the COMPOUNDRULE items into a regexp pattern:
Bram Moolenaar5195e452005-08-19 20:32:47 +00003587 * "a[bc]/a*b+" -> "^\(a[bc]\|a*b\+\)$".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003588 * Inserting backslashes may double the length, "^\(\)$<Nul>" is 7 bytes.
3589 * Conversion to utf-8 may double the size. */
3590 c = todo * 2 + 7;
3591#ifdef FEAT_MBYTE
3592 if (enc_utf8)
3593 c += todo * 2;
3594#endif
3595 pat = alloc((unsigned)c);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003596 if (pat == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003597 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003598
Bram Moolenaard12a1322005-08-21 22:08:24 +00003599 /* We also need a list of all flags that can appear at the start and one
3600 * for all flags. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003601 cp = alloc(todo + 1);
3602 if (cp == NULL)
3603 {
3604 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003605 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003606 }
3607 slang->sl_compstartflags = cp;
3608 *cp = NUL;
3609
Bram Moolenaard12a1322005-08-21 22:08:24 +00003610 ap = alloc(todo + 1);
3611 if (ap == NULL)
3612 {
3613 vim_free(pat);
Bram Moolenaar6de68532005-08-24 22:08:48 +00003614 return SP_OTHERERROR;
Bram Moolenaard12a1322005-08-21 22:08:24 +00003615 }
3616 slang->sl_compallflags = ap;
3617 *ap = NUL;
3618
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003619 /* And a list of all patterns in their original form, for checking whether
3620 * compounding may work in match_compoundrule(). This is freed when we
3621 * encounter a wildcard, the check doesn't work then. */
3622 crp = alloc(todo + 1);
3623 slang->sl_comprules = crp;
3624
Bram Moolenaar5195e452005-08-19 20:32:47 +00003625 pp = pat;
3626 *pp++ = '^';
3627 *pp++ = '\\';
3628 *pp++ = '(';
3629
3630 atstart = 1;
3631 while (todo-- > 0)
3632 {
3633 c = getc(fd); /* <compflags> */
Bram Moolenaara7241f52008-06-24 20:39:31 +00003634 if (c == EOF)
3635 {
3636 vim_free(pat);
3637 return SP_TRUNCERROR;
3638 }
Bram Moolenaard12a1322005-08-21 22:08:24 +00003639
3640 /* Add all flags to "sl_compallflags". */
3641 if (vim_strchr((char_u *)"+*[]/", c) == NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00003642 && !byte_in_str(slang->sl_compallflags, c))
Bram Moolenaard12a1322005-08-21 22:08:24 +00003643 {
3644 *ap++ = c;
3645 *ap = NUL;
3646 }
3647
Bram Moolenaar5195e452005-08-19 20:32:47 +00003648 if (atstart != 0)
3649 {
3650 /* At start of item: copy flags to "sl_compstartflags". For a
3651 * [abc] item set "atstart" to 2 and copy up to the ']'. */
3652 if (c == '[')
3653 atstart = 2;
3654 else if (c == ']')
3655 atstart = 0;
3656 else
3657 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00003658 if (!byte_in_str(slang->sl_compstartflags, c))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003659 {
3660 *cp++ = c;
3661 *cp = NUL;
3662 }
3663 if (atstart == 1)
3664 atstart = 0;
3665 }
3666 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003667
3668 /* Copy flag to "sl_comprules", unless we run into a wildcard. */
3669 if (crp != NULL)
3670 {
3671 if (c == '+' || c == '*')
3672 {
3673 vim_free(slang->sl_comprules);
3674 slang->sl_comprules = NULL;
3675 crp = NULL;
3676 }
3677 else
3678 *crp++ = c;
3679 }
3680
Bram Moolenaar5195e452005-08-19 20:32:47 +00003681 if (c == '/') /* slash separates two items */
3682 {
3683 *pp++ = '\\';
3684 *pp++ = '|';
3685 atstart = 1;
3686 }
3687 else /* normal char, "[abc]" and '*' are copied as-is */
3688 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00003689 if (c == '+' || c == '~')
Bram Moolenaar5195e452005-08-19 20:32:47 +00003690 *pp++ = '\\'; /* "a+" becomes "a\+" */
Bram Moolenaar6de68532005-08-24 22:08:48 +00003691#ifdef FEAT_MBYTE
3692 if (enc_utf8)
3693 pp += mb_char2bytes(c, pp);
3694 else
3695#endif
3696 *pp++ = c;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003697 }
3698 }
3699
3700 *pp++ = '\\';
3701 *pp++ = ')';
3702 *pp++ = '$';
3703 *pp = NUL;
3704
Bram Moolenaar9f94b052008-11-30 20:12:46 +00003705 if (crp != NULL)
3706 *crp = NUL;
3707
Bram Moolenaar5195e452005-08-19 20:32:47 +00003708 slang->sl_compprog = vim_regcomp(pat, RE_MAGIC + RE_STRING + RE_STRICT);
3709 vim_free(pat);
3710 if (slang->sl_compprog == NULL)
3711 return SP_FORMERROR;
3712
3713 return 0;
3714}
3715
Bram Moolenaar6de68532005-08-24 22:08:48 +00003716/*
Bram Moolenaar95529562005-08-25 21:21:38 +00003717 * Return TRUE if byte "n" appears in "str".
Bram Moolenaar6de68532005-08-24 22:08:48 +00003718 * Like strchr() but independent of locale.
3719 */
3720 static int
Bram Moolenaar95529562005-08-25 21:21:38 +00003721byte_in_str(str, n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003722 char_u *str;
Bram Moolenaar95529562005-08-25 21:21:38 +00003723 int n;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003724{
3725 char_u *p;
3726
3727 for (p = str; *p != NUL; ++p)
Bram Moolenaar95529562005-08-25 21:21:38 +00003728 if (*p == n)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003729 return TRUE;
3730 return FALSE;
3731}
3732
Bram Moolenaar5195e452005-08-19 20:32:47 +00003733#define SY_MAXLEN 30
3734typedef struct syl_item_S
3735{
3736 char_u sy_chars[SY_MAXLEN]; /* the sequence of chars */
3737 int sy_len;
3738} syl_item_T;
3739
3740/*
3741 * Truncate "slang->sl_syllable" at the first slash and put the following items
3742 * in "slang->sl_syl_items".
3743 */
3744 static int
3745init_syl_tab(slang)
3746 slang_T *slang;
3747{
3748 char_u *p;
3749 char_u *s;
3750 int l;
3751 syl_item_T *syl;
3752
3753 ga_init2(&slang->sl_syl_items, sizeof(syl_item_T), 4);
3754 p = vim_strchr(slang->sl_syllable, '/');
3755 while (p != NULL)
3756 {
3757 *p++ = NUL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00003758 if (*p == NUL) /* trailing slash */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003759 break;
3760 s = p;
3761 p = vim_strchr(p, '/');
3762 if (p == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003763 l = (int)STRLEN(s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003764 else
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003765 l = (int)(p - s);
Bram Moolenaar5195e452005-08-19 20:32:47 +00003766 if (l >= SY_MAXLEN)
3767 return SP_FORMERROR;
3768 if (ga_grow(&slang->sl_syl_items, 1) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003769 return SP_OTHERERROR;
Bram Moolenaar5195e452005-08-19 20:32:47 +00003770 syl = ((syl_item_T *)slang->sl_syl_items.ga_data)
3771 + slang->sl_syl_items.ga_len++;
3772 vim_strncpy(syl->sy_chars, s, l);
3773 syl->sy_len = l;
3774 }
3775 return OK;
3776}
3777
3778/*
3779 * Count the number of syllables in "word".
3780 * When "word" contains spaces the syllables after the last space are counted.
3781 * Returns zero if syllables are not defines.
3782 */
3783 static int
3784count_syllables(slang, word)
3785 slang_T *slang;
3786 char_u *word;
3787{
3788 int cnt = 0;
3789 int skip = FALSE;
3790 char_u *p;
3791 int len;
3792 int i;
3793 syl_item_T *syl;
3794 int c;
3795
3796 if (slang->sl_syllable == NULL)
3797 return 0;
3798
3799 for (p = word; *p != NUL; p += len)
3800 {
3801 /* When running into a space reset counter. */
3802 if (*p == ' ')
3803 {
3804 len = 1;
3805 cnt = 0;
3806 continue;
3807 }
3808
3809 /* Find longest match of syllable items. */
3810 len = 0;
3811 for (i = 0; i < slang->sl_syl_items.ga_len; ++i)
3812 {
3813 syl = ((syl_item_T *)slang->sl_syl_items.ga_data) + i;
3814 if (syl->sy_len > len
3815 && STRNCMP(p, syl->sy_chars, syl->sy_len) == 0)
3816 len = syl->sy_len;
3817 }
3818 if (len != 0) /* found a match, count syllable */
3819 {
3820 ++cnt;
3821 skip = FALSE;
3822 }
3823 else
3824 {
3825 /* No recognized syllable item, at least a syllable char then? */
3826#ifdef FEAT_MBYTE
3827 c = mb_ptr2char(p);
3828 len = (*mb_ptr2len)(p);
3829#else
3830 c = *p;
3831 len = 1;
3832#endif
3833 if (vim_strchr(slang->sl_syllable, c) == NULL)
3834 skip = FALSE; /* No, search for next syllable */
3835 else if (!skip)
3836 {
3837 ++cnt; /* Yes, count it */
3838 skip = TRUE; /* don't count following syllable chars */
3839 }
3840 }
3841 }
3842 return cnt;
3843}
3844
3845/*
Bram Moolenaar7887d882005-07-01 22:33:52 +00003846 * Set the SOFOFROM and SOFOTO items in language "lp".
Bram Moolenaar5195e452005-08-19 20:32:47 +00003847 * Returns SP_*ERROR flags when there is something wrong.
Bram Moolenaar7887d882005-07-01 22:33:52 +00003848 */
3849 static int
3850set_sofo(lp, from, to)
3851 slang_T *lp;
3852 char_u *from;
3853 char_u *to;
3854{
3855 int i;
3856
3857#ifdef FEAT_MBYTE
3858 garray_T *gap;
3859 char_u *s;
3860 char_u *p;
3861 int c;
3862 int *inp;
3863
3864 if (has_mbyte)
3865 {
3866 /* Use "sl_sal" as an array with 256 pointers to a list of wide
3867 * characters. The index is the low byte of the character.
3868 * The list contains from-to pairs with a terminating NUL.
3869 * sl_sal_first[] is used for latin1 "from" characters. */
3870 gap = &lp->sl_sal;
3871 ga_init2(gap, sizeof(int *), 1);
3872 if (ga_grow(gap, 256) == FAIL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003873 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003874 vim_memset(gap->ga_data, 0, sizeof(int *) * 256);
3875 gap->ga_len = 256;
3876
3877 /* First count the number of items for each list. Temporarily use
3878 * sl_sal_first[] for this. */
3879 for (p = from, s = to; *p != NUL && *s != NUL; )
3880 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003881 c = mb_cptr2char_adv(&p);
3882 mb_cptr_adv(s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003883 if (c >= 256)
3884 ++lp->sl_sal_first[c & 0xff];
3885 }
3886 if (*p != NUL || *s != NUL) /* lengths differ */
Bram Moolenaar5195e452005-08-19 20:32:47 +00003887 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003888
3889 /* Allocate the lists. */
3890 for (i = 0; i < 256; ++i)
3891 if (lp->sl_sal_first[i] > 0)
3892 {
3893 p = alloc(sizeof(int) * (lp->sl_sal_first[i] * 2 + 1));
3894 if (p == NULL)
Bram Moolenaar6de68532005-08-24 22:08:48 +00003895 return SP_OTHERERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003896 ((int **)gap->ga_data)[i] = (int *)p;
3897 *(int *)p = 0;
3898 }
3899
3900 /* Put the characters up to 255 in sl_sal_first[] the rest in a sl_sal
3901 * list. */
3902 vim_memset(lp->sl_sal_first, 0, sizeof(salfirst_T) * 256);
3903 for (p = from, s = to; *p != NUL && *s != NUL; )
3904 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003905 c = mb_cptr2char_adv(&p);
3906 i = mb_cptr2char_adv(&s);
Bram Moolenaar7887d882005-07-01 22:33:52 +00003907 if (c >= 256)
3908 {
3909 /* Append the from-to chars at the end of the list with
3910 * the low byte. */
3911 inp = ((int **)gap->ga_data)[c & 0xff];
3912 while (*inp != 0)
3913 ++inp;
3914 *inp++ = c; /* from char */
3915 *inp++ = i; /* to char */
3916 *inp++ = NUL; /* NUL at the end */
3917 }
3918 else
3919 /* mapping byte to char is done in sl_sal_first[] */
3920 lp->sl_sal_first[c] = i;
3921 }
3922 }
3923 else
3924#endif
3925 {
3926 /* mapping bytes to bytes is done in sl_sal_first[] */
3927 if (STRLEN(from) != STRLEN(to))
Bram Moolenaar5195e452005-08-19 20:32:47 +00003928 return SP_FORMERROR;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003929
3930 for (i = 0; to[i] != NUL; ++i)
3931 lp->sl_sal_first[from[i]] = to[i];
3932 lp->sl_sal.ga_len = 1; /* indicates we have soundfolding */
3933 }
3934
Bram Moolenaar5195e452005-08-19 20:32:47 +00003935 return 0;
Bram Moolenaar7887d882005-07-01 22:33:52 +00003936}
3937
3938/*
3939 * Fill the first-index table for "lp".
3940 */
3941 static void
3942set_sal_first(lp)
3943 slang_T *lp;
3944{
3945 salfirst_T *sfirst;
3946 int i;
3947 salitem_T *smp;
3948 int c;
3949 garray_T *gap = &lp->sl_sal;
3950
3951 sfirst = lp->sl_sal_first;
3952 for (i = 0; i < 256; ++i)
3953 sfirst[i] = -1;
3954 smp = (salitem_T *)gap->ga_data;
3955 for (i = 0; i < gap->ga_len; ++i)
3956 {
3957#ifdef FEAT_MBYTE
3958 if (has_mbyte)
3959 /* Use the lowest byte of the first character. For latin1 it's
3960 * the character, for other encodings it should differ for most
3961 * characters. */
3962 c = *smp[i].sm_lead_w & 0xff;
3963 else
3964#endif
3965 c = *smp[i].sm_lead;
3966 if (sfirst[c] == -1)
3967 {
3968 sfirst[c] = i;
3969#ifdef FEAT_MBYTE
3970 if (has_mbyte)
3971 {
3972 int n;
3973
3974 /* Make sure all entries with this byte are following each
3975 * other. Move the ones that are in the wrong position. Do
3976 * keep the same ordering! */
3977 while (i + 1 < gap->ga_len
3978 && (*smp[i + 1].sm_lead_w & 0xff) == c)
3979 /* Skip over entry with same index byte. */
3980 ++i;
3981
3982 for (n = 1; i + n < gap->ga_len; ++n)
3983 if ((*smp[i + n].sm_lead_w & 0xff) == c)
3984 {
3985 salitem_T tsal;
3986
3987 /* Move entry with same index byte after the entries
3988 * we already found. */
3989 ++i;
3990 --n;
3991 tsal = smp[i + n];
3992 mch_memmove(smp + i + 1, smp + i,
3993 sizeof(salitem_T) * n);
3994 smp[i] = tsal;
3995 }
3996 }
3997#endif
3998 }
3999 }
4000}
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004001
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004002#ifdef FEAT_MBYTE
4003/*
4004 * Turn a multi-byte string into a wide character string.
4005 * Return it in allocated memory (NULL for out-of-memory)
4006 */
4007 static int *
4008mb_str2wide(s)
4009 char_u *s;
4010{
4011 int *res;
4012 char_u *p;
4013 int i = 0;
4014
4015 res = (int *)alloc(sizeof(int) * (mb_charlen(s) + 1));
4016 if (res != NULL)
4017 {
4018 for (p = s; *p != NUL; )
4019 res[i++] = mb_ptr2char_adv(&p);
4020 res[i] = NUL;
4021 }
4022 return res;
4023}
4024#endif
4025
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004026/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00004027 * Read a tree from the .spl or .sug file.
4028 * Allocates the memory and stores pointers in "bytsp" and "idxsp".
4029 * This is skipped when the tree has zero length.
4030 * Returns zero when OK, SP_ value for an error.
4031 */
4032 static int
4033spell_read_tree(fd, bytsp, idxsp, prefixtree, prefixcnt)
4034 FILE *fd;
4035 char_u **bytsp;
4036 idx_T **idxsp;
4037 int prefixtree; /* TRUE for the prefix tree */
4038 int prefixcnt; /* when "prefixtree" is TRUE: prefix count */
4039{
4040 int len;
4041 int idx;
4042 char_u *bp;
4043 idx_T *ip;
4044
4045 /* The tree size was computed when writing the file, so that we can
4046 * allocate it as one long block. <nodecount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004047 len = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00004048 if (len < 0)
4049 return SP_TRUNCERROR;
4050 if (len > 0)
4051 {
4052 /* Allocate the byte array. */
4053 bp = lalloc((long_u)len, TRUE);
4054 if (bp == NULL)
4055 return SP_OTHERERROR;
4056 *bytsp = bp;
4057
4058 /* Allocate the index array. */
4059 ip = (idx_T *)lalloc_clear((long_u)(len * sizeof(int)), TRUE);
4060 if (ip == NULL)
4061 return SP_OTHERERROR;
4062 *idxsp = ip;
4063
4064 /* Recursively read the tree and store it in the array. */
4065 idx = read_tree_node(fd, bp, ip, len, 0, prefixtree, prefixcnt);
4066 if (idx < 0)
4067 return idx;
4068 }
4069 return 0;
4070}
4071
4072/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004073 * Read one row of siblings from the spell file and store it in the byte array
4074 * "byts" and index array "idxs". Recursively read the children.
4075 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00004076 * NOTE: The code here must match put_node()!
Bram Moolenaar51485f02005-06-04 21:55:20 +00004077 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00004078 * Returns the index (>= 0) following the siblings.
4079 * Returns SP_TRUNCERROR if the file is shorter than expected.
4080 * Returns SP_FORMERROR if there is a format error.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004081 */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004082 static idx_T
Bram Moolenaar4770d092006-01-12 23:22:24 +00004083read_tree_node(fd, byts, idxs, maxidx, startidx, prefixtree, maxprefcondnr)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004084 FILE *fd;
4085 char_u *byts;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004086 idx_T *idxs;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004087 int maxidx; /* size of arrays */
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004088 idx_T startidx; /* current index in "byts" and "idxs" */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004089 int prefixtree; /* TRUE for reading PREFIXTREE */
4090 int maxprefcondnr; /* maximum for <prefcondnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004091{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004092 int len;
4093 int i;
4094 int n;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004095 idx_T idx = startidx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004096 int c;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004097 int c2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004098#define SHARED_MASK 0x8000000
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004099
Bram Moolenaar51485f02005-06-04 21:55:20 +00004100 len = getc(fd); /* <siblingcount> */
4101 if (len <= 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004102 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004103
4104 if (startidx + len >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004105 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004106 byts[idx++] = len;
4107
4108 /* Read the byte values, flag/region bytes and shared indexes. */
4109 for (i = 1; i <= len; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004110 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00004111 c = getc(fd); /* <byte> */
4112 if (c < 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004113 return SP_TRUNCERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004114 if (c <= BY_SPECIAL)
4115 {
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004116 if (c == BY_NOFLAGS && !prefixtree)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004117 {
4118 /* No flags, all regions. */
4119 idxs[idx] = 0;
4120 c = 0;
4121 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004122 else if (c != BY_INDEX)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004123 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004124 if (prefixtree)
4125 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004126 /* Read the optional pflags byte, the prefix ID and the
4127 * condition nr. In idxs[] store the prefix ID in the low
4128 * byte, the condition index shifted up 8 bits, the flags
4129 * shifted up 24 bits. */
4130 if (c == BY_FLAGS)
4131 c = getc(fd) << 24; /* <pflags> */
4132 else
4133 c = 0;
4134
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004135 c |= getc(fd); /* <affixID> */
Bram Moolenaar53805d12005-08-01 07:08:33 +00004136
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004137 n = get2c(fd); /* <prefcondnr> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004138 if (n >= maxprefcondnr)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004139 return SP_FORMERROR;
Bram Moolenaar53805d12005-08-01 07:08:33 +00004140 c |= (n << 8);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004141 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004142 else /* c must be BY_FLAGS or BY_FLAGS2 */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004143 {
4144 /* Read flags and optional region and prefix ID. In
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004145 * idxs[] the flags go in the low two bytes, region above
4146 * that and prefix ID above the region. */
4147 c2 = c;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004148 c = getc(fd); /* <flags> */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004149 if (c2 == BY_FLAGS2)
4150 c = (getc(fd) << 8) + c; /* <flags2> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004151 if (c & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004152 c = (getc(fd) << 16) + c; /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004153 if (c & WF_AFX)
4154 c = (getc(fd) << 24) + c; /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004155 }
4156
Bram Moolenaar51485f02005-06-04 21:55:20 +00004157 idxs[idx] = c;
4158 c = 0;
4159 }
4160 else /* c == BY_INDEX */
4161 {
4162 /* <nodeidx> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +00004163 n = get3c(fd);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004164 if (n < 0 || n >= maxidx)
Bram Moolenaar4770d092006-01-12 23:22:24 +00004165 return SP_FORMERROR;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004166 idxs[idx] = n + SHARED_MASK;
4167 c = getc(fd); /* <xbyte> */
4168 }
4169 }
4170 byts[idx++] = c;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004171 }
4172
Bram Moolenaar51485f02005-06-04 21:55:20 +00004173 /* Recursively read the children for non-shared siblings.
4174 * Skip the end-of-word ones (zero byte value) and the shared ones (and
4175 * remove SHARED_MASK) */
4176 for (i = 1; i <= len; ++i)
4177 if (byts[startidx + i] != 0)
4178 {
4179 if (idxs[startidx + i] & SHARED_MASK)
4180 idxs[startidx + i] &= ~SHARED_MASK;
4181 else
4182 {
4183 idxs[startidx + i] = idx;
Bram Moolenaar4770d092006-01-12 23:22:24 +00004184 idx = read_tree_node(fd, byts, idxs, maxidx, idx,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004185 prefixtree, maxprefcondnr);
Bram Moolenaar51485f02005-06-04 21:55:20 +00004186 if (idx < 0)
4187 break;
4188 }
4189 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004190
Bram Moolenaar51485f02005-06-04 21:55:20 +00004191 return idx;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004192}
4193
4194/*
Bram Moolenaar860cae12010-06-05 23:22:07 +02004195 * Parse 'spelllang' and set w_s->b_langp accordingly.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004196 * Returns NULL if it's OK, an error message otherwise.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004197 */
4198 char_u *
Bram Moolenaar860cae12010-06-05 23:22:07 +02004199did_set_spelllang(wp)
4200 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004201{
4202 garray_T ga;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004203 char_u *splp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004204 char_u *region;
Bram Moolenaarb6356332005-07-18 21:40:44 +00004205 char_u region_cp[3];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004206 int filename;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004207 int region_mask;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004208 slang_T *slang;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004209 int c;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004210 char_u lang[MAXWLEN + 1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004211 char_u spf_name[MAXPATHL];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004212 int len;
4213 char_u *p;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004214 int round;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004215 char_u *spf;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004216 char_u *use_region = NULL;
4217 int dont_use_region = FALSE;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004218 int nobreak = FALSE;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004219 int i, j;
4220 langp_T *lp, *lp2;
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004221 static int recursive = FALSE;
4222 char_u *ret_msg = NULL;
4223 char_u *spl_copy;
4224
4225 /* We don't want to do this recursively. May happen when a language is
4226 * not available and the SpellFileMissing autocommand opens a new buffer
4227 * in which 'spell' is set. */
4228 if (recursive)
4229 return NULL;
4230 recursive = TRUE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004231
4232 ga_init2(&ga, sizeof(langp_T), 2);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004233 clear_midword(wp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004234
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004235 /* Make a copy of 'spellang', the SpellFileMissing autocommands may change
4236 * it under our fingers. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004237 spl_copy = vim_strsave(wp->w_s->b_p_spl);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004238 if (spl_copy == NULL)
4239 goto theend;
4240
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004241 /* loop over comma separated language names. */
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004242 for (splp = spl_copy; *splp != NUL; )
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004243 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004244 /* Get one language name. */
4245 copy_option_part(&splp, lang, MAXWLEN, ",");
Bram Moolenaar5482f332005-04-17 20:18:43 +00004246 region = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004247 len = (int)STRLEN(lang);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004248
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004249 /* If the name ends in ".spl" use it as the name of the spell file.
4250 * If there is a region name let "region" point to it and remove it
4251 * from the name. */
4252 if (len > 4 && fnamecmp(lang + len - 4, ".spl") == 0)
4253 {
4254 filename = TRUE;
4255
Bram Moolenaarb6356332005-07-18 21:40:44 +00004256 /* Locate a region and remove it from the file name. */
4257 p = vim_strchr(gettail(lang), '_');
4258 if (p != NULL && ASCII_ISALPHA(p[1]) && ASCII_ISALPHA(p[2])
4259 && !ASCII_ISALPHA(p[3]))
4260 {
4261 vim_strncpy(region_cp, p + 1, 2);
4262 mch_memmove(p, p + 3, len - (p - lang) - 2);
4263 len -= 3;
4264 region = region_cp;
4265 }
4266 else
4267 dont_use_region = TRUE;
4268
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004269 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004270 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4271 if (fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004272 break;
4273 }
4274 else
4275 {
4276 filename = FALSE;
4277 if (len > 3 && lang[len - 3] == '_')
4278 {
4279 region = lang + len - 2;
4280 len -= 3;
4281 lang[len] = NUL;
4282 }
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004283 else
4284 dont_use_region = TRUE;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004285
4286 /* Check if we loaded this language before. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004287 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4288 if (STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004289 break;
4290 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004291
Bram Moolenaarb6356332005-07-18 21:40:44 +00004292 if (region != NULL)
4293 {
4294 /* If the region differs from what was used before then don't
4295 * use it for 'spellfile'. */
4296 if (use_region != NULL && STRCMP(region, use_region) != 0)
4297 dont_use_region = TRUE;
4298 use_region = region;
4299 }
4300
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004301 /* If not found try loading the language now. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004302 if (slang == NULL)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004303 {
4304 if (filename)
4305 (void)spell_load_file(lang, lang, NULL, FALSE);
4306 else
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004307 {
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004308 spell_load_lang(lang);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004309#ifdef FEAT_AUTOCMD
4310 /* SpellFileMissing autocommands may do anything, including
4311 * destroying the buffer we are using... */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004312 if (!buf_valid(wp->w_buffer))
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004313 {
4314 ret_msg = (char_u *)"E797: SpellFileMissing autocommand deleted buffer";
4315 goto theend;
4316 }
4317#endif
4318 }
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004319 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004320
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004321 /*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004322 * Loop over the languages, there can be several files for "lang".
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004323 */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004324 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4325 if (filename ? fullpathcmp(lang, slang->sl_fname, FALSE) == FPC_SAME
4326 : STRICMP(lang, slang->sl_name) == 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004327 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004328 region_mask = REGION_ALL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004329 if (!filename && region != NULL)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004330 {
4331 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004332 c = find_region(slang->sl_regions, region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004333 if (c == REGION_ALL)
4334 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004335 if (slang->sl_add)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004336 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004337 if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004338 /* This addition file is for other regions. */
4339 region_mask = 0;
4340 }
4341 else
4342 /* This is probably an error. Give a warning and
4343 * accept the words anyway. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004344 smsg((char_u *)
4345 _("Warning: region %s not supported"),
4346 region);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004347 }
4348 else
4349 region_mask = 1 << c;
4350 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004351
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004352 if (region_mask != 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004353 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004354 if (ga_grow(&ga, 1) == FAIL)
4355 {
4356 ga_clear(&ga);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004357 ret_msg = e_outofmem;
4358 goto theend;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004359 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004360 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004361 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4362 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004363 use_midword(slang, wp);
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004364 if (slang->sl_nobreak)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004365 nobreak = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004366 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004367 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004368 }
4369
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004370 /* round 0: load int_wordlist, if possible.
4371 * round 1: load first name in 'spellfile'.
4372 * round 2: load second name in 'spellfile.
4373 * etc. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004374 spf = curwin->w_s->b_p_spf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004375 for (round = 0; round == 0 || *spf != NUL; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004376 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004377 if (round == 0)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004378 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004379 /* Internal wordlist, if there is one. */
4380 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004381 continue;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004382 int_wordlist_spl(spf_name);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004383 }
4384 else
4385 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004386 /* One entry in 'spellfile'. */
4387 copy_option_part(&spf, spf_name, MAXPATHL - 5, ",");
4388 STRCAT(spf_name, ".spl");
4389
4390 /* If it was already found above then skip it. */
4391 for (c = 0; c < ga.ga_len; ++c)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004392 {
4393 p = LANGP_ENTRY(ga, c)->lp_slang->sl_fname;
4394 if (p != NULL && fullpathcmp(spf_name, p, FALSE) == FPC_SAME)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004395 break;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004396 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004397 if (c < ga.ga_len)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004398 continue;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004399 }
4400
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004401 /* Check if it was loaded already. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004402 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
4403 if (fullpathcmp(spf_name, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004404 break;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004405 if (slang == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004406 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004407 /* Not loaded, try loading it now. The language name includes the
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004408 * region name, the region is ignored otherwise. for int_wordlist
4409 * use an arbitrary name. */
4410 if (round == 0)
4411 STRCPY(lang, "internal wordlist");
4412 else
Bram Moolenaar7887d882005-07-01 22:33:52 +00004413 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004414 vim_strncpy(lang, gettail(spf_name), MAXWLEN);
Bram Moolenaar7887d882005-07-01 22:33:52 +00004415 p = vim_strchr(lang, '.');
4416 if (p != NULL)
4417 *p = NUL; /* truncate at ".encoding.add" */
4418 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004419 slang = spell_load_file(spf_name, lang, NULL, TRUE);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00004420
4421 /* If one of the languages has NOBREAK we assume the addition
4422 * files also have this. */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004423 if (slang != NULL && nobreak)
4424 slang->sl_nobreak = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004425 }
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004426 if (slang != NULL && ga_grow(&ga, 1) == OK)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004427 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004428 region_mask = REGION_ALL;
4429 if (use_region != NULL && !dont_use_region)
4430 {
4431 /* find region in sl_regions */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004432 c = find_region(slang->sl_regions, use_region);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004433 if (c != REGION_ALL)
4434 region_mask = 1 << c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004435 else if (*slang->sl_regions != NUL)
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004436 /* This spell file is for other regions. */
4437 region_mask = 0;
4438 }
4439
4440 if (region_mask != 0)
4441 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004442 LANGP_ENTRY(ga, ga.ga_len)->lp_slang = slang;
4443 LANGP_ENTRY(ga, ga.ga_len)->lp_sallang = NULL;
4444 LANGP_ENTRY(ga, ga.ga_len)->lp_replang = NULL;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004445 LANGP_ENTRY(ga, ga.ga_len)->lp_region = region_mask;
4446 ++ga.ga_len;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004447 use_midword(slang, wp);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004448 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004449 }
4450 }
4451
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004452 /* Everything is fine, store the new b_langp value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004453 ga_clear(&wp->w_s->b_langp);
4454 wp->w_s->b_langp = ga;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004455
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004456 /* For each language figure out what language to use for sound folding and
4457 * REP items. If the language doesn't support it itself use another one
4458 * with the same name. E.g. for "en-math" use "en". */
4459 for (i = 0; i < ga.ga_len; ++i)
4460 {
4461 lp = LANGP_ENTRY(ga, i);
4462
4463 /* sound folding */
4464 if (lp->lp_slang->sl_sal.ga_len > 0)
4465 /* language does sound folding itself */
4466 lp->lp_sallang = lp->lp_slang;
4467 else
4468 /* find first similar language that does sound folding */
4469 for (j = 0; j < ga.ga_len; ++j)
4470 {
4471 lp2 = LANGP_ENTRY(ga, j);
4472 if (lp2->lp_slang->sl_sal.ga_len > 0
4473 && STRNCMP(lp->lp_slang->sl_name,
4474 lp2->lp_slang->sl_name, 2) == 0)
4475 {
4476 lp->lp_sallang = lp2->lp_slang;
4477 break;
4478 }
4479 }
4480
4481 /* REP items */
4482 if (lp->lp_slang->sl_rep.ga_len > 0)
4483 /* language has REP items itself */
4484 lp->lp_replang = lp->lp_slang;
4485 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00004486 /* find first similar language that has REP items */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004487 for (j = 0; j < ga.ga_len; ++j)
4488 {
4489 lp2 = LANGP_ENTRY(ga, j);
4490 if (lp2->lp_slang->sl_rep.ga_len > 0
4491 && STRNCMP(lp->lp_slang->sl_name,
4492 lp2->lp_slang->sl_name, 2) == 0)
4493 {
4494 lp->lp_replang = lp2->lp_slang;
4495 break;
4496 }
4497 }
4498 }
4499
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00004500theend:
4501 vim_free(spl_copy);
4502 recursive = FALSE;
4503 return ret_msg;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004504}
4505
4506/*
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004507 * Clear the midword characters for buffer "buf".
4508 */
4509 static void
Bram Moolenaar860cae12010-06-05 23:22:07 +02004510clear_midword(wp)
4511 win_T *wp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004512{
Bram Moolenaar860cae12010-06-05 23:22:07 +02004513 vim_memset(wp->w_s->b_spell_ismw, 0, 256);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004514#ifdef FEAT_MBYTE
Bram Moolenaar860cae12010-06-05 23:22:07 +02004515 vim_free(wp->w_s->b_spell_ismw_mb);
4516 wp->w_s->b_spell_ismw_mb = NULL;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004517#endif
4518}
4519
4520/*
4521 * Use the "sl_midword" field of language "lp" for buffer "buf".
4522 * They add up to any currently used midword characters.
4523 */
4524 static void
Bram Moolenaar860cae12010-06-05 23:22:07 +02004525use_midword(lp, wp)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004526 slang_T *lp;
Bram Moolenaar860cae12010-06-05 23:22:07 +02004527 win_T *wp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004528{
4529 char_u *p;
4530
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00004531 if (lp->sl_midword == NULL) /* there aren't any */
4532 return;
4533
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004534 for (p = lp->sl_midword; *p != NUL; )
4535#ifdef FEAT_MBYTE
4536 if (has_mbyte)
4537 {
4538 int c, l, n;
4539 char_u *bp;
4540
4541 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004542 l = (*mb_ptr2len)(p);
4543 if (c < 256 && l <= 2)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004544 wp->w_s->b_spell_ismw[c] = TRUE;
4545 else if (wp->w_s->b_spell_ismw_mb == NULL)
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004546 /* First multi-byte char in "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004547 wp->w_s->b_spell_ismw_mb = vim_strnsave(p, l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004548 else
4549 {
4550 /* Append multi-byte chars to "b_spell_ismw_mb". */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004551 n = (int)STRLEN(wp->w_s->b_spell_ismw_mb);
4552 bp = vim_strnsave(wp->w_s->b_spell_ismw_mb, n + l);
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004553 if (bp != NULL)
4554 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004555 vim_free(wp->w_s->b_spell_ismw_mb);
4556 wp->w_s->b_spell_ismw_mb = bp;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004557 vim_strncpy(bp + n, p, l);
4558 }
4559 }
4560 p += l;
4561 }
4562 else
4563#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02004564 wp->w_s->b_spell_ismw[*p++] = TRUE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004565}
4566
4567/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004568 * Find the region "region[2]" in "rp" (points to "sl_regions").
4569 * Each region is simply stored as the two characters of it's name.
Bram Moolenaar7887d882005-07-01 22:33:52 +00004570 * Returns the index if found (first is 0), REGION_ALL if not found.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004571 */
4572 static int
4573find_region(rp, region)
4574 char_u *rp;
4575 char_u *region;
4576{
4577 int i;
4578
4579 for (i = 0; ; i += 2)
4580 {
4581 if (rp[i] == NUL)
4582 return REGION_ALL;
4583 if (rp[i] == region[0] && rp[i + 1] == region[1])
4584 break;
4585 }
4586 return i / 2;
4587}
4588
4589/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004590 * Return case type of word:
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004591 * w word 0
Bram Moolenaar51485f02005-06-04 21:55:20 +00004592 * Word WF_ONECAP
4593 * W WORD WF_ALLCAP
4594 * WoRd wOrd WF_KEEPCAP
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004595 */
4596 static int
4597captype(word, end)
4598 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004599 char_u *end; /* When NULL use up to NUL byte. */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004600{
4601 char_u *p;
4602 int c;
4603 int firstcap;
4604 int allcap;
4605 int past_second = FALSE; /* past second word char */
4606
4607 /* find first letter */
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004608 for (p = word; !spell_iswordp_nmw(p); mb_ptr_adv(p))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004609 if (end == NULL ? *p == NUL : p >= end)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004610 return 0; /* only non-word characters, illegal word */
4611#ifdef FEAT_MBYTE
Bram Moolenaarb765d632005-06-07 21:00:02 +00004612 if (has_mbyte)
4613 c = mb_ptr2char_adv(&p);
4614 else
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004615#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00004616 c = *p++;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004617 firstcap = allcap = SPELL_ISUPPER(c);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004618
4619 /*
4620 * Need to check all letters to find a word with mixed upper/lower.
4621 * But a word with an upper char only at start is a ONECAP.
4622 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004623 for ( ; end == NULL ? *p != NUL : p < end; mb_ptr_adv(p))
Bram Moolenaar9c96f592005-06-30 21:52:39 +00004624 if (spell_iswordp_nmw(p))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004625 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00004626 c = PTR2CHAR(p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00004627 if (!SPELL_ISUPPER(c))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004628 {
4629 /* UUl -> KEEPCAP */
4630 if (past_second && allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004631 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004632 allcap = FALSE;
4633 }
4634 else if (!allcap)
4635 /* UlU -> KEEPCAP */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004636 return WF_KEEPCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004637 past_second = TRUE;
4638 }
4639
4640 if (allcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004641 return WF_ALLCAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004642 if (firstcap)
Bram Moolenaar51485f02005-06-04 21:55:20 +00004643 return WF_ONECAP;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004644 return 0;
4645}
4646
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004647/*
4648 * Like captype() but for a KEEPCAP word add ONECAP if the word starts with a
4649 * capital. So that make_case_word() can turn WOrd into Word.
4650 * Add ALLCAP for "WOrD".
4651 */
4652 static int
4653badword_captype(word, end)
4654 char_u *word;
4655 char_u *end;
4656{
4657 int flags = captype(word, end);
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004658 int c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004659 int l, u;
4660 int first;
4661 char_u *p;
4662
4663 if (flags & WF_KEEPCAP)
4664 {
4665 /* Count the number of UPPER and lower case letters. */
4666 l = u = 0;
4667 first = FALSE;
4668 for (p = word; p < end; mb_ptr_adv(p))
4669 {
Bram Moolenaar8b59de92005-08-11 19:59:29 +00004670 c = PTR2CHAR(p);
4671 if (SPELL_ISUPPER(c))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004672 {
4673 ++u;
4674 if (p == word)
4675 first = TRUE;
4676 }
4677 else
4678 ++l;
4679 }
4680
4681 /* If there are more UPPER than lower case letters suggest an
4682 * ALLCAP word. Otherwise, if the first letter is UPPER then
4683 * suggest ONECAP. Exception: "ALl" most likely should be "All",
4684 * require three upper case letters. */
4685 if (u > l && u > 2)
4686 flags |= WF_ALLCAP;
4687 else if (first)
4688 flags |= WF_ONECAP;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004689
4690 if (u >= 2 && l >= 2) /* maCARONI maCAroni */
4691 flags |= WF_MIXCAP;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004692 }
4693 return flags;
4694}
4695
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004696# if defined(FEAT_MBYTE) || defined(EXITFREE) || defined(PROTO)
4697/*
4698 * Free all languages.
4699 */
4700 void
4701spell_free_all()
4702{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004703 slang_T *slang;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004704 buf_T *buf;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004705 char_u fname[MAXPATHL];
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004706
Bram Moolenaar60bb4e12010-09-18 13:36:49 +02004707 /* Go through all buffers and handle 'spelllang'. <VN> */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004708 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004709 ga_clear(&buf->b_s.b_langp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004710
4711 while (first_lang != NULL)
4712 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004713 slang = first_lang;
4714 first_lang = slang->sl_next;
4715 slang_free(slang);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004716 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004717
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004718 if (int_wordlist != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00004719 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00004720 /* Delete the internal wordlist and its .spl file */
4721 mch_remove(int_wordlist);
4722 int_wordlist_spl(fname);
4723 mch_remove(fname);
4724 vim_free(int_wordlist);
4725 int_wordlist = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00004726 }
4727
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00004728 init_spell_chartab();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004729
4730 vim_free(repl_to);
4731 repl_to = NULL;
4732 vim_free(repl_from);
4733 repl_from = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004734}
4735# endif
4736
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004737# if defined(FEAT_MBYTE) || defined(PROTO)
4738/*
4739 * Clear all spelling tables and reload them.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00004740 * Used after 'encoding' is set and when ":mkspell" was used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004741 */
4742 void
4743spell_reload()
4744{
Bram Moolenaar3982c542005-06-08 21:56:31 +00004745 win_T *wp;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004746
Bram Moolenaarea408852005-06-25 22:49:46 +00004747 /* Initialize the table for spell_iswordp(). */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004748 init_spell_chartab();
4749
4750 /* Unload all allocated memory. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00004751 spell_free_all();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004752
4753 /* Go through all buffers and handle 'spelllang'. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004754 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004755 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00004756 /* Only load the wordlists when 'spelllang' is set and there is a
4757 * window for this buffer in which 'spell' is set. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02004758 if (*wp->w_s->b_p_spl != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004759 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004760 if (wp->w_p_spell)
Bram Moolenaar3982c542005-06-08 21:56:31 +00004761 {
Bram Moolenaar860cae12010-06-05 23:22:07 +02004762 (void)did_set_spelllang(wp);
Bram Moolenaar3982c542005-06-08 21:56:31 +00004763# ifdef FEAT_WINDOWS
4764 break;
4765# endif
4766 }
4767 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004768 }
4769}
4770# endif
4771
Bram Moolenaarb765d632005-06-07 21:00:02 +00004772/*
4773 * Reload the spell file "fname" if it's loaded.
4774 */
4775 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004776spell_reload_one(fname, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004777 char_u *fname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004778 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004779{
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004780 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004781 int didit = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004782
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004783 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004784 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004785 if (fullpathcmp(fname, slang->sl_fname, FALSE) == FPC_SAME)
Bram Moolenaarb765d632005-06-07 21:00:02 +00004786 {
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004787 slang_clear(slang);
4788 if (spell_load_file(fname, NULL, slang, FALSE) == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004789 /* reloading failed, clear the language */
Bram Moolenaar8b96d642005-09-05 22:05:30 +00004790 slang_clear(slang);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00004791 redraw_all_later(SOME_VALID);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004792 didit = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00004793 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004794 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004795
4796 /* When "zg" was used and the file wasn't loaded yet, should redo
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00004797 * 'spelllang' to load it now. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004798 if (added_word && !didit)
Bram Moolenaar860cae12010-06-05 23:22:07 +02004799 did_set_spelllang(curwin);
Bram Moolenaarb765d632005-06-07 21:00:02 +00004800}
4801
4802
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004803/*
4804 * Functions for ":mkspell".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004805 */
4806
Bram Moolenaar51485f02005-06-04 21:55:20 +00004807#define MAXLINELEN 500 /* Maximum length in bytes of a line in a .aff
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004808 and .dic file. */
4809/*
4810 * Main structure to store the contents of a ".aff" file.
4811 */
4812typedef struct afffile_S
4813{
4814 char_u *af_enc; /* "SET", normalized, alloc'ed string or NULL */
Bram Moolenaar95529562005-08-25 21:21:38 +00004815 int af_flagtype; /* AFT_CHAR, AFT_LONG, AFT_NUM or AFT_CAPLONG */
Bram Moolenaar371baa92005-12-29 22:43:53 +00004816 unsigned af_rare; /* RARE ID for rare word */
4817 unsigned af_keepcase; /* KEEPCASE ID for keep-case word */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004818 unsigned af_bad; /* BAD ID for banned word */
4819 unsigned af_needaffix; /* NEEDAFFIX ID */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00004820 unsigned af_circumfix; /* CIRCUMFIX ID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00004821 unsigned af_needcomp; /* NEEDCOMPOUND ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004822 unsigned af_comproot; /* COMPOUNDROOT ID */
4823 unsigned af_compforbid; /* COMPOUNDFORBIDFLAG ID */
4824 unsigned af_comppermit; /* COMPOUNDPERMITFLAG ID */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004825 unsigned af_nosuggest; /* NOSUGGEST ID */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004826 int af_pfxpostpone; /* postpone prefixes without chop string and
4827 without flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004828 hashtab_T af_pref; /* hashtable for prefixes, affheader_T */
4829 hashtab_T af_suff; /* hashtable for suffixes, affheader_T */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004830 hashtab_T af_comp; /* hashtable for compound flags, compitem_T */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004831} afffile_T;
4832
Bram Moolenaar6de68532005-08-24 22:08:48 +00004833#define AFT_CHAR 0 /* flags are one character */
Bram Moolenaar95529562005-08-25 21:21:38 +00004834#define AFT_LONG 1 /* flags are two characters */
4835#define AFT_CAPLONG 2 /* flags are one or two characters */
4836#define AFT_NUM 3 /* flags are numbers, comma separated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004837
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004838typedef struct affentry_S affentry_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004839/* Affix entry from ".aff" file. Used for prefixes and suffixes. */
4840struct affentry_S
4841{
4842 affentry_T *ae_next; /* next affix with same name/number */
4843 char_u *ae_chop; /* text to chop off basic word (can be NULL) */
4844 char_u *ae_add; /* text to add to basic word (can be NULL) */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004845 char_u *ae_flags; /* flags on the affix (can be NULL) */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004846 char_u *ae_cond; /* condition (NULL for ".") */
4847 regprog_T *ae_prog; /* regexp program for ae_cond or NULL */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00004848 char ae_compforbid; /* COMPOUNDFORBIDFLAG found */
4849 char ae_comppermit; /* COMPOUNDPERMITFLAG found */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004850};
4851
Bram Moolenaar6de68532005-08-24 22:08:48 +00004852#ifdef FEAT_MBYTE
4853# define AH_KEY_LEN 17 /* 2 x 8 bytes + NUL */
4854#else
Bram Moolenaar95529562005-08-25 21:21:38 +00004855# define AH_KEY_LEN 7 /* 6 digits + NUL */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004856#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +00004857
Bram Moolenaar51485f02005-06-04 21:55:20 +00004858/* Affix header from ".aff" file. Used for af_pref and af_suff. */
4859typedef struct affheader_S
4860{
Bram Moolenaar6de68532005-08-24 22:08:48 +00004861 char_u ah_key[AH_KEY_LEN]; /* key for hashtab == name of affix */
4862 unsigned ah_flag; /* affix name as number, uses "af_flagtype" */
4863 int ah_newID; /* prefix ID after renumbering; 0 if not used */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004864 int ah_combine; /* suffix may combine with prefix */
Bram Moolenaar95529562005-08-25 21:21:38 +00004865 int ah_follows; /* another affix block should be following */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004866 affentry_T *ah_first; /* first affix entry */
4867} affheader_T;
4868
4869#define HI2AH(hi) ((affheader_T *)(hi)->hi_key)
4870
Bram Moolenaar6de68532005-08-24 22:08:48 +00004871/* Flag used in compound items. */
4872typedef struct compitem_S
4873{
4874 char_u ci_key[AH_KEY_LEN]; /* key for hashtab == name of compound */
4875 unsigned ci_flag; /* affix name as number, uses "af_flagtype" */
4876 int ci_newID; /* affix ID after renumbering. */
4877} compitem_T;
4878
4879#define HI2CI(hi) ((compitem_T *)(hi)->hi_key)
4880
Bram Moolenaar51485f02005-06-04 21:55:20 +00004881/*
4882 * Structure that is used to store the items in the word tree. This avoids
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004883 * the need to keep track of each allocated thing, everything is freed all at
4884 * once after ":mkspell" is done.
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00004885 * Note: "sb_next" must be just before "sb_data" to make sure the alignment of
4886 * "sb_data" is correct for systems where pointers must be aligned on
4887 * pointer-size boundaries and sizeof(pointer) > sizeof(int) (e.g., Sparc).
Bram Moolenaar51485f02005-06-04 21:55:20 +00004888 */
4889#define SBLOCKSIZE 16000 /* size of sb_data */
4890typedef struct sblock_S sblock_T;
4891struct sblock_S
4892{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004893 int sb_used; /* nr of bytes already in use */
Bram Moolenaar6ae167a2009-02-11 16:58:49 +00004894 sblock_T *sb_next; /* next block in list */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004895 char_u sb_data[1]; /* data, actually longer */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004896};
4897
4898/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00004899 * A node in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004900 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004901typedef struct wordnode_S wordnode_T;
4902struct wordnode_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004903{
Bram Moolenaar0c405862005-06-22 22:26:26 +00004904 union /* shared to save space */
4905 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004906 char_u hashkey[6]; /* the hash key, only used while compressing */
Bram Moolenaar0c405862005-06-22 22:26:26 +00004907 int index; /* index in written nodes (valid after first
4908 round) */
4909 } wn_u1;
4910 union /* shared to save space */
4911 {
4912 wordnode_T *next; /* next node with same hash key */
4913 wordnode_T *wnode; /* parent node that will write this node */
4914 } wn_u2;
Bram Moolenaar51485f02005-06-04 21:55:20 +00004915 wordnode_T *wn_child; /* child (next byte in word) */
4916 wordnode_T *wn_sibling; /* next sibling (alternate byte in word,
4917 always sorted) */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004918 int wn_refs; /* Nr. of references to this node. Only
4919 relevant for first node in a list of
4920 siblings, in following siblings it is
4921 always one. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004922 char_u wn_byte; /* Byte for this node. NUL for word end */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004923
4924 /* Info for when "wn_byte" is NUL.
4925 * In PREFIXTREE "wn_region" is used for the prefcondnr.
4926 * In the soundfolded word tree "wn_flags" has the MSW of the wordnr and
4927 * "wn_region" the LSW of the wordnr. */
4928 char_u wn_affixID; /* supported/required prefix ID or 0 */
4929 short_u wn_flags; /* WF_ flags */
4930 short wn_region; /* region mask */
4931
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00004932#ifdef SPELL_PRINTTREE
4933 int wn_nr; /* sequence nr for printing */
4934#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004935};
4936
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00004937#define WN_MASK 0xffff /* mask relevant bits of "wn_flags" */
4938
Bram Moolenaar51485f02005-06-04 21:55:20 +00004939#define HI2WN(hi) (wordnode_T *)((hi)->hi_key)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004940
Bram Moolenaar51485f02005-06-04 21:55:20 +00004941/*
4942 * Info used while reading the spell files.
4943 */
4944typedef struct spellinfo_S
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00004945{
Bram Moolenaar51485f02005-06-04 21:55:20 +00004946 wordnode_T *si_foldroot; /* tree with case-folded words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004947 long si_foldwcount; /* nr of words in si_foldroot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004948
Bram Moolenaar51485f02005-06-04 21:55:20 +00004949 wordnode_T *si_keeproot; /* tree with keep-case words */
Bram Moolenaar8db73182005-06-17 21:51:16 +00004950 long si_keepwcount; /* nr of words in si_keeproot */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004951
Bram Moolenaar1d73c882005-06-19 22:48:47 +00004952 wordnode_T *si_prefroot; /* tree with postponed prefixes */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004953
Bram Moolenaar4770d092006-01-12 23:22:24 +00004954 long si_sugtree; /* creating the soundfolding trie */
4955
Bram Moolenaar51485f02005-06-04 21:55:20 +00004956 sblock_T *si_blocks; /* memory blocks used */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004957 long si_blocks_cnt; /* memory blocks allocated */
4958 long si_compress_cnt; /* words to add before lowering
4959 compression limit */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004960 wordnode_T *si_first_free; /* List of nodes that have been freed during
4961 compression, linked by "wn_child" field. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00004962 long si_free_count; /* number of nodes in si_first_free */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004963#ifdef SPELL_PRINTTREE
4964 int si_wordnode_nr; /* sequence nr for nodes */
4965#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +00004966 buf_T *si_spellbuf; /* buffer used to store soundfold word table */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004967
Bram Moolenaar51485f02005-06-04 21:55:20 +00004968 int si_ascii; /* handling only ASCII words */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004969 int si_add; /* addition file */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00004970 int si_clear_chartab; /* when TRUE clear char tables */
Bram Moolenaar51485f02005-06-04 21:55:20 +00004971 int si_region; /* region mask */
4972 vimconv_T si_conv; /* for conversion to 'encoding' */
Bram Moolenaar50cde822005-06-05 21:54:54 +00004973 int si_memtot; /* runtime memory used */
Bram Moolenaarb765d632005-06-07 21:00:02 +00004974 int si_verbose; /* verbose messages */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004975 int si_msg_count; /* number of words added since last message */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004976 char_u *si_info; /* info text chars or NULL */
Bram Moolenaar3982c542005-06-08 21:56:31 +00004977 int si_region_count; /* number of regions supported (1 when there
4978 are no regions) */
Bram Moolenaara8fc7982010-09-29 18:32:52 +02004979 char_u si_region_name[17]; /* region names; used only if
Bram Moolenaar5195e452005-08-19 20:32:47 +00004980 * si_region_count > 1) */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004981
4982 garray_T si_rep; /* list of fromto_T entries from REP lines */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004983 garray_T si_repsal; /* list of fromto_T entries from REPSAL lines */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004984 garray_T si_sal; /* list of fromto_T entries from SAL lines */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00004985 char_u *si_sofofr; /* SOFOFROM text */
4986 char_u *si_sofoto; /* SOFOTO text */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004987 int si_nosugfile; /* NOSUGFILE item found */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004988 int si_nosplitsugs; /* NOSPLITSUGS item found */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004989 int si_followup; /* soundsalike: ? */
4990 int si_collapse; /* soundsalike: ? */
Bram Moolenaar4770d092006-01-12 23:22:24 +00004991 hashtab_T si_commonwords; /* hashtable for common words */
4992 time_t si_sugtime; /* timestamp for .sug file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004993 int si_rem_accents; /* soundsalike: remove accents */
4994 garray_T si_map; /* MAP info concatenated */
Bram Moolenaar6de68532005-08-24 22:08:48 +00004995 char_u *si_midword; /* MIDWORD chars or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004996 int si_compmax; /* max nr of words for compounding */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004997 int si_compminlen; /* minimal length for compounding */
Bram Moolenaar5195e452005-08-19 20:32:47 +00004998 int si_compsylmax; /* max nr of syllables for compounding */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004999 int si_compoptions; /* COMP_ flags */
5000 garray_T si_comppat; /* CHECKCOMPOUNDPATTERN items, each stored as
5001 a string */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005002 char_u *si_compflags; /* flags used for compounding */
Bram Moolenaar78622822005-08-23 21:00:13 +00005003 char_u si_nobreak; /* NOBREAK */
Bram Moolenaar5195e452005-08-19 20:32:47 +00005004 char_u *si_syllable; /* syllable string */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005005 garray_T si_prefcond; /* table with conditions for postponed
5006 * prefixes, each stored as a string */
Bram Moolenaar6de68532005-08-24 22:08:48 +00005007 int si_newprefID; /* current value for ah_newID */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005008 int si_newcompID; /* current value for compound ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005009} spellinfo_T;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005010
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005011static afffile_T *spell_read_aff __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005012static int is_aff_rule __ARGS((char_u **items, int itemcnt, char *rulename, int mincount));
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005013static void aff_process_flags __ARGS((afffile_T *affile, affentry_T *entry));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005014static int spell_info_item __ARGS((char_u *s));
Bram Moolenaar6de68532005-08-24 22:08:48 +00005015static unsigned affitem2flag __ARGS((int flagtype, char_u *item, char_u *fname, int lnum));
5016static unsigned get_affitem __ARGS((int flagtype, char_u **pp));
5017static void process_compflags __ARGS((spellinfo_T *spin, afffile_T *aff, char_u *compflags));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005018static void check_renumber __ARGS((spellinfo_T *spin));
Bram Moolenaar6de68532005-08-24 22:08:48 +00005019static int flag_in_afflist __ARGS((int flagtype, char_u *afflist, unsigned flag));
5020static void aff_check_number __ARGS((int spinval, int affval, char *name));
5021static void aff_check_string __ARGS((char_u *spinval, char_u *affval, char *name));
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005022static int str_equal __ARGS((char_u *s1, char_u *s2));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005023static void add_fromto __ARGS((spellinfo_T *spin, garray_T *gap, char_u *from, char_u *to));
5024static int sal_to_bool __ARGS((char_u *s));
Bram Moolenaar5482f332005-04-17 20:18:43 +00005025static int has_non_ascii __ARGS((char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00005026static void spell_free_aff __ARGS((afffile_T *aff));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005027static int spell_read_dic __ARGS((spellinfo_T *spin, char_u *fname, afffile_T *affile));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005028static int get_affix_flags __ARGS((afffile_T *affile, char_u *afflist));
Bram Moolenaar5195e452005-08-19 20:32:47 +00005029static int get_pfxlist __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar6de68532005-08-24 22:08:48 +00005030static void get_compflags __ARGS((afffile_T *affile, char_u *afflist, char_u *store_afflist));
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005031static 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 +00005032static int spell_read_wordfile __ARGS((spellinfo_T *spin, char_u *fname));
5033static void *getroom __ARGS((spellinfo_T *spin, size_t len, int align));
5034static char_u *getroom_save __ARGS((spellinfo_T *spin, char_u *s));
Bram Moolenaar51485f02005-06-04 21:55:20 +00005035static void free_blocks __ARGS((sblock_T *bl));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005036static wordnode_T *wordtree_alloc __ARGS((spellinfo_T *spin));
Bram Moolenaar5195e452005-08-19 20:32:47 +00005037static 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 +00005038static 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 +00005039static wordnode_T *get_wordnode __ARGS((spellinfo_T *spin));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005040static int deref_wordnode __ARGS((spellinfo_T *spin, wordnode_T *node));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005041static void free_wordnode __ARGS((spellinfo_T *spin, wordnode_T *n));
5042static void wordtree_compress __ARGS((spellinfo_T *spin, wordnode_T *root));
5043static int node_compress __ARGS((spellinfo_T *spin, wordnode_T *node, hashtab_T *ht, int *tot));
Bram Moolenaar51485f02005-06-04 21:55:20 +00005044static int node_equal __ARGS((wordnode_T *n1, wordnode_T *n2));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005045static int write_vim_spell __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar0c405862005-06-22 22:26:26 +00005046static void clear_node __ARGS((wordnode_T *node));
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00005047static int put_node __ARGS((FILE *fd, wordnode_T *node, int idx, int regionmask, int prefixtree));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005048static void spell_make_sugfile __ARGS((spellinfo_T *spin, char_u *wfname));
5049static int sug_filltree __ARGS((spellinfo_T *spin, slang_T *slang));
5050static int sug_maketable __ARGS((spellinfo_T *spin));
5051static int sug_filltable __ARGS((spellinfo_T *spin, wordnode_T *node, int startwordnr, garray_T *gap));
5052static int offset2bytes __ARGS((int nr, char_u *buf));
5053static int bytes2offset __ARGS((char_u **pp));
5054static void sug_write __ARGS((spellinfo_T *spin, char_u *fname));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005055static void mkspell __ARGS((int fcount, char_u **fnames, int ascii, int overwrite, int added_word));
Bram Moolenaar4770d092006-01-12 23:22:24 +00005056static void spell_message __ARGS((spellinfo_T *spin, char_u *str));
Bram Moolenaarb765d632005-06-07 21:00:02 +00005057static void init_spellfile __ARGS((void));
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005058
Bram Moolenaar53805d12005-08-01 07:08:33 +00005059/* In the postponed prefixes tree wn_flags is used to store the WFP_ flags,
5060 * but it must be negative to indicate the prefix tree to tree_add_word().
5061 * Use a negative number with the lower 8 bits zero. */
5062#define PFX_FLAGS -256
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005063
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005064/* flags for "condit" argument of store_aff_word() */
5065#define CONDIT_COMB 1 /* affix must combine */
5066#define CONDIT_CFIX 2 /* affix must have CIRCUMFIX flag */
5067#define CONDIT_SUF 4 /* add a suffix for matching flags */
5068#define CONDIT_AFF 8 /* word already has an affix */
5069
Bram Moolenaar5195e452005-08-19 20:32:47 +00005070/*
5071 * Tunable parameters for when the tree is compressed. See 'mkspellmem'.
5072 */
5073static long compress_start = 30000; /* memory / SBLOCKSIZE */
5074static long compress_inc = 100; /* memory / SBLOCKSIZE */
5075static long compress_added = 500000; /* word count */
5076
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00005077#ifdef SPELL_PRINTTREE
5078/*
5079 * For debugging the tree code: print the current tree in a (more or less)
5080 * readable format, so that we can see what happens when adding a word and/or
5081 * compressing the tree.
5082 * Based on code from Olaf Seibert.
5083 */
5084#define PRINTLINESIZE 1000
5085#define PRINTWIDTH 6
5086
5087#define PRINTSOME(l, depth, fmt, a1, a2) vim_snprintf(l + depth * PRINTWIDTH, \
5088 PRINTLINESIZE - PRINTWIDTH * depth, fmt, a1, a2)
5089
5090static char line1[PRINTLINESIZE];
5091static char line2[PRINTLINESIZE];
5092static char line3[PRINTLINESIZE];
5093
5094 static void
5095spell_clear_flags(wordnode_T *node)
5096{
5097 wordnode_T *np;
5098
5099 for (np = node; np != NULL; np = np->wn_sibling)
5100 {
5101 np->wn_u1.index = FALSE;
5102 spell_clear_flags(np->wn_child);
5103 }
5104}
5105
5106 static void
5107spell_print_node(wordnode_T *node, int depth)
5108{
5109 if (node->wn_u1.index)
5110 {
5111 /* Done this node before, print the reference. */
5112 PRINTSOME(line1, depth, "(%d)", node->wn_nr, 0);
5113 PRINTSOME(line2, depth, " ", 0, 0);
5114 PRINTSOME(line3, depth, " ", 0, 0);
5115 msg(line1);
5116 msg(line2);
5117 msg(line3);
5118 }
5119 else
5120 {
5121 node->wn_u1.index = TRUE;
5122
5123 if (node->wn_byte != NUL)
5124 {
5125 if (node->wn_child != NULL)
5126 PRINTSOME(line1, depth, " %c -> ", node->wn_byte, 0);
5127 else
5128 /* Cannot happen? */
5129 PRINTSOME(line1, depth, " %c ???", node->wn_byte, 0);
5130 }
5131 else
5132 PRINTSOME(line1, depth, " $ ", 0, 0);
5133
5134 PRINTSOME(line2, depth, "%d/%d ", node->wn_nr, node->wn_refs);
5135
5136 if (node->wn_sibling != NULL)
5137 PRINTSOME(line3, depth, " | ", 0, 0);
5138 else
5139 PRINTSOME(line3, depth, " ", 0, 0);
5140
5141 if (node->wn_byte == NUL)
5142 {
5143 msg(line1);
5144 msg(line2);
5145 msg(line3);
5146 }
5147
5148 /* do the children */
5149 if (node->wn_byte != NUL && node->wn_child != NULL)
5150 spell_print_node(node->wn_child, depth + 1);
5151
5152 /* do the siblings */
5153 if (node->wn_sibling != NULL)
5154 {
5155 /* get rid of all parent details except | */
5156 STRCPY(line1, line3);
5157 STRCPY(line2, line3);
5158 spell_print_node(node->wn_sibling, depth);
5159 }
5160 }
5161}
5162
5163 static void
5164spell_print_tree(wordnode_T *root)
5165{
5166 if (root != NULL)
5167 {
5168 /* Clear the "wn_u1.index" fields, used to remember what has been
5169 * done. */
5170 spell_clear_flags(root);
5171
5172 /* Recursively print the tree. */
5173 spell_print_node(root, 0);
5174 }
5175}
5176#endif /* SPELL_PRINTTREE */
5177
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005178/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005179 * Read the affix file "fname".
Bram Moolenaar3982c542005-06-08 21:56:31 +00005180 * Returns an afffile_T, NULL for complete failure.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005181 */
5182 static afffile_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005183spell_read_aff(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00005184 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005185 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005186{
5187 FILE *fd;
5188 afffile_T *aff;
5189 char_u rline[MAXLINELEN];
5190 char_u *line;
5191 char_u *pc = NULL;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005192#define MAXITEMCNT 30
Bram Moolenaar8db73182005-06-17 21:51:16 +00005193 char_u *(items[MAXITEMCNT]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005194 int itemcnt;
5195 char_u *p;
5196 int lnum = 0;
5197 affheader_T *cur_aff = NULL;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005198 int did_postpone_prefix = FALSE;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005199 int aff_todo = 0;
5200 hashtab_T *tp;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005201 char_u *low = NULL;
5202 char_u *fol = NULL;
5203 char_u *upp = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005204 int do_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00005205 int do_repsal;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005206 int do_sal;
Bram Moolenaar89d40322006-08-29 15:30:07 +00005207 int do_mapline;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005208 int found_map = FALSE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00005209 hashitem_T *hi;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005210 int l;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005211 int compminlen = 0; /* COMPOUNDMIN value */
5212 int compsylmax = 0; /* COMPOUNDSYLMAX value */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005213 int compoptions = 0; /* COMP_ flags */
5214 int compmax = 0; /* COMPOUNDWORDMAX value */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005215 char_u *compflags = NULL; /* COMPOUNDFLAG and COMPOUNDRULE
Bram Moolenaar6de68532005-08-24 22:08:48 +00005216 concatenated */
5217 char_u *midword = NULL; /* MIDWORD value */
5218 char_u *syllable = NULL; /* SYLLABLE value */
5219 char_u *sofofrom = NULL; /* SOFOFROM value */
5220 char_u *sofoto = NULL; /* SOFOTO value */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005221
Bram Moolenaar51485f02005-06-04 21:55:20 +00005222 /*
5223 * Open the file.
5224 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00005225 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005226 if (fd == NULL)
5227 {
5228 EMSG2(_(e_notopen), fname);
5229 return NULL;
5230 }
5231
Bram Moolenaar4770d092006-01-12 23:22:24 +00005232 vim_snprintf((char *)IObuff, IOSIZE, _("Reading affix file %s ..."), fname);
5233 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005234
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005235 /* Only do REP lines when not done in another .aff file already. */
5236 do_rep = spin->si_rep.ga_len == 0;
5237
Bram Moolenaar4770d092006-01-12 23:22:24 +00005238 /* Only do REPSAL lines when not done in another .aff file already. */
5239 do_repsal = spin->si_repsal.ga_len == 0;
5240
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005241 /* Only do SAL lines when not done in another .aff file already. */
5242 do_sal = spin->si_sal.ga_len == 0;
5243
5244 /* Only do MAP lines when not done in another .aff file already. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00005245 do_mapline = spin->si_map.ga_len == 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005246
Bram Moolenaar51485f02005-06-04 21:55:20 +00005247 /*
5248 * Allocate and init the afffile_T structure.
5249 */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005250 aff = (afffile_T *)getroom(spin, sizeof(afffile_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005251 if (aff == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005252 {
5253 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005254 return NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005255 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005256 hash_init(&aff->af_pref);
5257 hash_init(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005258 hash_init(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005259
5260 /*
5261 * Read all the lines in the file one by one.
5262 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005263 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005264 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005265 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005266 ++lnum;
5267
5268 /* Skip comment lines. */
5269 if (*rline == '#')
5270 continue;
5271
5272 /* Convert from "SET" to 'encoding' when needed. */
5273 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00005274#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005275 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005276 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00005277 pc = string_convert(&spin->si_conv, rline, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005278 if (pc == NULL)
5279 {
5280 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
5281 fname, lnum, rline);
5282 continue;
5283 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005284 line = pc;
5285 }
5286 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00005287#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005288 {
5289 pc = NULL;
5290 line = rline;
5291 }
5292
5293 /* Split the line up in white separated items. Put a NUL after each
5294 * item. */
5295 itemcnt = 0;
5296 for (p = line; ; )
5297 {
5298 while (*p != NUL && *p <= ' ') /* skip white space and CR/NL */
5299 ++p;
5300 if (*p == NUL)
5301 break;
Bram Moolenaar8db73182005-06-17 21:51:16 +00005302 if (itemcnt == MAXITEMCNT) /* too many items */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005303 break;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005304 items[itemcnt++] = p;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005305 /* A few items have arbitrary text argument, don't split them. */
5306 if (itemcnt == 2 && spell_info_item(items[0]))
5307 while (*p >= ' ' || *p == TAB) /* skip until CR/NL */
5308 ++p;
5309 else
5310 while (*p > ' ') /* skip until white space or CR/NL */
5311 ++p;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005312 if (*p == NUL)
5313 break;
5314 *p++ = NUL;
5315 }
5316
5317 /* Handle non-empty lines. */
5318 if (itemcnt > 0)
5319 {
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005320 if (is_aff_rule(items, itemcnt, "SET", 2) && aff->af_enc == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005321 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00005322#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00005323 /* Setup for conversion from "ENC" to 'encoding'. */
5324 aff->af_enc = enc_canonize(items[1]);
5325 if (aff->af_enc != NULL && !spin->si_ascii
5326 && convert_setup(&spin->si_conv, aff->af_enc,
5327 p_enc) == FAIL)
5328 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
5329 fname, aff->af_enc, p_enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005330 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00005331#else
5332 smsg((char_u *)_("Conversion in %s not supported"), fname);
5333#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005334 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005335 else if (is_aff_rule(items, itemcnt, "FLAG", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005336 && aff->af_flagtype == AFT_CHAR)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005337 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005338 if (STRCMP(items[1], "long") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005339 aff->af_flagtype = AFT_LONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005340 else if (STRCMP(items[1], "num") == 0)
Bram Moolenaar95529562005-08-25 21:21:38 +00005341 aff->af_flagtype = AFT_NUM;
5342 else if (STRCMP(items[1], "caplong") == 0)
5343 aff->af_flagtype = AFT_CAPLONG;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005344 else
5345 smsg((char_u *)_("Invalid value for FLAG in %s line %d: %s"),
5346 fname, lnum, items[1]);
Bram Moolenaar371baa92005-12-29 22:43:53 +00005347 if (aff->af_rare != 0
5348 || aff->af_keepcase != 0
5349 || aff->af_bad != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005350 || aff->af_needaffix != 0
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005351 || aff->af_circumfix != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005352 || aff->af_needcomp != 0
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005353 || aff->af_comproot != 0
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005354 || aff->af_nosuggest != 0
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005355 || compflags != NULL
Bram Moolenaar6de68532005-08-24 22:08:48 +00005356 || aff->af_suff.ht_used > 0
5357 || aff->af_pref.ht_used > 0)
5358 smsg((char_u *)_("FLAG after using flags in %s line %d: %s"),
5359 fname, lnum, items[1]);
5360 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005361 else if (spell_info_item(items[0]))
5362 {
5363 p = (char_u *)getroom(spin,
5364 (spin->si_info == NULL ? 0 : STRLEN(spin->si_info))
5365 + STRLEN(items[0])
5366 + STRLEN(items[1]) + 3, FALSE);
5367 if (p != NULL)
5368 {
5369 if (spin->si_info != NULL)
5370 {
5371 STRCPY(p, spin->si_info);
5372 STRCAT(p, "\n");
5373 }
5374 STRCAT(p, items[0]);
5375 STRCAT(p, " ");
5376 STRCAT(p, items[1]);
5377 spin->si_info = p;
5378 }
5379 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005380 else if (is_aff_rule(items, itemcnt, "MIDWORD", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005381 && midword == NULL)
5382 {
5383 midword = getroom_save(spin, items[1]);
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005384 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005385 else if (is_aff_rule(items, itemcnt, "TRY", 2))
Bram Moolenaar51485f02005-06-04 21:55:20 +00005386 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005387 /* ignored, we look in the tree for what chars may appear */
Bram Moolenaar51485f02005-06-04 21:55:20 +00005388 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005389 /* TODO: remove "RAR" later */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005390 else if ((is_aff_rule(items, itemcnt, "RAR", 2)
5391 || is_aff_rule(items, itemcnt, "RARE", 2))
5392 && aff->af_rare == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005393 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005394 aff->af_rare = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005395 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005396 }
Bram Moolenaar371baa92005-12-29 22:43:53 +00005397 /* TODO: remove "KEP" later */
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005398 else if ((is_aff_rule(items, itemcnt, "KEP", 2)
5399 || is_aff_rule(items, itemcnt, "KEEPCASE", 2))
Bram Moolenaar371baa92005-12-29 22:43:53 +00005400 && aff->af_keepcase == 0)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005401 {
Bram Moolenaar371baa92005-12-29 22:43:53 +00005402 aff->af_keepcase = affitem2flag(aff->af_flagtype, items[1],
Bram Moolenaar6de68532005-08-24 22:08:48 +00005403 fname, lnum);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00005404 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005405 else if ((is_aff_rule(items, itemcnt, "BAD", 2)
5406 || is_aff_rule(items, itemcnt, "FORBIDDENWORD", 2))
5407 && aff->af_bad == 0)
Bram Moolenaar0c405862005-06-22 22:26:26 +00005408 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005409 aff->af_bad = affitem2flag(aff->af_flagtype, items[1],
5410 fname, lnum);
Bram Moolenaar0c405862005-06-22 22:26:26 +00005411 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005412 else if (is_aff_rule(items, itemcnt, "NEEDAFFIX", 2)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005413 && aff->af_needaffix == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005414 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005415 aff->af_needaffix = affitem2flag(aff->af_flagtype, items[1],
5416 fname, lnum);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005417 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005418 else if (is_aff_rule(items, itemcnt, "CIRCUMFIX", 2)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005419 && aff->af_circumfix == 0)
5420 {
5421 aff->af_circumfix = affitem2flag(aff->af_flagtype, items[1],
5422 fname, lnum);
5423 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005424 else if (is_aff_rule(items, itemcnt, "NOSUGGEST", 2)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005425 && aff->af_nosuggest == 0)
5426 {
5427 aff->af_nosuggest = affitem2flag(aff->af_flagtype, items[1],
5428 fname, lnum);
5429 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005430 else if ((is_aff_rule(items, itemcnt, "NEEDCOMPOUND", 2)
5431 || is_aff_rule(items, itemcnt, "ONLYINCOMPOUND", 2))
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005432 && aff->af_needcomp == 0)
5433 {
5434 aff->af_needcomp = affitem2flag(aff->af_flagtype, items[1],
5435 fname, lnum);
5436 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005437 else if (is_aff_rule(items, itemcnt, "COMPOUNDROOT", 2)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005438 && aff->af_comproot == 0)
5439 {
5440 aff->af_comproot = affitem2flag(aff->af_flagtype, items[1],
5441 fname, lnum);
5442 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005443 else if (is_aff_rule(items, itemcnt, "COMPOUNDFORBIDFLAG", 2)
5444 && aff->af_compforbid == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005445 {
5446 aff->af_compforbid = affitem2flag(aff->af_flagtype, items[1],
5447 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005448 if (aff->af_pref.ht_used > 0)
5449 smsg((char_u *)_("Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in %s line %d"),
5450 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005451 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005452 else if (is_aff_rule(items, itemcnt, "COMPOUNDPERMITFLAG", 2)
5453 && aff->af_comppermit == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005454 {
5455 aff->af_comppermit = affitem2flag(aff->af_flagtype, items[1],
5456 fname, lnum);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005457 if (aff->af_pref.ht_used > 0)
5458 smsg((char_u *)_("Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in %s line %d"),
5459 fname, lnum);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005460 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005461 else if (is_aff_rule(items, itemcnt, "COMPOUNDFLAG", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005462 && compflags == NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005463 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005464 /* Turn flag "c" into COMPOUNDRULE compatible string "c+",
Bram Moolenaar6de68532005-08-24 22:08:48 +00005465 * "Na" into "Na+", "1234" into "1234+". */
5466 p = getroom(spin, STRLEN(items[1]) + 2, FALSE);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005467 if (p != NULL)
5468 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005469 STRCPY(p, items[1]);
5470 STRCAT(p, "+");
5471 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005472 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00005473 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005474 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULES", 2))
5475 {
5476 /* We don't use the count, but do check that it's a number and
5477 * not COMPOUNDRULE mistyped. */
5478 if (atoi((char *)items[1]) == 0)
5479 smsg((char_u *)_("Wrong COMPOUNDRULES value in %s line %d: %s"),
5480 fname, lnum, items[1]);
5481 }
5482 else if (is_aff_rule(items, itemcnt, "COMPOUNDRULE", 2))
Bram Moolenaar5195e452005-08-19 20:32:47 +00005483 {
5484 /* Concatenate this string to previously defined ones, using a
5485 * slash to separate them. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005486 l = (int)STRLEN(items[1]) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00005487 if (compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005488 l += (int)STRLEN(compflags) + 1;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005489 p = getroom(spin, l, FALSE);
5490 if (p != NULL)
5491 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005492 if (compflags != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005493 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005494 STRCPY(p, compflags);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005495 STRCAT(p, "/");
5496 }
5497 STRCAT(p, items[1]);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005498 compflags = p;
Bram Moolenaar5195e452005-08-19 20:32:47 +00005499 }
5500 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005501 else if (is_aff_rule(items, itemcnt, "COMPOUNDWORDMAX", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005502 && compmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005503 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005504 compmax = atoi((char *)items[1]);
5505 if (compmax == 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005506 smsg((char_u *)_("Wrong COMPOUNDWORDMAX value in %s line %d: %s"),
Bram Moolenaar5195e452005-08-19 20:32:47 +00005507 fname, lnum, items[1]);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005508 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005509 else if (is_aff_rule(items, itemcnt, "COMPOUNDMIN", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005510 && compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005511 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005512 compminlen = atoi((char *)items[1]);
5513 if (compminlen == 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005514 smsg((char_u *)_("Wrong COMPOUNDMIN value in %s line %d: %s"),
5515 fname, lnum, items[1]);
5516 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005517 else if (is_aff_rule(items, itemcnt, "COMPOUNDSYLMAX", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005518 && compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005519 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005520 compsylmax = atoi((char *)items[1]);
5521 if (compsylmax == 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005522 smsg((char_u *)_("Wrong COMPOUNDSYLMAX value in %s line %d: %s"),
5523 fname, lnum, items[1]);
5524 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005525 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDDUP", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005526 {
5527 compoptions |= COMP_CHECKDUP;
5528 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005529 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDREP", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005530 {
5531 compoptions |= COMP_CHECKREP;
5532 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005533 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDCASE", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005534 {
5535 compoptions |= COMP_CHECKCASE;
5536 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005537 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDTRIPLE", 1))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005538 {
5539 compoptions |= COMP_CHECKTRIPLE;
5540 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005541 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 2))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005542 {
5543 if (atoi((char *)items[1]) == 0)
5544 smsg((char_u *)_("Wrong CHECKCOMPOUNDPATTERN value in %s line %d: %s"),
5545 fname, lnum, items[1]);
5546 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005547 else if (is_aff_rule(items, itemcnt, "CHECKCOMPOUNDPATTERN", 3))
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005548 {
5549 garray_T *gap = &spin->si_comppat;
5550 int i;
5551
5552 /* Only add the couple if it isn't already there. */
5553 for (i = 0; i < gap->ga_len - 1; i += 2)
5554 if (STRCMP(((char_u **)(gap->ga_data))[i], items[1]) == 0
5555 && STRCMP(((char_u **)(gap->ga_data))[i + 1],
5556 items[2]) == 0)
5557 break;
5558 if (i >= gap->ga_len && ga_grow(gap, 2) == OK)
5559 {
5560 ((char_u **)(gap->ga_data))[gap->ga_len++]
5561 = getroom_save(spin, items[1]);
5562 ((char_u **)(gap->ga_data))[gap->ga_len++]
5563 = getroom_save(spin, items[2]);
5564 }
5565 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005566 else if (is_aff_rule(items, itemcnt, "SYLLABLE", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005567 && syllable == NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00005568 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005569 syllable = getroom_save(spin, items[1]);
Bram Moolenaar5195e452005-08-19 20:32:47 +00005570 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005571 else if (is_aff_rule(items, itemcnt, "NOBREAK", 1))
Bram Moolenaar78622822005-08-23 21:00:13 +00005572 {
5573 spin->si_nobreak = TRUE;
5574 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005575 else if (is_aff_rule(items, itemcnt, "NOSPLITSUGS", 1))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005576 {
5577 spin->si_nosplitsugs = TRUE;
5578 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005579 else if (is_aff_rule(items, itemcnt, "NOSUGFILE", 1))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005580 {
5581 spin->si_nosugfile = TRUE;
5582 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005583 else if (is_aff_rule(items, itemcnt, "PFXPOSTPONE", 1))
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005584 {
5585 aff->af_pfxpostpone = TRUE;
5586 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005587 else if ((STRCMP(items[0], "PFX") == 0
5588 || STRCMP(items[0], "SFX") == 0)
5589 && aff_todo == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005590 && itemcnt >= 4)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005591 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005592 int lasti = 4;
5593 char_u key[AH_KEY_LEN];
5594
5595 if (*items[0] == 'P')
5596 tp = &aff->af_pref;
5597 else
5598 tp = &aff->af_suff;
5599
5600 /* Myspell allows the same affix name to be used multiple
5601 * times. The affix files that do this have an undocumented
5602 * "S" flag on all but the last block, thus we check for that
5603 * and store it in ah_follows. */
5604 vim_strncpy(key, items[1], AH_KEY_LEN - 1);
5605 hi = hash_find(tp, key);
5606 if (!HASHITEM_EMPTY(hi))
5607 {
5608 cur_aff = HI2AH(hi);
5609 if (cur_aff->ah_combine != (*items[2] == 'Y'))
5610 smsg((char_u *)_("Different combining flag in continued affix block in %s line %d: %s"),
5611 fname, lnum, items[1]);
5612 if (!cur_aff->ah_follows)
5613 smsg((char_u *)_("Duplicate affix in %s line %d: %s"),
5614 fname, lnum, items[1]);
5615 }
5616 else
5617 {
5618 /* New affix letter. */
5619 cur_aff = (affheader_T *)getroom(spin,
5620 sizeof(affheader_T), TRUE);
5621 if (cur_aff == NULL)
5622 break;
5623 cur_aff->ah_flag = affitem2flag(aff->af_flagtype, items[1],
5624 fname, lnum);
5625 if (cur_aff->ah_flag == 0 || STRLEN(items[1]) >= AH_KEY_LEN)
5626 break;
5627 if (cur_aff->ah_flag == aff->af_bad
Bram Moolenaar371baa92005-12-29 22:43:53 +00005628 || cur_aff->ah_flag == aff->af_rare
5629 || cur_aff->ah_flag == aff->af_keepcase
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005630 || cur_aff->ah_flag == aff->af_needaffix
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005631 || cur_aff->ah_flag == aff->af_circumfix
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005632 || cur_aff->ah_flag == aff->af_nosuggest
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005633 || cur_aff->ah_flag == aff->af_needcomp
5634 || cur_aff->ah_flag == aff->af_comproot)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005635 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 +00005636 fname, lnum, items[1]);
5637 STRCPY(cur_aff->ah_key, items[1]);
5638 hash_add(tp, cur_aff->ah_key);
5639
5640 cur_aff->ah_combine = (*items[2] == 'Y');
5641 }
5642
5643 /* Check for the "S" flag, which apparently means that another
5644 * block with the same affix name is following. */
5645 if (itemcnt > lasti && STRCMP(items[lasti], "S") == 0)
5646 {
5647 ++lasti;
5648 cur_aff->ah_follows = TRUE;
5649 }
5650 else
5651 cur_aff->ah_follows = FALSE;
5652
Bram Moolenaar8db73182005-06-17 21:51:16 +00005653 /* Myspell allows extra text after the item, but that might
5654 * mean mistakes go unnoticed. Require a comment-starter. */
Bram Moolenaar95529562005-08-25 21:21:38 +00005655 if (itemcnt > lasti && *items[lasti] != '#')
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005656 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005657
Bram Moolenaar95529562005-08-25 21:21:38 +00005658 if (STRCMP(items[2], "Y") != 0 && STRCMP(items[2], "N") != 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005659 smsg((char_u *)_("Expected Y or N in %s line %d: %s"),
5660 fname, lnum, items[2]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005661
Bram Moolenaar95529562005-08-25 21:21:38 +00005662 if (*items[0] == 'P' && aff->af_pfxpostpone)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005663 {
Bram Moolenaar95529562005-08-25 21:21:38 +00005664 if (cur_aff->ah_newID == 0)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005665 {
5666 /* Use a new number in the .spl file later, to be able
5667 * to handle multiple .aff files. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00005668 check_renumber(spin);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005669 cur_aff->ah_newID = ++spin->si_newprefID;
5670
5671 /* We only really use ah_newID if the prefix is
5672 * postponed. We know that only after handling all
5673 * the items. */
5674 did_postpone_prefix = FALSE;
5675 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005676 else
5677 /* Did use the ID in a previous block. */
5678 did_postpone_prefix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005679 }
Bram Moolenaar95529562005-08-25 21:21:38 +00005680
Bram Moolenaar51485f02005-06-04 21:55:20 +00005681 aff_todo = atoi((char *)items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005682 }
5683 else if ((STRCMP(items[0], "PFX") == 0
5684 || STRCMP(items[0], "SFX") == 0)
5685 && aff_todo > 0
5686 && STRCMP(cur_aff->ah_key, items[1]) == 0
Bram Moolenaar8db73182005-06-17 21:51:16 +00005687 && itemcnt >= 5)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005688 {
5689 affentry_T *aff_entry;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005690 int upper = FALSE;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00005691 int lasti = 5;
5692
Bram Moolenaar8db73182005-06-17 21:51:16 +00005693 /* Myspell allows extra text after the item, but that might
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005694 * mean mistakes go unnoticed. Require a comment-starter.
5695 * Hunspell uses a "-" item. */
5696 if (itemcnt > lasti && *items[lasti] != '#'
5697 && (STRCMP(items[lasti], "-") != 0
5698 || itemcnt != lasti + 1))
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005699 smsg((char_u *)_(e_afftrailing), fname, lnum, items[lasti]);
Bram Moolenaar8db73182005-06-17 21:51:16 +00005700
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005701 /* New item for an affix letter. */
5702 --aff_todo;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005703 aff_entry = (affentry_T *)getroom(spin,
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00005704 sizeof(affentry_T), TRUE);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005705 if (aff_entry == NULL)
5706 break;
Bram Moolenaar5482f332005-04-17 20:18:43 +00005707
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005708 if (STRCMP(items[2], "0") != 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005709 aff_entry->ae_chop = getroom_save(spin, items[2]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005710 if (STRCMP(items[3], "0") != 0)
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005711 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005712 aff_entry->ae_add = getroom_save(spin, items[3]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005713
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005714 /* Recognize flags on the affix: abcd/XYZ */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005715 aff_entry->ae_flags = vim_strchr(aff_entry->ae_add, '/');
5716 if (aff_entry->ae_flags != NULL)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005717 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005718 *aff_entry->ae_flags++ = NUL;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005719 aff_process_flags(aff, aff_entry);
5720 }
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005721 }
5722
Bram Moolenaar51485f02005-06-04 21:55:20 +00005723 /* Don't use an affix entry with non-ASCII characters when
5724 * "spin->si_ascii" is TRUE. */
5725 if (!spin->si_ascii || !(has_non_ascii(aff_entry->ae_chop)
Bram Moolenaar5482f332005-04-17 20:18:43 +00005726 || has_non_ascii(aff_entry->ae_add)))
5727 {
Bram Moolenaar5482f332005-04-17 20:18:43 +00005728 aff_entry->ae_next = cur_aff->ah_first;
5729 cur_aff->ah_first = aff_entry;
Bram Moolenaar51485f02005-06-04 21:55:20 +00005730
5731 if (STRCMP(items[4], ".") != 0)
5732 {
5733 char_u buf[MAXLINELEN];
5734
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005735 aff_entry->ae_cond = getroom_save(spin, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005736 if (*items[0] == 'P')
5737 sprintf((char *)buf, "^%s", items[4]);
5738 else
5739 sprintf((char *)buf, "%s$", items[4]);
5740 aff_entry->ae_prog = vim_regcomp(buf,
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005741 RE_MAGIC + RE_STRING + RE_STRICT);
5742 if (aff_entry->ae_prog == NULL)
5743 smsg((char_u *)_("Broken condition in %s line %d: %s"),
5744 fname, lnum, items[4]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00005745 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005746
5747 /* For postponed prefixes we need an entry in si_prefcond
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005748 * for the condition. Use an existing one if possible.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005749 * Can't be done for an affix with flags, ignoring
5750 * COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG. */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005751 if (*items[0] == 'P' && aff->af_pfxpostpone
5752 && aff_entry->ae_flags == NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005753 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005754 /* When the chop string is one lower-case letter and
5755 * the add string ends in the upper-case letter we set
5756 * the "upper" flag, clear "ae_chop" and remove the
5757 * letters from "ae_add". The condition must either
5758 * be empty or start with the same letter. */
5759 if (aff_entry->ae_chop != NULL
5760 && aff_entry->ae_add != NULL
5761#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005762 && aff_entry->ae_chop[(*mb_ptr2len)(
Bram Moolenaar53805d12005-08-01 07:08:33 +00005763 aff_entry->ae_chop)] == NUL
5764#else
5765 && aff_entry->ae_chop[1] == NUL
5766#endif
5767 )
5768 {
5769 int c, c_up;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005770
Bram Moolenaar53805d12005-08-01 07:08:33 +00005771 c = PTR2CHAR(aff_entry->ae_chop);
5772 c_up = SPELL_TOUPPER(c);
5773 if (c_up != c
5774 && (aff_entry->ae_cond == NULL
5775 || PTR2CHAR(aff_entry->ae_cond) == c))
5776 {
5777 p = aff_entry->ae_add
5778 + STRLEN(aff_entry->ae_add);
5779 mb_ptr_back(aff_entry->ae_add, p);
5780 if (PTR2CHAR(p) == c_up)
5781 {
5782 upper = TRUE;
5783 aff_entry->ae_chop = NULL;
5784 *p = NUL;
5785
5786 /* The condition is matched with the
5787 * actual word, thus must check for the
5788 * upper-case letter. */
5789 if (aff_entry->ae_cond != NULL)
5790 {
5791 char_u buf[MAXLINELEN];
5792#ifdef FEAT_MBYTE
5793 if (has_mbyte)
5794 {
5795 onecap_copy(items[4], buf, TRUE);
5796 aff_entry->ae_cond = getroom_save(
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005797 spin, buf);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005798 }
5799 else
5800#endif
5801 *aff_entry->ae_cond = c_up;
5802 if (aff_entry->ae_cond != NULL)
5803 {
5804 sprintf((char *)buf, "^%s",
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005805 aff_entry->ae_cond);
Bram Moolenaar53805d12005-08-01 07:08:33 +00005806 vim_free(aff_entry->ae_prog);
5807 aff_entry->ae_prog = vim_regcomp(
5808 buf, RE_MAGIC + RE_STRING);
5809 }
5810 }
5811 }
5812 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005813 }
5814
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005815 if (aff_entry->ae_chop == NULL
5816 && aff_entry->ae_flags == NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005817 {
Bram Moolenaar53805d12005-08-01 07:08:33 +00005818 int idx;
5819 char_u **pp;
5820 int n;
5821
Bram Moolenaar6de68532005-08-24 22:08:48 +00005822 /* Find a previously used condition. */
Bram Moolenaar53805d12005-08-01 07:08:33 +00005823 for (idx = spin->si_prefcond.ga_len - 1; idx >= 0;
5824 --idx)
5825 {
5826 p = ((char_u **)spin->si_prefcond.ga_data)[idx];
5827 if (str_equal(p, aff_entry->ae_cond))
5828 break;
5829 }
5830 if (idx < 0 && ga_grow(&spin->si_prefcond, 1) == OK)
5831 {
5832 /* Not found, add a new condition. */
5833 idx = spin->si_prefcond.ga_len++;
5834 pp = ((char_u **)spin->si_prefcond.ga_data)
5835 + idx;
5836 if (aff_entry->ae_cond == NULL)
5837 *pp = NULL;
5838 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005839 *pp = getroom_save(spin,
Bram Moolenaar53805d12005-08-01 07:08:33 +00005840 aff_entry->ae_cond);
5841 }
5842
5843 /* Add the prefix to the prefix tree. */
5844 if (aff_entry->ae_add == NULL)
5845 p = (char_u *)"";
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00005846 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00005847 p = aff_entry->ae_add;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00005848
Bram Moolenaar53805d12005-08-01 07:08:33 +00005849 /* PFX_FLAGS is a negative number, so that
5850 * tree_add_word() knows this is the prefix tree. */
5851 n = PFX_FLAGS;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005852 if (!cur_aff->ah_combine)
5853 n |= WFP_NC;
5854 if (upper)
5855 n |= WFP_UP;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00005856 if (aff_entry->ae_comppermit)
5857 n |= WFP_COMPPERMIT;
5858 if (aff_entry->ae_compforbid)
5859 n |= WFP_COMPFORBID;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005860 tree_add_word(spin, p, spin->si_prefroot, n,
5861 idx, cur_aff->ah_newID);
Bram Moolenaar6de68532005-08-24 22:08:48 +00005862 did_postpone_prefix = TRUE;
5863 }
5864
5865 /* Didn't actually use ah_newID, backup si_newprefID. */
5866 if (aff_todo == 0 && !did_postpone_prefix)
5867 {
5868 --spin->si_newprefID;
5869 cur_aff->ah_newID = 0;
Bram Moolenaar53805d12005-08-01 07:08:33 +00005870 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00005871 }
Bram Moolenaar5482f332005-04-17 20:18:43 +00005872 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005873 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005874 else if (is_aff_rule(items, itemcnt, "FOL", 2) && fol == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005875 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005876 fol = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005877 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005878 else if (is_aff_rule(items, itemcnt, "LOW", 2) && low == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005879 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005880 low = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005881 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005882 else if (is_aff_rule(items, itemcnt, "UPP", 2) && upp == NULL)
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005883 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005884 upp = vim_strsave(items[1]);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00005885 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005886 else if (is_aff_rule(items, itemcnt, "REP", 2)
5887 || is_aff_rule(items, itemcnt, "REPSAL", 2))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005888 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005889 /* Ignore REP/REPSAL count */;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005890 if (!isdigit(*items[1]))
Bram Moolenaar4770d092006-01-12 23:22:24 +00005891 smsg((char_u *)_("Expected REP(SAL) count in %s line %d"),
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005892 fname, lnum);
5893 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005894 else if ((STRCMP(items[0], "REP") == 0
5895 || STRCMP(items[0], "REPSAL") == 0)
5896 && itemcnt >= 3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005897 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00005898 /* REP/REPSAL item */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005899 /* Myspell ignores extra arguments, we require it starts with
5900 * # to detect mistakes. */
5901 if (itemcnt > 3 && items[3][0] != '#')
5902 smsg((char_u *)_(e_afftrailing), fname, lnum, items[3]);
Bram Moolenaar4770d092006-01-12 23:22:24 +00005903 if (items[0][3] == 'S' ? do_repsal : do_rep)
Bram Moolenaar1e015462005-09-25 22:16:38 +00005904 {
5905 /* Replace underscore with space (can't include a space
5906 * directly). */
5907 for (p = items[1]; *p != NUL; mb_ptr_adv(p))
5908 if (*p == '_')
5909 *p = ' ';
5910 for (p = items[2]; *p != NUL; mb_ptr_adv(p))
5911 if (*p == '_')
5912 *p = ' ';
Bram Moolenaar4770d092006-01-12 23:22:24 +00005913 add_fromto(spin, items[0][3] == 'S'
5914 ? &spin->si_repsal
5915 : &spin->si_rep, items[1], items[2]);
Bram Moolenaar1e015462005-09-25 22:16:38 +00005916 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005917 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005918 else if (is_aff_rule(items, itemcnt, "MAP", 2))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005919 {
5920 /* MAP item or count */
5921 if (!found_map)
5922 {
5923 /* First line contains the count. */
5924 found_map = TRUE;
5925 if (!isdigit(*items[1]))
5926 smsg((char_u *)_("Expected MAP count in %s line %d"),
5927 fname, lnum);
5928 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00005929 else if (do_mapline)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005930 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00005931 int c;
5932
5933 /* Check that every character appears only once. */
5934 for (p = items[1]; *p != NUL; )
5935 {
5936#ifdef FEAT_MBYTE
5937 c = mb_ptr2char_adv(&p);
5938#else
5939 c = *p++;
5940#endif
5941 if ((spin->si_map.ga_len > 0
5942 && vim_strchr(spin->si_map.ga_data, c)
5943 != NULL)
5944 || vim_strchr(p, c) != NULL)
5945 smsg((char_u *)_("Duplicate character in MAP in %s line %d"),
5946 fname, lnum);
5947 }
5948
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005949 /* We simply concatenate all the MAP strings, separated by
5950 * slashes. */
5951 ga_concat(&spin->si_map, items[1]);
5952 ga_append(&spin->si_map, '/');
5953 }
5954 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005955 /* Accept "SAL from to" and "SAL from to #comment". */
5956 else if (is_aff_rule(items, itemcnt, "SAL", 3))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005957 {
5958 if (do_sal)
5959 {
5960 /* SAL item (sounds-a-like)
5961 * Either one of the known keys or a from-to pair. */
5962 if (STRCMP(items[1], "followup") == 0)
5963 spin->si_followup = sal_to_bool(items[2]);
5964 else if (STRCMP(items[1], "collapse_result") == 0)
5965 spin->si_collapse = sal_to_bool(items[2]);
5966 else if (STRCMP(items[1], "remove_accents") == 0)
5967 spin->si_rem_accents = sal_to_bool(items[2]);
5968 else
5969 /* when "to" is "_" it means empty */
5970 add_fromto(spin, &spin->si_sal, items[1],
5971 STRCMP(items[2], "_") == 0 ? (char_u *)""
5972 : items[2]);
5973 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005974 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005975 else if (is_aff_rule(items, itemcnt, "SOFOFROM", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005976 && sofofrom == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005977 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005978 sofofrom = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005979 }
Bram Moolenaar9f94b052008-11-30 20:12:46 +00005980 else if (is_aff_rule(items, itemcnt, "SOFOTO", 2)
Bram Moolenaar6de68532005-08-24 22:08:48 +00005981 && sofoto == NULL)
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005982 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00005983 sofoto = getroom_save(spin, items[1]);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00005984 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00005985 else if (STRCMP(items[0], "COMMON") == 0)
5986 {
5987 int i;
5988
5989 for (i = 1; i < itemcnt; ++i)
5990 {
5991 if (HASHITEM_EMPTY(hash_find(&spin->si_commonwords,
5992 items[i])))
5993 {
5994 p = vim_strsave(items[i]);
5995 if (p == NULL)
5996 break;
5997 hash_add(&spin->si_commonwords, p);
5998 }
5999 }
6000 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006001 else
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006002 smsg((char_u *)_("Unrecognized or duplicate item in %s line %d: %s"),
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006003 fname, lnum, items[0]);
6004 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006005 }
6006
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006007 if (fol != NULL || low != NULL || upp != NULL)
6008 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006009 if (spin->si_clear_chartab)
6010 {
6011 /* Clear the char type tables, don't want to use any of the
6012 * currently used spell properties. */
6013 init_spell_chartab();
6014 spin->si_clear_chartab = FALSE;
6015 }
6016
Bram Moolenaar3982c542005-06-08 21:56:31 +00006017 /*
6018 * Don't write a word table for an ASCII file, so that we don't check
6019 * for conflicts with a word table that matches 'encoding'.
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006020 * Don't write one for utf-8 either, we use utf_*() and
Bram Moolenaar3982c542005-06-08 21:56:31 +00006021 * mb_get_class(), the list of chars in the file will be incomplete.
6022 */
6023 if (!spin->si_ascii
6024#ifdef FEAT_MBYTE
6025 && !enc_utf8
6026#endif
6027 )
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006028 {
6029 if (fol == NULL || low == NULL || upp == NULL)
6030 smsg((char_u *)_("Missing FOL/LOW/UPP line in %s"), fname);
6031 else
Bram Moolenaar3982c542005-06-08 21:56:31 +00006032 (void)set_spell_chartab(fol, low, upp);
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00006033 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006034
6035 vim_free(fol);
6036 vim_free(low);
6037 vim_free(upp);
6038 }
6039
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006040 /* Use compound specifications of the .aff file for the spell info. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006041 if (compmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006042 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006043 aff_check_number(spin->si_compmax, compmax, "COMPOUNDWORDMAX");
Bram Moolenaar6de68532005-08-24 22:08:48 +00006044 spin->si_compmax = compmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006045 }
6046
Bram Moolenaar6de68532005-08-24 22:08:48 +00006047 if (compminlen != 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006048 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006049 aff_check_number(spin->si_compminlen, compminlen, "COMPOUNDMIN");
6050 spin->si_compminlen = compminlen;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006051 }
6052
Bram Moolenaar6de68532005-08-24 22:08:48 +00006053 if (compsylmax != 0)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006054 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006055 if (syllable == NULL)
6056 smsg((char_u *)_("COMPOUNDSYLMAX used without SYLLABLE"));
6057 aff_check_number(spin->si_compsylmax, compsylmax, "COMPOUNDSYLMAX");
6058 spin->si_compsylmax = compsylmax;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006059 }
6060
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006061 if (compoptions != 0)
6062 {
6063 aff_check_number(spin->si_compoptions, compoptions, "COMPOUND options");
6064 spin->si_compoptions |= compoptions;
6065 }
6066
Bram Moolenaar6de68532005-08-24 22:08:48 +00006067 if (compflags != NULL)
6068 process_compflags(spin, aff, compflags);
6069
6070 /* Check that we didn't use too many renumbered flags. */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006071 if (spin->si_newcompID < spin->si_newprefID)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006072 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006073 if (spin->si_newcompID == 127 || spin->si_newcompID == 255)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006074 MSG(_("Too many postponed prefixes"));
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006075 else if (spin->si_newprefID == 0 || spin->si_newprefID == 127)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006076 MSG(_("Too many compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006077 else
Bram Moolenaar6949d1d2008-08-25 02:14:05 +00006078 MSG(_("Too many postponed prefixes and/or compound flags"));
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006079 }
6080
Bram Moolenaar6de68532005-08-24 22:08:48 +00006081 if (syllable != NULL)
Bram Moolenaar5195e452005-08-19 20:32:47 +00006082 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006083 aff_check_string(spin->si_syllable, syllable, "SYLLABLE");
6084 spin->si_syllable = syllable;
6085 }
6086
6087 if (sofofrom != NULL || sofoto != NULL)
6088 {
6089 if (sofofrom == NULL || sofoto == NULL)
6090 smsg((char_u *)_("Missing SOFO%s line in %s"),
6091 sofofrom == NULL ? "FROM" : "TO", fname);
6092 else if (spin->si_sal.ga_len > 0)
6093 smsg((char_u *)_("Both SAL and SOFO lines in %s"), fname);
Bram Moolenaar5195e452005-08-19 20:32:47 +00006094 else
Bram Moolenaar6de68532005-08-24 22:08:48 +00006095 {
6096 aff_check_string(spin->si_sofofr, sofofrom, "SOFOFROM");
6097 aff_check_string(spin->si_sofoto, sofoto, "SOFOTO");
6098 spin->si_sofofr = sofofrom;
6099 spin->si_sofoto = sofoto;
6100 }
6101 }
6102
6103 if (midword != NULL)
6104 {
6105 aff_check_string(spin->si_midword, midword, "MIDWORD");
6106 spin->si_midword = midword;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006107 }
6108
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006109 vim_free(pc);
6110 fclose(fd);
6111 return aff;
6112}
6113
6114/*
Bram Moolenaar9f94b052008-11-30 20:12:46 +00006115 * Return TRUE when items[0] equals "rulename", there are "mincount" items or
6116 * a comment is following after item "mincount".
6117 */
6118 static int
6119is_aff_rule(items, itemcnt, rulename, mincount)
6120 char_u **items;
6121 int itemcnt;
6122 char *rulename;
6123 int mincount;
6124{
6125 return (STRCMP(items[0], rulename) == 0
6126 && (itemcnt == mincount
6127 || (itemcnt > mincount && items[mincount][0] == '#')));
6128}
6129
6130/*
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006131 * For affix "entry" move COMPOUNDFORBIDFLAG and COMPOUNDPERMITFLAG from
6132 * ae_flags to ae_comppermit and ae_compforbid.
6133 */
6134 static void
6135aff_process_flags(affile, entry)
6136 afffile_T *affile;
6137 affentry_T *entry;
6138{
6139 char_u *p;
6140 char_u *prevp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006141 unsigned flag;
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006142
6143 if (entry->ae_flags != NULL
6144 && (affile->af_compforbid != 0 || affile->af_comppermit != 0))
6145 {
6146 for (p = entry->ae_flags; *p != NUL; )
6147 {
6148 prevp = p;
6149 flag = get_affitem(affile->af_flagtype, &p);
6150 if (flag == affile->af_comppermit || flag == affile->af_compforbid)
6151 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00006152 STRMOVE(prevp, p);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006153 p = prevp;
6154 if (flag == affile->af_comppermit)
6155 entry->ae_comppermit = TRUE;
6156 else
6157 entry->ae_compforbid = TRUE;
6158 }
6159 if (affile->af_flagtype == AFT_NUM && *p == ',')
6160 ++p;
6161 }
6162 if (*entry->ae_flags == NUL)
6163 entry->ae_flags = NULL; /* nothing left */
6164 }
6165}
6166
6167/*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006168 * Return TRUE if "s" is the name of an info item in the affix file.
6169 */
6170 static int
6171spell_info_item(s)
6172 char_u *s;
6173{
6174 return STRCMP(s, "NAME") == 0
6175 || STRCMP(s, "HOME") == 0
6176 || STRCMP(s, "VERSION") == 0
6177 || STRCMP(s, "AUTHOR") == 0
6178 || STRCMP(s, "EMAIL") == 0
6179 || STRCMP(s, "COPYRIGHT") == 0;
6180}
6181
6182/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006183 * Turn an affix flag name into a number, according to the FLAG type.
6184 * returns zero for failure.
6185 */
6186 static unsigned
6187affitem2flag(flagtype, item, fname, lnum)
6188 int flagtype;
6189 char_u *item;
6190 char_u *fname;
6191 int lnum;
6192{
6193 unsigned res;
6194 char_u *p = item;
6195
6196 res = get_affitem(flagtype, &p);
6197 if (res == 0)
6198 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006199 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006200 smsg((char_u *)_("Flag is not a number in %s line %d: %s"),
6201 fname, lnum, item);
6202 else
6203 smsg((char_u *)_("Illegal flag in %s line %d: %s"),
6204 fname, lnum, item);
6205 }
6206 if (*p != NUL)
6207 {
6208 smsg((char_u *)_(e_affname), fname, lnum, item);
6209 return 0;
6210 }
6211
6212 return res;
6213}
6214
6215/*
6216 * Get one affix name from "*pp" and advance the pointer.
6217 * Returns zero for an error, still advances the pointer then.
6218 */
6219 static unsigned
6220get_affitem(flagtype, pp)
6221 int flagtype;
6222 char_u **pp;
6223{
6224 int res;
6225
Bram Moolenaar95529562005-08-25 21:21:38 +00006226 if (flagtype == AFT_NUM)
Bram Moolenaar6de68532005-08-24 22:08:48 +00006227 {
6228 if (!VIM_ISDIGIT(**pp))
6229 {
Bram Moolenaar95529562005-08-25 21:21:38 +00006230 ++*pp; /* always advance, avoid getting stuck */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006231 return 0;
6232 }
6233 res = getdigits(pp);
6234 }
6235 else
6236 {
6237#ifdef FEAT_MBYTE
6238 res = mb_ptr2char_adv(pp);
6239#else
6240 res = *(*pp)++;
6241#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006242 if (flagtype == AFT_LONG || (flagtype == AFT_CAPLONG
Bram Moolenaar6de68532005-08-24 22:08:48 +00006243 && res >= 'A' && res <= 'Z'))
6244 {
6245 if (**pp == NUL)
6246 return 0;
6247#ifdef FEAT_MBYTE
6248 res = mb_ptr2char_adv(pp) + (res << 16);
6249#else
6250 res = *(*pp)++ + (res << 16);
6251#endif
6252 }
6253 }
6254 return res;
6255}
6256
6257/*
6258 * Process the "compflags" string used in an affix file and append it to
6259 * spin->si_compflags.
6260 * The processing involves changing the affix names to ID numbers, so that
6261 * they fit in one byte.
6262 */
6263 static void
6264process_compflags(spin, aff, compflags)
6265 spellinfo_T *spin;
6266 afffile_T *aff;
6267 char_u *compflags;
6268{
6269 char_u *p;
6270 char_u *prevp;
6271 unsigned flag;
6272 compitem_T *ci;
6273 int id;
6274 int len;
6275 char_u *tp;
6276 char_u key[AH_KEY_LEN];
6277 hashitem_T *hi;
6278
6279 /* Make room for the old and the new compflags, concatenated with a / in
6280 * between. Processing it makes it shorter, but we don't know by how
6281 * much, thus allocate the maximum. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006282 len = (int)STRLEN(compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006283 if (spin->si_compflags != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006284 len += (int)STRLEN(spin->si_compflags) + 1;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006285 p = getroom(spin, len, FALSE);
6286 if (p == NULL)
6287 return;
6288 if (spin->si_compflags != NULL)
6289 {
6290 STRCPY(p, spin->si_compflags);
6291 STRCAT(p, "/");
6292 }
Bram Moolenaar6de68532005-08-24 22:08:48 +00006293 spin->si_compflags = p;
6294 tp = p + STRLEN(p);
6295
6296 for (p = compflags; *p != NUL; )
6297 {
6298 if (vim_strchr((char_u *)"/*+[]", *p) != NULL)
6299 /* Copy non-flag characters directly. */
6300 *tp++ = *p++;
6301 else
6302 {
6303 /* First get the flag number, also checks validity. */
6304 prevp = p;
6305 flag = get_affitem(aff->af_flagtype, &p);
6306 if (flag != 0)
6307 {
6308 /* Find the flag in the hashtable. If it was used before, use
6309 * the existing ID. Otherwise add a new entry. */
6310 vim_strncpy(key, prevp, p - prevp);
6311 hi = hash_find(&aff->af_comp, key);
6312 if (!HASHITEM_EMPTY(hi))
6313 id = HI2CI(hi)->ci_newID;
6314 else
6315 {
6316 ci = (compitem_T *)getroom(spin, sizeof(compitem_T), TRUE);
6317 if (ci == NULL)
6318 break;
6319 STRCPY(ci->ci_key, key);
6320 ci->ci_flag = flag;
6321 /* Avoid using a flag ID that has a special meaning in a
6322 * regexp (also inside []). */
6323 do
6324 {
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006325 check_renumber(spin);
6326 id = spin->si_newcompID--;
6327 } while (vim_strchr((char_u *)"/+*[]\\-^", id) != NULL);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006328 ci->ci_newID = id;
6329 hash_add(&aff->af_comp, ci->ci_key);
6330 }
6331 *tp++ = id;
6332 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006333 if (aff->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006334 ++p;
6335 }
6336 }
6337
6338 *tp = NUL;
6339}
6340
6341/*
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00006342 * Check that the new IDs for postponed affixes and compounding don't overrun
6343 * each other. We have almost 255 available, but start at 0-127 to avoid
6344 * using two bytes for utf-8. When the 0-127 range is used up go to 128-255.
6345 * When that is used up an error message is given.
6346 */
6347 static void
6348check_renumber(spin)
6349 spellinfo_T *spin;
6350{
6351 if (spin->si_newprefID == spin->si_newcompID && spin->si_newcompID < 128)
6352 {
6353 spin->si_newprefID = 127;
6354 spin->si_newcompID = 255;
6355 }
6356}
6357
6358/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006359 * Return TRUE if flag "flag" appears in affix list "afflist".
6360 */
6361 static int
6362flag_in_afflist(flagtype, afflist, flag)
6363 int flagtype;
6364 char_u *afflist;
6365 unsigned flag;
6366{
6367 char_u *p;
6368 unsigned n;
6369
6370 switch (flagtype)
6371 {
6372 case AFT_CHAR:
6373 return vim_strchr(afflist, flag) != NULL;
6374
Bram Moolenaar95529562005-08-25 21:21:38 +00006375 case AFT_CAPLONG:
6376 case AFT_LONG:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006377 for (p = afflist; *p != NUL; )
6378 {
6379#ifdef FEAT_MBYTE
6380 n = mb_ptr2char_adv(&p);
6381#else
6382 n = *p++;
6383#endif
Bram Moolenaar95529562005-08-25 21:21:38 +00006384 if ((flagtype == AFT_LONG || (n >= 'A' && n <= 'Z'))
Bram Moolenaar6de68532005-08-24 22:08:48 +00006385 && *p != NUL)
6386#ifdef FEAT_MBYTE
6387 n = mb_ptr2char_adv(&p) + (n << 16);
6388#else
6389 n = *p++ + (n << 16);
6390#endif
6391 if (n == flag)
6392 return TRUE;
6393 }
6394 break;
6395
Bram Moolenaar95529562005-08-25 21:21:38 +00006396 case AFT_NUM:
Bram Moolenaar6de68532005-08-24 22:08:48 +00006397 for (p = afflist; *p != NUL; )
6398 {
6399 n = getdigits(&p);
6400 if (n == flag)
6401 return TRUE;
6402 if (*p != NUL) /* skip over comma */
6403 ++p;
6404 }
6405 break;
6406 }
6407 return FALSE;
6408}
6409
6410/*
6411 * Give a warning when "spinval" and "affval" numbers are set and not the same.
6412 */
6413 static void
6414aff_check_number(spinval, affval, name)
6415 int spinval;
6416 int affval;
6417 char *name;
6418{
6419 if (spinval != 0 && spinval != affval)
6420 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6421}
6422
6423/*
6424 * Give a warning when "spinval" and "affval" strings are set and not the same.
6425 */
6426 static void
6427aff_check_string(spinval, affval, name)
6428 char_u *spinval;
6429 char_u *affval;
6430 char *name;
6431{
6432 if (spinval != NULL && STRCMP(spinval, affval) != 0)
6433 smsg((char_u *)_("%s value differs from what is used in another .aff file"), name);
6434}
6435
6436/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006437 * Return TRUE if strings "s1" and "s2" are equal. Also consider both being
6438 * NULL as equal.
6439 */
6440 static int
6441str_equal(s1, s2)
6442 char_u *s1;
6443 char_u *s2;
6444{
6445 if (s1 == NULL || s2 == NULL)
6446 return s1 == s2;
6447 return STRCMP(s1, s2) == 0;
6448}
6449
6450/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006451 * Add a from-to item to "gap". Used for REP and SAL items.
6452 * They are stored case-folded.
6453 */
6454 static void
6455add_fromto(spin, gap, from, to)
6456 spellinfo_T *spin;
6457 garray_T *gap;
6458 char_u *from;
6459 char_u *to;
6460{
6461 fromto_T *ftp;
6462 char_u word[MAXWLEN];
6463
6464 if (ga_grow(gap, 1) == OK)
6465 {
6466 ftp = ((fromto_T *)gap->ga_data) + gap->ga_len;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006467 (void)spell_casefold(from, (int)STRLEN(from), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006468 ftp->ft_from = getroom_save(spin, word);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006469 (void)spell_casefold(to, (int)STRLEN(to), word, MAXWLEN);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006470 ftp->ft_to = getroom_save(spin, word);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006471 ++gap->ga_len;
6472 }
6473}
6474
6475/*
6476 * Convert a boolean argument in a SAL line to TRUE or FALSE;
6477 */
6478 static int
6479sal_to_bool(s)
6480 char_u *s;
6481{
6482 return STRCMP(s, "1") == 0 || STRCMP(s, "true") == 0;
6483}
6484
6485/*
Bram Moolenaar5482f332005-04-17 20:18:43 +00006486 * Return TRUE if string "s" contains a non-ASCII character (128 or higher).
6487 * When "s" is NULL FALSE is returned.
6488 */
6489 static int
6490has_non_ascii(s)
6491 char_u *s;
6492{
6493 char_u *p;
6494
6495 if (s != NULL)
6496 for (p = s; *p != NUL; ++p)
6497 if (*p >= 128)
6498 return TRUE;
6499 return FALSE;
6500}
6501
6502/*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006503 * Free the structure filled by spell_read_aff().
6504 */
6505 static void
6506spell_free_aff(aff)
6507 afffile_T *aff;
6508{
6509 hashtab_T *ht;
6510 hashitem_T *hi;
6511 int todo;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006512 affheader_T *ah;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006513 affentry_T *ae;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006514
6515 vim_free(aff->af_enc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006516
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006517 /* All this trouble to free the "ae_prog" items... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006518 for (ht = &aff->af_pref; ; ht = &aff->af_suff)
6519 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006520 todo = (int)ht->ht_used;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006521 for (hi = ht->ht_array; todo > 0; ++hi)
6522 {
6523 if (!HASHITEM_EMPTY(hi))
6524 {
6525 --todo;
6526 ah = HI2AH(hi);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006527 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
6528 vim_free(ae->ae_prog);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006529 }
6530 }
6531 if (ht == &aff->af_suff)
6532 break;
6533 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006534
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006535 hash_clear(&aff->af_pref);
6536 hash_clear(&aff->af_suff);
Bram Moolenaar6de68532005-08-24 22:08:48 +00006537 hash_clear(&aff->af_comp);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006538}
6539
6540/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006541 * Read dictionary file "fname".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006542 * Returns OK or FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006543 */
6544 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006545spell_read_dic(spin, fname, affile)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006546 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006547 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006548 afffile_T *affile;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006549{
Bram Moolenaar51485f02005-06-04 21:55:20 +00006550 hashtab_T ht;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006551 char_u line[MAXLINELEN];
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006552 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006553 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006554 char_u store_afflist[MAXWLEN];
6555 int pfxlen;
6556 int need_affix;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006557 char_u *dw;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006558 char_u *pc;
6559 char_u *w;
6560 int l;
6561 hash_T hash;
6562 hashitem_T *hi;
6563 FILE *fd;
6564 int lnum = 1;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006565 int non_ascii = 0;
6566 int retval = OK;
6567 char_u message[MAXLINELEN + MAXWLEN];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006568 int flags;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006569 int duplicate = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006570
Bram Moolenaar51485f02005-06-04 21:55:20 +00006571 /*
6572 * Open the file.
6573 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006574 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006575 if (fd == NULL)
6576 {
6577 EMSG2(_(e_notopen), fname);
6578 return FAIL;
6579 }
6580
Bram Moolenaar51485f02005-06-04 21:55:20 +00006581 /* The hashtable is only used to detect duplicated words. */
6582 hash_init(&ht);
6583
Bram Moolenaar4770d092006-01-12 23:22:24 +00006584 vim_snprintf((char *)IObuff, IOSIZE,
6585 _("Reading dictionary file %s ..."), fname);
6586 spell_message(spin, IObuff);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006587
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006588 /* start with a message for the first line */
6589 spin->si_msg_count = 999999;
6590
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006591 /* Read and ignore the first line: word count. */
6592 (void)vim_fgets(line, MAXLINELEN, fd);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006593 if (!vim_isdigit(*skipwhite(line)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006594 EMSG2(_("E760: No word count in %s"), fname);
6595
6596 /*
6597 * Read all the lines in the file one by one.
6598 * The words are converted to 'encoding' here, before being added to
6599 * the hashtable.
6600 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006601 while (!vim_fgets(line, MAXLINELEN, fd) && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006602 {
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006603 line_breakcheck();
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006604 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +00006605 if (line[0] == '#' || line[0] == '/')
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00006606 continue; /* comment line */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006607
Bram Moolenaar51485f02005-06-04 21:55:20 +00006608 /* Remove CR, LF and white space from the end. White space halfway
6609 * the word is kept to allow e.g., "et al.". */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006610 l = (int)STRLEN(line);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006611 while (l > 0 && line[l - 1] <= ' ')
6612 --l;
6613 if (l == 0)
6614 continue; /* empty line */
6615 line[l] = NUL;
6616
Bram Moolenaarb765d632005-06-07 21:00:02 +00006617#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006618 /* Convert from "SET" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006619 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006620 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006621 pc = string_convert(&spin->si_conv, line, NULL);
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006622 if (pc == NULL)
6623 {
6624 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
6625 fname, lnum, line);
6626 continue;
6627 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006628 w = pc;
6629 }
6630 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00006631#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006632 {
6633 pc = NULL;
6634 w = line;
6635 }
6636
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006637 /* Truncate the word at the "/", set "afflist" to what follows.
6638 * Replace "\/" by "/" and "\\" by "\". */
6639 afflist = NULL;
6640 for (p = w; *p != NUL; mb_ptr_adv(p))
6641 {
6642 if (*p == '\\' && (p[1] == '\\' || p[1] == '/'))
Bram Moolenaara7241f52008-06-24 20:39:31 +00006643 STRMOVE(p, p + 1);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006644 else if (*p == '/')
6645 {
6646 *p = NUL;
6647 afflist = p + 1;
6648 break;
6649 }
6650 }
6651
6652 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
6653 if (spin->si_ascii && has_non_ascii(w))
6654 {
6655 ++non_ascii;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006656 vim_free(pc);
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006657 continue;
6658 }
6659
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006660 /* This takes time, print a message every 10000 words. */
6661 if (spin->si_verbose && spin->si_msg_count > 10000)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006662 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006663 spin->si_msg_count = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006664 vim_snprintf((char *)message, sizeof(message),
6665 _("line %6d, word %6d - %s"),
6666 lnum, spin->si_foldwcount + spin->si_keepwcount, w);
6667 msg_start();
6668 msg_puts_long_attr(message, 0);
6669 msg_clr_eos();
6670 msg_didout = FALSE;
6671 msg_col = 0;
6672 out_flush();
6673 }
6674
Bram Moolenaar51485f02005-06-04 21:55:20 +00006675 /* Store the word in the hashtable to be able to find duplicates. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006676 dw = (char_u *)getroom_save(spin, w);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006677 if (dw == NULL)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006678 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006679 retval = FAIL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006680 vim_free(pc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006681 break;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006682 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006683
Bram Moolenaar51485f02005-06-04 21:55:20 +00006684 hash = hash_hash(dw);
6685 hi = hash_lookup(&ht, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006686 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006687 {
6688 if (p_verbose > 0)
6689 smsg((char_u *)_("Duplicate word in %s line %d: %s"),
Bram Moolenaar42eeac32005-06-29 22:40:58 +00006690 fname, lnum, dw);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006691 else if (duplicate == 0)
6692 smsg((char_u *)_("First duplicate word in %s line %d: %s"),
6693 fname, lnum, dw);
6694 ++duplicate;
6695 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006696 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00006697 hash_add_item(&ht, hi, dw, hash);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006698
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006699 flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006700 store_afflist[0] = NUL;
6701 pfxlen = 0;
6702 need_affix = FALSE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006703 if (afflist != NULL)
6704 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006705 /* Extract flags from the affix list. */
6706 flags |= get_affix_flags(affile, afflist);
6707
Bram Moolenaar6de68532005-08-24 22:08:48 +00006708 if (affile->af_needaffix != 0 && flag_in_afflist(
6709 affile->af_flagtype, afflist, affile->af_needaffix))
Bram Moolenaar5195e452005-08-19 20:32:47 +00006710 need_affix = TRUE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006711
6712 if (affile->af_pfxpostpone)
6713 /* Need to store the list of prefix IDs with the word. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006714 pfxlen = get_pfxlist(affile, afflist, store_afflist);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00006715
Bram Moolenaar5195e452005-08-19 20:32:47 +00006716 if (spin->si_compflags != NULL)
6717 /* Need to store the list of compound flags with the word.
6718 * Concatenate them to the list of prefix IDs. */
Bram Moolenaar6de68532005-08-24 22:08:48 +00006719 get_compflags(affile, afflist, store_afflist + pfxlen);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006720 }
6721
Bram Moolenaar51485f02005-06-04 21:55:20 +00006722 /* Add the word to the word tree(s). */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006723 if (store_word(spin, dw, flags, spin->si_region,
6724 store_afflist, need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006725 retval = FAIL;
6726
6727 if (afflist != NULL)
6728 {
6729 /* Find all matching suffixes and add the resulting words.
6730 * Additionally do matching prefixes that combine. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006731 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006732 &affile->af_suff, &affile->af_pref,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006733 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006734 retval = FAIL;
6735
6736 /* Find all matching prefixes and add the resulting words. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006737 if (store_aff_word(spin, dw, afflist, affile,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006738 &affile->af_pref, NULL,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006739 CONDIT_SUF, flags, store_afflist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006740 retval = FAIL;
6741 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00006742
6743 vim_free(pc);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006744 }
6745
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006746 if (duplicate > 0)
6747 smsg((char_u *)_("%d duplicate word(s) in %s"), duplicate, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006748 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006749 smsg((char_u *)_("Ignored %d word(s) with non-ASCII characters in %s"),
6750 non_ascii, fname);
Bram Moolenaar51485f02005-06-04 21:55:20 +00006751 hash_clear(&ht);
6752
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006753 fclose(fd);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006754 return retval;
6755}
6756
6757/*
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006758 * Check for affix flags in "afflist" that are turned into word flags.
6759 * Return WF_ flags.
6760 */
6761 static int
6762get_affix_flags(affile, afflist)
6763 afffile_T *affile;
6764 char_u *afflist;
6765{
6766 int flags = 0;
6767
6768 if (affile->af_keepcase != 0 && flag_in_afflist(
6769 affile->af_flagtype, afflist, affile->af_keepcase))
6770 flags |= WF_KEEPCAP | WF_FIXCAP;
6771 if (affile->af_rare != 0 && flag_in_afflist(
6772 affile->af_flagtype, afflist, affile->af_rare))
6773 flags |= WF_RARE;
6774 if (affile->af_bad != 0 && flag_in_afflist(
6775 affile->af_flagtype, afflist, affile->af_bad))
6776 flags |= WF_BANNED;
6777 if (affile->af_needcomp != 0 && flag_in_afflist(
6778 affile->af_flagtype, afflist, affile->af_needcomp))
6779 flags |= WF_NEEDCOMP;
6780 if (affile->af_comproot != 0 && flag_in_afflist(
6781 affile->af_flagtype, afflist, affile->af_comproot))
6782 flags |= WF_COMPROOT;
6783 if (affile->af_nosuggest != 0 && flag_in_afflist(
6784 affile->af_flagtype, afflist, affile->af_nosuggest))
6785 flags |= WF_NOSUGGEST;
6786 return flags;
6787}
6788
6789/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006790 * Get the list of prefix IDs from the affix list "afflist".
6791 * Used for PFXPOSTPONE.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006792 * Put the resulting flags in "store_afflist[MAXWLEN]" with a terminating NUL
6793 * and return the number of affixes.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006794 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006795 static int
6796get_pfxlist(affile, afflist, store_afflist)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006797 afffile_T *affile;
6798 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006799 char_u *store_afflist;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006800{
6801 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006802 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006803 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006804 int id;
6805 char_u key[AH_KEY_LEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006806 hashitem_T *hi;
6807
Bram Moolenaar6de68532005-08-24 22:08:48 +00006808 for (p = afflist; *p != NUL; )
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006809 {
Bram Moolenaar6de68532005-08-24 22:08:48 +00006810 prevp = p;
6811 if (get_affitem(affile->af_flagtype, &p) != 0)
6812 {
6813 /* A flag is a postponed prefix flag if it appears in "af_pref"
6814 * and it's ID is not zero. */
6815 vim_strncpy(key, prevp, p - prevp);
6816 hi = hash_find(&affile->af_pref, key);
6817 if (!HASHITEM_EMPTY(hi))
6818 {
6819 id = HI2AH(hi)->ah_newID;
6820 if (id != 0)
6821 store_afflist[cnt++] = id;
6822 }
6823 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006824 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006825 ++p;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006826 }
6827
Bram Moolenaar5195e452005-08-19 20:32:47 +00006828 store_afflist[cnt] = NUL;
6829 return cnt;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006830}
6831
6832/*
Bram Moolenaar6de68532005-08-24 22:08:48 +00006833 * Get the list of compound IDs from the affix list "afflist" that are used
6834 * for compound words.
Bram Moolenaar5195e452005-08-19 20:32:47 +00006835 * Puts the flags in "store_afflist[]".
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006836 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006837 static void
Bram Moolenaar6de68532005-08-24 22:08:48 +00006838get_compflags(affile, afflist, store_afflist)
6839 afffile_T *affile;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006840 char_u *afflist;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006841 char_u *store_afflist;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006842{
6843 char_u *p;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006844 char_u *prevp;
Bram Moolenaar5195e452005-08-19 20:32:47 +00006845 int cnt = 0;
Bram Moolenaar6de68532005-08-24 22:08:48 +00006846 char_u key[AH_KEY_LEN];
6847 hashitem_T *hi;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006848
Bram Moolenaar6de68532005-08-24 22:08:48 +00006849 for (p = afflist; *p != NUL; )
6850 {
6851 prevp = p;
6852 if (get_affitem(affile->af_flagtype, &p) != 0)
6853 {
6854 /* A flag is a compound flag if it appears in "af_comp". */
6855 vim_strncpy(key, prevp, p - prevp);
6856 hi = hash_find(&affile->af_comp, key);
6857 if (!HASHITEM_EMPTY(hi))
6858 store_afflist[cnt++] = HI2CI(hi)->ci_newID;
6859 }
Bram Moolenaar95529562005-08-25 21:21:38 +00006860 if (affile->af_flagtype == AFT_NUM && *p == ',')
Bram Moolenaar6de68532005-08-24 22:08:48 +00006861 ++p;
6862 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006863
Bram Moolenaar5195e452005-08-19 20:32:47 +00006864 store_afflist[cnt] = NUL;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006865}
6866
6867/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00006868 * Apply affixes to a word and store the resulting words.
6869 * "ht" is the hashtable with affentry_T that need to be applied, either
6870 * prefixes or suffixes.
6871 * "xht", when not NULL, is the prefix hashtable, to be used additionally on
6872 * the resulting words for combining affixes.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006873 *
6874 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006875 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006876 static int
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006877store_aff_word(spin, word, afflist, affile, ht, xht, condit, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +00006878 pfxlist, pfxlen)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006879 spellinfo_T *spin; /* spell info */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006880 char_u *word; /* basic word start */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006881 char_u *afflist; /* list of names of supported affixes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006882 afffile_T *affile;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006883 hashtab_T *ht;
6884 hashtab_T *xht;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006885 int condit; /* CONDIT_SUF et al. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00006886 int flags; /* flags for the word */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006887 char_u *pfxlist; /* list of prefix IDs */
Bram Moolenaar5195e452005-08-19 20:32:47 +00006888 int pfxlen; /* nr of flags in "pfxlist" for prefixes, rest
6889 * is compound flags */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006890{
6891 int todo;
6892 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006893 affheader_T *ah;
6894 affentry_T *ae;
6895 regmatch_T regmatch;
6896 char_u newword[MAXWLEN];
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00006897 int retval = OK;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006898 int i, j;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006899 char_u *p;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00006900 int use_flags;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00006901 char_u *use_pfxlist;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006902 int use_pfxlen;
6903 int need_affix;
6904 char_u store_afflist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006905 char_u pfx_pfxlist[MAXWLEN];
Bram Moolenaar5195e452005-08-19 20:32:47 +00006906 size_t wordlen = STRLEN(word);
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006907 int use_condit;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006908
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006909 todo = (int)ht->ht_used;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006910 for (hi = ht->ht_array; todo > 0 && retval == OK; ++hi)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006911 {
6912 if (!HASHITEM_EMPTY(hi))
6913 {
6914 --todo;
Bram Moolenaar51485f02005-06-04 21:55:20 +00006915 ah = HI2AH(hi);
Bram Moolenaar5482f332005-04-17 20:18:43 +00006916
Bram Moolenaar51485f02005-06-04 21:55:20 +00006917 /* Check that the affix combines, if required, and that the word
6918 * supports this affix. */
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006919 if (((condit & CONDIT_COMB) == 0 || ah->ah_combine)
6920 && flag_in_afflist(affile->af_flagtype, afflist,
6921 ah->ah_flag))
Bram Moolenaar5482f332005-04-17 20:18:43 +00006922 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006923 /* Loop over all affix entries with this name. */
6924 for (ae = ah->ah_first; ae != NULL; ae = ae->ae_next)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006925 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006926 /* Check the condition. It's not logical to match case
6927 * here, but it is required for compatibility with
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006928 * Myspell.
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006929 * Another requirement from Myspell is that the chop
6930 * string is shorter than the word itself.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006931 * For prefixes, when "PFXPOSTPONE" was used, only do
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006932 * prefixes with a chop string and/or flags.
6933 * When a previously added affix had CIRCUMFIX this one
6934 * must have it too, if it had not then this one must not
6935 * have one either. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00006936 regmatch.regprog = ae->ae_prog;
6937 regmatch.rm_ic = FALSE;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006938 if ((xht != NULL || !affile->af_pfxpostpone
Bram Moolenaar899dddf2006-03-26 21:06:50 +00006939 || ae->ae_chop != NULL
6940 || ae->ae_flags != NULL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00006941 && (ae->ae_chop == NULL
6942 || STRLEN(ae->ae_chop) < wordlen)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00006943 && (ae->ae_prog == NULL
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006944 || vim_regexec(&regmatch, word, (colnr_T)0))
6945 && (((condit & CONDIT_CFIX) == 0)
6946 == ((condit & CONDIT_AFF) == 0
6947 || ae->ae_flags == NULL
6948 || !flag_in_afflist(affile->af_flagtype,
6949 ae->ae_flags, affile->af_circumfix))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006950 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006951 /* Match. Remove the chop and add the affix. */
6952 if (xht == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006953 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006954 /* prefix: chop/add at the start of the word */
6955 if (ae->ae_add == NULL)
6956 *newword = NUL;
6957 else
6958 STRCPY(newword, ae->ae_add);
6959 p = word;
6960 if (ae->ae_chop != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00006961 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00006962 /* Skip chop string. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00006963#ifdef FEAT_MBYTE
6964 if (has_mbyte)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006965 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00006966 i = mb_charlen(ae->ae_chop);
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006967 for ( ; i > 0; --i)
6968 mb_ptr_adv(p);
6969 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00006970 else
6971#endif
Bram Moolenaar9f30f502005-06-14 22:01:04 +00006972 p += STRLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006973 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00006974 STRCAT(newword, p);
6975 }
6976 else
6977 {
6978 /* suffix: chop/add at the end of the word */
6979 STRCPY(newword, word);
6980 if (ae->ae_chop != NULL)
6981 {
6982 /* Remove chop string. */
6983 p = newword + STRLEN(newword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006984 i = (int)MB_CHARLEN(ae->ae_chop);
Bram Moolenaarb765d632005-06-07 21:00:02 +00006985 for ( ; i > 0; --i)
Bram Moolenaar51485f02005-06-04 21:55:20 +00006986 mb_ptr_back(newword, p);
6987 *p = NUL;
6988 }
6989 if (ae->ae_add != NULL)
6990 STRCAT(newword, ae->ae_add);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006991 }
6992
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006993 use_flags = flags;
6994 use_pfxlist = pfxlist;
6995 use_pfxlen = pfxlen;
6996 need_affix = FALSE;
6997 use_condit = condit | CONDIT_COMB | CONDIT_AFF;
6998 if (ae->ae_flags != NULL)
6999 {
7000 /* Extract flags from the affix list. */
7001 use_flags |= get_affix_flags(affile, ae->ae_flags);
7002
7003 if (affile->af_needaffix != 0 && flag_in_afflist(
7004 affile->af_flagtype, ae->ae_flags,
7005 affile->af_needaffix))
7006 need_affix = TRUE;
7007
7008 /* When there is a CIRCUMFIX flag the other affix
7009 * must also have it and we don't add the word
7010 * with one affix. */
7011 if (affile->af_circumfix != 0 && flag_in_afflist(
7012 affile->af_flagtype, ae->ae_flags,
7013 affile->af_circumfix))
7014 {
7015 use_condit |= CONDIT_CFIX;
7016 if ((condit & CONDIT_CFIX) == 0)
7017 need_affix = TRUE;
7018 }
7019
7020 if (affile->af_pfxpostpone
7021 || spin->si_compflags != NULL)
7022 {
7023 if (affile->af_pfxpostpone)
7024 /* Get prefix IDS from the affix list. */
7025 use_pfxlen = get_pfxlist(affile,
7026 ae->ae_flags, store_afflist);
7027 else
7028 use_pfxlen = 0;
7029 use_pfxlist = store_afflist;
7030
7031 /* Combine the prefix IDs. Avoid adding the
7032 * same ID twice. */
7033 for (i = 0; i < pfxlen; ++i)
7034 {
7035 for (j = 0; j < use_pfxlen; ++j)
7036 if (pfxlist[i] == use_pfxlist[j])
7037 break;
7038 if (j == use_pfxlen)
7039 use_pfxlist[use_pfxlen++] = pfxlist[i];
7040 }
7041
7042 if (spin->si_compflags != NULL)
7043 /* Get compound IDS from the affix list. */
7044 get_compflags(affile, ae->ae_flags,
7045 use_pfxlist + use_pfxlen);
7046
7047 /* Combine the list of compound flags.
7048 * Concatenate them to the prefix IDs list.
7049 * Avoid adding the same ID twice. */
7050 for (i = pfxlen; pfxlist[i] != NUL; ++i)
7051 {
7052 for (j = use_pfxlen;
7053 use_pfxlist[j] != NUL; ++j)
7054 if (pfxlist[i] == use_pfxlist[j])
7055 break;
7056 if (use_pfxlist[j] == NUL)
7057 {
7058 use_pfxlist[j++] = pfxlist[i];
7059 use_pfxlist[j] = NUL;
7060 }
7061 }
7062 }
7063 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00007064
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007065 /* Obey a "COMPOUNDFORBIDFLAG" of the affix: don't
Bram Moolenaar899dddf2006-03-26 21:06:50 +00007066 * use the compound flags. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007067 if (use_pfxlist != NULL && ae->ae_compforbid)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007068 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007069 vim_strncpy(pfx_pfxlist, use_pfxlist, use_pfxlen);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007070 use_pfxlist = pfx_pfxlist;
7071 }
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007072
7073 /* When there are postponed prefixes... */
Bram Moolenaar551f84f2005-07-06 22:29:20 +00007074 if (spin->si_prefroot != NULL
7075 && spin->si_prefroot->wn_sibling != NULL)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007076 {
7077 /* ... add a flag to indicate an affix was used. */
7078 use_flags |= WF_HAS_AFF;
7079
7080 /* ... don't use a prefix list if combining
Bram Moolenaar5195e452005-08-19 20:32:47 +00007081 * affixes is not allowed. But do use the
7082 * compound flags after them. */
Bram Moolenaar18144c82006-04-12 21:52:12 +00007083 if (!ah->ah_combine && use_pfxlist != NULL)
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007084 use_pfxlist += use_pfxlen;
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007085 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007086
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007087 /* When compounding is supported and there is no
7088 * "COMPOUNDPERMITFLAG" then forbid compounding on the
7089 * side where the affix is applied. */
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007090 if (spin->si_compflags != NULL && !ae->ae_comppermit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007091 {
7092 if (xht != NULL)
7093 use_flags |= WF_NOCOMPAFT;
7094 else
7095 use_flags |= WF_NOCOMPBEF;
7096 }
7097
Bram Moolenaar51485f02005-06-04 21:55:20 +00007098 /* Store the modified word. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007099 if (store_word(spin, newword, use_flags,
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007100 spin->si_region, use_pfxlist,
7101 need_affix) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007102 retval = FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007103
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007104 /* When added a prefix or a first suffix and the affix
7105 * has flags may add a(nother) suffix. RECURSIVE! */
7106 if ((condit & CONDIT_SUF) && ae->ae_flags != NULL)
7107 if (store_aff_word(spin, newword, ae->ae_flags,
7108 affile, &affile->af_suff, xht,
7109 use_condit & (xht == NULL
7110 ? ~0 : ~CONDIT_SUF),
Bram Moolenaar5195e452005-08-19 20:32:47 +00007111 use_flags, use_pfxlist, pfxlen) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007112 retval = FAIL;
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007113
7114 /* When added a suffix and combining is allowed also
7115 * try adding a prefix additionally. Both for the
7116 * word flags and for the affix flags. RECURSIVE! */
7117 if (xht != NULL && ah->ah_combine)
7118 {
7119 if (store_aff_word(spin, newword,
7120 afflist, affile,
7121 xht, NULL, use_condit,
7122 use_flags, use_pfxlist,
7123 pfxlen) == FAIL
7124 || (ae->ae_flags != NULL
7125 && store_aff_word(spin, newword,
7126 ae->ae_flags, affile,
7127 xht, NULL, use_condit,
7128 use_flags, use_pfxlist,
7129 pfxlen) == FAIL))
7130 retval = FAIL;
7131 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007132 }
7133 }
7134 }
7135 }
7136 }
7137
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007138 return retval;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007139}
7140
7141/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007142 * Read a file with a list of words.
7143 */
7144 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007145spell_read_wordfile(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007146 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007147 char_u *fname;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007148{
7149 FILE *fd;
7150 long lnum = 0;
7151 char_u rline[MAXLINELEN];
7152 char_u *line;
7153 char_u *pc = NULL;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007154 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007155 int l;
7156 int retval = OK;
7157 int did_word = FALSE;
7158 int non_ascii = 0;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007159 int flags;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007160 int regionmask;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007161
7162 /*
7163 * Open the file.
7164 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00007165 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007166 if (fd == NULL)
7167 {
7168 EMSG2(_(e_notopen), fname);
7169 return FAIL;
7170 }
7171
Bram Moolenaar4770d092006-01-12 23:22:24 +00007172 vim_snprintf((char *)IObuff, IOSIZE, _("Reading word file %s ..."), fname);
7173 spell_message(spin, IObuff);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007174
7175 /*
7176 * Read all the lines in the file one by one.
7177 */
7178 while (!vim_fgets(rline, MAXLINELEN, fd) && !got_int)
7179 {
7180 line_breakcheck();
7181 ++lnum;
7182
7183 /* Skip comment lines. */
7184 if (*rline == '#')
7185 continue;
7186
7187 /* Remove CR, LF and white space from the end. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007188 l = (int)STRLEN(rline);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007189 while (l > 0 && rline[l - 1] <= ' ')
7190 --l;
7191 if (l == 0)
7192 continue; /* empty or blank line */
7193 rline[l] = NUL;
7194
Bram Moolenaar9c102382006-05-03 21:26:49 +00007195 /* Convert from "/encoding={encoding}" to 'encoding' when needed. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007196 vim_free(pc);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007197#ifdef FEAT_MBYTE
Bram Moolenaar51485f02005-06-04 21:55:20 +00007198 if (spin->si_conv.vc_type != CONV_NONE)
7199 {
7200 pc = string_convert(&spin->si_conv, rline, NULL);
7201 if (pc == NULL)
7202 {
7203 smsg((char_u *)_("Conversion failure for word in %s line %d: %s"),
7204 fname, lnum, rline);
7205 continue;
7206 }
7207 line = pc;
7208 }
7209 else
Bram Moolenaarb765d632005-06-07 21:00:02 +00007210#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007211 {
7212 pc = NULL;
7213 line = rline;
7214 }
7215
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007216 if (*line == '/')
Bram Moolenaar51485f02005-06-04 21:55:20 +00007217 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007218 ++line;
7219 if (STRNCMP(line, "encoding=", 9) == 0)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007220 {
7221 if (spin->si_conv.vc_type != CONV_NONE)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007222 smsg((char_u *)_("Duplicate /encoding= line ignored in %s line %d: %s"),
7223 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007224 else if (did_word)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007225 smsg((char_u *)_("/encoding= line after word ignored in %s line %d: %s"),
7226 fname, lnum, line - 1);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007227 else
7228 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00007229#ifdef FEAT_MBYTE
7230 char_u *enc;
7231
Bram Moolenaar51485f02005-06-04 21:55:20 +00007232 /* Setup for conversion to 'encoding'. */
Bram Moolenaar9c102382006-05-03 21:26:49 +00007233 line += 9;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007234 enc = enc_canonize(line);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007235 if (enc != NULL && !spin->si_ascii
7236 && convert_setup(&spin->si_conv, enc,
7237 p_enc) == FAIL)
7238 smsg((char_u *)_("Conversion in %s not supported: from %s to %s"),
Bram Moolenaar3982c542005-06-08 21:56:31 +00007239 fname, line, p_enc);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007240 vim_free(enc);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00007241 spin->si_conv.vc_fail = TRUE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00007242#else
7243 smsg((char_u *)_("Conversion in %s not supported"), fname);
7244#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007245 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007246 continue;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007247 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007248
Bram Moolenaar3982c542005-06-08 21:56:31 +00007249 if (STRNCMP(line, "regions=", 8) == 0)
7250 {
7251 if (spin->si_region_count > 1)
7252 smsg((char_u *)_("Duplicate /regions= line ignored in %s line %d: %s"),
7253 fname, lnum, line);
7254 else
7255 {
7256 line += 8;
7257 if (STRLEN(line) > 16)
7258 smsg((char_u *)_("Too many regions in %s line %d: %s"),
7259 fname, lnum, line);
7260 else
7261 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007262 spin->si_region_count = (int)STRLEN(line) / 2;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007263 STRCPY(spin->si_region_name, line);
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007264
7265 /* Adjust the mask for a word valid in all regions. */
7266 spin->si_region = (1 << spin->si_region_count) - 1;
Bram Moolenaar3982c542005-06-08 21:56:31 +00007267 }
7268 }
7269 continue;
7270 }
7271
Bram Moolenaar7887d882005-07-01 22:33:52 +00007272 smsg((char_u *)_("/ line ignored in %s line %d: %s"),
7273 fname, lnum, line - 1);
7274 continue;
7275 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007276
Bram Moolenaar7887d882005-07-01 22:33:52 +00007277 flags = 0;
7278 regionmask = spin->si_region;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007279
Bram Moolenaar7887d882005-07-01 22:33:52 +00007280 /* Check for flags and region after a slash. */
7281 p = vim_strchr(line, '/');
7282 if (p != NULL)
7283 {
7284 *p++ = NUL;
7285 while (*p != NUL)
Bram Moolenaar3982c542005-06-08 21:56:31 +00007286 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007287 if (*p == '=') /* keep-case word */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00007288 flags |= WF_KEEPCAP | WF_FIXCAP;
Bram Moolenaar7887d882005-07-01 22:33:52 +00007289 else if (*p == '!') /* Bad, bad, wicked word. */
7290 flags |= WF_BANNED;
7291 else if (*p == '?') /* Rare word. */
7292 flags |= WF_RARE;
7293 else if (VIM_ISDIGIT(*p)) /* region number(s) */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007294 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00007295 if ((flags & WF_REGION) == 0) /* first one */
7296 regionmask = 0;
7297 flags |= WF_REGION;
7298
7299 l = *p - '0';
Bram Moolenaar3982c542005-06-08 21:56:31 +00007300 if (l > spin->si_region_count)
7301 {
7302 smsg((char_u *)_("Invalid region nr in %s line %d: %s"),
Bram Moolenaar7887d882005-07-01 22:33:52 +00007303 fname, lnum, p);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007304 break;
7305 }
7306 regionmask |= 1 << (l - 1);
Bram Moolenaar3982c542005-06-08 21:56:31 +00007307 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00007308 else
7309 {
7310 smsg((char_u *)_("Unrecognized flags in %s line %d: %s"),
7311 fname, lnum, p);
7312 break;
7313 }
7314 ++p;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007315 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007316 }
7317
7318 /* Skip non-ASCII words when "spin->si_ascii" is TRUE. */
7319 if (spin->si_ascii && has_non_ascii(line))
7320 {
7321 ++non_ascii;
7322 continue;
7323 }
7324
7325 /* Normal word: store it. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007326 if (store_word(spin, line, flags, regionmask, NULL, FALSE) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007327 {
7328 retval = FAIL;
7329 break;
7330 }
7331 did_word = TRUE;
7332 }
7333
7334 vim_free(pc);
7335 fclose(fd);
7336
Bram Moolenaar4770d092006-01-12 23:22:24 +00007337 if (spin->si_ascii && non_ascii > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007338 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007339 vim_snprintf((char *)IObuff, IOSIZE,
7340 _("Ignored %d words with non-ASCII characters"), non_ascii);
7341 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007342 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007343
Bram Moolenaar51485f02005-06-04 21:55:20 +00007344 return retval;
7345}
7346
7347/*
7348 * Get part of an sblock_T, "len" bytes long.
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007349 * This avoids calling free() for every little struct we use (and keeping
7350 * track of them).
Bram Moolenaar51485f02005-06-04 21:55:20 +00007351 * The memory is cleared to all zeros.
7352 * Returns NULL when out of memory.
7353 */
7354 static void *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007355getroom(spin, len, align)
7356 spellinfo_T *spin;
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007357 size_t len; /* length needed */
7358 int align; /* align for pointer */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007359{
7360 char_u *p;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007361 sblock_T *bl = spin->si_blocks;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007362
Bram Moolenaarcfc7d632005-07-28 22:28:16 +00007363 if (align && bl != NULL)
7364 /* Round size up for alignment. On some systems structures need to be
7365 * aligned to the size of a pointer (e.g., SPARC). */
7366 bl->sb_used = (bl->sb_used + sizeof(char *) - 1)
7367 & ~(sizeof(char *) - 1);
7368
Bram Moolenaar51485f02005-06-04 21:55:20 +00007369 if (bl == NULL || bl->sb_used + len > SBLOCKSIZE)
7370 {
7371 /* Allocate a block of memory. This is not freed until much later. */
7372 bl = (sblock_T *)alloc_clear((unsigned)(sizeof(sblock_T) + SBLOCKSIZE));
7373 if (bl == NULL)
7374 return NULL;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007375 bl->sb_next = spin->si_blocks;
7376 spin->si_blocks = bl;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007377 bl->sb_used = 0;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007378 ++spin->si_blocks_cnt;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007379 }
7380
7381 p = bl->sb_data + bl->sb_used;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007382 bl->sb_used += (int)len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007383
7384 return p;
7385}
7386
7387/*
7388 * Make a copy of a string into memory allocated with getroom().
7389 */
7390 static char_u *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007391getroom_save(spin, s)
7392 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007393 char_u *s;
7394{
7395 char_u *sc;
7396
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007397 sc = (char_u *)getroom(spin, STRLEN(s) + 1, FALSE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007398 if (sc != NULL)
7399 STRCPY(sc, s);
7400 return sc;
7401}
7402
7403
7404/*
7405 * Free the list of allocated sblock_T.
7406 */
7407 static void
7408free_blocks(bl)
7409 sblock_T *bl;
7410{
7411 sblock_T *next;
7412
7413 while (bl != NULL)
7414 {
7415 next = bl->sb_next;
7416 vim_free(bl);
7417 bl = next;
7418 }
7419}
7420
7421/*
7422 * Allocate the root of a word tree.
7423 */
7424 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007425wordtree_alloc(spin)
7426 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007427{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007428 return (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007429}
7430
7431/*
7432 * Store a word in the tree(s).
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007433 * Always store it in the case-folded tree. For a keep-case word this is
7434 * useful when the word can also be used with all caps (no WF_FIXCAP flag) and
7435 * used to find suggestions.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007436 * For a keep-case word also store it in the keep-case tree.
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007437 * When "pfxlist" is not NULL store the word for each postponed prefix ID and
7438 * compound flag.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007439 */
7440 static int
Bram Moolenaar5195e452005-08-19 20:32:47 +00007441store_word(spin, word, flags, region, pfxlist, need_affix)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007442 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007443 char_u *word;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007444 int flags; /* extra flags, WF_BANNED */
Bram Moolenaar3982c542005-06-08 21:56:31 +00007445 int region; /* supported region(s) */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007446 char_u *pfxlist; /* list of prefix IDs or NULL */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007447 int need_affix; /* only store word with affix ID */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007448{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007449 int len = (int)STRLEN(word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007450 int ct = captype(word, word + len);
7451 char_u foldword[MAXWLEN];
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007452 int res = OK;
7453 char_u *p;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007454
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007455 (void)spell_casefold(word, len, foldword, MAXWLEN);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007456 for (p = pfxlist; res == OK; ++p)
7457 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007458 if (!need_affix || (p != NULL && *p != NUL))
7459 res = tree_add_word(spin, foldword, spin->si_foldroot, ct | flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007460 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007461 if (p == NULL || *p == NUL)
7462 break;
7463 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007464 ++spin->si_foldwcount;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00007465
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007466 if (res == OK && (ct == WF_KEEPCAP || (flags & WF_KEEPCAP)))
Bram Moolenaar8db73182005-06-17 21:51:16 +00007467 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007468 for (p = pfxlist; res == OK; ++p)
7469 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00007470 if (!need_affix || (p != NULL && *p != NUL))
7471 res = tree_add_word(spin, word, spin->si_keeproot, flags,
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007472 region, p == NULL ? 0 : *p);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007473 if (p == NULL || *p == NUL)
7474 break;
7475 }
Bram Moolenaar8db73182005-06-17 21:51:16 +00007476 ++spin->si_keepwcount;
7477 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007478 return res;
7479}
7480
7481/*
7482 * Add word "word" to a word tree at "root".
Bram Moolenaar4770d092006-01-12 23:22:24 +00007483 * When "flags" < 0 we are adding to the prefix tree where "flags" is used for
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00007484 * "rare" and "region" is the condition nr.
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007485 * Returns FAIL when out of memory.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007486 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007487 static int
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007488tree_add_word(spin, word, root, flags, region, affixID)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007489 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007490 char_u *word;
7491 wordnode_T *root;
7492 int flags;
7493 int region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007494 int affixID;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007495{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007496 wordnode_T *node = root;
7497 wordnode_T *np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007498 wordnode_T *copyp, **copyprev;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007499 wordnode_T **prev = NULL;
7500 int i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007501
Bram Moolenaar51485f02005-06-04 21:55:20 +00007502 /* Add each byte of the word to the tree, including the NUL at the end. */
7503 for (i = 0; ; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007504 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007505 /* When there is more than one reference to this node we need to make
7506 * a copy, so that we can modify it. Copy the whole list of siblings
7507 * (we don't optimize for a partly shared list of siblings). */
7508 if (node != NULL && node->wn_refs > 1)
7509 {
7510 --node->wn_refs;
7511 copyprev = prev;
7512 for (copyp = node; copyp != NULL; copyp = copyp->wn_sibling)
7513 {
7514 /* Allocate a new node and copy the info. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007515 np = get_wordnode(spin);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007516 if (np == NULL)
7517 return FAIL;
7518 np->wn_child = copyp->wn_child;
7519 if (np->wn_child != NULL)
7520 ++np->wn_child->wn_refs; /* child gets extra ref */
7521 np->wn_byte = copyp->wn_byte;
7522 if (np->wn_byte == NUL)
7523 {
7524 np->wn_flags = copyp->wn_flags;
7525 np->wn_region = copyp->wn_region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007526 np->wn_affixID = copyp->wn_affixID;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007527 }
7528
7529 /* Link the new node in the list, there will be one ref. */
7530 np->wn_refs = 1;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007531 if (copyprev != NULL)
7532 *copyprev = np;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007533 copyprev = &np->wn_sibling;
7534
7535 /* Let "node" point to the head of the copied list. */
7536 if (copyp == node)
7537 node = np;
7538 }
7539 }
7540
Bram Moolenaar51485f02005-06-04 21:55:20 +00007541 /* Look for the sibling that has the same character. They are sorted
7542 * on byte value, thus stop searching when a sibling is found with a
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007543 * higher byte value. For zero bytes (end of word) the sorting is
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007544 * done on flags and then on affixID. */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007545 while (node != NULL
7546 && (node->wn_byte < word[i]
7547 || (node->wn_byte == NUL
7548 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007549 ? node->wn_affixID < (unsigned)affixID
7550 : (node->wn_flags < (unsigned)(flags & WN_MASK)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007551 || (node->wn_flags == (flags & WN_MASK)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007552 && (spin->si_sugtree
7553 ? (node->wn_region & 0xffff) < region
7554 : node->wn_affixID
7555 < (unsigned)affixID)))))))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007556 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007557 prev = &node->wn_sibling;
7558 node = *prev;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007559 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007560 if (node == NULL
7561 || node->wn_byte != word[i]
7562 || (word[i] == NUL
7563 && (flags < 0
Bram Moolenaar4770d092006-01-12 23:22:24 +00007564 || spin->si_sugtree
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00007565 || node->wn_flags != (flags & WN_MASK)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007566 || node->wn_affixID != affixID)))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007567 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007568 /* Allocate a new node. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007569 np = get_wordnode(spin);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007570 if (np == NULL)
7571 return FAIL;
7572 np->wn_byte = word[i];
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007573
7574 /* If "node" is NULL this is a new child or the end of the sibling
7575 * list: ref count is one. Otherwise use ref count of sibling and
7576 * make ref count of sibling one (matters when inserting in front
7577 * of the list of siblings). */
7578 if (node == NULL)
7579 np->wn_refs = 1;
7580 else
7581 {
7582 np->wn_refs = node->wn_refs;
7583 node->wn_refs = 1;
7584 }
Bram Moolenaardc781a72010-07-11 18:01:39 +02007585 if (prev != NULL)
7586 *prev = np;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007587 np->wn_sibling = node;
7588 node = np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007589 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007590
Bram Moolenaar51485f02005-06-04 21:55:20 +00007591 if (word[i] == NUL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007592 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007593 node->wn_flags = flags;
7594 node->wn_region |= region;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007595 node->wn_affixID = affixID;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007596 break;
Bram Moolenaar63d5a1e2005-04-19 21:30:25 +00007597 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007598 prev = &node->wn_child;
7599 node = *prev;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007600 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007601#ifdef SPELL_PRINTTREE
7602 smsg("Added \"%s\"", word);
7603 spell_print_tree(root->wn_sibling);
7604#endif
7605
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007606 /* count nr of words added since last message */
7607 ++spin->si_msg_count;
7608
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007609 if (spin->si_compress_cnt > 1)
7610 {
7611 if (--spin->si_compress_cnt == 1)
7612 /* Did enough words to lower the block count limit. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007613 spin->si_blocks_cnt += compress_inc;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007614 }
7615
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007616 /*
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007617 * When we have allocated lots of memory we need to compress the word tree
7618 * to free up some room. But compression is slow, and we might actually
7619 * need that room, thus only compress in the following situations:
7620 * 1. When not compressed before (si_compress_cnt == 0): when using
Bram Moolenaar5195e452005-08-19 20:32:47 +00007621 * "compress_start" blocks.
7622 * 2. When compressed before and used "compress_inc" blocks before
7623 * adding "compress_added" words (si_compress_cnt > 1).
7624 * 3. When compressed before, added "compress_added" words
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007625 * (si_compress_cnt == 1) and the number of free nodes drops below the
7626 * maximum word length.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007627 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007628#ifndef SPELL_PRINTTREE
7629 if (spin->si_compress_cnt == 1
7630 ? spin->si_free_count < MAXWLEN
Bram Moolenaar5195e452005-08-19 20:32:47 +00007631 : spin->si_blocks_cnt >= compress_start)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007632#endif
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007633 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007634 /* Decrement the block counter. The effect is that we compress again
Bram Moolenaar5195e452005-08-19 20:32:47 +00007635 * when the freed up room has been used and another "compress_inc"
7636 * blocks have been allocated. Unless "compress_added" words have
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007637 * been added, then the limit is put back again. */
Bram Moolenaar5195e452005-08-19 20:32:47 +00007638 spin->si_blocks_cnt -= compress_inc;
7639 spin->si_compress_cnt = compress_added;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007640
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007641 if (spin->si_verbose)
7642 {
7643 msg_start();
7644 msg_puts((char_u *)_(msg_compressing));
7645 msg_clr_eos();
7646 msg_didout = FALSE;
7647 msg_col = 0;
7648 out_flush();
7649 }
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007650
7651 /* Compress both trees. Either they both have many nodes, which makes
7652 * compression useful, or one of them is small, which means
Bram Moolenaar4770d092006-01-12 23:22:24 +00007653 * compression goes fast. But when filling the souldfold word tree
7654 * there is no keep-case tree. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007655 wordtree_compress(spin, spin->si_foldroot);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007656 if (affixID >= 0)
7657 wordtree_compress(spin, spin->si_keeproot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007658 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007659
7660 return OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007661}
7662
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007663/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007664 * Check the 'mkspellmem' option. Return FAIL if it's wrong.
7665 * Sets "sps_flags".
7666 */
7667 int
7668spell_check_msm()
7669{
7670 char_u *p = p_msm;
7671 long start = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007672 long incr = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007673 long added = 0;
7674
7675 if (!VIM_ISDIGIT(*p))
7676 return FAIL;
7677 /* block count = (value * 1024) / SBLOCKSIZE (but avoid overflow)*/
7678 start = (getdigits(&p) * 10) / (SBLOCKSIZE / 102);
7679 if (*p != ',')
7680 return FAIL;
7681 ++p;
7682 if (!VIM_ISDIGIT(*p))
7683 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007684 incr = (getdigits(&p) * 102) / (SBLOCKSIZE / 10);
Bram Moolenaar5195e452005-08-19 20:32:47 +00007685 if (*p != ',')
7686 return FAIL;
7687 ++p;
7688 if (!VIM_ISDIGIT(*p))
7689 return FAIL;
7690 added = getdigits(&p) * 1024;
7691 if (*p != NUL)
7692 return FAIL;
7693
Bram Moolenaar89d40322006-08-29 15:30:07 +00007694 if (start == 0 || incr == 0 || added == 0 || incr > start)
Bram Moolenaar5195e452005-08-19 20:32:47 +00007695 return FAIL;
7696
7697 compress_start = start;
Bram Moolenaar89d40322006-08-29 15:30:07 +00007698 compress_inc = incr;
Bram Moolenaar5195e452005-08-19 20:32:47 +00007699 compress_added = added;
7700 return OK;
7701}
7702
7703
7704/*
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007705 * Get a wordnode_T, either from the list of previously freed nodes or
7706 * allocate a new one.
7707 */
7708 static wordnode_T *
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007709get_wordnode(spin)
7710 spellinfo_T *spin;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007711{
7712 wordnode_T *n;
7713
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007714 if (spin->si_first_free == NULL)
7715 n = (wordnode_T *)getroom(spin, sizeof(wordnode_T), TRUE);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007716 else
7717 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007718 n = spin->si_first_free;
7719 spin->si_first_free = n->wn_child;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007720 vim_memset(n, 0, sizeof(wordnode_T));
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007721 --spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007722 }
7723#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007724 n->wn_nr = ++spin->si_wordnode_nr;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007725#endif
7726 return n;
7727}
7728
7729/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007730 * Decrement the reference count on a node (which is the head of a list of
7731 * siblings). If the reference count becomes zero free the node and its
7732 * siblings.
Bram Moolenaar4770d092006-01-12 23:22:24 +00007733 * Returns the number of nodes actually freed.
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007734 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007735 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007736deref_wordnode(spin, node)
7737 spellinfo_T *spin;
7738 wordnode_T *node;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007739{
Bram Moolenaar4770d092006-01-12 23:22:24 +00007740 wordnode_T *np;
7741 int cnt = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007742
7743 if (--node->wn_refs == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007744 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007745 for (np = node; np != NULL; np = np->wn_sibling)
7746 {
7747 if (np->wn_child != NULL)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007748 cnt += deref_wordnode(spin, np->wn_child);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007749 free_wordnode(spin, np);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007750 ++cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007751 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007752 ++cnt; /* length field */
7753 }
7754 return cnt;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007755}
7756
7757/*
7758 * Free a wordnode_T for re-use later.
7759 * Only the "wn_child" field becomes invalid.
7760 */
7761 static void
7762free_wordnode(spin, n)
7763 spellinfo_T *spin;
7764 wordnode_T *n;
7765{
7766 n->wn_child = spin->si_first_free;
7767 spin->si_first_free = n;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007768 ++spin->si_free_count;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007769}
7770
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007771/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00007772 * Compress a tree: find tails that are identical and can be shared.
7773 */
7774 static void
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007775wordtree_compress(spin, root)
Bram Moolenaarb765d632005-06-07 21:00:02 +00007776 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007777 wordnode_T *root;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007778{
7779 hashtab_T ht;
7780 int n;
7781 int tot = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007782 int perc;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007783
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007784 /* Skip the root itself, it's not actually used. The first sibling is the
7785 * start of the tree. */
7786 if (root->wn_sibling != NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007787 {
7788 hash_init(&ht);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007789 n = node_compress(spin, root->wn_sibling, &ht, &tot);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007790
7791#ifndef SPELL_PRINTTREE
Bram Moolenaarb765d632005-06-07 21:00:02 +00007792 if (spin->si_verbose || p_verbose > 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007793#endif
Bram Moolenaarb765d632005-06-07 21:00:02 +00007794 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007795 if (tot > 1000000)
7796 perc = (tot - n) / (tot / 100);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007797 else if (tot == 0)
7798 perc = 0;
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007799 else
7800 perc = (tot - n) * 100 / tot;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007801 vim_snprintf((char *)IObuff, IOSIZE,
7802 _("Compressed %d of %d nodes; %d (%d%%) remaining"),
7803 n, tot, tot - n, perc);
7804 spell_message(spin, IObuff);
Bram Moolenaarb765d632005-06-07 21:00:02 +00007805 }
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007806#ifdef SPELL_PRINTTREE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007807 spell_print_tree(root->wn_sibling);
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007808#endif
Bram Moolenaar51485f02005-06-04 21:55:20 +00007809 hash_clear(&ht);
7810 }
7811}
7812
7813/*
7814 * Compress a node, its siblings and its children, depth first.
7815 * Returns the number of compressed nodes.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007816 */
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00007817 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007818node_compress(spin, node, ht, tot)
7819 spellinfo_T *spin;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007820 wordnode_T *node;
7821 hashtab_T *ht;
7822 int *tot; /* total count of nodes before compressing,
7823 incremented while going through the tree */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007824{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007825 wordnode_T *np;
7826 wordnode_T *tp;
7827 wordnode_T *child;
7828 hash_T hash;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007829 hashitem_T *hi;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007830 int len = 0;
7831 unsigned nr, n;
7832 int compressed = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007833
Bram Moolenaar51485f02005-06-04 21:55:20 +00007834 /*
7835 * Go through the list of siblings. Compress each child and then try
7836 * finding an identical child to replace it.
7837 * Note that with "child" we mean not just the node that is pointed to,
Bram Moolenaar4770d092006-01-12 23:22:24 +00007838 * but the whole list of siblings of which the child node is the first.
Bram Moolenaar51485f02005-06-04 21:55:20 +00007839 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007840 for (np = node; np != NULL && !got_int; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007841 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007842 ++len;
7843 if ((child = np->wn_child) != NULL)
7844 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007845 /* Compress the child first. This fills hashkey. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007846 compressed += node_compress(spin, child, ht, tot);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007847
7848 /* Try to find an identical child. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007849 hash = hash_hash(child->wn_u1.hashkey);
7850 hi = hash_lookup(ht, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007851 if (!HASHITEM_EMPTY(hi))
7852 {
Bram Moolenaar4770d092006-01-12 23:22:24 +00007853 /* There are children we encountered before with a hash value
7854 * identical to the current child. Now check if there is one
7855 * that is really identical. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007856 for (tp = HI2WN(hi); tp != NULL; tp = tp->wn_u2.next)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007857 if (node_equal(child, tp))
7858 {
7859 /* Found one! Now use that child in place of the
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00007860 * current one. This means the current child and all
7861 * its siblings is unlinked from the tree. */
7862 ++tp->wn_refs;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007863 compressed += deref_wordnode(spin, child);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007864 np->wn_child = tp;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007865 break;
7866 }
7867 if (tp == NULL)
7868 {
7869 /* No other child with this hash value equals the child of
7870 * the node, add it to the linked list after the first
7871 * item. */
7872 tp = HI2WN(hi);
Bram Moolenaar0c405862005-06-22 22:26:26 +00007873 child->wn_u2.next = tp->wn_u2.next;
7874 tp->wn_u2.next = child;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007875 }
7876 }
7877 else
7878 /* No other child has this hash value, add it to the
7879 * hashtable. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007880 hash_add_item(ht, hi, child->wn_u1.hashkey, hash);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007881 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007882 }
Bram Moolenaar4770d092006-01-12 23:22:24 +00007883 *tot += len + 1; /* add one for the node that stores the length */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007884
7885 /*
7886 * Make a hash key for the node and its siblings, so that we can quickly
7887 * find a lookalike node. This must be done after compressing the sibling
7888 * list, otherwise the hash key would become invalid by the compression.
7889 */
Bram Moolenaar0c405862005-06-22 22:26:26 +00007890 node->wn_u1.hashkey[0] = len;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007891 nr = 0;
7892 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007893 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00007894 if (np->wn_byte == NUL)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007895 /* end node: use wn_flags, wn_region and wn_affixID */
7896 n = np->wn_flags + (np->wn_region << 8) + (np->wn_affixID << 16);
Bram Moolenaar51485f02005-06-04 21:55:20 +00007897 else
7898 /* byte node: use the byte value and the child pointer */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007899 n = (unsigned)(np->wn_byte + ((long_u)np->wn_child << 8));
Bram Moolenaar51485f02005-06-04 21:55:20 +00007900 nr = nr * 101 + n;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007901 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00007902
7903 /* Avoid NUL bytes, it terminates the hash key. */
7904 n = nr & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007905 node->wn_u1.hashkey[1] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007906 n = (nr >> 8) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007907 node->wn_u1.hashkey[2] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007908 n = (nr >> 16) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007909 node->wn_u1.hashkey[3] = n == 0 ? 1 : n;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007910 n = (nr >> 24) & 0xff;
Bram Moolenaar0c405862005-06-22 22:26:26 +00007911 node->wn_u1.hashkey[4] = n == 0 ? 1 : n;
7912 node->wn_u1.hashkey[5] = NUL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007913
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00007914 /* Check for CTRL-C pressed now and then. */
7915 fast_breakcheck();
7916
Bram Moolenaar51485f02005-06-04 21:55:20 +00007917 return compressed;
7918}
7919
7920/*
7921 * Return TRUE when two nodes have identical siblings and children.
7922 */
7923 static int
7924node_equal(n1, n2)
7925 wordnode_T *n1;
7926 wordnode_T *n2;
7927{
7928 wordnode_T *p1;
7929 wordnode_T *p2;
7930
7931 for (p1 = n1, p2 = n2; p1 != NULL && p2 != NULL;
7932 p1 = p1->wn_sibling, p2 = p2->wn_sibling)
7933 if (p1->wn_byte != p2->wn_byte
7934 || (p1->wn_byte == NUL
7935 ? (p1->wn_flags != p2->wn_flags
Bram Moolenaar1d73c882005-06-19 22:48:47 +00007936 || p1->wn_region != p2->wn_region
Bram Moolenaarae5bce12005-08-15 21:41:48 +00007937 || p1->wn_affixID != p2->wn_affixID)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007938 : (p1->wn_child != p2->wn_child)))
7939 break;
7940
7941 return p1 == NULL && p2 == NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007942}
7943
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007944static int
7945#ifdef __BORLANDC__
7946_RTLENTRYF
7947#endif
7948rep_compare __ARGS((const void *s1, const void *s2));
7949
7950/*
7951 * Function given to qsort() to sort the REP items on "from" string.
7952 */
7953 static int
7954#ifdef __BORLANDC__
7955_RTLENTRYF
7956#endif
7957rep_compare(s1, s2)
7958 const void *s1;
7959 const void *s2;
7960{
7961 fromto_T *p1 = (fromto_T *)s1;
7962 fromto_T *p2 = (fromto_T *)s2;
7963
7964 return STRCMP(p1->ft_from, p2->ft_from);
7965}
7966
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007967/*
Bram Moolenaar5195e452005-08-19 20:32:47 +00007968 * Write the Vim .spl file "fname".
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007969 * Return FAIL or OK;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007970 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007971 static int
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007972write_vim_spell(spin, fname)
Bram Moolenaar51485f02005-06-04 21:55:20 +00007973 spellinfo_T *spin;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007974 char_u *fname;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007975{
Bram Moolenaar51485f02005-06-04 21:55:20 +00007976 FILE *fd;
7977 int regionmask;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007978 int round;
Bram Moolenaar51485f02005-06-04 21:55:20 +00007979 wordnode_T *tree;
7980 int nodecount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007981 int i;
7982 int l;
7983 garray_T *gap;
7984 fromto_T *ftp;
7985 char_u *p;
7986 int rr;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007987 int retval = OK;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00007988 size_t fwv = 1; /* collect return value of fwrite() to avoid
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00007989 warnings from picky compiler */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007990
Bram Moolenaarb765d632005-06-07 21:00:02 +00007991 fd = mch_fopen((char *)fname, "w");
Bram Moolenaar51485f02005-06-04 21:55:20 +00007992 if (fd == NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007993 {
7994 EMSG2(_(e_notopen), fname);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007995 return FAIL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007996 }
7997
Bram Moolenaar5195e452005-08-19 20:32:47 +00007998 /* <HEADER>: <fileID> <versionnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00007999 /* <fileID> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008000 fwv &= fwrite(VIMSPELLMAGIC, VIMSPELLMAGICL, (size_t)1, fd);
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008001 if (fwv != (size_t)1)
8002 /* Catch first write error, don't try writing more. */
8003 goto theend;
8004
Bram Moolenaar5195e452005-08-19 20:32:47 +00008005 putc(VIMSPELLVERSION, fd); /* <versionnr> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008006
Bram Moolenaar5195e452005-08-19 20:32:47 +00008007 /*
8008 * <SECTIONS>: <section> ... <sectionend>
8009 */
8010
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008011 /* SN_INFO: <infotext> */
8012 if (spin->si_info != NULL)
8013 {
8014 putc(SN_INFO, fd); /* <sectionID> */
8015 putc(0, fd); /* <sectionflags> */
8016
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008017 i = (int)STRLEN(spin->si_info);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008018 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008019 fwv &= fwrite(spin->si_info, (size_t)i, (size_t)1, fd); /* <infotext> */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008020 }
8021
Bram Moolenaar5195e452005-08-19 20:32:47 +00008022 /* SN_REGION: <regionname> ...
8023 * Write the region names only if there is more than one. */
Bram Moolenaar3982c542005-06-08 21:56:31 +00008024 if (spin->si_region_count > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008025 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008026 putc(SN_REGION, fd); /* <sectionID> */
8027 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8028 l = spin->si_region_count * 2;
8029 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008030 fwv &= fwrite(spin->si_region_name, (size_t)l, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008031 /* <regionname> ... */
Bram Moolenaar3982c542005-06-08 21:56:31 +00008032 regionmask = (1 << spin->si_region_count) - 1;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008033 }
8034 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00008035 regionmask = 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008036
Bram Moolenaar5195e452005-08-19 20:32:47 +00008037 /* SN_CHARFLAGS: <charflagslen> <charflags> <folcharslen> <folchars>
8038 *
8039 * The table with character flags and the table for case folding.
8040 * This makes sure the same characters are recognized as word characters
8041 * when generating an when using a spell file.
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008042 * Skip this for ASCII, the table may conflict with the one used for
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008043 * 'encoding'.
8044 * Also skip this for an .add.spl file, the main spell file must contain
8045 * the table (avoids that it conflicts). File is shorter too.
8046 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008047 if (!spin->si_ascii && !spin->si_add)
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008048 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008049 char_u folchars[128 * 8];
8050 int flags;
8051
Bram Moolenaard12a1322005-08-21 22:08:24 +00008052 putc(SN_CHARFLAGS, fd); /* <sectionID> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008053 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8054
8055 /* Form the <folchars> string first, we need to know its length. */
8056 l = 0;
8057 for (i = 128; i < 256; ++i)
8058 {
8059#ifdef FEAT_MBYTE
8060 if (has_mbyte)
8061 l += mb_char2bytes(spelltab.st_fold[i], folchars + l);
8062 else
8063#endif
8064 folchars[l++] = spelltab.st_fold[i];
8065 }
8066 put_bytes(fd, (long_u)(1 + 128 + 2 + l), 4); /* <sectionlen> */
8067
8068 fputc(128, fd); /* <charflagslen> */
8069 for (i = 128; i < 256; ++i)
8070 {
8071 flags = 0;
8072 if (spelltab.st_isw[i])
8073 flags |= CF_WORD;
8074 if (spelltab.st_isu[i])
8075 flags |= CF_UPPER;
8076 fputc(flags, fd); /* <charflags> */
8077 }
8078
8079 put_bytes(fd, (long_u)l, 2); /* <folcharslen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008080 fwv &= fwrite(folchars, (size_t)l, (size_t)1, fd); /* <folchars> */
Bram Moolenaar6f3058f2005-04-24 21:58:05 +00008081 }
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008082
Bram Moolenaar5195e452005-08-19 20:32:47 +00008083 /* SN_MIDWORD: <midword> */
8084 if (spin->si_midword != NULL)
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008085 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008086 putc(SN_MIDWORD, fd); /* <sectionID> */
8087 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8088
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008089 i = (int)STRLEN(spin->si_midword);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008090 put_bytes(fd, (long_u)i, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008091 fwv &= fwrite(spin->si_midword, (size_t)i, (size_t)1, fd);
8092 /* <midword> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008093 }
8094
Bram Moolenaar5195e452005-08-19 20:32:47 +00008095 /* SN_PREFCOND: <prefcondcnt> <prefcond> ... */
8096 if (spin->si_prefcond.ga_len > 0)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008097 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008098 putc(SN_PREFCOND, fd); /* <sectionID> */
8099 putc(SNF_REQUIRED, fd); /* <sectionflags> */
8100
8101 l = write_spell_prefcond(NULL, &spin->si_prefcond);
8102 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8103
8104 write_spell_prefcond(fd, &spin->si_prefcond);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008105 }
8106
Bram Moolenaar5195e452005-08-19 20:32:47 +00008107 /* SN_REP: <repcount> <rep> ...
Bram Moolenaar4770d092006-01-12 23:22:24 +00008108 * SN_SAL: <salflags> <salcount> <sal> ...
8109 * SN_REPSAL: <repcount> <rep> ... */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008110
Bram Moolenaar5195e452005-08-19 20:32:47 +00008111 /* round 1: SN_REP section
Bram Moolenaar4770d092006-01-12 23:22:24 +00008112 * round 2: SN_SAL section (unless SN_SOFO is used)
8113 * round 3: SN_REPSAL section */
8114 for (round = 1; round <= 3; ++round)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008115 {
8116 if (round == 1)
8117 gap = &spin->si_rep;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008118 else if (round == 2)
8119 {
8120 /* Don't write SN_SAL when using a SN_SOFO section */
8121 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8122 continue;
8123 gap = &spin->si_sal;
Bram Moolenaar5195e452005-08-19 20:32:47 +00008124 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008125 else
Bram Moolenaar4770d092006-01-12 23:22:24 +00008126 gap = &spin->si_repsal;
8127
8128 /* Don't write the section if there are no items. */
8129 if (gap->ga_len == 0)
8130 continue;
8131
8132 /* Sort the REP/REPSAL items. */
8133 if (round != 2)
8134 qsort(gap->ga_data, (size_t)gap->ga_len,
8135 sizeof(fromto_T), rep_compare);
8136
8137 i = round == 1 ? SN_REP : (round == 2 ? SN_SAL : SN_REPSAL);
8138 putc(i, fd); /* <sectionID> */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008139
Bram Moolenaar5195e452005-08-19 20:32:47 +00008140 /* This is for making suggestions, section is not required. */
8141 putc(0, fd); /* <sectionflags> */
8142
8143 /* Compute the length of what follows. */
8144 l = 2; /* count <repcount> or <salcount> */
8145 for (i = 0; i < gap->ga_len; ++i)
8146 {
8147 ftp = &((fromto_T *)gap->ga_data)[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008148 l += 1 + (int)STRLEN(ftp->ft_from); /* count <*fromlen> and <*from> */
8149 l += 1 + (int)STRLEN(ftp->ft_to); /* count <*tolen> and <*to> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008150 }
8151 if (round == 2)
8152 ++l; /* count <salflags> */
8153 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
8154
8155 if (round == 2)
8156 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008157 i = 0;
8158 if (spin->si_followup)
8159 i |= SAL_F0LLOWUP;
8160 if (spin->si_collapse)
8161 i |= SAL_COLLAPSE;
8162 if (spin->si_rem_accents)
8163 i |= SAL_REM_ACCENTS;
8164 putc(i, fd); /* <salflags> */
8165 }
8166
8167 put_bytes(fd, (long_u)gap->ga_len, 2); /* <repcount> or <salcount> */
8168 for (i = 0; i < gap->ga_len; ++i)
8169 {
8170 /* <rep> : <repfromlen> <repfrom> <reptolen> <repto> */
8171 /* <sal> : <salfromlen> <salfrom> <saltolen> <salto> */
8172 ftp = &((fromto_T *)gap->ga_data)[i];
8173 for (rr = 1; rr <= 2; ++rr)
8174 {
8175 p = rr == 1 ? ftp->ft_from : ftp->ft_to;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008176 l = (int)STRLEN(p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008177 putc(l, fd);
Bram Moolenaar9bf13612008-11-29 19:11:40 +00008178 if (l > 0)
8179 fwv &= fwrite(p, l, (size_t)1, fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008180 }
8181 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008182
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008183 }
8184
Bram Moolenaar5195e452005-08-19 20:32:47 +00008185 /* SN_SOFO: <sofofromlen> <sofofrom> <sofotolen> <sofoto>
8186 * This is for making suggestions, section is not required. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008187 if (spin->si_sofofr != NULL && spin->si_sofoto != NULL)
8188 {
Bram Moolenaar5195e452005-08-19 20:32:47 +00008189 putc(SN_SOFO, fd); /* <sectionID> */
8190 putc(0, fd); /* <sectionflags> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008191
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008192 l = (int)STRLEN(spin->si_sofofr);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008193 put_bytes(fd, (long_u)(l + STRLEN(spin->si_sofoto) + 4), 4);
8194 /* <sectionlen> */
8195
8196 put_bytes(fd, (long_u)l, 2); /* <sofofromlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008197 fwv &= fwrite(spin->si_sofofr, l, (size_t)1, fd); /* <sofofrom> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008198
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008199 l = (int)STRLEN(spin->si_sofoto);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008200 put_bytes(fd, (long_u)l, 2); /* <sofotolen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008201 fwv &= fwrite(spin->si_sofoto, l, (size_t)1, fd); /* <sofoto> */
Bram Moolenaar42eeac32005-06-29 22:40:58 +00008202 }
8203
Bram Moolenaar4770d092006-01-12 23:22:24 +00008204 /* SN_WORDS: <word> ...
8205 * This is for making suggestions, section is not required. */
8206 if (spin->si_commonwords.ht_used > 0)
8207 {
8208 putc(SN_WORDS, fd); /* <sectionID> */
8209 putc(0, fd); /* <sectionflags> */
8210
8211 /* round 1: count the bytes
8212 * round 2: write the bytes */
8213 for (round = 1; round <= 2; ++round)
8214 {
8215 int todo;
8216 int len = 0;
8217 hashitem_T *hi;
8218
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008219 todo = (int)spin->si_commonwords.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008220 for (hi = spin->si_commonwords.ht_array; todo > 0; ++hi)
8221 if (!HASHITEM_EMPTY(hi))
8222 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008223 l = (int)STRLEN(hi->hi_key) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008224 len += l;
8225 if (round == 2) /* <word> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008226 fwv &= fwrite(hi->hi_key, (size_t)l, (size_t)1, fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008227 --todo;
8228 }
8229 if (round == 1)
8230 put_bytes(fd, (long_u)len, 4); /* <sectionlen> */
8231 }
8232 }
8233
Bram Moolenaar5195e452005-08-19 20:32:47 +00008234 /* SN_MAP: <mapstr>
8235 * This is for making suggestions, section is not required. */
8236 if (spin->si_map.ga_len > 0)
8237 {
8238 putc(SN_MAP, fd); /* <sectionID> */
8239 putc(0, fd); /* <sectionflags> */
8240 l = spin->si_map.ga_len;
8241 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008242 fwv &= fwrite(spin->si_map.ga_data, (size_t)l, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008243 /* <mapstr> */
8244 }
8245
Bram Moolenaar4770d092006-01-12 23:22:24 +00008246 /* SN_SUGFILE: <timestamp>
8247 * This is used to notify that a .sug file may be available and at the
8248 * same time allows for checking that a .sug file that is found matches
8249 * with this .spl file. That's because the word numbers must be exactly
8250 * right. */
8251 if (!spin->si_nosugfile
8252 && (spin->si_sal.ga_len > 0
8253 || (spin->si_sofofr != NULL && spin->si_sofoto != NULL)))
8254 {
8255 putc(SN_SUGFILE, fd); /* <sectionID> */
8256 putc(0, fd); /* <sectionflags> */
8257 put_bytes(fd, (long_u)8, 4); /* <sectionlen> */
8258
8259 /* Set si_sugtime and write it to the file. */
8260 spin->si_sugtime = time(NULL);
Bram Moolenaarcdf04202010-05-29 15:11:47 +02008261 put_time(fd, spin->si_sugtime); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008262 }
8263
Bram Moolenaare1438bb2006-03-01 22:01:55 +00008264 /* SN_NOSPLITSUGS: nothing
8265 * This is used to notify that no suggestions with word splits are to be
8266 * made. */
8267 if (spin->si_nosplitsugs)
8268 {
8269 putc(SN_NOSPLITSUGS, fd); /* <sectionID> */
8270 putc(0, fd); /* <sectionflags> */
8271 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8272 }
8273
Bram Moolenaar5195e452005-08-19 20:32:47 +00008274 /* SN_COMPOUND: compound info.
8275 * We don't mark it required, when not supported all compound words will
8276 * be bad words. */
8277 if (spin->si_compflags != NULL)
8278 {
8279 putc(SN_COMPOUND, fd); /* <sectionID> */
8280 putc(0, fd); /* <sectionflags> */
8281
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008282 l = (int)STRLEN(spin->si_compflags);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008283 for (i = 0; i < spin->si_comppat.ga_len; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008284 l += (int)STRLEN(((char_u **)(spin->si_comppat.ga_data))[i]) + 1;
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008285 put_bytes(fd, (long_u)(l + 7), 4); /* <sectionlen> */
8286
Bram Moolenaar5195e452005-08-19 20:32:47 +00008287 putc(spin->si_compmax, fd); /* <compmax> */
8288 putc(spin->si_compminlen, fd); /* <compminlen> */
8289 putc(spin->si_compsylmax, fd); /* <compsylmax> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008290 putc(0, fd); /* for Vim 7.0b compatibility */
8291 putc(spin->si_compoptions, fd); /* <compoptions> */
8292 put_bytes(fd, (long_u)spin->si_comppat.ga_len, 2);
8293 /* <comppatcount> */
8294 for (i = 0; i < spin->si_comppat.ga_len; ++i)
8295 {
8296 p = ((char_u **)(spin->si_comppat.ga_data))[i];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008297 putc((int)STRLEN(p), fd); /* <comppatlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008298 fwv &= fwrite(p, (size_t)STRLEN(p), (size_t)1, fd);
8299 /* <comppattext> */
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008300 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00008301 /* <compflags> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008302 fwv &= fwrite(spin->si_compflags, (size_t)STRLEN(spin->si_compflags),
Bram Moolenaar899dddf2006-03-26 21:06:50 +00008303 (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008304 }
8305
Bram Moolenaar78622822005-08-23 21:00:13 +00008306 /* SN_NOBREAK: NOBREAK flag */
8307 if (spin->si_nobreak)
8308 {
8309 putc(SN_NOBREAK, fd); /* <sectionID> */
8310 putc(0, fd); /* <sectionflags> */
8311
Bram Moolenaarf711faf2007-05-10 16:48:19 +00008312 /* It's empty, the presence of the section flags the feature. */
Bram Moolenaar78622822005-08-23 21:00:13 +00008313 put_bytes(fd, (long_u)0, 4); /* <sectionlen> */
8314 }
8315
Bram Moolenaar5195e452005-08-19 20:32:47 +00008316 /* SN_SYLLABLE: syllable info.
8317 * We don't mark it required, when not supported syllables will not be
8318 * counted. */
8319 if (spin->si_syllable != NULL)
8320 {
8321 putc(SN_SYLLABLE, fd); /* <sectionID> */
8322 putc(0, fd); /* <sectionflags> */
8323
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008324 l = (int)STRLEN(spin->si_syllable);
Bram Moolenaar5195e452005-08-19 20:32:47 +00008325 put_bytes(fd, (long_u)l, 4); /* <sectionlen> */
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008326 fwv &= fwrite(spin->si_syllable, (size_t)l, (size_t)1, fd);
8327 /* <syllable> */
Bram Moolenaar5195e452005-08-19 20:32:47 +00008328 }
8329
8330 /* end of <SECTIONS> */
8331 putc(SN_END, fd); /* <sectionend> */
8332
Bram Moolenaar50cde822005-06-05 21:54:54 +00008333
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008334 /*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008335 * <LWORDTREE> <KWORDTREE> <PREFIXTREE>
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008336 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008337 spin->si_memtot = 0;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008338 for (round = 1; round <= 3; ++round)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008339 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008340 if (round == 1)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008341 tree = spin->si_foldroot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008342 else if (round == 2)
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008343 tree = spin->si_keeproot->wn_sibling;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008344 else
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00008345 tree = spin->si_prefroot->wn_sibling;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008346
Bram Moolenaar0c405862005-06-22 22:26:26 +00008347 /* Clear the index and wnode fields in the tree. */
8348 clear_node(tree);
8349
Bram Moolenaar51485f02005-06-04 21:55:20 +00008350 /* Count the number of nodes. Needed to be able to allocate the
Bram Moolenaar0c405862005-06-22 22:26:26 +00008351 * memory when reading the nodes. Also fills in index for shared
Bram Moolenaar51485f02005-06-04 21:55:20 +00008352 * nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008353 nodecount = put_node(NULL, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008354
Bram Moolenaar51485f02005-06-04 21:55:20 +00008355 /* number of nodes in 4 bytes */
8356 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
Bram Moolenaar50cde822005-06-05 21:54:54 +00008357 spin->si_memtot += nodecount + nodecount * sizeof(int);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008358
Bram Moolenaar51485f02005-06-04 21:55:20 +00008359 /* Write the nodes. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008360 (void)put_node(fd, tree, 0, regionmask, round == 3);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008361 }
8362
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008363 /* Write another byte to check for errors (file system full). */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008364 if (putc(0, fd) == EOF)
8365 retval = FAIL;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008366theend:
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008367 if (fclose(fd) == EOF)
8368 retval = FAIL;
8369
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00008370 if (fwv != (size_t)1)
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00008371 retval = FAIL;
8372 if (retval == FAIL)
8373 EMSG(_(e_write));
8374
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00008375 return retval;
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008376}
8377
8378/*
Bram Moolenaar0c405862005-06-22 22:26:26 +00008379 * Clear the index and wnode fields of "node", it siblings and its
8380 * children. This is needed because they are a union with other items to save
8381 * space.
8382 */
8383 static void
8384clear_node(node)
8385 wordnode_T *node;
8386{
8387 wordnode_T *np;
8388
8389 if (node != NULL)
8390 for (np = node; np != NULL; np = np->wn_sibling)
8391 {
8392 np->wn_u1.index = 0;
8393 np->wn_u2.wnode = NULL;
8394
8395 if (np->wn_byte != NUL)
8396 clear_node(np->wn_child);
8397 }
8398}
8399
8400
8401/*
Bram Moolenaar51485f02005-06-04 21:55:20 +00008402 * Dump a word tree at node "node".
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008403 *
Bram Moolenaar51485f02005-06-04 21:55:20 +00008404 * This first writes the list of possible bytes (siblings). Then for each
8405 * byte recursively write the children.
8406 *
Bram Moolenaar4770d092006-01-12 23:22:24 +00008407 * NOTE: The code here must match the code in read_tree_node(), since
8408 * assumptions are made about the indexes (so that we don't have to write them
8409 * in the file).
Bram Moolenaar51485f02005-06-04 21:55:20 +00008410 *
8411 * Returns the number of nodes used.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008412 */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008413 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00008414put_node(fd, node, idx, regionmask, prefixtree)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008415 FILE *fd; /* NULL when only counting */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008416 wordnode_T *node;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008417 int idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008418 int regionmask;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008419 int prefixtree; /* TRUE for PREFIXTREE */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008420{
Bram Moolenaar89d40322006-08-29 15:30:07 +00008421 int newindex = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008422 int siblingcount = 0;
8423 wordnode_T *np;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008424 int flags;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008425
Bram Moolenaar51485f02005-06-04 21:55:20 +00008426 /* If "node" is zero the tree is empty. */
8427 if (node == NULL)
8428 return 0;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008429
Bram Moolenaar51485f02005-06-04 21:55:20 +00008430 /* Store the index where this node is written. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00008431 node->wn_u1.index = idx;
Bram Moolenaar51485f02005-06-04 21:55:20 +00008432
8433 /* Count the number of siblings. */
8434 for (np = node; np != NULL; np = np->wn_sibling)
8435 ++siblingcount;
8436
8437 /* Write the sibling count. */
8438 if (fd != NULL)
8439 putc(siblingcount, fd); /* <siblingcount> */
8440
8441 /* Write each sibling byte and optionally extra info. */
8442 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008443 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008444 if (np->wn_byte == 0)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008445 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00008446 if (fd != NULL)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008447 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008448 /* For a NUL byte (end of word) write the flags etc. */
8449 if (prefixtree)
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008450 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008451 /* In PREFIXTREE write the required affixID and the
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008452 * associated condition nr (stored in wn_region). The
8453 * byte value is misused to store the "rare" and "not
8454 * combining" flags */
Bram Moolenaar53805d12005-08-01 07:08:33 +00008455 if (np->wn_flags == (short_u)PFX_FLAGS)
8456 putc(BY_NOFLAGS, fd); /* <byte> */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00008457 else
Bram Moolenaar53805d12005-08-01 07:08:33 +00008458 {
8459 putc(BY_FLAGS, fd); /* <byte> */
8460 putc(np->wn_flags, fd); /* <pflags> */
8461 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008462 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008463 put_bytes(fd, (long_u)np->wn_region, 2); /* <prefcondnr> */
Bram Moolenaar51485f02005-06-04 21:55:20 +00008464 }
8465 else
8466 {
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008467 /* For word trees we write the flag/region items. */
8468 flags = np->wn_flags;
8469 if (regionmask != 0 && np->wn_region != regionmask)
8470 flags |= WF_REGION;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008471 if (np->wn_affixID != 0)
8472 flags |= WF_AFX;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008473 if (flags == 0)
8474 {
8475 /* word without flags or region */
8476 putc(BY_NOFLAGS, fd); /* <byte> */
8477 }
8478 else
8479 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00008480 if (np->wn_flags >= 0x100)
8481 {
8482 putc(BY_FLAGS2, fd); /* <byte> */
8483 putc(flags, fd); /* <flags> */
8484 putc((unsigned)flags >> 8, fd); /* <flags2> */
8485 }
8486 else
8487 {
8488 putc(BY_FLAGS, fd); /* <byte> */
8489 putc(flags, fd); /* <flags> */
8490 }
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008491 if (flags & WF_REGION)
8492 putc(np->wn_region, fd); /* <region> */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00008493 if (flags & WF_AFX)
8494 putc(np->wn_affixID, fd); /* <affixID> */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008495 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008496 }
8497 }
Bram Moolenaar2cf8b302005-04-20 19:37:22 +00008498 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008499 else
8500 {
Bram Moolenaar0c405862005-06-22 22:26:26 +00008501 if (np->wn_child->wn_u1.index != 0
8502 && np->wn_child->wn_u2.wnode != node)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008503 {
8504 /* The child is written elsewhere, write the reference. */
8505 if (fd != NULL)
8506 {
8507 putc(BY_INDEX, fd); /* <byte> */
8508 /* <nodeidx> */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008509 put_bytes(fd, (long_u)np->wn_child->wn_u1.index, 3);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008510 }
8511 }
Bram Moolenaar0c405862005-06-22 22:26:26 +00008512 else if (np->wn_child->wn_u2.wnode == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00008513 /* We will write the child below and give it an index. */
Bram Moolenaar0c405862005-06-22 22:26:26 +00008514 np->wn_child->wn_u2.wnode = node;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00008515
Bram Moolenaar51485f02005-06-04 21:55:20 +00008516 if (fd != NULL)
8517 if (putc(np->wn_byte, fd) == EOF) /* <byte> or <xbyte> */
8518 {
8519 EMSG(_(e_write));
8520 return 0;
8521 }
8522 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008523 }
Bram Moolenaar51485f02005-06-04 21:55:20 +00008524
8525 /* Space used in the array when reading: one for each sibling and one for
8526 * the count. */
8527 newindex += siblingcount + 1;
8528
8529 /* Recursively dump the children of each sibling. */
8530 for (np = node; np != NULL; np = np->wn_sibling)
Bram Moolenaar0c405862005-06-22 22:26:26 +00008531 if (np->wn_byte != 0 && np->wn_child->wn_u2.wnode == node)
8532 newindex = put_node(fd, np->wn_child, newindex, regionmask,
Bram Moolenaar1d73c882005-06-19 22:48:47 +00008533 prefixtree);
Bram Moolenaar51485f02005-06-04 21:55:20 +00008534
8535 return newindex;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008536}
8537
8538
8539/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00008540 * ":mkspell [-ascii] outfile infile ..."
8541 * ":mkspell [-ascii] addfile"
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008542 */
8543 void
8544ex_mkspell(eap)
8545 exarg_T *eap;
8546{
8547 int fcount;
8548 char_u **fnames;
Bram Moolenaarb765d632005-06-07 21:00:02 +00008549 char_u *arg = eap->arg;
8550 int ascii = FALSE;
8551
8552 if (STRNCMP(arg, "-ascii", 6) == 0)
8553 {
8554 ascii = TRUE;
8555 arg = skipwhite(arg + 6);
8556 }
8557
8558 /* Expand all the remaining arguments (e.g., $VIMRUNTIME). */
8559 if (get_arglist_exp(arg, &fcount, &fnames) == OK)
8560 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008561 mkspell(fcount, fnames, ascii, eap->forceit, FALSE);
Bram Moolenaarb765d632005-06-07 21:00:02 +00008562 FreeWild(fcount, fnames);
8563 }
8564}
8565
8566/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00008567 * Create the .sug file.
8568 * Uses the soundfold info in "spin".
8569 * Writes the file with the name "wfname", with ".spl" changed to ".sug".
8570 */
8571 static void
8572spell_make_sugfile(spin, wfname)
8573 spellinfo_T *spin;
8574 char_u *wfname;
8575{
8576 char_u fname[MAXPATHL];
8577 int len;
8578 slang_T *slang;
8579 int free_slang = FALSE;
8580
8581 /*
8582 * Read back the .spl file that was written. This fills the required
8583 * info for soundfolding. This also uses less memory than the
8584 * pointer-linked version of the trie. And it avoids having two versions
8585 * of the code for the soundfolding stuff.
8586 * It might have been done already by spell_reload_one().
8587 */
8588 for (slang = first_lang; slang != NULL; slang = slang->sl_next)
8589 if (fullpathcmp(wfname, slang->sl_fname, FALSE) == FPC_SAME)
8590 break;
8591 if (slang == NULL)
8592 {
8593 spell_message(spin, (char_u *)_("Reading back spell file..."));
8594 slang = spell_load_file(wfname, NULL, NULL, FALSE);
8595 if (slang == NULL)
8596 return;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008597 free_slang = TRUE;
8598 }
8599
8600 /*
8601 * Clear the info in "spin" that is used.
8602 */
8603 spin->si_blocks = NULL;
8604 spin->si_blocks_cnt = 0;
8605 spin->si_compress_cnt = 0; /* will stay at 0 all the time*/
8606 spin->si_free_count = 0;
8607 spin->si_first_free = NULL;
8608 spin->si_foldwcount = 0;
8609
8610 /*
8611 * Go through the trie of good words, soundfold each word and add it to
8612 * the soundfold trie.
8613 */
8614 spell_message(spin, (char_u *)_("Performing soundfolding..."));
8615 if (sug_filltree(spin, slang) == FAIL)
8616 goto theend;
8617
8618 /*
8619 * Create the table which links each soundfold word with a list of the
8620 * good words it may come from. Creates buffer "spin->si_spellbuf".
8621 * This also removes the wordnr from the NUL byte entries to make
8622 * compression possible.
8623 */
8624 if (sug_maketable(spin) == FAIL)
8625 goto theend;
8626
8627 smsg((char_u *)_("Number of words after soundfolding: %ld"),
8628 (long)spin->si_spellbuf->b_ml.ml_line_count);
8629
8630 /*
8631 * Compress the soundfold trie.
8632 */
8633 spell_message(spin, (char_u *)_(msg_compressing));
8634 wordtree_compress(spin, spin->si_foldroot);
8635
8636 /*
8637 * Write the .sug file.
8638 * Make the file name by changing ".spl" to ".sug".
8639 */
8640 STRCPY(fname, wfname);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008641 len = (int)STRLEN(fname);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008642 fname[len - 2] = 'u';
8643 fname[len - 1] = 'g';
8644 sug_write(spin, fname);
8645
8646theend:
8647 if (free_slang)
8648 slang_free(slang);
8649 free_blocks(spin->si_blocks);
8650 close_spellbuf(spin->si_spellbuf);
8651}
8652
8653/*
8654 * Build the soundfold trie for language "slang".
8655 */
8656 static int
8657sug_filltree(spin, slang)
8658 spellinfo_T *spin;
8659 slang_T *slang;
8660{
8661 char_u *byts;
8662 idx_T *idxs;
8663 int depth;
8664 idx_T arridx[MAXWLEN];
8665 int curi[MAXWLEN];
8666 char_u tword[MAXWLEN];
8667 char_u tsalword[MAXWLEN];
8668 int c;
8669 idx_T n;
8670 unsigned words_done = 0;
8671 int wordcount[MAXWLEN];
8672
8673 /* We use si_foldroot for the souldfolded trie. */
8674 spin->si_foldroot = wordtree_alloc(spin);
8675 if (spin->si_foldroot == NULL)
8676 return FAIL;
8677
8678 /* let tree_add_word() know we're adding to the soundfolded tree */
8679 spin->si_sugtree = TRUE;
8680
8681 /*
8682 * Go through the whole case-folded tree, soundfold each word and put it
8683 * in the trie.
8684 */
8685 byts = slang->sl_fbyts;
8686 idxs = slang->sl_fidxs;
8687
8688 arridx[0] = 0;
8689 curi[0] = 1;
8690 wordcount[0] = 0;
8691
8692 depth = 0;
8693 while (depth >= 0 && !got_int)
8694 {
8695 if (curi[depth] > byts[arridx[depth]])
8696 {
8697 /* Done all bytes at this node, go up one level. */
8698 idxs[arridx[depth]] = wordcount[depth];
8699 if (depth > 0)
8700 wordcount[depth - 1] += wordcount[depth];
8701
8702 --depth;
8703 line_breakcheck();
8704 }
8705 else
8706 {
8707
8708 /* Do one more byte at this node. */
8709 n = arridx[depth] + curi[depth];
8710 ++curi[depth];
8711
8712 c = byts[n];
8713 if (c == 0)
8714 {
8715 /* Sound-fold the word. */
8716 tword[depth] = NUL;
8717 spell_soundfold(slang, tword, TRUE, tsalword);
8718
8719 /* We use the "flags" field for the MSB of the wordnr,
8720 * "region" for the LSB of the wordnr. */
8721 if (tree_add_word(spin, tsalword, spin->si_foldroot,
8722 words_done >> 16, words_done & 0xffff,
8723 0) == FAIL)
8724 return FAIL;
8725
8726 ++words_done;
8727 ++wordcount[depth];
8728
8729 /* Reset the block count each time to avoid compression
8730 * kicking in. */
8731 spin->si_blocks_cnt = 0;
8732
8733 /* Skip over any other NUL bytes (same word with different
8734 * flags). */
8735 while (byts[n + 1] == 0)
8736 {
8737 ++n;
8738 ++curi[depth];
8739 }
8740 }
8741 else
8742 {
8743 /* Normal char, go one level deeper. */
8744 tword[depth++] = c;
8745 arridx[depth] = idxs[n];
8746 curi[depth] = 1;
8747 wordcount[depth] = 0;
8748 }
8749 }
8750 }
8751
8752 smsg((char_u *)_("Total number of words: %d"), words_done);
8753
8754 return OK;
8755}
8756
8757/*
8758 * Make the table that links each word in the soundfold trie to the words it
8759 * can be produced from.
8760 * This is not unlike lines in a file, thus use a memfile to be able to access
8761 * the table efficiently.
8762 * Returns FAIL when out of memory.
8763 */
8764 static int
8765sug_maketable(spin)
8766 spellinfo_T *spin;
8767{
8768 garray_T ga;
8769 int res = OK;
8770
8771 /* Allocate a buffer, open a memline for it and create the swap file
8772 * (uses a temp file, not a .swp file). */
8773 spin->si_spellbuf = open_spellbuf();
8774 if (spin->si_spellbuf == NULL)
8775 return FAIL;
8776
8777 /* Use a buffer to store the line info, avoids allocating many small
8778 * pieces of memory. */
8779 ga_init2(&ga, 1, 100);
8780
8781 /* recursively go through the tree */
8782 if (sug_filltable(spin, spin->si_foldroot->wn_sibling, 0, &ga) == -1)
8783 res = FAIL;
8784
8785 ga_clear(&ga);
8786 return res;
8787}
8788
8789/*
8790 * Fill the table for one node and its children.
8791 * Returns the wordnr at the start of the node.
8792 * Returns -1 when out of memory.
8793 */
8794 static int
8795sug_filltable(spin, node, startwordnr, gap)
8796 spellinfo_T *spin;
8797 wordnode_T *node;
8798 int startwordnr;
8799 garray_T *gap; /* place to store line of numbers */
8800{
8801 wordnode_T *p, *np;
8802 int wordnr = startwordnr;
8803 int nr;
8804 int prev_nr;
8805
8806 for (p = node; p != NULL; p = p->wn_sibling)
8807 {
8808 if (p->wn_byte == NUL)
8809 {
8810 gap->ga_len = 0;
8811 prev_nr = 0;
8812 for (np = p; np != NULL && np->wn_byte == NUL; np = np->wn_sibling)
8813 {
8814 if (ga_grow(gap, 10) == FAIL)
8815 return -1;
8816
8817 nr = (np->wn_flags << 16) + (np->wn_region & 0xffff);
8818 /* Compute the offset from the previous nr and store the
8819 * offset in a way that it takes a minimum number of bytes.
8820 * It's a bit like utf-8, but without the need to mark
8821 * following bytes. */
8822 nr -= prev_nr;
8823 prev_nr += nr;
8824 gap->ga_len += offset2bytes(nr,
8825 (char_u *)gap->ga_data + gap->ga_len);
8826 }
8827
8828 /* add the NUL byte */
8829 ((char_u *)gap->ga_data)[gap->ga_len++] = NUL;
8830
8831 if (ml_append_buf(spin->si_spellbuf, (linenr_T)wordnr,
8832 gap->ga_data, gap->ga_len, TRUE) == FAIL)
8833 return -1;
8834 ++wordnr;
8835
8836 /* Remove extra NUL entries, we no longer need them. We don't
8837 * bother freeing the nodes, the won't be reused anyway. */
8838 while (p->wn_sibling != NULL && p->wn_sibling->wn_byte == NUL)
8839 p->wn_sibling = p->wn_sibling->wn_sibling;
8840
8841 /* Clear the flags on the remaining NUL node, so that compression
8842 * works a lot better. */
8843 p->wn_flags = 0;
8844 p->wn_region = 0;
8845 }
8846 else
8847 {
8848 wordnr = sug_filltable(spin, p->wn_child, wordnr, gap);
8849 if (wordnr == -1)
8850 return -1;
8851 }
8852 }
8853 return wordnr;
8854}
8855
8856/*
8857 * Convert an offset into a minimal number of bytes.
8858 * Similar to utf_char2byters, but use 8 bits in followup bytes and avoid NUL
8859 * bytes.
8860 */
8861 static int
8862offset2bytes(nr, buf)
8863 int nr;
8864 char_u *buf;
8865{
8866 int rem;
8867 int b1, b2, b3, b4;
8868
8869 /* Split the number in parts of base 255. We need to avoid NUL bytes. */
8870 b1 = nr % 255 + 1;
8871 rem = nr / 255;
8872 b2 = rem % 255 + 1;
8873 rem = rem / 255;
8874 b3 = rem % 255 + 1;
8875 b4 = rem / 255 + 1;
8876
8877 if (b4 > 1 || b3 > 0x1f) /* 4 bytes */
8878 {
8879 buf[0] = 0xe0 + b4;
8880 buf[1] = b3;
8881 buf[2] = b2;
8882 buf[3] = b1;
8883 return 4;
8884 }
8885 if (b3 > 1 || b2 > 0x3f ) /* 3 bytes */
8886 {
8887 buf[0] = 0xc0 + b3;
8888 buf[1] = b2;
8889 buf[2] = b1;
8890 return 3;
8891 }
8892 if (b2 > 1 || b1 > 0x7f ) /* 2 bytes */
8893 {
8894 buf[0] = 0x80 + b2;
8895 buf[1] = b1;
8896 return 2;
8897 }
8898 /* 1 byte */
8899 buf[0] = b1;
8900 return 1;
8901}
8902
8903/*
8904 * Opposite of offset2bytes().
8905 * "pp" points to the bytes and is advanced over it.
8906 * Returns the offset.
8907 */
8908 static int
8909bytes2offset(pp)
8910 char_u **pp;
8911{
8912 char_u *p = *pp;
8913 int nr;
8914 int c;
8915
8916 c = *p++;
8917 if ((c & 0x80) == 0x00) /* 1 byte */
8918 {
8919 nr = c - 1;
8920 }
8921 else if ((c & 0xc0) == 0x80) /* 2 bytes */
8922 {
8923 nr = (c & 0x3f) - 1;
8924 nr = nr * 255 + (*p++ - 1);
8925 }
8926 else if ((c & 0xe0) == 0xc0) /* 3 bytes */
8927 {
8928 nr = (c & 0x1f) - 1;
8929 nr = nr * 255 + (*p++ - 1);
8930 nr = nr * 255 + (*p++ - 1);
8931 }
8932 else /* 4 bytes */
8933 {
8934 nr = (c & 0x0f) - 1;
8935 nr = nr * 255 + (*p++ - 1);
8936 nr = nr * 255 + (*p++ - 1);
8937 nr = nr * 255 + (*p++ - 1);
8938 }
8939
8940 *pp = p;
8941 return nr;
8942}
8943
8944/*
8945 * Write the .sug file in "fname".
8946 */
8947 static void
8948sug_write(spin, fname)
8949 spellinfo_T *spin;
8950 char_u *fname;
8951{
8952 FILE *fd;
8953 wordnode_T *tree;
8954 int nodecount;
8955 int wcount;
8956 char_u *line;
8957 linenr_T lnum;
8958 int len;
8959
8960 /* Create the file. Note that an existing file is silently overwritten! */
8961 fd = mch_fopen((char *)fname, "w");
8962 if (fd == NULL)
8963 {
8964 EMSG2(_(e_notopen), fname);
8965 return;
8966 }
8967
8968 vim_snprintf((char *)IObuff, IOSIZE,
8969 _("Writing suggestion file %s ..."), fname);
8970 spell_message(spin, IObuff);
8971
8972 /*
8973 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
8974 */
8975 if (fwrite(VIMSUGMAGIC, VIMSUGMAGICL, (size_t)1, fd) != 1) /* <fileID> */
8976 {
8977 EMSG(_(e_write));
8978 goto theend;
8979 }
8980 putc(VIMSUGVERSION, fd); /* <versionnr> */
8981
8982 /* Write si_sugtime to the file. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +02008983 put_time(fd, spin->si_sugtime); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +00008984
8985 /*
8986 * <SUGWORDTREE>
8987 */
8988 spin->si_memtot = 0;
8989 tree = spin->si_foldroot->wn_sibling;
8990
8991 /* Clear the index and wnode fields in the tree. */
8992 clear_node(tree);
8993
8994 /* Count the number of nodes. Needed to be able to allocate the
8995 * memory when reading the nodes. Also fills in index for shared
8996 * nodes. */
8997 nodecount = put_node(NULL, tree, 0, 0, FALSE);
8998
8999 /* number of nodes in 4 bytes */
9000 put_bytes(fd, (long_u)nodecount, 4); /* <nodecount> */
9001 spin->si_memtot += nodecount + nodecount * sizeof(int);
9002
9003 /* Write the nodes. */
9004 (void)put_node(fd, tree, 0, 0, FALSE);
9005
9006 /*
9007 * <SUGTABLE>: <sugwcount> <sugline> ...
9008 */
9009 wcount = spin->si_spellbuf->b_ml.ml_line_count;
9010 put_bytes(fd, (long_u)wcount, 4); /* <sugwcount> */
9011
9012 for (lnum = 1; lnum <= (linenr_T)wcount; ++lnum)
9013 {
9014 /* <sugline>: <sugnr> ... NUL */
9015 line = ml_get_buf(spin->si_spellbuf, lnum, FALSE);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009016 len = (int)STRLEN(line) + 1;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009017 if (fwrite(line, (size_t)len, (size_t)1, fd) == 0)
9018 {
9019 EMSG(_(e_write));
9020 goto theend;
9021 }
9022 spin->si_memtot += len;
9023 }
9024
9025 /* Write another byte to check for errors. */
9026 if (putc(0, fd) == EOF)
9027 EMSG(_(e_write));
9028
9029 vim_snprintf((char *)IObuff, IOSIZE,
9030 _("Estimated runtime memory use: %d bytes"), spin->si_memtot);
9031 spell_message(spin, IObuff);
9032
9033theend:
9034 /* close the file */
9035 fclose(fd);
9036}
9037
9038/*
9039 * Open a spell buffer. This is a nameless buffer that is not in the buffer
9040 * list and only contains text lines. Can use a swapfile to reduce memory
9041 * use.
9042 * Most other fields are invalid! Esp. watch out for string options being
9043 * NULL and there is no undo info.
9044 * Returns NULL when out of memory.
9045 */
9046 static buf_T *
9047open_spellbuf()
9048{
9049 buf_T *buf;
9050
9051 buf = (buf_T *)alloc_clear(sizeof(buf_T));
9052 if (buf != NULL)
9053 {
9054 buf->b_spell = TRUE;
9055 buf->b_p_swf = TRUE; /* may create a swap file */
9056 ml_open(buf);
9057 ml_open_file(buf); /* create swap file now */
9058 }
9059 return buf;
9060}
9061
9062/*
9063 * Close the buffer used for spell info.
9064 */
9065 static void
9066close_spellbuf(buf)
9067 buf_T *buf;
9068{
9069 if (buf != NULL)
9070 {
9071 ml_close(buf, TRUE);
9072 vim_free(buf);
9073 }
9074}
9075
9076
9077/*
Bram Moolenaarb765d632005-06-07 21:00:02 +00009078 * Create a Vim spell file from one or more word lists.
9079 * "fnames[0]" is the output file name.
9080 * "fnames[fcount - 1]" is the last input file name.
9081 * Exception: when "fnames[0]" ends in ".add" it's used as the input file name
9082 * and ".spl" is appended to make the output file name.
9083 */
9084 static void
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009085mkspell(fcount, fnames, ascii, overwrite, added_word)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009086 int fcount;
9087 char_u **fnames;
9088 int ascii; /* -ascii argument given */
9089 int overwrite; /* overwrite existing output file */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009090 int added_word; /* invoked through "zg" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009091{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009092 char_u fname[MAXPATHL];
9093 char_u wfname[MAXPATHL];
Bram Moolenaarb765d632005-06-07 21:00:02 +00009094 char_u **innames;
9095 int incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009096 afffile_T *(afile[8]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009097 int i;
9098 int len;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009099 struct stat st;
Bram Moolenaar8fef2ad2005-04-23 20:42:23 +00009100 int error = FALSE;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009101 spellinfo_T spin;
9102
9103 vim_memset(&spin, 0, sizeof(spin));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009104 spin.si_verbose = !added_word;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009105 spin.si_ascii = ascii;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009106 spin.si_followup = TRUE;
9107 spin.si_rem_accents = TRUE;
9108 ga_init2(&spin.si_rep, (int)sizeof(fromto_T), 20);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009109 ga_init2(&spin.si_repsal, (int)sizeof(fromto_T), 20);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009110 ga_init2(&spin.si_sal, (int)sizeof(fromto_T), 20);
9111 ga_init2(&spin.si_map, (int)sizeof(char_u), 100);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009112 ga_init2(&spin.si_comppat, (int)sizeof(char_u *), 20);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009113 ga_init2(&spin.si_prefcond, (int)sizeof(char_u *), 50);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009114 hash_init(&spin.si_commonwords);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009115 spin.si_newcompID = 127; /* start compound ID at first maximum */
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009116
Bram Moolenaarb765d632005-06-07 21:00:02 +00009117 /* default: fnames[0] is output file, following are input files */
9118 innames = &fnames[1];
9119 incount = fcount - 1;
9120
9121 if (fcount >= 1)
Bram Moolenaar5482f332005-04-17 20:18:43 +00009122 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009123 len = (int)STRLEN(fnames[0]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009124 if (fcount == 1 && len > 4 && STRCMP(fnames[0] + len - 4, ".add") == 0)
9125 {
9126 /* For ":mkspell path/en.latin1.add" output file is
9127 * "path/en.latin1.add.spl". */
9128 innames = &fnames[0];
9129 incount = 1;
9130 vim_snprintf((char *)wfname, sizeof(wfname), "%s.spl", fnames[0]);
9131 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009132 else if (fcount == 1)
9133 {
9134 /* For ":mkspell path/vim" output file is "path/vim.latin1.spl". */
9135 innames = &fnames[0];
9136 incount = 1;
Bram Moolenaar56f78042010-12-08 17:09:32 +01009137 vim_snprintf((char *)wfname, sizeof(wfname), SPL_FNAME_TMPL,
9138 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009139 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009140 else if (len > 4 && STRCMP(fnames[0] + len - 4, ".spl") == 0)
9141 {
9142 /* Name ends in ".spl", use as the file name. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009143 vim_strncpy(wfname, fnames[0], sizeof(wfname) - 1);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009144 }
9145 else
9146 /* Name should be language, make the file name from it. */
Bram Moolenaar56f78042010-12-08 17:09:32 +01009147 vim_snprintf((char *)wfname, sizeof(wfname), SPL_FNAME_TMPL,
9148 fnames[0], spin.si_ascii ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009149
9150 /* Check for .ascii.spl. */
Bram Moolenaar56f78042010-12-08 17:09:32 +01009151 if (strstr((char *)gettail(wfname), SPL_FNAME_ASCII) != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009152 spin.si_ascii = TRUE;
9153
9154 /* Check for .add.spl. */
Bram Moolenaar56f78042010-12-08 17:09:32 +01009155 if (strstr((char *)gettail(wfname), SPL_FNAME_ADD) != NULL)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009156 spin.si_add = TRUE;
Bram Moolenaar5482f332005-04-17 20:18:43 +00009157 }
9158
Bram Moolenaarb765d632005-06-07 21:00:02 +00009159 if (incount <= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009160 EMSG(_(e_invarg)); /* need at least output and input names */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009161 else if (vim_strchr(gettail(wfname), '_') != NULL)
9162 EMSG(_("E751: Output file name must not have region name"));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009163 else if (incount > 8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009164 EMSG(_("E754: Only up to 8 regions supported"));
9165 else
9166 {
9167 /* Check for overwriting before doing things that may take a lot of
9168 * time. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009169 if (!overwrite && mch_stat((char *)wfname, &st) >= 0)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009170 {
9171 EMSG(_(e_exists));
Bram Moolenaarb765d632005-06-07 21:00:02 +00009172 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009173 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009174 if (mch_isdir(wfname))
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009175 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009176 EMSG2(_(e_isadir2), wfname);
9177 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009178 }
9179
9180 /*
9181 * Init the aff and dic pointers.
9182 * Get the region names if there are more than 2 arguments.
9183 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009184 for (i = 0; i < incount; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009185 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009186 afile[i] = NULL;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009187
Bram Moolenaar3982c542005-06-08 21:56:31 +00009188 if (incount > 1)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009189 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009190 len = (int)STRLEN(innames[i]);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009191 if (STRLEN(gettail(innames[i])) < 5
9192 || innames[i][len - 3] != '_')
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009193 {
Bram Moolenaarb765d632005-06-07 21:00:02 +00009194 EMSG2(_("E755: Invalid region in %s"), innames[i]);
9195 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009196 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009197 spin.si_region_name[i * 2] = TOLOWER_ASC(innames[i][len - 2]);
9198 spin.si_region_name[i * 2 + 1] =
9199 TOLOWER_ASC(innames[i][len - 1]);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009200 }
9201 }
Bram Moolenaar3982c542005-06-08 21:56:31 +00009202 spin.si_region_count = incount;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009203
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009204 spin.si_foldroot = wordtree_alloc(&spin);
9205 spin.si_keeproot = wordtree_alloc(&spin);
9206 spin.si_prefroot = wordtree_alloc(&spin);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009207 if (spin.si_foldroot == NULL
9208 || spin.si_keeproot == NULL
9209 || spin.si_prefroot == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009210 {
Bram Moolenaar329cc7e2005-08-10 07:51:35 +00009211 free_blocks(spin.si_blocks);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009212 return;
Bram Moolenaar51485f02005-06-04 21:55:20 +00009213 }
9214
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009215 /* When not producing a .add.spl file clear the character table when
9216 * we encounter one in the .aff file. This means we dump the current
9217 * one in the .spl file if the .aff file doesn't define one. That's
9218 * better than guessing the contents, the table will match a
9219 * previously loaded spell file. */
9220 if (!spin.si_add)
9221 spin.si_clear_chartab = TRUE;
9222
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009223 /*
9224 * Read all the .aff and .dic files.
9225 * Text is converted to 'encoding'.
Bram Moolenaar51485f02005-06-04 21:55:20 +00009226 * Words are stored in the case-folded and keep-case trees.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009227 */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009228 for (i = 0; i < incount && !error; ++i)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009229 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009230 spin.si_conv.vc_type = CONV_NONE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009231 spin.si_region = 1 << i;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009232
Bram Moolenaarb765d632005-06-07 21:00:02 +00009233 vim_snprintf((char *)fname, sizeof(fname), "%s.aff", innames[i]);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009234 if (mch_stat((char *)fname, &st) >= 0)
9235 {
9236 /* Read the .aff file. Will init "spin->si_conv" based on the
9237 * "SET" line. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009238 afile[i] = spell_read_aff(&spin, fname);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009239 if (afile[i] == NULL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009240 error = TRUE;
9241 else
9242 {
9243 /* Read the .dic file and store the words in the trees. */
9244 vim_snprintf((char *)fname, sizeof(fname), "%s.dic",
Bram Moolenaarb765d632005-06-07 21:00:02 +00009245 innames[i]);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009246 if (spell_read_dic(&spin, fname, afile[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009247 error = TRUE;
9248 }
9249 }
9250 else
9251 {
9252 /* No .aff file, try reading the file as a word list. Store
9253 * the words in the trees. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009254 if (spell_read_wordfile(&spin, innames[i]) == FAIL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009255 error = TRUE;
9256 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009257
Bram Moolenaarb765d632005-06-07 21:00:02 +00009258#ifdef FEAT_MBYTE
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009259 /* Free any conversion stuff. */
Bram Moolenaar51485f02005-06-04 21:55:20 +00009260 convert_setup(&spin.si_conv, NULL, NULL);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009261#endif
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009262 }
9263
Bram Moolenaar78622822005-08-23 21:00:13 +00009264 if (spin.si_compflags != NULL && spin.si_nobreak)
9265 MSG(_("Warning: both compounding and NOBREAK specified"));
9266
Bram Moolenaar4770d092006-01-12 23:22:24 +00009267 if (!error && !got_int)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009268 {
Bram Moolenaar51485f02005-06-04 21:55:20 +00009269 /*
Bram Moolenaar51485f02005-06-04 21:55:20 +00009270 * Combine tails in the tree.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009271 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009272 spell_message(&spin, (char_u *)_(msg_compressing));
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009273 wordtree_compress(&spin, spin.si_foldroot);
9274 wordtree_compress(&spin, spin.si_keeproot);
9275 wordtree_compress(&spin, spin.si_prefroot);
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009276 }
9277
Bram Moolenaar4770d092006-01-12 23:22:24 +00009278 if (!error && !got_int)
Bram Moolenaar51485f02005-06-04 21:55:20 +00009279 {
9280 /*
9281 * Write the info in the spell file.
9282 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00009283 vim_snprintf((char *)IObuff, IOSIZE,
9284 _("Writing spell file %s ..."), wfname);
9285 spell_message(&spin, IObuff);
Bram Moolenaar50cde822005-06-05 21:54:54 +00009286
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009287 error = write_vim_spell(&spin, wfname) == FAIL;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009288
Bram Moolenaar4770d092006-01-12 23:22:24 +00009289 spell_message(&spin, (char_u *)_("Done!"));
9290 vim_snprintf((char *)IObuff, IOSIZE,
9291 _("Estimated runtime memory use: %d bytes"), spin.si_memtot);
9292 spell_message(&spin, IObuff);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009293
Bram Moolenaar4770d092006-01-12 23:22:24 +00009294 /*
9295 * If the file is loaded need to reload it.
9296 */
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009297 if (!error)
9298 spell_reload_one(wfname, added_word);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009299 }
9300
9301 /* Free the allocated memory. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009302 ga_clear(&spin.si_rep);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009303 ga_clear(&spin.si_repsal);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009304 ga_clear(&spin.si_sal);
9305 ga_clear(&spin.si_map);
Bram Moolenaar899dddf2006-03-26 21:06:50 +00009306 ga_clear(&spin.si_comppat);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009307 ga_clear(&spin.si_prefcond);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009308 hash_clear_all(&spin.si_commonwords, 0);
Bram Moolenaar51485f02005-06-04 21:55:20 +00009309
9310 /* Free the .aff file structures. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009311 for (i = 0; i < incount; ++i)
9312 if (afile[i] != NULL)
9313 spell_free_aff(afile[i]);
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009314
9315 /* Free all the bits and pieces at once. */
9316 free_blocks(spin.si_blocks);
Bram Moolenaar4770d092006-01-12 23:22:24 +00009317
9318 /*
9319 * If there is soundfolding info and no NOSUGFILE item create the
9320 * .sug file with the soundfolded word trie.
9321 */
9322 if (spin.si_sugtime != 0 && !error && !got_int)
9323 spell_make_sugfile(&spin, wfname);
9324
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009325 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009326}
9327
Bram Moolenaar4770d092006-01-12 23:22:24 +00009328/*
9329 * Display a message for spell file processing when 'verbose' is set or using
9330 * ":mkspell". "str" can be IObuff.
9331 */
9332 static void
9333spell_message(spin, str)
9334 spellinfo_T *spin;
9335 char_u *str;
9336{
9337 if (spin->si_verbose || p_verbose > 2)
9338 {
9339 if (!spin->si_verbose)
9340 verbose_enter();
9341 MSG(str);
9342 out_flush();
9343 if (!spin->si_verbose)
9344 verbose_leave();
9345 }
9346}
Bram Moolenaarb765d632005-06-07 21:00:02 +00009347
9348/*
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009349 * ":[count]spellgood {word}"
9350 * ":[count]spellwrong {word}"
Bram Moolenaard0131a82006-03-04 21:46:13 +00009351 * ":[count]spellundo {word}"
Bram Moolenaarb765d632005-06-07 21:00:02 +00009352 */
9353 void
9354ex_spell(eap)
9355 exarg_T *eap;
9356{
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009357 spell_add_word(eap->arg, (int)STRLEN(eap->arg), eap->cmdidx == CMD_spellwrong,
Bram Moolenaard0131a82006-03-04 21:46:13 +00009358 eap->forceit ? 0 : (int)eap->line2,
9359 eap->cmdidx == CMD_spellundo);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009360}
9361
9362/*
9363 * Add "word[len]" to 'spellfile' as a good or bad word.
9364 */
9365 void
Bram Moolenaar89d40322006-08-29 15:30:07 +00009366spell_add_word(word, len, bad, idx, undo)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009367 char_u *word;
9368 int len;
9369 int bad;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009370 int idx; /* "zG" and "zW": zero, otherwise index in
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009371 'spellfile' */
Bram Moolenaard0131a82006-03-04 21:46:13 +00009372 int undo; /* TRUE for "zug", "zuG", "zuw" and "zuW" */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009373{
Bram Moolenaara3917072006-09-14 08:48:14 +00009374 FILE *fd = NULL;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009375 buf_T *buf = NULL;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009376 int new_spf = FALSE;
Bram Moolenaar7887d882005-07-01 22:33:52 +00009377 char_u *fname;
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009378 char_u fnamebuf[MAXPATHL];
9379 char_u line[MAXWLEN * 2];
9380 long fpos, fpos_next = 0;
9381 int i;
9382 char_u *spf;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009383
Bram Moolenaar89d40322006-08-29 15:30:07 +00009384 if (idx == 0) /* use internal wordlist */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009385 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009386 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009387 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009388 int_wordlist = vim_tempname('s');
9389 if (int_wordlist == NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009390 return;
9391 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009392 fname = int_wordlist;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009393 }
Bram Moolenaarb765d632005-06-07 21:00:02 +00009394 else
9395 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009396 /* If 'spellfile' isn't set figure out a good default value. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009397 if (*curwin->w_s->b_p_spf == NUL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009398 {
9399 init_spellfile();
9400 new_spf = TRUE;
9401 }
9402
Bram Moolenaar860cae12010-06-05 23:22:07 +02009403 if (*curwin->w_s->b_p_spf == NUL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009404 {
Bram Moolenaarf75a9632005-09-13 21:20:47 +00009405 EMSG2(_(e_notset), "spellfile");
Bram Moolenaar7887d882005-07-01 22:33:52 +00009406 return;
9407 }
9408
Bram Moolenaar860cae12010-06-05 23:22:07 +02009409 for (spf = curwin->w_s->b_p_spf, i = 1; *spf != NUL; ++i)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009410 {
9411 copy_option_part(&spf, fnamebuf, MAXPATHL, ",");
Bram Moolenaar89d40322006-08-29 15:30:07 +00009412 if (i == idx)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009413 break;
9414 if (*spf == NUL)
9415 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00009416 EMSGN(_("E765: 'spellfile' does not have %ld entries"), idx);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009417 return;
9418 }
9419 }
9420
Bram Moolenaarb765d632005-06-07 21:00:02 +00009421 /* Check that the user isn't editing the .add file somewhere. */
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009422 buf = buflist_findname_exp(fnamebuf);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009423 if (buf != NULL && buf->b_ml.ml_mfp == NULL)
9424 buf = NULL;
9425 if (buf != NULL && bufIsChanged(buf))
Bram Moolenaarb765d632005-06-07 21:00:02 +00009426 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009427 EMSG(_(e_bufloaded));
9428 return;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009429 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009430
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009431 fname = fnamebuf;
9432 }
9433
Bram Moolenaard0131a82006-03-04 21:46:13 +00009434 if (bad || undo)
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009435 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009436 /* When the word appears as good word we need to remove that one,
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009437 * since its flags sort before the one with WF_BANNED. */
9438 fd = mch_fopen((char *)fname, "r");
9439 if (fd != NULL)
9440 {
9441 while (!vim_fgets(line, MAXWLEN * 2, fd))
9442 {
9443 fpos = fpos_next;
9444 fpos_next = ftell(fd);
9445 if (STRNCMP(word, line, len) == 0
9446 && (line[len] == '/' || line[len] < ' '))
9447 {
9448 /* Found duplicate word. Remove it by writing a '#' at
9449 * the start of the line. Mixing reading and writing
9450 * doesn't work for all systems, close the file first. */
9451 fclose(fd);
9452 fd = mch_fopen((char *)fname, "r+");
9453 if (fd == NULL)
9454 break;
9455 if (fseek(fd, fpos, SEEK_SET) == 0)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009456 {
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009457 fputc('#', fd);
Bram Moolenaard0131a82006-03-04 21:46:13 +00009458 if (undo)
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009459 {
9460 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009461 smsg((char_u *)_("Word removed from %s"), NameBuff);
Bram Moolenaar2113a1d2006-09-11 19:38:08 +00009462 }
Bram Moolenaard0131a82006-03-04 21:46:13 +00009463 }
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009464 fseek(fd, fpos_next, SEEK_SET);
9465 }
9466 }
Bram Moolenaara9d52e32010-07-31 16:44:19 +02009467 if (fd != NULL)
9468 fclose(fd);
Bram Moolenaarf9184a12005-07-02 23:10:47 +00009469 }
Bram Moolenaar7887d882005-07-01 22:33:52 +00009470 }
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009471
9472 if (!undo)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009473 {
Bram Moolenaard0131a82006-03-04 21:46:13 +00009474 fd = mch_fopen((char *)fname, "a");
9475 if (fd == NULL && new_spf)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009476 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009477 char_u *p;
9478
Bram Moolenaard0131a82006-03-04 21:46:13 +00009479 /* We just initialized the 'spellfile' option and can't open the
9480 * file. We may need to create the "spell" directory first. We
9481 * already checked the runtime directory is writable in
9482 * init_spellfile(). */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009483 if (!dir_of_file_exists(fname) && (p = gettail_sep(fname)) != fname)
Bram Moolenaard0131a82006-03-04 21:46:13 +00009484 {
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009485 int c = *p;
9486
Bram Moolenaard0131a82006-03-04 21:46:13 +00009487 /* The directory doesn't exist. Try creating it and opening
9488 * the file again. */
Bram Moolenaarac2adc72006-09-12 20:25:24 +00009489 *p = NUL;
9490 vim_mkdir(fname, 0755);
9491 *p = c;
Bram Moolenaard0131a82006-03-04 21:46:13 +00009492 fd = mch_fopen((char *)fname, "a");
9493 }
9494 }
9495
9496 if (fd == NULL)
9497 EMSG2(_(e_notopen), fname);
9498 else
9499 {
9500 if (bad)
9501 fprintf(fd, "%.*s/!\n", len, word);
9502 else
9503 fprintf(fd, "%.*s\n", len, word);
9504 fclose(fd);
9505
9506 home_replace(NULL, fname, NameBuff, MAXPATHL, TRUE);
9507 smsg((char_u *)_("Word added to %s"), NameBuff);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009508 }
9509 }
9510
Bram Moolenaard0131a82006-03-04 21:46:13 +00009511 if (fd != NULL)
Bram Moolenaar7887d882005-07-01 22:33:52 +00009512 {
Bram Moolenaar7887d882005-07-01 22:33:52 +00009513 /* Update the .add.spl file. */
9514 mkspell(1, &fname, FALSE, TRUE, TRUE);
9515
9516 /* If the .add file is edited somewhere, reload it. */
9517 if (buf != NULL)
Bram Moolenaarea8bd732006-01-14 21:15:59 +00009518 buf_reload(buf, buf->b_orig_mode);
Bram Moolenaar7887d882005-07-01 22:33:52 +00009519
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009520 redraw_all_later(SOME_VALID);
Bram Moolenaarb765d632005-06-07 21:00:02 +00009521 }
9522}
9523
9524/*
9525 * Initialize 'spellfile' for the current buffer.
9526 */
9527 static void
9528init_spellfile()
9529{
9530 char_u buf[MAXPATHL];
9531 int l;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00009532 char_u *fname;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009533 char_u *rtp;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009534 char_u *lend;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009535 int aspath = FALSE;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009536 char_u *lstart = curbuf->b_s.b_p_spl;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009537
Bram Moolenaar860cae12010-06-05 23:22:07 +02009538 if (*curwin->w_s->b_p_spl != NUL && curwin->w_s->b_langp.ga_len > 0)
Bram Moolenaarb765d632005-06-07 21:00:02 +00009539 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009540 /* Find the end of the language name. Exclude the region. If there
9541 * is a path separator remember the start of the tail. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009542 for (lend = curwin->w_s->b_p_spl; *lend != NUL
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009543 && vim_strchr((char_u *)",._", *lend) == NULL; ++lend)
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009544 if (vim_ispathsep(*lend))
9545 {
9546 aspath = TRUE;
9547 lstart = lend + 1;
9548 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +00009549
9550 /* Loop over all entries in 'runtimepath'. Use the first one where we
9551 * are allowed to write. */
Bram Moolenaarb765d632005-06-07 21:00:02 +00009552 rtp = p_rtp;
9553 while (*rtp != NUL)
9554 {
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009555 if (aspath)
9556 /* Use directory of an entry with path, e.g., for
9557 * "/dir/lg.utf-8.spl" use "/dir". */
Bram Moolenaara8fc7982010-09-29 18:32:52 +02009558 vim_strncpy(buf, curbuf->b_s.b_p_spl,
9559 lstart - curbuf->b_s.b_p_spl - 1);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009560 else
9561 /* Copy the path from 'runtimepath' to buf[]. */
9562 copy_option_part(&rtp, buf, MAXPATHL, ",");
Bram Moolenaarb765d632005-06-07 21:00:02 +00009563 if (filewritable(buf) == 2)
9564 {
Bram Moolenaar3982c542005-06-08 21:56:31 +00009565 /* Use the first language name from 'spelllang' and the
9566 * encoding used in the first loaded .spl file. */
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009567 if (aspath)
Bram Moolenaara8fc7982010-09-29 18:32:52 +02009568 vim_strncpy(buf, curbuf->b_s.b_p_spl,
9569 lend - curbuf->b_s.b_p_spl);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009570 else
9571 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009572 /* Create the "spell" directory if it doesn't exist yet. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009573 l = (int)STRLEN(buf);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009574 vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
Bram Moolenaara8fc7982010-09-29 18:32:52 +02009575 if (filewritable(buf) != 2)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009576 vim_mkdir(buf, 0755);
9577
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009578 l = (int)STRLEN(buf);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009579 vim_snprintf((char *)buf + l, MAXPATHL - l,
Bram Moolenaar910f66f2006-04-05 20:41:53 +00009580 "/%.*s", (int)(lend - lstart), lstart);
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009581 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009582 l = (int)STRLEN(buf);
Bram Moolenaar860cae12010-06-05 23:22:07 +02009583 fname = LANGP_ENTRY(curwin->w_s->b_langp, 0)->lp_slang->sl_fname;
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009584 vim_snprintf((char *)buf + l, MAXPATHL - l, ".%s.add",
9585 fname != NULL
9586 && strstr((char *)gettail(fname), ".ascii.") != NULL
9587 ? (char_u *)"ascii" : spell_enc());
Bram Moolenaarb765d632005-06-07 21:00:02 +00009588 set_option_value((char_u *)"spellfile", 0L, buf, OPT_LOCAL);
9589 break;
9590 }
Bram Moolenaarda2303d2005-08-30 21:55:26 +00009591 aspath = FALSE;
Bram Moolenaarb765d632005-06-07 21:00:02 +00009592 }
9593 }
9594}
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00009595
Bram Moolenaar51485f02005-06-04 21:55:20 +00009596
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009597/*
9598 * Init the chartab used for spelling for ASCII.
9599 * EBCDIC is not supported!
9600 */
9601 static void
9602clear_spell_chartab(sp)
9603 spelltab_T *sp;
9604{
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009605 int i;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009606
9607 /* Init everything to FALSE. */
9608 vim_memset(sp->st_isw, FALSE, sizeof(sp->st_isw));
9609 vim_memset(sp->st_isu, FALSE, sizeof(sp->st_isu));
9610 for (i = 0; i < 256; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009611 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009612 sp->st_fold[i] = i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009613 sp->st_upper[i] = i;
9614 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009615
9616 /* We include digits. A word shouldn't start with a digit, but handling
9617 * that is done separately. */
9618 for (i = '0'; i <= '9'; ++i)
9619 sp->st_isw[i] = TRUE;
9620 for (i = 'A'; i <= 'Z'; ++i)
9621 {
9622 sp->st_isw[i] = TRUE;
9623 sp->st_isu[i] = TRUE;
9624 sp->st_fold[i] = i + 0x20;
9625 }
9626 for (i = 'a'; i <= 'z'; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009627 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009628 sp->st_isw[i] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009629 sp->st_upper[i] = i - 0x20;
9630 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009631}
9632
9633/*
9634 * Init the chartab used for spelling. Only depends on 'encoding'.
9635 * Called once while starting up and when 'encoding' changes.
9636 * The default is to use isalpha(), but the spell file should define the word
9637 * characters to make it possible that 'encoding' differs from the current
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009638 * locale. For utf-8 we don't use isalpha() but our own functions.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009639 */
9640 void
9641init_spell_chartab()
9642{
9643 int i;
9644
9645 did_set_spelltab = FALSE;
9646 clear_spell_chartab(&spelltab);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009647#ifdef FEAT_MBYTE
9648 if (enc_dbcs)
9649 {
9650 /* DBCS: assume double-wide characters are word characters. */
9651 for (i = 128; i <= 255; ++i)
9652 if (MB_BYTE2LEN(i) == 2)
9653 spelltab.st_isw[i] = TRUE;
9654 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009655 else if (enc_utf8)
9656 {
9657 for (i = 128; i < 256; ++i)
9658 {
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02009659 int f = utf_fold(i);
9660 int u = utf_toupper(i);
9661
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009662 spelltab.st_isu[i] = utf_isupper(i);
9663 spelltab.st_isw[i] = spelltab.st_isu[i] || utf_islower(i);
Bram Moolenaar54ab0f12010-05-13 17:46:58 +02009664 /* The folded/upper-cased value is different between latin1 and
9665 * utf8 for 0xb5, causing E763 for no good reason. Use the latin1
9666 * value for utf-8 to avoid this. */
9667 spelltab.st_fold[i] = (f < 256) ? f : i;
9668 spelltab.st_upper[i] = (u < 256) ? u : i;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009669 }
9670 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009671 else
9672#endif
9673 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009674 /* Rough guess: use locale-dependent library functions. */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009675 for (i = 128; i < 256; ++i)
9676 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009677 if (MB_ISUPPER(i))
9678 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009679 spelltab.st_isw[i] = TRUE;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009680 spelltab.st_isu[i] = TRUE;
9681 spelltab.st_fold[i] = MB_TOLOWER(i);
9682 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009683 else if (MB_ISLOWER(i))
9684 {
9685 spelltab.st_isw[i] = TRUE;
9686 spelltab.st_upper[i] = MB_TOUPPER(i);
9687 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009688 }
9689 }
9690}
9691
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009692/*
9693 * Set the spell character tables from strings in the affix file.
9694 */
9695 static int
9696set_spell_chartab(fol, low, upp)
9697 char_u *fol;
9698 char_u *low;
9699 char_u *upp;
9700{
9701 /* We build the new tables here first, so that we can compare with the
9702 * previous one. */
9703 spelltab_T new_st;
9704 char_u *pf = fol, *pl = low, *pu = upp;
9705 int f, l, u;
9706
9707 clear_spell_chartab(&new_st);
9708
9709 while (*pf != NUL)
9710 {
9711 if (*pl == NUL || *pu == NUL)
9712 {
9713 EMSG(_(e_affform));
9714 return FAIL;
9715 }
9716#ifdef FEAT_MBYTE
9717 f = mb_ptr2char_adv(&pf);
9718 l = mb_ptr2char_adv(&pl);
9719 u = mb_ptr2char_adv(&pu);
9720#else
9721 f = *pf++;
9722 l = *pl++;
9723 u = *pu++;
9724#endif
9725 /* Every character that appears is a word character. */
9726 if (f < 256)
9727 new_st.st_isw[f] = TRUE;
9728 if (l < 256)
9729 new_st.st_isw[l] = TRUE;
9730 if (u < 256)
9731 new_st.st_isw[u] = TRUE;
9732
9733 /* if "LOW" and "FOL" are not the same the "LOW" char needs
9734 * case-folding */
9735 if (l < 256 && l != f)
9736 {
9737 if (f >= 256)
9738 {
9739 EMSG(_(e_affrange));
9740 return FAIL;
9741 }
9742 new_st.st_fold[l] = f;
9743 }
9744
9745 /* if "UPP" and "FOL" are not the same the "UPP" char needs
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009746 * case-folding, it's upper case and the "UPP" is the upper case of
9747 * "FOL" . */
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009748 if (u < 256 && u != f)
9749 {
9750 if (f >= 256)
9751 {
9752 EMSG(_(e_affrange));
9753 return FAIL;
9754 }
9755 new_st.st_fold[u] = f;
9756 new_st.st_isu[u] = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009757 new_st.st_upper[f] = u;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009758 }
9759 }
9760
9761 if (*pl != NUL || *pu != NUL)
9762 {
9763 EMSG(_(e_affform));
9764 return FAIL;
9765 }
9766
9767 return set_spell_finish(&new_st);
9768}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009769
9770/*
9771 * Set the spell character tables from strings in the .spl file.
9772 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009773 static void
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009774set_spell_charflags(flags, cnt, fol)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009775 char_u *flags;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009776 int cnt; /* length of "flags" */
9777 char_u *fol;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009778{
9779 /* We build the new tables here first, so that we can compare with the
9780 * previous one. */
9781 spelltab_T new_st;
9782 int i;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009783 char_u *p = fol;
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009784 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009785
9786 clear_spell_chartab(&new_st);
9787
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009788 for (i = 0; i < 128; ++i)
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009789 {
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009790 if (i < cnt)
9791 {
9792 new_st.st_isw[i + 128] = (flags[i] & CF_WORD) != 0;
9793 new_st.st_isu[i + 128] = (flags[i] & CF_UPPER) != 0;
9794 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009795
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009796 if (*p != NUL)
9797 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009798#ifdef FEAT_MBYTE
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009799 c = mb_ptr2char_adv(&p);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009800#else
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009801 c = *p++;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009802#endif
Bram Moolenaar0dc065e2005-07-04 22:49:24 +00009803 new_st.st_fold[i + 128] = c;
9804 if (i + 128 != c && new_st.st_isu[i + 128] && c < 256)
9805 new_st.st_upper[c] = i + 128;
9806 }
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009807 }
9808
Bram Moolenaar5195e452005-08-19 20:32:47 +00009809 (void)set_spell_finish(&new_st);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009810}
9811
9812 static int
9813set_spell_finish(new_st)
9814 spelltab_T *new_st;
9815{
9816 int i;
9817
9818 if (did_set_spelltab)
9819 {
9820 /* check that it's the same table */
9821 for (i = 0; i < 256; ++i)
9822 {
9823 if (spelltab.st_isw[i] != new_st->st_isw[i]
9824 || spelltab.st_isu[i] != new_st->st_isu[i]
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009825 || spelltab.st_fold[i] != new_st->st_fold[i]
9826 || spelltab.st_upper[i] != new_st->st_upper[i])
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009827 {
9828 EMSG(_("E763: Word characters differ between spell files"));
9829 return FAIL;
9830 }
9831 }
9832 }
9833 else
9834 {
9835 /* copy the new spelltab into the one being used */
9836 spelltab = *new_st;
9837 did_set_spelltab = TRUE;
9838 }
9839
9840 return OK;
9841}
9842
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009843/*
Bram Moolenaarea408852005-06-25 22:49:46 +00009844 * Return TRUE if "p" points to a word character.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009845 * As a special case we see "midword" characters as word character when it is
Bram Moolenaarea408852005-06-25 22:49:46 +00009846 * followed by a word character. This finds they'there but not 'they there'.
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009847 * Thus this only works properly when past the first character of the word.
Bram Moolenaarea408852005-06-25 22:49:46 +00009848 */
9849 static int
Bram Moolenaar860cae12010-06-05 23:22:07 +02009850spell_iswordp(p, wp)
Bram Moolenaarea408852005-06-25 22:49:46 +00009851 char_u *p;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009852 win_T *wp; /* buffer used */
Bram Moolenaarea408852005-06-25 22:49:46 +00009853{
Bram Moolenaarea408852005-06-25 22:49:46 +00009854#ifdef FEAT_MBYTE
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009855 char_u *s;
9856 int l;
9857 int c;
9858
9859 if (has_mbyte)
9860 {
9861 l = MB_BYTE2LEN(*p);
9862 s = p;
9863 if (l == 1)
9864 {
9865 /* be quick for ASCII */
Bram Moolenaar860cae12010-06-05 23:22:07 +02009866 if (wp->w_s->b_spell_ismw[*p])
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009867 s = p + 1; /* skip a mid-word character */
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009868 }
9869 else
9870 {
9871 c = mb_ptr2char(p);
Bram Moolenaar860cae12010-06-05 23:22:07 +02009872 if (c < 256 ? wp->w_s->b_spell_ismw[c]
9873 : (wp->w_s->b_spell_ismw_mb != NULL
9874 && vim_strchr(wp->w_s->b_spell_ismw_mb, c) != NULL))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009875 s = p + l;
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009876 }
9877
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009878 c = mb_ptr2char(s);
9879 if (c > 255)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009880 return spell_mb_isword_class(mb_get_class(s));
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009881 return spelltab.st_isw[c];
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009882 }
Bram Moolenaarea408852005-06-25 22:49:46 +00009883#endif
Bram Moolenaarcf6bf392005-06-27 22:27:46 +00009884
Bram Moolenaar860cae12010-06-05 23:22:07 +02009885 return spelltab.st_isw[wp->w_s->b_spell_ismw[*p] ? p[1] : p[0]];
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009886}
9887
9888/*
9889 * Return TRUE if "p" points to a word character.
9890 * Unlike spell_iswordp() this doesn't check for "midword" characters.
9891 */
9892 static int
9893spell_iswordp_nmw(p)
9894 char_u *p;
9895{
9896#ifdef FEAT_MBYTE
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009897 int c;
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009898
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009899 if (has_mbyte)
9900 {
9901 c = mb_ptr2char(p);
9902 if (c > 255)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009903 return spell_mb_isword_class(mb_get_class(p));
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009904 return spelltab.st_isw[c];
9905 }
9906#endif
Bram Moolenaar9c96f592005-06-30 21:52:39 +00009907 return spelltab.st_isw[*p];
Bram Moolenaarea408852005-06-25 22:49:46 +00009908}
9909
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009910#ifdef FEAT_MBYTE
9911/*
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009912 * Return TRUE if word class indicates a word character.
9913 * Only for characters above 255.
9914 * Unicode subscript and superscript are not considered word characters.
9915 */
9916 static int
9917spell_mb_isword_class(cl)
9918 int cl;
9919{
9920 return cl >= 2 && cl != 0x2070 && cl != 0x2080;
9921}
9922
9923/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009924 * Return TRUE if "p" points to a word character.
9925 * Wide version of spell_iswordp().
9926 */
9927 static int
Bram Moolenaar860cae12010-06-05 23:22:07 +02009928spell_iswordp_w(p, wp)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009929 int *p;
Bram Moolenaar860cae12010-06-05 23:22:07 +02009930 win_T *wp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009931{
9932 int *s;
9933
Bram Moolenaar860cae12010-06-05 23:22:07 +02009934 if (*p < 256 ? wp->w_s->b_spell_ismw[*p]
9935 : (wp->w_s->b_spell_ismw_mb != NULL
9936 && vim_strchr(wp->w_s->b_spell_ismw_mb, *p) != NULL))
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009937 s = p + 1;
9938 else
9939 s = p;
9940
Bram Moolenaardfb9ac02005-07-05 21:36:03 +00009941 if (*s > 255)
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009942 {
9943 if (enc_utf8)
Bram Moolenaar7a91a4a2008-04-09 13:49:57 +00009944 return spell_mb_isword_class(utf_class(*s));
Bram Moolenaara1ba8112005-06-28 23:23:32 +00009945 if (enc_dbcs)
9946 return dbcs_class((unsigned)*s >> 8, *s & 0xff) >= 2;
9947 return 0;
9948 }
9949 return spelltab.st_isw[*s];
9950}
9951#endif
9952
Bram Moolenaarea408852005-06-25 22:49:46 +00009953/*
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009954 * Write the table with prefix conditions to the .spl file.
Bram Moolenaar5195e452005-08-19 20:32:47 +00009955 * When "fd" is NULL only count the length of what is written.
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009956 */
Bram Moolenaar5195e452005-08-19 20:32:47 +00009957 static int
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009958write_spell_prefcond(fd, gap)
9959 FILE *fd;
9960 garray_T *gap;
9961{
9962 int i;
9963 char_u *p;
9964 int len;
Bram Moolenaar5195e452005-08-19 20:32:47 +00009965 int totlen;
Bram Moolenaar2eb6eb32008-11-29 19:19:19 +00009966 size_t x = 1; /* collect return value of fwrite() */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009967
Bram Moolenaar5195e452005-08-19 20:32:47 +00009968 if (fd != NULL)
9969 put_bytes(fd, (long_u)gap->ga_len, 2); /* <prefcondcnt> */
9970
9971 totlen = 2 + gap->ga_len; /* length of <prefcondcnt> and <condlen> bytes */
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009972
9973 for (i = 0; i < gap->ga_len; ++i)
9974 {
9975 /* <prefcond> : <condlen> <condstr> */
9976 p = ((char_u **)gap->ga_data)[i];
Bram Moolenaar5195e452005-08-19 20:32:47 +00009977 if (p != NULL)
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009978 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009979 len = (int)STRLEN(p);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009980 if (fd != NULL)
9981 {
9982 fputc(len, fd);
Bram Moolenaar3f3766b2008-11-28 09:08:51 +00009983 x &= fwrite(p, (size_t)len, (size_t)1, fd);
Bram Moolenaar5195e452005-08-19 20:32:47 +00009984 }
9985 totlen += len;
Bram Moolenaar1d73c882005-06-19 22:48:47 +00009986 }
Bram Moolenaar5195e452005-08-19 20:32:47 +00009987 else if (fd != NULL)
9988 fputc(0, fd);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009989 }
9990
Bram Moolenaar5195e452005-08-19 20:32:47 +00009991 return totlen;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009992}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009993
9994/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +00009995 * Case-fold "str[len]" into "buf[buflen]". The result is NUL terminated.
9996 * Uses the character definitions from the .spl file.
Bram Moolenaarcfc6c432005-06-06 21:50:35 +00009997 * When using a multi-byte 'encoding' the length may change!
9998 * Returns FAIL when something wrong.
9999 */
10000 static int
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010001spell_casefold(str, len, buf, buflen)
10002 char_u *str;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010003 int len;
10004 char_u *buf;
10005 int buflen;
10006{
10007 int i;
10008
10009 if (len >= buflen)
10010 {
10011 buf[0] = NUL;
10012 return FAIL; /* result will not fit */
10013 }
10014
10015#ifdef FEAT_MBYTE
10016 if (has_mbyte)
10017 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010018 int outi = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010019 char_u *p;
10020 int c;
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010021
10022 /* Fold one character at a time. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010023 for (p = str; p < str + len; )
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010024 {
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010025 if (outi + MB_MAXBYTES > buflen)
10026 {
10027 buf[outi] = NUL;
10028 return FAIL;
10029 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010030 c = mb_cptr2char_adv(&p);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010031 outi += mb_char2bytes(SPELL_TOFOLD(c), buf + outi);
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010032 }
10033 buf[outi] = NUL;
10034 }
10035 else
10036#endif
10037 {
10038 /* Be quick for non-multibyte encodings. */
10039 for (i = 0; i < len; ++i)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010040 buf[i] = spelltab.st_fold[str[i]];
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000010041 buf[i] = NUL;
10042 }
10043
10044 return OK;
10045}
10046
Bram Moolenaar4770d092006-01-12 23:22:24 +000010047/* values for sps_flags */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010048#define SPS_BEST 1
10049#define SPS_FAST 2
10050#define SPS_DOUBLE 4
10051
Bram Moolenaar4770d092006-01-12 23:22:24 +000010052static int sps_flags = SPS_BEST; /* flags from 'spellsuggest' */
10053static int sps_limit = 9999; /* max nr of suggestions given */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010054
10055/*
10056 * Check the 'spellsuggest' option. Return FAIL if it's wrong.
Bram Moolenaar5195e452005-08-19 20:32:47 +000010057 * Sets "sps_flags" and "sps_limit".
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010058 */
10059 int
10060spell_check_sps()
10061{
10062 char_u *p;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010063 char_u *s;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010064 char_u buf[MAXPATHL];
10065 int f;
10066
10067 sps_flags = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010068 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010069
10070 for (p = p_sps; *p != NUL; )
10071 {
10072 copy_option_part(&p, buf, MAXPATHL, ",");
10073
10074 f = 0;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010075 if (VIM_ISDIGIT(*buf))
10076 {
10077 s = buf;
10078 sps_limit = getdigits(&s);
10079 if (*s != NUL && !VIM_ISDIGIT(*s))
10080 f = -1;
10081 }
10082 else if (STRCMP(buf, "best") == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010083 f = SPS_BEST;
10084 else if (STRCMP(buf, "fast") == 0)
10085 f = SPS_FAST;
10086 else if (STRCMP(buf, "double") == 0)
10087 f = SPS_DOUBLE;
10088 else if (STRNCMP(buf, "expr:", 5) != 0
10089 && STRNCMP(buf, "file:", 5) != 0)
10090 f = -1;
10091
10092 if (f == -1 || (sps_flags != 0 && f != 0))
10093 {
10094 sps_flags = SPS_BEST;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010095 sps_limit = 9999;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010096 return FAIL;
10097 }
10098 if (f != 0)
10099 sps_flags = f;
10100 }
10101
10102 if (sps_flags == 0)
10103 sps_flags = SPS_BEST;
10104
10105 return OK;
10106}
10107
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010108/*
10109 * "z?": Find badly spelled word under or after the cursor.
10110 * Give suggestions for the properly spelled word.
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010111 * In Visual mode use the highlighted word as the bad word.
Bram Moolenaard12a1322005-08-21 22:08:24 +000010112 * When "count" is non-zero use that suggestion.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010113 */
10114 void
Bram Moolenaard12a1322005-08-21 22:08:24 +000010115spell_suggest(count)
10116 int count;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010117{
10118 char_u *line;
10119 pos_T prev_cursor = curwin->w_cursor;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010120 char_u wcopy[MAXWLEN + 2];
10121 char_u *p;
10122 int i;
10123 int c;
10124 suginfo_T sug;
10125 suggest_T *stp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010126 int mouse_used;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010127 int need_cap;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010128 int limit;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010129 int selected = count;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010130 int badlen = 0;
Bram Moolenaarb2450162009-07-22 09:04:20 +000010131 int msg_scroll_save = msg_scroll;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010132
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010133 if (no_spell_checking(curwin))
10134 return;
10135
10136#ifdef FEAT_VISUAL
10137 if (VIsual_active)
10138 {
10139 /* Use the Visually selected text as the bad word. But reject
10140 * a multi-line selection. */
10141 if (curwin->w_cursor.lnum != VIsual.lnum)
10142 {
10143 vim_beep();
10144 return;
10145 }
10146 badlen = (int)curwin->w_cursor.col - (int)VIsual.col;
10147 if (badlen < 0)
10148 badlen = -badlen;
10149 else
10150 curwin->w_cursor.col = VIsual.col;
10151 ++badlen;
10152 end_visual_mode();
10153 }
10154 else
10155#endif
10156 /* Find the start of the badly spelled word. */
10157 if (spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL) == 0
Bram Moolenaar0c405862005-06-22 22:26:26 +000010158 || curwin->w_cursor.col > prev_cursor.col)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010159 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010160 /* No bad word or it starts after the cursor: use the word under the
10161 * cursor. */
10162 curwin->w_cursor = prev_cursor;
10163 line = ml_get_curline();
10164 p = line + curwin->w_cursor.col;
10165 /* Backup to before start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010166 while (p > line && spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010167 mb_ptr_back(line, p);
10168 /* Forward to start of word. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010169 while (*p != NUL && !spell_iswordp_nmw(p))
Bram Moolenaar0c405862005-06-22 22:26:26 +000010170 mb_ptr_adv(p);
10171
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000010172 if (!spell_iswordp_nmw(p)) /* No word found. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010173 {
10174 beep_flush();
10175 return;
10176 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010177 curwin->w_cursor.col = (colnr_T)(p - line);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010178 }
10179
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010180 /* Get the word and its length. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010181
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010182 /* Figure out if the word should be capitalised. */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010183 need_cap = check_need_cap(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010184
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010185 /* Make a copy of current line since autocommands may free the line. */
10186 line = vim_strsave(ml_get_curline());
10187 if (line == NULL)
10188 goto skip;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010189
Bram Moolenaar5195e452005-08-19 20:32:47 +000010190 /* Get the list of suggestions. Limit to 'lines' - 2 or the number in
10191 * 'spellsuggest', whatever is smaller. */
10192 if (sps_limit > (int)Rows - 2)
10193 limit = (int)Rows - 2;
10194 else
10195 limit = sps_limit;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010196 spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010197 TRUE, need_cap, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010198
10199 if (sug.su_ga.ga_len == 0)
10200 MSG(_("Sorry, no suggestions"));
Bram Moolenaard12a1322005-08-21 22:08:24 +000010201 else if (count > 0)
10202 {
10203 if (count > sug.su_ga.ga_len)
10204 smsg((char_u *)_("Sorry, only %ld suggestions"),
10205 (long)sug.su_ga.ga_len);
10206 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010207 else
10208 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010209 vim_free(repl_from);
10210 repl_from = NULL;
10211 vim_free(repl_to);
10212 repl_to = NULL;
10213
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010214#ifdef FEAT_RIGHTLEFT
10215 /* When 'rightleft' is set the list is drawn right-left. */
10216 cmdmsg_rl = curwin->w_p_rl;
10217 if (cmdmsg_rl)
10218 msg_col = Columns - 1;
10219#endif
10220
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010221 /* List the suggestions. */
10222 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000010223 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010224 lines_left = Rows; /* avoid more prompt */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010225 vim_snprintf((char *)IObuff, IOSIZE, _("Change \"%.*s\" to:"),
10226 sug.su_badlen, sug.su_badptr);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010227#ifdef FEAT_RIGHTLEFT
10228 if (cmdmsg_rl && STRNCMP(IObuff, "Change", 6) == 0)
10229 {
10230 /* And now the rabbit from the high hat: Avoid showing the
10231 * untranslated message rightleft. */
10232 vim_snprintf((char *)IObuff, IOSIZE, ":ot \"%.*s\" egnahC",
10233 sug.su_badlen, sug.su_badptr);
10234 }
10235#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010236 msg_puts(IObuff);
10237 msg_clr_eos();
10238 msg_putchar('\n');
Bram Moolenaar0c405862005-06-22 22:26:26 +000010239
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010240 msg_scroll = TRUE;
10241 for (i = 0; i < sug.su_ga.ga_len; ++i)
10242 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010243 stp = &SUG(sug.su_ga, i);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010244
10245 /* The suggested word may replace only part of the bad word, add
10246 * the not replaced part. */
10247 STRCPY(wcopy, stp->st_word);
10248 if (sug.su_badlen > stp->st_orglen)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010249 vim_strncpy(wcopy + stp->st_wordlen,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010250 sug.su_badptr + stp->st_orglen,
10251 sug.su_badlen - stp->st_orglen);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010252 vim_snprintf((char *)IObuff, IOSIZE, "%2d", i + 1);
10253#ifdef FEAT_RIGHTLEFT
10254 if (cmdmsg_rl)
10255 rl_mirror(IObuff);
10256#endif
10257 msg_puts(IObuff);
10258
10259 vim_snprintf((char *)IObuff, IOSIZE, " \"%s\"", wcopy);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010260 msg_puts(IObuff);
10261
10262 /* The word may replace more than "su_badlen". */
10263 if (sug.su_badlen < stp->st_orglen)
10264 {
10265 vim_snprintf((char *)IObuff, IOSIZE, _(" < \"%.*s\""),
10266 stp->st_orglen, sug.su_badptr);
10267 msg_puts(IObuff);
10268 }
10269
Bram Moolenaar9f30f502005-06-14 22:01:04 +000010270 if (p_verbose > 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010271 {
Bram Moolenaar0c405862005-06-22 22:26:26 +000010272 /* Add the score. */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000010273 if (sps_flags & (SPS_DOUBLE | SPS_BEST))
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010274 vim_snprintf((char *)IObuff, IOSIZE, " (%s%d - %d)",
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010275 stp->st_salscore ? "s " : "",
10276 stp->st_score, stp->st_altscore);
10277 else
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010278 vim_snprintf((char *)IObuff, IOSIZE, " (%d)",
Bram Moolenaar0c405862005-06-22 22:26:26 +000010279 stp->st_score);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010280#ifdef FEAT_RIGHTLEFT
10281 if (cmdmsg_rl)
10282 /* Mirror the numbers, but keep the leading space. */
10283 rl_mirror(IObuff + 1);
10284#endif
Bram Moolenaar0c405862005-06-22 22:26:26 +000010285 msg_advance(30);
10286 msg_puts(IObuff);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010287 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010288 msg_putchar('\n');
10289 }
10290
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010291#ifdef FEAT_RIGHTLEFT
10292 cmdmsg_rl = FALSE;
10293 msg_col = 0;
10294#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010295 /* Ask for choice. */
Bram Moolenaard12a1322005-08-21 22:08:24 +000010296 selected = prompt_for_number(&mouse_used);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010297 if (mouse_used)
Bram Moolenaard12a1322005-08-21 22:08:24 +000010298 selected -= lines_left;
Bram Moolenaarb2450162009-07-22 09:04:20 +000010299 lines_left = Rows; /* avoid more prompt */
10300 /* don't delay for 'smd' in normal_cmd() */
10301 msg_scroll = msg_scroll_save;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010302 }
10303
Bram Moolenaard12a1322005-08-21 22:08:24 +000010304 if (selected > 0 && selected <= sug.su_ga.ga_len && u_save_cursor() == OK)
10305 {
10306 /* Save the from and to text for :spellrepall. */
10307 stp = &SUG(sug.su_ga, selected - 1);
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000010308 if (sug.su_badlen > stp->st_orglen)
10309 {
10310 /* Replacing less than "su_badlen", append the remainder to
10311 * repl_to. */
10312 repl_from = vim_strnsave(sug.su_badptr, sug.su_badlen);
10313 vim_snprintf((char *)IObuff, IOSIZE, "%s%.*s", stp->st_word,
10314 sug.su_badlen - stp->st_orglen,
10315 sug.su_badptr + stp->st_orglen);
10316 repl_to = vim_strsave(IObuff);
10317 }
10318 else
10319 {
10320 /* Replacing su_badlen or more, use the whole word. */
10321 repl_from = vim_strnsave(sug.su_badptr, stp->st_orglen);
10322 repl_to = vim_strsave(stp->st_word);
10323 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000010324
10325 /* Replace the word. */
Bram Moolenaarb2450162009-07-22 09:04:20 +000010326 p = alloc((unsigned)STRLEN(line) - stp->st_orglen
10327 + stp->st_wordlen + 1);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010328 if (p != NULL)
10329 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010330 c = (int)(sug.su_badptr - line);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010331 mch_memmove(p, line, c);
10332 STRCPY(p + c, stp->st_word);
10333 STRCAT(p, sug.su_badptr + stp->st_orglen);
10334 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10335 curwin->w_cursor.col = c;
Bram Moolenaard12a1322005-08-21 22:08:24 +000010336
10337 /* For redo we use a change-word command. */
10338 ResetRedobuff();
10339 AppendToRedobuff((char_u *)"ciw");
Bram Moolenaarebefac62005-12-28 22:39:57 +000010340 AppendToRedobuffLit(p + c,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010341 stp->st_wordlen + sug.su_badlen - stp->st_orglen);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010342 AppendCharToRedobuff(ESC);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010343
10344 /* After this "p" may be invalid. */
10345 changed_bytes(curwin->w_cursor.lnum, c);
Bram Moolenaard12a1322005-08-21 22:08:24 +000010346 }
10347 }
10348 else
10349 curwin->w_cursor = prev_cursor;
10350
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010351 spell_find_cleanup(&sug);
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010352skip:
10353 vim_free(line);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010354}
10355
10356/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010357 * Check if the word at line "lnum" column "col" is required to start with a
10358 * capital. This uses 'spellcapcheck' of the current buffer.
10359 */
10360 static int
10361check_need_cap(lnum, col)
10362 linenr_T lnum;
10363 colnr_T col;
10364{
10365 int need_cap = FALSE;
10366 char_u *line;
10367 char_u *line_copy = NULL;
10368 char_u *p;
10369 colnr_T endcol;
10370 regmatch_T regmatch;
10371
Bram Moolenaar860cae12010-06-05 23:22:07 +020010372 if (curwin->w_s->b_cap_prog == NULL)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010373 return FALSE;
10374
10375 line = ml_get_curline();
10376 endcol = 0;
10377 if ((int)(skipwhite(line) - line) >= (int)col)
10378 {
10379 /* At start of line, check if previous line is empty or sentence
10380 * ends there. */
10381 if (lnum == 1)
10382 need_cap = TRUE;
10383 else
10384 {
10385 line = ml_get(lnum - 1);
10386 if (*skipwhite(line) == NUL)
10387 need_cap = TRUE;
10388 else
10389 {
10390 /* Append a space in place of the line break. */
10391 line_copy = concat_str(line, (char_u *)" ");
10392 line = line_copy;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010393 endcol = (colnr_T)STRLEN(line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010394 }
10395 }
10396 }
10397 else
10398 endcol = col;
10399
10400 if (endcol > 0)
10401 {
10402 /* Check if sentence ends before the bad word. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010403 regmatch.regprog = curwin->w_s->b_cap_prog;
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010404 regmatch.rm_ic = FALSE;
10405 p = line + endcol;
10406 for (;;)
10407 {
10408 mb_ptr_back(line, p);
10409 if (p == line || spell_iswordp_nmw(p))
10410 break;
10411 if (vim_regexec(&regmatch, p, 0)
10412 && regmatch.endp[0] == line + endcol)
10413 {
10414 need_cap = TRUE;
10415 break;
10416 }
10417 }
10418 }
10419
10420 vim_free(line_copy);
10421
10422 return need_cap;
10423}
10424
10425
10426/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010427 * ":spellrepall"
10428 */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010429 void
10430ex_spellrepall(eap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000010431 exarg_T *eap UNUSED;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010432{
10433 pos_T pos = curwin->w_cursor;
10434 char_u *frompat;
10435 int addlen;
10436 char_u *line;
10437 char_u *p;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010438 int save_ws = p_ws;
Bram Moolenaar5195e452005-08-19 20:32:47 +000010439 linenr_T prev_lnum = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010440
10441 if (repl_from == NULL || repl_to == NULL)
10442 {
10443 EMSG(_("E752: No previous spell replacement"));
10444 return;
10445 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010446 addlen = (int)(STRLEN(repl_to) - STRLEN(repl_from));
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010447
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010448 frompat = alloc((unsigned)STRLEN(repl_from) + 7);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010449 if (frompat == NULL)
10450 return;
10451 sprintf((char *)frompat, "\\V\\<%s\\>", repl_from);
10452 p_ws = FALSE;
10453
Bram Moolenaar5195e452005-08-19 20:32:47 +000010454 sub_nsubs = 0;
10455 sub_nlines = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010456 curwin->w_cursor.lnum = 0;
10457 while (!got_int)
10458 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +000010459 if (do_search(NULL, '/', frompat, 1L, SEARCH_KEEP, NULL) == 0
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010460 || u_save_cursor() == FAIL)
10461 break;
10462
10463 /* Only replace when the right word isn't there yet. This happens
10464 * when changing "etc" to "etc.". */
10465 line = ml_get_curline();
10466 if (addlen <= 0 || STRNCMP(line + curwin->w_cursor.col,
10467 repl_to, STRLEN(repl_to)) != 0)
10468 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010469 p = alloc((unsigned)STRLEN(line) + addlen + 1);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010470 if (p == NULL)
10471 break;
10472 mch_memmove(p, line, curwin->w_cursor.col);
10473 STRCPY(p + curwin->w_cursor.col, repl_to);
10474 STRCAT(p, line + curwin->w_cursor.col + STRLEN(repl_from));
10475 ml_replace(curwin->w_cursor.lnum, p, FALSE);
10476 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010477
10478 if (curwin->w_cursor.lnum != prev_lnum)
10479 {
10480 ++sub_nlines;
10481 prev_lnum = curwin->w_cursor.lnum;
10482 }
10483 ++sub_nsubs;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010484 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010485 curwin->w_cursor.col += (colnr_T)STRLEN(repl_to);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010486 }
10487
10488 p_ws = save_ws;
10489 curwin->w_cursor = pos;
10490 vim_free(frompat);
10491
Bram Moolenaar5195e452005-08-19 20:32:47 +000010492 if (sub_nsubs == 0)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010493 EMSG2(_("E753: Not found: %s"), repl_from);
Bram Moolenaar5195e452005-08-19 20:32:47 +000010494 else
10495 do_sub_msg(FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010496}
10497
10498/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010499 * Find spell suggestions for "word". Return them in the growarray "*gap" as
10500 * a list of allocated strings.
10501 */
10502 void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010503spell_suggest_list(gap, word, maxcount, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010504 garray_T *gap;
10505 char_u *word;
10506 int maxcount; /* maximum nr of suggestions */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000010507 int need_cap; /* 'spellcapcheck' matched */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010508 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010509{
10510 suginfo_T sug;
10511 int i;
10512 suggest_T *stp;
10513 char_u *wcopy;
10514
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010515 spell_find_suggest(word, 0, &sug, maxcount, FALSE, need_cap, interactive);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010516
10517 /* Make room in "gap". */
10518 ga_init2(gap, sizeof(char_u *), sug.su_ga.ga_len + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010519 if (ga_grow(gap, sug.su_ga.ga_len) == OK)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010520 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010521 for (i = 0; i < sug.su_ga.ga_len; ++i)
10522 {
10523 stp = &SUG(sug.su_ga, i);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010524
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010525 /* The suggested word may replace only part of "word", add the not
10526 * replaced part. */
10527 wcopy = alloc(stp->st_wordlen
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010528 + (unsigned)STRLEN(sug.su_badptr + stp->st_orglen) + 1);
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010529 if (wcopy == NULL)
10530 break;
10531 STRCPY(wcopy, stp->st_word);
10532 STRCPY(wcopy + stp->st_wordlen, sug.su_badptr + stp->st_orglen);
10533 ((char_u **)gap->ga_data)[gap->ga_len++] = wcopy;
10534 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010535 }
10536
10537 spell_find_cleanup(&sug);
10538}
10539
10540/*
10541 * Find spell suggestions for the word at the start of "badptr".
10542 * Return the suggestions in "su->su_ga".
10543 * The maximum number of suggestions is "maxcount".
10544 * Note: does use info for the current window.
10545 * This is based on the mechanisms of Aspell, but completely reimplemented.
10546 */
10547 static void
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010548spell_find_suggest(badptr, badlen, su, maxcount, banbadword, need_cap, interactive)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010549 char_u *badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010550 int badlen; /* length of bad word or 0 if unknown */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010551 suginfo_T *su;
10552 int maxcount;
Bram Moolenaarea408852005-06-25 22:49:46 +000010553 int banbadword; /* don't include badword in suggestions */
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010554 int need_cap; /* word should start with capital */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010555 int interactive;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010556{
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010557 hlf_T attr = HLF_COUNT;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010558 char_u buf[MAXPATHL];
10559 char_u *p;
10560 int do_combine = FALSE;
10561 char_u *sps_copy;
10562#ifdef FEAT_EVAL
10563 static int expr_busy = FALSE;
10564#endif
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010565 int c;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010566 int i;
10567 langp_T *lp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010568
10569 /*
10570 * Set the info in "*su".
10571 */
10572 vim_memset(su, 0, sizeof(suginfo_T));
10573 ga_init2(&su->su_ga, (int)sizeof(suggest_T), 10);
10574 ga_init2(&su->su_sga, (int)sizeof(suggest_T), 10);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000010575 if (*badptr == NUL)
10576 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010577 hash_init(&su->su_banned);
10578
10579 su->su_badptr = badptr;
Bram Moolenaar66fa2712006-01-22 23:22:22 +000010580 if (badlen != 0)
10581 su->su_badlen = badlen;
10582 else
10583 su->su_badlen = spell_check(curwin, su->su_badptr, &attr, NULL, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010584 su->su_maxcount = maxcount;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010585 su->su_maxscore = SCORE_MAXINIT;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010586
10587 if (su->su_badlen >= MAXWLEN)
10588 su->su_badlen = MAXWLEN - 1; /* just in case */
10589 vim_strncpy(su->su_badword, su->su_badptr, su->su_badlen);
10590 (void)spell_casefold(su->su_badptr, su->su_badlen,
10591 su->su_fbadword, MAXWLEN);
Bram Moolenaar0c405862005-06-22 22:26:26 +000010592 /* get caps flags for bad word */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010593 su->su_badflags = badword_captype(su->su_badptr,
10594 su->su_badptr + su->su_badlen);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +000010595 if (need_cap)
10596 su->su_badflags |= WF_ONECAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010597
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010598 /* Find the default language for sound folding. We simply use the first
10599 * one in 'spelllang' that supports sound folding. That's good for when
10600 * using multiple files for one language, it's not that bad when mixing
10601 * languages (e.g., "pl,en"). */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010602 for (i = 0; i < curbuf->b_s.b_langp.ga_len; ++i)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010603 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020010604 lp = LANGP_ENTRY(curbuf->b_s.b_langp, i);
Bram Moolenaar8b96d642005-09-05 22:05:30 +000010605 if (lp->lp_sallang != NULL)
10606 {
10607 su->su_sallang = lp->lp_sallang;
10608 break;
10609 }
10610 }
10611
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010612 /* Soundfold the bad word with the default sound folding, so that we don't
10613 * have to do this many times. */
10614 if (su->su_sallang != NULL)
10615 spell_soundfold(su->su_sallang, su->su_fbadword, TRUE,
10616 su->su_sal_badword);
10617
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010618 /* If the word is not capitalised and spell_check() doesn't consider the
10619 * word to be bad then it might need to be capitalised. Add a suggestion
10620 * for that. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000010621 c = PTR2CHAR(su->su_badptr);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010622 if (!SPELL_ISUPPER(c) && attr == HLF_COUNT)
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010623 {
10624 make_case_word(su->su_badword, buf, WF_ONECAP);
10625 add_suggestion(su, &su->su_ga, buf, su->su_badlen, SCORE_ICASE,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010626 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaarf9184a12005-07-02 23:10:47 +000010627 }
10628
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010629 /* Ban the bad word itself. It may appear in another region. */
Bram Moolenaarea408852005-06-25 22:49:46 +000010630 if (banbadword)
10631 add_banned(su, su->su_badword);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010632
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010633 /* Make a copy of 'spellsuggest', because the expression may change it. */
10634 sps_copy = vim_strsave(p_sps);
10635 if (sps_copy == NULL)
10636 return;
10637
10638 /* Loop over the items in 'spellsuggest'. */
10639 for (p = sps_copy; *p != NUL; )
10640 {
10641 copy_option_part(&p, buf, MAXPATHL, ",");
10642
10643 if (STRNCMP(buf, "expr:", 5) == 0)
10644 {
10645#ifdef FEAT_EVAL
Bram Moolenaar42eeac32005-06-29 22:40:58 +000010646 /* Evaluate an expression. Skip this when called recursively,
10647 * when using spellsuggest() in the expression. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010648 if (!expr_busy)
10649 {
10650 expr_busy = TRUE;
10651 spell_suggest_expr(su, buf + 5);
10652 expr_busy = FALSE;
10653 }
10654#endif
10655 }
10656 else if (STRNCMP(buf, "file:", 5) == 0)
10657 /* Use list of suggestions in a file. */
10658 spell_suggest_file(su, buf + 5);
10659 else
10660 {
10661 /* Use internal method. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010662 spell_suggest_intern(su, interactive);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010663 if (sps_flags & SPS_DOUBLE)
10664 do_combine = TRUE;
10665 }
10666 }
10667
10668 vim_free(sps_copy);
10669
10670 if (do_combine)
10671 /* Combine the two list of suggestions. This must be done last,
10672 * because sorting changes the order again. */
10673 score_combine(su);
10674}
10675
10676#ifdef FEAT_EVAL
10677/*
10678 * Find suggestions by evaluating expression "expr".
10679 */
10680 static void
10681spell_suggest_expr(su, expr)
10682 suginfo_T *su;
10683 char_u *expr;
10684{
10685 list_T *list;
10686 listitem_T *li;
10687 int score;
10688 char_u *p;
10689
10690 /* The work is split up in a few parts to avoid having to export
10691 * suginfo_T.
10692 * First evaluate the expression and get the resulting list. */
10693 list = eval_spell_expr(su->su_badword, expr);
10694 if (list != NULL)
10695 {
10696 /* Loop over the items in the list. */
10697 for (li = list->lv_first; li != NULL; li = li->li_next)
10698 if (li->li_tv.v_type == VAR_LIST)
10699 {
10700 /* Get the word and the score from the items. */
10701 score = get_spellword(li->li_tv.vval.v_list, &p);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010702 if (score >= 0 && score <= su->su_maxscore)
10703 add_suggestion(su, &su->su_ga, p, su->su_badlen,
10704 score, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010705 }
10706 list_unref(list);
10707 }
10708
Bram Moolenaar4770d092006-01-12 23:22:24 +000010709 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10710 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010711 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10712}
10713#endif
10714
10715/*
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010716 * Find suggestions in file "fname". Used for "file:" in 'spellsuggest'.
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010717 */
10718 static void
10719spell_suggest_file(su, fname)
10720 suginfo_T *su;
10721 char_u *fname;
10722{
10723 FILE *fd;
10724 char_u line[MAXWLEN * 2];
10725 char_u *p;
10726 int len;
10727 char_u cword[MAXWLEN];
10728
10729 /* Open the file. */
10730 fd = mch_fopen((char *)fname, "r");
10731 if (fd == NULL)
10732 {
10733 EMSG2(_(e_notopen), fname);
10734 return;
10735 }
10736
10737 /* Read it line by line. */
10738 while (!vim_fgets(line, MAXWLEN * 2, fd) && !got_int)
10739 {
10740 line_breakcheck();
10741
10742 p = vim_strchr(line, '/');
10743 if (p == NULL)
10744 continue; /* No Tab found, just skip the line. */
10745 *p++ = NUL;
10746 if (STRICMP(su->su_badword, line) == 0)
10747 {
10748 /* Match! Isolate the good word, until CR or NL. */
10749 for (len = 0; p[len] >= ' '; ++len)
10750 ;
10751 p[len] = NUL;
10752
10753 /* If the suggestion doesn't have specific case duplicate the case
10754 * of the bad word. */
10755 if (captype(p, NULL) == 0)
10756 {
10757 make_case_word(p, cword, su->su_badflags);
10758 p = cword;
10759 }
10760
10761 add_suggestion(su, &su->su_ga, p, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000010762 SCORE_FILE, 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010763 }
10764 }
10765
10766 fclose(fd);
10767
Bram Moolenaar4770d092006-01-12 23:22:24 +000010768 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10769 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010770 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
10771}
10772
10773/*
10774 * Find suggestions for the internal method indicated by "sps_flags".
10775 */
10776 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000010777spell_suggest_intern(su, interactive)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010778 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000010779 int interactive;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010780{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010781 /*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010782 * Load the .sug file(s) that are available and not done yet.
10783 */
10784 suggest_load_files();
10785
10786 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010787 * 1. Try special cases, such as repeating a word: "the the" -> "the".
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010788 *
10789 * Set a maximum score to limit the combination of operations that is
10790 * tried.
10791 */
Bram Moolenaar0c405862005-06-22 22:26:26 +000010792 suggest_try_special(su);
10793
10794 /*
10795 * 2. Try inserting/deleting/swapping/changing a letter, use REP entries
10796 * from the .aff file and inserting a space (split the word).
10797 */
10798 suggest_try_change(su);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010799
10800 /* For the resulting top-scorers compute the sound-a-like score. */
10801 if (sps_flags & SPS_DOUBLE)
10802 score_comp_sal(su);
10803
10804 /*
Bram Moolenaar0c405862005-06-22 22:26:26 +000010805 * 3. Try finding sound-a-like words.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010806 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010807 if ((sps_flags & SPS_FAST) == 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010808 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000010809 if (sps_flags & SPS_BEST)
10810 /* Adjust the word score for the suggestions found so far for how
10811 * they sounds like. */
10812 rescore_suggestions(su);
10813
10814 /*
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010010815 * While going through the soundfold tree "su_maxscore" is the score
Bram Moolenaar4770d092006-01-12 23:22:24 +000010816 * for the soundfold word, limits the changes that are being tried,
10817 * and "su_sfmaxscore" the rescored score, which is set by
10818 * cleanup_suggestions().
10819 * First find words with a small edit distance, because this is much
10820 * faster and often already finds the top-N suggestions. If we didn't
10821 * find many suggestions try again with a higher edit distance.
10822 * "sl_sounddone" is used to avoid doing the same word twice.
10823 */
10824 suggest_try_soundalike_prep();
10825 su->su_maxscore = SCORE_SFMAX1;
10826 su->su_sfmaxscore = SCORE_MAXINIT * 3;
Bram Moolenaar0c405862005-06-22 22:26:26 +000010827 suggest_try_soundalike(su);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010828 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10829 {
10830 /* We didn't find enough matches, try again, allowing more
10831 * changes to the soundfold word. */
10832 su->su_maxscore = SCORE_SFMAX2;
10833 suggest_try_soundalike(su);
10834 if (su->su_ga.ga_len < SUG_CLEAN_COUNT(su))
10835 {
10836 /* Still didn't find enough matches, try again, allowing even
10837 * more changes to the soundfold word. */
10838 su->su_maxscore = SCORE_SFMAX3;
10839 suggest_try_soundalike(su);
10840 }
10841 }
10842 su->su_maxscore = su->su_sfmaxscore;
10843 suggest_try_soundalike_finish();
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010844 }
10845
Bram Moolenaar4770d092006-01-12 23:22:24 +000010846 /* When CTRL-C was hit while searching do show the results. Only clear
10847 * got_int when using a command, not for spellsuggest(). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010848 ui_breakcheck();
Bram Moolenaar4770d092006-01-12 23:22:24 +000010849 if (interactive && got_int)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010850 {
10851 (void)vgetc();
10852 got_int = FALSE;
10853 }
10854
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010855 if ((sps_flags & SPS_DOUBLE) == 0 && su->su_ga.ga_len != 0)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010856 {
10857 if (sps_flags & SPS_BEST)
10858 /* Adjust the word score for how it sounds like. */
10859 rescore_suggestions(su);
10860
Bram Moolenaar4770d092006-01-12 23:22:24 +000010861 /* Remove bogus suggestions, sort and truncate at "maxcount". */
10862 check_suggestions(su, &su->su_ga);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000010863 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000010864 }
10865}
10866
10867/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000010868 * Load the .sug files for languages that have one and weren't loaded yet.
10869 */
10870 static void
10871suggest_load_files()
10872{
10873 langp_T *lp;
10874 int lpi;
10875 slang_T *slang;
10876 char_u *dotp;
10877 FILE *fd;
10878 char_u buf[MAXWLEN];
10879 int i;
10880 time_t timestamp;
10881 int wcount;
10882 int wordnr;
10883 garray_T ga;
10884 int c;
10885
10886 /* Do this for all languages that support sound folding. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020010887 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000010888 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020010889 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010890 slang = lp->lp_slang;
10891 if (slang->sl_sugtime != 0 && !slang->sl_sugloaded)
10892 {
10893 /* Change ".spl" to ".sug" and open the file. When the file isn't
10894 * found silently skip it. Do set "sl_sugloaded" so that we
10895 * don't try again and again. */
10896 slang->sl_sugloaded = TRUE;
10897
10898 dotp = vim_strrchr(slang->sl_fname, '.');
10899 if (dotp == NULL || fnamecmp(dotp, ".spl") != 0)
10900 continue;
10901 STRCPY(dotp, ".sug");
Bram Moolenaar5555acc2006-04-07 21:33:12 +000010902 fd = mch_fopen((char *)slang->sl_fname, "r");
Bram Moolenaar4770d092006-01-12 23:22:24 +000010903 if (fd == NULL)
10904 goto nextone;
10905
10906 /*
10907 * <SUGHEADER>: <fileID> <versionnr> <timestamp>
10908 */
10909 for (i = 0; i < VIMSUGMAGICL; ++i)
10910 buf[i] = getc(fd); /* <fileID> */
10911 if (STRNCMP(buf, VIMSUGMAGIC, VIMSUGMAGICL) != 0)
10912 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010913 EMSG2(_("E778: This does not look like a .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010914 slang->sl_fname);
10915 goto nextone;
10916 }
10917 c = getc(fd); /* <versionnr> */
10918 if (c < VIMSUGVERSION)
10919 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010920 EMSG2(_("E779: Old .sug file, needs to be updated: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010921 slang->sl_fname);
10922 goto nextone;
10923 }
10924 else if (c > VIMSUGVERSION)
10925 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010926 EMSG2(_("E780: .sug file is for newer version of Vim: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010927 slang->sl_fname);
10928 goto nextone;
10929 }
10930
10931 /* Check the timestamp, it must be exactly the same as the one in
10932 * the .spl file. Otherwise the word numbers won't match. */
Bram Moolenaarcdf04202010-05-29 15:11:47 +020010933 timestamp = get8ctime(fd); /* <timestamp> */
Bram Moolenaar4770d092006-01-12 23:22:24 +000010934 if (timestamp != slang->sl_sugtime)
10935 {
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010936 EMSG2(_("E781: .sug file doesn't match .spl file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010937 slang->sl_fname);
10938 goto nextone;
10939 }
10940
10941 /*
10942 * <SUGWORDTREE>: <wordtree>
10943 * Read the trie with the soundfolded words.
10944 */
10945 if (spell_read_tree(fd, &slang->sl_sbyts, &slang->sl_sidxs,
10946 FALSE, 0) != 0)
10947 {
10948someerror:
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000010949 EMSG2(_("E782: error while reading .sug file: %s"),
Bram Moolenaar4770d092006-01-12 23:22:24 +000010950 slang->sl_fname);
10951 slang_clear_sug(slang);
10952 goto nextone;
10953 }
10954
10955 /*
10956 * <SUGTABLE>: <sugwcount> <sugline> ...
10957 *
10958 * Read the table with word numbers. We use a file buffer for
10959 * this, because it's so much like a file with lines. Makes it
10960 * possible to swap the info and save on memory use.
10961 */
10962 slang->sl_sugbuf = open_spellbuf();
10963 if (slang->sl_sugbuf == NULL)
10964 goto someerror;
10965 /* <sugwcount> */
Bram Moolenaarb388adb2006-02-28 23:50:17 +000010966 wcount = get4c(fd);
Bram Moolenaar4770d092006-01-12 23:22:24 +000010967 if (wcount < 0)
10968 goto someerror;
10969
10970 /* Read all the wordnr lists into the buffer, one NUL terminated
10971 * list per line. */
10972 ga_init2(&ga, 1, 100);
10973 for (wordnr = 0; wordnr < wcount; ++wordnr)
10974 {
10975 ga.ga_len = 0;
10976 for (;;)
10977 {
10978 c = getc(fd); /* <sugline> */
10979 if (c < 0 || ga_grow(&ga, 1) == FAIL)
10980 goto someerror;
10981 ((char_u *)ga.ga_data)[ga.ga_len++] = c;
10982 if (c == NUL)
10983 break;
10984 }
10985 if (ml_append_buf(slang->sl_sugbuf, (linenr_T)wordnr,
10986 ga.ga_data, ga.ga_len, TRUE) == FAIL)
10987 goto someerror;
10988 }
10989 ga_clear(&ga);
10990
10991 /*
10992 * Need to put word counts in the word tries, so that we can find
10993 * a word by its number.
10994 */
10995 tree_count_words(slang->sl_fbyts, slang->sl_fidxs);
10996 tree_count_words(slang->sl_sbyts, slang->sl_sidxs);
10997
10998nextone:
10999 if (fd != NULL)
11000 fclose(fd);
11001 STRCPY(dotp, ".spl");
11002 }
11003 }
11004}
11005
11006
11007/*
11008 * Fill in the wordcount fields for a trie.
11009 * Returns the total number of words.
11010 */
11011 static void
11012tree_count_words(byts, idxs)
11013 char_u *byts;
11014 idx_T *idxs;
11015{
11016 int depth;
11017 idx_T arridx[MAXWLEN];
11018 int curi[MAXWLEN];
11019 int c;
11020 idx_T n;
11021 int wordcount[MAXWLEN];
11022
11023 arridx[0] = 0;
11024 curi[0] = 1;
11025 wordcount[0] = 0;
11026 depth = 0;
11027 while (depth >= 0 && !got_int)
11028 {
11029 if (curi[depth] > byts[arridx[depth]])
11030 {
11031 /* Done all bytes at this node, go up one level. */
11032 idxs[arridx[depth]] = wordcount[depth];
11033 if (depth > 0)
11034 wordcount[depth - 1] += wordcount[depth];
11035
11036 --depth;
11037 fast_breakcheck();
11038 }
11039 else
11040 {
11041 /* Do one more byte at this node. */
11042 n = arridx[depth] + curi[depth];
11043 ++curi[depth];
11044
11045 c = byts[n];
11046 if (c == 0)
11047 {
11048 /* End of word, count it. */
11049 ++wordcount[depth];
11050
11051 /* Skip over any other NUL bytes (same word with different
11052 * flags). */
11053 while (byts[n + 1] == 0)
11054 {
11055 ++n;
11056 ++curi[depth];
11057 }
11058 }
11059 else
11060 {
11061 /* Normal char, go one level deeper to count the words. */
11062 ++depth;
11063 arridx[depth] = idxs[n];
11064 curi[depth] = 1;
11065 wordcount[depth] = 0;
11066 }
11067 }
11068 }
11069}
11070
11071/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000011072 * Free the info put in "*su" by spell_find_suggest().
11073 */
11074 static void
11075spell_find_cleanup(su)
11076 suginfo_T *su;
11077{
11078 int i;
11079
11080 /* Free the suggestions. */
11081 for (i = 0; i < su->su_ga.ga_len; ++i)
11082 vim_free(SUG(su->su_ga, i).st_word);
11083 ga_clear(&su->su_ga);
11084 for (i = 0; i < su->su_sga.ga_len; ++i)
11085 vim_free(SUG(su->su_sga, i).st_word);
11086 ga_clear(&su->su_sga);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011087
11088 /* Free the banned words. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011089 hash_clear_all(&su->su_banned, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011090}
11091
11092/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011093 * Make a copy of "word", with the first letter upper or lower cased, to
11094 * "wcopy[MAXWLEN]". "word" must not be empty.
11095 * The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011096 */
11097 static void
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011098onecap_copy(word, wcopy, upper)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011099 char_u *word;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011100 char_u *wcopy;
11101 int upper; /* TRUE: first letter made upper case */
11102{
11103 char_u *p;
11104 int c;
11105 int l;
11106
11107 p = word;
11108#ifdef FEAT_MBYTE
11109 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011110 c = mb_cptr2char_adv(&p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011111 else
11112#endif
11113 c = *p++;
11114 if (upper)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011115 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011116 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011117 c = SPELL_TOFOLD(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011118#ifdef FEAT_MBYTE
11119 if (has_mbyte)
11120 l = mb_char2bytes(c, wcopy);
11121 else
11122#endif
11123 {
11124 l = 1;
11125 wcopy[0] = c;
11126 }
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011127 vim_strncpy(wcopy + l, p, MAXWLEN - l - 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011128}
11129
11130/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011131 * Make a copy of "word" with all the letters upper cased into
11132 * "wcopy[MAXWLEN]". The result is NUL terminated.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011133 */
11134 static void
11135allcap_copy(word, wcopy)
11136 char_u *word;
11137 char_u *wcopy;
11138{
11139 char_u *s;
11140 char_u *d;
11141 int c;
11142
11143 d = wcopy;
11144 for (s = word; *s != NUL; )
11145 {
11146#ifdef FEAT_MBYTE
11147 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011148 c = mb_cptr2char_adv(&s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011149 else
11150#endif
11151 c = *s++;
Bram Moolenaar78622822005-08-23 21:00:13 +000011152
11153#ifdef FEAT_MBYTE
11154 /* We only change ß to SS when we are certain latin1 is used. It
11155 * would cause weird errors in other 8-bit encodings. */
11156 if (enc_latin1like && c == 0xdf)
11157 {
11158 c = 'S';
11159 if (d - wcopy >= MAXWLEN - 1)
11160 break;
11161 *d++ = c;
11162 }
11163 else
11164#endif
11165 c = SPELL_TOUPPER(c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011166
11167#ifdef FEAT_MBYTE
11168 if (has_mbyte)
11169 {
11170 if (d - wcopy >= MAXWLEN - MB_MAXBYTES)
11171 break;
11172 d += mb_char2bytes(c, d);
11173 }
11174 else
11175#endif
11176 {
11177 if (d - wcopy >= MAXWLEN - 1)
11178 break;
11179 *d++ = c;
11180 }
11181 }
11182 *d = NUL;
11183}
11184
11185/*
Bram Moolenaar0c405862005-06-22 22:26:26 +000011186 * Try finding suggestions by recognizing specific situations.
11187 */
11188 static void
11189suggest_try_special(su)
11190 suginfo_T *su;
11191{
11192 char_u *p;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011193 size_t len;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011194 int c;
11195 char_u word[MAXWLEN];
11196
11197 /*
11198 * Recognize a word that is repeated: "the the".
11199 */
11200 p = skiptowhite(su->su_fbadword);
11201 len = p - su->su_fbadword;
11202 p = skipwhite(p);
11203 if (STRLEN(p) == len && STRNCMP(su->su_fbadword, p, len) == 0)
11204 {
11205 /* Include badflags: if the badword is onecap or allcap
11206 * use that for the goodword too: "The the" -> "The". */
11207 c = su->su_fbadword[len];
11208 su->su_fbadword[len] = NUL;
11209 make_case_word(su->su_fbadword, word, su->su_badflags);
11210 su->su_fbadword[len] = c;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011211
11212 /* Give a soundalike score of 0, compute the score as if deleting one
11213 * character. */
11214 add_suggestion(su, &su->su_ga, word, su->su_badlen,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011215 RESCORE(SCORE_REP, 0), 0, TRUE, su->su_sallang, FALSE);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011216 }
11217}
11218
11219/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011220 * Try finding suggestions by adding/removing/swapping letters.
11221 */
11222 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000011223suggest_try_change(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011224 suginfo_T *su;
11225{
11226 char_u fword[MAXWLEN]; /* copy of the bad word, case-folded */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011227 int n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011228 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011229 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011230 langp_T *lp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011231
11232 /* We make a copy of the case-folded bad word, so that we can modify it
Bram Moolenaar0c405862005-06-22 22:26:26 +000011233 * to find matches (esp. REP items). Append some more text, changing
11234 * chars after the bad word may help. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011235 STRCPY(fword, su->su_fbadword);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011236 n = (int)STRLEN(fword);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011237 p = su->su_badptr + su->su_badlen;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011238 (void)spell_casefold(p, (int)STRLEN(p), fword + n, MAXWLEN - n);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011239
Bram Moolenaar860cae12010-06-05 23:22:07 +020011240 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011241 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020011242 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011243
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011244 /* If reloading a spell file fails it's still in the list but
11245 * everything has been cleared. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011246 if (lp->lp_slang->sl_fbyts == NULL)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011247 continue;
11248
Bram Moolenaar4770d092006-01-12 23:22:24 +000011249 /* Try it for this language. Will add possible suggestions. */
11250 suggest_trie_walk(su, lp, fword, FALSE);
11251 }
11252}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011253
Bram Moolenaar4770d092006-01-12 23:22:24 +000011254/* Check the maximum score, if we go over it we won't try this change. */
11255#define TRY_DEEPER(su, stack, depth, add) \
11256 (stack[depth].ts_score + (add) < su->su_maxscore)
11257
11258/*
11259 * Try finding suggestions by adding/removing/swapping letters.
11260 *
11261 * This uses a state machine. At each node in the tree we try various
11262 * operations. When trying if an operation works "depth" is increased and the
11263 * stack[] is used to store info. This allows combinations, thus insert one
11264 * character, replace one and delete another. The number of changes is
11265 * limited by su->su_maxscore.
11266 *
11267 * After implementing this I noticed an article by Kemal Oflazer that
11268 * describes something similar: "Error-tolerant Finite State Recognition with
11269 * Applications to Morphological Analysis and Spelling Correction" (1996).
11270 * The implementation in the article is simplified and requires a stack of
11271 * unknown depth. The implementation here only needs a stack depth equal to
11272 * the length of the word.
11273 *
11274 * This is also used for the sound-folded word, "soundfold" is TRUE then.
11275 * The mechanism is the same, but we find a match with a sound-folded word
11276 * that comes from one or more original words. Each of these words may be
11277 * added, this is done by add_sound_suggest().
11278 * Don't use:
11279 * the prefix tree or the keep-case tree
11280 * "su->su_badlen"
11281 * anything to do with upper and lower case
11282 * anything to do with word or non-word characters ("spell_iswordp()")
11283 * banned words
11284 * word flags (rare, region, compounding)
11285 * word splitting for now
11286 * "similar_chars()"
11287 * use "slang->sl_repsal" instead of "lp->lp_replang->sl_rep"
11288 */
11289 static void
11290suggest_trie_walk(su, lp, fword, soundfold)
11291 suginfo_T *su;
11292 langp_T *lp;
11293 char_u *fword;
11294 int soundfold;
11295{
11296 char_u tword[MAXWLEN]; /* good word collected so far */
11297 trystate_T stack[MAXWLEN];
11298 char_u preword[MAXWLEN * 3]; /* word found with proper case;
Bram Moolenaar3ea38ef2010-01-19 13:08:42 +010011299 * concatenation of prefix compound
Bram Moolenaar4770d092006-01-12 23:22:24 +000011300 * words and split word. NUL terminated
11301 * when going deeper but not when coming
11302 * back. */
11303 char_u compflags[MAXWLEN]; /* compound flags, one for each word */
11304 trystate_T *sp;
11305 int newscore;
11306 int score;
11307 char_u *byts, *fbyts, *pbyts;
11308 idx_T *idxs, *fidxs, *pidxs;
11309 int depth;
11310 int c, c2, c3;
11311 int n = 0;
11312 int flags;
11313 garray_T *gap;
11314 idx_T arridx;
11315 int len;
11316 char_u *p;
11317 fromto_T *ftp;
11318 int fl = 0, tl;
11319 int repextra = 0; /* extra bytes in fword[] from REP item */
11320 slang_T *slang = lp->lp_slang;
11321 int fword_ends;
11322 int goodword_ends;
11323#ifdef DEBUG_TRIEWALK
11324 /* Stores the name of the change made at each level. */
11325 char_u changename[MAXWLEN][80];
11326#endif
11327 int breakcheckcount = 1000;
11328 int compound_ok;
11329
11330 /*
11331 * Go through the whole case-fold tree, try changes at each node.
11332 * "tword[]" contains the word collected from nodes in the tree.
11333 * "fword[]" the word we are trying to match with (initially the bad
11334 * word).
11335 */
11336 depth = 0;
11337 sp = &stack[0];
11338 vim_memset(sp, 0, sizeof(trystate_T));
11339 sp->ts_curi = 1;
11340
11341 if (soundfold)
11342 {
11343 /* Going through the soundfold tree. */
11344 byts = fbyts = slang->sl_sbyts;
11345 idxs = fidxs = slang->sl_sidxs;
11346 pbyts = NULL;
11347 pidxs = NULL;
11348 sp->ts_prefixdepth = PFD_NOPREFIX;
11349 sp->ts_state = STATE_START;
11350 }
11351 else
11352 {
Bram Moolenaarea424162005-06-16 21:51:00 +000011353 /*
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011354 * When there are postponed prefixes we need to use these first. At
11355 * the end of the prefix we continue in the case-fold tree.
11356 */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011357 fbyts = slang->sl_fbyts;
11358 fidxs = slang->sl_fidxs;
11359 pbyts = slang->sl_pbyts;
11360 pidxs = slang->sl_pidxs;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011361 if (pbyts != NULL)
11362 {
11363 byts = pbyts;
11364 idxs = pidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011365 sp->ts_prefixdepth = PFD_PREFIXTREE;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011366 sp->ts_state = STATE_NOPREFIX; /* try without prefix first */
11367 }
11368 else
11369 {
11370 byts = fbyts;
11371 idxs = fidxs;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011372 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011373 sp->ts_state = STATE_START;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011374 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011375 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011376
Bram Moolenaar4770d092006-01-12 23:22:24 +000011377 /*
11378 * Loop to find all suggestions. At each round we either:
11379 * - For the current state try one operation, advance "ts_curi",
11380 * increase "depth".
11381 * - When a state is done go to the next, set "ts_state".
11382 * - When all states are tried decrease "depth".
11383 */
11384 while (depth >= 0 && !got_int)
11385 {
11386 sp = &stack[depth];
11387 switch (sp->ts_state)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011388 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011389 case STATE_START:
11390 case STATE_NOPREFIX:
11391 /*
11392 * Start of node: Deal with NUL bytes, which means
11393 * tword[] may end here.
11394 */
11395 arridx = sp->ts_arridx; /* current node in the tree */
11396 len = byts[arridx]; /* bytes in this node */
11397 arridx += sp->ts_curi; /* index of current byte */
11398
11399 if (sp->ts_prefixdepth == PFD_PREFIXTREE)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011400 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011401 /* Skip over the NUL bytes, we use them later. */
11402 for (n = 0; n < len && byts[arridx + n] == 0; ++n)
11403 ;
11404 sp->ts_curi += n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011405
Bram Moolenaar4770d092006-01-12 23:22:24 +000011406 /* Always past NUL bytes now. */
11407 n = (int)sp->ts_state;
11408 sp->ts_state = STATE_ENDNUL;
11409 sp->ts_save_badflags = su->su_badflags;
11410
11411 /* At end of a prefix or at start of prefixtree: check for
11412 * following word. */
11413 if (byts[arridx] == 0 || n == (int)STATE_NOPREFIX)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011414 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011415 /* Set su->su_badflags to the caps type at this position.
11416 * Use the caps type until here for the prefix itself. */
Bram Moolenaar53805d12005-08-01 07:08:33 +000011417#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011418 if (has_mbyte)
11419 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
11420 else
Bram Moolenaar53805d12005-08-01 07:08:33 +000011421#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011422 n = sp->ts_fidx;
11423 flags = badword_captype(su->su_badptr, su->su_badptr + n);
11424 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011425 su->su_badptr + su->su_badlen);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011426#ifdef DEBUG_TRIEWALK
11427 sprintf(changename[depth], "prefix");
11428#endif
11429 go_deeper(stack, depth, 0);
11430 ++depth;
11431 sp = &stack[depth];
11432 sp->ts_prefixdepth = depth - 1;
11433 byts = fbyts;
11434 idxs = fidxs;
11435 sp->ts_arridx = 0;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011436
Bram Moolenaar4770d092006-01-12 23:22:24 +000011437 /* Move the prefix to preword[] with the right case
11438 * and make find_keepcap_word() works. */
11439 tword[sp->ts_twordlen] = NUL;
11440 make_case_word(tword + sp->ts_splitoff,
11441 preword + sp->ts_prewordlen, flags);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011442 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011443 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011444 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011445 break;
11446 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011447
Bram Moolenaar4770d092006-01-12 23:22:24 +000011448 if (sp->ts_curi > len || byts[arridx] != 0)
11449 {
11450 /* Past bytes in node and/or past NUL bytes. */
11451 sp->ts_state = STATE_ENDNUL;
11452 sp->ts_save_badflags = su->su_badflags;
11453 break;
11454 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011455
Bram Moolenaar4770d092006-01-12 23:22:24 +000011456 /*
11457 * End of word in tree.
11458 */
11459 ++sp->ts_curi; /* eat one NUL byte */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011460
Bram Moolenaar4770d092006-01-12 23:22:24 +000011461 flags = (int)idxs[arridx];
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011462
11463 /* Skip words with the NOSUGGEST flag. */
11464 if (flags & WF_NOSUGGEST)
11465 break;
11466
Bram Moolenaar4770d092006-01-12 23:22:24 +000011467 fword_ends = (fword[sp->ts_fidx] == NUL
11468 || (soundfold
11469 ? vim_iswhite(fword[sp->ts_fidx])
Bram Moolenaar860cae12010-06-05 23:22:07 +020011470 : !spell_iswordp(fword + sp->ts_fidx, curwin)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000011471 tword[sp->ts_twordlen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011472
Bram Moolenaar4770d092006-01-12 23:22:24 +000011473 if (sp->ts_prefixdepth <= PFD_NOTSPECIAL
Bram Moolenaard12a1322005-08-21 22:08:24 +000011474 && (sp->ts_flags & TSF_PREFIXOK) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011475 {
11476 /* There was a prefix before the word. Check that the prefix
11477 * can be used with this word. */
11478 /* Count the length of the NULs in the prefix. If there are
11479 * none this must be the first try without a prefix. */
11480 n = stack[sp->ts_prefixdepth].ts_arridx;
11481 len = pbyts[n++];
11482 for (c = 0; c < len && pbyts[n + c] == 0; ++c)
11483 ;
11484 if (c > 0)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011485 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011486 c = valid_word_prefix(c, n, flags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011487 tword + sp->ts_splitoff, slang, FALSE);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011488 if (c == 0)
11489 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011490
Bram Moolenaar4770d092006-01-12 23:22:24 +000011491 /* Use the WF_RARE flag for a rare prefix. */
11492 if (c & WF_RAREPFX)
11493 flags |= WF_RARE;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011494
Bram Moolenaar4770d092006-01-12 23:22:24 +000011495 /* Tricky: when checking for both prefix and compounding
11496 * we run into the prefix flag first.
11497 * Remember that it's OK, so that we accept the prefix
11498 * when arriving at a compound flag. */
11499 sp->ts_flags |= TSF_PREFIXOK;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011500 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011501 }
Bram Moolenaar42eeac32005-06-29 22:40:58 +000011502
Bram Moolenaar4770d092006-01-12 23:22:24 +000011503 /* Check NEEDCOMPOUND: can't use word without compounding. Do try
11504 * appending another compound word below. */
11505 if (sp->ts_complen == sp->ts_compsplit && fword_ends
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011506 && (flags & WF_NEEDCOMP))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011507 goodword_ends = FALSE;
11508 else
11509 goodword_ends = TRUE;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011510
Bram Moolenaar4770d092006-01-12 23:22:24 +000011511 p = NULL;
11512 compound_ok = TRUE;
11513 if (sp->ts_complen > sp->ts_compsplit)
11514 {
11515 if (slang->sl_nobreak)
Bram Moolenaard12a1322005-08-21 22:08:24 +000011516 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011517 /* There was a word before this word. When there was no
11518 * change in this word (it was correct) add the first word
11519 * as a suggestion. If this word was corrected too, we
11520 * need to check if a correct word follows. */
11521 if (sp->ts_fidx - sp->ts_splitfidx
Bram Moolenaar78622822005-08-23 21:00:13 +000011522 == sp->ts_twordlen - sp->ts_splitoff
Bram Moolenaar4770d092006-01-12 23:22:24 +000011523 && STRNCMP(fword + sp->ts_splitfidx,
11524 tword + sp->ts_splitoff,
Bram Moolenaar78622822005-08-23 21:00:13 +000011525 sp->ts_fidx - sp->ts_splitfidx) == 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011526 {
11527 preword[sp->ts_prewordlen] = NUL;
11528 newscore = score_wordcount_adj(slang, sp->ts_score,
11529 preword + sp->ts_prewordlen,
11530 sp->ts_prewordlen > 0);
11531 /* Add the suggestion if the score isn't too bad. */
11532 if (newscore <= su->su_maxscore)
Bram Moolenaar78622822005-08-23 21:00:13 +000011533 add_suggestion(su, &su->su_ga, preword,
Bram Moolenaar8b96d642005-09-05 22:05:30 +000011534 sp->ts_splitfidx - repextra,
Bram Moolenaar4770d092006-01-12 23:22:24 +000011535 newscore, 0, FALSE,
11536 lp->lp_sallang, FALSE);
11537 break;
Bram Moolenaar78622822005-08-23 21:00:13 +000011538 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011539 }
Bram Moolenaare52325c2005-08-22 22:54:29 +000011540 else
Bram Moolenaar0c405862005-06-22 22:26:26 +000011541 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011542 /* There was a compound word before this word. If this
11543 * word does not support compounding then give up
11544 * (splitting is tried for the word without compound
11545 * flag). */
11546 if (((unsigned)flags >> 24) == 0
11547 || sp->ts_twordlen - sp->ts_splitoff
11548 < slang->sl_compminlen)
11549 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011550#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011551 /* For multi-byte chars check character length against
11552 * COMPOUNDMIN. */
11553 if (has_mbyte
11554 && slang->sl_compminlen > 0
11555 && mb_charlen(tword + sp->ts_splitoff)
11556 < slang->sl_compminlen)
11557 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000011558#endif
Bram Moolenaare52325c2005-08-22 22:54:29 +000011559
Bram Moolenaar4770d092006-01-12 23:22:24 +000011560 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11561 compflags[sp->ts_complen + 1] = NUL;
11562 vim_strncpy(preword + sp->ts_prewordlen,
11563 tword + sp->ts_splitoff,
11564 sp->ts_twordlen - sp->ts_splitoff);
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011565
11566 /* Verify CHECKCOMPOUNDPATTERN rules. */
11567 if (match_checkcompoundpattern(preword, sp->ts_prewordlen,
11568 &slang->sl_comppat))
Bram Moolenaar4770d092006-01-12 23:22:24 +000011569 compound_ok = FALSE;
11570
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011571 if (compound_ok)
11572 {
11573 p = preword;
11574 while (*skiptowhite(p) != NUL)
11575 p = skipwhite(skiptowhite(p));
11576 if (fword_ends && !can_compound(slang, p,
11577 compflags + sp->ts_compsplit))
11578 /* Compound is not allowed. But it may still be
11579 * possible if we add another (short) word. */
11580 compound_ok = FALSE;
11581 }
11582
Bram Moolenaar4770d092006-01-12 23:22:24 +000011583 /* Get pointer to last char of previous word. */
11584 p = preword + sp->ts_prewordlen;
11585 mb_ptr_back(preword, p);
Bram Moolenaar0c405862005-06-22 22:26:26 +000011586 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011587 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011588
Bram Moolenaar4770d092006-01-12 23:22:24 +000011589 /*
11590 * Form the word with proper case in preword.
11591 * If there is a word from a previous split, append.
11592 * For the soundfold tree don't change the case, simply append.
11593 */
11594 if (soundfold)
11595 STRCPY(preword + sp->ts_prewordlen, tword + sp->ts_splitoff);
11596 else if (flags & WF_KEEPCAP)
11597 /* Must find the word in the keep-case tree. */
11598 find_keepcap_word(slang, tword + sp->ts_splitoff,
11599 preword + sp->ts_prewordlen);
11600 else
11601 {
11602 /* Include badflags: If the badword is onecap or allcap
11603 * use that for the goodword too. But if the badword is
11604 * allcap and it's only one char long use onecap. */
11605 c = su->su_badflags;
11606 if ((c & WF_ALLCAP)
11607#ifdef FEAT_MBYTE
11608 && su->su_badlen == (*mb_ptr2len)(su->su_badptr)
11609#else
11610 && su->su_badlen == 1
11611#endif
11612 )
11613 c = WF_ONECAP;
11614 c |= flags;
11615
11616 /* When appending a compound word after a word character don't
11617 * use Onecap. */
11618 if (p != NULL && spell_iswordp_nmw(p))
11619 c &= ~WF_ONECAP;
11620 make_case_word(tword + sp->ts_splitoff,
11621 preword + sp->ts_prewordlen, c);
11622 }
11623
11624 if (!soundfold)
11625 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011626 /* Don't use a banned word. It may appear again as a good
11627 * word, thus remember it. */
11628 if (flags & WF_BANNED)
11629 {
Bram Moolenaar5195e452005-08-19 20:32:47 +000011630 add_banned(su, preword + sp->ts_prewordlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011631 break;
11632 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011633 if ((sp->ts_complen == sp->ts_compsplit
Bram Moolenaar4770d092006-01-12 23:22:24 +000011634 && WAS_BANNED(su, preword + sp->ts_prewordlen))
11635 || WAS_BANNED(su, preword))
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011636 {
11637 if (slang->sl_compprog == NULL)
11638 break;
11639 /* the word so far was banned but we may try compounding */
11640 goodword_ends = FALSE;
11641 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011642 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011643
Bram Moolenaar4770d092006-01-12 23:22:24 +000011644 newscore = 0;
11645 if (!soundfold) /* soundfold words don't have flags */
11646 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011647 if ((flags & WF_REGION)
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000011648 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011649 newscore += SCORE_REGION;
11650 if (flags & WF_RARE)
11651 newscore += SCORE_RARE;
11652
Bram Moolenaar0c405862005-06-22 22:26:26 +000011653 if (!spell_valid_case(su->su_badflags,
Bram Moolenaar5195e452005-08-19 20:32:47 +000011654 captype(preword + sp->ts_prewordlen, NULL)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011655 newscore += SCORE_ICASE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011656 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011657
Bram Moolenaar4770d092006-01-12 23:22:24 +000011658 /* TODO: how about splitting in the soundfold tree? */
11659 if (fword_ends
11660 && goodword_ends
11661 && sp->ts_fidx >= sp->ts_fidxtry
11662 && compound_ok)
11663 {
11664 /* The badword also ends: add suggestions. */
11665#ifdef DEBUG_TRIEWALK
11666 if (soundfold && STRCMP(preword, "smwrd") == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011667 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011668 int j;
11669
11670 /* print the stack of changes that brought us here */
11671 smsg("------ %s -------", fword);
11672 for (j = 0; j < depth; ++j)
11673 smsg("%s", changename[j]);
11674 }
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011675#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011676 if (soundfold)
11677 {
11678 /* For soundfolded words we need to find the original
Bram Moolenaarf711faf2007-05-10 16:48:19 +000011679 * words, the edit distance and then add them. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011680 add_sound_suggest(su, preword, sp->ts_score, lp);
11681 }
Bram Moolenaar7e88c3d2010-08-01 15:47:35 +020011682 else if (sp->ts_fidx > 0)
Bram Moolenaar4770d092006-01-12 23:22:24 +000011683 {
11684 /* Give a penalty when changing non-word char to word
11685 * char, e.g., "thes," -> "these". */
11686 p = fword + sp->ts_fidx;
11687 mb_ptr_back(fword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020011688 if (!spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011689 {
11690 p = preword + STRLEN(preword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000011691 mb_ptr_back(preword, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020011692 if (spell_iswordp(p, curwin))
Bram Moolenaarcf6bf392005-06-27 22:27:46 +000011693 newscore += SCORE_NONWORD;
11694 }
11695
Bram Moolenaar4770d092006-01-12 23:22:24 +000011696 /* Give a bonus to words seen before. */
11697 score = score_wordcount_adj(slang,
11698 sp->ts_score + newscore,
11699 preword + sp->ts_prewordlen,
11700 sp->ts_prewordlen > 0);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011701
Bram Moolenaar4770d092006-01-12 23:22:24 +000011702 /* Add the suggestion if the score isn't too bad. */
11703 if (score <= su->su_maxscore)
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011704 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011705 add_suggestion(su, &su->su_ga, preword,
11706 sp->ts_fidx - repextra,
11707 score, 0, FALSE, lp->lp_sallang, FALSE);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000011708
11709 if (su->su_badflags & WF_MIXCAP)
11710 {
11711 /* We really don't know if the word should be
11712 * upper or lower case, add both. */
11713 c = captype(preword, NULL);
11714 if (c == 0 || c == WF_ALLCAP)
11715 {
11716 make_case_word(tword + sp->ts_splitoff,
11717 preword + sp->ts_prewordlen,
11718 c == 0 ? WF_ALLCAP : 0);
11719
11720 add_suggestion(su, &su->su_ga, preword,
11721 sp->ts_fidx - repextra,
11722 score + SCORE_ICASE, 0, FALSE,
11723 lp->lp_sallang, FALSE);
11724 }
11725 }
11726 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011727 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011728 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011729
Bram Moolenaar4770d092006-01-12 23:22:24 +000011730 /*
11731 * Try word split and/or compounding.
11732 */
11733 if ((sp->ts_fidx >= sp->ts_fidxtry || fword_ends)
Bram Moolenaarea424162005-06-16 21:51:00 +000011734#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011735 /* Don't split halfway a character. */
11736 && (!has_mbyte || sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000011737#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011738 )
11739 {
11740 int try_compound;
11741 int try_split;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011742
Bram Moolenaar4770d092006-01-12 23:22:24 +000011743 /* If past the end of the bad word don't try a split.
11744 * Otherwise try changing the next word. E.g., find
11745 * suggestions for "the the" where the second "the" is
11746 * different. It's done like a split.
11747 * TODO: word split for soundfold words */
11748 try_split = (sp->ts_fidx - repextra < su->su_badlen)
11749 && !soundfold;
11750
11751 /* Get here in several situations:
11752 * 1. The word in the tree ends:
11753 * If the word allows compounding try that. Otherwise try
11754 * a split by inserting a space. For both check that a
11755 * valid words starts at fword[sp->ts_fidx].
11756 * For NOBREAK do like compounding to be able to check if
11757 * the next word is valid.
11758 * 2. The badword does end, but it was due to a change (e.g.,
11759 * a swap). No need to split, but do check that the
11760 * following word is valid.
11761 * 3. The badword and the word in the tree end. It may still
11762 * be possible to compound another (short) word.
11763 */
11764 try_compound = FALSE;
11765 if (!soundfold
11766 && slang->sl_compprog != NULL
11767 && ((unsigned)flags >> 24) != 0
11768 && sp->ts_twordlen - sp->ts_splitoff
11769 >= slang->sl_compminlen
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011770#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011771 && (!has_mbyte
11772 || slang->sl_compminlen == 0
11773 || mb_charlen(tword + sp->ts_splitoff)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011774 >= slang->sl_compminlen)
11775#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011776 && (slang->sl_compsylmax < MAXWLEN
11777 || sp->ts_complen + 1 - sp->ts_compsplit
11778 < slang->sl_compmax)
Bram Moolenaar9f94b052008-11-30 20:12:46 +000011779 && (can_be_compound(sp, slang,
11780 compflags, ((unsigned)flags >> 24))))
11781
Bram Moolenaar4770d092006-01-12 23:22:24 +000011782 {
11783 try_compound = TRUE;
11784 compflags[sp->ts_complen] = ((unsigned)flags >> 24);
11785 compflags[sp->ts_complen + 1] = NUL;
11786 }
Bram Moolenaard12a1322005-08-21 22:08:24 +000011787
Bram Moolenaar4770d092006-01-12 23:22:24 +000011788 /* For NOBREAK we never try splitting, it won't make any word
11789 * valid. */
11790 if (slang->sl_nobreak)
11791 try_compound = TRUE;
Bram Moolenaar78622822005-08-23 21:00:13 +000011792
Bram Moolenaar4770d092006-01-12 23:22:24 +000011793 /* If we could add a compound word, and it's also possible to
11794 * split at this point, do the split first and set
11795 * TSF_DIDSPLIT to avoid doing it again. */
11796 else if (!fword_ends
11797 && try_compound
11798 && (sp->ts_flags & TSF_DIDSPLIT) == 0)
11799 {
11800 try_compound = FALSE;
11801 sp->ts_flags |= TSF_DIDSPLIT;
11802 --sp->ts_curi; /* do the same NUL again */
11803 compflags[sp->ts_complen] = NUL;
11804 }
11805 else
11806 sp->ts_flags &= ~TSF_DIDSPLIT;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011807
Bram Moolenaar4770d092006-01-12 23:22:24 +000011808 if (try_split || try_compound)
11809 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011810 if (!try_compound && (!fword_ends || !goodword_ends))
Bram Moolenaard12a1322005-08-21 22:08:24 +000011811 {
11812 /* If we're going to split need to check that the
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011813 * words so far are valid for compounding. If there
11814 * is only one word it must not have the NEEDCOMPOUND
11815 * flag. */
11816 if (sp->ts_complen == sp->ts_compsplit
11817 && (flags & WF_NEEDCOMP))
11818 break;
Bram Moolenaare52325c2005-08-22 22:54:29 +000011819 p = preword;
11820 while (*skiptowhite(p) != NUL)
11821 p = skipwhite(skiptowhite(p));
Bram Moolenaard12a1322005-08-21 22:08:24 +000011822 if (sp->ts_complen > sp->ts_compsplit
Bram Moolenaare52325c2005-08-22 22:54:29 +000011823 && !can_compound(slang, p,
Bram Moolenaard12a1322005-08-21 22:08:24 +000011824 compflags + sp->ts_compsplit))
11825 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011826
11827 if (slang->sl_nosplitsugs)
11828 newscore += SCORE_SPLIT_NO;
11829 else
11830 newscore += SCORE_SPLIT;
Bram Moolenaar4770d092006-01-12 23:22:24 +000011831
11832 /* Give a bonus to words seen before. */
11833 newscore = score_wordcount_adj(slang, newscore,
11834 preword + sp->ts_prewordlen, TRUE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011835 }
11836
Bram Moolenaar4770d092006-01-12 23:22:24 +000011837 if (TRY_DEEPER(su, stack, depth, newscore))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011838 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000011839 go_deeper(stack, depth, newscore);
11840#ifdef DEBUG_TRIEWALK
11841 if (!try_compound && !fword_ends)
11842 sprintf(changename[depth], "%.*s-%s: split",
11843 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11844 else
11845 sprintf(changename[depth], "%.*s-%s: compound",
11846 sp->ts_twordlen, tword, fword + sp->ts_fidx);
11847#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011848 /* Save things to be restored at STATE_SPLITUNDO. */
Bram Moolenaar0c405862005-06-22 22:26:26 +000011849 sp->ts_save_badflags = su->su_badflags;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011850 sp->ts_state = STATE_SPLITUNDO;
11851
11852 ++depth;
11853 sp = &stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011854
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011855 /* Append a space to preword when splitting. */
11856 if (!try_compound && !fword_ends)
11857 STRCAT(preword, " ");
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011858 sp->ts_prewordlen = (char_u)STRLEN(preword);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011859 sp->ts_splitoff = sp->ts_twordlen;
Bram Moolenaar78622822005-08-23 21:00:13 +000011860 sp->ts_splitfidx = sp->ts_fidx;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011861
11862 /* If the badword has a non-word character at this
11863 * position skip it. That means replacing the
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011864 * non-word character with a space. Always skip a
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011865 * character when the word ends. But only when the
11866 * good word can end. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000011867 if (((!try_compound && !spell_iswordp_nmw(fword
11868 + sp->ts_fidx))
11869 || fword_ends)
11870 && fword[sp->ts_fidx] != NUL
11871 && goodword_ends)
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011872 {
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011873 int l;
11874
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011875#ifdef FEAT_MBYTE
11876 if (has_mbyte)
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011877 l = MB_BYTE2LEN(fword[sp->ts_fidx]);
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011878 else
11879#endif
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011880 l = 1;
11881 if (fword_ends)
11882 {
11883 /* Copy the skipped character to preword. */
Bram Moolenaar5195e452005-08-19 20:32:47 +000011884 mch_memmove(preword + sp->ts_prewordlen,
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011885 fword + sp->ts_fidx, l);
Bram Moolenaar5195e452005-08-19 20:32:47 +000011886 sp->ts_prewordlen += l;
11887 preword[sp->ts_prewordlen] = NUL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011888 }
11889 else
11890 sp->ts_score -= SCORE_SPLIT - SCORE_SUBST;
11891 sp->ts_fidx += l;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011892 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000011893
Bram Moolenaard12a1322005-08-21 22:08:24 +000011894 /* When compounding include compound flag in
11895 * compflags[] (already set above). When splitting we
11896 * may start compounding over again. */
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011897 if (try_compound)
Bram Moolenaar5195e452005-08-19 20:32:47 +000011898 ++sp->ts_complen;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011899 else
Bram Moolenaard12a1322005-08-21 22:08:24 +000011900 sp->ts_compsplit = sp->ts_complen;
11901 sp->ts_prefixdepth = PFD_NOPREFIX;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011902
Bram Moolenaar53805d12005-08-01 07:08:33 +000011903 /* set su->su_badflags to the caps type at this
11904 * position */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011905#ifdef FEAT_MBYTE
11906 if (has_mbyte)
Bram Moolenaar53805d12005-08-01 07:08:33 +000011907 n = nofold_len(fword, sp->ts_fidx, su->su_badptr);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011908 else
11909#endif
Bram Moolenaar53805d12005-08-01 07:08:33 +000011910 n = sp->ts_fidx;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011911 su->su_badflags = badword_captype(su->su_badptr + n,
Bram Moolenaar53805d12005-08-01 07:08:33 +000011912 su->su_badptr + su->su_badlen);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011913
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011914 /* Restart at top of the tree. */
Bram Moolenaar9c96f592005-06-30 21:52:39 +000011915 sp->ts_arridx = 0;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011916
11917 /* If there are postponed prefixes, try these too. */
11918 if (pbyts != NULL)
11919 {
11920 byts = pbyts;
11921 idxs = pidxs;
11922 sp->ts_prefixdepth = PFD_PREFIXTREE;
11923 sp->ts_state = STATE_NOPREFIX;
11924 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011925 }
11926 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000011927 }
11928 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011929
Bram Moolenaar4770d092006-01-12 23:22:24 +000011930 case STATE_SPLITUNDO:
11931 /* Undo the changes done for word split or compound word. */
11932 su->su_badflags = sp->ts_save_badflags;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011933
Bram Moolenaar4770d092006-01-12 23:22:24 +000011934 /* Continue looking for NUL bytes. */
11935 sp->ts_state = STATE_START;
Bram Moolenaard12a1322005-08-21 22:08:24 +000011936
Bram Moolenaar4770d092006-01-12 23:22:24 +000011937 /* In case we went into the prefix tree. */
11938 byts = fbyts;
11939 idxs = fidxs;
11940 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011941
Bram Moolenaar4770d092006-01-12 23:22:24 +000011942 case STATE_ENDNUL:
11943 /* Past the NUL bytes in the node. */
11944 su->su_badflags = sp->ts_save_badflags;
11945 if (fword[sp->ts_fidx] == NUL
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011946#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011947 && sp->ts_tcharlen == 0
Bram Moolenaarda2303d2005-08-30 21:55:26 +000011948#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011949 )
11950 {
11951 /* The badword ends, can't use STATE_PLAIN. */
11952 sp->ts_state = STATE_DEL;
11953 break;
11954 }
11955 sp->ts_state = STATE_PLAIN;
11956 /*FALLTHROUGH*/
11957
11958 case STATE_PLAIN:
11959 /*
11960 * Go over all possible bytes at this node, add each to tword[]
11961 * and use child node. "ts_curi" is the index.
11962 */
11963 arridx = sp->ts_arridx;
11964 if (sp->ts_curi > byts[arridx])
11965 {
11966 /* Done all bytes at this node, do next state. When still at
11967 * already changed bytes skip the other tricks. */
11968 if (sp->ts_fidx >= sp->ts_fidxtry)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011969 sp->ts_state = STATE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011970 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000011971 sp->ts_state = STATE_FINAL;
11972 }
11973 else
11974 {
11975 arridx += sp->ts_curi++;
11976 c = byts[arridx];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011977
Bram Moolenaar4770d092006-01-12 23:22:24 +000011978 /* Normal byte, go one level deeper. If it's not equal to the
11979 * byte in the bad word adjust the score. But don't even try
11980 * when the byte was already changed. And don't try when we
11981 * just deleted this byte, accepting it is always cheaper then
11982 * delete + substitute. */
11983 if (c == fword[sp->ts_fidx]
Bram Moolenaarea424162005-06-16 21:51:00 +000011984#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000011985 || (sp->ts_tcharlen > 0 && sp->ts_isdiff != DIFF_NONE)
Bram Moolenaar9f30f502005-06-14 22:01:04 +000011986#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000011987 )
11988 newscore = 0;
11989 else
11990 newscore = SCORE_SUBST;
11991 if ((newscore == 0
11992 || (sp->ts_fidx >= sp->ts_fidxtry
11993 && ((sp->ts_flags & TSF_DIDDEL) == 0
11994 || c != fword[sp->ts_delidx])))
11995 && TRY_DEEPER(su, stack, depth, newscore))
11996 {
11997 go_deeper(stack, depth, newscore);
11998#ifdef DEBUG_TRIEWALK
11999 if (newscore > 0)
12000 sprintf(changename[depth], "%.*s-%s: subst %c to %c",
12001 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12002 fword[sp->ts_fidx], c);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012003 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012004 sprintf(changename[depth], "%.*s-%s: accept %c",
12005 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12006 fword[sp->ts_fidx]);
12007#endif
12008 ++depth;
12009 sp = &stack[depth];
12010 ++sp->ts_fidx;
12011 tword[sp->ts_twordlen++] = c;
12012 sp->ts_arridx = idxs[arridx];
Bram Moolenaarea424162005-06-16 21:51:00 +000012013#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012014 if (newscore == SCORE_SUBST)
12015 sp->ts_isdiff = DIFF_YES;
12016 if (has_mbyte)
12017 {
12018 /* Multi-byte characters are a bit complicated to
12019 * handle: They differ when any of the bytes differ
12020 * and then their length may also differ. */
12021 if (sp->ts_tcharlen == 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000012022 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012023 /* First byte. */
12024 sp->ts_tcharidx = 0;
12025 sp->ts_tcharlen = MB_BYTE2LEN(c);
12026 sp->ts_fcharstart = sp->ts_fidx - 1;
12027 sp->ts_isdiff = (newscore != 0)
Bram Moolenaarea424162005-06-16 21:51:00 +000012028 ? DIFF_YES : DIFF_NONE;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012029 }
12030 else if (sp->ts_isdiff == DIFF_INSERT)
12031 /* When inserting trail bytes don't advance in the
12032 * bad word. */
12033 --sp->ts_fidx;
12034 if (++sp->ts_tcharidx == sp->ts_tcharlen)
12035 {
12036 /* Last byte of character. */
12037 if (sp->ts_isdiff == DIFF_YES)
Bram Moolenaarea424162005-06-16 21:51:00 +000012038 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012039 /* Correct ts_fidx for the byte length of the
12040 * character (we didn't check that before). */
12041 sp->ts_fidx = sp->ts_fcharstart
12042 + MB_BYTE2LEN(
Bram Moolenaarea424162005-06-16 21:51:00 +000012043 fword[sp->ts_fcharstart]);
12044
Bram Moolenaar4770d092006-01-12 23:22:24 +000012045 /* For changing a composing character adjust
12046 * the score from SCORE_SUBST to
12047 * SCORE_SUBCOMP. */
12048 if (enc_utf8
12049 && utf_iscomposing(
12050 mb_ptr2char(tword
12051 + sp->ts_twordlen
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012052 - sp->ts_tcharlen))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012053 && utf_iscomposing(
12054 mb_ptr2char(fword
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012055 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012056 sp->ts_score -=
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000012057 SCORE_SUBST - SCORE_SUBCOMP;
12058
Bram Moolenaar4770d092006-01-12 23:22:24 +000012059 /* For a similar character adjust score from
12060 * SCORE_SUBST to SCORE_SIMILAR. */
12061 else if (!soundfold
12062 && slang->sl_has_map
12063 && similar_chars(slang,
12064 mb_ptr2char(tword
12065 + sp->ts_twordlen
Bram Moolenaarea424162005-06-16 21:51:00 +000012066 - sp->ts_tcharlen),
Bram Moolenaar4770d092006-01-12 23:22:24 +000012067 mb_ptr2char(fword
Bram Moolenaarea424162005-06-16 21:51:00 +000012068 + sp->ts_fcharstart)))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012069 sp->ts_score -=
Bram Moolenaarea424162005-06-16 21:51:00 +000012070 SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea424162005-06-16 21:51:00 +000012071 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012072 else if (sp->ts_isdiff == DIFF_INSERT
12073 && sp->ts_twordlen > sp->ts_tcharlen)
12074 {
12075 p = tword + sp->ts_twordlen - sp->ts_tcharlen;
12076 c = mb_ptr2char(p);
12077 if (enc_utf8 && utf_iscomposing(c))
12078 {
12079 /* Inserting a composing char doesn't
12080 * count that much. */
12081 sp->ts_score -= SCORE_INS - SCORE_INSCOMP;
12082 }
12083 else
12084 {
12085 /* If the previous character was the same,
12086 * thus doubling a character, give a bonus
12087 * to the score. Also for the soundfold
12088 * tree (might seem illogical but does
12089 * give better scores). */
12090 mb_ptr_back(tword, p);
12091 if (c == mb_ptr2char(p))
12092 sp->ts_score -= SCORE_INS
12093 - SCORE_INSDUP;
12094 }
12095 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012096
Bram Moolenaar4770d092006-01-12 23:22:24 +000012097 /* Starting a new char, reset the length. */
12098 sp->ts_tcharlen = 0;
12099 }
Bram Moolenaarea408852005-06-25 22:49:46 +000012100 }
Bram Moolenaarea424162005-06-16 21:51:00 +000012101 else
12102#endif
Bram Moolenaarea408852005-06-25 22:49:46 +000012103 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012104 /* If we found a similar char adjust the score.
12105 * We do this after calling go_deeper() because
12106 * it's slow. */
12107 if (newscore != 0
12108 && !soundfold
12109 && slang->sl_has_map
12110 && similar_chars(slang,
12111 c, fword[sp->ts_fidx - 1]))
12112 sp->ts_score -= SCORE_SUBST - SCORE_SIMILAR;
Bram Moolenaarea408852005-06-25 22:49:46 +000012113 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012114 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012115 }
12116 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012117
Bram Moolenaar4770d092006-01-12 23:22:24 +000012118 case STATE_DEL:
12119#ifdef FEAT_MBYTE
12120 /* When past the first byte of a multi-byte char don't try
12121 * delete/insert/swap a character. */
12122 if (has_mbyte && sp->ts_tcharlen > 0)
12123 {
12124 sp->ts_state = STATE_FINAL;
12125 break;
12126 }
12127#endif
12128 /*
12129 * Try skipping one character in the bad word (delete it).
12130 */
12131 sp->ts_state = STATE_INS_PREP;
12132 sp->ts_curi = 1;
12133 if (soundfold && sp->ts_fidx == 0 && fword[sp->ts_fidx] == '*')
12134 /* Deleting a vowel at the start of a word counts less, see
12135 * soundalike_score(). */
12136 newscore = 2 * SCORE_DEL / 3;
12137 else
12138 newscore = SCORE_DEL;
12139 if (fword[sp->ts_fidx] != NUL
12140 && TRY_DEEPER(su, stack, depth, newscore))
12141 {
12142 go_deeper(stack, depth, newscore);
12143#ifdef DEBUG_TRIEWALK
12144 sprintf(changename[depth], "%.*s-%s: delete %c",
12145 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12146 fword[sp->ts_fidx]);
12147#endif
12148 ++depth;
12149
12150 /* Remember what character we deleted, so that we can avoid
12151 * inserting it again. */
12152 stack[depth].ts_flags |= TSF_DIDDEL;
12153 stack[depth].ts_delidx = sp->ts_fidx;
12154
12155 /* Advance over the character in fword[]. Give a bonus to the
12156 * score if the same character is following "nn" -> "n". It's
12157 * a bit illogical for soundfold tree but it does give better
12158 * results. */
12159#ifdef FEAT_MBYTE
12160 if (has_mbyte)
12161 {
12162 c = mb_ptr2char(fword + sp->ts_fidx);
12163 stack[depth].ts_fidx += MB_BYTE2LEN(fword[sp->ts_fidx]);
12164 if (enc_utf8 && utf_iscomposing(c))
12165 stack[depth].ts_score -= SCORE_DEL - SCORE_DELCOMP;
12166 else if (c == mb_ptr2char(fword + stack[depth].ts_fidx))
12167 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12168 }
12169 else
12170#endif
12171 {
12172 ++stack[depth].ts_fidx;
12173 if (fword[sp->ts_fidx] == fword[sp->ts_fidx + 1])
12174 stack[depth].ts_score -= SCORE_DEL - SCORE_DELDUP;
12175 }
12176 break;
12177 }
12178 /*FALLTHROUGH*/
12179
12180 case STATE_INS_PREP:
12181 if (sp->ts_flags & TSF_DIDDEL)
12182 {
12183 /* If we just deleted a byte then inserting won't make sense,
12184 * a substitute is always cheaper. */
12185 sp->ts_state = STATE_SWAP;
12186 break;
12187 }
12188
12189 /* skip over NUL bytes */
12190 n = sp->ts_arridx;
12191 for (;;)
12192 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012193 if (sp->ts_curi > byts[n])
12194 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012195 /* Only NUL bytes at this node, go to next state. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012196 sp->ts_state = STATE_SWAP;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012197 break;
12198 }
12199 if (byts[n + sp->ts_curi] != NUL)
12200 {
12201 /* Found a byte to insert. */
12202 sp->ts_state = STATE_INS;
12203 break;
12204 }
12205 ++sp->ts_curi;
12206 }
12207 break;
12208
12209 /*FALLTHROUGH*/
12210
12211 case STATE_INS:
12212 /* Insert one byte. Repeat this for each possible byte at this
12213 * node. */
12214 n = sp->ts_arridx;
12215 if (sp->ts_curi > byts[n])
12216 {
12217 /* Done all bytes at this node, go to next state. */
12218 sp->ts_state = STATE_SWAP;
12219 break;
12220 }
12221
12222 /* Do one more byte at this node, but:
12223 * - Skip NUL bytes.
12224 * - Skip the byte if it's equal to the byte in the word,
12225 * accepting that byte is always better.
12226 */
12227 n += sp->ts_curi++;
12228 c = byts[n];
12229 if (soundfold && sp->ts_twordlen == 0 && c == '*')
12230 /* Inserting a vowel at the start of a word counts less,
12231 * see soundalike_score(). */
12232 newscore = 2 * SCORE_INS / 3;
12233 else
12234 newscore = SCORE_INS;
12235 if (c != fword[sp->ts_fidx]
12236 && TRY_DEEPER(su, stack, depth, newscore))
12237 {
12238 go_deeper(stack, depth, newscore);
12239#ifdef DEBUG_TRIEWALK
12240 sprintf(changename[depth], "%.*s-%s: insert %c",
12241 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12242 c);
12243#endif
12244 ++depth;
12245 sp = &stack[depth];
12246 tword[sp->ts_twordlen++] = c;
12247 sp->ts_arridx = idxs[n];
12248#ifdef FEAT_MBYTE
12249 if (has_mbyte)
12250 {
12251 fl = MB_BYTE2LEN(c);
12252 if (fl > 1)
12253 {
12254 /* There are following bytes for the same character.
12255 * We must find all bytes before trying
12256 * delete/insert/swap/etc. */
12257 sp->ts_tcharlen = fl;
12258 sp->ts_tcharidx = 1;
12259 sp->ts_isdiff = DIFF_INSERT;
12260 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012261 }
12262 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000012263 fl = 1;
12264 if (fl == 1)
Bram Moolenaarea424162005-06-16 21:51:00 +000012265#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012266 {
12267 /* If the previous character was the same, thus doubling a
12268 * character, give a bonus to the score. Also for
12269 * soundfold words (illogical but does give a better
12270 * score). */
12271 if (sp->ts_twordlen >= 2
Bram Moolenaarea408852005-06-25 22:49:46 +000012272 && tword[sp->ts_twordlen - 2] == c)
Bram Moolenaar4770d092006-01-12 23:22:24 +000012273 sp->ts_score -= SCORE_INS - SCORE_INSDUP;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012274 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012275 }
12276 break;
12277
12278 case STATE_SWAP:
12279 /*
12280 * Swap two bytes in the bad word: "12" -> "21".
12281 * We change "fword" here, it's changed back afterwards at
12282 * STATE_UNSWAP.
12283 */
12284 p = fword + sp->ts_fidx;
12285 c = *p;
12286 if (c == NUL)
12287 {
12288 /* End of word, can't swap or replace. */
12289 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012290 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012291 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012292
Bram Moolenaar4770d092006-01-12 23:22:24 +000012293 /* Don't swap if the first character is not a word character.
12294 * SWAP3 etc. also don't make sense then. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020012295 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012296 {
12297 sp->ts_state = STATE_REP_INI;
12298 break;
12299 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012300
Bram Moolenaar4770d092006-01-12 23:22:24 +000012301#ifdef FEAT_MBYTE
12302 if (has_mbyte)
12303 {
12304 n = mb_cptr2len(p);
12305 c = mb_ptr2char(p);
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012306 if (p[n] == NUL)
12307 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +020012308 else if (!soundfold && !spell_iswordp(p + n, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012309 c2 = c; /* don't swap non-word char */
12310 else
12311 c2 = mb_ptr2char(p + n);
12312 }
12313 else
12314#endif
12315 {
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012316 if (p[1] == NUL)
12317 c2 = NUL;
Bram Moolenaar860cae12010-06-05 23:22:07 +020012318 else if (!soundfold && !spell_iswordp(p + 1, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012319 c2 = c; /* don't swap non-word char */
12320 else
12321 c2 = p[1];
12322 }
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012323
Bram Moolenaar3dcfbf72007-08-05 16:33:12 +000012324 /* When the second character is NUL we can't swap. */
12325 if (c2 == NUL)
12326 {
12327 sp->ts_state = STATE_REP_INI;
12328 break;
12329 }
12330
Bram Moolenaar4770d092006-01-12 23:22:24 +000012331 /* When characters are identical, swap won't do anything.
12332 * Also get here if the second char is not a word character. */
12333 if (c == c2)
12334 {
12335 sp->ts_state = STATE_SWAP3;
12336 break;
12337 }
12338 if (c2 != NUL && TRY_DEEPER(su, stack, depth, SCORE_SWAP))
12339 {
12340 go_deeper(stack, depth, SCORE_SWAP);
12341#ifdef DEBUG_TRIEWALK
12342 sprintf(changename[depth], "%.*s-%s: swap %c and %c",
12343 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12344 c, c2);
12345#endif
12346 sp->ts_state = STATE_UNSWAP;
12347 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012348#ifdef FEAT_MBYTE
12349 if (has_mbyte)
12350 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012351 fl = mb_char2len(c2);
12352 mch_memmove(p, p + n, fl);
12353 mb_char2bytes(c, p + fl);
12354 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012355 }
12356 else
12357#endif
Bram Moolenaarbb15b652005-10-03 21:52:09 +000012358 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012359 p[0] = c2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012360 p[1] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012361 stack[depth].ts_fidxtry = sp->ts_fidx + 2;
Bram Moolenaarea424162005-06-16 21:51:00 +000012362 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012363 }
12364 else
12365 /* If this swap doesn't work then SWAP3 won't either. */
12366 sp->ts_state = STATE_REP_INI;
12367 break;
Bram Moolenaarea424162005-06-16 21:51:00 +000012368
Bram Moolenaar4770d092006-01-12 23:22:24 +000012369 case STATE_UNSWAP:
12370 /* Undo the STATE_SWAP swap: "21" -> "12". */
12371 p = fword + sp->ts_fidx;
12372#ifdef FEAT_MBYTE
12373 if (has_mbyte)
12374 {
12375 n = MB_BYTE2LEN(*p);
12376 c = mb_ptr2char(p + n);
12377 mch_memmove(p + MB_BYTE2LEN(p[n]), p, n);
12378 mb_char2bytes(c, p);
12379 }
12380 else
12381#endif
12382 {
12383 c = *p;
12384 *p = p[1];
12385 p[1] = c;
12386 }
12387 /*FALLTHROUGH*/
12388
12389 case STATE_SWAP3:
12390 /* Swap two bytes, skipping one: "123" -> "321". We change
12391 * "fword" here, it's changed back afterwards at STATE_UNSWAP3. */
12392 p = fword + sp->ts_fidx;
12393#ifdef FEAT_MBYTE
12394 if (has_mbyte)
12395 {
12396 n = mb_cptr2len(p);
12397 c = mb_ptr2char(p);
12398 fl = mb_cptr2len(p + n);
12399 c2 = mb_ptr2char(p + n);
Bram Moolenaar860cae12010-06-05 23:22:07 +020012400 if (!soundfold && !spell_iswordp(p + n + fl, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012401 c3 = c; /* don't swap non-word char */
12402 else
12403 c3 = mb_ptr2char(p + n + fl);
12404 }
12405 else
12406#endif
12407 {
12408 c = *p;
12409 c2 = p[1];
Bram Moolenaar860cae12010-06-05 23:22:07 +020012410 if (!soundfold && !spell_iswordp(p + 2, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012411 c3 = c; /* don't swap non-word char */
12412 else
12413 c3 = p[2];
12414 }
12415
12416 /* When characters are identical: "121" then SWAP3 result is
12417 * identical, ROT3L result is same as SWAP: "211", ROT3L result is
12418 * same as SWAP on next char: "112". Thus skip all swapping.
12419 * Also skip when c3 is NUL.
12420 * Also get here when the third character is not a word character.
12421 * Second character may any char: "a.b" -> "b.a" */
12422 if (c == c3 || c3 == NUL)
12423 {
12424 sp->ts_state = STATE_REP_INI;
12425 break;
12426 }
12427 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12428 {
12429 go_deeper(stack, depth, SCORE_SWAP3);
12430#ifdef DEBUG_TRIEWALK
12431 sprintf(changename[depth], "%.*s-%s: swap3 %c and %c",
12432 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12433 c, c3);
12434#endif
12435 sp->ts_state = STATE_UNSWAP3;
12436 ++depth;
12437#ifdef FEAT_MBYTE
12438 if (has_mbyte)
12439 {
12440 tl = mb_char2len(c3);
12441 mch_memmove(p, p + n + fl, tl);
12442 mb_char2bytes(c2, p + tl);
12443 mb_char2bytes(c, p + fl + tl);
12444 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl + tl;
12445 }
12446 else
12447#endif
12448 {
12449 p[0] = p[2];
12450 p[2] = c;
12451 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12452 }
12453 }
12454 else
12455 sp->ts_state = STATE_REP_INI;
12456 break;
12457
12458 case STATE_UNSWAP3:
12459 /* Undo STATE_SWAP3: "321" -> "123" */
12460 p = fword + sp->ts_fidx;
12461#ifdef FEAT_MBYTE
12462 if (has_mbyte)
12463 {
12464 n = MB_BYTE2LEN(*p);
12465 c2 = mb_ptr2char(p + n);
12466 fl = MB_BYTE2LEN(p[n]);
12467 c = mb_ptr2char(p + n + fl);
12468 tl = MB_BYTE2LEN(p[n + fl]);
12469 mch_memmove(p + fl + tl, p, n);
12470 mb_char2bytes(c, p);
12471 mb_char2bytes(c2, p + tl);
12472 p = p + tl;
12473 }
12474 else
12475#endif
12476 {
12477 c = *p;
12478 *p = p[2];
12479 p[2] = c;
12480 ++p;
12481 }
12482
Bram Moolenaar860cae12010-06-05 23:22:07 +020012483 if (!soundfold && !spell_iswordp(p, curwin))
Bram Moolenaar4770d092006-01-12 23:22:24 +000012484 {
12485 /* Middle char is not a word char, skip the rotate. First and
12486 * third char were already checked at swap and swap3. */
12487 sp->ts_state = STATE_REP_INI;
12488 break;
12489 }
12490
12491 /* Rotate three characters left: "123" -> "231". We change
12492 * "fword" here, it's changed back afterwards at STATE_UNROT3L. */
12493 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12494 {
12495 go_deeper(stack, depth, SCORE_SWAP3);
12496#ifdef DEBUG_TRIEWALK
12497 p = fword + sp->ts_fidx;
12498 sprintf(changename[depth], "%.*s-%s: rotate left %c%c%c",
12499 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12500 p[0], p[1], p[2]);
12501#endif
12502 sp->ts_state = STATE_UNROT3L;
12503 ++depth;
Bram Moolenaarea424162005-06-16 21:51:00 +000012504 p = fword + sp->ts_fidx;
12505#ifdef FEAT_MBYTE
12506 if (has_mbyte)
12507 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012508 n = mb_cptr2len(p);
Bram Moolenaarea424162005-06-16 21:51:00 +000012509 c = mb_ptr2char(p);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012510 fl = mb_cptr2len(p + n);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012511 fl += mb_cptr2len(p + n + fl);
12512 mch_memmove(p, p + n, fl);
12513 mb_char2bytes(c, p + fl);
12514 stack[depth].ts_fidxtry = sp->ts_fidx + n + fl;
Bram Moolenaarea424162005-06-16 21:51:00 +000012515 }
12516 else
12517#endif
12518 {
12519 c = *p;
12520 *p = p[1];
12521 p[1] = p[2];
12522 p[2] = c;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012523 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
Bram Moolenaarea424162005-06-16 21:51:00 +000012524 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012525 }
12526 else
12527 sp->ts_state = STATE_REP_INI;
12528 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012529
Bram Moolenaar4770d092006-01-12 23:22:24 +000012530 case STATE_UNROT3L:
12531 /* Undo ROT3L: "231" -> "123" */
12532 p = fword + sp->ts_fidx;
Bram Moolenaarea424162005-06-16 21:51:00 +000012533#ifdef FEAT_MBYTE
Bram Moolenaar4770d092006-01-12 23:22:24 +000012534 if (has_mbyte)
12535 {
12536 n = MB_BYTE2LEN(*p);
12537 n += MB_BYTE2LEN(p[n]);
12538 c = mb_ptr2char(p + n);
12539 tl = MB_BYTE2LEN(p[n]);
12540 mch_memmove(p + tl, p, n);
12541 mb_char2bytes(c, p);
12542 }
12543 else
Bram Moolenaarea424162005-06-16 21:51:00 +000012544#endif
Bram Moolenaar4770d092006-01-12 23:22:24 +000012545 {
12546 c = p[2];
12547 p[2] = p[1];
12548 p[1] = *p;
12549 *p = c;
12550 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012551
Bram Moolenaar4770d092006-01-12 23:22:24 +000012552 /* Rotate three bytes right: "123" -> "312". We change "fword"
12553 * here, it's changed back afterwards at STATE_UNROT3R. */
12554 if (TRY_DEEPER(su, stack, depth, SCORE_SWAP3))
12555 {
12556 go_deeper(stack, depth, SCORE_SWAP3);
12557#ifdef DEBUG_TRIEWALK
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012558 p = fword + sp->ts_fidx;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012559 sprintf(changename[depth], "%.*s-%s: rotate right %c%c%c",
12560 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12561 p[0], p[1], p[2]);
12562#endif
12563 sp->ts_state = STATE_UNROT3R;
12564 ++depth;
12565 p = fword + sp->ts_fidx;
12566#ifdef FEAT_MBYTE
12567 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000012568 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012569 n = mb_cptr2len(p);
12570 n += mb_cptr2len(p + n);
12571 c = mb_ptr2char(p + n);
12572 tl = mb_cptr2len(p + n);
12573 mch_memmove(p + tl, p, n);
12574 mb_char2bytes(c, p);
12575 stack[depth].ts_fidxtry = sp->ts_fidx + n + tl;
Bram Moolenaar0c405862005-06-22 22:26:26 +000012576 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012577 else
12578#endif
12579 {
12580 c = p[2];
12581 p[2] = p[1];
12582 p[1] = *p;
12583 *p = c;
12584 stack[depth].ts_fidxtry = sp->ts_fidx + 3;
12585 }
12586 }
12587 else
12588 sp->ts_state = STATE_REP_INI;
12589 break;
12590
12591 case STATE_UNROT3R:
12592 /* Undo ROT3R: "312" -> "123" */
12593 p = fword + sp->ts_fidx;
12594#ifdef FEAT_MBYTE
12595 if (has_mbyte)
12596 {
12597 c = mb_ptr2char(p);
12598 tl = MB_BYTE2LEN(*p);
12599 n = MB_BYTE2LEN(p[tl]);
12600 n += MB_BYTE2LEN(p[tl + n]);
12601 mch_memmove(p, p + tl, n);
12602 mb_char2bytes(c, p + n);
12603 }
12604 else
12605#endif
12606 {
12607 c = *p;
12608 *p = p[1];
12609 p[1] = p[2];
12610 p[2] = c;
12611 }
12612 /*FALLTHROUGH*/
12613
12614 case STATE_REP_INI:
12615 /* Check if matching with REP items from the .aff file would work.
12616 * Quickly skip if:
12617 * - there are no REP items and we are not in the soundfold trie
12618 * - the score is going to be too high anyway
12619 * - already applied a REP item or swapped here */
12620 if ((lp->lp_replang == NULL && !soundfold)
12621 || sp->ts_score + SCORE_REP >= su->su_maxscore
12622 || sp->ts_fidx < sp->ts_fidxtry)
12623 {
12624 sp->ts_state = STATE_FINAL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012625 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012626 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012627
Bram Moolenaar4770d092006-01-12 23:22:24 +000012628 /* Use the first byte to quickly find the first entry that may
12629 * match. If the index is -1 there is none. */
12630 if (soundfold)
12631 sp->ts_curi = slang->sl_repsal_first[fword[sp->ts_fidx]];
12632 else
12633 sp->ts_curi = lp->lp_replang->sl_rep_first[fword[sp->ts_fidx]];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012634
Bram Moolenaar4770d092006-01-12 23:22:24 +000012635 if (sp->ts_curi < 0)
12636 {
12637 sp->ts_state = STATE_FINAL;
12638 break;
12639 }
12640
12641 sp->ts_state = STATE_REP;
12642 /*FALLTHROUGH*/
12643
12644 case STATE_REP:
12645 /* Try matching with REP items from the .aff file. For each match
12646 * replace the characters and check if the resulting word is
12647 * valid. */
12648 p = fword + sp->ts_fidx;
12649
12650 if (soundfold)
12651 gap = &slang->sl_repsal;
12652 else
12653 gap = &lp->lp_replang->sl_rep;
12654 while (sp->ts_curi < gap->ga_len)
12655 {
12656 ftp = (fromto_T *)gap->ga_data + sp->ts_curi++;
12657 if (*ftp->ft_from != *p)
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012658 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012659 /* past possible matching entries */
12660 sp->ts_curi = gap->ga_len;
12661 break;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012662 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000012663 if (STRNCMP(ftp->ft_from, p, STRLEN(ftp->ft_from)) == 0
12664 && TRY_DEEPER(su, stack, depth, SCORE_REP))
12665 {
12666 go_deeper(stack, depth, SCORE_REP);
12667#ifdef DEBUG_TRIEWALK
12668 sprintf(changename[depth], "%.*s-%s: replace %s with %s",
12669 sp->ts_twordlen, tword, fword + sp->ts_fidx,
12670 ftp->ft_from, ftp->ft_to);
12671#endif
12672 /* Need to undo this afterwards. */
12673 sp->ts_state = STATE_REP_UNDO;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012674
Bram Moolenaar4770d092006-01-12 23:22:24 +000012675 /* Change the "from" to the "to" string. */
12676 ++depth;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012677 fl = (int)STRLEN(ftp->ft_from);
12678 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012679 if (fl != tl)
12680 {
Bram Moolenaara7241f52008-06-24 20:39:31 +000012681 STRMOVE(p + tl, p + fl);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012682 repextra += tl - fl;
12683 }
12684 mch_memmove(p, ftp->ft_to, tl);
12685 stack[depth].ts_fidxtry = sp->ts_fidx + tl;
12686#ifdef FEAT_MBYTE
12687 stack[depth].ts_tcharlen = 0;
12688#endif
12689 break;
12690 }
12691 }
12692
12693 if (sp->ts_curi >= gap->ga_len && sp->ts_state == STATE_REP)
12694 /* No (more) matches. */
12695 sp->ts_state = STATE_FINAL;
12696
12697 break;
12698
12699 case STATE_REP_UNDO:
12700 /* Undo a REP replacement and continue with the next one. */
12701 if (soundfold)
12702 gap = &slang->sl_repsal;
12703 else
12704 gap = &lp->lp_replang->sl_rep;
12705 ftp = (fromto_T *)gap->ga_data + sp->ts_curi - 1;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012706 fl = (int)STRLEN(ftp->ft_from);
12707 tl = (int)STRLEN(ftp->ft_to);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012708 p = fword + sp->ts_fidx;
12709 if (fl != tl)
12710 {
Bram Moolenaara7241f52008-06-24 20:39:31 +000012711 STRMOVE(p + fl, p + tl);
Bram Moolenaar4770d092006-01-12 23:22:24 +000012712 repextra -= tl - fl;
12713 }
12714 mch_memmove(p, ftp->ft_from, fl);
12715 sp->ts_state = STATE_REP;
12716 break;
12717
12718 default:
12719 /* Did all possible states at this level, go up one level. */
12720 --depth;
12721
12722 if (depth >= 0 && stack[depth].ts_prefixdepth == PFD_PREFIXTREE)
12723 {
12724 /* Continue in or go back to the prefix tree. */
12725 byts = pbyts;
12726 idxs = pidxs;
12727 }
12728
12729 /* Don't check for CTRL-C too often, it takes time. */
12730 if (--breakcheckcount == 0)
12731 {
12732 ui_breakcheck();
12733 breakcheckcount = 1000;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012734 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012735 }
12736 }
12737}
12738
Bram Moolenaar4770d092006-01-12 23:22:24 +000012739
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012740/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000012741 * Go one level deeper in the tree.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012742 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000012743 static void
12744go_deeper(stack, depth, score_add)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012745 trystate_T *stack;
12746 int depth;
12747 int score_add;
12748{
Bram Moolenaarea424162005-06-16 21:51:00 +000012749 stack[depth + 1] = stack[depth];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012750 stack[depth + 1].ts_state = STATE_START;
Bram Moolenaar4770d092006-01-12 23:22:24 +000012751 stack[depth + 1].ts_score = stack[depth].ts_score + score_add;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012752 stack[depth + 1].ts_curi = 1; /* start just after length byte */
Bram Moolenaard12a1322005-08-21 22:08:24 +000012753 stack[depth + 1].ts_flags = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012754}
12755
Bram Moolenaar53805d12005-08-01 07:08:33 +000012756#ifdef FEAT_MBYTE
12757/*
12758 * Case-folding may change the number of bytes: Count nr of chars in
12759 * fword[flen] and return the byte length of that many chars in "word".
12760 */
12761 static int
12762nofold_len(fword, flen, word)
12763 char_u *fword;
12764 int flen;
12765 char_u *word;
12766{
12767 char_u *p;
12768 int i = 0;
12769
12770 for (p = fword; p < fword + flen; mb_ptr_adv(p))
12771 ++i;
12772 for (p = word; i > 0; mb_ptr_adv(p))
12773 --i;
12774 return (int)(p - word);
12775}
12776#endif
12777
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012778/*
12779 * "fword" is a good word with case folded. Find the matching keep-case
12780 * words and put it in "kword".
12781 * Theoretically there could be several keep-case words that result in the
12782 * same case-folded word, but we only find one...
12783 */
12784 static void
12785find_keepcap_word(slang, fword, kword)
12786 slang_T *slang;
12787 char_u *fword;
12788 char_u *kword;
12789{
12790 char_u uword[MAXWLEN]; /* "fword" in upper-case */
12791 int depth;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012792 idx_T tryidx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012793
12794 /* The following arrays are used at each depth in the tree. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012795 idx_T arridx[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012796 int round[MAXWLEN];
12797 int fwordidx[MAXWLEN];
12798 int uwordidx[MAXWLEN];
12799 int kwordlen[MAXWLEN];
12800
12801 int flen, ulen;
12802 int l;
12803 int len;
12804 int c;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012805 idx_T lo, hi, m;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012806 char_u *p;
12807 char_u *byts = slang->sl_kbyts; /* array with bytes of the words */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000012808 idx_T *idxs = slang->sl_kidxs; /* array with indexes */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012809
12810 if (byts == NULL)
12811 {
12812 /* array is empty: "cannot happen" */
12813 *kword = NUL;
12814 return;
12815 }
12816
12817 /* Make an all-cap version of "fword". */
12818 allcap_copy(fword, uword);
12819
12820 /*
12821 * Each character needs to be tried both case-folded and upper-case.
12822 * All this gets very complicated if we keep in mind that changing case
12823 * may change the byte length of a multi-byte character...
12824 */
12825 depth = 0;
12826 arridx[0] = 0;
12827 round[0] = 0;
12828 fwordidx[0] = 0;
12829 uwordidx[0] = 0;
12830 kwordlen[0] = 0;
12831 while (depth >= 0)
12832 {
12833 if (fword[fwordidx[depth]] == NUL)
12834 {
12835 /* We are at the end of "fword". If the tree allows a word to end
12836 * here we have found a match. */
12837 if (byts[arridx[depth] + 1] == 0)
12838 {
12839 kword[kwordlen[depth]] = NUL;
12840 return;
12841 }
12842
12843 /* kword is getting too long, continue one level up */
12844 --depth;
12845 }
12846 else if (++round[depth] > 2)
12847 {
12848 /* tried both fold-case and upper-case character, continue one
12849 * level up */
12850 --depth;
12851 }
12852 else
12853 {
12854 /*
12855 * round[depth] == 1: Try using the folded-case character.
12856 * round[depth] == 2: Try using the upper-case character.
12857 */
12858#ifdef FEAT_MBYTE
12859 if (has_mbyte)
12860 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000012861 flen = mb_cptr2len(fword + fwordidx[depth]);
12862 ulen = mb_cptr2len(uword + uwordidx[depth]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012863 }
12864 else
12865#endif
12866 ulen = flen = 1;
12867 if (round[depth] == 1)
12868 {
12869 p = fword + fwordidx[depth];
12870 l = flen;
12871 }
12872 else
12873 {
12874 p = uword + uwordidx[depth];
12875 l = ulen;
12876 }
12877
12878 for (tryidx = arridx[depth]; l > 0; --l)
12879 {
12880 /* Perform a binary search in the list of accepted bytes. */
12881 len = byts[tryidx++];
12882 c = *p++;
12883 lo = tryidx;
12884 hi = tryidx + len - 1;
12885 while (lo < hi)
12886 {
12887 m = (lo + hi) / 2;
12888 if (byts[m] > c)
12889 hi = m - 1;
12890 else if (byts[m] < c)
12891 lo = m + 1;
12892 else
12893 {
12894 lo = hi = m;
12895 break;
12896 }
12897 }
12898
12899 /* Stop if there is no matching byte. */
12900 if (hi < lo || byts[lo] != c)
12901 break;
12902
12903 /* Continue at the child (if there is one). */
12904 tryidx = idxs[lo];
12905 }
12906
12907 if (l == 0)
12908 {
12909 /*
12910 * Found the matching char. Copy it to "kword" and go a
12911 * level deeper.
12912 */
12913 if (round[depth] == 1)
12914 {
12915 STRNCPY(kword + kwordlen[depth], fword + fwordidx[depth],
12916 flen);
12917 kwordlen[depth + 1] = kwordlen[depth] + flen;
12918 }
12919 else
12920 {
12921 STRNCPY(kword + kwordlen[depth], uword + uwordidx[depth],
12922 ulen);
12923 kwordlen[depth + 1] = kwordlen[depth] + ulen;
12924 }
12925 fwordidx[depth + 1] = fwordidx[depth] + flen;
12926 uwordidx[depth + 1] = uwordidx[depth] + ulen;
12927
12928 ++depth;
12929 arridx[depth] = tryidx;
12930 round[depth] = 0;
12931 }
12932 }
12933 }
12934
12935 /* Didn't find it: "cannot happen". */
12936 *kword = NUL;
12937}
12938
12939/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012940 * Compute the sound-a-like score for suggestions in su->su_ga and add them to
12941 * su->su_sga.
12942 */
12943 static void
12944score_comp_sal(su)
12945 suginfo_T *su;
12946{
12947 langp_T *lp;
12948 char_u badsound[MAXWLEN];
12949 int i;
12950 suggest_T *stp;
12951 suggest_T *sstp;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012952 int score;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012953 int lpi;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012954
12955 if (ga_grow(&su->su_sga, su->su_ga.ga_len) == FAIL)
12956 return;
12957
12958 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020012959 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012960 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020012961 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012962 if (lp->lp_slang->sl_sal.ga_len > 0)
12963 {
12964 /* soundfold the bad word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000012965 spell_soundfold(lp->lp_slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012966
12967 for (i = 0; i < su->su_ga.ga_len; ++i)
12968 {
12969 stp = &SUG(su->su_ga, i);
12970
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000012971 /* Case-fold the suggested word, sound-fold it and compute the
12972 * sound-a-like score. */
12973 score = stp_sal_score(stp, su, lp->lp_slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012974 if (score < SCORE_MAXMAX)
12975 {
12976 /* Add the suggestion. */
12977 sstp = &SUG(su->su_sga, su->su_sga.ga_len);
12978 sstp->st_word = vim_strsave(stp->st_word);
12979 if (sstp->st_word != NULL)
12980 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000012981 sstp->st_wordlen = stp->st_wordlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012982 sstp->st_score = score;
12983 sstp->st_altscore = 0;
12984 sstp->st_orglen = stp->st_orglen;
12985 ++su->su_sga.ga_len;
12986 }
12987 }
12988 }
12989 break;
12990 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012991 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000012992}
12993
12994/*
12995 * Combine the list of suggestions in su->su_ga and su->su_sga.
12996 * They are intwined.
12997 */
12998 static void
12999score_combine(su)
13000 suginfo_T *su;
13001{
13002 int i;
13003 int j;
13004 garray_T ga;
13005 garray_T *gap;
13006 langp_T *lp;
13007 suggest_T *stp;
13008 char_u *p;
13009 char_u badsound[MAXWLEN];
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013010 int round;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013011 int lpi;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013012 slang_T *slang = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013013
13014 /* Add the alternate score to su_ga. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013015 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013016 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013017 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013018 if (lp->lp_slang->sl_sal.ga_len > 0)
13019 {
13020 /* soundfold the bad word */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013021 slang = lp->lp_slang;
13022 spell_soundfold(slang, su->su_fbadword, TRUE, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013023
13024 for (i = 0; i < su->su_ga.ga_len; ++i)
13025 {
13026 stp = &SUG(su->su_ga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013027 stp->st_altscore = stp_sal_score(stp, su, slang, badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013028 if (stp->st_altscore == SCORE_MAXMAX)
13029 stp->st_score = (stp->st_score * 3 + SCORE_BIG) / 4;
13030 else
13031 stp->st_score = (stp->st_score * 3
13032 + stp->st_altscore) / 4;
13033 stp->st_salscore = FALSE;
13034 }
13035 break;
13036 }
13037 }
13038
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013039 if (slang == NULL) /* Using "double" without sound folding. */
13040 {
13041 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore,
13042 su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013043 return;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013044 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013045
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013046 /* Add the alternate score to su_sga. */
13047 for (i = 0; i < su->su_sga.ga_len; ++i)
13048 {
13049 stp = &SUG(su->su_sga, i);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013050 stp->st_altscore = spell_edit_score(slang,
13051 su->su_badword, stp->st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013052 if (stp->st_score == SCORE_MAXMAX)
13053 stp->st_score = (SCORE_BIG * 7 + stp->st_altscore) / 8;
13054 else
13055 stp->st_score = (stp->st_score * 7 + stp->st_altscore) / 8;
13056 stp->st_salscore = TRUE;
13057 }
13058
Bram Moolenaar4770d092006-01-12 23:22:24 +000013059 /* Remove bad suggestions, sort the suggestions and truncate at "maxcount"
13060 * for both lists. */
13061 check_suggestions(su, &su->su_ga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013062 (void)cleanup_suggestions(&su->su_ga, su->su_maxscore, su->su_maxcount);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013063 check_suggestions(su, &su->su_sga);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013064 (void)cleanup_suggestions(&su->su_sga, su->su_maxscore, su->su_maxcount);
13065
13066 ga_init2(&ga, (int)sizeof(suginfo_T), 1);
13067 if (ga_grow(&ga, su->su_ga.ga_len + su->su_sga.ga_len) == FAIL)
13068 return;
13069
13070 stp = &SUG(ga, 0);
13071 for (i = 0; i < su->su_ga.ga_len || i < su->su_sga.ga_len; ++i)
13072 {
13073 /* round 1: get a suggestion from su_ga
13074 * round 2: get a suggestion from su_sga */
13075 for (round = 1; round <= 2; ++round)
13076 {
13077 gap = round == 1 ? &su->su_ga : &su->su_sga;
13078 if (i < gap->ga_len)
13079 {
13080 /* Don't add a word if it's already there. */
13081 p = SUG(*gap, i).st_word;
13082 for (j = 0; j < ga.ga_len; ++j)
13083 if (STRCMP(stp[j].st_word, p) == 0)
13084 break;
13085 if (j == ga.ga_len)
13086 stp[ga.ga_len++] = SUG(*gap, i);
13087 else
13088 vim_free(p);
13089 }
13090 }
13091 }
13092
13093 ga_clear(&su->su_ga);
13094 ga_clear(&su->su_sga);
13095
13096 /* Truncate the list to the number of suggestions that will be displayed. */
13097 if (ga.ga_len > su->su_maxcount)
13098 {
13099 for (i = su->su_maxcount; i < ga.ga_len; ++i)
13100 vim_free(stp[i].st_word);
13101 ga.ga_len = su->su_maxcount;
13102 }
13103
13104 su->su_ga = ga;
13105}
13106
13107/*
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013108 * For the goodword in "stp" compute the soundalike score compared to the
13109 * badword.
13110 */
13111 static int
13112stp_sal_score(stp, su, slang, badsound)
13113 suggest_T *stp;
13114 suginfo_T *su;
13115 slang_T *slang;
13116 char_u *badsound; /* sound-folded badword */
13117{
13118 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013119 char_u *pbad;
13120 char_u *pgood;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013121 char_u badsound2[MAXWLEN];
13122 char_u fword[MAXWLEN];
13123 char_u goodsound[MAXWLEN];
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013124 char_u goodword[MAXWLEN];
13125 int lendiff;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013126
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013127 lendiff = (int)(su->su_badlen - stp->st_orglen);
13128 if (lendiff >= 0)
13129 pbad = badsound;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013130 else
13131 {
13132 /* soundfold the bad word with more characters following */
13133 (void)spell_casefold(su->su_badptr, stp->st_orglen, fword, MAXWLEN);
13134
13135 /* When joining two words the sound often changes a lot. E.g., "t he"
13136 * sounds like "t h" while "the" sounds like "@". Avoid that by
13137 * removing the space. Don't do it when the good word also contains a
13138 * space. */
13139 if (vim_iswhite(su->su_badptr[su->su_badlen])
13140 && *skiptowhite(stp->st_word) == NUL)
13141 for (p = fword; *(p = skiptowhite(p)) != NUL; )
Bram Moolenaara7241f52008-06-24 20:39:31 +000013142 STRMOVE(p, p + 1);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013143
Bram Moolenaar42eeac32005-06-29 22:40:58 +000013144 spell_soundfold(slang, fword, TRUE, badsound2);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013145 pbad = badsound2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013146 }
13147
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013148 if (lendiff > 0)
13149 {
13150 /* Add part of the bad word to the good word, so that we soundfold
13151 * what replaces the bad word. */
13152 STRCPY(goodword, stp->st_word);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013153 vim_strncpy(goodword + stp->st_wordlen,
13154 su->su_badptr + su->su_badlen - lendiff, lendiff);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013155 pgood = goodword;
13156 }
13157 else
13158 pgood = stp->st_word;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013159
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013160 /* Sound-fold the word and compute the score for the difference. */
13161 spell_soundfold(slang, pgood, FALSE, goodsound);
13162
13163 return soundalike_score(goodsound, pbad);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013164}
13165
Bram Moolenaar4770d092006-01-12 23:22:24 +000013166/* structure used to store soundfolded words that add_sound_suggest() has
13167 * handled already. */
13168typedef struct
13169{
13170 short sft_score; /* lowest score used */
13171 char_u sft_word[1]; /* soundfolded word, actually longer */
13172} sftword_T;
13173
13174static sftword_T dumsft;
13175#define HIKEY2SFT(p) ((sftword_T *)(p - (dumsft.sft_word - (char_u *)&dumsft)))
13176#define HI2SFT(hi) HIKEY2SFT((hi)->hi_key)
13177
13178/*
13179 * Prepare for calling suggest_try_soundalike().
13180 */
13181 static void
13182suggest_try_soundalike_prep()
13183{
13184 langp_T *lp;
13185 int lpi;
13186 slang_T *slang;
13187
13188 /* Do this for all languages that support sound folding and for which a
13189 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013190 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013191 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013192 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013193 slang = lp->lp_slang;
13194 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13195 /* prepare the hashtable used by add_sound_suggest() */
13196 hash_init(&slang->sl_sounddone);
13197 }
13198}
13199
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013200/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013201 * Find suggestions by comparing the word in a sound-a-like form.
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013202 * Note: This doesn't support postponed prefixes.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013203 */
13204 static void
Bram Moolenaar0c405862005-06-22 22:26:26 +000013205suggest_try_soundalike(su)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013206 suginfo_T *su;
13207{
13208 char_u salword[MAXWLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013209 langp_T *lp;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013210 int lpi;
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013211 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013212
Bram Moolenaar4770d092006-01-12 23:22:24 +000013213 /* Do this for all languages that support sound folding and for which a
13214 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013215 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013216 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013217 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013218 slang = lp->lp_slang;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013219 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013220 {
13221 /* soundfold the bad word */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013222 spell_soundfold(slang, su->su_fbadword, TRUE, salword);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013223
Bram Moolenaar4770d092006-01-12 23:22:24 +000013224 /* try all kinds of inserts/deletes/swaps/etc. */
13225 /* TODO: also soundfold the next words, so that we can try joining
13226 * and splitting */
13227 suggest_trie_walk(su, lp, salword, TRUE);
13228 }
13229 }
13230}
13231
13232/*
13233 * Finish up after calling suggest_try_soundalike().
13234 */
13235 static void
13236suggest_try_soundalike_finish()
13237{
13238 langp_T *lp;
13239 int lpi;
13240 slang_T *slang;
13241 int todo;
13242 hashitem_T *hi;
13243
13244 /* Do this for all languages that support sound folding and for which a
13245 * .sug file has been loaded. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020013246 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar4770d092006-01-12 23:22:24 +000013247 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020013248 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013249 slang = lp->lp_slang;
13250 if (slang->sl_sal.ga_len > 0 && slang->sl_sbyts != NULL)
13251 {
13252 /* Free the info about handled words. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013253 todo = (int)slang->sl_sounddone.ht_used;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013254 for (hi = slang->sl_sounddone.ht_array; todo > 0; ++hi)
13255 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013256 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013257 vim_free(HI2SFT(hi));
13258 --todo;
13259 }
Bram Moolenaar6417da62007-03-08 13:49:53 +000013260
13261 /* Clear the hashtable, it may also be used by another region. */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013262 hash_clear(&slang->sl_sounddone);
Bram Moolenaar6417da62007-03-08 13:49:53 +000013263 hash_init(&slang->sl_sounddone);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013264 }
13265 }
13266}
13267
13268/*
13269 * A match with a soundfolded word is found. Add the good word(s) that
13270 * produce this soundfolded word.
13271 */
13272 static void
13273add_sound_suggest(su, goodword, score, lp)
13274 suginfo_T *su;
13275 char_u *goodword;
13276 int score; /* soundfold score */
13277 langp_T *lp;
13278{
13279 slang_T *slang = lp->lp_slang; /* language for sound folding */
13280 int sfwordnr;
13281 char_u *nrline;
13282 int orgnr;
13283 char_u theword[MAXWLEN];
13284 int i;
13285 int wlen;
13286 char_u *byts;
13287 idx_T *idxs;
13288 int n;
13289 int wordcount;
13290 int wc;
13291 int goodscore;
13292 hash_T hash;
13293 hashitem_T *hi;
13294 sftword_T *sft;
13295 int bc, gc;
13296 int limit;
13297
13298 /*
13299 * It's very well possible that the same soundfold word is found several
13300 * times with different scores. Since the following is quite slow only do
13301 * the words that have a better score than before. Use a hashtable to
13302 * remember the words that have been done.
13303 */
13304 hash = hash_hash(goodword);
13305 hi = hash_lookup(&slang->sl_sounddone, goodword, hash);
13306 if (HASHITEM_EMPTY(hi))
13307 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000013308 sft = (sftword_T *)alloc((unsigned)(sizeof(sftword_T)
13309 + STRLEN(goodword)));
Bram Moolenaar4770d092006-01-12 23:22:24 +000013310 if (sft != NULL)
13311 {
13312 sft->sft_score = score;
13313 STRCPY(sft->sft_word, goodword);
13314 hash_add_item(&slang->sl_sounddone, hi, sft->sft_word, hash);
13315 }
13316 }
13317 else
13318 {
13319 sft = HI2SFT(hi);
13320 if (score >= sft->sft_score)
13321 return;
13322 sft->sft_score = score;
13323 }
13324
13325 /*
13326 * Find the word nr in the soundfold tree.
13327 */
13328 sfwordnr = soundfold_find(slang, goodword);
13329 if (sfwordnr < 0)
13330 {
13331 EMSG2(_(e_intern2), "add_sound_suggest()");
13332 return;
13333 }
13334
13335 /*
13336 * go over the list of good words that produce this soundfold word
13337 */
13338 nrline = ml_get_buf(slang->sl_sugbuf, (linenr_T)(sfwordnr + 1), FALSE);
13339 orgnr = 0;
13340 while (*nrline != NUL)
13341 {
13342 /* The wordnr was stored in a minimal nr of bytes as an offset to the
13343 * previous wordnr. */
13344 orgnr += bytes2offset(&nrline);
13345
13346 byts = slang->sl_fbyts;
13347 idxs = slang->sl_fidxs;
13348
13349 /* Lookup the word "orgnr" one of the two tries. */
13350 n = 0;
13351 wlen = 0;
13352 wordcount = 0;
13353 for (;;)
13354 {
13355 i = 1;
13356 if (wordcount == orgnr && byts[n + 1] == NUL)
13357 break; /* found end of word */
13358
13359 if (byts[n + 1] == NUL)
13360 ++wordcount;
13361
13362 /* skip over the NUL bytes */
13363 for ( ; byts[n + i] == NUL; ++i)
13364 if (i > byts[n]) /* safety check */
13365 {
13366 STRCPY(theword + wlen, "BAD");
13367 goto badword;
13368 }
13369
13370 /* One of the siblings must have the word. */
13371 for ( ; i < byts[n]; ++i)
13372 {
13373 wc = idxs[idxs[n + i]]; /* nr of words under this byte */
13374 if (wordcount + wc > orgnr)
13375 break;
13376 wordcount += wc;
13377 }
13378
13379 theword[wlen++] = byts[n + i];
13380 n = idxs[n + i];
13381 }
13382badword:
13383 theword[wlen] = NUL;
13384
13385 /* Go over the possible flags and regions. */
13386 for (; i <= byts[n] && byts[n + i] == NUL; ++i)
13387 {
13388 char_u cword[MAXWLEN];
13389 char_u *p;
13390 int flags = (int)idxs[n + i];
13391
Bram Moolenaare1438bb2006-03-01 22:01:55 +000013392 /* Skip words with the NOSUGGEST flag */
13393 if (flags & WF_NOSUGGEST)
13394 continue;
13395
Bram Moolenaar4770d092006-01-12 23:22:24 +000013396 if (flags & WF_KEEPCAP)
13397 {
13398 /* Must find the word in the keep-case tree. */
13399 find_keepcap_word(slang, theword, cword);
13400 p = cword;
13401 }
13402 else
13403 {
13404 flags |= su->su_badflags;
13405 if ((flags & WF_CAPMASK) != 0)
13406 {
13407 /* Need to fix case according to "flags". */
13408 make_case_word(theword, cword, flags);
13409 p = cword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013410 }
13411 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013412 p = theword;
13413 }
13414
13415 /* Add the suggestion. */
13416 if (sps_flags & SPS_DOUBLE)
13417 {
13418 /* Add the suggestion if the score isn't too bad. */
13419 if (score <= su->su_maxscore)
13420 add_suggestion(su, &su->su_sga, p, su->su_badlen,
13421 score, 0, FALSE, slang, FALSE);
13422 }
13423 else
13424 {
13425 /* Add a penalty for words in another region. */
13426 if ((flags & WF_REGION)
13427 && (((unsigned)flags >> 16) & lp->lp_region) == 0)
13428 goodscore = SCORE_REGION;
13429 else
13430 goodscore = 0;
13431
13432 /* Add a small penalty for changing the first letter from
13433 * lower to upper case. Helps for "tath" -> "Kath", which is
13434 * less common thatn "tath" -> "path". Don't do it when the
13435 * letter is the same, that has already been counted. */
13436 gc = PTR2CHAR(p);
13437 if (SPELL_ISUPPER(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013438 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013439 bc = PTR2CHAR(su->su_badword);
13440 if (!SPELL_ISUPPER(bc)
13441 && SPELL_TOFOLD(bc) != SPELL_TOFOLD(gc))
13442 goodscore += SCORE_ICASE / 2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013443 }
13444
Bram Moolenaar4770d092006-01-12 23:22:24 +000013445 /* Compute the score for the good word. This only does letter
13446 * insert/delete/swap/replace. REP items are not considered,
13447 * which may make the score a bit higher.
13448 * Use a limit for the score to make it work faster. Use
13449 * MAXSCORE(), because RESCORE() will change the score.
13450 * If the limit is very high then the iterative method is
13451 * inefficient, using an array is quicker. */
13452 limit = MAXSCORE(su->su_sfmaxscore - goodscore, score);
13453 if (limit > SCORE_LIMITMAX)
13454 goodscore += spell_edit_score(slang, su->su_badword, p);
13455 else
13456 goodscore += spell_edit_score_limit(slang, su->su_badword,
13457 p, limit);
13458
13459 /* When going over the limit don't bother to do the rest. */
13460 if (goodscore < SCORE_MAXMAX)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013461 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013462 /* Give a bonus to words seen before. */
13463 goodscore = score_wordcount_adj(slang, goodscore, p, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013464
Bram Moolenaar4770d092006-01-12 23:22:24 +000013465 /* Add the suggestion if the score isn't too bad. */
13466 goodscore = RESCORE(goodscore, score);
13467 if (goodscore <= su->su_sfmaxscore)
13468 add_suggestion(su, &su->su_ga, p, su->su_badlen,
13469 goodscore, score, TRUE, slang, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013470 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013471 }
13472 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013473 /* smsg("word %s (%d): %s (%d)", sftword, sftnr, theword, orgnr); */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013474 }
13475}
13476
13477/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013478 * Find word "word" in fold-case tree for "slang" and return the word number.
13479 */
13480 static int
13481soundfold_find(slang, word)
13482 slang_T *slang;
13483 char_u *word;
13484{
13485 idx_T arridx = 0;
13486 int len;
13487 int wlen = 0;
13488 int c;
13489 char_u *ptr = word;
13490 char_u *byts;
13491 idx_T *idxs;
13492 int wordnr = 0;
13493
13494 byts = slang->sl_sbyts;
13495 idxs = slang->sl_sidxs;
13496
13497 for (;;)
13498 {
13499 /* First byte is the number of possible bytes. */
13500 len = byts[arridx++];
13501
13502 /* If the first possible byte is a zero the word could end here.
13503 * If the word ends we found the word. If not skip the NUL bytes. */
13504 c = ptr[wlen];
13505 if (byts[arridx] == NUL)
13506 {
13507 if (c == NUL)
13508 break;
13509
13510 /* Skip over the zeros, there can be several. */
13511 while (len > 0 && byts[arridx] == NUL)
13512 {
13513 ++arridx;
13514 --len;
13515 }
13516 if (len == 0)
13517 return -1; /* no children, word should have ended here */
13518 ++wordnr;
13519 }
13520
13521 /* If the word ends we didn't find it. */
13522 if (c == NUL)
13523 return -1;
13524
13525 /* Perform a binary search in the list of accepted bytes. */
13526 if (c == TAB) /* <Tab> is handled like <Space> */
13527 c = ' ';
13528 while (byts[arridx] < c)
13529 {
13530 /* The word count is in the first idxs[] entry of the child. */
13531 wordnr += idxs[idxs[arridx]];
13532 ++arridx;
13533 if (--len == 0) /* end of the bytes, didn't find it */
13534 return -1;
13535 }
13536 if (byts[arridx] != c) /* didn't find the byte */
13537 return -1;
13538
13539 /* Continue at the child (if there is one). */
13540 arridx = idxs[arridx];
13541 ++wlen;
13542
13543 /* One space in the good word may stand for several spaces in the
13544 * checked word. */
13545 if (c == ' ')
13546 while (ptr[wlen] == ' ' || ptr[wlen] == TAB)
13547 ++wlen;
13548 }
13549
13550 return wordnr;
13551}
13552
13553/*
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013554 * Copy "fword" to "cword", fixing case according to "flags".
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013555 */
13556 static void
13557make_case_word(fword, cword, flags)
13558 char_u *fword;
13559 char_u *cword;
13560 int flags;
13561{
13562 if (flags & WF_ALLCAP)
13563 /* Make it all upper-case */
13564 allcap_copy(fword, cword);
13565 else if (flags & WF_ONECAP)
13566 /* Make the first letter upper-case */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013567 onecap_copy(fword, cword, TRUE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013568 else
13569 /* Use goodword as-is. */
13570 STRCPY(cword, fword);
13571}
13572
Bram Moolenaarea424162005-06-16 21:51:00 +000013573/*
13574 * Use map string "map" for languages "lp".
13575 */
13576 static void
13577set_map_str(lp, map)
13578 slang_T *lp;
13579 char_u *map;
13580{
13581 char_u *p;
13582 int headc = 0;
13583 int c;
13584 int i;
13585
13586 if (*map == NUL)
13587 {
13588 lp->sl_has_map = FALSE;
13589 return;
13590 }
13591 lp->sl_has_map = TRUE;
13592
Bram Moolenaar4770d092006-01-12 23:22:24 +000013593 /* Init the array and hash tables empty. */
Bram Moolenaarea424162005-06-16 21:51:00 +000013594 for (i = 0; i < 256; ++i)
13595 lp->sl_map_array[i] = 0;
13596#ifdef FEAT_MBYTE
13597 hash_init(&lp->sl_map_hash);
13598#endif
13599
13600 /*
13601 * The similar characters are stored separated with slashes:
13602 * "aaa/bbb/ccc/". Fill sl_map_array[c] with the character before c and
13603 * before the same slash. For characters above 255 sl_map_hash is used.
13604 */
13605 for (p = map; *p != NUL; )
13606 {
13607#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013608 c = mb_cptr2char_adv(&p);
Bram Moolenaarea424162005-06-16 21:51:00 +000013609#else
13610 c = *p++;
13611#endif
13612 if (c == '/')
13613 headc = 0;
13614 else
13615 {
13616 if (headc == 0)
13617 headc = c;
13618
13619#ifdef FEAT_MBYTE
13620 /* Characters above 255 don't fit in sl_map_array[], put them in
13621 * the hash table. Each entry is the char, a NUL the headchar and
13622 * a NUL. */
13623 if (c >= 256)
13624 {
13625 int cl = mb_char2len(c);
13626 int headcl = mb_char2len(headc);
13627 char_u *b;
13628 hash_T hash;
13629 hashitem_T *hi;
13630
13631 b = alloc((unsigned)(cl + headcl + 2));
13632 if (b == NULL)
13633 return;
13634 mb_char2bytes(c, b);
13635 b[cl] = NUL;
13636 mb_char2bytes(headc, b + cl + 1);
13637 b[cl + 1 + headcl] = NUL;
13638 hash = hash_hash(b);
13639 hi = hash_lookup(&lp->sl_map_hash, b, hash);
13640 if (HASHITEM_EMPTY(hi))
13641 hash_add_item(&lp->sl_map_hash, hi, b, hash);
13642 else
13643 {
13644 /* This should have been checked when generating the .spl
13645 * file. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013646 EMSG(_("E783: duplicate char in MAP entry"));
Bram Moolenaarea424162005-06-16 21:51:00 +000013647 vim_free(b);
13648 }
13649 }
13650 else
13651#endif
13652 lp->sl_map_array[c] = headc;
13653 }
13654 }
13655}
13656
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013657/*
13658 * Return TRUE if "c1" and "c2" are similar characters according to the MAP
13659 * lines in the .aff file.
13660 */
13661 static int
13662similar_chars(slang, c1, c2)
13663 slang_T *slang;
13664 int c1;
13665 int c2;
13666{
Bram Moolenaarea424162005-06-16 21:51:00 +000013667 int m1, m2;
13668#ifdef FEAT_MBYTE
13669 char_u buf[MB_MAXBYTES];
13670 hashitem_T *hi;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013671
Bram Moolenaarea424162005-06-16 21:51:00 +000013672 if (c1 >= 256)
13673 {
13674 buf[mb_char2bytes(c1, buf)] = 0;
13675 hi = hash_find(&slang->sl_map_hash, buf);
13676 if (HASHITEM_EMPTY(hi))
13677 m1 = 0;
13678 else
13679 m1 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13680 }
13681 else
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013682#endif
Bram Moolenaarea424162005-06-16 21:51:00 +000013683 m1 = slang->sl_map_array[c1];
13684 if (m1 == 0)
13685 return FALSE;
13686
13687
13688#ifdef FEAT_MBYTE
13689 if (c2 >= 256)
13690 {
13691 buf[mb_char2bytes(c2, buf)] = 0;
13692 hi = hash_find(&slang->sl_map_hash, buf);
13693 if (HASHITEM_EMPTY(hi))
13694 m2 = 0;
13695 else
13696 m2 = mb_ptr2char(hi->hi_key + STRLEN(hi->hi_key) + 1);
13697 }
13698 else
13699#endif
13700 m2 = slang->sl_map_array[c2];
13701
13702 return m1 == m2;
13703}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013704
13705/*
13706 * Add a suggestion to the list of suggestions.
Bram Moolenaar4770d092006-01-12 23:22:24 +000013707 * For a suggestion that is already in the list the lowest score is remembered.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013708 */
13709 static void
Bram Moolenaar4770d092006-01-12 23:22:24 +000013710add_suggestion(su, gap, goodword, badlenarg, score, altscore, had_bonus,
13711 slang, maxsf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013712 suginfo_T *su;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013713 garray_T *gap; /* either su_ga or su_sga */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013714 char_u *goodword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013715 int badlenarg; /* len of bad word replaced with "goodword" */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013716 int score;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000013717 int altscore;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013718 int had_bonus; /* value for st_had_bonus */
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013719 slang_T *slang; /* language for sound folding */
Bram Moolenaar4770d092006-01-12 23:22:24 +000013720 int maxsf; /* su_maxscore applies to soundfold score,
13721 su_sfmaxscore to the total score. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013722{
Bram Moolenaar4770d092006-01-12 23:22:24 +000013723 int goodlen; /* len of goodword changed */
13724 int badlen; /* len of bad word changed */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013725 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013726 suggest_T new_sug;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013727 int i;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013728 char_u *pgood, *pbad;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013729
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013730 /* Minimize "badlen" for consistency. Avoids that changing "the the" to
13731 * "thee the" is added next to changing the first "the" the "thee". */
13732 pgood = goodword + STRLEN(goodword);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013733 pbad = su->su_badptr + badlenarg;
13734 for (;;)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013735 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013736 goodlen = (int)(pgood - goodword);
13737 badlen = (int)(pbad - su->su_badptr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000013738 if (goodlen <= 0 || badlen <= 0)
13739 break;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013740 mb_ptr_back(goodword, pgood);
13741 mb_ptr_back(su->su_badptr, pbad);
13742#ifdef FEAT_MBYTE
13743 if (has_mbyte)
Bram Moolenaar0c405862005-06-22 22:26:26 +000013744 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013745 if (mb_ptr2char(pgood) != mb_ptr2char(pbad))
13746 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013747 }
13748 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013749#endif
13750 if (*pgood != *pbad)
13751 break;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013752 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000013753
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013754 if (badlen == 0 && goodlen == 0)
13755 /* goodword doesn't change anything; may happen for "the the" changing
13756 * the first "the" to itself. */
13757 return;
Bram Moolenaar0c405862005-06-22 22:26:26 +000013758
Bram Moolenaar89d40322006-08-29 15:30:07 +000013759 if (gap->ga_len == 0)
13760 i = -1;
13761 else
13762 {
13763 /* Check if the word is already there. Also check the length that is
13764 * being replaced "thes," -> "these" is a different suggestion from
13765 * "thes" -> "these". */
13766 stp = &SUG(*gap, 0);
13767 for (i = gap->ga_len; --i >= 0; ++stp)
13768 if (stp->st_wordlen == goodlen
13769 && stp->st_orglen == badlen
13770 && STRNCMP(stp->st_word, goodword, goodlen) == 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013771 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013772 /*
13773 * Found it. Remember the word with the lowest score.
13774 */
13775 if (stp->st_slang == NULL)
13776 stp->st_slang = slang;
13777
13778 new_sug.st_score = score;
13779 new_sug.st_altscore = altscore;
13780 new_sug.st_had_bonus = had_bonus;
13781
13782 if (stp->st_had_bonus != had_bonus)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013783 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000013784 /* Only one of the two had the soundalike score computed.
13785 * Need to do that for the other one now, otherwise the
13786 * scores can't be compared. This happens because
13787 * suggest_try_change() doesn't compute the soundalike
13788 * word to keep it fast, while some special methods set
13789 * the soundalike score to zero. */
13790 if (had_bonus)
13791 rescore_one(su, stp);
13792 else
13793 {
13794 new_sug.st_word = stp->st_word;
13795 new_sug.st_wordlen = stp->st_wordlen;
13796 new_sug.st_slang = stp->st_slang;
13797 new_sug.st_orglen = badlen;
13798 rescore_one(su, &new_sug);
13799 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013800 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013801
Bram Moolenaar89d40322006-08-29 15:30:07 +000013802 if (stp->st_score > new_sug.st_score)
13803 {
13804 stp->st_score = new_sug.st_score;
13805 stp->st_altscore = new_sug.st_altscore;
13806 stp->st_had_bonus = new_sug.st_had_bonus;
13807 }
13808 break;
Bram Moolenaar4770d092006-01-12 23:22:24 +000013809 }
Bram Moolenaar89d40322006-08-29 15:30:07 +000013810 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013811
Bram Moolenaar4770d092006-01-12 23:22:24 +000013812 if (i < 0 && ga_grow(gap, 1) == OK)
13813 {
13814 /* Add a suggestion. */
13815 stp = &SUG(*gap, gap->ga_len);
13816 stp->st_word = vim_strnsave(goodword, goodlen);
13817 if (stp->st_word != NULL)
13818 {
13819 stp->st_wordlen = goodlen;
13820 stp->st_score = score;
13821 stp->st_altscore = altscore;
13822 stp->st_had_bonus = had_bonus;
13823 stp->st_orglen = badlen;
13824 stp->st_slang = slang;
13825 ++gap->ga_len;
13826
13827 /* If we have too many suggestions now, sort the list and keep
13828 * the best suggestions. */
13829 if (gap->ga_len > SUG_MAX_COUNT(su))
13830 {
13831 if (maxsf)
13832 su->su_sfmaxscore = cleanup_suggestions(gap,
13833 su->su_sfmaxscore, SUG_CLEAN_COUNT(su));
13834 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000013835 su->su_maxscore = cleanup_suggestions(gap,
13836 su->su_maxscore, SUG_CLEAN_COUNT(su));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013837 }
13838 }
13839 }
13840}
13841
13842/*
Bram Moolenaar4770d092006-01-12 23:22:24 +000013843 * Suggestions may in fact be flagged as errors. Esp. for banned words and
13844 * for split words, such as "the the". Remove these from the list here.
13845 */
13846 static void
13847check_suggestions(su, gap)
13848 suginfo_T *su;
13849 garray_T *gap; /* either su_ga or su_sga */
13850{
13851 suggest_T *stp;
13852 int i;
13853 char_u longword[MAXWLEN + 1];
13854 int len;
13855 hlf_T attr;
13856
13857 stp = &SUG(*gap, 0);
13858 for (i = gap->ga_len - 1; i >= 0; --i)
13859 {
13860 /* Need to append what follows to check for "the the". */
13861 STRCPY(longword, stp[i].st_word);
13862 len = stp[i].st_wordlen;
13863 vim_strncpy(longword + len, su->su_badptr + stp[i].st_orglen,
13864 MAXWLEN - len);
13865 attr = HLF_COUNT;
13866 (void)spell_check(curwin, longword, &attr, NULL, FALSE);
13867 if (attr != HLF_COUNT)
13868 {
13869 /* Remove this entry. */
13870 vim_free(stp[i].st_word);
13871 --gap->ga_len;
13872 if (i < gap->ga_len)
13873 mch_memmove(stp + i, stp + i + 1,
13874 sizeof(suggest_T) * (gap->ga_len - i));
13875 }
13876 }
13877}
13878
13879
13880/*
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013881 * Add a word to be banned.
13882 */
13883 static void
13884add_banned(su, word)
13885 suginfo_T *su;
13886 char_u *word;
13887{
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000013888 char_u *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013889 hash_T hash;
13890 hashitem_T *hi;
13891
Bram Moolenaar4770d092006-01-12 23:22:24 +000013892 hash = hash_hash(word);
13893 hi = hash_lookup(&su->su_banned, word, hash);
13894 if (HASHITEM_EMPTY(hi))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013895 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000013896 s = vim_strsave(word);
13897 if (s != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013898 hash_add_item(&su->su_banned, hi, s, hash);
13899 }
13900}
13901
13902/*
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013903 * Recompute the score for all suggestions if sound-folding is possible. This
13904 * is slow, thus only done for the final results.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013905 */
13906 static void
13907rescore_suggestions(su)
13908 suginfo_T *su;
13909{
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013910 int i;
13911
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013912 if (su->su_sallang != NULL)
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013913 for (i = 0; i < su->su_ga.ga_len; ++i)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013914 rescore_one(su, &SUG(su->su_ga, i));
13915}
13916
13917/*
13918 * Recompute the score for one suggestion if sound-folding is possible.
13919 */
13920 static void
13921rescore_one(su, stp)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013922 suginfo_T *su;
13923 suggest_T *stp;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013924{
13925 slang_T *slang = stp->st_slang;
13926 char_u sal_badword[MAXWLEN];
Bram Moolenaar4effc802005-09-30 21:12:02 +000013927 char_u *p;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013928
13929 /* Only rescore suggestions that have no sal score yet and do have a
13930 * language. */
13931 if (slang != NULL && slang->sl_sal.ga_len > 0 && !stp->st_had_bonus)
13932 {
13933 if (slang == su->su_sallang)
Bram Moolenaar4effc802005-09-30 21:12:02 +000013934 p = su->su_sal_badword;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013935 else
Bram Moolenaar8b96d642005-09-05 22:05:30 +000013936 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013937 spell_soundfold(slang, su->su_fbadword, TRUE, sal_badword);
Bram Moolenaar4effc802005-09-30 21:12:02 +000013938 p = sal_badword;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013939 }
Bram Moolenaar4effc802005-09-30 21:12:02 +000013940
13941 stp->st_altscore = stp_sal_score(stp, su, slang, p);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000013942 if (stp->st_altscore == SCORE_MAXMAX)
13943 stp->st_altscore = SCORE_BIG;
13944 stp->st_score = RESCORE(stp->st_score, stp->st_altscore);
13945 stp->st_had_bonus = TRUE;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013946 }
13947}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013948
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013949static int
13950#ifdef __BORLANDC__
13951_RTLENTRYF
13952#endif
13953sug_compare __ARGS((const void *s1, const void *s2));
13954
13955/*
13956 * Function given to qsort() to sort the suggestions on st_score.
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013957 * First on "st_score", then "st_altscore" then alphabetically.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013958 */
13959 static int
13960#ifdef __BORLANDC__
13961_RTLENTRYF
13962#endif
13963sug_compare(s1, s2)
13964 const void *s1;
13965 const void *s2;
13966{
13967 suggest_T *p1 = (suggest_T *)s1;
13968 suggest_T *p2 = (suggest_T *)s2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013969 int n = p1->st_score - p2->st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013970
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013971 if (n == 0)
Bram Moolenaar6b730e12005-09-16 21:47:57 +000013972 {
13973 n = p1->st_altscore - p2->st_altscore;
13974 if (n == 0)
13975 n = STRICMP(p1->st_word, p2->st_word);
13976 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013977 return n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013978}
13979
13980/*
13981 * Cleanup the suggestions:
13982 * - Sort on score.
13983 * - Remove words that won't be displayed.
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013984 * Returns the maximum score in the list or "maxscore" unmodified.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013985 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013986 static int
13987cleanup_suggestions(gap, maxscore, keep)
13988 garray_T *gap;
13989 int maxscore;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000013990 int keep; /* nr of suggestions to keep */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013991{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013992 suggest_T *stp = &SUG(*gap, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013993 int i;
13994
13995 /* Sort the list. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013996 qsort(gap->ga_data, (size_t)gap->ga_len, sizeof(suggest_T), sug_compare);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013997
13998 /* Truncate the list to the number of suggestions that will be displayed. */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013999 if (gap->ga_len > keep)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014000 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014001 for (i = keep; i < gap->ga_len; ++i)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014002 vim_free(stp[i].st_word);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014003 gap->ga_len = keep;
14004 return stp[keep - 1].st_score;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014005 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014006 return maxscore;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014007}
14008
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014009#if defined(FEAT_EVAL) || defined(PROTO)
14010/*
14011 * Soundfold a string, for soundfold().
14012 * Result is in allocated memory, NULL for an error.
14013 */
14014 char_u *
14015eval_soundfold(word)
14016 char_u *word;
14017{
14018 langp_T *lp;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014019 char_u sound[MAXWLEN];
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014020 int lpi;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014021
Bram Moolenaar860cae12010-06-05 23:22:07 +020014022 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014023 /* Use the sound-folding of the first language that supports it. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020014024 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014025 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020014026 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014027 if (lp->lp_slang->sl_sal.ga_len > 0)
14028 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014029 /* soundfold the word */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014030 spell_soundfold(lp->lp_slang, word, FALSE, sound);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014031 return vim_strsave(sound);
14032 }
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014033 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014034
14035 /* No language with sound folding, return word as-is. */
14036 return vim_strsave(word);
14037}
14038#endif
14039
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014040/*
14041 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
Bram Moolenaard12a1322005-08-21 22:08:24 +000014042 *
14043 * There are many ways to turn a word into a sound-a-like representation. The
14044 * oldest is Soundex (1918!). A nice overview can be found in "Approximate
14045 * swedish name matching - survey and test of different algorithms" by Klas
14046 * Erikson.
14047 *
14048 * We support two methods:
14049 * 1. SOFOFROM/SOFOTO do a simple character mapping.
14050 * 2. SAL items define a more advanced sound-folding (and much slower).
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014051 */
14052 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014053spell_soundfold(slang, inword, folded, res)
14054 slang_T *slang;
14055 char_u *inword;
14056 int folded; /* "inword" is already case-folded */
14057 char_u *res;
14058{
14059 char_u fword[MAXWLEN];
14060 char_u *word;
14061
14062 if (slang->sl_sofo)
14063 /* SOFOFROM and SOFOTO used */
14064 spell_soundfold_sofo(slang, inword, res);
14065 else
14066 {
14067 /* SAL items used. Requires the word to be case-folded. */
14068 if (folded)
14069 word = inword;
14070 else
14071 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014072 (void)spell_casefold(inword, (int)STRLEN(inword), fword, MAXWLEN);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014073 word = fword;
14074 }
14075
14076#ifdef FEAT_MBYTE
14077 if (has_mbyte)
14078 spell_soundfold_wsal(slang, word, res);
14079 else
14080#endif
14081 spell_soundfold_sal(slang, word, res);
14082 }
14083}
14084
14085/*
14086 * Perform sound folding of "inword" into "res" according to SOFOFROM and
14087 * SOFOTO lines.
14088 */
14089 static void
14090spell_soundfold_sofo(slang, inword, res)
14091 slang_T *slang;
14092 char_u *inword;
14093 char_u *res;
14094{
14095 char_u *s;
14096 int ri = 0;
14097 int c;
14098
14099#ifdef FEAT_MBYTE
14100 if (has_mbyte)
14101 {
14102 int prevc = 0;
14103 int *ip;
14104
14105 /* The sl_sal_first[] table contains the translation for chars up to
14106 * 255, sl_sal the rest. */
14107 for (s = inword; *s != NUL; )
14108 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014109 c = mb_cptr2char_adv(&s);
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014110 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14111 c = ' ';
14112 else if (c < 256)
14113 c = slang->sl_sal_first[c];
14114 else
14115 {
14116 ip = ((int **)slang->sl_sal.ga_data)[c & 0xff];
14117 if (ip == NULL) /* empty list, can't match */
14118 c = NUL;
14119 else
14120 for (;;) /* find "c" in the list */
14121 {
14122 if (*ip == 0) /* not found */
14123 {
14124 c = NUL;
14125 break;
14126 }
14127 if (*ip == c) /* match! */
14128 {
14129 c = ip[1];
14130 break;
14131 }
14132 ip += 2;
14133 }
14134 }
14135
14136 if (c != NUL && c != prevc)
14137 {
14138 ri += mb_char2bytes(c, res + ri);
14139 if (ri + MB_MAXBYTES > MAXWLEN)
14140 break;
14141 prevc = c;
14142 }
14143 }
14144 }
14145 else
14146#endif
14147 {
14148 /* The sl_sal_first[] table contains the translation. */
14149 for (s = inword; (c = *s) != NUL; ++s)
14150 {
14151 if (vim_iswhite(c))
14152 c = ' ';
14153 else
14154 c = slang->sl_sal_first[c];
14155 if (c != NUL && (ri == 0 || res[ri - 1] != c))
14156 res[ri++] = c;
14157 }
14158 }
14159
14160 res[ri] = NUL;
14161}
14162
14163 static void
14164spell_soundfold_sal(slang, inword, res)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014165 slang_T *slang;
14166 char_u *inword;
14167 char_u *res;
14168{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014169 salitem_T *smp;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014170 char_u word[MAXWLEN];
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014171 char_u *s = inword;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014172 char_u *t;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014173 char_u *pf;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014174 int i, j, z;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014175 int reslen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014176 int n, k = 0;
14177 int z0;
14178 int k0;
14179 int n0;
14180 int c;
14181 int pri;
14182 int p0 = -333;
14183 int c0;
14184
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014185 /* Remove accents, if wanted. We actually remove all non-word characters.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014186 * But keep white space. We need a copy, the word may be changed here. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014187 if (slang->sl_rem_accents)
14188 {
14189 t = word;
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014190 while (*s != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014191 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014192 if (vim_iswhite(*s))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014193 {
14194 *t++ = ' ';
14195 s = skipwhite(s);
14196 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014197 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014198 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014199 if (spell_iswordp_nmw(s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014200 *t++ = *s;
14201 ++s;
14202 }
14203 }
14204 *t = NUL;
14205 }
14206 else
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014207 STRCPY(word, s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014208
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014209 smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014210
14211 /*
14212 * This comes from Aspell phonet.cpp. Converted from C++ to C.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014213 * Changed to keep spaces.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014214 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014215 i = reslen = z = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014216 while ((c = word[i]) != NUL)
14217 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014218 /* Start with the first rule that has the character in the word. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014219 n = slang->sl_sal_first[c];
14220 z0 = 0;
14221
14222 if (n >= 0)
14223 {
14224 /* check all rules for the same letter */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014225 for (; (s = smp[n].sm_lead)[0] == c; ++n)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014226 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014227 /* Quickly skip entries that don't match the word. Most
14228 * entries are less then three chars, optimize for that. */
14229 k = smp[n].sm_leadlen;
14230 if (k > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014231 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014232 if (word[i + 1] != s[1])
14233 continue;
14234 if (k > 2)
14235 {
14236 for (j = 2; j < k; ++j)
14237 if (word[i + j] != s[j])
14238 break;
14239 if (j < k)
14240 continue;
14241 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014242 }
14243
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014244 if ((pf = smp[n].sm_oneof) != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014245 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014246 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014247 while (*pf != NUL && *pf != word[i + k])
14248 ++pf;
14249 if (*pf == NUL)
14250 continue;
14251 ++k;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014252 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014253 s = smp[n].sm_rules;
14254 pri = 5; /* default priority */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014255
14256 p0 = *s;
14257 k0 = k;
14258 while (*s == '-' && k > 1)
14259 {
14260 k--;
14261 s++;
14262 }
14263 if (*s == '<')
14264 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014265 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014266 {
14267 /* determine priority */
14268 pri = *s - '0';
14269 s++;
14270 }
14271 if (*s == '^' && *(s + 1) == '^')
14272 s++;
14273
14274 if (*s == NUL
14275 || (*s == '^'
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014276 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +020014277 || spell_iswordp(word + i - 1, curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014278 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +020014279 || (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014280 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +020014281 && spell_iswordp(word + i - 1, curwin)
14282 && (!spell_iswordp(word + i + k0, curwin))))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014283 {
14284 /* search for followup rules, if: */
14285 /* followup and k > 1 and NO '-' in searchstring */
14286 c0 = word[i + k - 1];
14287 n0 = slang->sl_sal_first[c0];
14288
14289 if (slang->sl_followup && k > 1 && n0 >= 0
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014290 && p0 != '-' && word[i + k] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014291 {
14292 /* test follow-up rule for "word[i + k]" */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014293 for ( ; (s = smp[n0].sm_lead)[0] == c0; ++n0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014294 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014295 /* Quickly skip entries that don't match the word.
14296 * */
14297 k0 = smp[n0].sm_leadlen;
14298 if (k0 > 1)
14299 {
14300 if (word[i + k] != s[1])
14301 continue;
14302 if (k0 > 2)
14303 {
14304 pf = word + i + k + 1;
14305 for (j = 2; j < k0; ++j)
14306 if (*pf++ != s[j])
14307 break;
14308 if (j < k0)
14309 continue;
14310 }
14311 }
14312 k0 += k - 1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014313
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014314 if ((pf = smp[n0].sm_oneof) != NULL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014315 {
14316 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014317 * "sm_oneof". */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014318 while (*pf != NUL && *pf != word[i + k0])
14319 ++pf;
14320 if (*pf == NUL)
14321 continue;
14322 ++k0;
14323 }
14324
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014325 p0 = 5;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014326 s = smp[n0].sm_rules;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014327 while (*s == '-')
14328 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014329 /* "k0" gets NOT reduced because
14330 * "if (k0 == k)" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014331 s++;
14332 }
14333 if (*s == '<')
14334 s++;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014335 if (VIM_ISDIGIT(*s))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014336 {
14337 p0 = *s - '0';
14338 s++;
14339 }
14340
14341 if (*s == NUL
14342 /* *s == '^' cuts */
14343 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014344 && !spell_iswordp(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +020014345 curwin)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014346 {
14347 if (k0 == k)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014348 /* this is just a piece of the string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014349 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014350
14351 if (p0 < pri)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014352 /* priority too low */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014353 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014354 /* rule fits; stop search */
14355 break;
14356 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014357 }
14358
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014359 if (p0 >= pri && smp[n0].sm_lead[0] == c0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014360 continue;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014361 }
14362
14363 /* replace string */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014364 s = smp[n].sm_to;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014365 if (s == NULL)
14366 s = (char_u *)"";
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014367 pf = smp[n].sm_rules;
14368 p0 = (vim_strchr(pf, '<') != NULL) ? 1 : 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014369 if (p0 == 1 && z == 0)
14370 {
14371 /* rule with '<' is used */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014372 if (reslen > 0 && *s != NUL && (res[reslen - 1] == c
14373 || res[reslen - 1] == *s))
14374 reslen--;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014375 z0 = 1;
14376 z = 1;
14377 k0 = 0;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014378 while (*s != NUL && word[i + k0] != NUL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014379 {
14380 word[i + k0] = *s;
14381 k0++;
14382 s++;
14383 }
14384 if (k > k0)
Bram Moolenaara7241f52008-06-24 20:39:31 +000014385 STRMOVE(word + i + k0, word + i + k);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014386
14387 /* new "actual letter" */
14388 c = word[i];
14389 }
14390 else
14391 {
14392 /* no '<' rule used */
14393 i += k - 1;
14394 z = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014395 while (*s != NUL && s[1] != NUL && reslen < MAXWLEN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014396 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014397 if (reslen == 0 || res[reslen - 1] != *s)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014398 res[reslen++] = *s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014399 s++;
14400 }
14401 /* new "actual letter" */
14402 c = *s;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014403 if (strstr((char *)pf, "^^") != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014404 {
14405 if (c != NUL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014406 res[reslen++] = c;
Bram Moolenaara7241f52008-06-24 20:39:31 +000014407 STRMOVE(word, word + i + 1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014408 i = 0;
14409 z0 = 1;
14410 }
14411 }
14412 break;
14413 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014414 }
14415 }
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014416 else if (vim_iswhite(c))
14417 {
14418 c = ' ';
14419 k = 1;
14420 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014421
14422 if (z0 == 0)
14423 {
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014424 if (k && !p0 && reslen < MAXWLEN && c != NUL
14425 && (!slang->sl_collapse || reslen == 0
14426 || res[reslen - 1] != c))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014427 /* condense only double letters */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014428 res[reslen++] = c;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014429
14430 i++;
14431 z = 0;
14432 k = 0;
14433 }
14434 }
14435
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014436 res[reslen] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014437}
14438
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014439#ifdef FEAT_MBYTE
14440/*
14441 * Turn "inword" into its sound-a-like equivalent in "res[MAXWLEN]".
14442 * Multi-byte version of spell_soundfold().
14443 */
14444 static void
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014445spell_soundfold_wsal(slang, inword, res)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014446 slang_T *slang;
14447 char_u *inword;
14448 char_u *res;
14449{
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014450 salitem_T *smp = (salitem_T *)slang->sl_sal.ga_data;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014451 int word[MAXWLEN];
14452 int wres[MAXWLEN];
14453 int l;
14454 char_u *s;
14455 int *ws;
14456 char_u *t;
14457 int *pf;
14458 int i, j, z;
14459 int reslen;
14460 int n, k = 0;
14461 int z0;
14462 int k0;
14463 int n0;
14464 int c;
14465 int pri;
14466 int p0 = -333;
14467 int c0;
14468 int did_white = FALSE;
14469
14470 /*
14471 * Convert the multi-byte string to a wide-character string.
14472 * Remove accents, if wanted. We actually remove all non-word characters.
14473 * But keep white space.
14474 */
14475 n = 0;
14476 for (s = inword; *s != NUL; )
14477 {
14478 t = s;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014479 c = mb_cptr2char_adv(&s);
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014480 if (slang->sl_rem_accents)
14481 {
14482 if (enc_utf8 ? utf_class(c) == 0 : vim_iswhite(c))
14483 {
14484 if (did_white)
14485 continue;
14486 c = ' ';
14487 did_white = TRUE;
14488 }
14489 else
14490 {
14491 did_white = FALSE;
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014492 if (!spell_iswordp_nmw(t))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014493 continue;
14494 }
14495 }
14496 word[n++] = c;
14497 }
14498 word[n] = NUL;
14499
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014500 /*
14501 * This comes from Aspell phonet.cpp.
14502 * Converted from C++ to C. Added support for multi-byte chars.
14503 * Changed to keep spaces.
14504 */
14505 i = reslen = z = 0;
14506 while ((c = word[i]) != NUL)
14507 {
14508 /* Start with the first rule that has the character in the word. */
14509 n = slang->sl_sal_first[c & 0xff];
14510 z0 = 0;
14511
14512 if (n >= 0)
14513 {
Bram Moolenaar95e85792010-08-01 15:37:02 +020014514 /* Check all rules for the same index byte.
14515 * If c is 0x300 need extra check for the end of the array, as
14516 * (c & 0xff) is NUL. */
14517 for (; ((ws = smp[n].sm_lead_w)[0] & 0xff) == (c & 0xff)
14518 && ws[0] != NUL; ++n)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014519 {
14520 /* Quickly skip entries that don't match the word. Most
14521 * entries are less then three chars, optimize for that. */
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014522 if (c != ws[0])
14523 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014524 k = smp[n].sm_leadlen;
14525 if (k > 1)
14526 {
14527 if (word[i + 1] != ws[1])
14528 continue;
14529 if (k > 2)
14530 {
14531 for (j = 2; j < k; ++j)
14532 if (word[i + j] != ws[j])
14533 break;
14534 if (j < k)
14535 continue;
14536 }
14537 }
14538
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014539 if ((pf = smp[n].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014540 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014541 /* Check for match with one of the chars in "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014542 while (*pf != NUL && *pf != word[i + k])
14543 ++pf;
14544 if (*pf == NUL)
14545 continue;
14546 ++k;
14547 }
14548 s = smp[n].sm_rules;
14549 pri = 5; /* default priority */
14550
14551 p0 = *s;
14552 k0 = k;
14553 while (*s == '-' && k > 1)
14554 {
14555 k--;
14556 s++;
14557 }
14558 if (*s == '<')
14559 s++;
14560 if (VIM_ISDIGIT(*s))
14561 {
14562 /* determine priority */
14563 pri = *s - '0';
14564 s++;
14565 }
14566 if (*s == '^' && *(s + 1) == '^')
14567 s++;
14568
14569 if (*s == NUL
14570 || (*s == '^'
14571 && (i == 0 || !(word[i - 1] == ' '
Bram Moolenaar860cae12010-06-05 23:22:07 +020014572 || spell_iswordp_w(word + i - 1, curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014573 && (*(s + 1) != '$'
Bram Moolenaar860cae12010-06-05 23:22:07 +020014574 || (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014575 || (*s == '$' && i > 0
Bram Moolenaar860cae12010-06-05 23:22:07 +020014576 && spell_iswordp_w(word + i - 1, curwin)
14577 && (!spell_iswordp_w(word + i + k0, curwin))))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014578 {
14579 /* search for followup rules, if: */
14580 /* followup and k > 1 and NO '-' in searchstring */
14581 c0 = word[i + k - 1];
14582 n0 = slang->sl_sal_first[c0 & 0xff];
14583
14584 if (slang->sl_followup && k > 1 && n0 >= 0
14585 && p0 != '-' && word[i + k] != NUL)
14586 {
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014587 /* Test follow-up rule for "word[i + k]"; loop over
14588 * all entries with the same index byte. */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014589 for ( ; ((ws = smp[n0].sm_lead_w)[0] & 0xff)
14590 == (c0 & 0xff); ++n0)
14591 {
14592 /* Quickly skip entries that don't match the word.
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014593 */
14594 if (c0 != ws[0])
14595 continue;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014596 k0 = smp[n0].sm_leadlen;
14597 if (k0 > 1)
14598 {
14599 if (word[i + k] != ws[1])
14600 continue;
14601 if (k0 > 2)
14602 {
14603 pf = word + i + k + 1;
14604 for (j = 2; j < k0; ++j)
14605 if (*pf++ != ws[j])
14606 break;
14607 if (j < k0)
14608 continue;
14609 }
14610 }
14611 k0 += k - 1;
14612
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014613 if ((pf = smp[n0].sm_oneof_w) != NULL)
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014614 {
14615 /* Check for match with one of the chars in
Bram Moolenaar42eeac32005-06-29 22:40:58 +000014616 * "sm_oneof". */
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014617 while (*pf != NUL && *pf != word[i + k0])
14618 ++pf;
14619 if (*pf == NUL)
14620 continue;
14621 ++k0;
14622 }
14623
14624 p0 = 5;
14625 s = smp[n0].sm_rules;
14626 while (*s == '-')
14627 {
14628 /* "k0" gets NOT reduced because
14629 * "if (k0 == k)" */
14630 s++;
14631 }
14632 if (*s == '<')
14633 s++;
14634 if (VIM_ISDIGIT(*s))
14635 {
14636 p0 = *s - '0';
14637 s++;
14638 }
14639
14640 if (*s == NUL
14641 /* *s == '^' cuts */
14642 || (*s == '$'
Bram Moolenaar9c96f592005-06-30 21:52:39 +000014643 && !spell_iswordp_w(word + i + k0,
Bram Moolenaar860cae12010-06-05 23:22:07 +020014644 curwin)))
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014645 {
14646 if (k0 == k)
14647 /* this is just a piece of the string */
14648 continue;
14649
14650 if (p0 < pri)
14651 /* priority too low */
14652 continue;
14653 /* rule fits; stop search */
14654 break;
14655 }
14656 }
14657
14658 if (p0 >= pri && (smp[n0].sm_lead_w[0] & 0xff)
14659 == (c0 & 0xff))
14660 continue;
14661 }
14662
14663 /* replace string */
14664 ws = smp[n].sm_to_w;
14665 s = smp[n].sm_rules;
14666 p0 = (vim_strchr(s, '<') != NULL) ? 1 : 0;
14667 if (p0 == 1 && z == 0)
14668 {
14669 /* rule with '<' is used */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014670 if (reslen > 0 && ws != NULL && *ws != NUL
14671 && (wres[reslen - 1] == c
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014672 || wres[reslen - 1] == *ws))
14673 reslen--;
14674 z0 = 1;
14675 z = 1;
14676 k0 = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014677 if (ws != NULL)
14678 while (*ws != NUL && word[i + k0] != NUL)
14679 {
14680 word[i + k0] = *ws;
14681 k0++;
14682 ws++;
14683 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014684 if (k > k0)
14685 mch_memmove(word + i + k0, word + i + k,
14686 sizeof(int) * (STRLEN(word + i + k) + 1));
14687
14688 /* new "actual letter" */
14689 c = word[i];
14690 }
14691 else
14692 {
14693 /* no '<' rule used */
14694 i += k - 1;
14695 z = 0;
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014696 if (ws != NULL)
14697 while (*ws != NUL && ws[1] != NUL
14698 && reslen < MAXWLEN)
14699 {
14700 if (reslen == 0 || wres[reslen - 1] != *ws)
14701 wres[reslen++] = *ws;
14702 ws++;
14703 }
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014704 /* new "actual letter" */
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000014705 if (ws == NULL)
14706 c = NUL;
14707 else
14708 c = *ws;
Bram Moolenaara1ba8112005-06-28 23:23:32 +000014709 if (strstr((char *)s, "^^") != NULL)
14710 {
14711 if (c != NUL)
14712 wres[reslen++] = c;
14713 mch_memmove(word, word + i + 1,
14714 sizeof(int) * (STRLEN(word + i + 1) + 1));
14715 i = 0;
14716 z0 = 1;
14717 }
14718 }
14719 break;
14720 }
14721 }
14722 }
14723 else if (vim_iswhite(c))
14724 {
14725 c = ' ';
14726 k = 1;
14727 }
14728
14729 if (z0 == 0)
14730 {
14731 if (k && !p0 && reslen < MAXWLEN && c != NUL
14732 && (!slang->sl_collapse || reslen == 0
14733 || wres[reslen - 1] != c))
14734 /* condense only double letters */
14735 wres[reslen++] = c;
14736
14737 i++;
14738 z = 0;
14739 k = 0;
14740 }
14741 }
14742
14743 /* Convert wide characters in "wres" to a multi-byte string in "res". */
14744 l = 0;
14745 for (n = 0; n < reslen; ++n)
14746 {
14747 l += mb_char2bytes(wres[n], res + l);
14748 if (l + MB_MAXBYTES > MAXWLEN)
14749 break;
14750 }
14751 res[l] = NUL;
14752}
14753#endif
14754
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014755/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014756 * Compute a score for two sound-a-like words.
14757 * This permits up to two inserts/deletes/swaps/etc. to keep things fast.
14758 * Instead of a generic loop we write out the code. That keeps it fast by
14759 * avoiding checks that will not be possible.
14760 */
14761 static int
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014762soundalike_score(goodstart, badstart)
14763 char_u *goodstart; /* sound-folded good word */
14764 char_u *badstart; /* sound-folded bad word */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014765{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014766 char_u *goodsound = goodstart;
14767 char_u *badsound = badstart;
14768 int goodlen;
14769 int badlen;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014770 int n;
14771 char_u *pl, *ps;
14772 char_u *pl2, *ps2;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014773 int score = 0;
14774
Bram Moolenaar121d95f2010-08-01 15:11:43 +020014775 /* Adding/inserting "*" at the start (word starts with vowel) shouldn't be
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014776 * counted so much, vowels halfway the word aren't counted at all. */
14777 if ((*badsound == '*' || *goodsound == '*') && *badsound != *goodsound)
14778 {
Bram Moolenaar121d95f2010-08-01 15:11:43 +020014779 if ((badsound[0] == NUL && goodsound[1] == NUL)
14780 || (goodsound[0] == NUL && badsound[1] == NUL))
14781 /* changing word with vowel to word without a sound */
14782 return SCORE_DEL;
14783 if (badsound[0] == NUL || goodsound[0] == NUL)
14784 /* more than two changes */
14785 return SCORE_MAXMAX;
14786
Bram Moolenaar4770d092006-01-12 23:22:24 +000014787 if (badsound[1] == goodsound[1]
14788 || (badsound[1] != NUL
14789 && goodsound[1] != NUL
14790 && badsound[2] == goodsound[2]))
14791 {
14792 /* handle like a substitute */
14793 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014794 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000014795 {
14796 score = 2 * SCORE_DEL / 3;
14797 if (*badsound == '*')
14798 ++badsound;
14799 else
14800 ++goodsound;
14801 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014802 }
14803
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014804 goodlen = (int)STRLEN(goodsound);
14805 badlen = (int)STRLEN(badsound);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014806
Bram Moolenaarf711faf2007-05-10 16:48:19 +000014807 /* Return quickly if the lengths are too different to be fixed by two
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014808 * changes. */
14809 n = goodlen - badlen;
14810 if (n < -2 || n > 2)
14811 return SCORE_MAXMAX;
14812
14813 if (n > 0)
14814 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014815 pl = goodsound; /* goodsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014816 ps = badsound;
14817 }
14818 else
14819 {
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014820 pl = badsound; /* badsound is longest */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014821 ps = goodsound;
14822 }
14823
14824 /* Skip over the identical part. */
14825 while (*pl == *ps && *pl != NUL)
14826 {
14827 ++pl;
14828 ++ps;
14829 }
14830
14831 switch (n)
14832 {
14833 case -2:
14834 case 2:
14835 /*
14836 * Must delete two characters from "pl".
14837 */
14838 ++pl; /* first delete */
14839 while (*pl == *ps)
14840 {
14841 ++pl;
14842 ++ps;
14843 }
14844 /* strings must be equal after second delete */
14845 if (STRCMP(pl + 1, ps) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014846 return score + SCORE_DEL * 2;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014847
14848 /* Failed to compare. */
14849 break;
14850
14851 case -1:
14852 case 1:
14853 /*
14854 * Minimal one delete from "pl" required.
14855 */
14856
14857 /* 1: delete */
14858 pl2 = pl + 1;
14859 ps2 = ps;
14860 while (*pl2 == *ps2)
14861 {
14862 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014863 return score + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014864 ++pl2;
14865 ++ps2;
14866 }
14867
14868 /* 2: delete then swap, then rest must be equal */
14869 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14870 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014871 return score + SCORE_DEL + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014872
14873 /* 3: delete then substitute, then the rest must be equal */
14874 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014875 return score + SCORE_DEL + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014876
14877 /* 4: first swap then delete */
14878 if (pl[0] == ps[1] && pl[1] == ps[0])
14879 {
14880 pl2 = pl + 2; /* swap, skip two chars */
14881 ps2 = ps + 2;
14882 while (*pl2 == *ps2)
14883 {
14884 ++pl2;
14885 ++ps2;
14886 }
14887 /* delete a char and then strings must be equal */
14888 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014889 return score + SCORE_SWAP + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014890 }
14891
14892 /* 5: first substitute then delete */
14893 pl2 = pl + 1; /* substitute, skip one char */
14894 ps2 = ps + 1;
14895 while (*pl2 == *ps2)
14896 {
14897 ++pl2;
14898 ++ps2;
14899 }
14900 /* delete a char and then strings must be equal */
14901 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014902 return score + SCORE_SUBST + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014903
14904 /* Failed to compare. */
14905 break;
14906
14907 case 0:
14908 /*
Bram Moolenaar6ae167a2009-02-11 16:58:49 +000014909 * Lengths are equal, thus changes must result in same length: An
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014910 * insert is only possible in combination with a delete.
14911 * 1: check if for identical strings
14912 */
14913 if (*pl == NUL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014914 return score;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014915
14916 /* 2: swap */
14917 if (pl[0] == ps[1] && pl[1] == ps[0])
14918 {
14919 pl2 = pl + 2; /* swap, skip two chars */
14920 ps2 = ps + 2;
14921 while (*pl2 == *ps2)
14922 {
14923 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014924 return score + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014925 ++pl2;
14926 ++ps2;
14927 }
14928 /* 3: swap and swap again */
14929 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14930 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014931 return score + SCORE_SWAP + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014932
14933 /* 4: swap and substitute */
14934 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014935 return score + SCORE_SWAP + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014936 }
14937
14938 /* 5: substitute */
14939 pl2 = pl + 1;
14940 ps2 = ps + 1;
14941 while (*pl2 == *ps2)
14942 {
14943 if (*pl2 == NUL) /* reached the end */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014944 return score + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014945 ++pl2;
14946 ++ps2;
14947 }
14948
14949 /* 6: substitute and swap */
14950 if (pl2[0] == ps2[1] && pl2[1] == ps2[0]
14951 && STRCMP(pl2 + 2, ps2 + 2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014952 return score + SCORE_SUBST + SCORE_SWAP;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014953
14954 /* 7: substitute and substitute */
14955 if (STRCMP(pl2 + 1, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014956 return score + SCORE_SUBST + SCORE_SUBST;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014957
14958 /* 8: insert then delete */
14959 pl2 = pl;
14960 ps2 = ps + 1;
14961 while (*pl2 == *ps2)
14962 {
14963 ++pl2;
14964 ++ps2;
14965 }
14966 if (STRCMP(pl2 + 1, ps2) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014967 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014968
14969 /* 9: delete then insert */
14970 pl2 = pl + 1;
14971 ps2 = ps;
14972 while (*pl2 == *ps2)
14973 {
14974 ++pl2;
14975 ++ps2;
14976 }
14977 if (STRCMP(pl2, ps2 + 1) == 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000014978 return score + SCORE_INS + SCORE_DEL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014979
14980 /* Failed to compare. */
14981 break;
14982 }
14983
14984 return SCORE_MAXMAX;
14985}
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014986
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014987/*
14988 * Compute the "edit distance" to turn "badword" into "goodword". The less
Bram Moolenaard857f0e2005-06-21 22:37:39 +000014989 * deletes/inserts/substitutes/swaps are required the lower the score.
Bram Moolenaar9f30f502005-06-14 22:01:04 +000014990 *
Bram Moolenaard12a1322005-08-21 22:08:24 +000014991 * The algorithm is described by Du and Chang, 1992.
14992 * The implementation of the algorithm comes from Aspell editdist.cpp,
14993 * edit_distance(). It has been converted from C++ to C and modified to
14994 * support multi-byte characters.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014995 */
14996 static int
Bram Moolenaar4770d092006-01-12 23:22:24 +000014997spell_edit_score(slang, badword, goodword)
14998 slang_T *slang;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014999 char_u *badword;
15000 char_u *goodword;
15001{
15002 int *cnt;
Bram Moolenaarf711faf2007-05-10 16:48:19 +000015003 int badlen, goodlen; /* lengths including NUL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015004 int j, i;
15005 int t;
15006 int bc, gc;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015007 int pbc, pgc;
15008#ifdef FEAT_MBYTE
15009 char_u *p;
15010 int wbadword[MAXWLEN];
15011 int wgoodword[MAXWLEN];
15012
15013 if (has_mbyte)
15014 {
15015 /* Get the characters from the multi-byte strings and put them in an
15016 * int array for easy access. */
15017 for (p = badword, badlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015018 wbadword[badlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000015019 wbadword[badlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015020 for (p = goodword, goodlen = 0; *p != NUL; )
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000015021 wgoodword[goodlen++] = mb_cptr2char_adv(&p);
Bram Moolenaar97409f12005-07-08 22:17:29 +000015022 wgoodword[goodlen++] = 0;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015023 }
15024 else
15025#endif
15026 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015027 badlen = (int)STRLEN(badword) + 1;
15028 goodlen = (int)STRLEN(goodword) + 1;
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015029 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015030
15031 /* We use "cnt" as an array: CNT(badword_idx, goodword_idx). */
15032#define CNT(a, b) cnt[(a) + (b) * (badlen + 1)]
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015033 cnt = (int *)lalloc((long_u)(sizeof(int) * (badlen + 1) * (goodlen + 1)),
15034 TRUE);
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015035 if (cnt == NULL)
15036 return 0; /* out of memory */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015037
15038 CNT(0, 0) = 0;
15039 for (j = 1; j <= goodlen; ++j)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015040 CNT(0, j) = CNT(0, j - 1) + SCORE_INS;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015041
15042 for (i = 1; i <= badlen; ++i)
15043 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000015044 CNT(i, 0) = CNT(i - 1, 0) + SCORE_DEL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015045 for (j = 1; j <= goodlen; ++j)
15046 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015047#ifdef FEAT_MBYTE
15048 if (has_mbyte)
15049 {
15050 bc = wbadword[i - 1];
15051 gc = wgoodword[j - 1];
15052 }
15053 else
15054#endif
15055 {
15056 bc = badword[i - 1];
15057 gc = goodword[j - 1];
15058 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015059 if (bc == gc)
15060 CNT(i, j) = CNT(i - 1, j - 1);
15061 else
15062 {
15063 /* Use a better score when there is only a case difference. */
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015064 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015065 CNT(i, j) = SCORE_ICASE + CNT(i - 1, j - 1);
15066 else
Bram Moolenaar4770d092006-01-12 23:22:24 +000015067 {
15068 /* For a similar character use SCORE_SIMILAR. */
15069 if (slang != NULL
15070 && slang->sl_has_map
15071 && similar_chars(slang, gc, bc))
15072 CNT(i, j) = SCORE_SIMILAR + CNT(i - 1, j - 1);
15073 else
15074 CNT(i, j) = SCORE_SUBST + CNT(i - 1, j - 1);
15075 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015076
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015077 if (i > 1 && j > 1)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015078 {
Bram Moolenaar9f30f502005-06-14 22:01:04 +000015079#ifdef FEAT_MBYTE
15080 if (has_mbyte)
15081 {
15082 pbc = wbadword[i - 2];
15083 pgc = wgoodword[j - 2];
15084 }
15085 else
15086#endif
15087 {
15088 pbc = badword[i - 2];
15089 pgc = goodword[j - 2];
15090 }
15091 if (bc == pgc && pbc == gc)
15092 {
15093 t = SCORE_SWAP + CNT(i - 2, j - 2);
15094 if (t < CNT(i, j))
15095 CNT(i, j) = t;
15096 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015097 }
15098 t = SCORE_DEL + CNT(i - 1, j);
15099 if (t < CNT(i, j))
15100 CNT(i, j) = t;
15101 t = SCORE_INS + CNT(i, j - 1);
15102 if (t < CNT(i, j))
15103 CNT(i, j) = t;
15104 }
15105 }
15106 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000015107
15108 i = CNT(badlen - 1, goodlen - 1);
15109 vim_free(cnt);
15110 return i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015111}
Bram Moolenaarcfc6c432005-06-06 21:50:35 +000015112
Bram Moolenaar4770d092006-01-12 23:22:24 +000015113typedef struct
15114{
15115 int badi;
15116 int goodi;
15117 int score;
15118} limitscore_T;
15119
15120/*
15121 * Like spell_edit_score(), but with a limit on the score to make it faster.
15122 * May return SCORE_MAXMAX when the score is higher than "limit".
15123 *
15124 * This uses a stack for the edits still to be tried.
15125 * The idea comes from Aspell leditdist.cpp. Rewritten in C and added support
15126 * for multi-byte characters.
15127 */
15128 static int
15129spell_edit_score_limit(slang, badword, goodword, limit)
15130 slang_T *slang;
15131 char_u *badword;
15132 char_u *goodword;
15133 int limit;
15134{
15135 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15136 int stackidx;
15137 int bi, gi;
15138 int bi2, gi2;
15139 int bc, gc;
15140 int score;
15141 int score_off;
15142 int minscore;
15143 int round;
15144
15145#ifdef FEAT_MBYTE
15146 /* Multi-byte characters require a bit more work, use a different function
15147 * to avoid testing "has_mbyte" quite often. */
15148 if (has_mbyte)
15149 return spell_edit_score_limit_w(slang, badword, goodword, limit);
15150#endif
15151
15152 /*
15153 * The idea is to go from start to end over the words. So long as
15154 * characters are equal just continue, this always gives the lowest score.
15155 * When there is a difference try several alternatives. Each alternative
15156 * increases "score" for the edit distance. Some of the alternatives are
15157 * pushed unto a stack and tried later, some are tried right away. At the
15158 * end of the word the score for one alternative is known. The lowest
15159 * possible score is stored in "minscore".
15160 */
15161 stackidx = 0;
15162 bi = 0;
15163 gi = 0;
15164 score = 0;
15165 minscore = limit + 1;
15166
15167 for (;;)
15168 {
15169 /* Skip over an equal part, score remains the same. */
15170 for (;;)
15171 {
15172 bc = badword[bi];
15173 gc = goodword[gi];
15174 if (bc != gc) /* stop at a char that's different */
15175 break;
15176 if (bc == NUL) /* both words end */
15177 {
15178 if (score < minscore)
15179 minscore = score;
15180 goto pop; /* do next alternative */
15181 }
15182 ++bi;
15183 ++gi;
15184 }
15185
15186 if (gc == NUL) /* goodword ends, delete badword chars */
15187 {
15188 do
15189 {
15190 if ((score += SCORE_DEL) >= minscore)
15191 goto pop; /* do next alternative */
15192 } while (badword[++bi] != NUL);
15193 minscore = score;
15194 }
15195 else if (bc == NUL) /* badword ends, insert badword chars */
15196 {
15197 do
15198 {
15199 if ((score += SCORE_INS) >= minscore)
15200 goto pop; /* do next alternative */
15201 } while (goodword[++gi] != NUL);
15202 minscore = score;
15203 }
15204 else /* both words continue */
15205 {
15206 /* If not close to the limit, perform a change. Only try changes
15207 * that may lead to a lower score than "minscore".
15208 * round 0: try deleting a char from badword
15209 * round 1: try inserting a char in badword */
15210 for (round = 0; round <= 1; ++round)
15211 {
15212 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15213 if (score_off < minscore)
15214 {
15215 if (score_off + SCORE_EDIT_MIN >= minscore)
15216 {
15217 /* Near the limit, rest of the words must match. We
15218 * can check that right now, no need to push an item
15219 * onto the stack. */
15220 bi2 = bi + 1 - round;
15221 gi2 = gi + round;
15222 while (goodword[gi2] == badword[bi2])
15223 {
15224 if (goodword[gi2] == NUL)
15225 {
15226 minscore = score_off;
15227 break;
15228 }
15229 ++bi2;
15230 ++gi2;
15231 }
15232 }
15233 else
15234 {
15235 /* try deleting/inserting a character later */
15236 stack[stackidx].badi = bi + 1 - round;
15237 stack[stackidx].goodi = gi + round;
15238 stack[stackidx].score = score_off;
15239 ++stackidx;
15240 }
15241 }
15242 }
15243
15244 if (score + SCORE_SWAP < minscore)
15245 {
15246 /* If swapping two characters makes a match then the
15247 * substitution is more expensive, thus there is no need to
15248 * try both. */
15249 if (gc == badword[bi + 1] && bc == goodword[gi + 1])
15250 {
15251 /* Swap two characters, that is: skip them. */
15252 gi += 2;
15253 bi += 2;
15254 score += SCORE_SWAP;
15255 continue;
15256 }
15257 }
15258
15259 /* Substitute one character for another which is the same
15260 * thing as deleting a character from both goodword and badword.
15261 * Use a better score when there is only a case difference. */
15262 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15263 score += SCORE_ICASE;
15264 else
15265 {
15266 /* For a similar character use SCORE_SIMILAR. */
15267 if (slang != NULL
15268 && slang->sl_has_map
15269 && similar_chars(slang, gc, bc))
15270 score += SCORE_SIMILAR;
15271 else
15272 score += SCORE_SUBST;
15273 }
15274
15275 if (score < minscore)
15276 {
15277 /* Do the substitution. */
15278 ++gi;
15279 ++bi;
15280 continue;
15281 }
15282 }
15283pop:
15284 /*
15285 * Get here to try the next alternative, pop it from the stack.
15286 */
15287 if (stackidx == 0) /* stack is empty, finished */
15288 break;
15289
15290 /* pop an item from the stack */
15291 --stackidx;
15292 gi = stack[stackidx].goodi;
15293 bi = stack[stackidx].badi;
15294 score = stack[stackidx].score;
15295 }
15296
15297 /* When the score goes over "limit" it may actually be much higher.
15298 * Return a very large number to avoid going below the limit when giving a
15299 * bonus. */
15300 if (minscore > limit)
15301 return SCORE_MAXMAX;
15302 return minscore;
15303}
15304
15305#ifdef FEAT_MBYTE
15306/*
15307 * Multi-byte version of spell_edit_score_limit().
15308 * Keep it in sync with the above!
15309 */
15310 static int
15311spell_edit_score_limit_w(slang, badword, goodword, limit)
15312 slang_T *slang;
15313 char_u *badword;
15314 char_u *goodword;
15315 int limit;
15316{
15317 limitscore_T stack[10]; /* allow for over 3 * 2 edits */
15318 int stackidx;
15319 int bi, gi;
15320 int bi2, gi2;
15321 int bc, gc;
15322 int score;
15323 int score_off;
15324 int minscore;
15325 int round;
15326 char_u *p;
15327 int wbadword[MAXWLEN];
15328 int wgoodword[MAXWLEN];
15329
15330 /* Get the characters from the multi-byte strings and put them in an
15331 * int array for easy access. */
15332 bi = 0;
15333 for (p = badword; *p != NUL; )
15334 wbadword[bi++] = mb_cptr2char_adv(&p);
15335 wbadword[bi++] = 0;
15336 gi = 0;
15337 for (p = goodword; *p != NUL; )
15338 wgoodword[gi++] = mb_cptr2char_adv(&p);
15339 wgoodword[gi++] = 0;
15340
15341 /*
15342 * The idea is to go from start to end over the words. So long as
15343 * characters are equal just continue, this always gives the lowest score.
15344 * When there is a difference try several alternatives. Each alternative
15345 * increases "score" for the edit distance. Some of the alternatives are
15346 * pushed unto a stack and tried later, some are tried right away. At the
15347 * end of the word the score for one alternative is known. The lowest
15348 * possible score is stored in "minscore".
15349 */
15350 stackidx = 0;
15351 bi = 0;
15352 gi = 0;
15353 score = 0;
15354 minscore = limit + 1;
15355
15356 for (;;)
15357 {
15358 /* Skip over an equal part, score remains the same. */
15359 for (;;)
15360 {
15361 bc = wbadword[bi];
15362 gc = wgoodword[gi];
15363
15364 if (bc != gc) /* stop at a char that's different */
15365 break;
15366 if (bc == NUL) /* both words end */
15367 {
15368 if (score < minscore)
15369 minscore = score;
15370 goto pop; /* do next alternative */
15371 }
15372 ++bi;
15373 ++gi;
15374 }
15375
15376 if (gc == NUL) /* goodword ends, delete badword chars */
15377 {
15378 do
15379 {
15380 if ((score += SCORE_DEL) >= minscore)
15381 goto pop; /* do next alternative */
15382 } while (wbadword[++bi] != NUL);
15383 minscore = score;
15384 }
15385 else if (bc == NUL) /* badword ends, insert badword chars */
15386 {
15387 do
15388 {
15389 if ((score += SCORE_INS) >= minscore)
15390 goto pop; /* do next alternative */
15391 } while (wgoodword[++gi] != NUL);
15392 minscore = score;
15393 }
15394 else /* both words continue */
15395 {
15396 /* If not close to the limit, perform a change. Only try changes
15397 * that may lead to a lower score than "minscore".
15398 * round 0: try deleting a char from badword
15399 * round 1: try inserting a char in badword */
15400 for (round = 0; round <= 1; ++round)
15401 {
15402 score_off = score + (round == 0 ? SCORE_DEL : SCORE_INS);
15403 if (score_off < minscore)
15404 {
15405 if (score_off + SCORE_EDIT_MIN >= minscore)
15406 {
15407 /* Near the limit, rest of the words must match. We
15408 * can check that right now, no need to push an item
15409 * onto the stack. */
15410 bi2 = bi + 1 - round;
15411 gi2 = gi + round;
15412 while (wgoodword[gi2] == wbadword[bi2])
15413 {
15414 if (wgoodword[gi2] == NUL)
15415 {
15416 minscore = score_off;
15417 break;
15418 }
15419 ++bi2;
15420 ++gi2;
15421 }
15422 }
15423 else
15424 {
15425 /* try deleting a character from badword later */
15426 stack[stackidx].badi = bi + 1 - round;
15427 stack[stackidx].goodi = gi + round;
15428 stack[stackidx].score = score_off;
15429 ++stackidx;
15430 }
15431 }
15432 }
15433
15434 if (score + SCORE_SWAP < minscore)
15435 {
15436 /* If swapping two characters makes a match then the
15437 * substitution is more expensive, thus there is no need to
15438 * try both. */
15439 if (gc == wbadword[bi + 1] && bc == wgoodword[gi + 1])
15440 {
15441 /* Swap two characters, that is: skip them. */
15442 gi += 2;
15443 bi += 2;
15444 score += SCORE_SWAP;
15445 continue;
15446 }
15447 }
15448
15449 /* Substitute one character for another which is the same
15450 * thing as deleting a character from both goodword and badword.
15451 * Use a better score when there is only a case difference. */
15452 if (SPELL_TOFOLD(bc) == SPELL_TOFOLD(gc))
15453 score += SCORE_ICASE;
15454 else
15455 {
15456 /* For a similar character use SCORE_SIMILAR. */
15457 if (slang != NULL
15458 && slang->sl_has_map
15459 && similar_chars(slang, gc, bc))
15460 score += SCORE_SIMILAR;
15461 else
15462 score += SCORE_SUBST;
15463 }
15464
15465 if (score < minscore)
15466 {
15467 /* Do the substitution. */
15468 ++gi;
15469 ++bi;
15470 continue;
15471 }
15472 }
15473pop:
15474 /*
15475 * Get here to try the next alternative, pop it from the stack.
15476 */
15477 if (stackidx == 0) /* stack is empty, finished */
15478 break;
15479
15480 /* pop an item from the stack */
15481 --stackidx;
15482 gi = stack[stackidx].goodi;
15483 bi = stack[stackidx].badi;
15484 score = stack[stackidx].score;
15485 }
15486
15487 /* When the score goes over "limit" it may actually be much higher.
15488 * Return a very large number to avoid going below the limit when giving a
15489 * bonus. */
15490 if (minscore > limit)
15491 return SCORE_MAXMAX;
15492 return minscore;
15493}
15494#endif
15495
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015496/*
15497 * ":spellinfo"
15498 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015499 void
15500ex_spellinfo(eap)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000015501 exarg_T *eap UNUSED;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015502{
15503 int lpi;
15504 langp_T *lp;
15505 char_u *p;
15506
15507 if (no_spell_checking(curwin))
15508 return;
15509
15510 msg_start();
Bram Moolenaar860cae12010-06-05 23:22:07 +020015511 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len && !got_int; ++lpi)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015512 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015513 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015514 msg_puts((char_u *)"file: ");
15515 msg_puts(lp->lp_slang->sl_fname);
15516 msg_putchar('\n');
15517 p = lp->lp_slang->sl_info;
15518 if (p != NULL)
15519 {
15520 msg_puts(p);
15521 msg_putchar('\n');
15522 }
15523 }
15524 msg_end();
15525}
15526
Bram Moolenaar4770d092006-01-12 23:22:24 +000015527#define DUMPFLAG_KEEPCASE 1 /* round 2: keep-case tree */
15528#define DUMPFLAG_COUNT 2 /* include word count */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015529#define DUMPFLAG_ICASE 4 /* ignore case when finding matches */
Bram Moolenaard0131a82006-03-04 21:46:13 +000015530#define DUMPFLAG_ONECAP 8 /* pattern starts with capital */
15531#define DUMPFLAG_ALLCAP 16 /* pattern is all capitals */
Bram Moolenaar4770d092006-01-12 23:22:24 +000015532
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015533/*
15534 * ":spelldump"
15535 */
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015536 void
15537ex_spelldump(eap)
15538 exarg_T *eap;
15539{
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015540 if (no_spell_checking(curwin))
15541 return;
15542
15543 /* Create a new empty buffer by splitting the window. */
15544 do_cmdline_cmd((char_u *)"new");
Bram Moolenaar860cae12010-06-05 23:22:07 +020015545 if (!bufempty() || !buf_valid(curbuf))
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015546 return;
15547
Bram Moolenaar860cae12010-06-05 23:22:07 +020015548 spell_dump_compl(NULL, 0, NULL, eap->forceit ? DUMPFLAG_COUNT : 0);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015549
15550 /* Delete the empty line that we started with. */
15551 if (curbuf->b_ml.ml_line_count > 1)
15552 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
15553
15554 redraw_later(NOT_VALID);
15555}
15556
15557/*
15558 * Go through all possible words and:
15559 * 1. When "pat" is NULL: dump a list of all words in the current buffer.
15560 * "ic" and "dir" are not used.
15561 * 2. When "pat" is not NULL: add matching words to insert mode completion.
15562 */
15563 void
Bram Moolenaar860cae12010-06-05 23:22:07 +020015564spell_dump_compl(pat, ic, dir, dumpflags_arg)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015565 char_u *pat; /* leading part of the word */
15566 int ic; /* ignore case */
15567 int *dir; /* direction for adding matches */
15568 int dumpflags_arg; /* DUMPFLAG_* */
15569{
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015570 langp_T *lp;
15571 slang_T *slang;
15572 idx_T arridx[MAXWLEN];
15573 int curi[MAXWLEN];
15574 char_u word[MAXWLEN];
15575 int c;
15576 char_u *byts;
15577 idx_T *idxs;
15578 linenr_T lnum = 0;
15579 int round;
15580 int depth;
15581 int n;
15582 int flags;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015583 char_u *region_names = NULL; /* region names being used */
15584 int do_region = TRUE; /* dump region names and numbers */
15585 char_u *p;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015586 int lpi;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015587 int dumpflags = dumpflags_arg;
15588 int patlen;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015589
Bram Moolenaard0131a82006-03-04 21:46:13 +000015590 /* When ignoring case or when the pattern starts with capital pass this on
15591 * to dump_word(). */
15592 if (pat != NULL)
15593 {
15594 if (ic)
15595 dumpflags |= DUMPFLAG_ICASE;
15596 else
15597 {
15598 n = captype(pat, NULL);
15599 if (n == WF_ONECAP)
15600 dumpflags |= DUMPFLAG_ONECAP;
15601 else if (n == WF_ALLCAP
15602#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015603 && (int)STRLEN(pat) > mb_ptr2len(pat)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015604#else
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015605 && (int)STRLEN(pat) > 1
Bram Moolenaard0131a82006-03-04 21:46:13 +000015606#endif
15607 )
15608 dumpflags |= DUMPFLAG_ALLCAP;
15609 }
15610 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015611
Bram Moolenaar7887d882005-07-01 22:33:52 +000015612 /* Find out if we can support regions: All languages must support the same
15613 * regions or none at all. */
Bram Moolenaar860cae12010-06-05 23:22:07 +020015614 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaar7887d882005-07-01 22:33:52 +000015615 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015616 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaar7887d882005-07-01 22:33:52 +000015617 p = lp->lp_slang->sl_regions;
15618 if (p[0] != 0)
15619 {
15620 if (region_names == NULL) /* first language with regions */
15621 region_names = p;
15622 else if (STRCMP(region_names, p) != 0)
15623 {
15624 do_region = FALSE; /* region names are different */
15625 break;
15626 }
15627 }
15628 }
15629
15630 if (do_region && region_names != NULL)
15631 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015632 if (pat == NULL)
15633 {
15634 vim_snprintf((char *)IObuff, IOSIZE, "/regions=%s", region_names);
15635 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15636 }
Bram Moolenaar7887d882005-07-01 22:33:52 +000015637 }
15638 else
15639 do_region = FALSE;
15640
15641 /*
15642 * Loop over all files loaded for the entries in 'spelllang'.
15643 */
Bram Moolenaar860cae12010-06-05 23:22:07 +020015644 for (lpi = 0; lpi < curwin->w_s->b_langp.ga_len; ++lpi)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015645 {
Bram Moolenaar860cae12010-06-05 23:22:07 +020015646 lp = LANGP_ENTRY(curwin->w_s->b_langp, lpi);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015647 slang = lp->lp_slang;
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015648 if (slang->sl_fbyts == NULL) /* reloading failed */
15649 continue;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015650
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015651 if (pat == NULL)
15652 {
15653 vim_snprintf((char *)IObuff, IOSIZE, "# file: %s", slang->sl_fname);
15654 ml_append(lnum++, IObuff, (colnr_T)0, FALSE);
15655 }
15656
15657 /* When matching with a pattern and there are no prefixes only use
15658 * parts of the tree that match "pat". */
15659 if (pat != NULL && slang->sl_pbyts == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015660 patlen = (int)STRLEN(pat);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015661 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015662 patlen = -1;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015663
15664 /* round 1: case-folded tree
15665 * round 2: keep-case tree */
15666 for (round = 1; round <= 2; ++round)
15667 {
15668 if (round == 1)
15669 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015670 dumpflags &= ~DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015671 byts = slang->sl_fbyts;
15672 idxs = slang->sl_fidxs;
15673 }
15674 else
15675 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015676 dumpflags |= DUMPFLAG_KEEPCASE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015677 byts = slang->sl_kbyts;
15678 idxs = slang->sl_kidxs;
15679 }
15680 if (byts == NULL)
15681 continue; /* array is empty */
15682
15683 depth = 0;
15684 arridx[0] = 0;
15685 curi[0] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015686 while (depth >= 0 && !got_int
15687 && (pat == NULL || !compl_interrupted))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015688 {
15689 if (curi[depth] > byts[arridx[depth]])
15690 {
15691 /* Done all bytes at this node, go up one level. */
15692 --depth;
15693 line_breakcheck();
Bram Moolenaara2031822006-03-07 22:29:51 +000015694 ins_compl_check_keys(50);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015695 }
15696 else
15697 {
15698 /* Do one more byte at this node. */
15699 n = arridx[depth] + curi[depth];
15700 ++curi[depth];
15701 c = byts[n];
15702 if (c == 0)
15703 {
15704 /* End of word, deal with the word.
15705 * Don't use keep-case words in the fold-case tree,
15706 * they will appear in the keep-case tree.
15707 * Only use the word when the region matches. */
15708 flags = (int)idxs[n];
15709 if ((round == 2 || (flags & WF_KEEPCAP) == 0)
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000015710 && (flags & WF_NEEDCOMP) == 0
Bram Moolenaar7887d882005-07-01 22:33:52 +000015711 && (do_region
15712 || (flags & WF_REGION) == 0
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015713 || (((unsigned)flags >> 16)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015714 & lp->lp_region) != 0))
15715 {
15716 word[depth] = NUL;
Bram Moolenaar7887d882005-07-01 22:33:52 +000015717 if (!do_region)
15718 flags &= ~WF_REGION;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015719
15720 /* Dump the basic word if there is no prefix or
15721 * when it's the first one. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015722 c = (unsigned)flags >> 24;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015723 if (c == 0 || curi[depth] == 2)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015724 {
15725 dump_word(slang, word, pat, dir,
15726 dumpflags, flags, lnum);
15727 if (pat == NULL)
15728 ++lnum;
15729 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015730
15731 /* Apply the prefix, if there is one. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +000015732 if (c != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015733 lnum = dump_prefixes(slang, word, pat, dir,
15734 dumpflags, flags, lnum);
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015735 }
15736 }
15737 else
15738 {
15739 /* Normal char, go one level deeper. */
15740 word[depth++] = c;
15741 arridx[depth] = idxs[n];
15742 curi[depth] = 1;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015743
15744 /* Check if this characters matches with the pattern.
15745 * If not skip the whole tree below it.
Bram Moolenaard0131a82006-03-04 21:46:13 +000015746 * Always ignore case here, dump_word() will check
15747 * proper case later. This isn't exactly right when
15748 * length changes for multi-byte characters with
15749 * ignore case... */
15750 if (depth <= patlen
15751 && MB_STRNICMP(word, pat, depth) != 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015752 --depth;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015753 }
15754 }
15755 }
15756 }
15757 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015758}
15759
15760/*
15761 * Dump one word: apply case modifications and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015762 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015763 */
15764 static void
Bram Moolenaard0131a82006-03-04 21:46:13 +000015765dump_word(slang, word, pat, dir, dumpflags, wordflags, lnum)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015766 slang_T *slang;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015767 char_u *word;
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015768 char_u *pat;
15769 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015770 int dumpflags;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015771 int wordflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015772 linenr_T lnum;
15773{
15774 int keepcap = FALSE;
15775 char_u *p;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015776 char_u *tw;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015777 char_u cword[MAXWLEN];
Bram Moolenaar7887d882005-07-01 22:33:52 +000015778 char_u badword[MAXWLEN + 10];
15779 int i;
Bram Moolenaard0131a82006-03-04 21:46:13 +000015780 int flags = wordflags;
15781
15782 if (dumpflags & DUMPFLAG_ONECAP)
15783 flags |= WF_ONECAP;
15784 if (dumpflags & DUMPFLAG_ALLCAP)
15785 flags |= WF_ALLCAP;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015786
Bram Moolenaar4770d092006-01-12 23:22:24 +000015787 if ((dumpflags & DUMPFLAG_KEEPCASE) == 0 && (flags & WF_CAPMASK) != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015788 {
15789 /* Need to fix case according to "flags". */
15790 make_case_word(word, cword, flags);
15791 p = cword;
15792 }
15793 else
15794 {
15795 p = word;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015796 if ((dumpflags & DUMPFLAG_KEEPCASE)
15797 && ((captype(word, NULL) & WF_KEEPCAP) == 0
Bram Moolenaar0dc065e2005-07-04 22:49:24 +000015798 || (flags & WF_FIXCAP) != 0))
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015799 keepcap = TRUE;
15800 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015801 tw = p;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015802
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015803 if (pat == NULL)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015804 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015805 /* Add flags and regions after a slash. */
15806 if ((flags & (WF_BANNED | WF_RARE | WF_REGION)) || keepcap)
Bram Moolenaar4770d092006-01-12 23:22:24 +000015807 {
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015808 STRCPY(badword, p);
15809 STRCAT(badword, "/");
15810 if (keepcap)
15811 STRCAT(badword, "=");
15812 if (flags & WF_BANNED)
15813 STRCAT(badword, "!");
15814 else if (flags & WF_RARE)
15815 STRCAT(badword, "?");
15816 if (flags & WF_REGION)
15817 for (i = 0; i < 7; ++i)
15818 if (flags & (0x10000 << i))
15819 sprintf((char *)badword + STRLEN(badword), "%d", i + 1);
15820 p = badword;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015821 }
Bram Moolenaar4770d092006-01-12 23:22:24 +000015822
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015823 if (dumpflags & DUMPFLAG_COUNT)
15824 {
15825 hashitem_T *hi;
15826
15827 /* Include the word count for ":spelldump!". */
15828 hi = hash_find(&slang->sl_wordcount, tw);
15829 if (!HASHITEM_EMPTY(hi))
15830 {
15831 vim_snprintf((char *)IObuff, IOSIZE, "%s\t%d",
15832 tw, HI2WC(hi)->wc_count);
15833 p = IObuff;
15834 }
15835 }
15836
15837 ml_append(lnum, p, (colnr_T)0, FALSE);
15838 }
Bram Moolenaard0131a82006-03-04 21:46:13 +000015839 else if (((dumpflags & DUMPFLAG_ICASE)
15840 ? MB_STRNICMP(p, pat, STRLEN(pat)) == 0
15841 : STRNCMP(p, pat, STRLEN(pat)) == 0)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015842 && ins_compl_add_infercase(p, (int)STRLEN(p),
Bram Moolenaare8c3a142006-08-29 14:30:35 +000015843 p_ic, NULL, *dir, 0) == OK)
Bram Moolenaard0131a82006-03-04 21:46:13 +000015844 /* if dir was BACKWARD then honor it just once */
15845 *dir = FORWARD;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015846}
15847
15848/*
Bram Moolenaara1ba8112005-06-28 23:23:32 +000015849 * For ":spelldump": Find matching prefixes for "word". Prepend each to
15850 * "word" and append a line to the buffer.
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015851 * When "lnum" is zero add insert mode completion.
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015852 * Return the updated line number.
15853 */
15854 static linenr_T
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015855dump_prefixes(slang, word, pat, dir, dumpflags, flags, startlnum)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015856 slang_T *slang;
15857 char_u *word; /* case-folded word */
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015858 char_u *pat;
15859 int *dir;
Bram Moolenaar4770d092006-01-12 23:22:24 +000015860 int dumpflags;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015861 int flags; /* flags with prefix ID */
15862 linenr_T startlnum;
15863{
15864 idx_T arridx[MAXWLEN];
15865 int curi[MAXWLEN];
15866 char_u prefix[MAXWLEN];
Bram Moolenaar53805d12005-08-01 07:08:33 +000015867 char_u word_up[MAXWLEN];
15868 int has_word_up = FALSE;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015869 int c;
15870 char_u *byts;
15871 idx_T *idxs;
15872 linenr_T lnum = startlnum;
15873 int depth;
15874 int n;
15875 int len;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015876 int i;
15877
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015878 /* If the word starts with a lower-case letter make the word with an
Bram Moolenaar53805d12005-08-01 07:08:33 +000015879 * upper-case letter in word_up[]. */
15880 c = PTR2CHAR(word);
15881 if (SPELL_TOUPPER(c) != c)
15882 {
15883 onecap_copy(word, word_up, TRUE);
15884 has_word_up = TRUE;
15885 }
15886
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015887 byts = slang->sl_pbyts;
15888 idxs = slang->sl_pidxs;
15889 if (byts != NULL) /* array not is empty */
15890 {
15891 /*
15892 * Loop over all prefixes, building them byte-by-byte in prefix[].
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015893 * When at the end of a prefix check that it supports "flags".
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015894 */
15895 depth = 0;
15896 arridx[0] = 0;
15897 curi[0] = 1;
15898 while (depth >= 0 && !got_int)
15899 {
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015900 n = arridx[depth];
15901 len = byts[n];
15902 if (curi[depth] > len)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015903 {
15904 /* Done all bytes at this node, go up one level. */
15905 --depth;
15906 line_breakcheck();
15907 }
15908 else
15909 {
15910 /* Do one more byte at this node. */
Bram Moolenaardfb9ac02005-07-05 21:36:03 +000015911 n += curi[depth];
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015912 ++curi[depth];
15913 c = byts[n];
15914 if (c == 0)
15915 {
15916 /* End of prefix, find out how many IDs there are. */
15917 for (i = 1; i < len; ++i)
15918 if (byts[n + i] != 0)
15919 break;
15920 curi[depth] += i - 1;
15921
Bram Moolenaar53805d12005-08-01 07:08:33 +000015922 c = valid_word_prefix(i, n, flags, word, slang, FALSE);
15923 if (c != 0)
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015924 {
Bram Moolenaar9c96f592005-06-30 21:52:39 +000015925 vim_strncpy(prefix + depth, word, MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015926 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015927 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015928 : flags, lnum);
15929 if (lnum != 0)
15930 ++lnum;
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015931 }
Bram Moolenaar53805d12005-08-01 07:08:33 +000015932
15933 /* Check for prefix that matches the word when the
15934 * first letter is upper-case, but only if the prefix has
15935 * a condition. */
15936 if (has_word_up)
15937 {
15938 c = valid_word_prefix(i, n, flags, word_up, slang,
15939 TRUE);
15940 if (c != 0)
15941 {
15942 vim_strncpy(prefix + depth, word_up,
15943 MAXWLEN - depth - 1);
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015944 dump_word(slang, prefix, pat, dir, dumpflags,
Bram Moolenaar53805d12005-08-01 07:08:33 +000015945 (c & WF_RAREPFX) ? (flags | WF_RARE)
Bram Moolenaarb475fb92006-03-02 22:40:52 +000015946 : flags, lnum);
15947 if (lnum != 0)
15948 ++lnum;
Bram Moolenaar53805d12005-08-01 07:08:33 +000015949 }
15950 }
Bram Moolenaarf417f2b2005-06-23 22:29:21 +000015951 }
15952 else
15953 {
15954 /* Normal char, go one level deeper. */
15955 prefix[depth++] = c;
15956 arridx[depth] = idxs[n];
15957 curi[depth] = 1;
15958 }
15959 }
15960 }
15961 }
15962
15963 return lnum;
15964}
15965
Bram Moolenaar95529562005-08-25 21:21:38 +000015966/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015967 * Move "p" to the end of word "start".
15968 * Uses the spell-checking word characters.
Bram Moolenaar95529562005-08-25 21:21:38 +000015969 */
15970 char_u *
Bram Moolenaar860cae12010-06-05 23:22:07 +020015971spell_to_word_end(start, win)
Bram Moolenaar95529562005-08-25 21:21:38 +000015972 char_u *start;
Bram Moolenaar860cae12010-06-05 23:22:07 +020015973 win_T *win;
Bram Moolenaar95529562005-08-25 21:21:38 +000015974{
15975 char_u *p = start;
15976
Bram Moolenaar860cae12010-06-05 23:22:07 +020015977 while (*p != NUL && spell_iswordp(p, win))
Bram Moolenaar95529562005-08-25 21:21:38 +000015978 mb_ptr_adv(p);
15979 return p;
15980}
15981
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015982#if defined(FEAT_INS_EXPAND) || defined(PROTO)
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015983/*
Bram Moolenaara40ceaf2006-01-13 22:35:40 +000015984 * For Insert mode completion CTRL-X s:
15985 * Find start of the word in front of column "startcol".
15986 * We don't check if it is badly spelled, with completion we can only change
15987 * the word in front of the cursor.
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015988 * Returns the column number of the word.
15989 */
15990 int
15991spell_word_start(startcol)
15992 int startcol;
15993{
15994 char_u *line;
15995 char_u *p;
15996 int col = 0;
15997
Bram Moolenaar95529562005-08-25 21:21:38 +000015998 if (no_spell_checking(curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000015999 return startcol;
16000
16001 /* Find a word character before "startcol". */
16002 line = ml_get_curline();
16003 for (p = line + startcol; p > line; )
16004 {
16005 mb_ptr_back(line, p);
16006 if (spell_iswordp_nmw(p))
16007 break;
16008 }
16009
16010 /* Go back to start of the word. */
16011 while (p > line)
16012 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016013 col = (int)(p - line);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016014 mb_ptr_back(line, p);
Bram Moolenaar860cae12010-06-05 23:22:07 +020016015 if (!spell_iswordp(p, curwin))
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016016 break;
16017 col = 0;
16018 }
16019
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016020 return col;
16021}
16022
16023/*
Bram Moolenaar4effc802005-09-30 21:12:02 +000016024 * Need to check for 'spellcapcheck' now, the word is removed before
16025 * expand_spelling() is called. Therefore the ugly global variable.
16026 */
16027static int spell_expand_need_cap;
16028
16029 void
16030spell_expand_check_cap(col)
16031 colnr_T col;
16032{
16033 spell_expand_need_cap = check_need_cap(curwin->w_cursor.lnum, col);
16034}
16035
16036/*
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016037 * Get list of spelling suggestions.
16038 * Used for Insert mode completion CTRL-X ?.
16039 * Returns the number of matches. The matches are in "matchp[]", array of
16040 * allocated strings.
16041 */
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016042 int
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000016043expand_spelling(lnum, pat, matchp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +000016044 linenr_T lnum UNUSED;
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016045 char_u *pat;
16046 char_u ***matchp;
16047{
16048 garray_T ga;
16049
Bram Moolenaar4770d092006-01-12 23:22:24 +000016050 spell_suggest_list(&ga, pat, 100, spell_expand_need_cap, TRUE);
Bram Moolenaar8b59de92005-08-11 19:59:29 +000016051 *matchp = ga.ga_data;
16052 return ga.ga_len;
16053}
16054#endif
16055
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000016056#endif /* FEAT_SPELL */